/
tabbook.go
243 lines (195 loc) · 5.22 KB
/
tabbook.go
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package widget
import (
"image"
"github.com/justclimber/ebitenui/event"
"github.com/justclimber/ebitenui/input"
"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/image/font"
)
type TabBook struct {
TabSelectedEvent *event.Event
tabs []*TabBookTab
containerOpts []ContainerOpt
buttonOpts []StateButtonOpt
buttonImages map[interface{}]*ButtonImage
buttonFace font.Face
buttonColor *ButtonTextColor
flipBookOpts []FlipBookOpt
buttonSpacing int
spacing int
init *MultiOnce
container *Container
tabToButton map[*TabBookTab]*StateButton
flipBook *FlipBook
tab *TabBookTab
}
type TabBookTab struct {
Disabled bool
label string
widget PreferredSizeLocateableWidget
}
type TabBookOpt func(t *TabBook)
type TabBookTabSelectedEventArgs struct {
TabBook *TabBook
Tab *TabBookTab
PreviousTab *TabBookTab
}
type TabBookTabSelectedHandlerFunc func(args *TabBookTabSelectedEventArgs)
type TabBookOptions struct {
}
var TabBookOpts TabBookOptions
func NewTabBook(opts ...TabBookOpt) *TabBook {
t := &TabBook{
TabSelectedEvent: &event.Event{},
init: &MultiOnce{},
tabToButton: map[*TabBookTab]*StateButton{},
}
t.init.Append(t.createWidget)
for _, o := range opts {
o(t)
}
return t
}
func NewTabBookTab(label string, widget PreferredSizeLocateableWidget) *TabBookTab {
return &TabBookTab{
label: label,
widget: widget,
}
}
func (o TabBookOptions) ContainerOpts(opts ...ContainerOpt) TabBookOpt {
return func(t *TabBook) {
t.containerOpts = append(t.containerOpts, opts...)
}
}
func (o TabBookOptions) TabButtonOpts(opts ...StateButtonOpt) TabBookOpt {
return func(t *TabBook) {
t.buttonOpts = append(t.buttonOpts, opts...)
}
}
func (o TabBookOptions) FlipBookOpts(opts ...FlipBookOpt) TabBookOpt {
return func(t *TabBook) {
t.flipBookOpts = append(t.flipBookOpts, opts...)
}
}
func (o TabBookOptions) TabButtonImage(idle *ButtonImage, selected *ButtonImage) TabBookOpt {
return func(t *TabBook) {
t.buttonImages = map[interface{}]*ButtonImage{
false: idle,
true: selected,
}
}
}
func (o TabBookOptions) TabButtonSpacing(s int) TabBookOpt {
return func(t *TabBook) {
t.buttonSpacing = s
}
}
func (o TabBookOptions) TabButtonText(face font.Face, color *ButtonTextColor) TabBookOpt {
return func(t *TabBook) {
t.buttonFace = face
t.buttonColor = color
}
}
func (o TabBookOptions) Spacing(s int) TabBookOpt {
return func(t *TabBook) {
t.spacing = s
}
}
func (o TabBookOptions) Tabs(tabs ...*TabBookTab) TabBookOpt {
return func(t *TabBook) {
t.tabs = append(t.tabs, tabs...)
}
}
func (o TabBookOptions) TabSelectedHandler(f TabBookTabSelectedHandlerFunc) TabBookOpt {
return func(t *TabBook) {
t.TabSelectedEvent.AddHandler(func(args interface{}) {
f(args.(*TabBookTabSelectedEventArgs))
})
}
}
func (t *TabBook) GetWidget() *Widget {
t.init.Do()
return t.container.GetWidget()
}
func (t *TabBook) PreferredSize() (int, int) {
t.init.Do()
return t.container.PreferredSize()
}
func (t *TabBook) SetLocation(rect image.Rectangle) {
t.init.Do()
t.container.SetLocation(rect)
}
func (t *TabBook) RequestRelayout() {
t.init.Do()
t.container.RequestRelayout()
}
func (t *TabBook) SetupInputLayer(def input.DeferredSetupInputLayerFunc) {
t.init.Do()
t.container.SetupInputLayer(def)
}
func (t *TabBook) Render(screen *ebiten.Image, def DeferredRenderFunc, debugMode DebugMode) {
t.init.Do()
d := t.container.GetWidget().Disabled
for tab, b := range t.tabToButton {
b.GetWidget().Disabled = d || tab.Disabled
}
t.container.Render(screen, def, debugMode)
}
func (t *TabBook) createWidget() {
t.container = NewContainer("tab book", append(t.containerOpts, []ContainerOpt{
ContainerOpts.Layout(NewGridLayout(
GridLayoutOpts.Columns(1),
GridLayoutOpts.Stretch([]bool{true}, []bool{false, true}),
GridLayoutOpts.Spacing(0, t.spacing))),
ContainerOpts.AutoDisableChildren(),
}...)...)
t.containerOpts = nil
buttonsContainer := NewContainer(
"tab book buttons",
ContainerOpts.Layout(NewRowLayout(
RowLayoutOpts.Spacing(t.buttonSpacing))))
t.container.AddChild(buttonsContainer)
for _, tab := range t.tabs {
tab := tab
b := NewStateButton(append(t.buttonOpts, []StateButtonOpt{
StateButtonOpts.StateImages(t.buttonImages),
StateButtonOpts.ButtonOpts(
ButtonOpts.Text(tab.label, t.buttonFace, t.buttonColor),
ButtonOpts.ClickedHandler(func(args *ButtonClickedEventArgs) {
t.SetTab(tab)
})),
}...)...)
buttonsContainer.AddChild(b)
t.tabToButton[tab] = b
}
t.buttonOpts = nil
t.buttonImages = nil
t.flipBook = NewFlipBook(append(t.flipBookOpts,
FlipBookOpts.ContainerOpts(ContainerOpts.AutoDisableChildren()))...)
t.container.AddChild(t.flipBook)
t.flipBookOpts = nil
t.setTab(t.tabs[0], false)
}
func (t *TabBook) SetTab(tab *TabBookTab) {
t.setTab(tab, true)
}
func (t *TabBook) setTab(tab *TabBookTab, fireEvent bool) {
if tab != t.tab {
previousTab := t.tab
t.tab = tab
t.flipBook.SetPage(tab.widget)
for bt, b := range t.tabToButton {
b.State = bt == tab
}
if fireEvent {
t.TabSelectedEvent.Fire(&TabBookTabSelectedEventArgs{
TabBook: t,
Tab: tab,
PreviousTab: previousTab,
})
}
}
}
func (t *TabBook) Tab() *TabBookTab {
return t.tab
}