Skip to content

Commit 538e901

Browse files
committed
Add KeyCallback, CharCallback, ScrollCallback.
Browser implementation is currently incomplete.
1 parent 6323674 commit 538e901

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

browser.go

+94-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,58 @@ func CreateWindow(_, _ int, title string, monitor *Monitor, share *Window) (*Win
7171
w.Context = gl
7272
}
7373

74+
document.AddEventListener("keydown", false, func(event dom.Event) {
75+
if w.keyCallback == nil {
76+
return
77+
}
78+
ke := event.(*dom.KeyboardEvent)
79+
80+
action := Press
81+
if ke.Repeat {
82+
action = Repeat
83+
}
84+
85+
mods := ModifierKey(0) // TODO: ke.CtrlKey && !ke.AltKey && !ke.MetaKey && !ke.ShiftKey.
86+
87+
switch {
88+
case ke.KeyCode == 13: // Enter.
89+
case ke.KeyCode == 27: // Escape.
90+
case ke.KeyCode == 49:
91+
w.keyCallback(w, Key1, -1, action, mods)
92+
case ke.KeyCode == 50:
93+
w.keyCallback(w, Key2, -1, action, mods)
94+
case ke.KeyCode == 51:
95+
w.keyCallback(w, Key3, -1, action, mods)
96+
default:
97+
fmt.Println("Unknown KeyCode:", ke.KeyCode)
98+
}
99+
100+
ke.PreventDefault()
101+
})
102+
document.AddEventListener("keyup", false, func(event dom.Event) {
103+
if w.keyCallback == nil {
104+
return
105+
}
106+
ke := event.(*dom.KeyboardEvent)
107+
108+
mods := ModifierKey(0) // TODO: ke.CtrlKey && !ke.AltKey && !ke.MetaKey && !ke.ShiftKey.
109+
110+
switch {
111+
case ke.KeyCode == 13: // Enter.
112+
case ke.KeyCode == 27: // Escape.
113+
case ke.KeyCode == 49:
114+
w.keyCallback(w, Key1, -1, Release, mods)
115+
case ke.KeyCode == 50:
116+
w.keyCallback(w, Key2, -1, Release, mods)
117+
case ke.KeyCode == 51:
118+
w.keyCallback(w, Key3, -1, Release, mods)
119+
default:
120+
fmt.Println("Unknown KeyCode:", ke.KeyCode)
121+
}
122+
123+
ke.PreventDefault()
124+
})
125+
74126
document.AddEventListener("mousedown", false, func(event dom.Event) {
75127
me := event.(*dom.MouseEvent)
76128
if !(me.Button >= 0 && me.Button <= 2) {
@@ -146,17 +198,24 @@ func CreateWindow(_, _ int, title string, monitor *Monitor, share *Window) (*Win
146198
return w, nil
147199
}
148200

201+
func SwapInterval(interval int) error {
202+
// TODO: Implement.
203+
return nil
204+
}
205+
149206
type Window struct {
150207
Context *webgl.Context
151208

152209
canvas *dom.HTMLCanvasElement
153210

211+
cursorMode int
154212
cursorPosition [2]float64
155213
mouseButton [3]Action
156214

157215
cursorPositionCallback CursorPositionCallback
158216
mouseMovementCallback MouseMovementCallback
159217
mouseButtonCallback MouseButtonCallback
218+
keyCallback KeyCallback
160219

161220
touches js.Object // Hacky mouse-emulation-via-touch.
162221
}
@@ -190,6 +249,29 @@ func (w *Window) SetMouseMovementCallback(cbfun MouseMovementCallback) (previous
190249
return nil, nil
191250
}
192251

252+
type KeyCallback func(w *Window, key Key, scancode int, action Action, mods ModifierKey)
253+
254+
func (w *Window) SetKeyCallback(cbfun KeyCallback) (previous KeyCallback, err error) {
255+
w.keyCallback = cbfun
256+
257+
// TODO: Handle previous.
258+
return nil, nil
259+
}
260+
261+
type CharCallback func(w *Window, char rune)
262+
263+
func (w *Window) SetCharCallback(cbfun CharCallback) (previous CharCallback, err error) {
264+
// TODO.
265+
return nil, nil
266+
}
267+
268+
type ScrollCallback func(w *Window, xoff float64, yoff float64)
269+
270+
func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback, err error) {
271+
// TODO.
272+
return nil, nil
273+
}
274+
193275
type MouseButtonCallback func(w *Window, button MouseButton, action Action, mods ModifierKey)
194276

195277
func (w *Window) SetMouseButtonCallback(cbfun MouseButtonCallback) (previous MouseButtonCallback, err error) {
@@ -284,7 +366,12 @@ func (w *Window) GetMouseButton(button MouseButton) (Action, error) {
284366
}
285367

286368
func (w *Window) GetInputMode(mode InputMode) (int, error) {
287-
return 0, errors.New("not yet impl")
369+
switch mode {
370+
case Cursor:
371+
return w.cursorMode, nil
372+
default:
373+
return 0, errors.New("not yet impl")
374+
}
288375
}
289376

290377
var ErrInvalidParameter = errors.New("invalid parameter")
@@ -295,14 +382,17 @@ func (w *Window) SetInputMode(mode InputMode, value int) error {
295382
case Cursor:
296383
switch value {
297384
case CursorNormal:
385+
w.cursorMode = value
298386
document.Underlying().Call("exitPointerLock")
299387
w.canvas.Style().SetProperty("cursor", "initial", "")
300388
return nil
301389
case CursorHidden:
390+
w.cursorMode = value
302391
document.Underlying().Call("exitPointerLock")
303392
w.canvas.Style().SetProperty("cursor", "none", "")
304393
return nil
305394
case CursorDisabled:
395+
w.cursorMode = value
306396
w.canvas.Underlying().Call("requestPointerLock")
307397
return nil
308398
default:
@@ -322,6 +412,9 @@ type Key int
322412
const (
323413
KeyLeftShift Key = 340
324414
KeyRightShift Key = 344
415+
Key1 Key = 49
416+
Key2 Key = 50
417+
Key3 Key = 51
325418
)
326419

327420
type MouseButton int

desktop.go

+45
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,48 @@ func (w *Window) SetMouseMovementCallback(cbfun MouseMovementCallback) (previous
9595
return nil, err
9696
}
9797

98+
type KeyCallback func(w *Window, key Key, scancode int, action Action, mods ModifierKey)
99+
100+
func (w *Window) SetKeyCallback(cbfun KeyCallback) (previous KeyCallback, err error) {
101+
wrappedCbfun := func(_ *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
102+
cbfun(w, Key(key), scancode, Action(action), ModifierKey(mods))
103+
}
104+
105+
p, err := w.Window.SetKeyCallback(wrappedCbfun)
106+
_ = p
107+
108+
// TODO: Handle previous.
109+
return nil, err
110+
}
111+
112+
type CharCallback func(w *Window, char rune)
113+
114+
func (w *Window) SetCharCallback(cbfun CharCallback) (previous CharCallback, err error) {
115+
wrappedCbfun := func(_ *glfw.Window, char rune) {
116+
cbfun(w, char)
117+
}
118+
119+
p, err := w.Window.SetCharCallback(wrappedCbfun)
120+
_ = p
121+
122+
// TODO: Handle previous.
123+
return nil, err
124+
}
125+
126+
type ScrollCallback func(w *Window, xoff float64, yoff float64)
127+
128+
func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback, err error) {
129+
wrappedCbfun := func(_ *glfw.Window, xoff float64, yoff float64) {
130+
cbfun(w, xoff, yoff)
131+
}
132+
133+
p, err := w.Window.SetScrollCallback(wrappedCbfun)
134+
_ = p
135+
136+
// TODO: Handle previous.
137+
return nil, err
138+
}
139+
98140
type MouseButtonCallback func(w *Window, button MouseButton, action Action, mods ModifierKey)
99141

100142
func (w *Window) SetMouseButtonCallback(cbfun MouseButtonCallback) (previous MouseButtonCallback, err error) {
@@ -146,6 +188,9 @@ type Key glfw.Key
146188
const (
147189
KeyLeftShift = Key(glfw.KeyLeftShift)
148190
KeyRightShift = Key(glfw.KeyRightShift)
191+
Key1 = Key(glfw.Key1)
192+
Key2 = Key(glfw.Key2)
193+
Key3 = Key(glfw.Key3)
149194
)
150195

151196
type MouseButton glfw.MouseButton

0 commit comments

Comments
 (0)