-
Notifications
You must be signed in to change notification settings - Fork 1
/
Krutilities.lua
322 lines (261 loc) · 8.11 KB
/
Krutilities.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
do
local _M = { Version = 1.4 };
if Krutilities and Krutilities.Version >= _M.Version then
-- Newer/equal version already loaded.
return;
end
-- [[ Optimization ]] --
local type = type;
local pairs = pairs;
local CreateFrame = CreateFrame;
-- [[ Local Functions ]] --
local Shared_ProcessPoints = function(target, points, parent)
if points then
if type(points) == "string" then
target:SetPoint(points, parent, points, 0, 0);
else
if #points == 0 then
-- Single point.
points.point = points.point or "CENTER";
target:SetPoint(points.point, points.relativeTo or parent, points.relativePoint or points.point, points.x or 0, points.y or 0);
else
-- Many points
for i = 1, #points do
local point = points[i];
point.point = point.point or "CENTER";
target:SetPoint(point.point, point.relativeTo or parent, point.relativePoint or point.point, point.x or 0, point.y or 0);
end
end
end
end
end
local Shared_Sizing = function(target, node)
local width = node.width or nil;
local height = node.height or nil;
if node.size then
if type(node.size) == "table" then
width = node.size[1];
height = node.size[2];
else
width = node.size;
height = node.size;
end
end
if width then target:SetWidth(width); end
if height then target:SetHeight(height); end
end
local Shared_Mixin = function(target, mixin)
if mixin then
for key, value in pairs(mixin) do
target[key] = value;
end
end
end
local Shared_Inject = function(target, parent, injectSelf)
if injectSelf then
parent[injectSelf] = target;
end
end
local Shared_CreateChild = function(createFunc, frame, node)
local new = createFunc(frame, node);
if node.buttonTex then
if node.buttonTex == "PUSHED" then
frame:SetPushedTexture(new);
elseif node.buttonTex == "HIGHLIGHT" then
frame:SetHighlightTexture(new);
end
end
if node.scrollChild then
frame:SetScrollChild(new);
end
end
local Shared_HandleChildren = function(frame, childFunc, node)
if node == nil then
return;
end
local nodeCount = #node;
if nodeCount > 0 then
-- Node contains children, spawn them all.
for i = 1, nodeCount do
Shared_CreateChild(childFunc, frame, node[i]);
end
else
-- No children, treat as a single object.
Shared_CreateChild(childFunc, frame, node);
end
end
-- [[ Global Utility ]] --
-- [[ Clone a table, shallow or deep ]] --
_M.CloneTable = function(input, deep)
local inputType = type(input);
local output;
if inputType == "table" then
output = {};
if deep then -- Deep copy (copy-by-value)
for key, value in next, input, nil do
output[_M.CloneTable(key, true)] = _M.CloneTable(value, true);
end
else -- Shallow copy (copy-by-reference)
for key, value in pairs(input) do
output[key] = value;
end
end
else
output = input;
end
return output;
end
-- [[ Dump an object using Blizzard's debugging tool ]] --
_M.Dump = function(input)
if type(input) ~= "string" then
_M._TEMP = input;
input = "Krutilities._TEMP";
end
SlashCmdList["DUMP"](input);
_M._TEMP = nil;
end
-- [[ Event handler creation utility ]] --
_M.EventHandler = function(addon, events)
local eventFrame = CreateFrame("FRAME");
for eventName, funcName in pairs(events) do
eventFrame:RegisterEvent(eventName);
end
eventFrame:SetScript("OnEvent", function(self, event, ...)
addon[events[event]](...);
end);
return eventFrame;
end
_M.Frame = function(self, node)
assert(type(node) == "table", "Krutilities:Frame called with invalid constructor table.");
Shared_Mixin(node, node.mixin);
if self ~= _M then
node.parent = self;
end
if node.parentName then node.name = "$parent" .. node.parentName; end
if node.parent then
-- Parent cannot be string, attempt a global lookup.
if type(node.parent) == "string" then
node.parent = _G[node.parent];
end
else
-- Default to UIParent.
node.parent = UIParent;
end
local frame = CreateFrame(node.type or "FRAME", node.name, node.parent, node.inherit);
if node.hidden then frame:Hide(); end
if node.enableMouse then frame:EnableMouse(); end
if node.strata then
frame:SetFrameStrata(node.strata);
end
-- Generic stuff.
Shared_Sizing(frame, node);
Shared_Inject(frame, node.parent, node.injectSelf);
-- Anchor points
if node.setAllPoints then frame:SetAllPoints(true); end
if node.points == nil then node.points = { point = "CENTER" }; end
Shared_ProcessPoints(frame, node.points, node.parent);
-- Backdrop
if node.backdrop then frame:SetBackdrop(node.backdrop); end
-- Data
if node.data then
for key, value in pairs(node.data) do
frame[key] = value;
end
end
-- Editbox Stuff
if node.type == "EDITBOX" then
if node.multiLine then frame:SetMultiLine(true); else frame:SetMultiLine(false); end
if node.autoFocus then frame:SetAutoFocus(true); else frame:SetAutoFocus(false); end
end
-- Children
Shared_HandleChildren(frame, _M.Texture, node.textures);
Shared_HandleChildren(frame, _M.Frame, node.frames);
Shared_HandleChildren(frame, _M.Text, node.texts);
-- Scripts
if node.scripts then
for scriptEvent, scriptFunc in pairs(node.scripts) do
if scriptEvent == "OnLoad" then
scriptFunc(frame);
else
frame:SetScript(scriptEvent, scriptFunc);
if not node.hidden and scriptEvent == "OnShow" then
scriptFunc(frame);
end
end
end
end
-- Inject shortcut functions.
frame.SpawnTexture = _M.Texture;
frame.SpawnText = _M.Text;
frame.SpawnFrame = _M.Frame;
return frame;
end
_M.Texture = function(frame, node)
assert(type(node) == "table", "Krutilities:Texture called with invalid constructor table.");
Shared_Mixin(node, node.mixin);
if not node.parent then
node.parent = frame ~= _M and frame or UIParent;
end
if node.parentName then node.name = "$parent" .. node.parentName; end
local tex = node.parent:CreateTexture(node.name, node.layer, node.inherit, node.subLevel or 0);
-- Generic stuff
Shared_Sizing(tex, node);
Shared_Inject(tex, frame, node.injectSelf);
-- Tiling
local tileX = node.tile or node.tileX;
local tileY = node.tile or node.tileY;
tex:SetHorizTile(tileX);
tex:SetVertTile(tileY);
tex:SetTexture(node.texture, tileX, tileY);
-- Anchor points
if node.points == nil and node.setAllPoints ~= false then
node.setAllPoints = true;
end
if node.setAllPoints then tex:SetAllPoints(true); end
Shared_ProcessPoints(tex, node.points, frame);
-- Colour filter
if node.color then
local r = node.color.r or node.color[1] or 0;
local g = node.color.g or node.color[2] or 0;
local b = node.color.b or node.color[3] or 0;
local a = node.color.a or node.color[4] or 1;
tex:SetVertexColor(r, g, b, a);
end
-- Tex coords.
if node.texCoord then
tex:SetTexCoord(node.texCoord[1], node.texCoord[2], node.texCoord[3], node.texCoord[4]);
end
return tex;
end
_M.Text = function(frame, node)
assert(type(node) == "table", "Krutilities:Text called with invalid constructor table.");
Shared_Mixin(node, node.mixin);
if not node.parent then
node.parent = frame ~= _M and frame or UIParent;
end
if node.parentName then node.name = "$parent" .. node.parentName; end
local text = frame:CreateFontString(node.name, node.layer, node.inherit);
-- Generic Stuff
Shared_Sizing(text, node);
Shared_Inject(text, frame, node.injectSelf);
-- Text / Alignment
if node.text then text:SetText(node.text); end
if node.justifyH then text:SetJustifyH(node.justifyH); end
if node.justifyV then text:SetJustifyV(node.justifyV); end
if node.maxLines then text:SetMaxLines(node.maxLines); end
-- Colouring
if node.color then
local r = node.color.r or node.color[1] or 0;
local g = node.color.g or node.color[2] or 0;
local b = node.color.b or node.color[3] or 0;
local a = node.color.a or node.color[4] or 1;
text:SetTextColor(r, g, b, a);
end
-- Anchor points
if node.points == nil then node.points = { point = "CENTER" }; end
Shared_ProcessPoints(text, node.points, frame);
return text;
end
-- Expose module to global scope.
Krutilities = _M;
end