-
Notifications
You must be signed in to change notification settings - Fork 0
/
penguingui.lua
executable file
·2597 lines (2279 loc) · 63.4 KB
/
penguingui.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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- "version": "Beta v. Upbeat Giraffe",
-- "author": "PenguinToast",
-- "version": "0.4.1",
-- "support_url": "http://penguintoast.github.io/PenguinGUI"
-- This script contains all the scripts in this library, so you only need to
-- include this script for production purposes.
--------------------------------------------------------------------------------
-- Util.lua
--------------------------------------------------------------------------------
PtUtil = {}
PtUtil.charWidths = {6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 4, 8, 12, 10, 12, 12, 4, 6, 6, 8, 8, 6, 8, 4, 12, 10, 6, 10, 10, 10, 10, 10, 10, 10, 10, 4, 4, 8, 8, 8, 10, 12, 10, 10, 8, 10, 8, 8, 10, 10, 8, 10, 10, 8, 12, 10, 10, 10, 10, 10, 10, 8, 10, 10, 12, 10, 10, 8, 6, 12, 6, 8, 10, 6, 10, 10, 9, 10, 10, 8, 10, 10, 4, 6, 9, 4, 12, 10, 10, 10, 10, 8, 10, 8, 10, 10, 12, 8, 10, 10, 8, 4, 8, 10, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 10, 10, 15, 10, 5, 13, 7, 14, 15, 15, 10, 6, 14, 12, 16, 14, 7, 7, 6, 11, 12, 8, 7, 6, 16, 16, 15, 15, 15, 10, 10, 10, 10, 10, 10, 10, 14, 10, 8, 8, 8, 8, 8, 8, 8, 8, 13, 10, 10, 10, 10, 10, 10, 10, 13, 10, 10, 10, 10, 10, 14, 11, 10, 10, 10, 10, 10, 10, 15, 9, 10, 10, 10, 10, 8, 8, 8, 8, 12, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15, 10}
function PtUtil.library()
return {
"/penguingui/Util.lua",
"/penguingui/Binding.lua",
"/penguingui/BindingFunctions.lua",
"/penguingui/GUI.lua",
"/penguingui/Component.lua",
"/penguingui/Line.lua",
"/penguingui/Rectangle.lua",
"/penguingui/Align.lua",
"/penguingui/HorizontalLayout.lua",
"/penguingui/VerticalLayout.lua",
"/penguingui/Panel.lua",
"/penguingui/Frame.lua",
"/penguingui/Button.lua",
"/penguingui/Label.lua",
"/penguingui/TextButton.lua",
"/penguingui/TextField.lua",
"/penguingui/Image.lua",
"/penguingui/CheckBox.lua",
"/penguingui/RadioButton.lua",
"/penguingui/TextRadioButton.lua",
"/penguingui/Slider.lua",
"/penguingui/List.lua",
"/lib/profilerapi.lua",
"/lib/inspect.lua"
}
end
function PtUtil.drawText(text, options, fontSize, color)
fontSize = fontSize or 16
if text:byte() == 32 then -- If it starts with a space, offset the string
local xOffset = PtUtil.getStringWidth(" ", fontSize)
local oldX = options.position[1]
options.position[1] = oldX + xOffset
console.canvasDrawText(text, options, fontSize, color)
options.position[1] = oldX
else
console.canvasDrawText(text, options, fontSize, color)
end
end
function PtUtil.getStringWidth(text, fontSize)
local widths = PtUtil.charWidths
local scale = PtUtil.getFontScale(fontSize)
local out = 0
local len = #text
for i=1,len,1 do
out = out + widths[string.byte(text, i)]
end
return out * scale
end
function PtUtil.getFontScale(size)
return size / 16
end
PtUtil.specialKeyMap = {
[8] = "backspace",
[13] = "enter",
[127] = "delete",
[275] = "right",
[276] = "left",
[278] = "home",
[279] = "end",
[301] = "capslock",
[303] = "shift",
[304] = "shift"
}
PtUtil.shiftKeyMap = {
[39] = "\"",
[44] = "<",
[45] = "_",
[46] = ">",
[47] = "?",
[48] = ")",
[49] = "!",
[50] = "@",
[51] = "#",
[52] = "$",
[53] = "%",
[54] = "^",
[55] = "&",
[56] = "*",
[57] = "(",
[59] = ":",
[61] = "+",
[91] = "{",
[92] = "|",
[93] = "}",
[96] = "~"
}
function PtUtil.getKey(key, shift, capslock)
if (capslock and not shift) or (shift and not capslock) then
if key >= 97 and key <= 122 then
return string.upper(string.char(key))
end
end
if shift and PtUtil.shiftKeyMap[key] then
return PtUtil.shiftKeyMap[key]
else
if key >= 32 and key <= 122 then
return string.char(key)
elseif PtUtil.specialKeyMap[key] then
return PtUtil.specialKeyMap[key]
else
return "unknown"
end
end
end
function PtUtil.fillRect(rect, color)
console.canvasDrawRect(rect, color)
end
function PtUtil.fillPoly(poly, color)
console.logInfo("fillPoly is not functional yet")
end
function PtUtil.drawLine(p1, p2, color, width)
console.canvasDrawLine(p1, p2, color, width * 2)
end
function PtUtil.drawRect(rect, color, width)
local minX = rect[1] + width / 2
local minY = math.floor((rect[2] + width / 2) * 2) / 2
local maxX = rect[3] - width / 2
local maxY = math.floor((rect[4] - width / 2) * 2) / 2
PtUtil.drawLine(
{minX - width / 2, minY},
{maxX + width / 2, minY},
color, width
)
PtUtil.drawLine(
{maxX, minY},
{maxX, maxY},
color, width
)
PtUtil.drawLine(
{minX - width / 2, maxY},
{maxX + width / 2, maxY},
color, width
)
PtUtil.drawLine(
{minX, minY},
{minX, maxY},
color, width
)
end
function PtUtil.drawPoly(poly, color, width)
for i=1,#poly - 1,1 do
PtUtil.drawLine(poly[i], poly[i + 1], color, width)
end
PtUtil.drawLine(poly[#poly], poly[1], color, width)
end
function PtUtil.drawImage(image, position, scale)
console.canvasDrawImage(image, position, scale)
end
function ripairs(t)
local function ripairs_it(t,i)
i=i-1
local v=t[i]
if v==nil then return v end
return i,v
end
return ripairs_it, t, #t+1
end
function PtUtil.removeObject(t, o)
for i,obj in ipairs(t) do
if obj == o then
table.remove(t, i)
return i
end
end
return -1
end
function class(...)
local cls, bases = {}, {...}
for i, base in ipairs(bases) do
for k, v in pairs(base) do
cls[k] = v
end
end
cls.__index, cls.is_a = cls, {[cls] = true}
for i, base in ipairs(bases) do
for c in pairs(base.is_a) do
cls.is_a[c] = true
end
cls.is_a[base] = true
end
setmetatable(
cls,
{
__call = function (c, ...)
local instance = setmetatable({}, c)
instance = Binding.proxy(instance)
local init = instance._init
if init then init(instance, ...) end
return instance
end
}
)
return cls
end
function dump(value, indent, seen)
if type(value) ~= "table" then
if type(value) == "string" then
return string.format('%q', value)
else
return tostring(value)
end
else
if type(seen) ~= "table" then
seen = {}
elseif seen[value] then
return "{...}"
end
seen[value] = true
indent = indent or ""
if next(value) == nil then
return "{}"
end
local str = "{"
local first = true
for k,v in pairs(value) do
if first then
first = false
else
str = str..","
end
str = str.."\n"..indent.." ".."["..dump(k, "", seen)
.."] = "..dump(v, indent.." ", seen)
end
str = str.."\n"..indent.."}"
return str
end
end
--------------------------------------------------------------------------------
-- Binding.lua
--------------------------------------------------------------------------------
Binding = setmetatable(
{},
{
__call = function(t, ...)
return t.value(...)
end
}
)
Binding.proxyTable = {
__index = function(t, k)
local out = t._instance[k]
if out ~= nil then
return out
else
return Binding.proxyTable[k]
end
end,
__newindex = function(t, k, v)
local instance = t._instance
local old = instance[k]
local new = v
instance[k] = new
if old ~= v then
local listeners = instance.listeners
if listeners and listeners[k] then
local keyListeners = listeners[k]
for _,keyListener in ipairs(keyListeners) do
new = keyListener(t, k, old, new) or new
end
end
local bindings = instance.bindings
if bindings and bindings[k] then
local keyBindings = bindings[k]
for _,keyBinding in ipairs(keyBindings) do
keyBinding:valueChanged(old, new)
end
end
end
end,
__pairs = function(t)
return pairs(t._instance)
end,
__ipairs = function(t)
return ipairs(t._instance)
end,
__add = function(a, b)
return a._instance + (type(b) == "table" and b._instance or b)
end,
__sub = function(a, b)
return a._instance - (type(b) == "table" and b._instance or b)
end,
__mul = function(a, b)
return a._instance * (type(b) == "table" and b._instance or b)
end,
__div = function(a, b)
return a._instance / (type(b) == "table" and b._instance or b)
end,
__mod = function(a, b)
return a._instance % (type(b) == "table" and b._instance or b)
end,
__pow = function(a, b)
return a._instance ^ (type(b) == "table" and b._instance or b)
end,
__unm = function(a)
return -a._instance
end,
__concat = function(a, b)
return a._instance .. (type(b) == "table" and b._instance or b)
end,
__len = function(a)
return #a._instance
end,
__eq = function(a, b)
return a._instance == b._instance
end,
__lt = function(a, b)
return a._instance < b._instance
end,
__le = function(a, b)
return a._instance <= b._instance
end,
__call = function(t, ...)
return t._instance(...)
end
}
function Binding.isValue(object)
return type(object) == "table"
and getmetatable(object._instance) == Binding.valueTable
end
Binding.weakMetaTable = {
__mode = "v"
}
Binding.valueTable = {}
Binding.valueTable.__index = Binding.valueTable
function Binding.valueTable:addValueListener(listener)
return self:addListener("value", listener)
end
function Binding.valueTable:removeValueListener(listener)
return self:removeListener("value", listener)
end
function Binding.valueTable:addValueBinding(binding)
return self:addBinding("value", binding)
end
function Binding.valueTable:removeValueBinding(binding)
return self:removeBinding("value", binding)
end
function Binding.valueTable:unbind()
local bindings = self.bindings
if bindings and bindings.value then
local valueBindings = bindings.value
for _,binding in ipairs(valueBindings) do
binding:unbind()
end
end
local boundto = self.boundto
for _,bound in ipairs(boundto) do
for _,boundTable in pairs(bound.bindings) do
PtUtil.removeObject(boundTable, self)
end
end
self.boundto = nil
local bindTargets = self.bindTargets
if bindTargets then
for bindTarget,_ in pairs(bindTargets) do
local bindTargetBoundto = bindTarget.boundto
for key,binding in pairs(bindTargetBoundto) do
if binding == self then
bindTargetBoundto[key] = nil
end
end
end
end
end
function Binding.unbindChain(binding)
Binding.valueTable.unbind(binding)
local bindingTable = binding.bindingTable
for i=1, #bindingTable - 1, 1 do
bindingTable[i]:unbind()
bindingTable[i] = nil
end
end
function Binding.value(t, k)
local out = Binding.proxy(setmetatable({}, Binding.valueTable))
if type(k) == "string" then -- Single key
out.value = t[k]
out.valueChanged = function(binding, old, new)
binding.value = new
end
t:addBinding(k, out)
out.boundto = {t}
return out
else -- Table of keys
local numKeys = #k
local currTable = t
local bindingTable = {}
for i=1, numKeys - 1, 1 do
local currBinding = Binding.proxy(setmetatable({}, Binding.valueTable))
local currKey = k[i]
local index = i
currBinding.valueChanged = function(binding, old, new)
if old == new then return end
local oldTable = old
local newTable = new
local subKey
local transplant
for j=index + 1, numKeys, 1 do
subKey = k[j]
transplant = bindingTable[j]
transplant.boundto[1] = newTable
oldTable:removeBinding(subKey, transplant)
newTable:addBinding(subKey, transplant)
if j < numKeys then
oldTable = oldTable[subKey]
newTable = newTable[subKey]
end
end
end
currBinding.boundto = {currTable}
currTable:addBinding(currKey, currBinding)
bindingTable[index] = currBinding
currTable = t[currKey]
end
out.valueChanged = function(binding, old, new)
binding.value = new
end
out.bindingTable = bindingTable
out.boundto = {currTable}
out.unbind = Binding.unbindChain
currTable:addBinding(k[numKeys], out)
bindingTable[numKeys] = out
return out
end
end
function Binding.proxyTable:addListener(key, listener)
local listeners = self.listeners
if not listeners then
listeners = {}
self.listeners = listeners
end
local keyListeners = listeners[key]
if not keyListeners then
keyListeners = {}
listeners[key] = keyListeners
end
table.insert(keyListeners, listener)
return listener
end
function Binding.proxyTable:removeListener(key, listener)
local keyListeners = self.listeners[key]
return PtUtil.removeObject(keyListeners, listener) ~= -1
end
function Binding.proxyTable:addBinding(key, binding)
local bindings = self.bindings
if not bindings then
bindings = {}
self.bindings = bindings
end
local keyBindings = bindings[key]
if not keyBindings then
keyBindings = setmetatable({}, Binding.weakMetaTable)
bindings[key] = keyBindings
end
table.insert(keyBindings, binding)
binding:valueChanged(self[key], self[key])
return binding
end
function Binding.proxyTable:removeBinding(key, binding)
local keyBindings = self.bindings[key]
return PtUtil.removeObject(keyBindings, binding) ~= -1
end
function Binding.bind(target, key, value)
local listener = function(t, k, old, new)
target[key] = new
end
value:addValueListener(listener)
local boundto = target.boundto
if not boundto then
boundto = {}
target.boundto = boundto
end
local boundtoKey = boundto[key]
assert(not boundtoKey, key .. " is already bound to another value")
boundto[key] = value
local bindTargets = value.bindTargets
if not bindTargets then
bindTargets = {}
value.bindTargets = bindTargets
end
local bindKeyTargets = bindTargets[target]
if not bindKeyTargets then
bindKeyTargets = {}
bindTargets[target] = bindKeyTargets
end
bindKeyTargets[key] = listener
target[key] = value.value
end
Binding.proxyTable.bind = Binding.bind
function Binding.bindBidirectional(t1, k1, t2, k2)
t1:bind(k1, Binding(t2, k2))
t2:bind(k2, Binding(t1, k1))
end
Binding.proxyTable.bindBidirectional = Binding.bindBidirectional
function Binding.unbind(target, key)
local binding = target.boundto[key]
if binding then
binding:removeValueListener(binding.bindTargets[target][key])
binding.bindTargets[target][key] = nil
target.boundto[key] = nil
end
end
Binding.proxyTable.unbind = Binding.unbind
function Binding.proxy(instance)
return setmetatable(
{_instance = instance},
Binding.proxyTable
)
end
--------------------------------------------------------------------------------
-- BindingFunctions.lua
--------------------------------------------------------------------------------
local createFunction = function(f)
return function(...)
local out = Binding.proxy(setmetatable({}, Binding.valueTable))
local getters = {}
local boundto = {self}
local args = table.pack(...)
local numArgs = args.n
for i = 1, numArgs, 1 do
local value = args[i]
local getter
if Binding.isValue(value) then
getter = function()
return value.value
end
else
getter = function()
return value
end
end
getters[i] = getter
end
out.valueChanged = function(binding, old, new)
out.value = f(table.unpack(getters))
end
for i = 1, numArgs, 1 do
local value = args[i]
if Binding.isValue(value) then
value:addValueBinding(out)
end
table.insert(boundto, value)
end
out.boundto = boundto
return out
end
end
Binding.valueTable.tostring = createFunction(
function(value)
return tostring(value())
end
)
Binding.tostring = Binding.valueTable.tostring
Binding.valueTable.tonumber = createFunction(
function(value)
return tonumber(value())
end
)
Binding.tonumber = Binding.valueTable.tonumber
Binding.valueTable.add = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out + value()
end
return out
end
)
Binding.add = Binding.valueTable.add
Binding.valueTable.sub = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out - value()
end
return out
end
)
Binding.sub = Binding.valueTable.sub
Binding.valueTable.mul = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out * value()
end
return out
end
)
Binding.mul = Binding.valueTable.mul
Binding.valueTable.div = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out / value()
end
return out
end
)
Binding.div = Binding.valueTable.div
Binding.valueTable.mod = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out % value()
end
return out
end
)
Binding.mod = Binding.valueTable.mod
Binding.valueTable.pow = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out ^ value()
end
return out
end
)
Binding.pow = Binding.valueTable.pow
Binding.valueTable.negate = createFunction(
function(value)
return -value()
end
)
Binding.negate = Binding.valueTable.negate
Binding.valueTable.concat = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out .. value()
end
return out
end
)
Binding.concat = Binding.valueTable.concat
Binding.valueTable.len = createFunction(
function(value)
return #value()
end
)
Binding.len = Binding.valueTable.len
Binding.valueTable.eq = createFunction(
function(a, b)
return a() == b()
end
)
Binding.eq = Binding.valueTable.eq
Binding.valueTable.ne = createFunction(
function(a, b)
return a() ~= b()
end
)
Binding.ne = Binding.valueTable.ne
Binding.valueTable.lt = createFunction(
function(a, b)
return a() < b()
end
)
Binding.lt = Binding.valueTable.lt
Binding.valueTable.gt = createFunction(
function(a, b)
return a() > b()
end
)
Binding.gt = Binding.valueTable.gt
Binding.valueTable.le = createFunction(
function(a, b)
return a() <= b()
end
)
Binding.le = Binding.valueTable.le
Binding.valueTable.ge = createFunction(
function(a, b)
return a() >= b()
end
)
Binding.ge = Binding.valueTable.ge
Binding.valueTable.AND = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out and value()
end
return out
end
)
Binding.AND = Binding.valueTable.AND
Binding.valueTable.OR = createFunction(
function(first, ...)
local out = first()
local args = table.pack(...)
for _,value in ipairs(args) do
out = out or value()
end
return out
end
)
Binding.OR = Binding.valueTable.OR
Binding.valueTable.NOT = createFunction(
function(value)
return not value()
end
)
Binding.NOT = Binding.valueTable.NOT
Binding.valueTable.THEN = function(self, ifTrue, ifFalse)
local out = Binding.proxy(setmetatable({}, Binding.valueTable))
local trueFunction
local falseFunction
local boundto = {self}
if Binding.isValue(ifTrue) then
trueFunction = function()
return ifTrue.value
end
else
trueFunction = function()
return ifTrue
end
end
if Binding.isValue(ifFalse) then
falseFunction = function()
return ifFalse.value
end
else
falseFunction = function()
return ifFalse
end
end
out.valueChanged = function(binding, old, new)
if self.value then
out.value = trueFunction()
else
out.value = falseFunction()
end
end
self:addValueBinding(out)
if Binding.isValue(ifTrue) then
ifTrue:addValueBinding(out)
table.insert(boundto, ifTrue)
end
if Binding.isValue(ifFalse) then
ifFalse:addValueBinding(out)
table.insert(boundto, ifFalse)
end
out.boundto = boundto
return out
end
Binding.THEN = Binding.valueTable.THEN
--------------------------------------------------------------------------------
-- GUI.lua
--------------------------------------------------------------------------------
GUI = {
components = {},
mouseState = {},
keyState = {},
mousePosition = {0, 0}
}
function GUI.add(component)
GUI.components[#GUI.components + 1] = component
component:setParent(nil)
end
function GUI.remove(component)
for index,comp in ripairs(GUI.components) do
if (comp == component) then
component:removeSelf()
return true
end
end
return false
end
function GUI.setFocusedComponent(component)
local focusedComponent = GUI.focusedComponent
if focusedComponent then
focusedComponent.hasFocus = false
end
GUI.focusedComponent = component
component.hasFocus = true
end
function GUI.clickEvent(position, button, pressed)
GUI.mouseState[button] = pressed
local components = GUI.components
local topFound = false
for index,component in ripairs(components) do
if component.visible ~= false then
if not topFound then
if component:contains(position) then
table.remove(components, index)
components[#components + 1] = component
topFound = true
end
end
if GUI.clickEventHelper(component, position, button, pressed) then
break
end
end
end
end
function GUI.clickEventHelper(component, position, button, pressed)
local children = component.children
for _,child in ripairs(children) do
if child.visible ~= false then
if GUI.clickEventHelper(child, position, button, pressed) then
return true
end
end
end
if component.clickEvent then
if component:contains(position) then
if component:clickEvent(position, button, pressed) then
GUI.setFocusedComponent(component)
return true
end
end
end
end
function GUI.keyEvent(key, pressed)
GUI.keyState[key] = pressed
local component = GUI.focusedComponent
while component do
if component.visible ~= false then
local keyEvent = component.keyEvent
if keyEvent then
if keyEvent(component, key, pressed) then
return
end
end
end
component = component.parent
end
end
function GUI.step(dt)
GUI.mousePosition = console.canvasMousePosition()
local hoverComponent
for _,component in ipairs(GUI.components) do
if component.visible ~= false then
hoverComponent = component:step(dt) or hoverComponent
end
end
if hoverComponent then
hoverComponent.mouseOver = true
end
end
--------------------------------------------------------------------------------
-- Component.lua
--------------------------------------------------------------------------------
Component = class()
Component.x = 0
Component.y = 0
Component.width = 0
Component.height = 0
Component.mouseOver = nil
Component.hasFocus = nil
function Component:_init()
self.children = {}
self.offset = Binding.proxy({0, 0})
end
function Component:add(child)
local children = self.children
children[#children + 1] = child
child:setParent(self)
end
function Component:remove(child)
local children = self.children
for index,comp in ripairs(children) do
if (comp == child) then
child:removeSelf()
return true
end
end
return false
end
function Component:removeSelf()
local siblings
if self.parent then
siblings = self.parent.children
else
siblings = GUI.components