diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-02-16 18:57:15 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:50 +0100 |
| commit | c12057320c06f9b4a0bd6c9b25a1dd2d2bd8fd8d (patch) | |
| tree | 77e2d4f4b1430b95fd20dc14a8c2c561a80d6bc8 /go/ui/hoverDetector.go | |
| parent | 2f01274717d62e59e4bf3b4426a2ec6a76ffac11 (diff) | |
| download | muhqs-game-c12057320c06f9b4a0bd6c9b25a1dd2d2bd8fd8d.tar.gz muhqs-game-c12057320c06f9b4a0bd6c9b25a1dd2d2bd8fd8d.zip | |
move more ui logic into Collection
* Generalize hover detection and move it into ui.Collection
* Implement transparencu for the prompt banner
Diffstat (limited to 'go/ui/hoverDetector.go')
| -rw-r--r-- | go/ui/hoverDetector.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/go/ui/hoverDetector.go b/go/ui/hoverDetector.go new file mode 100644 index 00000000..49161f4f --- /dev/null +++ b/go/ui/hoverDetector.go @@ -0,0 +1,41 @@ +package ui + +// A simple structure to detect if the cursor has not moved or started moving again. +// hoverDetector.update must be called during each Update invocation to track the +// frame and the current cursor position. +type hoverDetector struct { + last_x, last_y int + ticks int + hovering Hoverable +} + +func (h *hoverDetector) update(collection *Collection, x, y int, w Widget) { + if x == h.last_x && y == h.last_y { + h.hover(collection, x, y, w) + } else { + h.reset(x, y) + h.last_x, h.last_y = x, y + } +} + +func (h *hoverDetector) hover(collection *Collection, x, y int, w Widget) { + h.ticks++ + + if w == nil || h.hovering != nil { + return + } + + if hoverable, ok := w.(Hoverable); ok && hoverable.onHover(h.ticks, x, y, collection) { + h.hovering = hoverable + } +} + +func (h *hoverDetector) reset(x, y int) { + if h.hovering != nil { + if !h.hovering.stopHover(h.ticks, x, y) { + return + } + h.hovering = nil + } + h.ticks = 0 +} |
