-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
nlay.lua
1505 lines (1268 loc) · 43.9 KB
/
nlay.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
-- NPad's Layouting Library, based on ConstraintLayout
--
-- Copyright (c) 2024 Miku AuahDark
--
-- 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.
---@class NLay._Cache
---@field package x number?
---@field package y number?
---@field package w number?
---@field package h number?
---@field package referenced (NLay.BaseConstraint?)[]
---@field package refLength integer
---@class NLay.BaseConstraint
---@field package cache table<NLay.BaseConstraint, NLay._Cache?>
---@field protected userTag any
---@field public _NLay_type_ string
local BaseConstraint = {
cache = setmetatable({}, {__mode = "k"})
}
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function BaseConstraint:get(offx, offy)
---@diagnostic disable-next-line: missing-return
end
---Tag this constraint with some userdata, like an id, for example.
---Useful to keep track of constraints when they're rebuilt.
---@generic T: NLay.BaseConstraint
---@param self T
---@param userdata any
---@return T
function BaseConstraint:tag(userdata)
self.userTag = userdata
return self
end
---@return any
function BaseConstraint:getTag()
return self.userTag
end
local function dupmethods(t)
local r = {}
for k, v in pairs(t) do
if type(v) == "function" then
r[k] = v
end
end
return r
end
---@class NLay.Constraint: NLay.BaseConstraint
---@field package top NLay.BaseConstraint?
---@field package left NLay.BaseConstraint?
---@field package bottom NLay.BaseConstraint?
---@field package right NLay.BaseConstraint?
---@field package inTop boolean
---@field package inLeft boolean
---@field package inBottom boolean
---@field package inRight boolean
---@field package marginX number
---@field package marginY number
---@field package marginW number
---@field package marginH number
---@field private w number
---@field private h number
---@field package pad number[]
---@field private relW boolean
---@field private relH boolean
---@field private biasHorz number
---@field private biasVert number
---@field package parent NLay.BaseConstraint
local Constraint = dupmethods(BaseConstraint)
---@private
Constraint.__index = Constraint ---@diagnostic disable-line: inject-field
Constraint._NLay_type_ = "NLay.Constraint"
---@param a number
---@param b number
---@param t number
local function mix(a, b, t)
return (1 - t) * a + t * b
end
---@param self NLay.Constraint
---@param target NLay.BaseConstraint
local function resolveWithoutPadding(self, target)
local x, y, w, h = target:get()
if self.parent ~= target and target._NLay_type_ == Constraint._NLay_type_ then
---@cast self NLay.Constraint
x = x - self.pad[2]
y = y - self.pad[1]
w = w + self.pad[4] + self.pad[2]
h = h + self.pad[3] + self.pad[1]
end
return x, y, w, h
end
---@param self NLay.Constraint
local function resolveWidthSize0(self)
local x, width
if self.left == nil or self.right == nil then
error("insufficient constraint for width 0")
end
-- Left
local e1x, _, e1w = resolveWithoutPadding(self, self.left)
if self.inLeft then
x = e1x + self.marginX
else
x = e1x + e1w + self.marginX
end
-- Right
local e2x, _, e2w = resolveWithoutPadding(self, self.right)
if self.inRight then
width = e2x + e2w - x - self.marginW
else
width = e2x - x - self.marginW
end
return x, width
end
---@param self NLay.Constraint
local function resolveHeightSize0(self)
local y, height
if self.bottom == nil or self.top == nil then
error("insufficient constraint for height 0")
end
local e1y, _, e1h = select(2, resolveWithoutPadding(self, self.top))
if self.inTop then
y = e1y + self.marginY
else
y = e1y + e1h + self.marginY
end
local e2y, _, e2h = select(2, resolveWithoutPadding(self, self.bottom))
if self.inBottom then
height = e2y + e2h - y - self.marginH
else
height = e2y - y - self.marginH
end
return y, height
end
local function isPercentMode(str, name)
if str == "percent" then
return true
elseif str == "pixel" then
return false
else
error("invalid \""..name.."\" size mode (\"pixel\" or \"percent\" expected)", 2)
end
end
---@param constraint NLay.BaseConstraint
local function getCachedData(constraint)
local c = BaseConstraint.cache[constraint]
if not c then
return nil, nil, nil, nil
end
return c.x, c.y, c.w, c.h
end
---@param constraint NLay.BaseConstraint
local function getCacheEntry(constraint)
local c = BaseConstraint.cache[constraint]
if not c then
c = {
referenced = setmetatable({}, {__mode = "v"}),
refLength = 0
}
BaseConstraint.cache[constraint] = c
end
return c
end
---@param constraint NLay.BaseConstraint
---@param x number
---@param y number
---@param w number
---@param h number
local function insertCached(constraint, x, y, w, h)
local c = getCacheEntry(constraint)
c.x, c.y, c.w, c.h = x, y, w, h
end
---@param constraint NLay.BaseConstraint
local function invalidateCache(constraint)
local c = getCacheEntry(constraint)
-- Constraint that reference `constraint` must be invalidated too.
local newRefLen = 0
for i = 1, c.refLength do
local other = c.referenced[i]
if other then
newRefLen = i
invalidateCache(other)
end
end
c.refLength = newRefLen
c.x, c.y, c.w, c.h = nil, nil, nil, nil
end
---Constraint `constraint` reference `other` constraint.
---@param constraint NLay.BaseConstraint
---@param other NLay.BaseConstraint
local function addRefCache(constraint, other)
local c = getCacheEntry(other)
local targetIndex = 0
for i = 1, c.refLength do
local r = c.referenced[i]
-- No duplicates
if r == constraint then
return
elseif not r then
targetIndex = i
end
end
if targetIndex == 0 then
c.refLength = c.refLength + 1
targetIndex = c.refLength
end
c.referenced[targetIndex] = constraint
end
---@generic T
---@param ... T|nil
---@return T
local function selectDefault(...)
local value
for i = 1, select("#", ...) do
local v = select(i, ...)
if v ~= nil then
value = v
break
end
end
return value
end
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function Constraint:get(offx, offy)
local finalX, finalY, finalW, finalH = getCachedData(self)
if not (finalX and finalY and finalW and finalH) then
if (self.left ~= nil or self.right ~= nil) and (self.top ~= nil or self.bottom ~= nil) then
local x, y, w, h
local width, height = self.w, self.h
-- Convert percent values to pixel values
if self.relW then
width = select(2, resolveWidthSize0(self)) * self.w
end
if self.relH then
height = select(2, resolveHeightSize0(self)) * self.h
end
if width == -1 then
-- Match parent
local px, _, pw = resolveWithoutPadding(self, self.parent)
x, width = px, pw
elseif width == 0 then
-- Match constraint
x, width = resolveWidthSize0(self)
end
if height == -1 then
-- Match parent
---@type number,number,number
local py, _, ph = select(2, resolveWithoutPadding(self, self.parent)) ---@diagnostic disable-line: assign-type-mismatch
y, height = py, ph
elseif height == 0 then
-- Match constraint
y, height = resolveHeightSize0(self)
end
do
local l, r
w = width
if self.left then
-- Left orientation
local e1x, _, e1w = resolveWithoutPadding(self, self.left)
if self.inLeft then
l = e1x + self.marginX
else
l = e1x + e1w + self.marginX
end
end
if self.right then
-- Right orientation
local e2x, _, e2w = resolveWithoutPadding(self, self.right)
if self.inRight then
r = e2x + e2w - self.marginW - w
else
r = e2x - self.marginW - w
end
end
if l ~= nil and r ~= nil then
-- Horizontally centered
x = mix(l, r, self.biasHorz)
else
x = l or r
end
end
do
local t, b
h = height
if self.top then
-- Top orientation
local e1y, _, e1h = select(2, resolveWithoutPadding(self, self.top))
if self.inTop then
t = e1y + self.marginY
else
t = e1y + e1h + self.marginY
end
end
if self.bottom then
-- Bottom orientation
local e2y, _, e2h = select(2, resolveWithoutPadding(self, self.bottom))
if self.inBottom then
b = e2y + e2h - self.marginH - h
else
b = e2y - self.marginH - h
end
end
if t ~= nil and b ~= nil then
-- Vertically centered
y = mix(t, b, self.biasVert)
else
y = t or b
end
end
assert(x and y and w and h, "fatal error please report!")
finalX = x + self.pad[2]
finalY = y + self.pad[1]
finalW = math.max(w - self.pad[4] - self.pad[2], 0)
finalH = math.max(h - self.pad[3] - self.pad[1], 0)
insertCached(self, finalX, finalY, finalW, finalH)
else
error("insufficient constraint")
end
end
return finalX + (offx or 0), finalY + (offy or 0), finalW, finalH
end
---Set the constraint margin
---@param margin number|number[] Either number to apply all margins or table {top, left, bottom, right} margin. Defaults to 0 for absent/nil values.
---@return NLay.Constraint
function Constraint:margin(margin)
margin = margin or 0
if type(margin) == "number" then
self.marginX, self.marginY, self.marginW, self.marginH = margin, margin, margin, margin
else
self.marginX = margin[2] or 0
self.marginY = margin[1] or 0
self.marginW = margin[4] or 0
self.marginH = margin[3] or 0
end
invalidateCache(self)
return self
end
---@param kind string
---@param c1name string
---@param c2name string
---@param c1 NLay.BaseConstraint?
---@param c2 NLay.BaseConstraint?
local function constraintValidate(kind, c1name, c2name, c1, c2)
if c1 == nil then
error(kind.." but "..c1name.." constraint is unset", 2)
elseif c2 == nil then
error(kind.." but "..c2name.." constraint is unset", 2)
end
end
---Set the size of constraint. If width/height is 0, it will calculate it based on the other connected constraint.
---If it's -1, then it will use parent's width/height minus padding.
---
---"percent" width requires left and right constraint attached. "percent" height requires top and bottom constraint
---attached. Both defaults to "pixel" if not specified, which ensure older code works without modification.
---@param width number Constraint width.
---@param height number Constraint height.
---@param modeW? '"percent"' | '"pixel"'
---@param modeH? '"percent"' | '"pixel"'
---@return NLay.Constraint
function Constraint:size(width, height, modeW, modeH)
assert(width >= 0 or width == -1, "invalid width")
assert(height >= 0 or height == -1, "invalid height")
self.w, self.h = width, height
self.relW = isPercentMode(modeW or "pixel", "width")
self.relH = isPercentMode(modeH or "pixel", "height")
-- Validate
if self.w == 0 then
constraintValidate("size 0 width", "left", "right", self.left, self.right)
elseif self.relW then
constraintValidate("relative width", "left", "right", self.left, self.right)
end
if self.h == 0 then
constraintValidate("size 0 height", "top", "bottom", self.top, self.bottom)
elseif self.relH then
constraintValidate("relative height", "top", "bottom", self.top, self.bottom)
end
-- Ok. Invalidate cache.
invalidateCache(self)
return self
end
---Set the constraint bias. By default, for fixed width/height, the position are centered around (bias 0.5).
---@param horz number|nil Horizontal bias, real value between 0..1 inclusive.
---@param vert number|nil Vertical bias, real value between 0..1 inclusive.
---@param unclamped? boolean Do not limit bias ratio?
---@return NLay.Constraint
function Constraint:bias(horz, vert, unclamped)
if horz then
constraintValidate("setting horizontal bias", "left", "right", self.left, self.right)
if unclamped then
self.biasHorz = horz
else
self.biasHorz = math.min(math.max(horz, 0), 1)
end
end
if vert then
constraintValidate("setting vertical bias", "top", "bottom", self.top, self.bottom)
if unclamped then
self.biasVert = vert
else
self.biasVert = math.min(math.max(vert, 0), 1)
end
end
invalidateCache(self)
return self
end
---@class NLay.MaxConstraint: NLay.BaseConstraint
---@field private list NLay.BaseConstraint[]
local MaxConstraint = dupmethods(BaseConstraint)
---@private
MaxConstraint.__index = MaxConstraint ---@diagnostic disable-line: inject-field
MaxConstraint._NLay_type_ = "NLay.MaxConstraint"
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function MaxConstraint:get(offx, offy)
local minx, miny, maxx, maxy = self.list[1]:get()
maxx = maxx + minx
maxy = maxy + miny
for i = 2, #self.list do
local x, y, w, h = self.list[i]:get()
minx = math.min(minx, x)
miny = math.min(miny, y)
maxx = math.max(maxx, x + w)
maxy = math.max(maxy, y + h)
end
return minx + (offx or 0), miny + (offy or 0), maxx - minx, maxy - miny
end
---@class NLay.LineConstraint: NLay.BaseConstraint
---@field private constraint NLay.BaseConstraint
---@field private direction '"horizontal"' | '"vertical"'
---@field private mode '"percent"' | '"pixel"'
---@field private lineOffset number
---@field private flip boolean
local LineConstraint = dupmethods(BaseConstraint)
---@private
LineConstraint.__index = LineConstraint ---@diagnostic disable-line: inject-field
LineConstraint._NLay_type_ = "NLay.LineConstraint"
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function LineConstraint:get(offx, offy)
local fx, fy, fw, fh = getCachedData(self)
if not (fx and fy and fw and fh) then
local x, y, w, h = self.constraint:get()
if self.direction == "horizontal" then
-- Vertical line for horizontal constraint
if self.mode == "percent" then
-- Interpolate
--return mix(x, x + w, (self.flip and 1 or 0) + self.lineOffset) + offx, y + offy, 0, h
fx, fy, fw, fh = mix(x, x + w, (self.flip and 1 or 0) + self.lineOffset), y, 0, h
else
-- Offset
-- x + (self.flip and w or 0) + self.lineOffset + offx, y + offy, 0, h
fx, fy, fw, fh = x + (self.flip and w or 0) + self.lineOffset, y, 0, h
end
else
-- Horizontal line for vertical constraint
if self.mode == "percent" then
-- Interpolate
-- x + offx, mix(y, y + h, (self.flip and 1 or 0) + self.lineOffset) + offy, w, 0
fx, fy, fw, fh = x, mix(y, y + h, (self.flip and 1 or 0) + self.lineOffset), w, 0
else
-- Offset
--return x + offx, y + (self.flip and h or 0) + self.lineOffset + offy, w, 0
fx, fy, fw, fh = x, y + (self.flip and h or 0) + self.lineOffset, w, 0
end
end
insertCached(self, fx, fy, fw, fh)
end
return fx + (offx or 0), fy + (offy or 0), fw, fh
end
-- (Re)-set the line offset.
function LineConstraint:offset(off)
self.lineOffset = off + 0
invalidateCache(self)
return self
end
---@class NLay.GridCellConstraint: NLay.BaseConstraint
---@field private context NLay.Grid
---@field private x0 integer
---@field private y0 integer
local GridCellConstraint = dupmethods(BaseConstraint)
---@private
GridCellConstraint.__index = GridCellConstraint ---@diagnostic disable-line: inject-field
GridCellConstraint._NLay_type_ = "NLay.GridCellConstraint"
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function GridCellConstraint:get(offx, offy)
local x, y, w, h = self.context:_resolveCell(self.x0, self.y0)
return x + (offx or 0), y + (offy or 0), w, h
end
---@class NLay.Grid
---@field private constraint NLay.Constraint
---@field private list NLay.GridCellConstraint[]
---@field private rows integer
---@field private cols integer
---@field private hspacing number
---@field private vspacing number
---@field private hfl boolean
---@field private vfl boolean
---@field private cellW number
---@field private cellH number
---@field public _NLay_type_ string
local Grid = {_NLay_type_ = "NLay.Grid"}
---@private
Grid.__index = Grid ---@diagnostic disable-line: inject-field
---Retrieve GridCellConstraint at specified rows and columns.
---@param row integer Row number from 1 to max rows inclusive.
---@param col integer Column number from 1 to max columns inclusive.
function Grid:get(row, col)
local constraint = self.list[(row - 1) * self.cols + col]
if not constraint then
constraint = setmetatable({
context = self,
x0 = col - 1,
y0 = row - 1
}, GridCellConstraint)
self.list[(row - 1) * self.cols + col] = constraint
end
return constraint
end
---@private
function Grid:_invalidateCellCache()
-- Invalidate cache for cells
for y = 1, self.rows do
for x = 1, self.cols do
local constraint = self.list[(y - 1) * self.cols + x]
if constraint then
invalidateCache(constraint)
end
end
end
end
function Grid:spacing(h, v, hfl, vfl)
self.hspacing = h or self.hspacing
self.vspacing = v or self.vspacing
if hfl ~= nil then
self.hfl = not not hfl
end
if vfl ~= nil then
self.vfl = not not vfl
end
self:_invalidateCellCache()
return self
end
---Calls a function for each grid cell constraint.
---
---NOTE: This initializes all the grid cell constraint in the list, which may slow.
---@param func fun(constraint:NLay.GridCellConstraint,row:integer,col:integer,...)
function Grid:foreach(func, ...)
for y = 1, self.rows do
for x = 1, self.cols do
func(self:get(y, x), y, x, ...)
end
end
return self
end
local function dummy() end
---Forcefully initialize all the grid cell constraint in the list.
function Grid:preload()
return self:foreach(dummy)
end
---Set fixed grid cell size.
---
---On dynamic mode, this function does nothing.
---@param width number
---@param height number
function Grid:cellSize(width, height)
if self:isFixed() then
self.cellW, self.cellH = width or self.cellW, height or self.cellH
self:_updateSize()
end
return self
end
---Is the grid in dynamic mode or fixed mode?
---
---Dynamic mode means the cell size is calculated on-the-fly.
function Grid:isFixed()
return not not (self.cellW or self.cellH)
end
---Retrieve dimensions of a single cell.
---
---NOTE: On dynamic mode, this function resolve the whole constraint so use with care!
---@return number,number
function Grid:getCellDimensions()
local w, h = select(3, self:_resolveCell(0, 0))
---@diagnostic disable-next-line: return-type-mismatch
return w, h
end
---@package
function Grid:_updateSize()
local width = self.hspacing * (self.cols + (self.hfl and 1 or -1)) + self.cellW * self.cols
local height = self.vspacing * (self.rows + (self.vfl and 1 or -1)) + self.cellH * self.rows
self.constraint:size(width, height)
self:_invalidateCellCache()
end
---@package
---@param x number
---@param y number
function Grid:_resolveCell(x, y)
-- x and y must be 0-based
local xc, yc, w, h = self.constraint:get()
if self.cellW and self.cellH then
w, h = self.cellW, self.cellH
else
local cellW = (w - self.hspacing * (self.cols + (self.hfl and 1 or -1))) / self.cols
local cellH = (h - self.vspacing * (self.rows + (self.vfl and 1 or -1))) / self.rows
w, h = cellW, cellH
end
local xp = (x + (self.hfl and 1 or 0)) * self.hspacing + x * w
local yp = (y + (self.vfl and 1 or 0)) * self.vspacing + y * h
return xp + xc, yp + yc, w, h
end
---@class NLay.RatioConstraint: NLay.BaseConstraint
---@field private parent NLay.BaseConstraint
---@field private numerator number
---@field private denominator number
---@field private biasHorz number
---@field private biasVert number
---@field private expand boolean
local RatioConstraint = dupmethods(BaseConstraint)
---@private
RatioConstraint.__index = RatioConstraint ---@diagnostic disable-line: inject-field
RatioConstraint._NLay_type_ = "NLay.RatioConstraint"
---@param constraint NLay.BaseConstraint
---@param expand boolean
local function makeRatioConstraint(constraint, expand)
local result = setmetatable({
parent = constraint,
numerator = 1,
denominator = 1,
biasHorz = 0.5,
biasVert = 0.5,
expand = expand,
}, RatioConstraint)
addRefCache(result, constraint)
return result
end
---Set constraint aspect ratio.
---@param num number Rational number in form of `numerator/denominator` for `numerator:denominator` aspect ratio (absolute value is taken).
---@return NLay.RatioConstraint
---@diagnostic disable-next-line: duplicate-set-field, missing-return
function RatioConstraint:ratio(num) end
---Set constraint aspect ratio.
---@param numerator number Aspect ratio numerator (absolute value is taken).
---@param denominator number Aspect ratio denominator (absolute value is taken).
---@return NLay.RatioConstraint
---@diagnostic disable-next-line: duplicate-set-field
function RatioConstraint:ratio(numerator, denominator)
local den = math.abs(denominator or 1)
assert(den > 0, "divide by zero")
self.numerator = math.abs(numerator)
self.denominator = den
invalidateCache(self)
return self
end
---Set the constraint bias. By default, the position are centered around (bias 0.5).
---@param horz number|nil Horizontal bias, real value between 0..1 inclusive.
---@param vert number|nil Vertical bias, real value between 0..1 inclusive.
---@param unclamped? boolean Do not limit bias ratio?
---@return NLay.RatioConstraint
function RatioConstraint:bias(horz, vert, unclamped)
if horz then
if unclamped then
self.biasHorz = horz
else
self.biasHorz = math.min(math.max(horz, 0), 1)
end
end
if vert then
if unclamped then
self.biasVert = vert
else
self.biasVert = math.min(math.max(vert, 0), 1)
end
end
invalidateCache(self)
return self
end
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function RatioConstraint:get(offx, offy)
local x, y, w, h = self.parent:get()
local rx, ry, rw, rh, scale
-- TODO: Reduce division operation?
if self.expand then
scale = math.max(w / self.numerator, h / self.denominator)
else
scale = math.min(w / self.numerator, h / self.denominator)
end
rw = self.numerator * scale
rh = self.denominator * scale
rx = mix(0, w - rw, self.biasHorz)
ry = mix(0, h - rh, self.biasVert)
return x + rx + (offx or 0), y + ry + (offy or 0), rw, rh
end
---@class NLay.FloatingConstraint: NLay.BaseConstraint
---@field private x number
---@field private y number
---@field private w number
---@field private h number
local FloatingConstraint = dupmethods(BaseConstraint)
---@private
FloatingConstraint.__index = FloatingConstraint ---@diagnostic disable-line: inject-field
FloatingConstraint._NLay_type_ = "NLay.FloatingConstraint"
---Set floating constraint position.
---@param x? number Floating constraint X position
---@param y? number Floating constraint Y position
---@return NLay.FloatingConstraint
function FloatingConstraint:pos(x, y)
x = x or self.x
y = y or self.y
local diff = x ~= self.x or y ~= self.y
self.x = x
self.y = y
if diff then
invalidateCache(self)
end
return self
end
---Set floating constraint size.
---@param w? number Floating constraint width (absolute value is taken)
---@param h? number Floating constraint height (absolute value is taken)
---@return NLay.FloatingConstraint
function FloatingConstraint:size(w, h)
w = math.abs(w or self.w)
h = math.abs(h or self.h)
local diff = w ~= self.w or h ~= self.h
self.w = w
self.h = h
if diff then
invalidateCache(self)
end
return self
end
---Set floating constraint position and dimensions
---@param x? number Floating constraint X position
---@param y? number Floating constraint Y position
---@param w? number Floating constraint width (absolute value is taken)
---@param h? number Floating constraint height (absolute value is taken)
function FloatingConstraint:update(x, y, w, h)
self:size(w, h)
return self:pos(x, y)
end
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function FloatingConstraint:get(offx, offy)
return self.x + (offx or 0), self.y + (offy or 0), self.w, self.h
end
---@class NLay.ForeignConstraint: NLay.BaseConstraint
---@field private getter {get:fun(self:any):(number,number,number,number)}
---@field private manualupdate boolean
local ForeignConstraint = dupmethods(BaseConstraint)
---@private
ForeignConstraint.__index = ForeignConstraint ---@diagnostic disable-line: inject-field
ForeignConstraint._NLay_type_ = "NLay.ForeignConstraint"
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function ForeignConstraint:get(offx, offy)
if not self.manualupdate then
invalidateCache(self)
end
local x, y, w, h = self.getter:get()
return x + (offx or 0), y + (offy + 0), w, h
end
---@class NLay.SelectableConstraint: NLay.BaseConstraint
---@field package constraints NLay.BaseConstraint[]
---@field private selectedIndex integer
local SelectableConstraint = dupmethods(BaseConstraint)
---@private
SelectableConstraint.__index = SelectableConstraint ---@diagnostic disable-line: inject-field
SelectableConstraint._NLay_type_ = "NLay.SelectableConstraint"
---Select active constraint by specified index.
---@param i integer Constraint index.
function SelectableConstraint:index(i)
assert(i > 0 and i <= #self.constraints, "index out of range")
self.selectedIndex = i
invalidateCache(self)
return self
end
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function SelectableConstraint:get(offx, offy)
return self.constraints[self.selectedIndex]:get(offx, offy)
end
---@class NLay.TransposedConstraint: NLay.BaseConstraint
---@field private parent NLay.BaseConstraint
local TransposedConstraint = dupmethods(BaseConstraint)
---@private
TransposedConstraint.__index = TransposedConstraint ---@diagnostic disable-line: inject-field
TransposedConstraint._NLay_type_ = "NLay.TransposedConstraint"
---Compute and retrieve the top-left and the dimensions of layout.
---@param offx? number X offset (default to 0)
---@param offy? number Y offset (default to 0)
---@return number,number,number,number @Position (x, y) and dimensions (width, height) of the constraint.
function TransposedConstraint:get(offx, offy)
local x, y, w, h = self.parent:get(offx, offy)
return x, y, h, w -- note the order
end
---This class is used to mark to consider the inner border of a constraint instead of the outer.
---@class NLay.Into
---@field public value NLay.BaseConstraint
---@field public _NLay_type_ string
local Into = {_NLay_type_ = "NLay.Into"}
---@private