/
SecureUIPanelTemplates.lua
487 lines (419 loc) · 14.3 KB
/
SecureUIPanelTemplates.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
---------------
--NOTE - Please do not change this section without understanding the full implications of the secure environment
--We usually don't want to call out of this environment from this file. Calls should usually go through Outbound
local _, tbl = ...;
if tbl then
tbl.SecureCapsuleGet = SecureCapsuleGet;
local function Import(name)
tbl[name] = tbl.SecureCapsuleGet(name);
end
Import("IsOnGlueScreen");
if ( tbl.IsOnGlueScreen() ) then
tbl._G = _G; --Allow us to explicitly access the global environment at the glue screens
Import("C_StoreGlue");
end
setfenv(1, tbl);
Import("math");
Import("PlaySound");
Import("SOUNDKIT");
end
----------------
function EditBox_OnTabPressed(self)
if ( self.previousEditBox and IsShiftKeyDown() ) then
self.previousEditBox:SetFocus();
elseif ( self.nextEditBox ) then
self.nextEditBox:SetFocus();
end
end
function EditBox_ClearFocus(self)
self:ClearFocus();
end
function EditBox_HighlightText(self)
self:HighlightText();
end
function EditBox_ClearHighlight(self)
self:HighlightText(0, 0);
end
function UIPanelScrollBarScrollUpButton_OnClick(self)
local parent = self:GetParent();
local scrollStep = self:GetParent().scrollStep or (parent:GetHeight() / 2);
parent:SetValue(parent:GetValue() - scrollStep);
PlaySound(SOUNDKIT.U_CHAT_SCROLL_BUTTON);
end
function UIPanelScrollBarScrollDownButton_OnClick(self)
local parent = self:GetParent();
local scrollStep = self:GetParent().scrollStep or (parent:GetHeight() / 2);
parent:SetValue(parent:GetValue() + scrollStep);
PlaySound(SOUNDKIT.U_CHAT_SCROLL_BUTTON);
end
function UIPanelScrollBar_OnValueChanged(self, value)
self:GetParent():SetVerticalScroll(value);
end
function ScrollFrame_OnLoad(self)
local scrollbar = self.ScrollBar or _G[self:GetName().."ScrollBar"];
scrollbar:SetMinMaxValues(0, 0);
scrollbar:SetValue(0);
self.offset = 0;
local scrollDownButton = scrollbar.ScrollDownButton or _G[scrollbar:GetName().."ScrollDownButton"];
local scrollUpButton = scrollbar.ScrollUpButton or _G[scrollbar:GetName().."ScrollUpButton"];
scrollDownButton:Disable();
scrollUpButton:Disable();
if ( self.scrollBarHideable ) then
scrollbar:Hide();
scrollDownButton:Hide();
scrollUpButton:Hide();
else
scrollDownButton:Disable();
scrollUpButton:Disable();
scrollDownButton:Show();
scrollUpButton:Show();
end
if ( self.noScrollThumb ) then
(scrollbar.ThumbTexture or _G[scrollbar:GetName().."ThumbTexture"]):Hide();
end
end
function ScrollFrameTemplate_OnMouseWheel(self, value, scrollBar)
scrollBar = scrollBar or self.ScrollBar or _G[self:GetName() .. "ScrollBar"];
local scrollStep = scrollBar.scrollStep or scrollBar:GetHeight() / 2
if ( value > 0 ) then
scrollBar:SetValue(scrollBar:GetValue() - scrollStep);
else
scrollBar:SetValue(scrollBar:GetValue() + scrollStep);
end
end
function ScrollFrame_OnScrollRangeChanged(self, xrange, yrange)
local name = self:GetName();
local scrollbar = self.ScrollBar or _G[name.."ScrollBar"];
if ( not yrange ) then
yrange = self:GetVerticalScrollRange();
end
-- Accounting for very small ranges
yrange = math.floor(yrange);
local value = math.min(scrollbar:GetValue(), yrange);
scrollbar:SetMinMaxValues(0, yrange);
scrollbar:SetValue(value);
local scrollDownButton = scrollbar.ScrollDownButton or _G[scrollbar:GetName().."ScrollDownButton"];
local scrollUpButton = scrollbar.ScrollUpButton or _G[scrollbar:GetName().."ScrollUpButton"];
local thumbTexture = scrollbar.ThumbTexture or _G[scrollbar:GetName().."ThumbTexture"];
if ( yrange == 0 ) then
if ( self.scrollBarHideable ) then
scrollbar:Hide();
scrollDownButton:Hide();
scrollUpButton:Hide();
thumbTexture:Hide();
else
scrollDownButton:Disable();
scrollUpButton:Disable();
scrollDownButton:Show();
scrollUpButton:Show();
if ( not self.noScrollThumb ) then
thumbTexture:Show();
end
end
else
scrollDownButton:Show();
scrollUpButton:Show();
scrollbar:Show();
if ( not self.noScrollThumb ) then
thumbTexture:Show();
end
-- The 0.005 is to account for precision errors
if ( yrange - value > 0.005 ) then
scrollDownButton:Enable();
else
scrollDownButton:Disable();
end
end
-- Hide/show scrollframe borders
local top = self.Top or name and _G[name.."Top"];
local bottom = self.Bottom or name and _G[name.."Bottom"];
local middle = self.Middle or name and _G[name.."Middle"];
if ( top and bottom and self.scrollBarHideable ) then
if ( self:GetVerticalScrollRange() == 0 ) then
top:Hide();
bottom:Hide();
else
top:Show();
bottom:Show();
end
end
if ( middle and self.scrollBarHideable ) then
if ( self:GetVerticalScrollRange() == 0 ) then
middle:Hide();
else
middle:Show();
end
end
end
function ScrollFrame_OnVerticalScroll(self, offset)
local scrollbar = self.ScrollBar or _G[self:GetName().."ScrollBar"];
scrollbar:SetValue(offset);
local min, max = scrollbar:GetMinMaxValues();
(scrollbar.ScrollUpButton or _G[scrollbar:GetName().."ScrollUpButton"]):SetEnabled(offset ~= 0);
(scrollbar.ScrollDownButton or _G[scrollbar:GetName().."ScrollDownButton"]):SetEnabled((scrollbar:GetValue() - max) ~= 0);
end
function ScrollFrame_SetScrollOffset(self, scrollOffset)
local scrollbar = self.ScrollBar or _G[self:GetName().."ScrollBar"];
scrollbar:SetValue(scrollOffset);
end
function ScrollingEdit_OnTextChanged(self, scrollFrame)
-- force an update when the text changes
self.handleCursorChange = true;
ScrollingEdit_OnUpdate(self, 0, scrollFrame);
end
function ScrollingEdit_OnLoad(self)
ScrollingEdit_SetCursorOffsets(self, 0, 0);
end
function ScrollingEdit_SetCursorOffsets(self, offset, height)
self.cursorOffset = offset;
self.cursorHeight = height;
end
function ScrollingEdit_OnCursorChanged(self, x, y, w, h)
ScrollingEdit_SetCursorOffsets(self, y, h);
self.handleCursorChange = true;
end
-- NOTE: If your edit box never shows partial lines of text, then this function will not work when you use
-- your mouse to move the edit cursor. You need the edit box to cut lines of text so that you can use your
-- mouse to highlight those partially-seen lines; otherwise you won't be able to use the mouse to move the
-- cursor above or below the current scroll area of the edit box.
function ScrollingEdit_OnUpdate(self, elapsed, scrollFrame)
local height, range, scroll, size, cursorOffset;
if ( self.handleCursorChange ) then
if ( not scrollFrame ) then
scrollFrame = self:GetParent();
end
height = scrollFrame:GetHeight();
range = scrollFrame:GetVerticalScrollRange();
scroll = scrollFrame:GetVerticalScroll();
size = height + range;
cursorOffset = -self.cursorOffset;
if ( math.floor(height) <= 0 or math.floor(range) <= 0 ) then
--Frame has no area, nothing to calculate.
return;
end
while ( cursorOffset < scroll ) do
scroll = (scroll - (height / 2));
if ( scroll < 0 ) then
scroll = 0;
end
scrollFrame:SetVerticalScroll(scroll);
end
while ( (cursorOffset + self.cursorHeight) > (scroll + height) and scroll < range ) do
scroll = (scroll + (height / 2));
if ( scroll > range ) then
scroll = range;
end
scrollFrame:SetVerticalScroll(scroll);
end
self.handleCursorChange = false;
end
end
function FauxScrollFrame_GetChildFrames(frame)
local frameName = frame:GetName();
if frameName then
return _G[ frameName.."ScrollBar" ], _G[ frameName.."ScrollChildFrame" ], _G[ frameName.."ScrollBarScrollUpButton" ], _G[ frameName.."ScrollBarScrollDownButton" ];
else
return frame.ScrollBar, frame.ScrollChildFrame, frame.ScrollBar.ScrollUpButton, frame.ScrollBar.ScrollDownButton;
end
end
function FauxScrollFrame_Update(frame, numItems, numToDisplay, buttonHeight, button, smallWidth, bigWidth, highlightFrame, smallHighlightWidth, bigHighlightWidth, alwaysShowScrollBar)
local scrollBar, scrollChildFrame, scrollUpButton, scrollDownButton = FauxScrollFrame_GetChildFrames(frame);
-- If more than one screen full of items then show the scrollbar
local showScrollBar;
if ( numItems > numToDisplay or alwaysShowScrollBar ) then
frame:Show();
showScrollBar = 1;
else
scrollBar:SetValue(0);
frame:Hide();
end
if ( frame:IsShown() ) then
local scrollFrameHeight = 0;
local scrollChildHeight = 0;
if ( numItems > 0 ) then
scrollFrameHeight = (numItems - numToDisplay) * buttonHeight;
scrollChildHeight = numItems * buttonHeight;
if ( scrollFrameHeight < 0 ) then
scrollFrameHeight = 0;
end
scrollChildFrame:Show();
else
scrollChildFrame:Hide();
end
local maxRange = (numItems - numToDisplay) * buttonHeight;
if (maxRange < 0) then
maxRange = 0;
end
scrollBar:SetMinMaxValues(0, maxRange);
scrollBar:SetValueStep(buttonHeight);
scrollBar:SetStepsPerPage(numToDisplay-1);
scrollChildFrame:SetHeight(scrollChildHeight);
-- Arrow button handling
if ( scrollBar:GetValue() == 0 ) then
scrollUpButton:Disable();
else
scrollUpButton:Enable();
end
if ((scrollBar:GetValue() - scrollFrameHeight) == 0) then
scrollDownButton:Disable();
else
scrollDownButton:Enable();
end
-- Shrink because scrollbar is shown
if ( highlightFrame ) then
highlightFrame:SetWidth(smallHighlightWidth);
end
if ( button ) then
for i=1, numToDisplay do
_G[button..i]:SetWidth(smallWidth);
end
end
else
-- Widen because scrollbar is hidden
if ( highlightFrame ) then
highlightFrame:SetWidth(bigHighlightWidth);
end
if ( button ) then
for i=1, numToDisplay do
_G[button..i]:SetWidth(bigWidth);
end
end
end
return showScrollBar;
end
function FauxScrollFrame_OnVerticalScroll(self, value, itemHeight, updateFunction)
local scrollbar = FauxScrollFrame_GetChildFrames(self);
scrollbar:SetValue(value);
self.offset = math.floor((value / itemHeight) + 0.5);
if ( updateFunction ) then
updateFunction(self);
end
end
function FauxScrollFrame_GetOffset(frame)
return frame.offset or 0;
end
function FauxScrollFrame_SetOffset(frame, offset)
frame.offset = offset;
end
function InputScrollFrame_OnLoad(self)
local scrollBar = self.ScrollBar;
scrollBar:ClearAllPoints();
scrollBar:SetPoint("TOPLEFT", self, "TOPRIGHT", -13, -11);
scrollBar:SetPoint("BOTTOMLEFT", self, "BOTTOMRIGHT", -13, 9);
-- reposition the up and down buttons
self.ScrollBar.ScrollDownButton:SetPoint("TOP", scrollBar, "BOTTOM", 0, 4);
self.ScrollBar.ScrollUpButton:SetPoint("BOTTOM", scrollBar, "TOP", 0, -4);
-- make the scroll bar hideable and force it to start off hidden so positioning calculations can be done
-- as soon as it needs to be shown
self.scrollBarHideable = 1;
scrollBar:Hide();
self.EditBox:SetWidth(self:GetWidth() - 18);
self.EditBox:SetMaxLetters(self.maxLetters);
self.EditBox.Instructions:SetText(self.instructions);
self.EditBox.Instructions:SetWidth(self:GetWidth());
self.CharCount:SetShown(not self.hideCharCount);
end
function InputScrollFrame_OnMouseDown(self)
self.EditBox:SetFocus();
end
InputScrollFrame_OnTabPressed = EditBox_OnTabPressed;
function InputScrollFrame_OnTextChanged(self)
local scrollFrame = self:GetParent();
ScrollingEdit_OnTextChanged(self, scrollFrame);
if ( self:GetText() ~= "" ) then
self.Instructions:Hide();
else
self.Instructions:Show();
end
scrollFrame.CharCount:SetText(self:GetMaxLetters() - self:GetNumLetters());
if ( scrollFrame.ScrollBar:IsShown() ) then
scrollFrame.CharCount:SetPoint("BOTTOMRIGHT", -17, 0);
else
scrollFrame.CharCount:SetPoint("BOTTOMRIGHT", 0, 0);
end
end
function InputScrollFrame_OnUpdate(self, elapsed)
ScrollingEdit_OnUpdate(self, elapsed, self:GetParent());
end
function InputScrollFrame_OnEscapePressed(self)
self:ClearFocus();
end
function UIPanelButton_OnLoad(self)
if ( not self:IsEnabled() ) then
self.Left:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
self.Middle:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
self.Right:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
end
end
function UIPanelButton_OnMouseDown(self)
if ( self:IsEnabled() ) then
self.Left:SetTexture("Interface\\Buttons\\UI-Panel-Button-Down");
self.Middle:SetTexture("Interface\\Buttons\\UI-Panel-Button-Down");
self.Right:SetTexture("Interface\\Buttons\\UI-Panel-Button-Down");
end
end
function UIPanelButton_OnMouseUp(self)
if ( self:IsEnabled() ) then
self.Left:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
self.Middle:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
self.Right:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
end
end
function UIPanelButton_OnShow(self)
if ( self:IsEnabled() ) then
-- we need to reset our textures just in case we were hidden before a mouse up fired
self.Left:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
self.Middle:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
self.Right:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
end
end
function UIPanelButton_OnDisable(self)
self.Left:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
self.Middle:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
self.Right:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
end
function UIPanelButton_OnEnable(self)
self.Left:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
self.Middle:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
self.Right:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
end
UIButtonFitToTextBehaviorMixin = {};
function UIButtonFitToTextBehaviorMixin:SetTextToFit(text)
self:SetText(text);
self:FitToText();
end
function UIButtonFitToTextBehaviorMixin:FitToText()
local minWidth = self.fitTextCanWidthDecrease and 0 or self:GetWidth();
self:SetWidth(math.max(minWidth, self:GetTextWidth() + self.fitTextWidthPadding));
end
UIPanelButtonNoTooltipResizeToFitMixin = {};
function UIPanelButtonNoTooltipResizeToFitMixin:OnLoad()
UIPanelButton_OnLoad(self);
self.Text.layoutIndex = 1;
self:MarkDirty();
end
function UIPanelButtonNoTooltipResizeToFitMixin:SetText(text)
self.Text:SetText(text);
self:MarkDirty();
end
function SelectionFrameCancelButton_OnClick(self, ...)
PlaySound(SOUNDKIT.GS_TITLE_OPTION_OK);
local cancelFunction = self:GetParent().OnCancel;
if cancelFunction then
cancelFunction(self, ...);
end
end
function SelectionFrameOkayButton_OnClick(self, ...)
PlaySound(SOUNDKIT.GS_TITLE_OPTION_OK);
local okayFunction = self:GetParent().OnOkay;
if okayFunction then
okayFunction(self, ...);
end
end
LoadingSpinnerMixin = {};
function LoadingSpinnerMixin:OnShow()
self.Anim:Play();
end
function LoadingSpinnerMixin:OnHide()
self.Anim:Stop();
end