aboutsummaryrefslogtreecommitdiff
path: root/go/ui/collection.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/ui/collection.go')
-rw-r--r--go/ui/collection.go44
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)
}
}