-
Notifications
You must be signed in to change notification settings - Fork 26
/
StdUi.lua
297 lines (249 loc) · 6.94 KB
/
StdUi.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
local MAJOR, MINOR = 'StdUi', 5;
--- @class StdUi
local StdUi = LibStub:NewLibrary(MAJOR, MINOR);
if not StdUi then
return
end
local TableInsert = tinsert;
StdUi.moduleVersions = {};
if not StdUiInstances then
StdUiInstances = {StdUi};
else
TableInsert(StdUiInstances, StdUi);
end
function StdUi:NewInstance()
local instance = CopyTable(self);
instance:ResetConfig();
TableInsert(StdUiInstances, instance);
return instance;
end
function StdUi:RegisterModule(module, version)
self.moduleVersions[module] = version;
end
function StdUi:UpgradeNeeded(module, version)
if not self.moduleVersions[module] then
return true;
end
return self.moduleVersions[module] < version;
end
function StdUi:RegisterWidget(name, func)
if not self[name] then
self[name] = func;
return true;
end
return false;
end
function StdUi:InitWidget(widget)
widget.isWidget = true;
function widget:GetChildrenWidgets()
local children = {widget:GetChildren()};
local result = {};
for i = 1, #children do
local child = children[i];
if child.isWidget then
TableInsert(result, child);
end
end
return result;
end
end
function StdUi:SetObjSize(obj, width, height)
if width then
obj:SetWidth(width);
end
if height then
obj:SetHeight(height);
end
end
function StdUi:SetTextColor(fontString, colorType)
colorType = colorType or 'normal';
if fontString.SetTextColor then
local c = self.config.font.color[colorType];
fontString:SetTextColor(c.r, c.g, c.b, c.a);
end
end
StdUi.SetHighlightBorder = function(self)
if self.target then
self = self.target;
end
if self.isDisabled then
return
end
local hc = self.stdUi.config.highlight.color;
if not self.origBackdropBorderColor then
self.origBackdropBorderColor = {self:GetBackdropBorderColor()};
end
self:SetBackdropBorderColor(hc.r, hc.g, hc.b, 1);
end
StdUi.ResetHighlightBorder = function(self)
if self.target then
self = self.target;
end
if self.isDisabled then
return
end
local hc = self.origBackdropBorderColor;
if hc then
self:SetBackdropBorderColor(unpack(hc));
end
end
function StdUi:HookHoverBorder(object)
if not object.SetBackdrop then
Mixin(object, BackdropTemplateMixin)
end
object:HookScript('OnEnter', self.SetHighlightBorder);
object:HookScript('OnLeave', self.ResetHighlightBorder);
end
function StdUi:ApplyBackdrop(frame, type, border, insets)
local config = frame.config or self.config;
local backdrop = {
bgFile = config.backdrop.texture,
edgeFile = config.backdrop.texture,
edgeSize = 1,
};
if insets then
backdrop.insets = insets;
end
if not frame.SetBackdrop then
Mixin(frame, BackdropTemplateMixin)
end
frame:SetBackdrop(backdrop);
type = type or 'button';
border = border or 'border';
if config.backdrop[type] then
frame:SetBackdropColor(
config.backdrop[type].r,
config.backdrop[type].g,
config.backdrop[type].b,
config.backdrop[type].a
);
end
if config.backdrop[border] then
frame:SetBackdropBorderColor(
config.backdrop[border].r,
config.backdrop[border].g,
config.backdrop[border].b,
config.backdrop[border].a
);
end
end
function StdUi:ClearBackdrop(frame)
if not frame.SetBackdrop then
Mixin(frame, BackdropTemplateMixin)
end
frame:SetBackdrop(nil);
end
function StdUi:ApplyDisabledBackdrop(frame, enabled)
if frame.target then
frame = frame.target;
end
if enabled then
self:ApplyBackdrop(frame, 'button', 'border');
self:SetTextColor(frame, 'normal');
if frame.label then
self:SetTextColor(frame.label, 'normal');
end
if frame.text then
self:SetTextColor(frame.text, 'normal');
end
frame.isDisabled = false;
else
self:ApplyBackdrop(frame, 'buttonDisabled', 'borderDisabled');
self:SetTextColor(frame, 'disabled');
if frame.label then
self:SetTextColor(frame.label, 'disabled');
end
if frame.text then
self:SetTextColor(frame.text, 'disabled');
end
frame.isDisabled = true;
end
end
function StdUi:HookDisabledBackdrop(frame)
local this = self;
hooksecurefunc(frame, 'Disable', function(self)
this:ApplyDisabledBackdrop(self, false);
end);
hooksecurefunc(frame, 'Enable', function(self)
this:ApplyDisabledBackdrop(self, true);
end);
end
function StdUi:StripTextures(frame)
for i = 1, frame:GetNumRegions() do
local region = select(i, frame:GetRegions());
if region and region:GetObjectType() == 'Texture' then
region:SetTexture(nil);
end
end
end
function StdUi:MakeDraggable(frame, handle)
frame:SetMovable(true);
frame:EnableMouse(true);
frame:RegisterForDrag('LeftButton');
frame:SetScript('OnDragStart', frame.StartMoving);
frame:SetScript('OnDragStop', frame.StopMovingOrSizing);
if handle then
handle:EnableMouse(true);
handle:SetMovable(true);
handle:RegisterForDrag('LeftButton');
handle:SetScript('OnDragStart', function(self)
frame.StartMoving(frame);
end);
handle:SetScript('OnDragStop', function(self)
frame.StopMovingOrSizing(frame);
end);
end
end
-- Make a frame resizable
function StdUi:MakeResizable(frame, direction)
-- Possible resize directions and handle rotation values
local anchorDirections = {
["TOP"] = 0,
["TOPRIGHT"] = 1.5708,
["RIGHT"] = 0,
["BOTTOMRIGHT"] = 0,
["BOTTOM"] = 0,
["BOTTOMLEFT"] = -1.5708,
["LEFT"] = 0,
["TOPLEFT"] = 3.1416,
}
direction = string.upper(direction);
-- Return if invalid direction
if not anchorDirections[direction] then return false end
frame:SetResizable(true);
-- Create the resize anchor
local anchor = CreateFrame("Button", nil, frame);
anchor:SetPoint(direction, frame, direction);
-- Attach side anchor to adjacent sides of frame
if direction == "TOP" or direction == "BOTTOM" then
anchor:SetHeight(self.config.resizeHandle.height);
anchor:SetPoint("LEFT", frame, "LEFT", self.config.resizeHandle.width, 0);
anchor:SetPoint("RIGHT", frame, "RIGHT", self.config.resizeHandle.width*-1, 0);
elseif direction == "LEFT" or direction == "RIGHT" then
anchor:SetWidth(self.config.resizeHandle.width);
anchor:SetPoint("TOP", frame, "TOP", 0, self.config.resizeHandle.height*-1);
anchor:SetPoint("BOTTOM", frame, "BOTTOM", 0, self.config.resizeHandle.height);
else
-- Set the corner anchor textures
anchor:SetNormalTexture(self.config.resizeHandle.texture.normal);
anchor:SetHighlightTexture(self.config.resizeHandle.texture.highlight);
anchor:SetPushedTexture(self.config.resizeHandle.texture.pushed);
-- Set size and rotate corner anchor
anchor:SetSize(self.config.resizeHandle.width, self.config.resizeHandle.height);
anchor:GetNormalTexture():SetRotation(anchorDirections[direction]);
anchor:GetHighlightTexture():SetRotation(anchorDirections[direction]);
anchor:GetPushedTexture():SetRotation(anchorDirections[direction]);
end
-- Resize anchor click handlers
anchor:SetScript("OnMouseDown", function(self, button)
if button == "LeftButton" then
frame:StartSizing(direction);
frame:SetUserPlaced(true);
end
end)
anchor:SetScript("OnMouseUp", function(self, button)
if button == "LeftButton" then
frame:StopMovingOrSizing();
end
end)
end