-
Notifications
You must be signed in to change notification settings - Fork 26
/
StdUiLayout.lua
211 lines (168 loc) · 4.52 KB
/
StdUiLayout.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
--- @type StdUi
local StdUi = LibStub and LibStub('StdUi', true);
if not StdUi then
return
end
local module, version = 'Layout', 3;
if not StdUi:UpgradeNeeded(module, version) then
return
end
local TableInsert = tinsert;
local TableRemove = tremove;
local pairs = pairs;
local MathMax = math.max;
local MathFloor = math.floor;
local defaultLayoutConfig = {
gutter = 10,
columns = 12,
padding = {
top = 0,
right = 10,
left = 10,
bottom = 10,
}
};
local defaultRowConfig = {
margin = {
top = 0,
right = 0,
bottom = 15,
left = 0
}
};
local defaultElementConfig = {
margin = {
top = 0,
right = 0,
bottom = 0,
left = 0
}
};
local EasyLayoutRow = {
AddElement = function(self, frame, config)
if not frame.layoutConfig then
frame.layoutConfig = StdUi.Util.tableMerge(defaultElementConfig, config or {});
elseif config then
frame.layoutConfig = StdUi.Util.tableMerge(frame.layoutConfig, config or {});
end
TableInsert(self.elements, frame);
end,
AddElements = function(self, ...)
local r = { ... };
local cfg = TableRemove(r, #r);
if cfg.column == 'even' then
cfg.column = MathFloor(self.parent.layout.columns / #r);
end
for i = 1, #r do
self:AddElement(r[i], StdUi.Util.tableMerge(defaultElementConfig, cfg));
end
end,
GetColumnsTaken = function(self)
local columnsTaken = 0;
local l = self.parent.layout;
for i = 1, #self.elements do
local lc = self.elements[i].layoutConfig;
local col = lc.column or l.columns;
columnsTaken = columnsTaken + col;
end
return columnsTaken;
end,
DrawRow = function(self, parentWidth, yOffset)
yOffset = yOffset or 0;
local l = self.parent.layout;
local g = l.gutter;
local rowMargin = self.config.margin;
local totalHeight = 0;
local columnsTaken = 0;
local x = g + l.padding.left + rowMargin.left;
-- if row has margins, cut down available width
parentWidth = parentWidth - rowMargin.left - rowMargin.right;
for i = 1, #self.elements do
local frame = self.elements[i];
frame:ClearAllPoints();
-- Frame layout config
local lc = frame.layoutConfig;
local m = lc.margin;
-- take full size
if lc.fullSize then
StdUi:GlueAcross(
frame,
self.parent,
l.padding.left,
-l.padding.top,
-l.padding.right,
l.padding.bottom
);
if frame.DoLayout then
frame:DoLayout();
end
totalHeight = MathMax(totalHeight, frame:GetHeight() + m.bottom + m.top + rowMargin.top + rowMargin.bottom);
return totalHeight;
end
local col = lc.column or l.columns;
local w = (parentWidth / (l.columns / col)) - 2 * g;
frame:SetWidth(w);
if columnsTaken + col > self.parent.layout.columns then
print('Element will not fit row capacity: ' .. l.columns);
return totalHeight;
end
-- move it down by rowMargin and element margin
frame:SetPoint('TOPLEFT', self.parent, 'TOPLEFT', x, yOffset - m.top - rowMargin.top);
if lc.fullHeight then
frame:SetPoint('BOTTOMLEFT', self.parent, 'BOTTOMLEFT', x, m.bottom + rowMargin.bottom);
end
--each element takes 1 gutter plus column * colWidth, while gutter is inclusive
x = x + w + 2 * g; -- double the gutter because width subtracts gutter
-- if that frame is container itself, do layout for it too
if frame.DoLayout then
frame:DoLayout();
end
totalHeight = MathMax(totalHeight, frame:GetHeight() + m.bottom + m.top + rowMargin.top + rowMargin.bottom);
columnsTaken = columnsTaken + col;
end
return totalHeight;
end
}
---EasyLayoutRow
---@param parent Frame
---@param config table
function StdUi:EasyLayoutRow(parent, config)
---@class EasyLayoutRow
local row = {
parent = parent,
config = self.Util.tableMerge(defaultRowConfig, config or {}),
elements = {}
};
for k, v in pairs(EasyLayoutRow) do
row[k] = v;
end
return row;
end
local EasyLayout = {
---@return EasyLayoutRow
AddRow = function(self, config)
if not self.rows then
self.rows = {};
end
local row = self.stdUi:EasyLayoutRow(self, config);
TableInsert(self.rows, row);
return row;
end,
DoLayout = function(self)
local l = self.layout;
local width = self:GetWidth() - l.padding.left - l.padding.right;
local y = -l.padding.top;
for i = 1, #self.rows do
local row = self.rows[i];
y = y - row:DrawRow(width, y);
end
end
};
function StdUi:EasyLayout(parent, config)
parent.stdUi = self;
parent.layout = self.Util.tableMerge(defaultLayoutConfig, config or {});
for k, v in pairs(EasyLayout) do
parent[k] = v;
end
end
StdUi:RegisterModule(module, version);