-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.lua
917 lines (748 loc) · 22.2 KB
/
utils.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
local utf8 = require("utf8")
local uiu = {}
local patchyStatus, patchy = pcall(require, "patchy")
if not patchyStatus then
patchy = require("ui.patchy")
end
uiu.patchy = patchy
function uiu.nop()
end
function uiu.default(value, default)
if value ~= nil then
return value
end
return default
end
function uiu.fract(num, default)
if not num then
return num, default
end
local fract
if num < 0 then
num, fract = uiu.fract(-num, default)
return -num, fract
end
fract = num % 1
if fract <= 0.0001 or fract >= 0.9999 then
fract = default or 0
elseif fract <= 0.005 then
fract = 0
elseif fract >= 0.995 then
fract = 1
end
return num - (num % 1), fract
end
uiu.dataRoots = {}
function uiu.path(path)
local root = ""
local split = path:find(":")
if split then
if split == 1 then
path = path:sub(2)
else
root = path:sub(1, split - 1)
path = path:sub(split + 1)
end
end
root = root or ""
local mappedRoot = uiu.dataRoots[root]
if mappedRoot then
path = mappedRoot .. "/" .. path
elseif #root > 0 then
path = root .. "/data/" .. path
else
path = "data/" .. path
end
return path
end
uiu.imageCache = {}
function uiu.image(path)
path = uiu.path(path) .. ".png"
local cache = uiu.imageCache
local img = cache[path]
if img then
return img
end
img = love.graphics.newImage(path, { mipmaps = true } )
img:setFilter("linear", "linear")
img:setMipmapFilter("linear", 1)
cache[path] = img
return img
end
uiu.patchCache = {}
function uiu.patch(path, ids)
local patch, force
if ids then
patch, force = uiu.patch(string.format(path, "_" .. ids[1]))
if patch or force then
return patch, force
end
for i = 1, #ids do
patch, force = uiu.patch(string.format(path, ids[i]))
if patch or force then
return patch, force
end
end
return uiu.patch(string.format(path, "default"))
end
force = false
::repath::
path = uiu.path(path)
local txt = love.filesystem.read(path .. ".9.txt")
if txt then
force = true
path = txt:match("^()%s*$") and "" or txt:match("^%s*(.*%S)")
if #path == 0 then
return false, true
end
goto repath
end
path = path .. ".9.png"
local cache = uiu.patchCache
patch = cache[path]
if patch ~= nil then
return patch, force
end
local patchStatus
patchStatus, patch = pcall(patchy.load, path)
if not patchStatus then
patch = false
end
cache[path] = patch
return patch, force
end
function uiu.isCallback(cb)
if type(cb) == "function" then
return true
end
local mt = getmetatable(cb)
return mt and mt.__call and true
end
function uiu.round(x)
return x + 0.5 - (x + 0.5) % 1
end
math.round = math.round or uiu.round
function uiu.sign(x)
return x > 0 and 1 or x < 0 and -1 or 0
end
math.sign = math.sign or uiu.sign
function uiu.listRange(from, to, step)
local output = {}
local count = 1
for i = from, to, (step or 1) do
output[count] = i
count = count + 1
end
return output
end
function uiu.map(input, fn)
local output = {}
for k, v in pairs(input) do
local newV, newK = fn(v, k, input)
if newK ~= nil then
output[newK] = newV
else
output[k] = newV
end
end
return output
end
function uiu.fadeSwap(faded, color, colorPrev, prev, next)
if prev == next then
return faded, color, colorPrev, colorPrev, next
end
local copy = {color[1], color[2], color[3], color[4], color[5]}
return true, color, copy, copy, next
end
function uiu.fade(faded, f, color, prev, next)
if next[5] then
if f < 1 and prev[5] and color[5] then
color[1] = prev[1] + (next[1] - prev[1]) * f
color[2] = prev[2] + (next[2] - prev[2]) * f
color[3] = prev[3] + (next[3] - prev[3]) * f
color[4] = prev[4] + (next[4] - prev[4]) * f
color[5] = prev[5] + (next[5] - prev[5]) * f
return true
else
if color[1] == next[1] and color[2] == next[2] and color[3] == next[3] and color[4] == next[4] and color[5] == next[5] then
return faded
end
color[1] = next[1]
color[2] = next[2]
color[3] = next[3]
color[4] = next[4]
color[5] = next[5]
return true
end
else
if f < 1 and prev[4] and color[4] then
color[1] = prev[1] + (next[1] - prev[1]) * f
color[2] = prev[2] + (next[2] - prev[2]) * f
color[3] = prev[3] + (next[3] - prev[3]) * f
color[4] = prev[4] + (next[4] - prev[4]) * f
return true
else
if color[1] == next[1] and color[2] == next[2] and color[3] == next[3] and color[4] == next[4] then
return faded
end
color[1] = next[1]
color[2] = next[2]
color[3] = next[3]
color[4] = next[4]
return true
end
end
end
-- Adapted from https://love2d.org/forums/viewtopic.php?p=196103&sid=ee7a367880e9968d161c042542058a93#p196103
function uiu.getWrap(font, input, width)
local wrap = {}
_, wrap = font:getWrap(input, width)
if type(input) == "string" then
return table.concat(wrap, "\n")
end
-- Copy the input table to not modify any passed references.
local ct = {}
for i = 1, #input do
ct[i] = input[i]
end
local lines = {}
local li = 1
local ci = 1
local cl = #ct
local wi = 1
local wl = #wrap
lines[1] = {}
while ci <= cl and wi <= wl do
local from, to = string.find(wrap[wi], ct[ci + 1], nil, true)
if from and to then -- wrap contains full ct line
-- copy full ct line with color
lines[wi][li] = ct[ci]
lines[wi][li + 1] = ct[ci + 1]
wrap[wi] = string.sub(wrap[wi], to + 1, -1)
li = li + 2
ci = ci + 2
else -- wrap is not containing a full ct line
-- copy wrap line in ct color and modify ct
lines[wi][li] = ct[ci]
lines[wi][li + 1] = wrap[wi]
ct[ci + 1] = string.sub(ct[ci + 1], #wrap[wi] + 1, -1)
li = 1
wi = wi + 1
lines[wi] = {}
end
end
-- TODO: Replace the above code from the love2d forums to immediately return a text-compatible table.
wi = 1
wrap = {}
local lc = #lines
for li = 1, lc do
local line = lines[li]
for ci = 1, #line - 1 do
wrap[wi] = line[ci]
wi = wi + 1
end
if li < lc - 1 then
wrap[wi] = line[#line] .. "\n"
else
wrap[wi] = line[#line]
end
wi = wi + 1
end
return wrap
end
function uiu.countformat(count, one, more)
return string.format(count == 1 and one or more, count)
end
local prevR = -1
local prevG = -1
local prevB = -1
local prevA = -1
function uiu.resetColor()
prevR = -1
end
function uiu.setColor(r, g, b, a)
if r then
if g then
a = a or 1
else
g = r[2]
if not g then
return false
end
b = r[3]
a = r[4] or 1
r = r[1]
end
else
return false
end
if a < 0.0001 then
return false
end
if r ~= prevR or g ~= prevG or b ~= prevB or a ~= prevA then
love.graphics.setColor(r, g, b, a)
prevR = r
prevG = g
prevB = b
prevA = a
end
return true
end
function uiu.drawCanvas(canvas, x, y, r, sx, sy, ox, oy, kx, ky)
if canvas.draw then
canvas:draw(x, y, r, sx, sy, ox, oy, kx, ky)
else
love.graphics.setBlendMode("alpha", "premultiplied")
love.graphics.draw(canvas.canvas, x, y, r, sx, sy, ox, oy, kx, ky)
love.graphics.setBlendMode("alpha", "alphamultiply")
end
end
function uiu.magic(fn, ...)
local magic = uiu.magic
local mask = { ... }
return function(...)
local input = { ... }
local args = {}
local ii = 1
for i = 1, #mask do
local arg = mask[i]
if arg == magic then
arg = input[ii]
ii = ii + 1
end
args[i] = arg
end
local offs = #args + 1
for i = ii, #input do
args[i - ii + offs] = input[i]
end
return fn(table.unpack(args))
end
end
function uiu.hook(target, nameOrMap, cb)
if type(target) == "string" or (target and not nameOrMap) then
cb = nameOrMap
nameOrMap = target
return function(target)
uiu.hook(target, nameOrMap, cb)
end
end
if type(nameOrMap) ~= "string" then
for name, cb in pairs(nameOrMap) do
uiu.hook(target, name, cb)
end
return target
end
local name = nameOrMap
local orig = target[name] or uiu.nop
target[name] = function(...)
return cb(orig, ...)
end
return target
end
function uiu.fillWidth(el, arg2, arg3)
local except
local fract
local respectSiblings
local late
local function apply(el)
except = uiu.default(except, 0)
except, fract = uiu.fract(except, 1)
respectSiblings = uiu.default(respectSiblings, false)
late = uiu.default(late, true)
local exceptSpacing = false -- FIXME: Expose exceptSpacing!
return
late and
uiu.hook(el, {
layoutLazy = function(orig, self)
-- Required to allow the container to shrink again.
orig(self)
self.width = 0
end,
layoutLateLazy = function(orig, self)
-- Always reflow this child whenever its parent gets reflowed.
self:layoutLate()
self:repaint()
end,
layoutLate = function(orig, self)
local extra = except
if exceptSpacing then
extra = extra + self.parent.style.spacing
end
local width = self.parent.innerWidth * fract - extra
if respectSiblings then
local spacing = self.parent.style.spacing
local children = self.parent.children
for i = 1, #children do
local c = children[i]
if c ~= self then
width = width - c.width - spacing
end
end
end
width = math.floor(width)
self.width = width
self.innerWidth = width - (self.style:getIndex("padding", 1) or 0) - (self.style:getIndex("padding", 3) or 0)
orig(self)
end
})
or
uiu.hook(el, {
layoutLazy = function(orig, self)
-- Always reflow this child whenever its parent gets reflowed.
self:layout()
self:repaint()
end,
layout = function(orig, self)
local extra = except
if exceptSpacing then
extra = extra + self.parent.style.spacing
end
local width = self.parent.innerWidth * fract - extra
if respectSiblings then
local spacing = self.parent.style.spacing
local children = self.parent.children
for i = 1, #children do
local c = children[i]
if c ~= self then
width = width - c.width - spacing
end
end
end
width = math.floor(width)
self.width = width
self.innerWidth = width - (self.style:get("padding") or 0) * 2
orig(self)
end
})
end
if el == nil then
return apply
elseif type(el) == "number" then
except = el
respectSiblings = arg2
late = arg3
return apply
elseif type(el) == "boolean" then
respectSiblings = el
late = arg2
return apply
else
return apply(el)
end
end
function uiu.fillHeight(el, arg2, arg3)
local except
local fract
local respectSiblings
local late
local function apply(el)
except = uiu.default(except, 0)
except, fract = uiu.fract(except, 1)
respectSiblings = uiu.default(respectSiblings, false)
late = uiu.default(late, true)
local exceptSpacing = false -- FIXME: Expose exceptSpacing!
return
late and
uiu.hook(el, {
layoutLazy = function(orig, self)
-- Required to allow the container to shrink again.
orig(self)
self.height = 0
end,
layoutLateLazy = function(orig, self)
-- Always reflow this child whenever its parent gets reflowed.
self:layoutLate()
self:repaint()
end,
layoutLate = function(orig, self)
local extra = except
if exceptSpacing then
extra = extra + self.parent.style.spacing
end
local height = self.parent.innerHeight * fract - extra
if respectSiblings then
local spacing = self.parent.style.spacing
local children = self.parent.children
for i = 1, #children do
local c = children[i]
if c ~= self then
height = height - c.height - spacing
end
end
end
height = math.floor(height)
self.height = height
self.innerHeight = height - (self.style:get("padding") or 0) * 2
orig(self)
end
})
or
uiu.hook(el, {
layoutLazy = function(orig, self)
-- Always reflow this child whenever its parent gets reflowed.
self:layout()
self:repaint()
end,
layout = function(orig, self)
local extra = except
if exceptSpacing then
extra = extra + self.parent.style.spacing
end
local height = self.parent.innerHeight * fract - extra
if respectSiblings then
local spacing = self.parent.style.spacing
local children = self.parent.children
for i = 1, #children do
local c = children[i]
if c ~= self then
height = height - c.height - spacing
end
end
end
height = math.floor(height)
self.height = height
self.innerHeight = height - (self.style:get("padding") or 0) * 2
orig(self)
end
})
end
if el == nil then
return apply
elseif type(el) == "number" then
except = el
respectSiblings = arg2
late = arg3
return apply
elseif type(el) == "boolean" then
respectSiblings = el
late = arg2
return apply
else
return apply(el)
end
end
function uiu.fill(el)
local except
local fract
local function apply(el)
except = uiu.default(except, 0)
except, fract = uiu.fract(except, 1)
return uiu.hook(el, {
layoutLazy = function(orig, self)
-- Required to allow the container to shrink again.
orig(self)
self.width = 0
self.height = 0
end,
layoutLateLazy = function(orig, self)
-- Always reflow this child whenever its parent gets reflowed.
self:layoutLate()
self:repaint()
end,
layoutLate = function(orig, self)
local width = math.floor(self.parent.innerWidth * fract - except)
local height = math.floor(self.parent.innerHeight * fract - except)
self.width = width
self.height = height
local padding = self.style:get("padding") or 0
self.innerWidth = width - padding * 2
self.innerHeight = height - padding * 2
orig(self)
end
})
end
if el == nil then
return apply
elseif type(el) == "number" then
except = el
return apply
else
return apply(el)
end
end
function uiu.at(el, arg2, arg3)
local x
local xf
local y
local yf
local function apply(el)
x, xf = uiu.fract(x, 0)
y, yf = uiu.fract(y, 0)
return
uiu.hook(el, {
layoutLateLazy = function(orig, self)
-- Always reflow this child whenever its parent gets reflowed.
self:layoutLate()
self:repaint()
end,
layoutLate = function(orig, self)
local parent = self.parent
if x then
self.realX = math.floor(x + parent.innerWidth * xf)
end
if y then
self.realY = math.floor(y + parent.innerHeight * yf)
end
orig(self)
end
})
end
if type(el) == "number" or type(el) == "boolean" then
x = el
y = arg2
return apply
else
x = arg2
y = arg3
return apply(el)
end
end
function uiu.rightbound(el, arg2)
local offs
local offsf
local function apply(el)
offs, offsf = uiu.fract(offs, 0)
if not offs then
offs = 0
end
return
uiu.hook(el, {
layoutLateLazy = function(orig, self)
-- Always reflow this child whenever its parent gets reflowed.
self:layoutLate()
self:repaint()
end,
layoutLate = function(orig, self)
local parent = self.parent
self.realX = math.floor(parent.width - (parent.style:get("padding") or 0) - self.width - offs - parent.innerWidth * offsf)
orig(self)
end
})
end
if type(el) == "number" then
offs = el
return apply
else
offs = arg2
return apply(el)
end
end
function uiu.bottombound(el, arg2)
local offs
local offsf
local function apply(el)
offs, offsf = uiu.fract(offs, 0)
if not offs then
offs = 0
end
return
uiu.hook(el, {
layoutLateLazy = function(orig, self)
-- Always reflow this child whenever its parent gets reflowed.
self:layoutLate()
self:repaint()
end,
layoutLate = function(orig, self)
local parent = self.parent
self.realY = math.floor(parent.height - (parent.style:get("padding") or 0) - self.height - offs - parent.innerHeight * offsf)
orig(self)
end
})
end
if type(el) == "number" then
offs = el
return apply
else
offs = arg2
return apply(el)
end
end
-- Word "jumping" typically allows underscores as part of words
function uiu.isWordCharacter(char, allowUnderscore)
if allowUnderscore ~= false and char == "_" then
return true
end
if char == " " then
return false
end
-- Punctuation characters
if string.match(char, "%p") then
return false
end
return true
end
function uiu.findWordBorder(text, index, direction)
local len = utf8.len(text)
if len == 0 then
return 0
end
local start = index
local stop = direction > 0 and len or 1
for i = start, stop, direction do
local offset = utf8.offset(text, i)
local char = utf8.char(utf8.codepoint(text, offset, offset))
local wordChar = uiu.isWordCharacter(char)
if not wordChar then
return i - direction
end
end
return stop
end
function uiu.getTextCursorOffset(font, text, index)
-- Returns nil if out of bounds for the text, default to 0
local utf8Offset = utf8.offset(text, index + 1) or 0
return index == 0 and 0 or font:getWidth(text:sub(1, utf8Offset - 1))
end
function uiu.getTextIndexForCursor(font, text, x)
local min = 0
local max = utf8.len(text) + 1
while max - min > 1 do
local mid = min + math.ceil((max - min) / 2)
local midx = font:getWidth(text:sub(1, utf8.offset(text, mid + 1) - 1)) - font:getWidth(text:sub(utf8.offset(text, mid), utf8.offset(text, mid + 1) - 1)) * 0.4
if x <= midx then
max = mid
else
min = mid
end
end
return min
end
local mtStyleDeep = {
__index = function(self, key)
return self.__styleOrig[key]
end,
__newindex = function(self, key, value)
self.__styleOrig[key] = value
local children = self.el.children
for i = 1, #children do
children[i].style[key] = value
end
end
}
function uiu.styleDeep(el)
el.__style = setmetatable({
el = el,
__styleOrig = el.__style,
get = function(self, ...)
return self.__styleOrig:get(...)
end,
getIndex = function(self, ...)
return self.__styleOrig:getIndex(...)
end,
}, mtStyleDeep)
el:hook({
repaint = function(orig, self)
local children = self.children
for i = 1, #children do
children[i]:repaint()
end
end
})
return el
end
table.pack = table.pack or function(...)
return { ... }
end
table.unpack = table.unpack or _G.unpack
return uiu