aboutsummaryrefslogtreecommitdiffstats
path: root/win/events.go
blob: e7dbc31ff26940ab184c46be2df41809f5d959f1 (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
50
51
52
53
54
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:
			sendEvent(events, "mo", "down", moX, moY)
		case glfw.Release:
			sendEvent(events, "mo", "up", moX, moY)
		}
	})

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

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

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

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

func sendEvent(events chan<- string, a ...interface{}) {
	go func() {
		events <- mkEvent(a...)
	}()
}

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)
}