comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-EditBox.lua @ 57:01b63b8ed811 v21

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