/
aglwindow.go
754 lines (630 loc) · 19.8 KB
/
aglwindow.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
package pixels
import (
"errors"
"fmt"
"image"
"image/color"
"runtime"
"time"
"github.com/markel1974/godoom/pixels/executor"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
type WindowConfig struct {
Title string
Icon []IPicture
Bounds Rect
Position Vec
Monitor *GLMonitor
Smooth bool
Resizable bool
Undecorated bool
NoIconify bool
AlwaysOnTop bool
TransparentFramebuffer bool
VSync bool
Maximized bool
Invisible bool
SamplesMSAA int
}
type GLWindow struct {
window *glfw.Window
bounds Rect
canvas *GLCanvas
vsync bool
cursorVisible bool
cursorInsideWindow bool
// need to save these to correctly restore a fullscreen window
restore struct {
xPos, yPos, width, height int
}
prevInp, currInp, tempInp struct {
mouse Vec
buttons [KeyLast + 1]bool
repeat [KeyLast + 1]bool
scroll Vec
typed string
}
keysPressed map[Button]bool
pressEvents, tempPressEvents [KeyLast + 1]bool
releaseEvents, tempReleaseEvents [KeyLast + 1]bool
prevJoy, currJoy, tempJoy GLJoystick
}
var currWin *GLWindow
func NewGLWindow(cfg WindowConfig) (*GLWindow, error) {
bool2int := map[bool]int{
true: glfw.True,
false: glfw.False,
}
w := &GLWindow{bounds: cfg.Bounds, cursorVisible: true, keysPressed: make(map[Button]bool)}
flag := false
for _, v := range []int{0, 2, 4, 8, 16} {
if cfg.SamplesMSAA == v {
flag = true
break
}
}
if !flag {
return nil, fmt.Errorf("invalid value '%v' for msaaSamples", cfg.SamplesMSAA)
}
err := executor.Thread.CallErr(func() error {
var err error
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Resizable, bool2int[cfg.Resizable])
glfw.WindowHint(glfw.Decorated, bool2int[!cfg.Undecorated])
glfw.WindowHint(glfw.Floating, bool2int[cfg.AlwaysOnTop])
glfw.WindowHint(glfw.AutoIconify, bool2int[!cfg.NoIconify])
glfw.WindowHint(glfw.TransparentFramebuffer, bool2int[cfg.TransparentFramebuffer])
glfw.WindowHint(glfw.Maximized, bool2int[cfg.Maximized])
glfw.WindowHint(glfw.Visible, bool2int[!cfg.Invisible])
glfw.WindowHint(glfw.Samples, cfg.SamplesMSAA)
if cfg.Position.X != 0 || cfg.Position.Y != 0 {
glfw.WindowHint(glfw.Visible, glfw.False)
}
var share *glfw.Window
if currWin != nil {
share = currWin.window
}
_, _, width, height := intBounds(cfg.Bounds)
w.window, err = glfw.CreateWindow(width, height, cfg.Title, nil, share)
if err != nil {
return err
}
if cfg.Position.X != 0 || cfg.Position.Y != 0 {
w.window.SetPos(int(cfg.Position.X), int(cfg.Position.Y))
w.window.Show()
}
// enter the OpenGL context
w.begin()
executor.Init()
gl.Enable(gl.MULTISAMPLE)
w.end()
return nil
})
if err != nil {
return nil, errors.New("creating window failed")
}
if len(cfg.Icon) > 0 {
imgs := make([]image.Image, len(cfg.Icon))
for i, icon := range cfg.Icon {
pic := NewPictureRGBAFromPicture(icon)
fmt.Println(pic, i)
imgs[i] = pic.Image()
}
executor.Thread.Call(func() {
w.window.SetIcon(imgs)
})
}
w.SetVSync(cfg.VSync)
w.initInput()
w.SetMonitor(cfg.Monitor)
w.canvas = NewGLCanvas(cfg.Bounds, cfg.Smooth)
w.Update()
runtime.SetFinalizer(w, (*GLWindow).Destroy)
return w, nil
}
// Destroy destroys the GLWindow. The GLWindow can't be used any further.
func (w *GLWindow) Destroy() {
executor.Thread.Call(func() {
w.window.Destroy()
})
}
// Update swaps buffers and polls events. Call this method at the end of each frame.
func (w *GLWindow) Update() {
w.SwapBuffers()
w.UpdateInput()
}
// ClipboardText returns the current value of the systems clipboard.
func (w *GLWindow) ClipboardText() string {
return w.window.GetClipboardString()
}
// SetClipboardText passes the given string to the underlying glfw window to set the systems clipboard.
func (w *GLWindow) SetClipboardText(text string) {
w.window.SetClipboardString(text)
}
// SwapBuffers swaps buffers. Call this to swap buffers without polling window events.
// Note that Update invokes SwapBuffers.
func (w *GLWindow) SwapBuffers() {
executor.Thread.Call(func() {
_, _, oldW, oldH := intBounds(w.bounds)
newW, newH := w.window.GetSize()
w.bounds = w.bounds.ResizedMin(w.bounds.Size().Add(MakeVec(
float64(newW-oldW),
float64(newH-oldH),
)))
})
w.canvas.SetBounds(w.bounds)
executor.Thread.Call(func() {
w.begin()
framebufferWidth, framebufferHeight := w.window.GetFramebufferSize()
executor.Bounds(0, 0, framebufferWidth, framebufferHeight)
executor.Clear(0, 0, 0, 0)
w.canvas.gf.Frame().Begin()
w.canvas.gf.Frame().Blit(
nil,
0, 0, w.canvas.Texture().Width(), w.canvas.Texture().Height(),
0, 0, framebufferWidth, framebufferHeight,
)
w.canvas.gf.Frame().End()
if w.vsync {
glfw.SwapInterval(1)
} else {
glfw.SwapInterval(0)
}
w.window.SwapBuffers()
w.end()
})
}
// SetClosed sets the closed flag of the GLWindow.
//
// This is useful when overriding the user's attempt to close the GLWindow, or just to close the
// GLWindow from within the program.
func (w *GLWindow) SetClosed(closed bool) {
executor.Thread.Call(func() {
w.window.SetShouldClose(closed)
})
}
// Closed returns the closed flag of the GLWindow, which reports whether the GLWindow should be closed.
//
// The closed flag is automatically set when a user attempts to close the GLWindow.
func (w *GLWindow) Closed() bool {
var closed bool
executor.Thread.Call(func() {
closed = w.window.ShouldClose()
})
return closed
}
// SetTitle changes the title of the GLWindow.
func (w *GLWindow) SetTitle(title string) {
executor.Thread.Call(func() {
w.window.SetTitle(title)
})
}
// SetBounds sets the bounds of the GLWindow in pixels. Bounds can be fractional, but the actual size
// of the window will be rounded to integers.
func (w *GLWindow) SetBounds(bounds Rect) {
w.bounds = bounds
executor.Thread.Call(func() {
_, _, width, height := intBounds(bounds)
w.window.SetSize(width, height)
})
}
// SetPos sets the position, in screen coordinates, of the upper-left corner
// of the client area of the window. Position can be fractional, but the actual position
// of the window will be rounded to integers.
//
// If it is a full screen window, this function does nothing.
func (w *GLWindow) SetPos(pos Vec) {
executor.Thread.Call(func() {
left, top := int(pos.X), int(pos.Y)
w.window.SetPos(left, top)
})
}
// GetPos gets the position, in screen coordinates, of the upper-left corner
// of the client area of the window. The position is rounded to integers.
func (w *GLWindow) GetPos() Vec {
var v Vec
executor.Thread.Call(func() {
x, y := w.window.GetPos()
v = MakeVec(float64(x), float64(y))
})
return v
}
// Bounds returns the current bounds of the GLWindow.
func (w *GLWindow) Bounds() Rect {
return w.bounds
}
func (w *GLWindow) setFullscreen(monitor *GLMonitor) {
executor.Thread.Call(func() {
w.restore.xPos, w.restore.yPos = w.window.GetPos()
w.restore.width, w.restore.height = w.window.GetSize()
mode := monitor.monitor.GetVideoMode()
w.window.SetMonitor(
monitor.monitor,
0,
0,
mode.Width,
mode.Height,
mode.RefreshRate,
)
})
}
func (w *GLWindow) setWindowed() {
executor.Thread.Call(func() {
w.window.SetMonitor(
nil,
w.restore.xPos,
w.restore.yPos,
w.restore.width,
w.restore.height,
0,
)
})
}
// SetMonitor sets the GLWindow fullscreen on the given GLMonitor. If the GLMonitor is nil, the GLWindow
// will be restored to windowed state instead.
//
// The GLWindow will be automatically set to the GLMonitor's resolution. If you want a different
// resolution, you will need to set it manually with SetBounds method.
func (w *GLWindow) SetMonitor(monitor *GLMonitor) {
if w.Monitor() != monitor {
if monitor != nil {
w.setFullscreen(monitor)
} else {
w.setWindowed()
}
}
}
// GLMonitor returns a monitor the GLWindow is fullscreen on. If the GLWindow is not fullscreen, this
// function returns nil.
func (w *GLWindow) Monitor() *GLMonitor {
var monitor *glfw.Monitor
executor.Thread.Call(func() {
monitor = w.window.GetMonitor()
})
if monitor == nil {
return nil
}
return &GLMonitor{
monitor: monitor,
}
}
// Focused returns true if the GLWindow has input focus.
func (w *GLWindow) Focused() bool {
var focused bool
executor.Thread.Call(func() {
focused = w.window.GetAttrib(glfw.Focused) == glfw.True
})
return focused
}
// SetVSync sets whether the GLWindow's Update should synchronize with the monitor refresh rate.
func (w *GLWindow) SetVSync(vsync bool) {
w.vsync = vsync
}
// VSync returns whether the GLWindow is set to synchronize with the monitor refresh rate.
func (w *GLWindow) VSync() bool {
return w.vsync
}
// SetCursorVisible sets the visibility of the mouse cursor inside the GLWindow client area.
func (w *GLWindow) SetCursorVisible(visible bool) {
w.cursorVisible = visible
executor.Thread.Call(func() {
if visible {
w.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
} else {
w.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
}
})
}
// SetCursorDisabled hides the cursor and provides unlimited virtual cursor movement
// make cursor visible using SetCursorVisible
func (w *GLWindow) SetCursorDisabled() {
w.cursorVisible = false
executor.Thread.Call(func() {
w.window.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
})
}
// CursorVisible returns the visibility status of the mouse cursor.
func (w *GLWindow) CursorVisible() bool {
return w.cursorVisible
}
// Note: must be called inside the main thread.
func (w *GLWindow) begin() {
if currWin != w {
w.window.MakeContextCurrent()
currWin = w
}
}
// Note: must be called inside the main thread.
func (w *GLWindow) end() {
// nothing, really
}
// MakeTriangles generates a specialized copy of the supplied ITriangles that will draw onto this
// GLWindow.
//
// GLWindow supports ITrianglesPosition, ITrianglesColor and ITrianglesPicture.
func (w *GLWindow) MakeTriangles(t ITriangles) ITargetTriangles {
return w.canvas.MakeTriangles(t)
}
// MakePicture generates a specialized copy of the supplied IPicture that will draw onto this GLWindow.
//
// GLWindow supports IPictureColor.
func (w *GLWindow) MakePicture(p IPicture) ITargetPicture {
return w.canvas.MakePicture(p)
}
// SetMatrix sets a Matrix that every point will be projected by.
func (w *GLWindow) SetMatrix(m Matrix) {
w.canvas.SetMatrix(m)
}
// SetColorMask sets a global color mask for the GLWindow.
func (w *GLWindow) SetColorMask(c color.Color) {
w.canvas.SetColorMask(c)
}
// SetComposeMethod sets a Porter-Duff composition method to be used in the following draws onto
// this GLWindow.
func (w *GLWindow) SetComposeMethod(cmp ComposeMethod) {
w.canvas.SetComposeMethod(cmp)
}
// SetSmooth sets whether the stretched Pictures drawn onto this GLWindow should be drawn smooth or pixelated.
func (w *GLWindow) SetSmooth(smooth bool) {
w.canvas.SetSmooth(smooth)
}
// Smooth returns whether the stretched Pictures drawn onto this GLWindow are set to be drawn smooth or pixelated.
func (w *GLWindow) Smooth() bool {
return w.canvas.Smooth()
}
// Clear clears the GLWindow with a single color.
func (w *GLWindow) Clear(c color.Color) {
w.canvas.Clear(c)
}
// Color returns the color of the pixel over the given position inside the GLWindow.
func (w *GLWindow) Color(at Vec) RGBA {
return w.canvas.Color(at)
}
// GLCanvas returns the window's underlying GLCanvas
func (w *GLWindow) Canvas() *GLCanvas {
return w.canvas
}
// Show makes the window visible, if it was previously hidden. If the window is already visible or is in full screen mode, this function does nothing.
func (w *GLWindow) Show() {
executor.Thread.Call(func() {
w.window.Show()
})
}
// Clipboard returns the contents of the system clipboard.
func (w *GLWindow) Clipboard() string {
var clipboard string
executor.Thread.Call(func() {
clipboard = w.window.GetClipboardString()
})
return clipboard
}
// SetClipboardString sets the system clipboard to the specified UTF-8 encoded string.
func (w *GLWindow) SetClipboard(str string) {
executor.Thread.Call(func() {
w.window.SetClipboardString(str)
})
}
func (w *GLWindow) KeysPressed() map[Button]bool {
return w.keysPressed
}
func (w *GLWindow) Pressed(button Button) bool {
return w.currInp.buttons[button]
}
// JustPressed returns whether the Button has been pressed in the last frame.
func (w *GLWindow) JustPressed(button Button) bool {
return w.pressEvents[button]
}
// JustReleased returns whether the Button has been released in the last frame.
func (w *GLWindow) JustReleased(button Button) bool {
return w.releaseEvents[button]
}
// Repeated returns whether a repeat event has been triggered on button.
//
// Repeat event occurs repeatedly when a button is held down for some time.
func (w *GLWindow) Repeated(button Button) bool {
return w.currInp.repeat[button]
}
// MousePosition returns the current mouse position in the GLWindow's Bounds.
func (w *GLWindow) MousePosition() Vec {
return w.currInp.mouse
}
// MousePreviousPosition returns the previous mouse position in the GLWindow's Bounds.
func (w *GLWindow) MousePreviousPosition() Vec {
return w.prevInp.mouse
}
// SetMousePosition positions the mouse cursor anywhere within the GLWindow's Bounds.
func (w *GLWindow) SetMousePosition(v Vec) {
executor.Thread.Call(func() {
if (v.X >= 0 && v.X <= w.bounds.W()) &&
(v.Y >= 0 && v.Y <= w.bounds.H()) {
w.window.SetCursorPos(
v.X+w.bounds.Min.X,
(w.bounds.H()-v.Y)+w.bounds.Min.Y,
)
w.prevInp.mouse = v
w.currInp.mouse = v
w.tempInp.mouse = v
}
})
}
// MouseInsideWindow returns true if the mouse position is within the GLWindow's Bounds.
func (w *GLWindow) MouseInsideWindow() bool {
return w.cursorInsideWindow
}
// MouseScroll returns the mouse scroll amount (in both axes) since the last call to GLWindow.Update.
func (w *GLWindow) MouseScroll() Vec {
return w.currInp.scroll
}
// Typed returns the text typed on the keyboard since the last call to GLWindow.Update.
func (w *GLWindow) Typed() string {
return w.currInp.typed
}
func (w *GLWindow) initInput() {
executor.Thread.Call(func() {
w.window.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
switch action {
case glfw.Press:
w.tempPressEvents[Button(button)] = true
w.tempInp.buttons[Button(button)] = true
case glfw.Release:
w.tempReleaseEvents[Button(button)] = true
w.tempInp.buttons[Button(button)] = false
}
})
w.window.SetKeyCallback(func(_ *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if key == glfw.KeyUnknown {
return
}
switch action {
case glfw.Press:
w.keysPressed[Button(key)] = true
w.tempPressEvents[Button(key)] = true
w.tempInp.buttons[Button(key)] = true
case glfw.Release:
delete(w.keysPressed, Button(key))
w.tempReleaseEvents[Button(key)] = true
w.tempInp.buttons[Button(key)] = false
case glfw.Repeat:
w.keysPressed[Button(key)] = true
w.tempInp.repeat[Button(key)] = true
}
})
w.window.SetCursorEnterCallback(func(_ *glfw.Window, entered bool) {
w.cursorInsideWindow = entered
})
w.window.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) {
w.tempInp.mouse = MakeVec(
x+w.bounds.Min.X,
(w.bounds.H()-y)+w.bounds.Min.Y,
)
})
w.window.SetScrollCallback(func(_ *glfw.Window, xoff, yoff float64) {
w.tempInp.scroll.X += xoff
w.tempInp.scroll.Y += yoff
})
w.window.SetCharCallback(func(_ *glfw.Window, r rune) {
w.tempInp.typed += string(r)
})
})
}
// UpdateInput polls window events. Call this function to poll window events without swapping buffers. Note that the Update method invokes UpdateInput.
func (w *GLWindow) UpdateInput() {
executor.Thread.Call(func() { glfw.PollEvents() })
w.doUpdateInput()
}
// UpdateInputWait blocks until an event is received or a timeout. If timeout is 0
// then it will wait indefinitely
func (w *GLWindow) UpdateInputWait(timeout time.Duration) {
executor.Thread.Call(func() {
if timeout <= 0 {
glfw.WaitEvents()
} else {
glfw.WaitEventsTimeout(timeout.Seconds())
}
})
w.doUpdateInput()
}
// internal input bookkeeping
func (w *GLWindow) doUpdateInput() {
w.prevInp = w.currInp
w.currInp = w.tempInp
//w.keysPressed = w.tempKeysPressed
w.pressEvents = w.tempPressEvents
w.releaseEvents = w.tempReleaseEvents
// Clear last frame's temporary status
//w.tempKeysPressed = []Button{}
w.tempPressEvents = [KeyLast + 1]bool{}
w.tempReleaseEvents = [KeyLast + 1]bool{}
w.tempInp.repeat = [KeyLast + 1]bool{}
w.tempInp.scroll = ZV
w.tempInp.typed = ""
w.updateJoystickInput()
}
// JoystickPresent returns if the joystick is currently connected.
//
// This API is experimental.
func (w *GLWindow) JoystickPresent(js Joystick) bool {
return w.currJoy.connected[js]
}
// JoystickName returns the name of the joystick. A disconnected joystick will return an
// empty string.
//
// This API is experimental.
func (w *GLWindow) JoystickName(js Joystick) string {
return w.currJoy.name[js]
}
// JoystickButtonCount returns the number of buttons a connected joystick has.
//
// This API is experimental.
func (w *GLWindow) JoystickButtonCount(js Joystick) int {
return len(w.currJoy.buttons[js])
}
// JoystickAxisCount returns the number of axes a connected joystick has.
//
// This API is experimental.
func (w *GLWindow) JoystickAxisCount(js Joystick) int {
return len(w.currJoy.axis[js])
}
// JoystickPressed returns whether the joystick Button is currently pressed down.
// If the button index is out of range, this will return false.
//
// This API is experimental.
func (w *GLWindow) JoystickPressed(js Joystick, button GamepadButton) bool {
return w.currJoy.getButton(js, int(button))
}
// JoystickJustPressed returns whether the joystick Button has just been pressed down.
// If the button index is out of range, this will return false.
//
// This API is experimental.
func (w *GLWindow) JoystickJustPressed(js Joystick, button GamepadButton) bool {
return w.currJoy.getButton(js, int(button)) && !w.prevJoy.getButton(js, int(button))
}
// JoystickJustReleased returns whether the joystick Button has just been released up.
// If the button index is out of range, this will return false.
//
// This API is experimental.
func (w *GLWindow) JoystickJustReleased(js Joystick, button GamepadButton) bool {
return !w.currJoy.getButton(js, int(button)) && w.prevJoy.getButton(js, int(button))
}
// JoystickAxis returns the value of a joystick axis at the last call to GLWindow.Update.
// If the axis index is out of range, this will return 0.
//
// This API is experimental.
func (w *GLWindow) JoystickAxis(js Joystick, axis GamepadAxis) float64 {
return w.currJoy.getAxis(js, int(axis))
}
// Used internally during GLWindow.UpdateInput to update the state of the joysticks.
func (w *GLWindow) updateJoystickInput() {
for js := Joystick1; js <= JoystickLast; js++ {
// Determine and store if the joystick was connected
joystickPresent := glfw.Joystick(js).Present()
w.tempJoy.connected[js] = joystickPresent
if joystickPresent {
if glfw.Joystick(js).IsGamepad() {
gamepadInputs := glfw.Joystick(js).GetGamepadState()
w.tempJoy.buttons[js] = gamepadInputs.Buttons[:]
w.tempJoy.axis[js] = gamepadInputs.Axes[:]
} else {
w.tempJoy.buttons[js] = glfw.Joystick(js).GetButtons()
w.tempJoy.axis[js] = glfw.Joystick(js).GetAxes()
}
if !w.currJoy.connected[js] {
// The joystick was recently connected, we get the name
w.tempJoy.name[js] = glfw.Joystick(js).GetName()
} else {
// Use the name from the previous one
w.tempJoy.name[js] = w.currJoy.name[js]
}
} else {
w.tempJoy.buttons[js] = []glfw.Action{}
w.tempJoy.axis[js] = []float32{}
w.tempJoy.name[js] = ""
}
}
w.prevJoy = w.currJoy
w.currJoy = w.tempJoy
}