annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-EditBox.lua @ 11:371e14cd2feb

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