-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhStack.lua
More file actions
185 lines (152 loc) · 5.19 KB
/
hStack.lua
File metadata and controls
185 lines (152 loc) · 5.19 KB
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
if not gui then
gui = {}
end
gui.hstack = class('gui.hstack')
function gui.hstack:created(padding, spacing, stretchChildren)
self.padding = {left = 10, right = 10, top = 10, bottom = 10}
if padding then
self:setPadding(padding)
end
self.spacing = spacing or 5
self.stretchChildren = stretchChildren or false
self.oldSize = nil
self.oldNum = nil
end
function gui.hstack:start()
self:stretchItems()
end
function gui.hstack:setPadding(padding)
if type(padding) == "number" or padding.x ~= nil then
if type(padding) == "number" then
self.padding.left = padding
self.padding.right = padding
self.padding.top = padding
self.padding.bottom = padding
else
self.padding.left = padding.x
self.padding.right = padding.x
self.padding.top = padding.y
self.padding.bottom = padding.y
end
else
self.padding = padding
end
end
function gui.hstack:stretchItems()
local e = self.entity
local sp = self.spacing
local stretchSize = self.entity.size.x
local numberOfStretchChildren = 0
local numActiveChildren = 0
for i = 1, e.childCount do
local child = e:childAt(i)
if child.active and child.fakeParent == nil then
if child.stretch then
numberOfStretchChildren = numberOfStretchChildren + 1
stretchSize = stretchSize
else
stretchSize = stretchSize - child.size.x
end
numActiveChildren = numActiveChildren + 1
end
end
stretchSize = stretchSize - (self.padding.left + self.padding.right) - ((numActiveChildren - 1) * sp)
for i = 1, e.childCount do
local child = e:childAt(i)
if child.active and child.fakeParent == nil then
if child.stretch and child.fakeParent == nil then
child.size = vec2(stretchSize / numberOfStretchChildren, e.size.y - (self.padding.top + self.padding.bottom))
else
child.size = vec2(child.size.x, e.size.y - (self.padding.top + self.padding.bottom))
end
end
end
e.shouldUpdateStack = nil
end
function gui.hstack:checkActiveCount()
local e = self.entity
local num = 0
for i = 1, e.childCount do
local child = e:childAt(i)
if child.active and child.fakeParent == nil then
num = num + 1
end
end
return num
end
function gui.hstack:checkSize()
if not self.stretchChildren then
local e = self.entity
local pd = self.padding
local sp = self.spacing
local size = e.size
size.x = self.padding.left
for i = 1, e.childCount do
local child = e:childAt(i)
if child.active and child.fakeParent == nil then
size.x = size.x + child.size.x + ((i < e.childCount) and sp or 0)
end
end
size.x = size.x + self.padding.right
e.size = size
end
end
function gui.hstack:layout()
local e = self.entity
local padding = self.padding or { left = 0, right = 0, top = 0, bottom = 0 }
local left, right, top, bottom = padding.left, padding.right, padding.top, padding.bottom
local spacing = self.spacing or 0
local height = math.max(0, e.size.y - top - bottom)
local childCount = e.childCount
-- Initialize cached values if not set
self.oldSize = self.oldSize or e.size.x
self.oldNum = self.oldNum or childCount
self.activeCount = self.activeCount or 0
-- Check if layout needs updating
local firstChild = childCount > 0 and e:childAt(1)
local shouldChange = (
self.oldSize ~= e.size.x or
self.oldNum ~= childCount or
self.activeCount ~= self:checkActiveCount() or
(firstChild and firstChild.active and not firstChild.fakeParent and
(firstChild.x ~= left or firstChild.size.y ~= height)) or e.shouldUpdateStack
)
if not shouldChange then
return self.cachedX or right
end
self:stretchItems()
local x = left
for i = 1, childCount do
local child = e:childAt(i)
if child and child.active and not child.fakeParent then
child.x = x
child.y = (bottom - top) * 0.5
child.pivot = vec2(0, 0.5)
child:anchor(LEFT, STRETCH)
local childWidth = child.size.x
child.size = vec2(childWidth, height)
x = x + childWidth + (i < childCount and spacing or 0)
end
end
x = x + right
-- Update cached state
self.oldSize = e.size.x
self.oldNum = childCount
self.activeCount = self:checkActiveCount()
self.cachedX = x
self:checkSize()
return x
end
function gui.fixStack(enti)
if enti.childCount ~= 0 then
if enti:has(gui.hstack) then
enti:get(gui.hstack):stretchItems()
elseif enti:has(gui.vstack) then
enti:get(gui.vstack):stretchItems()
end
for k, childEnti in ipairs(enti.children) do
gui.fixStack(childEnti)
end
end
end
Profiler.wrapClass(gui.hstack)