-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayout.lua
652 lines (575 loc) · 16.5 KB
/
playout.lua
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
644
645
646
647
648
649
650
651
652
local gfx <const> = playdate.graphics
local geo <const> = playdate.geometry
-- inherit property values from parent nodes
local function inheritProperty(name, node, fallback)
local cursor = node
local value
local style
repeat
style = cursor.properties.style or {}
value = style[name] or cursor.properties[name]
cursor = cursor.parent
until value or (not cursor)
return value or fallback
end
-- generate a list of offsets for drawing stroke outlines
local function strokeOffsets(radius)
local offsets = {}
for x = -radius, radius do
for y = -radius, radius do
if x * x + y * y <= radius * radius then
table.insert(offsets, { x, y })
end
end
end
return offsets
end
local function round(n)
return math.floor(n)
end
local floor = math.floor
local function tableFromDefaults(options, defaults)
options = options or {}
local t = table.shallowcopy(options)
for key, val in pairs(defaults) do
t[key] = options[key] or defaults[key]
end
return t
end
-- constants for box layout
local kDirectionVertical = 1
local kDirectionHorizontal = 2
local kAlignStart = 1
local kAlignCenter = 2
local kAlignEnd = 3
local kAlignStretch = 4
local kAnchorTopLeft = 1
local kAnchorTopCenter = 2
local kAnchorTopRight = 3
local kAnchorCenterLeft = 4
local kAnchorCenter = 5
local kAnchorCenterRight = 6
local kAnchorBottomLeft = 7
local kAnchorBottomCenter = 8
local kAnchorBottomRight = 9
-- generic box node
local box = {}
box.__index = box
local defaultBoxProperties = {
minWidth = 1,
minHeight = 1,
maxWidth = 400,
maxHeight = 240,
width = nil,
height = nil,
scroll = false,
direction = kDirectionVertical,
padding = 0,
paddingTop = nil,
paddingLeft = nil,
paddingRight = nil,
paddingBottom = nil,
backgroundColor = nil,
backgroundAlpha = nil,
nineSlice = nil,
hAlign = kAlignCenter,
vAlign = kAlignCenter,
selfAlign = nil,
border = 0,
borderColor = gfx.kColorBlack,
borderRadius = 0,
spacing = 0,
font = nil,
fontFamily = nil,
flex = nil,
shadow = nil,
shadowAlpha = 0
}
local defaultDrawContext = {
maxWidth = math.huge,
maxHeight = math.huge
}
function box.new(properties, children)
properties = properties or {}
local o = {
properties = tableFromDefaults(properties, defaultBoxProperties),
children = children or {},
childRects = nil,
parent = nil,
scrollPos = 0,
style = properties.style or nil
}
setmetatable(o, box)
return o
end
function box:appendChild(child)
table.insert(self.children, child)
child.parent = self
end
function box:insertChild(position, child)
table.insert(self.children, position, child)
child.parent = self
end
function box:layout(context)
context = tableFromDefaults(context, defaultDrawContext)
local props = self.properties
if self.style then props = tableFromDefaults(self.style, props) end
local constrainedWidth = math.min(context.maxWidth, (props.width or props.maxWidth))
local constrainedHeight = math.min(context.maxHeight, (props.height or props.maxHeight))
local isVertical = props.direction == kDirectionVertical
local children = self.children
if props.scroll then
if isVertical then
constrainedHeight = math.huge
else
constrainedWidth = math.huge
end
end
-- compute padding from shorthands
local paddingTop = props.paddingTop or props.padding
local paddingLeft = props.paddingLeft or props.padding
local paddingRight = props.paddingRight or props.paddingLeft or props.padding
local paddingBottom = props.paddingBottom or props.paddingTop or props.padding
local shadow = props.shadow or 0
local availableWidth = constrainedWidth - paddingLeft - paddingRight
local availableHeight = constrainedHeight - paddingTop - paddingBottom - shadow
local childRects = {}
self.childRects = childRects
local child
local childFlex
local remainingHeight = availableHeight
local remainingWidth = availableWidth
local intrinsicWidth = 0
local intrinsicHeight = 0
local totalFlex = 0
for i = 1, #children do
if i > 1 then
if isVertical then
remainingHeight = remainingHeight - props.spacing
intrinsicHeight = intrinsicHeight + props.spacing
else
remainingWidth = remainingWidth - props.spacing
intrinsicWidth = intrinsicWidth + props.spacing
end
end
childFlex = nil
child = children[i]
-- determine intrinsic size of child node
local childRect = child:layout({
maxWidth = remainingWidth,
maxHeight = remainingHeight,
path = context.path .. '.' .. (child.properties.id or i)
})
-- accumulate flex if specified
childFlex = child.properties.flex
childRects[i] = childRect
if childFlex then
totalFlex = totalFlex + child.properties.flex
end
if isVertical then
if not childFlex then
remainingHeight = remainingHeight - childRect.height
end
intrinsicHeight = intrinsicHeight + childRect.height
intrinsicWidth = math.max(intrinsicWidth, childRect.width)
else
if not childFlex then
remainingWidth = remainingWidth - childRect.width
end
intrinsicWidth = intrinsicWidth + childRect.width
intrinsicHeight = math.max(intrinsicHeight, childRect.height)
end
end
local actualWidth = constrainedWidth
local actualHeight = constrainedHeight
if not props.width then
if (not isVertical) and totalFlex > 0 then
actualWidth = constrainedWidth
else
actualWidth = math.max(props.minWidth, math.min(intrinsicWidth + paddingLeft + paddingRight, props.maxWidth))
remainingWidth = 0
end
end
if not props.height then
if isVertical and totalFlex > 0 then
actualHeight = constrainedHeight
else
actualHeight = math.max(props.minHeight, math.min(intrinsicHeight + paddingTop + paddingBottom + shadow, props.maxHeight))
remainingHeight = 0
end
end
local rect = geo.rect.new(0, 0, floor(actualWidth), floor(actualHeight))
local innerWidth = actualWidth - paddingLeft - paddingRight
local innerHeight = actualHeight - paddingTop - paddingBottom - shadow
local child, x, y, childProps, align, flexRatio
-- set initial layout position
if isVertical then
y = paddingTop
if totalFlex == 0 then
if props.vAlign == kAlignCenter then y = y + floor(remainingHeight / 2) end
if props.vAlign == kAlignEnd then y = y + remainingHeight end
end
else
x = paddingLeft
if totalFlex == 0 then
if props.hAlign == kAlignCenter then x = x + floor(remainingWidth / 2) end
if props.hAlign == kAlignEnd then x = x + remainingWidth end
end
end
for i = 1, #childRects do
child = childRects[i]
childProps = children[i].properties or {}
childFlex = childProps.flex
local align
if isVertical then
x = paddingLeft
align = childProps.selfAlign or props.hAlign
if align == kAlignCenter then x = x + round(innerWidth - child.width) / 2 end
if align == kAlignEnd then x = x + innerWidth - child.width end
else
y = paddingTop
align = childProps.selfAlign or props.vAlign
if align == kAlignCenter then y = y + round(innerHeight - child.height) / 2 end
if align == kAlignEnd then y = y + innerHeight - child.height end
end
-- generate final layout rect for child node
if childFlex then
flexRatio = childFlex / totalFlex
if isVertical then
if align == kAlignStretch then
childRects[i] = geo.rect.new(x, y, innerWidth, floor(flexRatio * remainingHeight))
else
childRects[i] = geo.rect.new(x, y, child.width, floor(flexRatio * remainingHeight))
end
else
if align == kAlignStretch then
childRects[i] = geo.rect.new(x, y, floor(flexRatio * remainingWidth), innerHeight)
else
childRects[i] = geo.rect.new(x, y, floor(flexRatio * remainingWidth), child.height)
end
end
child = childRects[i]
else
if align == kAlignStretch then
if isVertical then
child.width = innerWidth
else
child.height = innerHeight
end
end
child:offset(x, y)
end
-- move positioning cursor to next position
if isVertical then
y = y + child.height + props.spacing
else
x = x + child.width + props.spacing
end
end
return rect
end
function box:draw(rect)
local props = self.properties
if self.style then props = tableFromDefaults(self.style, props) end
local border = props.border
self.rect = rect
local r = rect
if props.backgroundColor then
if props.shadow then
gfx.setColor(gfx.kColorBlack)
gfx.setDitherPattern(props.shadowAlpha or 0)
gfx.fillRoundRect(r, props.borderRadius)
gfx.setColor(props.backgroundColor)
if props.backgroundAlpha then
gfx.setDitherPattern(props.backgroundAlpha)
end
gfx.fillRoundRect(r.x, r.y, r.width, r.height - props.shadow, props.borderRadius)
else
gfx.setColor(props.backgroundColor)
if props.backgroundAlpha then
gfx.setDitherPattern(props.backgroundAlpha)
end
gfx.fillRoundRect(r, props.borderRadius)
end
end
-- figure out why nineslice leaks memory all over the place
-- if props.nineSlice then
-- props.nineSlice:drawInRect(rect)
-- end
if border > 0 then
gfx.setColor(props.borderColor)
gfx.setLineWidth(border)
gfx.setStrokeLocation(gfx.kStrokeInside)
if props.shadow then
gfx.drawRoundRect(r.x, r.y, r.width, r.height - props.shadow, props.borderRadius)
else
gfx.drawRoundRect(r, props.borderRadius)
end
end
for i = 1, #self.children do
local child = self.children[i]
local drawRect = self.childRects[i]:offsetBy(r.x, r.y)
child:draw(drawRect)
end
end
-- text node
local text = {}
text.__index = text
local defaultTextProperties = {
font = nil,
fontFamily = nil,
alignment = kTextAlignment.left,
leading = nil,
flex = nil,
wrap = true,
stroke = 0,
color = nil
}
function text.new(textContent, properties)
properties = properties or {}
local o = {
text = textContent,
properties = tableFromDefaults(properties, defaultTextProperties),
parent = nil,
style = properties.style or nil
}
setmetatable(o, text)
return o
end
function text:layout(context)
context = tableFromDefaults(context, defaultDrawContext)
local props = self.properties
if self.style then props = tableFromDefaults(self.style, props) end
local maxWidth = context.maxWidth or math.huge
local maxHeight = context.maxHeight or math.huge
gfx.pushContext()
local font = inheritProperty("font", self, nil)
local fontFamily = inheritProperty("fontFamily", self, nil)
if font then
gfx.setFont(font)
end
if fontFamily then
gfx.setFontFamily(fontFamily)
end
if props.wrap then
local idealWidth, idealHeight = gfx.getTextSizeForMaxWidth(self.text, maxWidth, props.leading)
local width = idealWidth
local height = math.min(idealHeight, maxHeight)
gfx.popContext()
return geo.rect.new(0, 0, width, height)
end
local width, height = gfx.getTextSize(self.text)
gfx.popContext()
return geo.rect.new(0, 0, math.min(width, maxWidth), height)
end
function text:draw(rect)
local props = self.properties
local alignment = props.alignment;
self.rect = rect
gfx.pushContext()
local font = inheritProperty("font", self, nil)
local fontFamily = inheritProperty("fontFamily", self, nil)
if font then
gfx.setFont(font)
end
if fontFamily then
gfx.setFontFamily(fontFamily)
end
if props.stroke > 0 then
local offsets = strokeOffsets(props.stroke)
gfx.setImageDrawMode(gfx.kDrawModeFillWhite)
for _, o in pairs(offsets) do
gfx.drawTextInRect(self.text, rect:offsetBy(o[1], o[2]), props.leading, "...", alignment)
end
gfx.setImageDrawMode(gfx.kDrawModeCopy)
end
if props.color then
if props.color == gfx.kColorBlack then
gfx.setImageDrawMode(gfx.kDrawModeFillBlack)
end
if props.color == gfx.kColorWhite then
gfx.setImageDrawMode(gfx.kDrawModeFillWhite)
end
end
gfx.drawTextInRect(self.text, rect, props.leading, "...", alignment)
gfx.setImageDrawMode(gfx.kDrawModeCopy)
gfx.popContext()
end
-- image node
local image = {}
image.__index = image
local defaultImageProperties = {
}
function image.new(img, properties)
properties = properties or {}
local o = {
img = img,
properties = tableFromDefaults(properties, defaultImageProperties),
parent = nil,
width = img.width,
height = img.height
}
setmetatable(o, image)
return o
end
function image:layout(context)
return geo.rect.new(0, 0, self.width, self.height)
end
function image:draw(rect)
self.rect = rect
self.img:draw(rect.x, rect.y)
end
-- top-level tree
local tree = {}
tree.__index = tree
local treeBuilders = {
box = box.new,
image = image.new,
text = text.new
}
local defaultTreeOptions = {
useCache = true
}
function tree.new(root, options)
options = tableFromDefaults(options, defaultTreeOptions)
local o = {
root = root or box.new(),
rect = nil,
img = nil,
sprite = nil,
useCache = options.useCache,
tabIndex = nil
}
if o.useCache then
o.cache = {}
end
setmetatable(o, tree)
-- set parent-child relationships
function walk(node)
if node.children then
for i = 1, #node.children do
node.children[i].parent = node
walk(node.children[i])
end
end
end
walk(root)
return o
end
function tree:build(builder)
return tree.new(builder(treeBuilders))
end
function tree:layout()
local rect = self.root:layout({
maxWidth = 400,
maxHeight = 240,
tree = self,
path = 'root'
})
self.rect = rect
end
function tree:draw()
if not self.rect then
self:layout()
end
local rect = self.rect
-- avoid needless reallocations
if not self.img or self.img.width ~= rect.width or self.img.height ~= rect.height then
self.img = gfx.image.new(rect.width, rect.height)
else
self.img:clear(gfx.kColorClear)
end
gfx.pushContext(self.img)
self.root:draw(rect)
gfx.popContext()
if self.sprite then
self.sprite:setImage(self.img)
end
return self.img
end
function tree:asSprite()
self.sprite = gfx.sprite.new()
self:draw()
return self.sprite
end
function tree:get(id)
if self.useCache and self.cache[id] then
return self.cache[id]
end
function walk(node)
if node.properties and node.properties.id == id then
return node
end
if node.children then
for i = 1, #node.children do
local found = walk(node.children[i])
if found then
if self.useCache then
self.cache[id] = found
end
return found
end
end
end
end
return walk(self.root)
end
function tree:computeTabIndex(id)
local tabIndex = {}
function walk(node)
if node.properties and node.properties.tabIndex then
table.insert(tabIndex, node)
end
if node.children then
for i = 1, #node.children do
walk(node.children[i])
end
end
end
walk(self.root)
table.sort(tabIndex, function(a, b)
return a.properties.tabIndex < b.properties.tabIndex
end)
self.tabIndex = tabIndex
end
-- util
function getRectAnchor(r, anchor)
horizontal = horizontal or kAlignCenter
vertical = vertical or kAlignCenter
local p = geo.point.new
local cx = r.x + r.width / 2
local cy = r.y + r.height / 2
if anchor == kAnchorTopLeft then return p(r.left, r.top) end
if anchor == kAnchorTopCenter then return p(cx, r.top) end
if anchor == kAnchorTopRight then return p(r.right, r.top) end
if anchor == kAnchorCenterLeft then return p(r.left, cy) end
if anchor == kAnchorCenter then return p(cx, cy) end
if anchor == kAnchorCenterRight then return p(r.right, cy) end
if anchor == kAnchorBottomLeft then return p(r.left, r.bottom) end
if anchor == kAnchorBottomCenter then return p(cx, r.bottom) end
if anchor == kAnchorBottomRight then return p(r.right, r.bottom) end
return p(cx, cy)
end
-- interface
playout = {
box = box,
text = text,
tree = tree,
image = image,
kDirectionHorizontal = kDirectionHorizontal,
kDirectionVertical = kDirectionVertical,
kAlignStart = kAlignStart,
kAlignCenter = kAlignCenter,
kAlignEnd = kAlignEnd,
kAlignStretch = kAlignStretch,
getRectAnchor = getRectAnchor,
kAnchorTopLeft = kAnchorTopLeft,
kAnchorTopCenter = kAnchorTopCenter,
kAnchorTopRight = kAnchorTopRight,
kAnchorCenterLeft = kAnchorCenterLeft,
kAnchorCenter = kAnchorCenter,
kAnchorCenterRight = kAnchorCenterRight,
kAnchorBottomLeft = kAnchorBottomLeft,
kAnchorBottomCenter = kAnchorBottomCenter,
kAnchorBottomRight = kAnchorBottomRight,
}