-
Notifications
You must be signed in to change notification settings - Fork 7
/
textentry.lua
1341 lines (1113 loc) · 43.6 KB
/
textentry.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[=[
When enter is pressed a callback function is called in this format:
local onEnterPressed = function(parameter1, parameter2, text, self, byScript)
--parameter1 and parameter2 are set with self:SetParameters(param1, param2)
--they act like fixed parameters for the callback
--use they to use a single function for multiple text entries
end
textentry.MyObject.func(textentry.MyObject.param1, textentry.MyObject.param2, text, textentry, byScript or textentry)
--]=]
local detailsFramework = _G["DetailsFramework"]
if (not detailsFramework or not DetailsFrameworkCanLoad) then
return
end
local _
local APITextEntryFunctions = false
do
local metaPrototype = {
WidgetType = "textentry",
dversion = detailsFramework.dversion,
}
--check if there's a metaPrototype already existing
if (_G[detailsFramework.GlobalWidgetControlNames["textentry"]]) then
--get the already existing metaPrototype
local oldMetaPrototype = _G[detailsFramework.GlobalWidgetControlNames["textentry"]]
--check if is older
if ( (not oldMetaPrototype.dversion) or (oldMetaPrototype.dversion < detailsFramework.dversion) ) then
--the version is older them the currently loading one
--copy the new values into the old metatable
for funcName, _ in pairs(metaPrototype) do
oldMetaPrototype[funcName] = metaPrototype[funcName]
end
end
else
--first time loading the framework
_G[detailsFramework.GlobalWidgetControlNames["textentry"]] = metaPrototype
end
end
local TextEntryMetaFunctions = _G[detailsFramework.GlobalWidgetControlNames["textentry"]]
detailsFramework:Mixin(TextEntryMetaFunctions, detailsFramework.SetPointMixin)
detailsFramework:Mixin(TextEntryMetaFunctions, detailsFramework.FrameMixin)
detailsFramework:Mixin(TextEntryMetaFunctions, detailsFramework.TooltipHandlerMixin)
detailsFramework:Mixin(TextEntryMetaFunctions, detailsFramework.ScriptHookMixin)
detailsFramework.TextEntryCounter = detailsFramework.TextEntryCounter or 1
------------------------------------------------------------------------------------------------------------
--metatables
TextEntryMetaFunctions.__call = function(object, value)
end
------------------------------------------------------------------------------------------------------------
--members
--tooltip
local gmember_tooltip = function(object)
return object:GetTooltip()
end
--shown
local gmember_shown = function(object)
return object:IsShown()
end
--frame width
local gmember_width = function(object)
return object.editbox:GetWidth()
end
--frame height
local gmember_height = function(object)
return object.editbox:GetHeight()
end
--get text
local gmember_text = function(object)
return object.editbox:GetText()
end
--return if the text entry has focus
local gmember_hasfocus = function(object)
return object:HasFocus()
end
TextEntryMetaFunctions.GetMembers = TextEntryMetaFunctions.GetMembers or {}
TextEntryMetaFunctions.GetMembers["tooltip"] = gmember_tooltip
TextEntryMetaFunctions.GetMembers["shown"] = gmember_shown
TextEntryMetaFunctions.GetMembers["width"] = gmember_width
TextEntryMetaFunctions.GetMembers["height"] = gmember_height
TextEntryMetaFunctions.GetMembers["text"] = gmember_text
TextEntryMetaFunctions.GetMembers["hasfocus"] = gmember_hasfocus
TextEntryMetaFunctions.__index = function(object, key)
local func = TextEntryMetaFunctions.GetMembers[key]
if (func) then
return func(object, key)
end
local fromMe = rawget(object, key)
if (fromMe) then
return fromMe
end
return TextEntryMetaFunctions[key]
end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--tooltip
local smember_tooltip = function(object, value)
return object:SetTooltip(value)
end
--show
local smember_show = function(object, value)
if (value) then
return object:Show()
else
return object:Hide()
end
end
--hide
local smember_hide = function(object, value)
if (not value) then
return object:Show()
else
return object:Hide()
end
end
--frame width
local smember_width = function(object, value)
return object.editbox:SetWidth(value)
end
--frame height
local smember_height = function(object, value)
return object.editbox:SetHeight(value)
end
--set text
local smember_text = function(object, value)
return object.editbox:SetText(value)
end
--set multiline
local smember_multiline = function(object, value)
if (value) then
return object.editbox:SetMultiLine(true)
else
return object.editbox:SetMultiLine(false)
end
end
local smember_fontsize = function(object, value)
return detailsFramework:SetFontSize(object.editbox, value)
end
--text horizontal pos
local smember_horizontalpos = function(object, value)
return object.editbox:SetJustifyH(string.lower(value))
end
TextEntryMetaFunctions.SetMembers = TextEntryMetaFunctions.SetMembers or {}
TextEntryMetaFunctions.SetMembers["tooltip"] = smember_tooltip
TextEntryMetaFunctions.SetMembers["show"] = smember_show
TextEntryMetaFunctions.SetMembers["hide"] = smember_hide
TextEntryMetaFunctions.SetMembers["width"] = smember_width
TextEntryMetaFunctions.SetMembers["height"] = smember_height
TextEntryMetaFunctions.SetMembers["text"] = smember_text
TextEntryMetaFunctions.SetMembers["multiline"] = smember_multiline
TextEntryMetaFunctions.SetMembers["align"] = smember_horizontalpos
TextEntryMetaFunctions.SetMembers["fontsize"] = smember_fontsize
TextEntryMetaFunctions.SetMembers["textsize"] = smember_fontsize
TextEntryMetaFunctions.__newindex = function(object, key, value)
local func = TextEntryMetaFunctions.SetMembers[key]
if (func) then
return func(object, value)
else
return rawset(object, key, value)
end
end
------------------------------------------------------------------------------------------------------------
--methods
local cleanfunction = function()end
function TextEntryMetaFunctions:SetEnterFunction(func, param1, param2)
if (func) then
rawset(self, "func", func)
else
rawset(self, "func", cleanfunction)
end
if (param1 ~= nil) then
rawset(self, "param1", param1)
end
if (param2 ~= nil) then
rawset(self, "param2", param2)
end
end
function TextEntryMetaFunctions:SetText(text)
self.editbox:SetText(text)
end
function TextEntryMetaFunctions:GetText()
return self.editbox:GetText()
end
--select all text
function TextEntryMetaFunctions:SelectAll()
self.editbox:HighlightText()
end
function TextEntryMetaFunctions:SetAutoSelectTextOnFocus(value)
self.autoSelectAllText = value
end
--set label description
function TextEntryMetaFunctions:SetLabelText(text)
self.label:SetText(text)
end
--set tab order
function TextEntryMetaFunctions:SetNext(nextbox)
self.next = nextbox
end
function TextEntryMetaFunctions:SetParameters(param1, param2)
if (param1 ~= nil) then
self.param1 = param1
end
if (param2 ~= nil) then
self.param2 = param2
end
end
--blink
function TextEntryMetaFunctions:Blink()
self.label:SetTextColor(1, .2, .2, 1)
end
--hooks
function TextEntryMetaFunctions:Enable()
if (not self.editbox:IsEnabled()) then
self.editbox:Enable()
self.editbox:SetBackdropBorderColor(unpack(self.enabled_border_color))
self.editbox:SetBackdropColor(unpack(self.enabled_backdrop_color))
self.editbox:SetTextColor(unpack(self.enabled_text_color))
if (self.editbox.borderframe) then
local r, g, b, a = detailsFramework:ParseColors(unpack(self.editbox.borderframe.onleave_backdrop))
self.editbox.borderframe:SetBackdropColor(r, g, b, a)
end
end
end
function TextEntryMetaFunctions:Disable()
if (self.editbox:IsEnabled()) then
self.enabled_border_color = {self.editbox:GetBackdropBorderColor()}
self.enabled_backdrop_color = {self.editbox:GetBackdropColor()}
self.enabled_text_color = {self.editbox:GetTextColor()}
self.editbox:Disable()
self.editbox:SetBackdropBorderColor(0, 0, 0, 1)
self.editbox:SetBackdropColor(.1, .1, .1, .834)
self.editbox:SetTextColor(.5, .5, .5, .5)
if (self.editbox.borderframe) then
self.editbox.borderframe:SetBackdropColor(.5, .5, .5, .5)
end
end
end
function TextEntryMetaFunctions:SetCommitFunction(func)
if (type(func) == "function") then
self.func = func
end
end
function TextEntryMetaFunctions:IgnoreNextCallback()
self.ignoreNextCallback = true
end
------------------------------------------------------------------------------------------------------------
--scripts and hooks
local OnEnter = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnEnter", textentry, object)
if (kill) then
return
end
object:ShowTooltip()
textentry.mouse_over = true
if (textentry:IsEnabled()) then
textentry.current_bordercolor = textentry.current_bordercolor or {textentry:GetBackdropBorderColor()}
local onEnterBorderColor = object.onenter_backdrop_border_color
if (onEnterBorderColor) then
textentry:SetBackdropBorderColor(detailsFramework:ParseColors(onEnterBorderColor))
else
textentry:SetBackdropBorderColor(1, 1, 1, 0.6)
end
end
end
local OnLeave = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnLeave", textentry, object)
if (kill) then
return
end
object:HideTooltip()
textentry.mouse_over = false
if (textentry:IsEnabled()) then
textentry:SetBackdropBorderColor(unpack(textentry.current_bordercolor))
end
end
local OnHide = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnHide", textentry, object)
if (kill) then
return
end
end
local OnShow = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnShow", textentry, object)
if (kill) then
return
end
end
local OnEnterPressed = function(textentry, byScript)
local object = textentry.MyObject
if (object.ignoreNextCallback) then
detailsFramework.Schedules.RunNextTick(function() object.ignoreNextCallback = nil end)
return
end
local kill = object:RunHooksForWidget("OnEnterPressed", textentry, object, object.text)
if (kill) then
return
end
local text = detailsFramework:Trim(textentry:GetText())
if (string.len(text) > 0) then
textentry.text = text
if (textentry.MyObject.func) then
--need to have a dispatch here
textentry.MyObject.func(textentry.MyObject.param1, textentry.MyObject.param2, text, textentry, byScript or textentry)
end
else
textentry:SetText("")
textentry.MyObject.currenttext = ""
end
if (not object.NoClearFocusOnEnterPressed) then
textentry.focuslost = true --quando estiver editando e clicar em outra caixa
textentry:ClearFocus()
if (textentry.MyObject.tab_on_enter and textentry.MyObject.next) then
textentry.MyObject.next:SetFocus()
end
end
end
local OnEscapePressed = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnEscapePressed", textentry, object, object.text)
if (kill) then
return
end
textentry.focuslost = true
textentry:ClearFocus()
end
local OnSpacePressed = function(textEntry)
local object = textEntry.MyObject
local kill = object:RunHooksForWidget("OnSpacePressed", textEntry, object)
if (kill) then
return
end
end
local OnEditFocusLost = function(textEntry)
local object = textEntry.MyObject
if (object.ignoreNextCallback) then
detailsFramework.Schedules.RunNextTick(function() object.ignoreNextCallback = nil end)
return
end
if (textEntry:IsShown()) then
local kill = object:RunHooksForWidget("OnEditFocusLost", textEntry, object, object.text)
if (kill) then
return
end
if (not textEntry.focuslost) then
local text = detailsFramework:Trim(textEntry:GetText())
if (string.len(text) > 0) then
textEntry.MyObject.currenttext = text
if (textEntry.MyObject.func) then
textEntry.MyObject.func(textEntry.MyObject.param1, textEntry.MyObject.param2, text, textEntry, nil)
end
else
textEntry:SetText("")
textEntry.MyObject.currenttext = ""
end
else
textEntry.focuslost = false
end
textEntry.MyObject.label:SetTextColor(.8, .8, .8, 1)
end
end
local OnEditFocusGained = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnEditFocusGained", textentry, object)
if (kill) then
return
end
textentry.MyObject.label:SetTextColor(1, 1, 1, 1)
if (object.autoSelectAllText) then
object:SelectAll()
end
end
local OnChar = function(textentry, char)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnChar", textentry, char, object)
if (kill) then
return
end
end
local OnTextChanged = function(textentry, byUser)
local capsule = textentry.MyObject
local kill = capsule:RunHooksForWidget("OnTextChanged", textentry, byUser, capsule)
if (kill) then
return
end
end
local OnTabPressed = function(textentry)
local capsule = textentry.MyObject
local bByUser = false
local kill = capsule:RunHooksForWidget("OnTabPressed", textentry, bByUser, capsule)
if (kill) then
return
end
if (textentry.MyObject.next) then
OnEnterPressed(textentry, false)
textentry.MyObject.next:SetFocus()
end
end
function TextEntryMetaFunctions:PressEnter(byScript)
OnEnterPressed(self.editbox, byScript)
end
---set the textEntry as a search box, it will add a magnifying glass icon on the left side and a clearSearchButton in the right.
function TextEntryMetaFunctions:SetAsSearchBox()
if (self.__bIsSearchBox) then
return
end
self:SetJustifyH("left")
self:SetJustifyV("middle")
self:SetTextInsets(18, 14, 0, 0)
local magnifyingGlassTexture = self:CreateTexture(nil, "OVERLAY")
magnifyingGlassTexture:SetAtlas("common-search-magnifyingglass")
magnifyingGlassTexture:SetPoint("left", self.widget, "left", 4, 0)
magnifyingGlassTexture:SetSize(self:GetHeight()-10, self:GetHeight()-10)
magnifyingGlassTexture:SetAlpha(0.5)
self.MagnifyingGlassTexture = magnifyingGlassTexture
local searchFontString = self:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
searchFontString:SetText("search")
searchFontString:SetAlpha(0.3)
searchFontString:SetPoint("left", magnifyingGlassTexture, "right", 2, 0)
detailsFramework:SetFontSize(searchFontString, 10)
self.SearchFontString = searchFontString
local clearSearchButton = CreateFrame("button", nil, self.widget, "UIPanelCloseButton")
clearSearchButton:SetPoint("right", self.widget, "right", -3, 0)
clearSearchButton:SetSize(10, 10)
clearSearchButton:SetAlpha(0.3)
clearSearchButton:GetNormalTexture():SetAtlas("common-search-clearbutton")
clearSearchButton:GetHighlightTexture():SetAtlas("common-search-clearbutton")
clearSearchButton:GetPushedTexture():SetAtlas("common-search-clearbutton")
clearSearchButton:Hide()
self.ClearSearchButton = clearSearchButton
clearSearchButton:SetScript("OnClick", function()
self:SetText("")
local bByScript = true
self:PressEnter(bByScript)
self:ClearFocus()
end)
self:SetHook("OnEditFocusGained", function()
clearSearchButton:Show()
searchFontString:Hide()
end)
self:SetHook("OnEditFocusLost", function()
if (self:GetText() == "") then
searchFontString:Show()
clearSearchButton:Hide()
end
end)
self.__bIsSearchBox = true
end
------------------------------------------------------------------------------------------------------------
function TextEntryMetaFunctions:SetTemplate(template)
template = detailsFramework:ParseTemplate(self.type, template) --"textentry"
if (template.multiline) then
self.editbox:SetMultiLine(true)
end
if (template.width) then
self.editbox:SetWidth(template.width)
end
if (template.height) then
self.editbox:SetHeight(template.height)
end
if (template.backdrop) then
self.editbox:SetBackdrop(template.backdrop)
end
if (template.backdropcolor) then
local r, g, b, a = detailsFramework:ParseColors(template.backdropcolor)
self.editbox:SetBackdropColor(r, g, b, a)
self.onleave_backdrop = {r, g, b, a}
end
if (template.backdropbordercolor) then
local r, g, b, a = detailsFramework:ParseColors(template.backdropbordercolor)
self.editbox:SetBackdropBorderColor(r, g, b, a)
self.editbox.current_bordercolor[1] = r
self.editbox.current_bordercolor[2] = g
self.editbox.current_bordercolor[3] = b
self.editbox.current_bordercolor[4] = a
self.onleave_backdrop_border_color = {r, g, b, a}
end
if (template.onentercolor) then
local r, g, b, a = detailsFramework:ParseColors(template.onentercolor)
self.onenter_backdrop = {r, g, b, a}
self:HookScript("OnEnter", detailsFramework.TemplateOnEnter)
self.__has_onentercolor_script = true
end
if (template.onleavecolor) then
local r, g, b, a = detailsFramework:ParseColors(template.onleavecolor)
self.onleave_backdrop = {r, g, b, a}
self:HookScript("OnLeave", detailsFramework.TemplateOnLeave)
self.__has_onleavecolor_script = true
end
if (template.onenterbordercolor) then
local r, g, b, a = detailsFramework:ParseColors(template.onenterbordercolor)
self.onenter_backdrop_border_color = {r, g, b, a}
if (not self.__has_onentercolor_script) then
self:HookScript("OnEnter", detailsFramework.TemplateOnEnter)
end
end
if (template.onleavebordercolor) then
local r, g, b, a = detailsFramework:ParseColors(template.onleavebordercolor)
self.onleave_backdrop_border_color = {r, g, b, a}
if (not self.__has_onleavecolor_script) then
self:HookScript("OnLeave", detailsFramework.TemplateOnLeave)
end
end
end
--TextEntryMetaFunctions.SetTemplate = DetailsFramework.SetTemplate
------------------------------------------------------------------------------------------------------------
--object constructor
---@class df_textentry : editbox
---@field widget editbox
---@field tooltip any
---@field show any
---@field hide any
---@field width any
---@field height any
---@field text any
---@field multiline any
---@field align any
---@field fontsize any
---@field param1 any
---@field param2 any
---@field ShouldOptimizeAutoComplete boolean?
---@field AutoComplete_StopOnEnterPress boolean?
---@field SetTemplate fun(self:df_textentry, template:table|string)
---@field Disable fun(self:df_textentry)
---@field Enable fun(self:df_textentry)
---@field SetCommitFunction fun(self:df_textentry, func:function)
---@field SetParameters fun(self:df_textentry, param1:any, param2:any)
---@field SetNext fun(self:df_textentry, next:df_textentry)
---@field SetLabelText fun(self:df_textentry, text:string)
---@field SelectAll fun(self:df_textentry)
---@field SetAutoSelectTextOnFocus fun(self:df_textentry, value:boolean)
---@field Blink fun(self:df_textentry)
---@field SetText fun(self:df_textentry, text:string|number)
---@field GetText fun(self:df_textentry)
---@field SetEnterFunction fun(self:df_textentry, func:function, param1:any, param2:any)
---@field SetHook fun(self:df_textentry, hookName:string, func:function)
---@field SetAsSearchBox fun(self:df_textentry)
---@field SetAsAutoComplete fun(self:df_textentry, poolName:string, poolTable:table?, shouldOptimize:boolean?) poolName is the name of the member on textEntry that will be used to store the pool table, poolTable is an array with word to be used on the autocomplete, shouldOptimize is a boolean that will optimize the autocomplete by using a cache table, it's recommended to use it if the autocomplete array is too large.
---@param parent frame
---@param textChangedCallback function
---@param width number
---@param height number
---@param member string?
---@param name string?
---@param labelText string?
---@param textentryTemplate table?
---@param labelTemplate table?
---@return df_textentry
function detailsFramework:CreateTextEntry(parent, textChangedCallback, width, height, member, name, labelText, textentryTemplate, labelTemplate)
---@diagnostic disable-next-line: return-type-mismatch
return detailsFramework:NewTextEntry(parent, parent, name, member, width, height, textChangedCallback, nil, nil, nil, labelText, textentryTemplate, labelTemplate)
end
function detailsFramework:NewTextEntry(parent, container, name, member, width, height, func, param1, param2, space, withLabel, entryTemplate, labelTemplate)
if (not name) then
name = "DetailsFrameworkTextEntryNumber" .. detailsFramework.TextEntryCounter
detailsFramework.TextEntryCounter = detailsFramework.TextEntryCounter + 1
elseif (not parent) then
return error("Details! FrameWork: parent not found.", 2)
end
if (not container) then
container = parent
end
if (name:find("$parent")) then
local parentName = detailsFramework:GetParentName(parent)
name = name:gsub("$parent", parentName)
end
local newTextEntryObject = {type = "textentry", dframework = true}
if (member) then
parent[member] = newTextEntryObject
end
if (parent.dframework) then
parent = parent.widget
end
if (container.dframework) then
container = container.widget
end
--misc
newTextEntryObject.container = container
if (not width and space) then
width = space
end
--editbox
newTextEntryObject.editbox = CreateFrame("EditBox", name, parent,"BackdropTemplate")
newTextEntryObject.editbox:SetSize(232, 20)
newTextEntryObject.editbox:SetBackdrop({bgFile = [["Interface\DialogFrame\UI-DialogBox-Background"]], tileSize = 64, tile = true, edgeFile = [[Interface\DialogFrame\UI-DialogBox-Border]], edgeSize = 10, insets = {left = 1, right = 1, top = 0, bottom = 0}})
newTextEntryObject.editbox:SetTextInsets(3, 0, 0, -3)
newTextEntryObject.editbox:SetWidth(width)
newTextEntryObject.editbox:SetHeight(height)
newTextEntryObject.editbox:SetJustifyH("center")
newTextEntryObject.editbox:EnableMouse(true)
newTextEntryObject.editbox:SetText("")
newTextEntryObject.editbox:SetAutoFocus(false)
newTextEntryObject.editbox:SetFontObject("GameFontHighlightSmall")
newTextEntryObject.editbox:SetJustifyH("left")
newTextEntryObject.editbox:SetTextInsets(5, 3, 0, 0)
--editbox label
newTextEntryObject.editbox.label = newTextEntryObject.editbox:CreateFontString("$parent_Desc", "OVERLAY", "GameFontHighlightSmall")
newTextEntryObject.editbox.label:SetJustifyH("left")
newTextEntryObject.editbox.label:SetPoint("RIGHT", newTextEntryObject.editbox, "LEFT", -2, 0)
newTextEntryObject.label = newTextEntryObject.editbox.label
newTextEntryObject.widget = newTextEntryObject.editbox
newTextEntryObject.editbox.MyObject = newTextEntryObject
if (not APITextEntryFunctions) then
APITextEntryFunctions = true
local idx = getmetatable(newTextEntryObject.editbox).__index
for funcName, funcAddress in pairs(idx) do
if (not TextEntryMetaFunctions[funcName]) then
TextEntryMetaFunctions[funcName] = function(object, ...)
local x = loadstring( "return _G['"..object.editbox:GetName().."']:"..funcName.."(...)")
return x(...)
end
end
end
end
newTextEntryObject.editbox.current_bordercolor = {1, 1, 1, 0.7}
newTextEntryObject.enabled_border_color = {newTextEntryObject.editbox:GetBackdropBorderColor()}
newTextEntryObject.enabled_backdrop_color = {newTextEntryObject.editbox:GetBackdropColor()}
newTextEntryObject.enabled_text_color = {newTextEntryObject.editbox:GetTextColor()}
newTextEntryObject.onleave_backdrop = {newTextEntryObject.editbox:GetBackdropColor()}
newTextEntryObject.onleave_backdrop_border_color = {newTextEntryObject.editbox:GetBackdropBorderColor()}
newTextEntryObject.func = func
newTextEntryObject.param1 = param1
newTextEntryObject.param2 = param2
newTextEntryObject.next = nil
newTextEntryObject.space = space
newTextEntryObject.tab_on_enter = false
newTextEntryObject.editbox:SetBackdrop({bgFile = [[Interface\Tooltips\UI-Tooltip-Background]], tileSize = 64, edgeFile = [[Interface\Buttons\WHITE8X8]], edgeSize = 1, insets = {left = 1, right = 1, top = 1, bottom = 1}})
newTextEntryObject.editbox:SetBackdropColor(.2, .2, .2, 1)
newTextEntryObject.editbox:SetBackdropBorderColor(1, 1, 1, 0.7)
--hooks
newTextEntryObject.HookList = {
OnEnter = {},
OnLeave = {},
OnHide = {},
OnShow = {},
OnEnterPressed = {},
OnEscapePressed = {},
OnSpacePressed = {},
OnEditFocusLost = {},
OnEditFocusGained = {},
OnChar = {},
OnTextChanged = {},
OnTabPressed = {},
}
newTextEntryObject.editbox:SetScript("OnEnter", OnEnter)
newTextEntryObject.editbox:SetScript("OnLeave", OnLeave)
newTextEntryObject.editbox:SetScript("OnHide", OnHide)
newTextEntryObject.editbox:SetScript("OnShow", OnShow)
newTextEntryObject.editbox:SetScript("OnEnterPressed", OnEnterPressed)
newTextEntryObject.editbox:SetScript("OnEscapePressed", OnEscapePressed)
newTextEntryObject.editbox:SetScript("OnSpacePressed", OnSpacePressed)
newTextEntryObject.editbox:SetScript("OnEditFocusLost", OnEditFocusLost)
newTextEntryObject.editbox:SetScript("OnEditFocusGained", OnEditFocusGained)
newTextEntryObject.editbox:SetScript("OnChar", OnChar)
newTextEntryObject.editbox:SetScript("OnTextChanged", OnTextChanged)
newTextEntryObject.editbox:SetScript("OnTabPressed", OnTabPressed)
setmetatable(newTextEntryObject, TextEntryMetaFunctions)
if (withLabel) then
local label = detailsFramework:CreateLabel(newTextEntryObject.editbox, withLabel, nil, nil, nil, "label", nil, "overlay")
label.text = withLabel
newTextEntryObject.editbox:SetPoint("left", label.widget, "right", 2, 0)
if (labelTemplate) then
label:SetTemplate(labelTemplate)
end
withLabel = label
end
if (entryTemplate) then
newTextEntryObject:SetTemplate(entryTemplate)
end
return newTextEntryObject, withLabel
end
---@class df_searchbox : df_textentry
---@field ClearSearchButton button
---@field MagnifyingGlassTexture texture
---@field SearchFontString fontstring
---@field BottomLineTexture texture
---@field PressEnter fun(self:df_searchbox)
---@field ClearFocus fun(self:df_searchbox)
---create a search box with no backdrop, a magnifying glass icon and a clear search button
---@param parent frame
---@param callback any
---@return df_searchbox
function detailsFramework:CreateSearchBox(parent, callback)
local onSearchPressEnterCallback = function(_, _, text, self)
callback(self)
end
local searchBox = detailsFramework:CreateTextEntry(parent, onSearchPressEnterCallback, 220, 26)
---@cast searchBox df_searchbox
searchBox:SetAsSearchBox()
searchBox:SetTextInsets(25, 5, 0, 0)
searchBox:SetBackdrop(nil)
searchBox:SetHook("OnTextChanged", callback)
local file, size, flags = searchBox:GetFont()
searchBox:SetFont(file, 12, flags)
searchBox.ClearSearchButton:SetAlpha(0)
searchBox.BottomLineTexture = searchBox:CreateTexture(nil, "border")
searchBox.BottomLineTexture:SetPoint("bottomleft", searchBox.widget, "bottomleft", -15, 0)
searchBox.BottomLineTexture:SetPoint("bottomright", searchBox.widget, "bottomright", 0, 0)
local bUseAtlasSize = false
searchBox.BottomLineTexture:SetAtlas("common-slider-track")
searchBox.BottomLineTexture:SetHeight(8)
--create the button to clear the search box
searchBox.ClearSearchButton = CreateFrame("button", nil, searchBox.widget, "UIPanelCloseButton")
searchBox.ClearSearchButton:SetPoint("right", searchBox.widget, "right", -3, 0)
searchBox.ClearSearchButton:SetSize(10, 10)
searchBox.ClearSearchButton:SetAlpha(0.3)
searchBox.ClearSearchButton:GetNormalTexture():SetAtlas("common-search-clearbutton")
searchBox.ClearSearchButton:GetHighlightTexture():SetAtlas("common-search-clearbutton")
searchBox.ClearSearchButton:GetPushedTexture():SetAtlas("common-search-clearbutton")
searchBox.ClearSearchButton:SetScript("OnClick", function()
searchBox:SetText("")
searchBox:PressEnter()
searchBox:ClearFocus()
end)
return searchBox
end
function detailsFramework:NewSpellEntry(parent, func, width, height, param1, param2, member, name)
local editbox = detailsFramework:NewTextEntry(parent, parent, name, member, width, height, func, param1, param2)
return editbox
end
local function_gettext = function(self)
return self.editbox:GetText()
end
local function_settext = function(self, text)
return self.editbox:SetText(text)
end
local function_clearfocus = function(self)
return self.editbox:ClearFocus()
end
local function_setfocus = function(self)
return self.editbox:SetFocus(true)
end
local get_last_word = function(self)
self.lastword = ""
local cursor_pos = self.editbox:GetCursorPosition()
local text = self.editbox:GetText()
for i = cursor_pos, 1, -1 do
local character = text:sub (i, i)
if (character:match ("%a")) then
self.lastword = character .. self.lastword
--print(self.lastword)
else
break
end
end
end
--On Text Changed
local AutoComplete_OnTextChanged = function(editboxWidget, byUser, capsule)
capsule = capsule or editboxWidget.MyObject or editboxWidget
local chars_now = editboxWidget:GetText():len()
if (not editboxWidget.ignore_textchange) then
--backspace
if (chars_now == capsule.characters_count -1) then
capsule.lastword = capsule.lastword:sub (1, capsule.lastword:len()-1)
--delete lots of text
elseif (chars_now < capsule.characters_count) then
--o auto complete selecionou outra palavra bem menor e caiu nesse filtro
editboxWidget.end_selection = nil
capsule:GetLastWord()
end
else
editboxWidget.ignore_textchange = nil
end
capsule.characters_count = chars_now
--call the other hooks for the widget
capsule:RunHooksForWidget("OnTextChanged", editboxWidget, byUser, capsule)
end
local AutoComplete_OnSpacePressed = function(editboxWidget, capsule)
capsule = capsule or editboxWidget.MyObject or editboxWidget
-- if (not gotMatch) then
--editboxWidget.end_selection = nil
-- end
end
local AutoComplete_OnEscapePressed = function(editboxWidget)
editboxWidget.end_selection = nil
end
local AutoComplete_OnEnterPressed = function(editboxWidget)
local capsule = editboxWidget.MyObject or editboxWidget
if (editboxWidget.end_selection) then
editboxWidget:SetCursorPosition (editboxWidget.end_selection)
editboxWidget:HighlightText (0, 0)
editboxWidget.end_selection = nil
--editboxWidget:Insert (" ") --estava causando a adi��o de uma palavra a mais quando o pr�ximo catactere for um espa�o
else
if (editboxWidget:IsMultiLine()) then
editboxWidget:Insert ("\n")
--reseta a palavra se acabou de ganhar focus e apertou enter
if (editboxWidget.focusGained) then
capsule.lastword = ""
editboxWidget.focusGained = nil
end
else
editboxWidget:Insert ("")
editboxWidget.focuslost = true
editboxWidget:ClearFocus()
end
end
capsule.lastword = ""
if (capsule.AutoComplete_StopOnEnterPress) then
editboxWidget:ClearFocus()
end
end
local AutoComplete_OnEditFocusGained = function(editboxWidget)
local capsule = editboxWidget.MyObject or editboxWidget
capsule:GetLastWord()
--print("last word:", editboxWidget.lastword)
editboxWidget.end_selection = nil
editboxWidget.focusGained = true
capsule.characters_count = editboxWidget:GetText():len()
end
local OptimizeAutoCompleteTable = function(self, wordList)
local optimizedTable = {}
local lower = string.lower
local sub = string.sub
local len = string.len
local subTables = 0
for i = 1, #wordList do
local thisWord = wordList [i]
if (len (thisWord) > 0) then
thisWord = lower (thisWord)
local firstCharacter = sub (thisWord, 1, 1)
local charTable = optimizedTable [firstCharacter]
if (not charTable) then
charTable = {}
optimizedTable [firstCharacter] = charTable
subTables = subTables + 1
end
charTable [#charTable+1] = thisWord
end
end
wordList.Optimized = optimizedTable
end
local AutoComplete_OnChar = function(editboxWidget, char, capsule)
if (char == "") then
return