comparison Libs/DF/textentry.lua @ 11:2f09fe4be15c

Added an Options Panel.
author Tercio
date Mon, 20 Apr 2015 16:34:18 -0300
parents
children dc1c77254f80
comparison
equal deleted inserted replaced
10:f1e32be6773e 11:2f09fe4be15c
1
2
3
4 local DF = _G ["DetailsFramework"]
5 local _
6
7 local _rawset = rawset --> lua local
8 local _rawget = rawget --> lua local
9 local _setmetatable = setmetatable --> lua local
10 local _unpack = unpack --> lua local
11 local _type = type --> lua local
12 local _math_floor = math.floor --> lua local
13 local loadstring = loadstring --> lua local
14 local _string_len = string.len --> lua local
15
16 local cleanfunction = function() end
17 local APITextEntryFunctions = false
18 local TextEntryMetaFunctions = {}
19
20 DF.TextEntryCounter = 1
21
22 ------------------------------------------------------------------------------------------------------------
23 --> metatables
24
25 TextEntryMetaFunctions.__call = function (_table, value)
26 --> unknow
27 end
28
29 ------------------------------------------------------------------------------------------------------------
30 --> members
31
32 --> tooltip
33 local gmember_tooltip = function (_object)
34 return _object:GetTooltip()
35 end
36 --> shown
37 local gmember_shown = function (_object)
38 return _object:IsShown()
39 end
40 --> frame width
41 local gmember_width = function (_object)
42 return _object.editbox:GetWidth()
43 end
44 --> frame height
45 local gmember_height = function (_object)
46 return _object.editbox:GetHeight()
47 end
48 --> get text
49 local gmember_text = function (_object)
50 return _object.editbox:GetText()
51 end
52
53 local get_members_function_index = {
54 ["tooltip"] = gmember_tooltip,
55 ["shown"] = gmember_shown,
56 ["width"] = gmember_width,
57 ["height"] = gmember_height,
58 ["text"] = gmember_text,
59 }
60
61 TextEntryMetaFunctions.__index = function (_table, _member_requested)
62
63 local func = get_members_function_index [_member_requested]
64 if (func) then
65 return func (_table, _member_requested)
66 end
67
68 local fromMe = _rawget (_table, _member_requested)
69 if (fromMe) then
70 return fromMe
71 end
72
73 return TextEntryMetaFunctions [_member_requested]
74 end
75
76 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
77
78 --> tooltip
79 local smember_tooltip = function (_object, _value)
80 return _object:SetTooltip (_value)
81 end
82 --> show
83 local smember_show = function (_object, _value)
84 if (_value) then
85 return _object:Show()
86 else
87 return _object:Hide()
88 end
89 end
90 --> hide
91 local smember_hide = function (_object, _value)
92 if (not _value) then
93 return _object:Show()
94 else
95 return _object:Hide()
96 end
97 end
98 --> frame width
99 local smember_width = function (_object, _value)
100 return _object.editbox:SetWidth (_value)
101 end
102 --> frame height
103 local smember_height = function (_object, _value)
104 return _object.editbox:SetHeight (_value)
105 end
106 --> set text
107 local smember_text = function (_object, _value)
108 return _object.editbox:SetText (_value)
109 end
110 --> set multiline
111 local smember_multiline = function (_object, _value)
112 if (_value) then
113 return _object.editbox:SetMultiLine (true)
114 else
115 return _object.editbox:SetMultiLine (false)
116 end
117 end
118 --> text horizontal pos
119 local smember_horizontalpos = function (_object, _value)
120 return _object.editbox:SetJustifyH (string.lower (_value))
121 end
122
123 local set_members_function_index = {
124 ["tooltip"] = smember_tooltip,
125 ["show"] = smember_show,
126 ["hide"] = smember_hide,
127 ["width"] = smember_width,
128 ["height"] = smember_height,
129 ["text"] = smember_text,
130 ["multiline"] = smember_multiline,
131 ["align"] = smember_horizontalpos,
132 }
133
134 TextEntryMetaFunctions.__newindex = function (_table, _key, _value)
135 local func = set_members_function_index [_key]
136 if (func) then
137 return func (_table, _value)
138 else
139 return _rawset (_table, _key, _value)
140 end
141 end
142
143 ------------------------------------------------------------------------------------------------------------
144 --> methods
145
146 --> set point
147 function TextEntryMetaFunctions:SetPoint (MyAnchor, SnapTo, HisAnchor, x, y, Width)
148
149 if (type (MyAnchor) == "boolean" and MyAnchor and self.space) then
150 local textWidth = self.label:GetStringWidth()+2
151 self.editbox:SetWidth (self.space - textWidth - 15)
152 return
153
154 elseif (type (MyAnchor) == "boolean" and MyAnchor and not self.space) then
155 self.space = self.label:GetStringWidth()+2 + self.editbox:GetWidth()
156 end
157
158 if (Width) then
159 self.space = Width
160 end
161
162 MyAnchor, SnapTo, HisAnchor, x, y = DF:CheckPoints (MyAnchor, SnapTo, HisAnchor, x, y, self)
163 if (not MyAnchor) then
164 print ("Invalid parameter for SetPoint")
165 return
166 end
167
168 if (self.space) then
169 self.label:ClearAllPoints()
170 self.editbox:ClearAllPoints()
171
172 self.label:SetPoint (MyAnchor, SnapTo, HisAnchor, x, y)
173 self.editbox:SetPoint ("left", self.label, "right", 2, 0)
174
175 local textWidth = self.label:GetStringWidth()+2
176 self.editbox:SetWidth (self.space - textWidth - 15)
177 else
178 self.label:ClearAllPoints()
179 self.editbox:ClearAllPoints()
180 self.editbox:SetPoint (MyAnchor, SnapTo, HisAnchor, x, y)
181 end
182
183 end
184
185 --> frame levels
186 function TextEntryMetaFunctions:GetFrameLevel()
187 return self.editbox:GetFrameLevel()
188 end
189 function TextEntryMetaFunctions:SetFrameLevel (level, frame)
190 if (not frame) then
191 return self.editbox:SetFrameLevel (level)
192 else
193 local framelevel = frame:GetFrameLevel (frame) + level
194 return self.editbox:SetFrameLevel (framelevel)
195 end
196 end
197
198 --> select all text
199 function TextEntryMetaFunctions:SelectAll()
200 self.editbox:HighlightText()
201 end
202
203 --> set labal description
204 function TextEntryMetaFunctions:SetLabelText (text)
205 if (text) then
206 self.label:SetText (text)
207 else
208 self.label:SetText ("")
209 end
210 self:SetPoint (true) --> refresh
211 end
212
213 --> set tab order
214 function TextEntryMetaFunctions:SetNext (nextbox)
215 self.next = nextbox
216 end
217
218 --> blink
219 function TextEntryMetaFunctions:Blink()
220 self.label:SetTextColor (1, .2, .2, 1)
221 end
222
223 --> show & hide
224 function TextEntryMetaFunctions:IsShown()
225 return self.editbox:IsShown()
226 end
227 function TextEntryMetaFunctions:Show()
228 return self.editbox:Show()
229 end
230 function TextEntryMetaFunctions:Hide()
231 return self.editbox:Hide()
232 end
233
234 -- tooltip
235 function TextEntryMetaFunctions:SetTooltip (tooltip)
236 if (tooltip) then
237 return _rawset (self, "have_tooltip", tooltip)
238 else
239 return _rawset (self, "have_tooltip", nil)
240 end
241 end
242 function TextEntryMetaFunctions:GetTooltip()
243 return _rawget (self, "have_tooltip")
244 end
245
246 --> hooks
247 function TextEntryMetaFunctions:SetHook (hookType, func)
248 if (func) then
249 _rawset (self, hookType.."Hook", func)
250 else
251 _rawset (self, hookType.."Hook", nil)
252 end
253 end
254
255 function TextEntryMetaFunctions:Enable()
256 if (not self.editbox:IsEnabled()) then
257 self.editbox:Enable()
258 self.editbox:SetBackdropBorderColor (unpack (self.enabled_border_color))
259 self.editbox:SetBackdropColor (unpack (self.enabled_backdrop_color))
260 self.editbox:SetTextColor (unpack (self.enabled_text_color))
261 end
262 end
263
264 function TextEntryMetaFunctions:Disable()
265 if (self.editbox:IsEnabled()) then
266 self.enabled_border_color = {self.editbox:GetBackdropBorderColor()}
267 self.enabled_backdrop_color = {self.editbox:GetBackdropColor()}
268 self.enabled_text_color = {self.editbox:GetTextColor()}
269
270 self.editbox:Disable()
271
272 self.editbox:SetBackdropBorderColor (.5, .5, .5, .5)
273 self.editbox:SetBackdropColor (.5, .5, .5, .5)
274 self.editbox:SetTextColor (.5, .5, .5, .5)
275 end
276 end
277
278 ------------------------------------------------------------------------------------------------------------
279 --> scripts
280 local OnEnter = function (textentry)
281
282 if (textentry.MyObject.OnEnterHook) then
283 local interrupt = textentry.MyObject.OnEnterHook (textentry)
284 if (interrupt) then
285 return
286 end
287 end
288
289 if (textentry.MyObject.have_tooltip) then
290 GameCooltip2:Preset (2)
291 GameCooltip2:AddLine (textentry.MyObject.have_tooltip)
292 GameCooltip2:ShowCooltip (textentry, "tooltip")
293 end
294
295 textentry.mouse_over = true
296
297 if (textentry:IsEnabled()) then
298 textentry.current_bordercolor = textentry.current_bordercolor or {textentry:GetBackdropBorderColor()}
299 textentry:SetBackdropBorderColor (1, 1, 1, 1)
300 end
301
302 local parent = textentry:GetParent().MyObject
303 if (parent and parent.type == "panel") then
304 if (parent.GradientEnabled) then
305 parent:RunGradient()
306 end
307 end
308
309 end
310
311 local OnLeave = function (textentry)
312 if (textentry.MyObject.OnLeaveHook) then
313 local interrupt = textentry.MyObject.OnLeaveHook (textentry)
314 if (interrupt) then
315 return
316 end
317 end
318
319 if (textentry.MyObject.have_tooltip) then
320 GameCooltip2:ShowMe (false)
321 end
322
323 textentry.mouse_over = false
324
325 if (textentry:IsEnabled()) then
326 textentry:SetBackdropBorderColor (unpack (textentry.current_bordercolor))
327 end
328
329 local parent = textentry:GetParent().MyObject
330 if (parent and parent.type == "panel") then
331 if (parent.GradientEnabled) then
332 parent:RunGradient (false)
333 end
334 end
335 end
336
337 local OnHide = function (textentry)
338 if (textentry.MyObject.OnHideHook) then
339 local interrupt = textentry.MyObject.OnHideHook (textentry)
340 if (interrupt) then
341 return
342 end
343 end
344 end
345
346 local OnShow = function (textentry)
347 if (textentry.MyObject.OnShowHook) then
348 local interrupt = textentry.MyObject.OnShowHook (textentry)
349 if (interrupt) then
350 return
351 end
352 end
353 end
354
355 local OnEnterPressed = function (textentry, byScript)
356
357 if (textentry.MyObject.OnEnterPressedHook) then
358 local interrupt = textentry.MyObject.OnEnterPressedHook (textentry)
359 if (interrupt) then
360 return
361 end
362 end
363
364 local texto = DF:trim (textentry:GetText())
365 if (_string_len (texto) > 0) then
366 textentry.text = texto
367 if (textentry.MyObject.func) then
368 textentry.MyObject.func (textentry.MyObject.param1, textentry.MyObject.param2, texto, textentry, byScript or textentry)
369 end
370 else
371 textentry:SetText ("")
372 textentry.MyObject.currenttext = ""
373 end
374 textentry.focuslost = true --> perdeu_focus isso aqui pra quando estiver editando e clicar em outra caixa
375 textentry:ClearFocus()
376
377 if (textentry.MyObject.tab_on_enter and textentry.MyObject.next) then
378 textentry.MyObject.next:SetFocus()
379 end
380 end
381
382 local OnEscapePressed = function (textentry)
383
384 if (textentry.MyObject.OnEscapePressedHook) then
385 local interrupt = textentry.MyObject.OnEscapePressedHook (textentry)
386 if (interrupt) then
387 return
388 end
389 end
390
391 --textentry:SetText("")
392 --textentry.MyObject.currenttext = ""
393 textentry.focuslost = true
394 textentry:ClearFocus()
395 end
396
397 local OnEditFocusLost = function (textentry)
398
399 if (textentry:IsShown()) then
400
401 if (textentry.MyObject.OnEditFocusLostHook) then
402 local interrupt = textentry.MyObject.OnEditFocusLostHook (textentry)
403 if (interrupt) then
404 return
405 end
406 end
407
408 if (not textentry.focuslost) then
409 local texto = DF:trim (textentry:GetText())
410 if (_string_len (texto) > 0) then
411 textentry.MyObject.currenttext = texto
412 if (textentry.MyObject.func) then
413 textentry.MyObject.func (textentry.MyObject.param1, textentry.MyObject.param2, texto, textentry, nil)
414 end
415 else
416 textentry:SetText ("")
417 textentry.MyObject.currenttext = ""
418 end
419 else
420 textentry.focuslost = false
421 end
422
423 textentry.MyObject.label:SetTextColor (.8, .8, .8, 1)
424
425 end
426 end
427
428 local OnEditFocusGained = function (textentry)
429 if (textentry.MyObject.OnEditFocusGainedHook) then
430 local interrupt = textentry.MyObject.OnEditFocusGainedHook (textentry)
431 if (interrupt) then
432 return
433 end
434 end
435 textentry.MyObject.label:SetTextColor (1, 1, 1, 1)
436 end
437
438 local OnChar = function (textentry, text)
439 if (textentry.MyObject.OnCharHook) then
440 local interrupt = textentry.MyObject.OnCharHook (textentry, text)
441 if (interrupt) then
442 return
443 end
444 end
445 end
446
447 local OnTextChanged = function (textentry, byUser)
448 if (textentry.MyObject.OnTextChangedHook) then
449 local interrupt = textentry.MyObject.OnTextChangedHook (textentry, byUser)
450 if (interrupt) then
451 return
452 end
453 end
454 end
455
456 local OnTabPressed = function (textentry)
457 if (textentry.MyObject.OnTabPressedHook) then
458 local interrupt = textentry.MyObject.OnTabPressedHook (textentry, byUser)
459 if (interrupt) then
460 return
461 end
462 end
463
464 if (textentry.MyObject.next) then
465 OnEnterPressed (textentry, false)
466 textentry.MyObject.next:SetFocus()
467 end
468 end
469
470 function TextEntryMetaFunctions:PressEnter (byScript)
471 OnEnterPressed (self.editbox, byScript)
472 end
473
474 ------------------------------------------------------------------------------------------------------------
475 --> object constructor
476
477 function DF:CreateTextEntry (parent, func, w, h, member, name)
478 return DF:NewTextEntry (parent, parent, name, member, w, h, func)
479 end
480
481 function DF:NewTextEntry (parent, container, name, member, w, h, func, param1, param2, space)
482
483 if (not name) then
484 name = "DetailsFrameworkTextEntryNumber" .. DF.TextEntryCounter
485 DF.TextEntryCounter = DF.TextEntryCounter + 1
486
487 elseif (not parent) then
488 return nil
489 end
490
491 if (not container) then
492 container = parent
493 end
494
495 if (name:find ("$parent")) then
496 name = name:gsub ("$parent", parent:GetName())
497 end
498
499 local TextEntryObject = {type = "textentry", dframework = true}
500
501 if (member) then
502 parent [member] = TextEntryObject
503 end
504
505 if (parent.dframework) then
506 parent = parent.widget
507 end
508 if (container.dframework) then
509 container = container.widget
510 end
511
512 --> default members:
513 --> hooks
514 TextEntryObject.OnEnterHook = nil
515 TextEntryObject.OnLeaveHook = nil
516 TextEntryObject.OnHideHook = nil
517 TextEntryObject.OnShowHook = nil
518 TextEntryObject.OnEnterPressedHook = nil
519 TextEntryObject.OnEscapePressedHook = nil
520 TextEntryObject.OnEditFocusGainedHook = nil
521 TextEntryObject.OnEditFocusLostHook = nil
522 TextEntryObject.OnCharHook = nil
523 TextEntryObject.OnTextChangedHook = nil
524 TextEntryObject.OnTabPressedHook = nil
525
526 --> misc
527 TextEntryObject.container = container
528 TextEntryObject.have_tooltip = nil
529
530 TextEntryObject.editbox = CreateFrame ("EditBox", name, parent, "DetailsFrameworkEditBoxTemplate2")
531 TextEntryObject.widget = TextEntryObject.editbox
532
533 TextEntryObject.editbox:SetTextInsets (3, 0, 0, -3)
534
535 if (not APITextEntryFunctions) then
536 APITextEntryFunctions = true
537 local idx = getmetatable (TextEntryObject.editbox).__index
538 for funcName, funcAddress in pairs (idx) do
539 if (not TextEntryMetaFunctions [funcName]) then
540 TextEntryMetaFunctions [funcName] = function (object, ...)
541 local x = loadstring ( "return _G."..object.editbox:GetName()..":"..funcName.."(...)")
542 return x (...)
543 end
544 end
545 end
546 end
547
548 TextEntryObject.editbox.MyObject = TextEntryObject
549
550 if (not w and space) then
551 w = space
552 elseif (w and space) then
553 if (DF.debug) then
554 print ("warning: you are using width and space, try use only space for better results.")
555 end
556 end
557
558 TextEntryObject.editbox:SetWidth (w)
559 TextEntryObject.editbox:SetHeight (h)
560
561 TextEntryObject.editbox:SetJustifyH ("center")
562 TextEntryObject.editbox:EnableMouse (true)
563 TextEntryObject.editbox:SetText ("")
564
565 TextEntryObject.editbox:SetAutoFocus (false)
566 TextEntryObject.editbox:SetFontObject ("GameFontHighlightSmall")
567
568 TextEntryObject.editbox.current_bordercolor = {1, 1, 1, 0.7}
569 TextEntryObject.editbox:SetBackdropBorderColor (1, 1, 1, 0.7)
570 TextEntryObject.enabled_border_color = {TextEntryObject.editbox:GetBackdropBorderColor()}
571 TextEntryObject.enabled_backdrop_color = {TextEntryObject.editbox:GetBackdropColor()}
572 TextEntryObject.enabled_text_color = {TextEntryObject.editbox:GetTextColor()}
573
574 TextEntryObject.func = func
575 TextEntryObject.param1 = param1
576 TextEntryObject.param2 = param2
577 TextEntryObject.next = nil
578 TextEntryObject.space = space
579 TextEntryObject.tab_on_enter = false
580
581 TextEntryObject.label = _G [name .. "_Desc"]
582
583 TextEntryObject.editbox:SetBackdrop ({bgFile = DF.folder .. "background", tileSize = 64, edgeFile = DF.folder .. "border_2", edgeSize = 10, insets = {left = 1, right = 1, top = 1, bottom = 1}})
584
585 --> hooks
586 TextEntryObject.editbox:SetScript ("OnEnter", OnEnter)
587 TextEntryObject.editbox:SetScript ("OnLeave", OnLeave)
588 TextEntryObject.editbox:SetScript ("OnHide", OnHide)
589 TextEntryObject.editbox:SetScript ("OnShow", OnShow)
590
591 TextEntryObject.editbox:SetScript ("OnEnterPressed", OnEnterPressed)
592 TextEntryObject.editbox:SetScript ("OnEscapePressed", OnEscapePressed)
593 TextEntryObject.editbox:SetScript ("OnEditFocusLost", OnEditFocusLost)
594 TextEntryObject.editbox:SetScript ("OnEditFocusGained", OnEditFocusGained)
595 TextEntryObject.editbox:SetScript ("OnChar", OnChar)
596 TextEntryObject.editbox:SetScript ("OnTextChanged", OnTextChanged)
597 TextEntryObject.editbox:SetScript ("OnTabPressed", OnTabPressed)
598
599 _setmetatable (TextEntryObject, TextEntryMetaFunctions)
600
601 return TextEntryObject
602
603 end
604
605 local function_gettext = function (self)
606 return self.editbox:GetText()
607 end
608 local function_settext = function (self, text)
609 return self.editbox:SetText (text)
610 end
611 local function_clearfocus = function (self)
612 return self.editbox:ClearFocus()
613 end
614 local function_setfocus = function (self)
615 return self.editbox:SetFocus (true)
616 end
617
618 function DF:NewSpecialLuaEditorEntry (parent, w, h, member, name, nointent)
619
620 if (name:find ("$parent")) then
621 name = name:gsub ("$parent", parent:GetName())
622 end
623
624 local borderframe = CreateFrame ("Frame", name, parent)
625 borderframe:SetSize (w, h)
626
627 if (member) then
628 parent [member] = borderframe
629 end
630
631 local scrollframe = CreateFrame ("ScrollFrame", name, borderframe, "DetailsFrameworkEditBoxMultiLineTemplate")
632
633 scrollframe:SetScript ("OnSizeChanged", function (self)
634 scrollframe.editbox:SetSize (self:GetSize())
635 end)
636
637 scrollframe:SetPoint ("topleft", borderframe, "topleft", 10, -10)
638 scrollframe:SetPoint ("bottomright", borderframe, "bottomright", -30, 10)
639
640 scrollframe.editbox:SetMultiLine (true)
641 scrollframe.editbox:SetJustifyH ("left")
642 scrollframe.editbox:SetJustifyV ("top")
643 scrollframe.editbox:SetMaxBytes (1024000)
644 scrollframe.editbox:SetMaxLetters (128000)
645
646 borderframe.GetText = function_gettext
647 borderframe.SetText = function_settext
648 borderframe.ClearFocus = function_clearfocus
649 borderframe.SetFocus = function_setfocus
650
651 if (not nointent) then
652 IndentationLib.enable (scrollframe.editbox, nil, 4)
653 end
654
655 borderframe:SetBackdrop ({bgFile = [[Interface\Tooltips\UI-Tooltip-Background]], edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]],
656 tile = 1, tileSize = 16, edgeSize = 16, insets = {left = 5, right = 5, top = 5, bottom = 5}})
657 borderframe:SetBackdropColor (0.090195, 0.090195, 0.188234, 1)
658 borderframe:SetBackdropBorderColor (1, 1, 1, 1)
659
660 borderframe.scroll = scrollframe
661 borderframe.editbox = scrollframe.editbox
662
663 return borderframe
664 end