-
Notifications
You must be signed in to change notification settings - Fork 7
/
view.go
463 lines (411 loc) · 9.88 KB
/
view.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package furex
import (
"fmt"
"image"
"strings"
"sync"
"github.com/hajimehoshi/ebiten/v2"
)
// View represents a UI element.
// You can set flex options, size, position and so on.
// Handlers can be set to create custom component such as button or list.
type View struct {
// TODO: Remove these fields in the future.
Left int
Right *int
Top int
Bottom *int
Width int
WidthInPct float64
Height int
HeightInPct float64
MarginLeft int
MarginTop int
MarginRight int
MarginBottom int
Position Position
Direction Direction
Wrap FlexWrap
Justify Justify
AlignItems AlignItem
AlignContent AlignContent
Grow float64
Shrink float64
Display Display
ID string
Raw string
TagName string
Text string
Attrs map[string]string
Hidden bool
Handler Handler
containerEmbed
flexEmbed
lock sync.Mutex
hasParent bool
parent *View
}
// Update updates the view
func (v *View) Update() {
if v.isDirty {
v.startLayout()
}
if !v.hasParent {
v.processHandler()
}
for _, v := range v.children {
v.item.Update()
v.item.processHandler()
}
if !v.hasParent {
v.processEvent()
}
}
func (v *View) processHandler() {
if u, ok := v.Handler.(UpdateHandler); ok {
u.HandleUpdate()
return
}
if u, ok := v.Handler.(Updater); ok {
u.Update(v)
return
}
}
func (v *View) startLayout() {
v.lock.Lock()
defer v.lock.Unlock()
if !v.hasParent {
v.frame = image.Rect(v.Left, v.Top, v.Left+v.Width, v.Top+v.Height)
}
v.flexEmbed.View = v
for _, child := range v.children {
if child.item.Position == PositionStatic {
child.item.startLayout()
}
}
v.layout(v.frame.Dx(), v.frame.Dy(), &v.containerEmbed)
v.isDirty = false
}
// UpdateWithSize the view with modified height and width
func (v *View) UpdateWithSize(width, height int) {
if !v.hasParent && (v.Width != width || v.Height != height) {
v.Height = height
v.Width = width
v.isDirty = true
}
v.Update()
}
// Layout marks the view as dirty
func (v *View) Layout() {
v.isDirty = true
if v.hasParent {
v.parent.isDirty = true
}
}
// Draw draws the view
func (v *View) Draw(screen *ebiten.Image) {
if v.isDirty {
v.startLayout()
}
if !v.hasParent {
v.handleDrawRoot(screen, v.frame)
}
if !v.Hidden && v.Display != DisplayNone {
v.containerEmbed.Draw(screen)
}
if Debug && !v.hasParent && v.Display != DisplayNone {
debugBorders(screen, v.containerEmbed)
}
}
// AddTo add itself to a parent view
func (v *View) AddTo(parent *View) *View {
if v.hasParent {
panic("this view has been already added to a parent")
}
parent.AddChild(v)
return v
}
// AddChild adds one or multiple child views
func (v *View) AddChild(views ...*View) *View {
for _, vv := range views {
v.addChild(vv)
}
return v
}
// RemoveChild removes a specified view
func (v *View) RemoveChild(cv *View) bool {
for i, child := range v.children {
if child.item == cv {
v.children = append(v.children[:i], v.children[i+1:]...)
v.isDirty = true
cv.hasParent = false
cv.parent = nil
return true
}
}
return false
}
// RemoveAll removes all children view
func (v *View) RemoveAll() {
v.isDirty = true
for _, child := range v.children {
child.item.hasParent = false
child.item.parent = nil
}
v.children = []*child{}
}
// PopChild remove the last child view add to this view
func (v *View) PopChild() *View {
if len(v.children) == 0 {
return nil
}
c := v.children[len(v.children)-1]
v.children = v.children[:len(v.children)-1]
v.isDirty = true
c.item.hasParent = false
c.item.parent = nil
return c.item
}
func (v *View) addChild(cv *View) *View {
child := &child{item: cv, handledTouchID: -1}
v.children = append(v.children, child)
v.isDirty = true
cv.hasParent = true
cv.parent = v
return v
}
func (v *View) isWidthFixed() bool {
return v.Width != 0 || v.WidthInPct != 0
}
func (v *View) width() int {
if v.Width == 0 {
return v.calculatedWidth
}
return v.Width
}
func (v *View) isHeightFixed() bool {
return v.Height != 0 || v.HeightInPct != 0
}
func (v *View) height() int {
if v.Height == 0 {
return v.calculatedHeight
}
return v.Height
}
func (v *View) getChildren() []*View {
if v == nil || v.children == nil {
return nil
}
ret := make([]*View, len(v.children))
for i, child := range v.children {
ret[i] = child.item
}
return ret
}
// GetByID returns the view with the specified id.
// It returns nil if not found.
func (v *View) GetByID(id string) (*View, bool) {
if v.ID == id {
return v, true
}
for _, child := range v.children {
if v, ok := child.item.GetByID(id); ok {
return v, true
}
}
return nil, false
}
// MustGetByID returns the view with the specified id.
// It panics if not found.
func (v *View) MustGetByID(id string) *View {
vv, ok := v.GetByID(id)
if !ok {
panic("view not found")
}
return vv
}
// SetLeft sets the left position of the view.
func (v *View) SetLeft(left int) {
v.Left = left
v.Layout()
}
// SetRight sets the right position of the view.
func (v *View) SetRight(right int) {
v.Right = Int(right)
v.Layout()
}
// SetTop sets the top position of the view.
func (v *View) SetTop(top int) {
v.Top = top
v.Layout()
}
// SetBottom sets the bottom position of the view.
func (v *View) SetBottom(bottom int) {
v.Bottom = Int(bottom)
v.Layout()
}
// SetWidth sets the width of the view.
func (v *View) SetWidth(width int) {
v.Width = width
v.Layout()
}
// SetHeight sets the height of the view.
func (v *View) SetHeight(height int) {
v.Height = height
v.Layout()
}
// SetMarginLeft sets the left margin of the view.
func (v *View) SetMarginLeft(marginLeft int) {
v.MarginLeft = marginLeft
v.Layout()
}
// SetMarginTop sets the top margin of the view.
func (v *View) SetMarginTop(marginTop int) {
v.MarginTop = marginTop
v.Layout()
}
// SetMarginRight sets the right margin of the view.
func (v *View) SetMarginRight(marginRight int) {
v.MarginRight = marginRight
v.Layout()
}
// SetMarginBottom sets the bottom margin of the view.
func (v *View) SetMarginBottom(marginBottom int) {
v.MarginBottom = marginBottom
v.Layout()
}
// SetPosition sets the position of the view.
func (v *View) SetPosition(position Position) {
v.Position = position
v.Layout()
}
// SetDirection sets the direction of the view.
func (v *View) SetDirection(direction Direction) {
v.Direction = direction
v.Layout()
}
// SetWrap sets the wrap property of the view.
func (v *View) SetWrap(wrap FlexWrap) {
v.Wrap = wrap
v.Layout()
}
// SetJustify sets the justify property of the view.
func (v *View) SetJustify(justify Justify) {
v.Justify = justify
v.Layout()
}
// SetAlignItems sets the align items property of the view.
func (v *View) SetAlignItems(alignItems AlignItem) {
v.AlignItems = alignItems
v.Layout()
}
// SetAlignContent sets the align content property of the view.
func (v *View) SetAlignContent(alignContent AlignContent) {
v.AlignContent = alignContent
v.Layout()
}
// SetGrow sets the grow property of the view.
func (v *View) SetGrow(grow float64) {
v.Grow = grow
v.Layout()
}
// SetShrink sets the shrink property of the view.
func (v *View) SetShrink(shrink float64) {
v.Shrink = shrink
v.Layout()
}
// SetDisplay sets the display property of the view.
func (v *View) SetDisplay(display Display) {
v.Display = display
v.Layout()
}
// SetHidden sets the hidden property of the view.
func (v *View) SetHidden(hidden bool) {
v.Hidden = hidden
v.Layout()
}
func (v *View) Config() ViewConfig {
cfg := ViewConfig{
TagName: v.TagName,
ID: v.ID,
Left: v.Left,
Right: v.Right,
Top: v.Top,
Bottom: v.Bottom,
Width: v.Width,
Height: v.Height,
MarginLeft: v.MarginLeft,
MarginTop: v.MarginTop,
MarginRight: v.MarginRight,
MarginBottom: v.MarginBottom,
Position: v.Position,
Direction: v.Direction,
Wrap: v.Wrap,
Justify: v.Justify,
AlignItems: v.AlignItems,
AlignContent: v.AlignContent,
Grow: v.Grow,
Shrink: v.Shrink,
children: []ViewConfig{},
}
for _, child := range v.getChildren() {
cfg.children = append(cfg.children, child.Config())
}
return cfg
}
func (v *View) handleDrawRoot(screen *ebiten.Image, b image.Rectangle) {
if h, ok := v.Handler.(DrawHandler); ok {
h.HandleDraw(screen, b)
return
}
if h, ok := v.Handler.(Drawer); ok {
h.Draw(screen, b, v)
}
}
// This is for debugging and testing.
type ViewConfig struct {
TagName string
ID string
Left int
Right *int
Top int
Bottom *int
Width int
Height int
MarginLeft int
MarginTop int
MarginRight int
MarginBottom int
Position Position
Direction Direction
Wrap FlexWrap
Justify Justify
AlignItems AlignItem
AlignContent AlignContent
Grow float64
Shrink float64
children []ViewConfig
}
func (cfg ViewConfig) Tree() string {
return cfg.tree("")
}
// TODO: This is a bit of a mess. Clean it up.
func (cfg ViewConfig) tree(indent string) string {
sb := &strings.Builder{}
sb.WriteString(fmt.Sprintf("%s<%s ", indent, cfg.TagName))
if cfg.ID != "" {
sb.WriteString(fmt.Sprintf("id=\"%s\" ", cfg.ID))
}
sb.WriteString("style=\"")
sb.WriteString(
fmt.Sprintf("left: %d, right: %d, top: %d, bottom: %d, width: %d, height: %d, marginLeft: %d, marginTop: %d, marginRight: %d, marginBottom: %d, position: %s, direction: %s, wrap: %s, justify: %s, alignItems: %s, alignContent: %s, grow: %f, shrink: %f",
cfg.Left, *cfg.Right, cfg.Top, *cfg.Bottom, cfg.Width, cfg.Height, cfg.MarginLeft, cfg.MarginTop, cfg.MarginRight, cfg.MarginBottom, cfg.Position, cfg.Direction, cfg.Wrap, cfg.Justify, cfg.AlignItems, cfg.AlignContent, cfg.Grow, cfg.Shrink))
sb.WriteString("\">\n")
for _, child := range cfg.children {
sb.WriteString(child.tree(indent + " "))
sb.WriteString("\n")
}
sb.WriteString(fmt.Sprintf("%s</%s>", indent, cfg.TagName))
sb.WriteString("\n")
return sb.String()
}