This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxer.lua
735 lines (672 loc) · 20.3 KB
/
boxer.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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
local boxer = {
_VERSION = 'Boxer',
_DESCRIPTION = 'Layout library for LÖVE.',
_URL = 'https://github.com/tesselode/boxer',
_LICENSE = [[
MIT License
Copyright (c) 2019 Andrew Minnich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
}
-- returns the number of arguments that evaluate to true
local function count(...)
local amount = 0
for i = 1, select('#', ...) do
if select(i, ...) then amount = amount + 1 end
end
return amount
end
--[[
many properties in boxer can be set to either a raw value (number, string, etc.)
or a function that returns a raw value. this function will call the function if
necessary and return the raw value.
]]
local function get(self, value)
if type(value) == 'function' then
return value(self)
else
return value
end
end
-- creates a boxer class
local function createClass(parent)
local class = {}
function class.extend() return createClass(class) end
function class:__index(k)
if class.properties and class.properties[k] then
if class.properties[k].get then
return class.properties[k].get(self)
end
return get(self, self['_' .. k])
end
if class[k] then return class[k] end
if parent then return parent.__index(self, k) end
end
function class:__newindex(k, v)
if class.properties and class.properties[k] then
if class.properties[k].set then
class.properties[k].set(self, v)
else
self['_' .. k] = v
end
elseif parent then
parent.__newindex(self, k, v)
else
rawset(self, k, v)
end
end
setmetatable(class, {
__call = function(self, ...)
local instance = setmetatable({}, class)
if instance.new then instance:new(...) end
return instance
end,
})
return class
end
local Box = createClass()
Box.properties = {
x = {
get = function(self) return self:getX(0) end,
set = function(self, v) self:setX(v, 0) end,
},
left = {
get = function(self) return self:getX(0) end,
set = function(self, v) self:setX(v, 0) end,
},
center = {
get = function(self) return self:getX(.5) end,
set = function(self, v) self:setX(v, .5) end,
},
right = {
get = function(self) return self:getX(1) end,
set = function(self, v) self:setX(v, 1) end,
},
y = {
get = function(self) return self:getY(0) end,
set = function(self, v) self:setY(v, 0) end,
},
top = {
get = function(self) return self:getY(0) end,
set = function(self, v) self:setY(v, 0) end,
},
middle = {
get = function(self) return self:getY(.5) end,
set = function(self, v) self:setY(v, .5) end,
},
bottom = {
get = function(self) return self:getY(1) end,
set = function(self, v) self:setY(v, 1) end,
},
width = {},
height = {},
clipChildren = {},
transparent = {},
hidden = {},
disabled = {},
}
-- makes sure that an options table doesn't have more than one horizontal
-- or vertical position property
function Box:validatePositionOptions(options)
if count(options.x, options.left, options.center, options.right) > 1 then
error('Cannot provide more than one horizontal position property', 3)
end
if count(options.y, options.top, options.middle, options.bottom) > 1 then
error('Cannot provide more than one vertical position property', 3)
end
end
-- given an options table, sets properties (and defaults) of a box
-- that are common to every boxer class
function Box:setCommonOptions(options)
self.x, self.y = 0, 0
if options.x then self.x = options.x end
if options.left then self.left = options.left end
if options.center then self.center = options.center end
if options.right then self.right = options.right end
if options.y then self.y = options.y end
if options.top then self.top = options.top end
if options.middle then self.middle = options.middle end
if options.bottom then self.bottom = options.bottom end
self.style = options.style
self.children = options.children or {}
self.clipChildren = options.clipChildren
self.transparent = options.transparent
self.hidden = options.hidden
self.disabled = options.disabled
self.onMove = options.onMove
self.onDrag = options.onDrag
self.onEnter = options.onEnter
self.onLeave = options.onLeave
self.onClick = options.onClick
self.onPress = options.onPress
end
function Box:new(options)
options = options or {}
self:validatePositionOptions(options)
self.width = options.width or 0
self.height = options.height or 0
self:setCommonOptions(options)
end
-- gets a position along the x-axis of the box depending on the offset
-- (0 = left, 0.5 = center, 1 = right, etc.)
function Box:getX(offset)
offset = offset or 0
local x = get(self, self._x)
x = x - self.width * self._anchorX
return x + self.width * offset
end
-- gets a position along the y-axis of the box depending on the offset
-- (0 = top, 0.5 = middle, 1 = bottom, etc.)
function Box:getY(offset)
offset = offset or 0
local y = get(self, self._y)
y = y - self.height * self._anchorY
return y + self.height * offset
end
function Box:getRect()
return self.x, self.y, self.width, self.height
end
-- gets a child by name
function Box:getChild(name)
for _, child in ipairs(self.children) do
if child.name == name then
return child
end
end
return false
end
-- gets a style property given the box's state
-- (idle/pressed/released)
function Box:getCurrentStyle(property)
if not self.style then return end
if self._pressed and self.style.pressed and self.style.pressed[property] then
return get(self, self.style.pressed[property])
elseif self._hovered and self.style.hovered and self.style.hovered[property] then
return get(self, self.style.hovered[property])
elseif self.style.idle then
return get(self, self.style.idle[property])
end
end
function Box:containsPoint(x, y)
return x >= self.left
and x <= self.right
and y >= self.top
and y <= self.bottom
end
--[[
sets the position of a certain point along the x-axis of the box (defined by anchorX)
0 = left, 0.5 = center, 1 = right, etc.
x can be a number or a function that returns a number
]]
function Box:setX(x, anchorX)
anchorX = anchorX or 0
self._x, self._anchorX = x, anchorX
end
--[[
sets the position of a certain point along the y-axis of the box (defined by anchorY)
0 = top, 0.5 = middle, 1 = bottom, etc.
y can be a number or a function that returns a number
]]
function Box:setY(y, anchorY)
anchorY = anchorY or 0
self._y, self._anchorY = y, anchorY
end
--[[
resizes the box to contain all of the children and adjusts
the childrens' positions to be relative to the new position
of the box.
]]
function Box:wrap(padding)
-- set the size to (0, 0) if it's not already set
self.width = self.width or 0
self.height = self.height or 0
if #self.children == 0 then return end
padding = padding or 0
-- get a bounding box around the children
local left = self.children[1].left + self.x
local top = self.children[1].top + self.y
local right = self.children[1].right + self.x
local bottom = self.children[1].bottom + self.y
for i = 2, #self.children do
local child = self.children[i]
left = math.min(left, child.left + self.x)
top = math.min(top, child.top + self.y)
right = math.max(right, child.right + self.x)
bottom = math.max(bottom, child.bottom + self.y)
end
-- add padding
left = left - padding
top = top - padding
right = right + padding
bottom = bottom + padding
-- adjust child positions
for _, child in ipairs(self.children) do
if type(child._x) == 'function' then
local oldX = child._x
child._x = function() return oldX() - (left - self.left) end
else
child._x = child._x - (left - self.left)
end
if type(child._y) == 'function' then
local oldY = child._y
child._y = function() return oldY() - (top - self.top) end
else
child._y = child._y - (top - self.top)
end
end
-- resize the box
self.left = left
self.top = top
self.width = right - left
self.height = bottom - top
end
--[[
tells the box about mouse movement. corresponds to love.mousemoved.
mousemoved events will also be passed to child boxes, and child boxes will
block other child boxes behind them (unless they're transparent)
]]
function Box:mousemoved(x, y, dx, dy, istouch)
if self.disabled then return end
if self.clipChildren and not self:containsPoint(x, y) then
for _, child in ipairs(self.children) do
child:_mouseoff()
end
self:_mouseoff()
return
end
for i = #self.children, 1, -1 do
local child = self.children[i]
child:mousemoved(x - self.x, y - self.y, dx, dy, istouch)
if not child.transparent and child:containsPoint(x - self.x, y - self.y) then
for j = i - 1, 1, -1 do
local lowerChild = self.children[j]
lowerChild:_mouseoff()
end
self:_mouseoff()
return
end
end
self._hoveredPrevious = self._hovered
self._hovered = self:containsPoint(x, y)
if self.onMove and self._hovered then
self:onMove(x - self.x, y - self.y, dx, dy)
end
if self.onDrag and self._pressed then
self:onDrag(self._pressed, x - self.x, y - self.y, dx, dy)
end
if self._hovered and not self._hoveredPrevious and self.onEnter then
self:onEnter()
end
if not self._hovered and self._hoveredPrevious and self.onLeave then
self:onLeave()
end
end
--[[
tells a box that the mouse is no longer hovering it.
used when a box shouldn't be hovered, but the mouse is technically over it
(outside of a clipping region, blocked by another box, etc.)
]]
function Box:_mouseoff()
if self.disabled then return end
self._hoveredPrevious = self._hovered
self._hovered = false
if not self._hovered and self._hoveredPrevious and self.onLeave then
self:onLeave()
end
end
--[[
tells a box that a mouse button was pressed. corresponds to love.mousepressed.
also tells child boxes about mouse presses
]]
function Box:mousepressed(x, y, button, istouch, presses)
if self.disabled then return end
if self.clipChildren and not self:containsPoint(x, y) then
return
end
for i = #self.children, 1, -1 do
local child = self.children[i]
if child:containsPoint(x - self.x, y - self.y) then
child:mousepressed(x - self.x, y - self.y, button, istouch, presses)
if not child.transparent then return end
end
end
if not self._pressed and self:containsPoint(x, y) then
self._pressed = button
if self.onClick then self:onClick(button) end
end
end
--[[
tells a box that a mouse button was released. corresponds to love.mousereleased.
also tells child boxes about mouse releases
]]
function Box:mousereleased(x, y, button, istouch, presses)
-- mouse releases should trigger regardless of whether the box is hovered or not,
-- so we don't bother with blocking/clipping checks here
if self.disabled then return end
if button == self._pressed then
self._pressed = false
if self.onPress and self._hovered then
self:onPress(button)
end
end
for _, child in ipairs(self.children) do
child:mousereleased(x, y, button, istouch, presses)
end
end
--[[
draws a box with appropriate styling. can be overridden for custom visuals.
note that drawSelf should be written as if the top-left corner of the box
is always at 0, 0 (box.draw applies a translation based on the box's position)
]]
function Box:drawSelf()
local _, _, width, height = self:getRect()
if self:getCurrentStyle 'fillColor' then
love.graphics.setColor(self:getCurrentStyle 'fillColor')
love.graphics.rectangle('fill', 0, 0, width, height,
self:getCurrentStyle 'radiusX', self:getCurrentStyle 'radiusY')
end
if self:getCurrentStyle 'outlineColor' then
love.graphics.setColor(self:getCurrentStyle 'outlineColor')
love.graphics.setLineWidth(self:getCurrentStyle 'lineWidth' or 1)
love.graphics.rectangle('line', 0, 0, width, height,
self:getCurrentStyle 'radiusX', self:getCurrentStyle 'radiusY')
end
end
--[[
draws the stencil for a box. used for clipping children.
can be overridden to match a custom drawSelf function.
note that stencil should be written as if the top-left corner of the box
is always at 0, 0 (box.draw applies a translation based on the box's position)
]]
function Box:stencil()
local _, _, width, height = self:getRect()
love.graphics.rectangle('fill', 0, 0, width, height,
self:getCurrentStyle 'radiusX', self:getCurrentStyle 'radiusY')
end
-- "pushes" a stencil onto the "stack". used for nested stencils
function Box:_pushStencil(stencilValue)
love.graphics.push 'all'
love.graphics.stencil(self.stencilFunction, 'increment', 1, true)
love.graphics.setStencilTest('greater', stencilValue)
end
-- "pops" a stencil from the "stack". used for nested stencils
function Box:_popStencil()
love.graphics.stencil(self.stencilFunction, 'decrement', 1, true)
love.graphics.pop()
end
-- draws the box and its children
function Box:draw(stencilValue)
if self.hidden then return end
stencilValue = stencilValue or 0
love.graphics.push 'all'
love.graphics.translate(self:getRect())
self:drawSelf()
if self.clipChildren then
-- cache the stencil function so we don't have to create it every frame
self.stencilFunction = self.stencilFunction or function()
self:stencil()
end
self:_pushStencil(stencilValue)
for _, child in ipairs(self.children) do
child:draw(stencilValue + 1)
end
self:_popStencil()
else
for _, child in ipairs(self.children) do
child:draw(stencilValue)
end
end
love.graphics.pop()
end
boxer.Box = Box
--[[
creates a box around a number of children and adjusts the children's
position to be relative to the box.
]]
function boxer.wrap(options)
if not options then
error('Must provide an options table to boxer.wrap()', 2)
end
if not (options.children and #options.children > 0) then
error('Must provide at least one child to wrap', 2)
end
local box = boxer.Box()
box.children = options.children
box:wrap(options.padding)
return box
end
local Text = Box.extend()
Text.properties = {
font = {},
text = {},
width = {
get = function(self)
return self.font:getWidth(self.text) * self.scaleX
end,
set = function(self, v)
self.scaleX = v / self.font:getWidth(self.text)
end,
},
height = {
get = function(self)
return self.font:getHeight() * self.scaleY
end,
set = function(self, v)
self.scaleY = v / self.font:getHeight()
end,
},
scaleX = {},
scaleY = {},
}
function Text:drawSelf()
love.graphics.setFont(self.font)
if self:getCurrentStyle 'shadowColor' then
love.graphics.setColor(self:getCurrentStyle 'shadowColor')
love.graphics.print(
self.text,
self:getCurrentStyle 'shadowOffsetX' or 1,
self:getCurrentStyle 'shadowOffsetY' or 1,
0,
self.scaleX,
self.scaleY
)
end
if self:getCurrentStyle 'color' then
love.graphics.setColor(self:getCurrentStyle 'color')
else
love.graphics.setColor(1, 1, 1)
end
love.graphics.print(self.text, 0, 0, 0, self.scaleX, self.scaleY)
end
function Text:new(options)
-- validate options
if not options then
error('Must provide an options table', 2)
end
if not options.text then
error('Must provide a text string', 2)
end
if not options.font then
error('Must provide a font', 2)
end
if count(options.width, options.scaleX) > 1 then
error('Cannot provide both width and scaleX', 2)
end
if count(options.height, options.scaleY) > 1 then
error('Cannot provide both height and scaleY', 2)
end
self:validatePositionOptions(options)
self.text = options.text
self.font = options.font
self.scaleX, self.scaleY = 1, 1
if options.scaleX then self.scaleX = options.scaleX end
if options.scaleY then self.scaleY = options.scaleY end
if options.width then self.width = options.width end
if options.height then self.height = options.height end
self:setCommonOptions(options)
if options.transparent == nil then self.transparent = true end
end
boxer.Text = Text
local Paragraph = Box.extend()
Paragraph.properties = {
font = {},
text = {},
width = {},
height = {
get = function(self)
local _, lines = self.font:getWrap(self.text, self.width)
return #lines * self.font:getHeight() * self.font:getLineHeight()
end,
set = function(self, v)
error('Cannot set height of a paragraph directly', 2)
end,
},
align = {},
}
function Paragraph:drawSelf()
love.graphics.setFont(self.font)
if self:getCurrentStyle 'shadowColor' then
love.graphics.setColor(self:getCurrentStyle 'shadowColor')
love.graphics.print(
self.text,
self:getCurrentStyle 'shadowOffsetX' or 1,
self:getCurrentStyle 'shadowOffsetY' or 1,
self.width,
self.align
)
end
if self:getCurrentStyle 'color' then
love.graphics.setColor(self:getCurrentStyle 'color')
else
love.graphics.setColor(1, 1, 1)
end
love.graphics.printf(self.text, 0, 0, self.width, self.align)
end
function Paragraph:new(options)
-- validate options
if not options then
error('Must provide an options table', 2)
end
if not options.text then
error('Must provide a text string', 2)
end
if not options.font then
error('Must provide a font', 2)
end
if not options.width then
error('Must provide a width', 2)
end
if options.height then
error('Cannot set height of a paragraph directly', 2)
end
self:validatePositionOptions(options)
self.text = options.text
self.font = options.font
self.width = options.width
self:setCommonOptions(options)
if options.transparent == nil then self.transparent = true end
end
boxer.Paragraph = Paragraph
local Image = Box.extend()
Image.properties = {
image = {},
width = {
get = function(self)
return self.image:getWidth() * self.scaleX
end,
set = function(self, v)
self.scaleX = v / self.image:getWidth()
end,
},
height = {
get = function(self)
return self.image:getHeight() * self.scaleY
end,
set = function(self, v)
self.scaleY = v / self.image:getHeight()
end,
},
scaleX = {},
scaleY = {},
}
function Image:drawSelf()
local r, g, b, a = self:getCurrentStyle 'color'
if r then
love.graphics.setColor(r, g, b, a)
else
love.graphics.setColor(1, 1, 1)
end
love.graphics.draw(self.image, 0, 0, 0, self.scaleX, self.scaleY)
end
function Image:new(options)
-- validate options
if not options then
error('Must provide an options table', 2)
end
if not options.image then
error('Must provide an image', 2)
end
if count(options.width, options.scaleX) > 1 then
error('Cannot provide both width and scaleX', 2)
end
if count(options.height, options.scaleY) > 1 then
error('Cannot provide both height and scaleY', 2)
end
self:validatePositionOptions(options)
self.image = options.image
self.scaleX, self.scaleY = 1, 1
if options.scaleX then self.scaleX = options.scaleX end
if options.scaleY then self.scaleY = options.scaleY end
if options.width then self.width = options.width end
if options.height then self.height = options.height end
self:setCommonOptions(options)
end
boxer.Image = Image
local Ellipse = Box.extend()
Ellipse.properties = {
segments = {},
}
function Ellipse:containsPoint(x, y)
local rx, ry = self.width/2, self.height/2
return ((x - self.center) ^ 2) / (rx ^ 2) + ((y - self.middle) ^ 2) / (ry ^ 2) <= 1
end
function Ellipse:drawSelf()
if self:getCurrentStyle 'fillColor' then
love.graphics.setColor(self:getCurrentStyle 'fillColor')
love.graphics.ellipse('fill', self.width/2, self.height/2,
self.width/2, self.height/2, self.segments)
end
if self:getCurrentStyle 'outlineColor' then
love.graphics.setColor(self:getCurrentStyle 'outlineColor')
love.graphics.setLineWidth(self:getCurrentStyle 'lineWidth' or 1)
love.graphics.ellipse('line', self.width/2, self.height/2,
self.width/2, self.height/2, self.segments)
end
end
function Ellipse:stencil()
love.graphics.ellipse('fill', self.width/2, self.height/2,
self.width/2, self.height/2, self.segments)
end
function Ellipse:new(options)
options = options or {}
self:validatePositionOptions(options)
self.width = options.width or 0
self.height = options.height or 0
self:setCommonOptions(options)
end
boxer.Ellipse = Ellipse
return boxer