-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlfFixedList.lua
809 lines (659 loc) · 19.8 KB
/
lfFixedList.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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
-- Copyright (c) 2012-2014 Kenny Shields --
--]]------------------------------------------------
return function(loveframes)
---------- module start ----------
-- list object
local newobject = loveframes.NewObject("fixedlist", "lf_fixed_list", true)
--[[---------------------------------------------------------
- func: initialize()
- desc: initializes the object
--]]---------------------------------------------------------
function newobject:initialize()
self.type = "list"
self.display = "vertical"
self.width = 300
self.height = 150
self.clickx = 0
self.clicky = 0
self.padding = 0
self.spacing = 0
self.offsety = 0
self.offsetx = 0
self.extrawidth = 0
self.extraheight = 0
self.buttonscrollamount = 0.10
self.mousewheelscrollamount = 10
self.internal = false
self.hbar = false
self.vbar = false
self.autoscroll = false
self.horizontalstacking = false
self.dtscrolling = false
self.internals = {}
self.children = {}
self.OnScroll = nil
self:SetDrawFunc()
end
--[[---------------------------------------------------------
- func: update(deltatime)
- desc: updates the object
--]]---------------------------------------------------------
function newobject:update(dt)
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
local alwaysupdate = self.alwaysupdate
if not visible then
if not alwaysupdate then
return
end
end
self:CheckHover()
local internals = self.internals
local children = self.children
local display = self.display
local parent = self.parent
local horizontalstacking = self.horizontalstacking
local base = loveframes.base
local update = self.Update
-- move to parent if there is a parent
if parent ~= base then
self.x = self.parent.x + self.staticx
self.y = self.parent.y + self.staticy
end
for k, v in ipairs(internals) do
v:update(dt)
for _, p in pairs(self:GetParents()) do
v.x = v.x - (p.offsetx or 0)
v.y = v.y - (p.offsety or 0)
end
end
local x = self.x
local y = self.y
local width = self.width
local height = self.height
local offsetx = self.offsetx
local offsety = self.offsety
for k, v in ipairs(children) do
v:update(dt)
v:SetClickBounds(x, y, width, height)
v.x = (v.parent.x + v.staticx) - offsetx
v.y = (v.parent.y + v.staticy) - offsety
-- Fixed: Why the hell LoveFrames
-- for _, p in pairs(self:GetParents()) do
-- v.x = v.x - (p.offsetx or 0)
-- v.y = v.y - (p.offsety or 0)
-- end
if display == "vertical" then
if v.lastheight ~= v.height then
self:CalculateSize()
self:RedoLayout()
end
end
end
if update then
update(self, dt)
end
end
--[[---------------------------------------------------------
- func: draw()
- desc: draws the object
--]]---------------------------------------------------------
function newobject:draw()
if loveframes.state ~= self.state then
return
end
if not self.visible then
return
end
local x = self.x
local y = self.y
local width = self.width
local height = self.height
local stencilfunc = function()
love.graphics.rectangle("fill", x, y, width, height)
end
self:SetDrawOrder()
local drawfunc = self.Draw or self.drawfunc
if drawfunc then
drawfunc(self)
end
love.graphics.stencil(stencilfunc)
love.graphics.setStencilTest("greater", 0)
local children = self.children
if children then
for k, v in ipairs(children) do
local col = loveframes.BoundingBox(x, v.x, y, v.y, width, v.width, height, v.height)
if col then
v:draw()
end
end
end
love.graphics.setStencilTest()
local drawfunc = self.DrawOver or self.drawoverfunc
if drawfunc then
drawfunc(self)
end
local internals = self.internals
if internals then
for k, v in ipairs(internals) do
v:draw()
end
end
end
--[[---------------------------------------------------------
- func: mousepressed(x, y, button)
- desc: called when the player presses a mouse button
--]]---------------------------------------------------------
function newobject:mousepressed(x, y, button)
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
return
end
local hover = self.hover
local children = self.children
local internals = self.internals
if hover and button == 1 then
local baseparent = self:GetBaseParent()
if baseparent and baseparent.type == "frame" then
baseparent:MakeTop()
end
end
for k, v in ipairs(internals) do
v:mousepressed(x, y, button)
end
for k, v in ipairs(children) do
v:mousepressed(x, y, button)
end
end
--[[---------------------------------------------------------
- func: wheelmoved(x, y)
- desc: called when the player moves a mouse wheel
--]]---------------------------------------------------------
function newobject:wheelmoved(x, y)
local toplist = self:IsTopList()
local vbar = self.vbar
local hbar = self.hbar
local scrollamount = self.mousewheelscrollamount
if (vbar or hbar) and toplist then
local bar = self:GetScrollBar()
local dtscrolling = self.dtscrolling
if dtscrolling then
local dt = love.timer.getDelta()
bar:Scroll(-y * scrollamount * dt)
else
bar:Scroll(-y * scrollamount)
end
end
end
--[[---------------------------------------------------------
- func: AddItem(object)
- desc: adds an item to the object
--]]---------------------------------------------------------
function newobject:AddItem(object)
local objtype = object.type
if objtype == "frame" then
return
end
local children = self.children
local state = self.state
-- remove the item object from its current parent and make its new parent the list object
object:Remove()
object.parent = self
object:SetState(state)
-- insert the item object into the list object's children table
table.insert(children, object)
-- resize the list and redo its layout
self:CalculateSize()
self:RedoLayout()
return self
end
--[[---------------------------------------------------------
- func: RemoveItem(object or number)
- desc: removes an item from the object
--]]---------------------------------------------------------
function newobject:RemoveItem(data)
local dtype = type(data)
if dtype == "number" then
local children = self.children
local item = children[data]
if item then
item:Remove()
end
else
data:Remove()
end
self:CalculateSize()
self:RedoLayout()
return self
end
--[[---------------------------------------------------------
- func: CalculateSize()
- desc: calculates the size of the object's children
--]]---------------------------------------------------------
function newobject:CalculateSize()
local numitems = #self.children
local height = self.height
local width = self.width
local padding = self.padding
local spacing = self.spacing
local itemheight = self.padding
local itemwidth = self.padding
local display = self.display
local vbar = self.vbar
local hbar = self.hbar
local internals = self.internals
local children = self.children
local horizontalstacking = self.horizontalstacking
if display == "vertical" then
if horizontalstacking then
local curwidth = 0
local maxwidth = width - padding * 2
local prevheight = 0
local scrollbar = self:GetScrollBar()
if scrollbar then
maxwidth = maxwidth - scrollbar.width
end
for k, v in ipairs(children) do
if v.height > prevheight then
prevheight = v.height
end
curwidth = curwidth + v.width + spacing
if children[k + 1] then
if curwidth + children[k + 1].width > maxwidth then
curwidth = padding
itemheight = itemheight + prevheight + spacing
prevheight = 0
end
else
itemheight = itemheight + prevheight + padding
end
end
self.itemheight = itemheight
else
for k, v in ipairs(children) do
itemheight = itemheight + v.height + spacing
end
self.itemheight = (itemheight - spacing) + padding
end
local itemheight = self.itemheight
if itemheight > height then
self.extraheight = itemheight - height
if not vbar then
local scrollbar = loveframes.objects["scrollbody"]:new(self, display)
table.insert(internals, scrollbar)
self.vbar = true
self:GetScrollBar().autoscroll = self.autoscroll
end
else
if vbar then
local bar = internals[1]
bar:Remove()
self.vbar = false
self.offsety = 0
end
end
elseif display == "horizontal" then
for k, v in ipairs(children) do
itemwidth = itemwidth + v.width + spacing
end
self.itemwidth = (itemwidth - spacing) + padding
local itemwidth = self.itemwidth
if itemwidth > width then
self.extrawidth = itemwidth - width
if not hbar then
local scrollbar = loveframes.objects["scrollbody"]:new(self, display)
table.insert(internals, scrollbar)
self.hbar = true
self:GetScrollBar().autoscroll = self.autoscroll
end
else
if hbar then
local bar = internals[1]
bar:Remove()
self.hbar = false
self.offsetx = 0
end
end
end
return self
end
--[[---------------------------------------------------------
- func: RedoLayout()
- desc: used to redo the layout of the object
--]]---------------------------------------------------------
function newobject:RedoLayout()
local width = self.width
local height = self.height
local children = self.children
local internals = self.internals
local padding = self.padding
local spacing = self.spacing
local starty = padding
local startx = padding
local vbar = self.vbar
local hbar = self.hbar
local display = self.display
local horizontalstacking = self.horizontalstacking
local scrollbody, scrollbodywidth, scrollbodyheight
if vbar or hbar then
scrollbody = internals[1]
scrollbodywidth = scrollbody.width
scrollbodyheight = scrollbody.height
end
if #children > 0 then
if display == "vertical" then
if horizontalstacking then
local curwidth = padding
local curheight = padding
local maxwidth = self.width - padding * 2
local prevheight = 0
local scrollbar = self:GetScrollBar()
if scrollbar then
maxwidth = maxwidth - scrollbar.width
end
for k, v in ipairs(children) do
local itemheight = v.height
v.lastheight = itemheight
v.staticx = curwidth
v.staticy = curheight
if v.height > prevheight then
prevheight = v.height
end
if children[k + 1] then
curwidth = curwidth + v.width + spacing
if curwidth + (children[k + 1].width) > maxwidth then
curwidth = padding
curheight = curheight + prevheight + spacing
prevheight = 0
end
end
end
else
for k, v in ipairs(children) do
local itemwidth = v.width
local itemheight = v.height
local retainsize = v.retainsize
v.staticx = padding
v.staticy = starty
v.lastheight = itemheight
if vbar then
if itemwidth + padding > (width - scrollbodywidth) then
v:SetWidth((width - scrollbodywidth) - (padding * 2))
end
if not retainsize then
v:SetWidth((width - scrollbodywidth) - (padding * 2))
end
scrollbody.staticx = width - scrollbodywidth
scrollbody.height = height
else
if not retainsize then
v:SetWidth(width - (padding * 2))
end
end
starty = starty + itemheight
starty = starty + spacing
end
end
elseif display == "horizontal" then
for k, v in ipairs(children) do
local itemwidth = v.width
local itemheight = v.height
local retainsize = v.retainsize
v.staticx = startx
v.staticy = padding
if hbar then
if itemheight + padding > (height - scrollbodyheight) then
v:SetHeight((height - scrollbodyheight) - (padding * 2))
end
if not retainsize then
v:SetHeight((height - scrollbodyheight) - (padding * 2))
end
scrollbody.staticy = height - scrollbodyheight
scrollbody.width = width
else
if not retainsize then
v:SetHeight(height - (padding * 2))
end
end
startx = startx + itemwidth
startx = startx + spacing
end
end
end
return self
end
--[[---------------------------------------------------------
- func: SetDisplayType(type)
- desc: sets the object's display type
--]]---------------------------------------------------------
function newobject:SetDisplayType(type)
local children = self.children
local numchildren = #children
self.display = type
self.vbar = false
self.hbar = false
self.offsetx = 0
self.offsety = 0
self.internals = {}
if numchildren > 0 then
self:CalculateSize()
self:RedoLayout()
end
return self
end
--[[---------------------------------------------------------
- func: GetDisplayType()
- desc: gets the object's display type
--]]---------------------------------------------------------
function newobject:GetDisplayType()
return self.display
end
--[[---------------------------------------------------------
- func: SetPadding(amount)
- desc: sets the object's padding
--]]---------------------------------------------------------
function newobject:SetPadding(amount)
local children = self.children
local numchildren = #children
self.padding = amount
if numchildren > 0 then
self:CalculateSize()
self:RedoLayout()
end
return self
end
--[[---------------------------------------------------------
- func: SetSpacing(amount)
- desc: sets the object's spacing
--]]---------------------------------------------------------
function newobject:SetSpacing(amount)
local children = self.children
local numchildren = #children
self.spacing = amount
if numchildren > 0 then
self:CalculateSize()
self:RedoLayout()
end
return self
end
--[[---------------------------------------------------------
- func: Clear()
- desc: removes all of the object's children
--]]---------------------------------------------------------
function newobject:Clear()
self.children = {}
self:CalculateSize()
self:RedoLayout()
return self
end
--[[---------------------------------------------------------
- func: SetWidth(width, relative)
- desc: sets the object's width
--]]---------------------------------------------------------
function newobject:SetWidth(width, relative)
if relative then
self.width = self.parent.width * width
else
self.width = width
end
self:CalculateSize()
self:RedoLayout()
return self
end
--[[---------------------------------------------------------
- func: SetHeight(height, relative)
- desc: sets the object's height
--]]---------------------------------------------------------
function newobject:SetHeight(height, relative)
if relative then
self.height = self.parent.height * height
else
self.height = height
end
self:CalculateSize()
self:RedoLayout()
return self
end
--[[---------------------------------------------------------
- func: SetSize(width, height, r1, r2)
- desc: sets the object's size
--]]---------------------------------------------------------
function newobject:SetSize(width, height, r1, r2)
if r1 then
self.width = self.parent.width * width
else
self.width = width
end
if r2 then
self.height = self.parent.height * height
else
self.height = height
end
self:CalculateSize()
self:RedoLayout()
return self
end
--[[---------------------------------------------------------
- func: GetScrollBar()
- desc: gets the object's scroll bar
--]]---------------------------------------------------------
function newobject:GetScrollBar()
local vbar = self.vbar
local hbar = self.hbar
local internals = self.internals
if vbar or hbar then
local scrollbody = internals[1]
local scrollarea = scrollbody.internals[1]
local scrollbar = scrollarea.internals[1]
return scrollbar
else
return false
end
end
--[[---------------------------------------------------------
- func: SetAutoScroll(bool)
- desc: sets whether or not the list's scrollbar should
auto scroll to the bottom when a new object is
added to the list
--]]---------------------------------------------------------
function newobject:SetAutoScroll(bool)
local scrollbar = self:GetScrollBar()
self.autoscroll = bool
if scrollbar then
scrollbar.autoscroll = bool
end
return self
end
--[[---------------------------------------------------------
- func: GetAutoScroll()
- desc: gets whether or not the list's scrollbar should
auto scroll to the bottom when a new object is
added to the list
--]]---------------------------------------------------------
function newobject:GetAutoScroll()
return self.autoscroll
end
--[[---------------------------------------------------------
- func: SetButtonScrollAmount(speed)
- desc: sets the scroll amount of the object's scrollbar
buttons
--]]---------------------------------------------------------
function newobject:SetButtonScrollAmount(amount)
self.buttonscrollamount = amount
return self
end
--[[---------------------------------------------------------
- func: GetButtonScrollAmount()
- desc: gets the scroll amount of the object's scrollbar
buttons
--]]---------------------------------------------------------
function newobject:GetButtonScrollAmount()
return self.buttonscrollamount
end
--[[---------------------------------------------------------
- func: SetMouseWheelScrollAmount(amount)
- desc: sets the scroll amount of the mouse wheel
--]]---------------------------------------------------------
function newobject:SetMouseWheelScrollAmount(amount)
self.mousewheelscrollamount = amount
return self
end
--[[---------------------------------------------------------
- func: GetMouseWheelScrollAmount()
- desc: gets the scroll amount of the mouse wheel
--]]---------------------------------------------------------
function newobject:GetButtonScrollAmount()
return self.mousewheelscrollamount
end
--[[---------------------------------------------------------
- func: EnableHorizontalStacking(bool)
- desc: enables or disables horizontal stacking
--]]---------------------------------------------------------
function newobject:EnableHorizontalStacking(bool)
local children = self.children
local numchildren = #children
self.horizontalstacking = bool
if numchildren > 0 then
self:CalculateSize()
self:RedoLayout()
end
return self
end
--[[---------------------------------------------------------
- func: GetHorizontalStacking()
- desc: gets whether or not the object allows horizontal
stacking
--]]---------------------------------------------------------
function newobject:GetHorizontalStacking()
return self.horizontalstacking
end
--[[---------------------------------------------------------
- func: SetDTScrolling(bool)
- desc: sets whether or not the object should use delta
time when scrolling
--]]---------------------------------------------------------
function newobject:SetDTScrolling(bool)
self.dtscrolling = bool
return self
end
--[[---------------------------------------------------------
- func: GetDTScrolling()
- desc: gets whether or not the object should use delta
time when scrolling
--]]---------------------------------------------------------
function newobject:GetDTScrolling()
return self.dtscrolling
end
---------- module end ----------
end