forked from hajimehoshi/ebiten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genevents.go
401 lines (374 loc) · 11.1 KB
/
genevents.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright 2019 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build ignore
// +build ignore
package main
import (
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
type Event struct {
Comment string
Members []Member
}
func (e *Event) Name() string {
return strings.SplitN(e.Comment, " ", 2)[0]
}
type Member struct {
Comment string
Type string
}
func (m *Member) Name() string {
return strings.SplitN(m.Comment, " ", 2)[0]
}
var (
membersKeyboardKey = []Member{
{
Comment: "Key is the key code of the key pressed or released.",
Type: "Key",
},
{
Comment: "Modifier is the logical-or value of the modifiers pressed together with the key.",
Type: "Modifier",
},
}
membersGamepadButton = []Member{
{
Comment: "ID represents which gamepad caused the event.",
Type: "int",
},
{
Comment: "Button is the button that was pressed on the game pad.",
Type: "int",
},
{
Comment: "Pressure is the pressure that is applied to the gamepad button. It varies between 0.0 for not pressed, and 1.0 for completely pressed.",
Type: "float32",
},
}
membersMouseButton = []Member{
{
Comment: "X is the X position of the mouse pointer. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "Y is the Y position of the mouse pointer. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "Button is the button on the mouse that was pressed. TODO: this should change later from an int to an enumeration type.",
Type: "int",
},
{
Comment: "Pressure is the pressure applied on the mouse button. It varies between 0.0 for not pressed, and 1.0 for completely pressed.",
Type: "float32",
},
}
membersMouseEnter = []Member{
{
Comment: "X is the X position of the mouse pointer. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "Y is the Y position of the mouse pointer. This value is expressed in device independent pixels.",
Type: "float32",
},
}
membersTouch = []Member{
{
Comment: "ID identifies the touch that caused the touch event.",
Type: "int",
},
{
Comment: "X is the X position of the touch. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "Y is the Y position of the touch. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "DeltaX is the change in X since last touch event. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "Deltay is the change in Y since last touch event. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "Pressure of applied touch. It varies between 0.0 for not pressed, and 1.0 for completely pressed.",
Type: "float32",
},
{
Comment: "Primary represents whether the touch event is the primary touch or not. If it is true, then it is a primary touch. If it is false then it is not.",
Type: "bool",
},
}
events = []Event{
{
Comment: "KeyboardKeyCharacter is an event that occurs when a character is actually typed on the keyboard. This may be provided by an input method.",
Members: []Member{
{
Comment: "Key is the key code of the key typed.",
Type: "Key",
},
{
Comment: "Modifier is the logical-or value of the modifiers pressed together with the key.",
Type: "Modifier",
},
{
Comment: "Character is the character that was typed.",
Type: "rune",
},
},
},
{
Comment: "KeyboardKeyDown is an event that occurs when a key is pressed on the keyboard.",
Members: membersKeyboardKey,
},
{
Comment: "KeyboardKeyUp is an event that occurs when a key is released on the keyboard.",
Members: membersKeyboardKey,
},
{
Comment: "GamepadAxis is for event where an axis on a gamepad changes.",
Members: []Member{
{
Comment: "ID represents which gamepad caused the event.",
Type: "int",
},
{
Comment: "Axis is the axis of the game pad that changed position.",
Type: "int",
},
{
Comment: "Position is the position of the axis after the change. It varies between -1.0 and 1.0.",
Type: "float32",
},
},
},
{
Comment: "GamepadButtonDown is a gamepad button press event.",
Members: membersGamepadButton,
},
{
Comment: "GamepadButtonUp is a gamepad button release event.",
Members: membersGamepadButton,
},
{
Comment: "GamepadAttach happens when a new gamepad is attached.",
Members: []Member{
{
Comment: "ID represents which gamepad caused the event.",
Type: "int",
},
{
Comment: "Axes represents the amount of axes the gamepad has.",
Type: "int",
},
{
Comment: "Buttons represents the amount of buttons the gamepad has.",
Type: "int",
},
},
},
{
Comment: "GamepadDetach happens when a gamepad is detached.",
Members: []Member{
{
Comment: "ID represents which gamepad caused the event.",
Type: "int",
},
},
},
{
Comment: "MouseMove is a mouse movement event.",
Members: []Member{
{
Comment: "X is the X position of the mouse pointer. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "Y is the Y position of the mouse pointer. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "DeltaX is the change in X since the last MouseMove event. This value is expressed in device independent pixels.",
Type: "float32",
},
{
Comment: "DeltaY is the change in Y since the last MouseMove event. This value is expressed in device independent pixels.",
Type: "float32",
},
},
},
{
Comment: "MouseWheel is a mouse wheel event.",
Members: []Member{
{
Comment: "X is the X position of the mouse wheel. This value is expressed in arbitrary units. It increases when the mouse wheel is scrolled downwards, and decreases when the mouse is scrolled upwards.",
Type: "float32",
},
{
Comment: "Y is the Y position of the mouse wheel. This value is expressed in arbitrary units. It increases when the mouse wheel is scrolled to the right, and decreases when the mouse is scrolled to the left.",
Type: "float32",
},
{
Comment: "DeltaX is the change in X since the last MouseWheel event. This value is expressed in arbitrary units. It is positive when the mouse wheel is scrolled downwards, and negative when the mouse is scrolled upwards.",
Type: "float32",
},
{
Comment: "DeltaY is the change in Y since the last MouseWheel event. This value is expressed in arbitrary units. It is positive when the mouse wheel is scrolled to the right, and negative when the mouse is scrolled to the left.",
Type: "float32",
},
},
},
{
Comment: "MouseButtonDown is a mouse button press event.",
Members: membersMouseButton,
},
{
Comment: "MouseButtonUp is a mouse button release event.",
Members: membersMouseButton,
},
{
Comment: "MouseEnter occurs when the mouse enters the view window.",
Members: membersMouseEnter,
},
{
Comment: "MouseLeave occurs when the mouse leaves the view window.",
Members: membersMouseEnter,
},
{
Comment: "TouchBegin occurs when a touch begins.",
Members: membersTouch,
},
{
Comment: "TouchMove occurs when a touch moved, or in other words, is dragged.",
Members: membersTouch,
},
{
Comment: "TouchEnd occurs when a touch ends.",
Members: membersTouch,
},
{
Comment: "TouchCancel occurs when a touch is canceled. This can happen in various situations, depending on the underlying platform, for example when the application loses focus.",
Members: []Member{
{
Comment: "ID identifies the touch that caused the touch event.",
Type: "int",
},
},
},
{
Comment: "ViewUpdate occurs when the application is ready to update the next frame on the view port.",
},
{
Comment: "ViewSize occurs when the size of the application's view port changes.",
Members: []Member{
{
Comment: "Width is the width of the view. This value is expressed in device independent pixels.",
Type: "int",
},
{
Comment: "Height is the height of the view. This value is expressed in device independent pixels.",
Type: "int",
},
},
},
}
)
const (
license = `// Copyright 2013 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
`
doNotEdit = "// Code generated by genevents.go using 'go generate'. DO NOT EDIT."
)
var eventTmpl = template.Must(template.New("event.go").Parse(`{{.License}}
{{.DoNotEdit}}
package {{.Package}}
type Event interface{}
{{range .Events}}// {{.Comment}}
type {{.Name}} struct {
{{range .Members}} // {{.Comment}}
{{.Name}} {{.Type}}
{{end}}
}
{{end}}
`))
var chanTmpl = template.Must(template.New("chan.go").Parse(`{{.License}}
{{.DoNotEdit}}
package {{.Package}}
import (
"fmt"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
)
func convertCh(driverCh chan driver.Event) (chan Event) {
ch := make(chan Event)
go func() {
defer close(ch)
for v := range driverCh {
switch v := v.(type) {
{{range .Events}}case driver.{{.Name}}:
ch <- {{.Name}}(v)
{{end}}default:
panic(fmt.Sprintf("event: unknown event: %v", v))
}
}
}()
return ch
}
`))
func main() {
for path, tmpl := range map[string]*template.Template{
filepath.Join("event", "event.go"): eventTmpl,
filepath.Join("event", "chan.go"): chanTmpl,
filepath.Join("internal", "driver", "event.go"): eventTmpl,
} {
f, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
tokens := strings.Split(path, string(filepath.Separator))
pkg := tokens[len(tokens)-2]
if err := tmpl.Execute(f, struct {
License string
DoNotEdit string
Package string
Events []Event
}{
License: license,
DoNotEdit: doNotEdit,
Package: pkg,
Events: events,
}); err != nil {
log.Fatal(err)
}
}
}