forked from aykevl/tinygl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
643 lines (585 loc) · 18.9 KB
/
container.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package tinygl
import (
"tinygo.org/x/drivers/pixel"
)
type VBox[T pixel.Color] struct {
Rect[T]
children []Object[T]
childHeights []int16
}
func NewVBox[T pixel.Color](background T, children ...Object[T]) *VBox[T] {
box := &VBox[T]{}
box.children = children
box.childHeights = make([]int16, len(children))
for _, child := range children {
child.SetParent(box)
}
box.Rect.background = background
box.flags |= flagNeedsLayout | flagNeedsUpdate | flagNeedsChildUpdate
return box
}
// RequestUpdate will request an update for this object and all of its children.
func (b *VBox[T]) RequestUpdate() {
b.Rect.RequestUpdate()
for _, child := range b.children {
child.RequestUpdate()
}
}
// MinSize returns the minimal size to fit around all the elements in this
// container.
func (b *VBox[T]) MinSize() (width, height int) {
for _, child := range b.children {
childWidth, childHeight := child.MinSize()
if childWidth > width {
width = childWidth
}
height += childHeight
}
return
}
func (b *VBox[T]) Layout(width, height int) {
if b.flags&flagNeedsLayout == 0 {
return // nothing to do
}
// Determine minimal size of all children.
var minHeightSum int
var growableSum int
for _, child := range b.children {
_, childGrowable := child.growable()
_, childHeight := child.MinSize()
minHeightSum += childHeight
growableSum += childGrowable
}
// Go through each child and determine its size.
// The 'leftover' parts are the extra pixels at the bottom of the VBox.
leftoverHeight := height - minHeightSum
leftoverGrowable := growableSum
if leftoverHeight < 0 {
leftoverHeight = 0 // don't shrink children when the VBox is full
}
childY := 0 // Y relative to the VBox start
previousChildY := 0
for i, child := range b.children {
// Determine child size.
_, childGrowable := child.growable()
_, childHeight := child.MinSize()
if childGrowable != 0 {
growPixels := leftoverHeight * childGrowable / leftoverGrowable
childHeight += growPixels
leftoverHeight -= growPixels
leftoverGrowable -= childGrowable
}
child.Layout(width, childHeight)
// Check whether the child changed position or size.
if childHeight != int(b.childHeights[i]) || childY != previousChildY {
child.RequestUpdate()
}
// Keep track of the child height.
previousChildY += int(b.childHeights[i])
childY += childHeight
b.childHeights[i] = int16(childHeight)
}
if childY != previousChildY && leftoverHeight != 0 {
// Redraw the area at the bottom of the VBox (the "slack").
b.Rect.RequestUpdate()
}
b.flags &^= flagNeedsLayout
}
func (b *VBox[T]) Update(screen *Screen[T], displayX, displayY, displayWidth, displayHeight, x, y int) {
if b.flags&(flagNeedsUpdate|flagNeedsChildUpdate) == 0 {
return
}
childOffset := 0
for i, child := range b.children {
childHeight := int(b.childHeights[i])
childDisplayY := displayY - y + childOffset
childY := 0
childDisplayHeight := int(b.childHeights[i])
// Cut off the top part if it is outside the update area.
if childDisplayY < displayY {
childDisplayHeight -= displayY - childDisplayY
childY = displayY - childDisplayY
childDisplayY = displayY
}
// Cut off the bottom part if it is outside the update area.
if childDisplayY+childDisplayHeight > displayY+displayHeight {
childDisplayHeight = (displayY + displayHeight) - childDisplayY
}
if childDisplayHeight > 0 {
child.Update(screen, displayX, childDisplayY, displayWidth, childDisplayHeight, x, childY)
}
childOffset += childHeight
}
slackTop := displayY - y + childOffset
slackBottom := displayY + displayHeight
slackHeight := slackBottom - slackTop
if b.flags&flagNeedsUpdate != 0 && slackHeight > 0 {
PaintSolidColor(screen, b.background, displayX, slackTop, displayWidth, slackHeight)
}
}
func (b *VBox[T]) MarkUpdated() {
if _, anyChild := b.NeedsUpdate(); anyChild {
for _, child := range b.children {
child.MarkUpdated()
}
}
b.Rect.MarkUpdated()
}
// HandleEvent propagates an event to its children.
func (b *VBox[T]) HandleEvent(event Event, x, y int) {
childY := 0
for i, child := range b.children {
childHeight := b.childHeights[i]
if y >= childY && y < childY+int(childHeight) {
child.HandleEvent(event, x, y-childY)
}
childY += int(childHeight)
}
}
// ScrollBox is a scrollable wrapper of an object.
// It is growable and reports a minimal size of (0, 0) to the parent, so that it
// will take up the remaining space in the parent box.
type ScrollBox[T pixel.Color] struct {
Rect[T]
child Object[T]
offsetX int
offsetY int
maxOffsetX int
maxOffsetY int
height int16
lastTouchX int16
lastTouchY int16
}
// NewScrollBox returns an initialized scroll box with the given child.
// If you want to scroll multiple children (vertically, for example), you can
// use a VBox instead.
func NewScrollBox[T pixel.Color](child Object[T]) *ScrollBox[T] {
box := &ScrollBox[T]{
child: child,
}
child.SetParent(box)
return box
}
func (b *ScrollBox[T]) MinSize() (width, height int) {
// A scroll area should be set to expand.
return 0, 0
}
func (b *ScrollBox[T]) growable() (horizontal, vertical int) {
// A scroll box is always growable.
return 1, 1
}
func (b *ScrollBox[T]) SetGrowable(horizontal, vertical int) {
// No-op, because the scroll area is always growable.
}
func (b *ScrollBox[T]) Layout(width, height int) {
// Allow the child to use its entire MinSize if it wants to (stretching it
// to the parent object if needed).
b.height = int16(height)
minWidth, minHeight := b.child.MinSize()
if width < minWidth {
b.maxOffsetX = minWidth - width
width = minWidth
} else {
b.maxOffsetX = 0
}
if height < minHeight {
b.maxOffsetY = minHeight - height
height = minHeight
} else {
b.maxOffsetY = 0
}
b.child.Layout(width, height)
}
func (b *ScrollBox[T]) Update(screen *Screen[T], displayX, displayY, displayWidth, displayHeight, x, y int) {
// Redraw the child (if needed) with an offset.
b.child.Update(screen, displayX, displayY, displayWidth, displayHeight, x+b.offsetX, y+b.offsetY)
}
func (b *ScrollBox[T]) MarkUpdated() {
b.Rect.MarkUpdated()
b.child.MarkUpdated()
}
func (b *ScrollBox[T]) HandleEvent(event Event, x, y int) {
switch event {
case TouchStart:
b.lastTouchX = int16(x)
b.lastTouchY = int16(y)
case TouchMove:
// Add the last distance moved to the offset.
offsetX := b.offsetX + (int(b.lastTouchX) - x)
if offsetX < 0 {
offsetX = 0
}
if offsetX > b.maxOffsetX {
offsetX = b.maxOffsetX
}
offsetY := b.offsetY + (int(b.lastTouchY) - y)
if offsetY < 0 {
offsetY = 0
}
if offsetY > b.maxOffsetY {
offsetY = b.maxOffsetY
}
if offsetX != b.offsetX || offsetY != b.offsetY {
// The offset changed, so redraw the child.
// One example where this does not happen, is when scrolling further
// than is possible in the scroll area. In that case, there is
// nothing to update as the child position didn't change even if the
// touch point changed.
// TODO: use hardware scrolling if available.
b.offsetX = offsetX
b.offsetY = offsetY
b.RequestUpdate()
}
b.lastTouchX = int16(x)
b.lastTouchY = int16(y)
case TouchTap:
// Pass tap events to the child (with the scroll offset).
b.child.HandleEvent(event, x+b.offsetX, y+b.offsetY)
}
}
func (b *ScrollBox[T]) RequestUpdate() {
b.child.RequestUpdate()
}
func (b *ScrollBox[T]) ScrollIntoViewVertical(top, bottom int, child Object[T]) {
if top < b.offsetY {
// Scroll up if 'top' is above the visible area.
b.offsetY = top
b.RequestUpdate()
} else {
// Scroll down if 'bottom' is below the visible area.
bottomScrollOffset := bottom - int(b.height)
if bottomScrollOffset > b.maxOffsetY {
bottomScrollOffset = b.maxOffsetY
}
if bottomScrollOffset < b.offsetY {
bottomScrollOffset = b.offsetY
}
if bottomScrollOffset != b.offsetY {
b.offsetY = bottomScrollOffset
b.RequestUpdate()
}
}
}
// VerticalScrollBox is a scrollable wrapper of an object that will use hardware
// acceleration when available.
// It is growable and reports a minimal size of (0, 0) to the parent, so that it
// will take up the remaining space in the parent box.
type VerticalScrollBox[T pixel.Color] struct {
Rect[T]
child Object[T]
top Object[T] // top fixed object, or nil
bottom Object[T] // bottom fixed object, or nil
scrollOffset int // current distance from the top
lastScrollOffset int // scroll offset in previous Update call
maxScrollOffset int // max value for scrollOffset
lastTouchY int16
childHeight int16
bottomHeight int16
topHeight int16
}
// NewVerticalScrollBox returns an initialized scroll box with the given child.
// If you want to scroll multiple children (vertically, for example), you can
// use a VBox to wrap these children.
func NewVerticalScrollBox[T pixel.Color](top, child, bottom Object[T]) *VerticalScrollBox[T] {
b := &VerticalScrollBox[T]{
top: top,
child: child,
bottom: bottom,
}
if top != nil {
top.SetParent(b)
}
if bottom != nil {
bottom.SetParent(b)
}
child.SetParent(b)
return b
}
func (b *VerticalScrollBox[T]) MinSize() (width, height int) {
// The scroll area takes up whatever space the parent gives it.
var topH, bottomH int
if b.top != nil {
_, topH = b.top.MinSize()
}
if b.bottom != nil {
_, bottomH = b.bottom.MinSize()
}
return 0, topH + bottomH
}
func (b *VerticalScrollBox[T]) growable() (horizontal, vertical int) {
// A scroll box is always growable.
return 1, 1
}
func (b *VerticalScrollBox[T]) SetGrowable(horizontal, vertical int) {
// No-op, because the scroll area is always growable.
}
func (b *VerticalScrollBox[T]) Layout(width, height int) {
// Layout the top area.
if b.top != nil {
_, topH := b.top.MinSize()
if topH > height {
topH = height
}
if topH != int(b.topHeight) {
b.topHeight = int16(topH)
b.top.Layout(width, topH)
b.top.RequestUpdate()
b.child.RequestUpdate()
}
height -= topH
}
// Layout the bottom area.
if b.bottom != nil {
_, bottomH := b.bottom.MinSize()
if bottomH > height {
bottomH = height
}
if bottomH != int(b.bottomHeight) {
b.bottomHeight = int16(bottomH)
b.bottom.Layout(width, bottomH)
b.bottom.RequestUpdate()
b.child.RequestUpdate()
}
height -= bottomH
}
// Allow the child to use its entire MinSize if it wants to (stretching it
// to the parent object if needed).
_, childH := b.child.MinSize()
if childH > height {
b.maxScrollOffset = childH - height
childH = height
} else {
b.maxScrollOffset = 0
}
if childH != int(b.childHeight) {
b.childHeight = int16(childH)
b.child.Layout(width, height)
b.child.RequestUpdate()
}
}
func (b *VerticalScrollBox[T]) Update(screen *Screen[T], displayX, displayY, displayWidth, displayHeight, x, y int) {
if b.flags&(flagNeedsUpdate|flagNeedsChildUpdate) == 0 {
return
}
// The code below assumes x and y are both 0.
if x != 0 || y != 0 {
panic("todo: x and y inside VerticalScrollBox")
}
// Draw fixed top area.
if b.top != nil {
b.top.Update(screen, displayX, displayY, displayWidth, int(b.topHeight), 0, 0)
displayY += int(b.topHeight)
displayHeight -= int(b.topHeight)
}
// Draw fixed bottom area.
if b.bottom != nil {
bottomH := int(b.bottomHeight)
b.bottom.Update(screen, displayX, displayY+displayHeight-bottomH, displayWidth, bottomH, 0, 0)
displayHeight -= bottomH
}
// Now follows updating the complicated part: updating the child.
// We return after it has been updated.
// Try to modify the screen scroll mode.
hwscroll := false
if b.Rect.parent == nil {
line := int(b.topHeight) + b.scrollOffset%int(b.childHeight)
hwscroll = screen.setScroll(b.topHeight, b.bottomHeight, int16(line))
}
// Draw the scrollable child.
if !hwscroll {
if b.scrollOffset != b.lastScrollOffset {
// Fallback: update entire child on scroll.
b.child.RequestUpdate()
}
b.lastScrollOffset = b.scrollOffset
b.child.Update(screen, displayX, displayY, displayWidth, displayHeight, 0, b.scrollOffset)
return
}
// Check whether the entire screen was changed, in which case we simply
// update everything.
diff := b.scrollOffset - b.lastScrollOffset
if diff >= int(b.childHeight) || diff <= -int(b.childHeight) {
b.child.RequestUpdate()
b.child.Update(screen, displayX, displayY, displayWidth, displayHeight, 0, b.scrollOffset)
b.lastScrollOffset = b.scrollOffset
return
}
// TODO: the code below is way too complex. I'm pretty sure this can be
// simplified a great deal, but my head already hurts thinking through the
// code as-is.
// The screen may have been moved, but at least some part is still visible.
if b.scrollOffset > b.lastScrollOffset {
// Moved up.
newArea := b.scrollOffset - b.lastScrollOffset // size of newly exposed area at the bottom
existingArea := int(b.childHeight) - newArea // size of moved area at the top
// Update the moved part at the top.
line := (int(b.childHeight) - b.scrollOffset) % int(b.childHeight)
if line >= existingArea {
b.child.Update(screen, displayX, displayY+b.scrollOffset%int(b.childHeight), displayWidth, existingArea, 0, b.scrollOffset)
} else {
height1 := line
height2 := existingArea - line
if height1 > 0 {
b.child.Update(screen, displayX, displayY+b.scrollOffset%int(b.childHeight), displayWidth, height1, 0, b.scrollOffset)
}
if height2 > 0 {
b.child.Update(screen, displayX, displayY, displayWidth, height2, 0, b.scrollOffset+height1)
}
}
// Update the newly exposed area at the bottom, using a bit of a hack:
// by requesting an update and then drawing only the newly exposed part.
b.child.RequestUpdate()
scrollLine := (b.scrollOffset + existingArea) % int(b.childHeight)
if scrollLine+newArea >= displayHeight {
// The newly exposed area straddles the scroll line.
// Therefore, we have to do the update in two parts, one before the
// scroll line and one after.
updateHeight1 := displayHeight - scrollLine
updateHeight2 := newArea - updateHeight1
b.child.Update(screen, displayX, displayY+scrollLine, displayWidth, updateHeight1, 0, b.scrollOffset+existingArea)
b.child.Update(screen, displayX, displayY, displayWidth, updateHeight2, 0, b.scrollOffset+existingArea+updateHeight1)
} else {
// The newly exposed area doesn't cross a scroll line, so we can do
// this update in one go.
b.child.Update(screen, displayX, displayY+scrollLine, displayWidth, newArea, 0, b.scrollOffset+existingArea)
}
} else if b.scrollOffset < b.lastScrollOffset {
// Moved down.
newArea := b.lastScrollOffset - b.scrollOffset // size of newly exposed area at the top
existingArea := int(b.childHeight) - newArea // size of moved area at the bottom
// Update the moved part at the bottom.
line := (int(b.childHeight) - b.scrollOffset) % int(b.childHeight)
if line <= newArea {
b.child.Update(screen, displayX, displayY+(b.scrollOffset+newArea)%int(b.childHeight), displayWidth, existingArea, 0, b.scrollOffset+newArea)
} else {
height1 := line - newArea
height2 := existingArea - height1
if height1 > 0 {
b.child.Update(screen, displayX, displayY+displayHeight-height1, displayWidth, height1, 0, b.scrollOffset+newArea)
}
if height2 > 0 {
b.child.Update(screen, displayX, displayY, displayWidth, height2, 0, b.scrollOffset+newArea+height1)
}
}
// Update the newly exposed area at the top, using a bit of a hack:
// by requesting an update and then drawing only the newly exposed part.
b.child.RequestUpdate()
scrollLine := b.scrollOffset % int(b.childHeight)
if scrollLine+newArea >= displayHeight {
// The newly exposed area straddles the scroll line.
// Therefore, we have to do the update in two parts, one before the
// scroll line and one after.
updateHeight1 := displayHeight - scrollLine
updateHeight2 := newArea - updateHeight1
b.child.Update(screen, displayX, displayY+scrollLine, displayWidth, updateHeight1, 0, b.scrollOffset)
b.child.Update(screen, displayX, displayY, displayWidth, updateHeight2, 0, b.scrollOffset+updateHeight1)
} else {
// The newly exposed area doesn't cross a scroll line, so we can do
// this update in one go.
b.child.Update(screen, displayX, displayY+scrollLine, displayWidth, newArea, 0, b.scrollOffset)
}
} else {
// No scrolling happened. Update the child if needed.
scrollLine := b.scrollOffset % int(b.childHeight)
height1 := displayHeight - scrollLine
height2 := scrollLine
if height1 > 0 {
b.child.Update(screen, displayX, displayY+scrollLine, displayWidth, height1, 0, b.scrollOffset)
}
if height2 > 0 {
b.child.Update(screen, displayX, displayY, displayWidth, height2, 0, b.scrollOffset+height1)
}
}
b.lastScrollOffset = b.scrollOffset
}
func (b *VerticalScrollBox[T]) MarkUpdated() {
b.Rect.MarkUpdated()
if b.top != nil {
b.top.MarkUpdated()
}
if b.bottom != nil {
b.bottom.MarkUpdated()
}
b.child.MarkUpdated()
}
func (b *VerticalScrollBox[T]) HandleEvent(event Event, x, y int) {
switch event {
case TouchStart:
b.lastTouchY = int16(y)
case TouchMove:
// Add the last distance moved to the scrollOffset.
scrollOffset := b.scrollOffset + (int(b.lastTouchY) - y)
if scrollOffset < 0 {
scrollOffset = 0
}
if scrollOffset > b.maxScrollOffset {
scrollOffset = b.maxScrollOffset
}
if scrollOffset != b.scrollOffset {
b.scrollOffset = scrollOffset
b.Rect.RequestUpdate()
}
b.lastTouchY = int16(y)
case TouchTap:
if y < int(b.topHeight) {
b.top.HandleEvent(event, x, y)
} else if y < int(b.topHeight)+int(b.childHeight) {
b.child.HandleEvent(event, x, y-int(b.topHeight)+b.scrollOffset)
} else {
b.bottom.HandleEvent(event, x, y-int(b.topHeight)-int(b.childHeight))
}
}
}
func (b *VerticalScrollBox[T]) RequestUpdate() {
b.Rect.RequestUpdate()
if b.top != nil {
b.top.RequestUpdate()
}
if b.bottom != nil {
b.bottom.RequestUpdate()
}
b.child.RequestUpdate()
}
func (b *VerticalScrollBox[T]) ScrollIntoViewVertical(top, bottom int, child Object[T]) {
if child != b.child || b.maxScrollOffset == 0 {
return
}
if top < b.scrollOffset {
// Scroll up if 'top' is above the visible area.
b.scrollOffset = top
b.Rect.RequestUpdate()
} else {
// Scroll down if 'bottom' is below the visible area.
bottomScrollOffset := bottom - int(b.childHeight)
if bottomScrollOffset > b.maxScrollOffset {
bottomScrollOffset = b.maxScrollOffset
}
if bottomScrollOffset < b.scrollOffset {
bottomScrollOffset = b.scrollOffset
}
if bottomScrollOffset != b.scrollOffset {
b.scrollOffset = bottomScrollOffset
b.Rect.RequestUpdate()
}
}
}
// EventBox wraps an object and handles events for it.
type EventBox[T pixel.Color] struct {
Object[T]
handleEvent func(event Event, x, y int)
}
// Create a new wrapper container that handles events.
func NewEventBox[T pixel.Color](child Object[T]) *EventBox[T] {
return &EventBox[T]{
Object: child,
}
}
// SetEventHandler sets the event callback for this object.
func (b *EventBox[T]) SetEventHandler(handler func(event Event, x, y int)) {
b.handleEvent = handler
}
func (b *EventBox[T]) HandleEvent(event Event, x, y int) {
if b.handleEvent != nil {
b.handleEvent(event, x, y)
}
}