aboutsummaryrefslogtreecommitdiff
path: root/go/ui/update.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-06-17 11:43:34 -0500
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:57:21 +0200
commit4206d7fe5cf450fbc1f9a16e8d57b85c08df3dab (patch)
treec0dd008bcb47fe6e7af49e5b79ba928ace002cc7 /go/ui/update.go
parentab08e0b7c7ebc50549392e6de3908dfa61f1e973 (diff)
downloadmuhqs-game-4206d7fe5cf450fbc1f9a16e8d57b85c08df3dab.tar.gz
muhqs-game-4206d7fe5cf450fbc1f9a16e8d57b85c08df3dab.zip
make input events consumable
Having a input queue of consumable input events allow widgets to prevent the further handling of the same event by different collections by consuming the event.
Diffstat (limited to 'go/ui/update.go')
-rw-r--r--go/ui/update.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/go/ui/update.go b/go/ui/update.go
new file mode 100644
index 00000000..fdb0f19e
--- /dev/null
+++ b/go/ui/update.go
@@ -0,0 +1,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
+}