-
Notifications
You must be signed in to change notification settings - Fork 0
/
windoweventcallbacks.go
135 lines (116 loc) · 3.96 KB
/
windoweventcallbacks.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
package dull
import (
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/pekim/dull/geometry"
)
// GridSizeCallback is a function for use with SetGridSizeCallback.
type GridSizeCallback func(columns, rows int)
// SetGridSizeCallback sets or clears a function to call when the
// window's grid size changes.
// This might occur when the window size changes or when the font
// size changes.
//
// When the callback is called, all cells in the new grid will be
// set to a blank Rune, with default background and foreground colors.
//
// To remove a previously set callback, pass nil for the callback.
//
// The callback will run on the main thread, so there is no need
// to use the Do function to effect updates from the callback.
// Do not perform any long running or blocking operations in the callback.
func (w *Window) SetGridSizeCallback(fn GridSizeCallback) {
w.gridSizeCallback = fn
}
func (w *Window) callGridSizeCallback() {
if w.gridSizeCallback != nil {
w.gridSizeCallback(w.grid.width, w.grid.height)
}
}
// KeyCallback is a function for use with SetKeyCallback.
type KeyCallback func(key Key, action Action, mods ModifierKey)
// SetKeyCallback sets or clears a function to call when a key is
// pressed, repeated or released.
//
// To remove a previously set callback, pass nil for the callback.
//
// The callback will run on the main thread, so there is no need
// to use the Do function to effect updates from the callback.
// Do not perform any long running or blocking operations in the callback.
func (w *Window) SetKeyCallback(fn KeyCallback) {
w.keyCallback = fn
}
func (w *Window) callKeyCallback(_ *glfw.Window,
key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey,
) {
w.handleKeyEvent(Key(key), Action(action), ModifierKey(mods))
if w.keyCallback != nil {
w.keyCallback(Key(key), Action(action), ModifierKey(mods))
w.draw()
}
}
func (w *Window) handleKeyEvent(key Key, action Action, mods ModifierKey) {
for _, binding := range w.keybindings {
if key == binding.key && mods == binding.mods {
if action == Press || action == Repeat {
w.blockCharEvents = true
binding.fn()
} else {
// On release unblock char events.
w.blockCharEvents = false
}
}
}
}
func (w *Window) windowZoomReset() {
w.setFontSize(defaultFontSize - w.fontSize)
}
func (w *Window) windowZoomIn() {
w.setFontSize(+fontZoomDelta)
}
func (w *Window) windowZoomOut() {
w.setFontSize(-fontZoomDelta)
}
func (w *Window) ToggleFullscreen() {
videoMode := glfw.GetPrimaryMonitor().GetVideoMode()
if w.glfwWindow.GetMonitor() == nil {
// preserve current windowed bounds
x, y := w.glfwWindow.GetPos()
width, height := w.glfwWindow.GetSize()
w.windowedBounds = geometry.RectNewXYWH(x, y, width, height)
// make fullscreen
w.glfwWindow.SetMonitor(glfw.GetPrimaryMonitor(),
0, 0,
videoMode.Width, videoMode.Height,
videoMode.RefreshRate,
)
} else {
// make windowed.
w.glfwWindow.SetMonitor(nil,
w.windowedBounds.Position.X, w.windowedBounds.Position.Y,
w.windowedBounds.Size.Width, w.windowedBounds.Size.Height,
videoMode.RefreshRate,
)
// Need to set this again, because it appears to no longer be honoured
// after window has been fullscreened and then windowed again..
w.setResizeIncrement()
}
}
// CharCallback is a function for use with SetCharCallback.
type CharCallback func(char rune, mods ModifierKey)
// SetCharCallback sets or clears a function to call when a character
// is input.
//
// To remove a previously set callback, pass nil for the callback.
//
// The callback will run on the main thread, so there is no need
// to use the Do function to effect updates from the callback.
// Do not perform any long running or blocking operations in the callback.
func (w *Window) SetCharCallback(fn CharCallback) {
w.charCallback = fn
}
func (w *Window) callCharCallback(_ *glfw.Window, char rune, mods glfw.ModifierKey) {
if w.charCallback != nil && !w.blockCharEvents {
w.charCallback(char, ModifierKey(mods))
w.draw()
}
}