-
Notifications
You must be signed in to change notification settings - Fork 136
/
engo_js.go
483 lines (426 loc) · 13.1 KB
/
engo_js.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//+build js
package engo
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"math"
"os"
"strings"
"sync"
"syscall/js"
"time"
"github.com/EngoEngine/gl"
)
var (
// Gl is the current OpenGL context
Gl *gl.Context
devicePixelRatio float64
poll = make(map[int]bool)
pollLock sync.Mutex
mod = Modifier(0)
document = js.Global().Get("document")
window = js.Global().Get("window")
canvas js.Value
)
// CreateWindow creates a window with the specified parameters
func CreateWindow(title string, width, height int, fullscreen bool, msaa int) {
rafPolyfill()
CurrentBackEnd = BackEndWeb
canvas = document.Call("createElement", "canvas")
devicePixelRatio = js.Global().Get("devicePixelRatio").Float()
canvas.Set("width", int(float64(width)+0.5)) // Nearest non-negative int.
canvas.Set("height", int(float64(height)+0.5)) // Nearest non-negative int.
if document.Get("body").IsNull() {
document.Set("body", document.Call("createElement", "body"))
}
body := document.Get("body")
body.Get("style").Set("margin", "0")
body.Get("style").Set("padding", "0")
canvas.Call("setAttribute", "tabindex", 1)
canvas.Get("style").Set("outline", "none")
body.Call("appendChild", canvas)
document.Set("title", title)
Gl, _ = gl.NewContext(canvas, nil)
Gl.GetExtension("OES_texture_float")
gameWidth = float32(width)
gameHeight = float32(height)
windowWidth = WindowWidth()
windowHeight = WindowHeight()
ResizeXOffset = gameWidth - CanvasWidth()
ResizeYOffset = gameHeight - CanvasHeight()
canvas.Call("addEventListener", "keypress", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
// TODO: Not sure what to do here, come back
//ke := ev.(*dom.KeyboardEvent)
//responser.Type(rune(keyStates[Key(ke.KeyCode)]))
return nil
}))
canvas.Call("addEventListener", "keydown", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
event := args[0]
ke := event.Get("code") // TODO: preventdefault if it's a special key so it doesn't scroll when you press it!
if ke.Type() == js.TypeUndefined {
kc := event.Get("keyCode").Int()
go func(i int) {
pollLock.Lock()
poll[i] = true
pollLock.Unlock()
}(kc)
k := Key(kc)
if k == KeyArrowUp || k == KeyArrowDown || k == KeyArrowLeft || k == KeyArrowRight || k == KeyTab || k == KeyBackspace || k == KeySpace {
event.Call("preventDefault")
}
return nil
}
k := jsStrToKey[ke.String()]
go func(i int) {
pollLock.Lock()
poll[i] = true
pollLock.Unlock()
}(int(k))
if k == KeyArrowUp || k == KeyArrowDown || k == KeyArrowLeft || k == KeyArrowRight || k == KeyTab || k == KeyBackspace || k == KeySpace {
event.Call("preventDefault")
}
char := event.Get("key").String()
if len(char) == 1 {
Mailbox.Dispatch(TextMessage{[]rune(char)[0]})
}
checkModifiers(event)
return nil
}))
canvas.Call("addEventListener", "keyup", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
event := args[0]
ke := event.Get("code")
if ke.Type() == js.TypeUndefined {
kc := event.Get("keyCode").Int()
go func(i int) {
pollLock.Lock()
poll[i] = false
pollLock.Unlock()
}(kc)
k := Key(kc)
if k == KeyArrowUp || k == KeyArrowDown || k == KeyArrowLeft || k == KeyArrowRight || k == KeyTab || k == KeyBackspace || k == KeySpace {
event.Call("preventDefault")
}
return nil
}
k := jsStrToKey[ke.String()]
go func(i int) {
pollLock.Lock()
poll[i] = false
pollLock.Unlock()
}(int(k))
if k == KeyArrowUp || k == KeyArrowDown || k == KeyArrowLeft || k == KeyArrowRight || k == KeyTab || k == KeyBackspace || k == KeySpace {
event.Call("preventDefault")
}
checkModifiers(event)
return nil
}))
canvas.Call("addEventListener", "mousemove", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
event := args[0]
mmX, mmY := event.Get("clientX").Int(), event.Get("clientY").Int()
Input.Mouse.X = float32(mmX) / opts.GlobalScale.X
Input.Mouse.Y = float32(mmY) / opts.GlobalScale.Y
return nil
}))
canvas.Call("addEventListener", "mousedown", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
event := args[0]
mmX, mmY := event.Get("clientX").Int(), event.Get("clientY").Int()
Input.Mouse.X = float32(mmX) / opts.GlobalScale.X
Input.Mouse.Y = float32(mmY) / opts.GlobalScale.Y
Input.Mouse.Action = Press
return nil
}))
canvas.Call("addEventListener", "mouseup", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
event := args[0]
mmX, mmY := event.Get("clientX").Int(), event.Get("clientY").Int()
Input.Mouse.X = float32(mmX) / opts.GlobalScale.X
Input.Mouse.Y = float32(mmY) / opts.GlobalScale.Y
Input.Mouse.Action = Release
return nil
}))
window.Call("addEventListener", "gamepaddisconnected", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
println("gamepad disconnected")
joy := args[0].Get("gamepad")
if joy.IsNull() {
return nil
}
Input.gamepads.mutex.Lock()
defer Input.gamepads.mutex.Unlock()
for _, gamepad := range Input.gamepads.gamepads {
if gamepad.id == joy.Get("id").String() {
gamepad.connected = false
}
}
return nil
}))
window.Call("addEventListener", "gamepadconnected", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
println("gamepad connected")
joy := args[0].Get("gamepad")
if joy.IsNull() {
return nil
}
Input.gamepads.mutex.Lock()
defer Input.gamepads.mutex.Unlock()
found := false
for _, gamepad := range Input.gamepads.gamepads {
if gamepad.id == joy.Get("id").String() {
gamepad.connected = true
found = true
}
}
if !found {
for name, gamepad := range Input.gamepads.gamepads {
if !gamepad.connected && gamepad.id == "" { //gamepad was connected after registered
Input.gamepads.gamepads[name] = &Gamepad{
id: joy.Get("id").String(),
connected: true,
}
}
}
}
return nil
}))
}
// DestroyWindow handles destroying the window when done
func DestroyWindow() {}
// CursorPos returns the current cursor position
func CursorPos() (x, y float32) {
return Input.Mouse.X * opts.GlobalScale.X, Input.Mouse.Y * opts.GlobalScale.Y
}
// SetTitle changes the title of the page to the given string
func SetTitle(title string) {
if opts.HeadlessMode {
log.Println("Title set to:", title)
} else {
document.Set("title", title)
}
}
// WindowSize returns the width and height of the current window
func WindowSize() (w, h int) {
w = int(WindowWidth())
h = int(WindowHeight())
return
}
// WindowWidth returns the current window width
func WindowWidth() float32 {
return float32(window.Get("innerWidth").Int())
}
// WindowHeight returns the current window height
func WindowHeight() float32 {
return float32(window.Get("innerHeight").Int())
}
// CanvasWidth returns the current canvas width
func CanvasWidth() float32 {
return float32(canvas.Get("width").Int())
}
// CanvasHeight returns the current canvas height
func CanvasHeight() float32 {
return float32(canvas.Get("height").Int())
}
func CanvasScale() float32 {
return 1
}
func rafPolyfill() {
vendors := []string{"ms", "moz", "webkit", "o"}
if window.Get("requestAnimationFrame").Type() == js.TypeUndefined {
for i := 0; i < len(vendors) && window.Get("requestAnimationFrame").Type() == js.TypeUndefined; i++ {
vendor := vendors[i]
window.Set("requestAnimationFrame", window.Get(vendor+"RequestAnimationFrame"))
window.Set("cancelAnimationFrame", window.Get(vendor+"CancelAnimationFrame"))
if window.Get("cancelAnimationFrame").Type() == js.TypeUndefined {
window.Set("cancelAnimationFrame", window.Get(vendor+"CancelRequestAnimationFrame"))
}
}
}
lastTime := 0.0
if window.Get("requestAnimationFrame").Type() == js.TypeUndefined {
window.Set("requestAnimationFrame", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
currTime := js.Global().Get("Date").New().Call("getTime").Float()
timeToCall := math.Max(0, 16-(currTime-lastTime))
window.Call("setTimeout", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
args[0].Invoke(currTime + timeToCall)
return nil
}), timeToCall)
lastTime = currTime + timeToCall
return nil
}))
}
if window.Get("cancelAnimationFrame").Type() == js.TypeUndefined {
window.Set("cancelAnimationFrame", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
js.Global().Get("clearTimeout").Invoke(args[0])
return nil
}))
}
}
// RunIteration runs one iteration per frame
func RunIteration() {
Time.Tick()
Input.update()
jsPollKeys()
currentUpdater.Update(Time.Delta())
Input.Mouse.Action = Neutral
// TODO: this may not work, and sky-rocket the FPS
// requestAnimationFrame(func(dt float32) {
// currentWorld.Update(Time.Delta())
// keysUpdate()
// if !headless {
// // TODO: does this require !headless?
// Mouse.ScrollX, Mouse.ScrollY = 0, 0
// }
// Time.Tick()
// })
}
// jsPollKeys polls the keys collected by the javascript callback
// this ensures the keys only get updated once per frame, since the
// callback has no information about the frames and is invoked several
// times between frames. This makes Input.Button.JustPressed and JustReleased
// able to return true properly.
func jsPollKeys() {
pollLock.Lock()
defer pollLock.Unlock()
Input.Modifier = mod
for key, state := range poll {
Input.keys.Set(Key(key), state)
delete(poll, key)
}
}
func checkModifiers(event js.Value) {
mod = 0
for str, modifier := range jsStrToMod {
if event.Get(str).Bool() {
go func(m Modifier) {
pollLock.Lock()
mod = m
pollLock.Unlock()
}(modifier)
break
}
}
}
func requestAnimationFrame(callback func(float32)) int {
//return dom.GetWindow().RequestAnimationFrame(callback)
return js.Global().Call("requestAnimationFrame", callback).Int()
}
func cancelAnimationFrame(id int) {
window.Call("cancelAnimationFrame", id)
}
// RunPreparation is called automatically when calling Open. It should only be called once.
func RunPreparation() {
Time = NewClock()
if !opts.HeadlessMode {
window.Call("addEventListener", "onbeforeunload", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
window.Call("alert", "You're closing")
return nil
}))
}
}
func runLoop(defaultScene Scene, headless bool) {
SetScene(defaultScene, false)
RunPreparation()
ticker := time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
// Start tick, minimize the delta
Time.Tick()
for {
select {
case <-ticker.C:
RunIteration()
case <-resetLoopTicker:
ticker.Stop()
ticker = time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
case <-closeGame:
ticker.Stop()
closeEvent()
return
}
}
}
func openFile(url string) (io.ReadCloser, error) {
if Headless() { // Headless would be node.js
return os.Open(url)
}
var err error
var resp js.Value
ch := make(chan struct{})
req := js.Global().Get("XMLHttpRequest").New()
req.Call("open", "GET", url, true)
req.Set("responseType", "arraybuffer")
loadf := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer close(ch)
status := req.Get("status").Int()
if 200 <= status && status < 400 {
resp = req.Get("response")
return nil
}
err = errors.New(fmt.Sprintf("http error: %d", status))
return nil
})
defer loadf.Release()
req.Call("addEventListener", "load", loadf)
errorf := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer close(ch)
err = errors.New(fmt.Sprintf("XMLHttpRequest error: %s", req.Get("statusText").String()))
return nil
})
req.Call("addEventListener", "error", errorf)
defer errorf.Release()
req.Call("send")
t := time.NewTicker(time.Duration(int(time.Second) * 10))
select {
case <-ch:
if err != nil {
return nil, err
}
buf := make([]byte, resp.Get("byteLength").Int())
js.CopyBytesToGo(buf, js.Global().Get("Uint8Array").New(resp))
f := &noCloseReadCloser{bytes.NewReader(buf)}
return f, nil
case <-t.C:
return nil, errors.New("Did not receive a response in 10 seconds.")
}
}
type noCloseReadCloser struct {
r io.Reader
}
func (n noCloseReadCloser) Close() error { return nil }
func (n noCloseReadCloser) Read(p []byte) (int, error) {
return n.r.Read(p)
}
// SetCursor changes the cursor
func SetCursor(c Cursor) {
switch c {
case CursorNone:
document.Get("body").Get("style").Set("cursor", "default")
case CursorHand:
document.Get("body").Get("style").Set("cursor", "hand")
}
}
//SetCursorVisibility sets the visibility of the cursor.
//If true the cursor is visible, if false the cursor is not.
func SetCursorVisibility(visible bool) {
if visible {
document.Get("body").Get("style").Set("cursor", "default")
} else {
document.Get("body").Get("style").Set("cursor", "none")
}
}
// IsAndroidChrome tells if the browser is Chrome for android
func IsAndroidChrome() bool {
ua := js.Global().Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "Android") {
return false
}
if !strings.Contains(ua, "Chrome") {
return false
}
return true
}
// GetKeyName returns the string returned from the given Key. So you can write "Press W to move forward"
// and get a W for QWERTY and Z for AZERTY
func GetKeyName(k Key) string {
if 96 <= k && k <= 105 {
k = k - 48
}
return js.Global().Get("String").Call("fromCharCode", k).String()
}