From 4206d7fe5cf450fbc1f9a16e8d57b85c08df3dab Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 17 Jun 2025 11:43:34 -0500 Subject: 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. --- go/ui/update.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 go/ui/update.go (limited to 'go/ui/update.go') 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 +} -- cgit v1.2.3