annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-EditBox.lua @ 23:52973d00a183

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