/
kc.go
214 lines (200 loc) · 8.46 KB
/
kc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright © 2013-2018 Galvanized Logic Inc.
// Use is governed by a BSD-style license found in the LICENSE file.
package main
import (
"log"
"github.com/gazed/vu"
)
// kc explores treating the keyboard as a controller.
// It assigns each keyboard key a unique symbol.
//
// Treating the keyboard like a complicated console controller
// basically means ignoring its ability to input text. Overall
// a simplification over regular keyboards, but having alot more
// potential controls than a console controller.
//
// CONTROLS:
// key : highlight key press
// mouse : highlight mouse click
func kc() {
defer catchErrors()
if err := vu.Run(&kctag{}); err != nil {
log.Printf("kc: error starting engine %s", err)
}
}
// Globally unique "tag" that encapsulates example specific data.
type kctag struct {
ui *vu.Ent // 2D user interface.
focus *vu.Ent // Hilights first pressed key.
positions map[int]pos // Screen position for each key.
}
// Create is the startup asset creation.
func (kc *kctag) Create(eng vu.Eng, s *vu.State) {
eng.Set(vu.Title("Keyboard Controller"), vu.Size(200, 200, 900, 400))
eng.Set(vu.Color(0.45, 0.45, 0.45, 1))
// create the 2D overlay
kc.ui = eng.AddScene().SetUI()
kc.ui.Cam().SetClip(0, 10)
kc.positions = kc.keyPositions()
// Create the keyboard image.
kb := kc.ui.AddPart().SetScale(900, 255, 0).SetAt(450, 100+85, 0)
kb.MakeModel("textured", "msh:icon", "tex:keyboard")
// Pressed key focus
kc.focus = kc.ui.AddPart().SetScale(50, 50, 0)
kc.focus.MakeModel("textured", "msh:icon", "tex:particle")
// Place the key symbols over the keys.
for code, key := range kc.positions { // map key is key code, map value is key struct
if char := vu.Symbol(code); char > 0 {
cx, cy := key.location()
letter := kc.ui.AddPart().SetAt(cx, cy, 0)
letter.MakeLabel("labeled", "lucidiaSu18").SetStr(string(char))
letter.SetColor(0, 0, 0)
}
}
}
// Update is the regular engine callback.
func (kc *kctag) Update(eng vu.Eng, in *vu.Input, s *vu.State) {
kc.focus.Cull(true)
for press, down := range in.Down {
// hilight the first pressed key.
if down > 0 {
position := kc.positions[press]
cx, cy := position.location()
kc.focus.SetAt(cx+6, cy+10, 0)
kc.focus.Cull(false)
}
break
}
}
// Position the keys on the keyboard image.
func (kc *kctag) keyPositions() map[int]pos {
return map[int]pos{
vu.K0: {col: 9, row: 4, xoff: 0.8, yoff: 0.0},
vu.K1: {col: 1, row: 4, xoff: 0.0, yoff: 0.0},
vu.K2: {col: 2, row: 4, xoff: 0.0, yoff: 0.0},
vu.K3: {col: 3, row: 4, xoff: 0.0, yoff: 0.0},
vu.K4: {col: 4, row: 4, xoff: 0.0, yoff: 0.0},
vu.K5: {col: 5, row: 4, xoff: 0.0, yoff: 0.0},
vu.K6: {col: 5, row: 4, xoff: 0.9, yoff: 0.0},
vu.K7: {col: 6, row: 4, xoff: 0.8, yoff: 0.0},
vu.K8: {col: 7, row: 4, xoff: 0.8, yoff: 0.0},
vu.K9: {col: 8, row: 4, xoff: 0.8, yoff: 0.0},
vu.KA: {col: 1, row: 2, xoff: 0.9, yoff: 0.0},
vu.KB: {col: 6, row: 1, xoff: 0.1, yoff: 0.0},
vu.KC: {col: 4, row: 1, xoff: 0.2, yoff: 0.0},
vu.KD: {col: 3, row: 2, xoff: 0.8, yoff: 0.0},
vu.KE: {col: 3, row: 3, xoff: 0.5, yoff: 0.0},
vu.KF: {col: 4, row: 2, xoff: 0.7, yoff: 0.0},
vu.KG: {col: 5, row: 2, xoff: 0.6, yoff: 0.0},
vu.KH: {col: 6, row: 2, xoff: 0.5, yoff: 0.0},
vu.KI: {col: 8, row: 3, xoff: 0.3, yoff: 0.0},
vu.KJ: {col: 7, row: 2, xoff: 0.5, yoff: 0.0},
vu.KK: {col: 8, row: 2, xoff: 0.5, yoff: 0.0},
vu.KL: {col: 9, row: 2, xoff: 0.5, yoff: 0.0},
vu.KM: {col: 8, row: 1, xoff: 0.0, yoff: 0.0},
vu.KN: {col: 7, row: 1, xoff: 0.0, yoff: 0.0},
vu.KO: {col: 9, row: 3, xoff: 0.1, yoff: 0.0},
vu.KP: {col: 10, row: 3, xoff: 0.1, yoff: 0.0},
vu.KQ: {col: 1, row: 3, xoff: 0.6, yoff: 0.0},
vu.KR: {col: 4, row: 3, xoff: 0.5, yoff: 0.0},
vu.KS: {col: 2, row: 2, xoff: 0.8, yoff: 0.0},
vu.KT: {col: 5, row: 3, xoff: 0.4, yoff: 0.0},
vu.KU: {col: 7, row: 3, xoff: 0.2, yoff: 0.0},
vu.KV: {col: 5, row: 1, xoff: 0.1, yoff: 0.0},
vu.KW: {col: 2, row: 3, xoff: 0.5, yoff: 0.0},
vu.KX: {col: 3, row: 1, xoff: 0.3, yoff: 0.0},
vu.KY: {col: 6, row: 3, xoff: 0.3, yoff: 0.0},
vu.KZ: {col: 2, row: 1, xoff: 0.2, yoff: 0.0},
vu.KEqual: {col: 11, row: 4, xoff: 0.6, yoff: 0.0},
vu.KMinus: {col: 10, row: 4, xoff: 0.7, yoff: 0.0},
vu.KRBkt: {col: 12, row: 3, xoff: 0.1, yoff: 0.0},
vu.KLBkt: {col: 11, row: 3, xoff: 0.1, yoff: 0.0},
vu.KQt: {col: 11, row: 2, xoff: 0.5, yoff: 0.0},
vu.KSemi: {col: 10, row: 2, xoff: 0.5, yoff: 0.0},
vu.KBSl: {col: 13, row: 3, xoff: 0.0, yoff: 0.0},
vu.KComma: {col: 9, row: 1, xoff: 0.0, yoff: 0.0},
vu.KSlash: {col: 11, row: 1, xoff: 0.0, yoff: 0.0},
vu.KDot: {col: 10, row: 1, xoff: 0.0, yoff: 0.0},
vu.KGrave: {col: 0, row: 4, xoff: 0.0, yoff: 0.0},
vu.KRet: {col: 12, row: 2, xoff: 0.5, yoff: 0.0},
vu.KTab: {col: 0, row: 3, xoff: 0.0, yoff: 0.0},
vu.KSpace: {col: 7, row: 0, xoff: 0.0, yoff: 0.0},
vu.KDel: {col: 12, row: 4, xoff: 0.6, yoff: 0.0},
vu.KEsc: {col: 0, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF1: {col: 1, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF2: {col: 2, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF3: {col: 3, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF4: {col: 4, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF5: {col: 5, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF6: {col: 6, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF7: {col: 7, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF8: {col: 8, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF9: {col: 9, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF10: {col: 10, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF11: {col: 11, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF12: {col: 12, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF13: {col: 14, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF14: {col: 15, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF15: {col: 16, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF16: {col: 17, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF17: {col: 18, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF18: {col: 19, row: 5, xoff: 0.0, yoff: 0.0},
vu.KF19: {col: 20, row: 5, xoff: 0.0, yoff: 0.0},
vu.KHome: {col: 15, row: 4, xoff: 0.0, yoff: 0.0},
vu.KPgUp: {col: 16, row: 4, xoff: 0.0, yoff: 0.0},
vu.KFDel: {col: 14, row: 3, xoff: 0.0, yoff: 0.0},
vu.KEnd: {col: 15, row: 3, xoff: 0.0, yoff: 0.0},
vu.KPgDn: {col: 16, row: 3, xoff: 0.0, yoff: 0.0},
vu.KLa: {col: 14, row: 0, xoff: 0.0, yoff: 0.0},
vu.KRa: {col: 16, row: 0, xoff: 0.0, yoff: 0.0},
vu.KDa: {col: 15, row: 0, xoff: 0.0, yoff: 0.0},
vu.KUa: {col: 15, row: 1, xoff: 0.0, yoff: 0.0},
vu.KKpDot: {col: 19, row: 0, xoff: 0.0, yoff: 0.0},
vu.KKpMlt: {col: 20, row: 4, xoff: 0.0, yoff: 0.0},
vu.KKpAdd: {col: 20, row: 2, xoff: 0.0, yoff: 0.0},
vu.KKpClr: {col: 17, row: 4, xoff: 0.0, yoff: 0.0},
vu.KKpDiv: {col: 19, row: 4, xoff: 0.0, yoff: 0.0},
vu.KKpEnt: {col: 20, row: 0, xoff: 0.0, yoff: 0.0},
vu.KKpSub: {col: 20, row: 3, xoff: 0.0, yoff: 0.0},
vu.KKpEql: {col: 18, row: 4, xoff: 0.0, yoff: 0.0},
vu.KKp0: {col: 17, row: 0, xoff: 0.0, yoff: 0.0},
vu.KKp1: {col: 17, row: 1, xoff: 0.0, yoff: 0.0},
vu.KKp2: {col: 18, row: 1, xoff: 0.0, yoff: 0.0},
vu.KKp3: {col: 19, row: 1, xoff: 0.0, yoff: 0.0},
vu.KKp4: {col: 17, row: 2, xoff: 0.0, yoff: 0.0},
vu.KKp5: {col: 18, row: 2, xoff: 0.0, yoff: 0.0},
vu.KKp6: {col: 19, row: 2, xoff: 0.0, yoff: 0.0},
vu.KKp7: {col: 17, row: 3, xoff: 0.0, yoff: 0.0},
vu.KKp8: {col: 18, row: 3, xoff: 0.0, yoff: 0.0},
vu.KKp9: {col: 19, row: 3, xoff: 0.0, yoff: 0.0},
vu.KLm: {col: 1, row: 6, xoff: 0.0, yoff: 0.0},
vu.KMm: {col: 1, row: 6, xoff: 0.5, yoff: 0.0},
vu.KRm: {col: 2, row: 6, xoff: 0.0, yoff: 0.0},
vu.KCtl: {col: 0, row: 0, xoff: 0.0, yoff: 0.0},
vu.KFn: {col: 14, row: 4, xoff: 0.0, yoff: 0.0},
vu.KShift: {col: 0, row: 1, xoff: 0.0, yoff: 0.0},
vu.KCmd: {col: 3, row: 0, xoff: 0.0, yoff: 0.0},
vu.KAlt: {col: 1, row: 0, xoff: 0.6, yoff: 0.0},
}
}
// pos is used to locate each rune on the keyboard image.
type pos struct {
col int // keyboard position.. 21 columns.
row int // keyboard position.. 6 rows + 1 mouse row.
xoff float64 // column offustment.
yoff float64 // row offustment.
}
// location gives a positions x, y location in screen pixels.
func (p *pos) location() (x, y float64) {
xspan := 41.0
yspan := 38.0
x = 25.0 + (float64(p.col)+p.xoff)*xspan
y = 85.0 + (float64(p.row)+p.yoff)*yspan
if p.col > 13 {
x += 12 // first gap
}
if p.col > 16 {
x += 12 // second gap
}
return x, y
}