comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-EditBox.lua @ 0:c6ff7ba0e8f6

Reasonably functional now. Cleaning up some stuff which might have to be reverted.
author Zerotorescue
date Thu, 07 Oct 2010 17:17:43 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c6ff7ba0e8f6
1 --[[-----------------------------------------------------------------------------
2 EditBox Widget
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "EditBox", 21
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, GetSpellName = GetCursorInfo, ClearCursor, GetSpellName
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 EditBox_OnEscapePressed(frame)
63 AceGUI:ClearFocus()
64 end
65
66 local function EditBox_OnEnterPressed(frame)
67 local self = frame.obj
68 local value = frame:GetText()
69 local cancel = self:Fire("OnEnterPressed", value)
70 if not cancel then
71 PlaySound("igMainMenuOptionCheckBoxOn")
72 HideButton(self)
73 end
74 end
75
76 local function EditBox_OnReceiveDrag(frame)
77 local self = frame.obj
78 local type, id, info = GetCursorInfo()
79 if type == "item" then
80 self:SetText(info)
81 self:Fire("OnEnterPressed", info)
82 ClearCursor()
83 elseif type == "spell" then
84 local name, rank = GetSpellName(id, info)
85 if rank and rank:match("%d") then
86 name = name.."("..rank..")"
87 end
88 self:SetText(name)
89 self:Fire("OnEnterPressed", name)
90 ClearCursor()
91 end
92 HideButton(self)
93 AceGUI:ClearFocus()
94 end
95
96 local function EditBox_OnTextChanged(frame)
97 local self = frame.obj
98 local value = frame:GetText()
99 if tostring(value) ~= tostring(self.lasttext) then
100 self:Fire("OnTextChanged", value)
101 self.lasttext = value
102 ShowButton(self)
103 end
104 end
105
106 local function Button_OnClick(frame)
107 local editbox = frame.obj.editbox
108 editbox:ClearFocus()
109 EditBox_OnEnterPressed(editbox)
110 end
111
112 --[[-----------------------------------------------------------------------------
113 Methods
114 -------------------------------------------------------------------------------]]
115 local methods = {
116 ["OnAcquire"] = function(self)
117 -- height is controlled by SetLabel
118 self:SetWidth(200)
119 self:SetDisabled(false)
120 self:SetLabel()
121 self:SetText()
122 self:DisableButton(false)
123 self:SetMaxLetters(0)
124 end,
125
126 -- ["OnRelease"] = nil,
127
128 ["SetDisabled"] = function(self, disabled)
129 self.disabled = disabled
130 if disabled then
131 self.editbox:EnableMouse(false)
132 self.editbox:ClearFocus()
133 self.editbox:SetTextColor(0.5,0.5,0.5)
134 self.label:SetTextColor(0.5,0.5,0.5)
135 else
136 self.editbox:EnableMouse(true)
137 self.editbox:SetTextColor(1,1,1)
138 self.label:SetTextColor(1,.82,0)
139 end
140 end,
141
142 ["SetText"] = function(self, text)
143 self.lasttext = text or ""
144 self.editbox:SetText(text or "")
145 self.editbox:SetCursorPosition(0)
146 HideButton(self)
147 end,
148
149 ["SetLabel"] = function(self, text)
150 if text and text ~= "" then
151 self.label:SetText(text)
152 self.label:Show()
153 self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,-18)
154 self:SetHeight(44)
155 self.alignoffset = 30
156 else
157 self.label:SetText("")
158 self.label:Hide()
159 self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,0)
160 self:SetHeight(26)
161 self.alignoffset = 12
162 end
163 end,
164
165 ["DisableButton"] = function(self, disabled)
166 self.disablebutton = disabled
167 end,
168
169 ["SetMaxLetters"] = function (self, num)
170 self.editbox:SetMaxLetters(num or 0)
171 end
172 }
173
174 --[[-----------------------------------------------------------------------------
175 Constructor
176 -------------------------------------------------------------------------------]]
177 local function Constructor()
178 local num = AceGUI:GetNextWidgetNum(Type)
179 local frame = CreateFrame("Frame", nil, UIParent)
180 frame:Hide()
181
182 local editbox = CreateFrame("EditBox", "AceGUI-3.0EditBox"..num, frame, "InputBoxTemplate")
183 editbox:SetAutoFocus(false)
184 editbox:SetFontObject(ChatFontNormal)
185 editbox:SetScript("OnEnter", Control_OnEnter)
186 editbox:SetScript("OnLeave", Control_OnLeave)
187 editbox:SetScript("OnEscapePressed", EditBox_OnEscapePressed)
188 editbox:SetScript("OnEnterPressed", EditBox_OnEnterPressed)
189 editbox:SetScript("OnTextChanged", EditBox_OnTextChanged)
190 editbox:SetScript("OnReceiveDrag", EditBox_OnReceiveDrag)
191 editbox:SetScript("OnMouseDown", EditBox_OnReceiveDrag)
192 editbox:SetTextInsets(0, 0, 3, 3)
193 editbox:SetMaxLetters(256)
194 editbox:SetPoint("BOTTOMLEFT", 6, 0)
195 editbox:SetPoint("BOTTOMRIGHT")
196 editbox:SetHeight(19)
197
198 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
199 label:SetPoint("TOPLEFT", 0, -2)
200 label:SetPoint("TOPRIGHT", 0, -2)
201 label:SetJustifyH("LEFT")
202 label:SetHeight(18)
203
204 local button = CreateFrame("Button", nil, editbox, "UIPanelButtonTemplate")
205 button:SetWidth(40)
206 button:SetHeight(20)
207 button:SetPoint("RIGHT", -2, 0)
208 button:SetText(OKAY)
209 button:SetScript("OnClick", Button_OnClick)
210 button:Hide()
211
212 local widget = {
213 alignoffset = 30,
214 editbox = editbox,
215 label = label,
216 button = button,
217 frame = frame,
218 type = Type
219 }
220 for method, func in pairs(methods) do
221 widget[method] = func
222 end
223 editbox.obj, button.obj = widget, widget
224
225 return AceGUI:RegisterAsWidget(widget)
226 end
227
228 AceGUI:RegisterWidgetType(Type, Constructor, Version)