aboutsummaryrefslogtreecommitdiffstats
path: root/win/events.go
blob: 7ce93e57a5701c18203a5c9ded534f8b0cb9119f (plain) (blame)
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
package win

import (
	"fmt"
	"strings"

	"github.com/faiface/gui/event"
	"github.com/go-gl/glfw/v3.2/glfw"
)

func (w *Win) setUpEvents(events chan<- string) {
	var moX, moY int

	w.w.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
		switch action {
		case glfw.Press:
			events <- mkEvent("mo", "down", moX, moY)
		case glfw.Release:
			events <- mkEvent("mo", "up", moX, moY)
		}
	})

	w.w.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) {
		moX, moY = int(x), int(y)
		events <- mkEvent("mo", "move", moX, moY)
	})

	w.w.SetCharCallback(func(_ *glfw.Window, r rune) {
		events <- mkEvent("kb", "type", r)
	})

	w.w.SetSizeCallback(func(_ *glfw.Window, width, height int) {
		w.resize(width, height)
		events <- mkEvent("wi", "resize", width, height)
	})

	w.w.SetCloseCallback(func(_ *glfw.Window) {
		events <- mkEvent("wi", "close")
		w.close()
	})
}

func mkEvent(a ...interface{}) string {
	s := make([]string, len(a))
	for i := range s {
		s[i] = fmt.Sprint(a[i])
	}
	return strings.Join(s, event.Sep)
}