aboutsummaryrefslogtreecommitdiff
path: root/go/ui/update.go
blob: fdb0f19eaee47632269c219bc5161b21c6490bda (plain)
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
package ui

import (
	"log"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/inpututil"
)

type InputType int

const (
	Click InputType = iota + 1
	Scroll
	Text
)

type InputEvent struct {
	kind InputType
	x, y int
	ctx  any
}

type ClickCtx struct {
	btn ebiten.MouseButton
}

type ScrollCtx struct {
	scrollX, scrollY int
}

type TextCtx struct {
	in []rune
}

// Input queue
var Input []InputEvent

func consumeInput(i int) {
	log.Println("consume input", i)
	if len(Input) == 1 {
		Input = []InputEvent{}
	} else {
		Input[i] = Input[len(Input)-1]
	}
}

// Update all ui singletons and collect all occurred input
func Update() error {
	Input = []InputEvent{}
	if err := TouchManager.Update(); err != nil {
		return err
	}

	// TODO: make hover detector a singleton

	x, y := ebiten.CursorPosition()
	scrollX, scrollY := ebiten.Wheel()
	if scrollX != 0 || scrollY != 0 {
		Input = append(Input, InputEvent{Scroll, x, y, ScrollCtx{int(scrollX), int(scrollY)}})
	}

	var in []rune
	in = ebiten.AppendInputChars(in)
	if len(in) > 0 {
		Input = append(Input, InputEvent{Text, x, y, TextCtx{in}})
	}

	if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
		Input = append(Input, InputEvent{Click, x, y, ClickCtx{ebiten.MouseButtonLeft}})
	}

	return nil
}