-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathEditBox.lua
390 lines (305 loc) · 9.05 KB
/
EditBox.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
--- @type StdUi
local StdUi = LibStub and LibStub('StdUi', true);
if not StdUi then
return
end
local module, version = 'EditBox', 9;
if not StdUi:UpgradeNeeded(module, version) then return end;
local pairs = pairs;
local strlen = strlen;
----------------------------------------------------
--- SimpleEditBox
----------------------------------------------------
local SimpleEditBoxMethods = {
SetFontSize = function(self, newSize)
self:SetFont(self:GetFont(), newSize, self.stdUi.config.font.effect);
end
};
local SimpleEditBoxEvents = {
OnEscapePressed = function (self)
self:ClearFocus();
end
}
--- @return EditBox
function StdUi:SimpleEditBox(parent, width, height, text)
--- @type EditBox
local editBox = CreateFrame('EditBox', nil, parent);
editBox.stdUi = self;
self:InitWidget(editBox);
editBox:SetTextInsets(3, 3, 3, 3);
editBox:SetFontObject(ChatFontNormal);
editBox:SetAutoFocus(false);
for k, v in pairs(SimpleEditBoxMethods) do
editBox[k] = v;
end
for k, v in pairs(SimpleEditBoxEvents) do
editBox:SetScript(k, v);
end
if text then
editBox:SetText(text);
end
self:HookDisabledBackdrop(editBox);
self:HookHoverBorder(editBox);
self:ApplyBackdrop(editBox);
self:SetObjSize(editBox, width, height);
return editBox;
end
----------------------------------------------------
--- ApplyPlaceholder
----------------------------------------------------
local ApplyPlaceholderOnTextChanged = function(self)
if strlen(self:GetText()) > 0 then
self.placeholder.icon:Hide();
self.placeholder.label:Hide();
else
self.placeholder.icon:Show();
self.placeholder.label:Show();
end
end
function StdUi:ApplyPlaceholder(widget, placeholderText, icon, iconColor)
widget.placeholder = {};
local label = self:Label(widget, placeholderText);
self:SetTextColor(label, 'disabled');
widget.placeholder.label = label;
if icon then
local texture = self:Texture(widget, 14, 14, icon);
local c = iconColor or self.config.font.color.disabled;
texture:SetVertexColor(c.r, c.g, c.b, c.a);
self:GlueLeft(texture, widget, 5, 0, true);
self:GlueRight(label, texture, 2, 0);
widget.placeholder.icon = texture;
else
self:GlueLeft(label, widget, 2, 0, true);
end
widget:HookScript('OnTextChanged', ApplyPlaceholderOnTextChanged);
end
----------------------------------------------------
--- SearchEditBox
----------------------------------------------------
local SearchEditBoxOnTextChanged = function(self)
if self.OnValueChanged then
self:OnValueChanged(self:GetText());
end
end
function StdUi:SearchEditBox(parent, width, height, placeholderText)
local editBox = self:SimpleEditBox(parent, width, height, '');
editBox:SetScript('OnTextChanged', SearchEditBoxOnTextChanged);
self:ApplyPlaceholder(editBox, placeholderText, [[Interface\Common\UI-Searchbox-Icon]]);
return editBox;
end
----------------------------------------------------
--- SearchEditBox
----------------------------------------------------
local EditBoxMethods = {
GetValue = function(self)
return self.value;
end,
SetValue = function(self, value)
self.value = value;
self:SetText(value);
self:Validate();
self.button:Hide();
end,
IsValid = function(self)
return self.isValid;
end,
Validate = function(self)
self.isValidated = true;
self.isValid = self.validator(self);
if self.isValid then
if self.button then
self.button:Hide();
end
if self.OnValueChanged and tostring(self.lastValue) ~= tostring(self.value) then
self:OnValueChanged(self.value);
self.lastValue = self.value;
end
end
self.isValidated = false;
end;
}
local EditBoxButtonOnClick = function(self)
self.editBox:Validate(self.editBox);
end
local EditBoxEvents = {
OnEnterPressed = function(self)
self:Validate();
end,
OnTextChanged = function(self, isUserInput)
local value = StdUi.Util.stripColors(self:GetText());
if tostring(value) ~= tostring(self.value) then
if not self.isValidated and self.button and isUserInput then
self.button:Show();
end
else
self.button:Hide();
end
end
}
--- @return EditBox
function StdUi:EditBox(parent, width, height, text, validator)
validator = validator or StdUi.Util.editBoxValidator;
local editBox = self:SimpleEditBox(parent, width, height, text);
editBox.validator = validator;
local button = self:Button(editBox, 40, height - 4, OKAY);
button:SetPoint('RIGHT', -2, 0);
button:Hide();
button.editBox = editBox;
editBox.button = button;
for k, v in pairs(EditBoxMethods) do
editBox[k] = v;
end
button:SetScript('OnClick', EditBoxButtonOnClick);
for k, v in pairs(EditBoxEvents) do
editBox:SetScript(k, v);
end
return editBox;
end
----------------------------------------------------
--- SearchEditBox
----------------------------------------------------
local NumericBoxMethods = {
SetMaxValue = function(self, value)
self.maxValue = value;
self:Validate();
end;
SetMinValue = function(self, value)
self.minValue = value;
self:Validate();
end;
SetMinMaxValue = function(self, min, max)
self.minValue = min;
self.maxValue = max;
self:Validate();
end
}
function StdUi:NumericBox(parent, width, height, text, validator)
validator = validator or self.Util.numericBoxValidator;
local editBox = self:EditBox(parent, width, height, text, validator);
editBox:SetNumeric(true);
for k, v in pairs(NumericBoxMethods) do
editBox[k] = v;
end
return editBox;
end
----------------------------------------------------
--- MoneyBox
----------------------------------------------------
local MoneyBoxMethods = {
SetValue = function(self, value)
self.value = value;
local formatted = self.stdUi.Util.formatMoney(value);
self:SetText(formatted);
self:Validate();
self.button:Hide();
end;
};
function StdUi:MoneyBox(parent, width, height, text, validator, excludeCopper)
if excludeCopper then
validator = validator or self.Util.moneyBoxValidatorExC;
else
validator = validator or self.Util.moneyBoxValidator;
end
local editBox = self:EditBox(parent, width, height, text, validator);
editBox.stdUi = self;
editBox:SetMaxLetters(20);
for k, v in pairs(MoneyBoxMethods) do
editBox[k] = v;
end
return editBox;
end
----------------------------------------------------
--- MultiLineBox
----------------------------------------------------
local MultiLineBoxMethods = {
SetValue = function(self, value)
self.editBox:SetText(value);
if self.OnValueChanged then
self:OnValueChanged(value);
end
end,
GetValue = function(self)
return self.editBox:GetText();
end,
SetFont = function(self, font, size, flags)
self.editBox:SetFont(font, size, flags);
end,
Enable = function(self)
self.editBox:Enable();
end,
Disable = function(self)
self.editBox:Disable();
end,
SetFocus = function(self)
self.editBox:SetFocus();
end,
ClearFocus = function(self)
self.editBox:ClearFocus();
end,
HasFocus = function(self)
return self.editBox:HasFocus();
end
};
local MultiLineBoxOnCursorChanged = function(self, _, y, _, cursorHeight)
local sf, newY = self.scrollFrame, -y;
local offset = sf:GetVerticalScroll();
if newY < offset then
sf:SetVerticalScroll(newY);
else
newY = newY + cursorHeight - sf:GetHeight() + 6; --text insets
if newY > offset then
sf:SetVerticalScroll(math.ceil(newY));
end
end
end
local MultiLineBoxOnTextChanged = function(self)
if self.panel.OnValueChanged then
self.panel.OnValueChanged(self.panel, self:GetText());
end
end
local MultiLineBoxScrollOnMouseDown = function(self, button)
self.scrollChild:SetFocus();
end
local MultiLineBoxScrollOnVerticalScroll = function(self, offset)
self.scrollChild:SetHitRectInsets(0, 0, offset, self.scrollChild:GetHeight() - offset - self:GetHeight());
end
function StdUi:MultiLineBox(parent, width, height, text)
local editBox = CreateFrame('EditBox');
local widget = self:ScrollFrame(parent, width, height, editBox);
editBox.stdUi = self;
local scrollFrame = widget.scrollFrame;
scrollFrame.editBox = editBox;
widget.editBox = editBox;
editBox.panel = widget;
self:ApplyBackdrop(widget, 'button');
self:HookHoverBorder(scrollFrame);
self:HookHoverBorder(editBox);
editBox:SetWidth(scrollFrame:GetWidth());
self:GlueAcross(scrollFrame, widget, 2, -2, -widget.scrollBarWidth - 2, 3);
--editBox:SetHeight(scrollFrame:GetHeight());
editBox:SetTextInsets(3, 3, 3, 3);
editBox:SetFontObject(ChatFontNormal);
editBox:SetAutoFocus(false);
editBox:SetScript('OnEscapePressed', editBox.ClearFocus);
editBox:SetMultiLine(true);
editBox:EnableMouse(true);
editBox:SetAutoFocus(false);
editBox:SetCountInvisibleLetters(false);
editBox:SetAllPoints();
editBox.scrollFrame = scrollFrame;
editBox.panel = widget;
for k, v in pairs(MultiLineBoxMethods) do
widget[k] = v;
end
if text then
editBox:SetText(text);
end
editBox:SetScript('OnCursorChanged', MultiLineBoxOnCursorChanged)
editBox:SetScript('OnTextChanged', MultiLineBoxOnTextChanged);
scrollFrame:HookScript('OnMouseDown', MultiLineBoxScrollOnMouseDown);
scrollFrame:HookScript('OnVerticalScroll', MultiLineBoxScrollOnVerticalScroll);
widget.SetText = widget.SetValue;
widget.GetText = widget.GetValue;
return widget;
end
StdUi:RegisterModule(module, version);