yellowfive@57
|
1 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
2 EditBox Widget
|
yellowfive@57
|
3 -------------------------------------------------------------------------------]]
|
yellowfive@124
|
4 local Type, Version = "EditBox", 28
|
yellowfive@57
|
5 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
|
yellowfive@57
|
6 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
|
yellowfive@57
|
7
|
yellowfive@57
|
8 -- Lua APIs
|
yellowfive@57
|
9 local tostring, pairs = tostring, pairs
|
yellowfive@57
|
10
|
yellowfive@57
|
11 -- WoW APIs
|
yellowfive@57
|
12 local PlaySound = PlaySound
|
yellowfive@57
|
13 local GetCursorInfo, ClearCursor, GetSpellInfo = GetCursorInfo, ClearCursor, GetSpellInfo
|
yellowfive@57
|
14 local CreateFrame, UIParent = CreateFrame, UIParent
|
yellowfive@57
|
15 local _G = _G
|
yellowfive@57
|
16
|
yellowfive@57
|
17 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
|
yellowfive@57
|
18 -- List them here for Mikk's FindGlobals script
|
yellowfive@57
|
19 -- GLOBALS: AceGUIEditBoxInsertLink, ChatFontNormal, OKAY
|
yellowfive@57
|
20
|
yellowfive@57
|
21 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
22 Support functions
|
yellowfive@57
|
23 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
24 if not AceGUIEditBoxInsertLink then
|
yellowfive@57
|
25 -- upgradeable hook
|
yellowfive@57
|
26 hooksecurefunc("ChatEdit_InsertLink", function(...) return _G.AceGUIEditBoxInsertLink(...) end)
|
yellowfive@57
|
27 end
|
yellowfive@57
|
28
|
yellowfive@57
|
29 function _G.AceGUIEditBoxInsertLink(text)
|
yellowfive@57
|
30 for i = 1, AceGUI:GetWidgetCount(Type) do
|
yellowfive@57
|
31 local editbox = _G["AceGUI-3.0EditBox"..i]
|
yellowfive@57
|
32 if editbox and editbox:IsVisible() and editbox:HasFocus() then
|
yellowfive@57
|
33 editbox:Insert(text)
|
yellowfive@57
|
34 return true
|
yellowfive@57
|
35 end
|
yellowfive@57
|
36 end
|
yellowfive@57
|
37 end
|
yellowfive@57
|
38
|
yellowfive@57
|
39 local function ShowButton(self)
|
yellowfive@57
|
40 if not self.disablebutton then
|
yellowfive@57
|
41 self.button:Show()
|
yellowfive@57
|
42 self.editbox:SetTextInsets(0, 20, 3, 3)
|
yellowfive@57
|
43 end
|
yellowfive@57
|
44 end
|
yellowfive@57
|
45
|
yellowfive@57
|
46 local function HideButton(self)
|
yellowfive@57
|
47 self.button:Hide()
|
yellowfive@57
|
48 self.editbox:SetTextInsets(0, 0, 3, 3)
|
yellowfive@57
|
49 end
|
yellowfive@57
|
50
|
yellowfive@57
|
51 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
52 Scripts
|
yellowfive@57
|
53 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
54 local function Control_OnEnter(frame)
|
yellowfive@57
|
55 frame.obj:Fire("OnEnter")
|
yellowfive@57
|
56 end
|
yellowfive@57
|
57
|
yellowfive@57
|
58 local function Control_OnLeave(frame)
|
yellowfive@57
|
59 frame.obj:Fire("OnLeave")
|
yellowfive@57
|
60 end
|
yellowfive@57
|
61
|
yellowfive@57
|
62 local function Frame_OnShowFocus(frame)
|
yellowfive@57
|
63 frame.obj.editbox:SetFocus()
|
yellowfive@57
|
64 frame:SetScript("OnShow", nil)
|
yellowfive@57
|
65 end
|
yellowfive@57
|
66
|
yellowfive@57
|
67 local function EditBox_OnEscapePressed(frame)
|
yellowfive@57
|
68 AceGUI:ClearFocus()
|
yellowfive@57
|
69 end
|
yellowfive@57
|
70
|
yellowfive@57
|
71 local function EditBox_OnEnterPressed(frame)
|
yellowfive@57
|
72 local self = frame.obj
|
yellowfive@57
|
73 local value = frame:GetText()
|
yellowfive@57
|
74 local cancel = self:Fire("OnEnterPressed", value)
|
yellowfive@57
|
75 if not cancel then
|
yellowfive@124
|
76 PlaySound(856) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON
|
yellowfive@57
|
77 HideButton(self)
|
yellowfive@57
|
78 end
|
yellowfive@57
|
79 end
|
yellowfive@57
|
80
|
yellowfive@57
|
81 local function EditBox_OnReceiveDrag(frame)
|
yellowfive@57
|
82 local self = frame.obj
|
yellowfive@57
|
83 local type, id, info = GetCursorInfo()
|
yellowfive@124
|
84 local name
|
yellowfive@57
|
85 if type == "item" then
|
yellowfive@124
|
86 name = info
|
yellowfive@57
|
87 elseif type == "spell" then
|
yellowfive@124
|
88 name = GetSpellInfo(id, info)
|
yellowfive@124
|
89 elseif type == "macro" then
|
yellowfive@124
|
90 name = GetMacroInfo(id)
|
yellowfive@124
|
91 end
|
yellowfive@124
|
92 if name then
|
yellowfive@57
|
93 self:SetText(name)
|
yellowfive@57
|
94 self:Fire("OnEnterPressed", name)
|
yellowfive@57
|
95 ClearCursor()
|
yellowfive@124
|
96 HideButton(self)
|
yellowfive@124
|
97 AceGUI:ClearFocus()
|
yellowfive@57
|
98 end
|
yellowfive@57
|
99 end
|
yellowfive@57
|
100
|
yellowfive@57
|
101 local function EditBox_OnTextChanged(frame)
|
yellowfive@57
|
102 local self = frame.obj
|
yellowfive@57
|
103 local value = frame:GetText()
|
yellowfive@57
|
104 if tostring(value) ~= tostring(self.lasttext) then
|
yellowfive@57
|
105 self:Fire("OnTextChanged", value)
|
yellowfive@57
|
106 self.lasttext = value
|
yellowfive@57
|
107 ShowButton(self)
|
yellowfive@57
|
108 end
|
yellowfive@57
|
109 end
|
yellowfive@57
|
110
|
yellowfive@57
|
111 local function EditBox_OnFocusGained(frame)
|
yellowfive@57
|
112 AceGUI:SetFocus(frame.obj)
|
yellowfive@57
|
113 end
|
yellowfive@57
|
114
|
yellowfive@57
|
115 local function Button_OnClick(frame)
|
yellowfive@57
|
116 local editbox = frame.obj.editbox
|
yellowfive@57
|
117 editbox:ClearFocus()
|
yellowfive@57
|
118 EditBox_OnEnterPressed(editbox)
|
yellowfive@57
|
119 end
|
yellowfive@57
|
120
|
yellowfive@57
|
121 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
122 Methods
|
yellowfive@57
|
123 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
124 local methods = {
|
yellowfive@57
|
125 ["OnAcquire"] = function(self)
|
yellowfive@57
|
126 -- height is controlled by SetLabel
|
yellowfive@57
|
127 self:SetWidth(200)
|
yellowfive@57
|
128 self:SetDisabled(false)
|
yellowfive@57
|
129 self:SetLabel()
|
yellowfive@57
|
130 self:SetText()
|
yellowfive@57
|
131 self:DisableButton(false)
|
yellowfive@57
|
132 self:SetMaxLetters(0)
|
yellowfive@57
|
133 end,
|
yellowfive@57
|
134
|
yellowfive@57
|
135 ["OnRelease"] = function(self)
|
yellowfive@57
|
136 self:ClearFocus()
|
yellowfive@57
|
137 end,
|
yellowfive@57
|
138
|
yellowfive@57
|
139 ["SetDisabled"] = function(self, disabled)
|
yellowfive@57
|
140 self.disabled = disabled
|
yellowfive@57
|
141 if disabled then
|
yellowfive@57
|
142 self.editbox:EnableMouse(false)
|
yellowfive@57
|
143 self.editbox:ClearFocus()
|
yellowfive@57
|
144 self.editbox:SetTextColor(0.5,0.5,0.5)
|
yellowfive@57
|
145 self.label:SetTextColor(0.5,0.5,0.5)
|
yellowfive@57
|
146 else
|
yellowfive@57
|
147 self.editbox:EnableMouse(true)
|
yellowfive@57
|
148 self.editbox:SetTextColor(1,1,1)
|
yellowfive@57
|
149 self.label:SetTextColor(1,.82,0)
|
yellowfive@57
|
150 end
|
yellowfive@57
|
151 end,
|
yellowfive@57
|
152
|
yellowfive@57
|
153 ["SetText"] = function(self, text)
|
yellowfive@57
|
154 self.lasttext = text or ""
|
yellowfive@57
|
155 self.editbox:SetText(text or "")
|
yellowfive@57
|
156 self.editbox:SetCursorPosition(0)
|
yellowfive@57
|
157 HideButton(self)
|
yellowfive@57
|
158 end,
|
yellowfive@57
|
159
|
yellowfive@57
|
160 ["GetText"] = function(self, text)
|
yellowfive@57
|
161 return self.editbox:GetText()
|
yellowfive@57
|
162 end,
|
yellowfive@57
|
163
|
yellowfive@57
|
164 ["SetLabel"] = function(self, text)
|
yellowfive@57
|
165 if text and text ~= "" then
|
yellowfive@57
|
166 self.label:SetText(text)
|
yellowfive@57
|
167 self.label:Show()
|
yellowfive@57
|
168 self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,-18)
|
yellowfive@57
|
169 self:SetHeight(44)
|
yellowfive@57
|
170 self.alignoffset = 30
|
yellowfive@57
|
171 else
|
yellowfive@57
|
172 self.label:SetText("")
|
yellowfive@57
|
173 self.label:Hide()
|
yellowfive@57
|
174 self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,0)
|
yellowfive@57
|
175 self:SetHeight(26)
|
yellowfive@57
|
176 self.alignoffset = 12
|
yellowfive@57
|
177 end
|
yellowfive@57
|
178 end,
|
yellowfive@57
|
179
|
yellowfive@57
|
180 ["DisableButton"] = function(self, disabled)
|
yellowfive@57
|
181 self.disablebutton = disabled
|
yellowfive@57
|
182 if disabled then
|
yellowfive@57
|
183 HideButton(self)
|
yellowfive@57
|
184 end
|
yellowfive@57
|
185 end,
|
yellowfive@57
|
186
|
yellowfive@57
|
187 ["SetMaxLetters"] = function (self, num)
|
yellowfive@57
|
188 self.editbox:SetMaxLetters(num or 0)
|
yellowfive@57
|
189 end,
|
yellowfive@57
|
190
|
yellowfive@57
|
191 ["ClearFocus"] = function(self)
|
yellowfive@57
|
192 self.editbox:ClearFocus()
|
yellowfive@57
|
193 self.frame:SetScript("OnShow", nil)
|
yellowfive@57
|
194 end,
|
yellowfive@57
|
195
|
yellowfive@57
|
196 ["SetFocus"] = function(self)
|
yellowfive@57
|
197 self.editbox:SetFocus()
|
yellowfive@57
|
198 if not self.frame:IsShown() then
|
yellowfive@57
|
199 self.frame:SetScript("OnShow", Frame_OnShowFocus)
|
yellowfive@57
|
200 end
|
yellowfive@106
|
201 end,
|
yellowfive@106
|
202
|
yellowfive@106
|
203 ["HighlightText"] = function(self, from, to)
|
yellowfive@106
|
204 self.editbox:HighlightText(from, to)
|
yellowfive@57
|
205 end
|
yellowfive@57
|
206 }
|
yellowfive@57
|
207
|
yellowfive@57
|
208 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
209 Constructor
|
yellowfive@57
|
210 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
211 local function Constructor()
|
yellowfive@57
|
212 local num = AceGUI:GetNextWidgetNum(Type)
|
yellowfive@57
|
213 local frame = CreateFrame("Frame", nil, UIParent)
|
yellowfive@57
|
214 frame:Hide()
|
yellowfive@57
|
215
|
yellowfive@57
|
216 local editbox = CreateFrame("EditBox", "AceGUI-3.0EditBox"..num, frame, "InputBoxTemplate")
|
yellowfive@57
|
217 editbox:SetAutoFocus(false)
|
yellowfive@57
|
218 editbox:SetFontObject(ChatFontNormal)
|
yellowfive@57
|
219 editbox:SetScript("OnEnter", Control_OnEnter)
|
yellowfive@57
|
220 editbox:SetScript("OnLeave", Control_OnLeave)
|
yellowfive@57
|
221 editbox:SetScript("OnEscapePressed", EditBox_OnEscapePressed)
|
yellowfive@57
|
222 editbox:SetScript("OnEnterPressed", EditBox_OnEnterPressed)
|
yellowfive@57
|
223 editbox:SetScript("OnTextChanged", EditBox_OnTextChanged)
|
yellowfive@57
|
224 editbox:SetScript("OnReceiveDrag", EditBox_OnReceiveDrag)
|
yellowfive@57
|
225 editbox:SetScript("OnMouseDown", EditBox_OnReceiveDrag)
|
yellowfive@57
|
226 editbox:SetScript("OnEditFocusGained", EditBox_OnFocusGained)
|
yellowfive@57
|
227 editbox:SetTextInsets(0, 0, 3, 3)
|
yellowfive@57
|
228 editbox:SetMaxLetters(256)
|
yellowfive@57
|
229 editbox:SetPoint("BOTTOMLEFT", 6, 0)
|
yellowfive@57
|
230 editbox:SetPoint("BOTTOMRIGHT")
|
yellowfive@57
|
231 editbox:SetHeight(19)
|
yellowfive@57
|
232
|
yellowfive@57
|
233 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
|
yellowfive@57
|
234 label:SetPoint("TOPLEFT", 0, -2)
|
yellowfive@57
|
235 label:SetPoint("TOPRIGHT", 0, -2)
|
yellowfive@57
|
236 label:SetJustifyH("LEFT")
|
yellowfive@57
|
237 label:SetHeight(18)
|
yellowfive@57
|
238
|
yellowfive@57
|
239 local button = CreateFrame("Button", nil, editbox, "UIPanelButtonTemplate")
|
yellowfive@57
|
240 button:SetWidth(40)
|
yellowfive@57
|
241 button:SetHeight(20)
|
yellowfive@57
|
242 button:SetPoint("RIGHT", -2, 0)
|
yellowfive@57
|
243 button:SetText(OKAY)
|
yellowfive@57
|
244 button:SetScript("OnClick", Button_OnClick)
|
yellowfive@57
|
245 button:Hide()
|
yellowfive@57
|
246
|
yellowfive@57
|
247 local widget = {
|
yellowfive@57
|
248 alignoffset = 30,
|
yellowfive@57
|
249 editbox = editbox,
|
yellowfive@57
|
250 label = label,
|
yellowfive@57
|
251 button = button,
|
yellowfive@57
|
252 frame = frame,
|
yellowfive@57
|
253 type = Type
|
yellowfive@57
|
254 }
|
yellowfive@57
|
255 for method, func in pairs(methods) do
|
yellowfive@57
|
256 widget[method] = func
|
yellowfive@57
|
257 end
|
yellowfive@57
|
258 editbox.obj, button.obj = widget, widget
|
yellowfive@57
|
259
|
yellowfive@57
|
260 return AceGUI:RegisterAsWidget(widget)
|
yellowfive@57
|
261 end
|
yellowfive@57
|
262
|
yellowfive@57
|
263 AceGUI:RegisterWidgetType(Type, Constructor, Version)
|