diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-02-16 18:57:15 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 15:50:10 +0200 |
| commit | 63ad22242562b1e66353694f2a17df6d1c64a8fd (patch) | |
| tree | f91a3a98e554e58a30d5bb0a34b021c7fb206311 /go/ui/collection.go | |
| parent | c0ab1110429be3a2a1664084469952ea082961c1 (diff) | |
| download | muhqs-game-63ad22242562b1e66353694f2a17df6d1c64a8fd.tar.gz muhqs-game-63ad22242562b1e66353694f2a17df6d1c64a8fd.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/collection.go')
| -rw-r--r-- | go/ui/collection.go | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/go/ui/collection.go b/go/ui/collection.go index ac53ba8c..d75e3267 100644 --- a/go/ui/collection.go +++ b/go/ui/collection.go @@ -1,12 +1,20 @@ package ui import ( + "log" + "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/inpututil" ) type Collection struct { - widgets []Widget + Width, Height int + hoverDetector hoverDetector + widgets []Widget +} + +func (c *Collection) Layout() (int, int) { + return c.Width, c.Height } func (c *Collection) Widgets() []Widget { @@ -17,10 +25,40 @@ func (c *Collection) Clear() { c.widgets = []Widget{} } +func (c *Collection) ResetHover() { + c.hoverDetector.reset(0, 0) +} + func (c *Collection) AddWidget(w Widget) { + if w == nil { + log.Panicf("Adding nil widget to collection") + } c.widgets = append(c.widgets, w) } +func (c *Collection) FindWidget(toFind Widget) int { + for i, w := range c.widgets { + if w != toFind { + continue + } + return i + } + return -1 +} + +func (c *Collection) RemoveWidget(widget Widget) { + idx := c.FindWidget(widget) + if idx != -1 { + c.RemoveWidgetAt(idx) + } +} + +func (c *Collection) RemoveWidgetAt(idx int) { + nwidgets := len(c.widgets) + c.widgets[idx] = c.widgets[nwidgets-1] + c.widgets = c.widgets[:nwidgets-1] +} + func (c *Collection) Draw(screen *ebiten.Image) { for _, w := range c.widgets { w.Draw(screen) @@ -39,6 +77,8 @@ func (c *Collection) Update() error { break } + c.hoverDetector.update(c, x, y, activeWidget) + if activeWidget == nil { return nil } @@ -61,7 +101,7 @@ func (c *Collection) Update() error { } if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) { - if btn, ok := activeWidget.(Button); ok { + if btn, ok := activeWidget.(Clickable); ok { btn.Click(x, y) } } |
