aboutsummaryrefslogtreecommitdiff
path: root/go/ui/collection.go
blob: 662d1bbfbe5f75c9567f0f60c916a8e35a7cb07e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ui

import (
	"log"
	"slices"

	"github.com/hajimehoshi/ebiten/v2"
)

// Container to organise multiple Widgets
// A collection does implement the Widget interface and thus can be nested in
// another Collection.
type Collection struct {
	Width, Height int
	widgets       []Widget
	focused       Widget
	hovering      Widget
}

type CollectionInterface interface {
	Widget
	Widgets() []Widget
	Clear()
	AddWidget(Widget)
	FindWidget(Widget) int
	RemoveWidget(Widget)
	RemoveWidgetAt(int)
}

func (c *Collection) Layout() (int, int) {
	return c.Width, c.Height
}

func (c *Collection) Widgets() []Widget {
	return c.widgets
}

func (c *Collection) Clear() {
	c.widgets = []Widget{}
}

func (c *Collection) AddWidget(w Widget) {
	if w == nil {
		log.Panicf("Adding nil widget to collection")
	}
	if slices.Contains(c.widgets, w) {
		log.Panicf("Double insertion of %v", w)
	}
	c.widgets = append(c.widgets, w)
}

// MoveIdxToLast shifts the widget at index idx to the last position.
func (c *Collection) MoveIdxToLast(idx int) {
	w := c.widgets[idx]
	after := c.widgets[idx+1:]
	c.widgets = c.widgets[:idx]
	c.widgets = append(c.widgets, after...)
	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)
	}
}

func (c *Collection) Contains(x, y int) bool {
	for _, w := range c.widgets {
		if w.Contains(x, y) {
			return true
		}
	}
	return false
}

func (c *Collection) FindWidgetAt(x, y int) Widget {
	// Iterate the widget in reverse order to ensure that upper widgets are
	// considered first
	for i := len(c.widgets) - 1; i >= 0; i-- {
		w := c.widgets[i]
		if !w.Contains(x, y) {
			continue
		}
		return w
	}
	return nil
}

func (c *Collection) FindObjectAt(x, y int) any {
	// Iterate the widget in reverse order to ensure that upper widgets are
	// considered first
	for i := len(c.widgets) - 1; i >= 0; i-- {
		w := c.widgets[i]
		if o := w.FindObjectAt(x, y); o != nil {
			return o
		}
	}

	return nil
}

func (c *Collection) switchFocus(fcsbl Widget) {
	log.Printf("switch focus from %v to %v\n", c.focused, fcsbl)
	if c.focused != nil {
		c.focused.Focus(false)
	}
	if fcsbl != nil {
		fcsbl.Focus(true)
		c.focused = fcsbl
	} else {
		c.focused = nil
	}
}

func (c *Collection) IsUpdatable() bool {
	return true
}

func (c *Collection) updateWidgets() error {
	// Update all updatables
	for _, w := range c.widgets {
		if w.IsUpdatable() {
			if err := w.Update(); err != nil {
				return err
			}
		}
	}
	return nil
}

func (c *Collection) Update() error {
	defer c.updateWidgets()

	for i := len(Input) - 1; i >= 0; i-- {
		ev := Input[i]
		w := c.FindWidgetAt(ev.X, ev.Y)
		if w != nil {
			switch ev.Kind {
			case Click, Tap:
				// Only consider taps or left clicks
				if w.IsClickable() && (ev.Kind != Click || ev.Ctx.(ClickCtx).Btn == ebiten.MouseButtonLeft) {
					w.Click(ev.X, ev.Y)
					ConsumeInput(i)
				}
				if w.IsFocusable() && (ev.Kind != Click || ev.Ctx.(ClickCtx).Btn == ebiten.MouseButtonLeft) {
					c.switchFocus(w)
				}
			case Scroll, Pan:
				if w.IsScrollable() {
					s := ev.Ctx.(DistanceCtx)
					w.Scroll(s.scrollX, s.scrollY)
					ConsumeInput(i)
				}
			case Text:
			case HoverEnd:
				if c.hovering != nil {
					c.hovering.Hover(false, ev.X, ev.Y)
					c.hovering = nil
				}
			case HoverStart:
				if w.IsHoverable() {
					w.Hover(true, ev.X, ev.Y)
					c.hovering = w
				}
			}
		}
	}

	return nil
}

// The events for the contained widgets
// are emitted during the collections
// Update method.
func (c *Collection) IsClickable() bool {
	return false
}
func (c *Collection) Click(int, int) {}

func (c *Collection) IsScrollable() bool {
	return false
}
func (c *Collection) Scroll(int, int) {}

func (c *Collection) IsFocusable() bool {
	return false
}
func (c *Collection) Focus(bool) {}