forked from remogatto/mandala
/
callback.go
60 lines (49 loc) · 1.36 KB
/
callback.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
// +build !android
package gorgasm
import (
glfw "github.com/go-gl/glfw3"
)
func errorCallback(err glfw.ErrorCode, desc string) {
Logf("%v: %v\n", err, desc)
}
func exitCallback(window *glfw.Window) {
event <- DestroyEvent{}
}
func mouseButtonCallback(
window *glfw.Window,
button glfw.MouseButton,
action glfw.Action,
mod glfw.ModifierKey) {
if button == glfw.MouseButton1 {
down := action == glfw.Press
x, y := window.GetCursorPosition()
event <- ActionUpDownEvent{
Down: down,
X: float32(x),
Y: float32(y),
}
}
}
func cursorPositionCallback(window *glfw.Window, x float64, y float64) {
event <- ActionMoveEvent{
X: float32(x),
Y: float32(y),
}
}
// Init initializes a glfw.Window to be used in a xorg Gorgasm
// application. It has to be called after the GLFW initialization
// boilerplate. See
// https://github.com/remogatto/gorgasm-examples/triangle/src/triangle/main.go
// for an example.
func Init(window *glfw.Window) {
glfw.SetErrorCallback(errorCallback)
// Set callbacks associated with window events
window.SetCloseCallback(exitCallback)
window.SetMouseButtonCallback(mouseButtonCallback)
window.SetCursorPositionCallback(cursorPositionCallback)
// Begin sending events related to the creation process
event <- CreateEvent{}
event <- StartEvent{}
event <- ResumeEvent{}
event <- NativeWindowCreatedEvent{Window: window}
}