annotate ui/AmrUiTextarea.lua @ 61:cf2b6b9a8337 v23

6.2 update, shopping list bug fixes, ui scale option
author yellowfive
date Tue, 23 Jun 2015 00:27:21 -0700
parents 01b63b8ed811
children 0515882856f1
rev   line source
yellowfive@57 1 --[[-----------------------------------------------------------------------------
yellowfive@57 2 Textarea Widget
yellowfive@57 3 -------------------------------------------------------------------------------]]
yellowfive@57 4 local Type, Version = "AmrUiTextarea", 1
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 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot")
yellowfive@57 9
yellowfive@57 10 --[[-----------------------------------------------------------------------------
yellowfive@57 11 Scripts
yellowfive@57 12 -------------------------------------------------------------------------------]]
yellowfive@57 13
yellowfive@57 14 -- handles clicking in the scrollframe but below the bounds of the editbox (which can't be sized to same as scrollframe)
yellowfive@57 15 local function scrollFrameMouseUp(self)
yellowfive@57 16 local editbox = self.obj.editbox
yellowfive@57 17 editbox:SetFocus()
yellowfive@57 18 editbox:SetCursorPosition(editbox:GetNumLetters())
yellowfive@57 19 end
yellowfive@57 20
yellowfive@57 21 local function scrollFrameVerticalScroll(self, offset)
yellowfive@57 22 local editbox = self.obj.editbox
yellowfive@57 23 editbox:SetHitRectInsets(0, 0, offset, editbox:GetHeight() - offset - self:GetHeight())
yellowfive@57 24 end
yellowfive@57 25
yellowfive@57 26 local function editboxCursorChanged(self, _, y, _, cursorHeight)
yellowfive@57 27 self, y = self.obj.scrollFrame, -y
yellowfive@57 28 local offset = self:GetVerticalScroll()
yellowfive@57 29 if y < offset then
yellowfive@57 30 self:SetVerticalScroll(y)
yellowfive@57 31 else
yellowfive@57 32 y = y + cursorHeight - self:GetHeight()
yellowfive@57 33 if y > offset then
yellowfive@57 34 self:SetVerticalScroll(y)
yellowfive@57 35 end
yellowfive@57 36 end
yellowfive@57 37 end
yellowfive@57 38
yellowfive@57 39 local function editboxEditFocusLost(self)
yellowfive@57 40 self:HighlightText(0, 0)
yellowfive@57 41 self.obj:Fire("OnEditFocusLost")
yellowfive@57 42 end
yellowfive@57 43
yellowfive@57 44 local function editboxEditFocusGained(frame)
yellowfive@57 45 AceGUI:SetFocus(frame.obj)
yellowfive@57 46 frame.obj:Fire("OnEditFocusGained")
yellowfive@57 47 end
yellowfive@57 48
yellowfive@57 49 local function editboxTextChanged(self, userInput)
yellowfive@57 50 if userInput then
yellowfive@57 51 self = self.obj
yellowfive@57 52 self:Fire("OnTextChanged", self.editbox:GetText())
yellowfive@57 53 end
yellowfive@57 54 end
yellowfive@57 55
yellowfive@57 56 local function editboxTextSet(self)
yellowfive@57 57 self:HighlightText()
yellowfive@57 58 self:SetCursorPosition(self:GetNumLetters())
yellowfive@57 59 self = self.obj
yellowfive@57 60 self:Fire("OnTextSet", self.editbox:GetText())
yellowfive@57 61 end
yellowfive@57 62
yellowfive@61 63 local function editboxEnterPressed(self)
yellowfive@61 64 self.obj:Fire("OnEnterPressed")
yellowfive@61 65 end
yellowfive@61 66
yellowfive@57 67 -- works for both the scrollframe and the editbox, handles e.g. dragging a spell link into the textarea
yellowfive@57 68 local function onReceiveDrag(self)
yellowfive@57 69 local type, id, info = GetCursorInfo()
yellowfive@57 70 if type == "spell" then
yellowfive@57 71 info = GetSpellInfo(id, info)
yellowfive@57 72 elseif type ~= "item" then
yellowfive@57 73 return
yellowfive@57 74 end
yellowfive@57 75 ClearCursor()
yellowfive@57 76 self = self.obj
yellowfive@57 77 local editbox = self.editbox
yellowfive@57 78 if not editbox:HasFocus() then
yellowfive@57 79 editbox:SetFocus()
yellowfive@57 80 editbox:SetCursorPosition(editbox:GetNumLetters())
yellowfive@57 81 end
yellowfive@57 82 editbox:Insert(info)
yellowfive@57 83 end
yellowfive@57 84
yellowfive@57 85 --[[-----------------------------------------------------------------------------
yellowfive@57 86 Methods
yellowfive@57 87 -------------------------------------------------------------------------------]]
yellowfive@57 88 local methods = {
yellowfive@57 89 ["OnAcquire"] = function(self)
yellowfive@57 90 -- restore default values
yellowfive@57 91 self:SetWidth(200)
yellowfive@57 92 self:SetHeight(24)
yellowfive@61 93 self:SetMultiLine(true)
yellowfive@57 94 self:SetFont(Amr.CreateFont("Regular", 16, Amr.Colors.Text))
yellowfive@57 95 self:SetText("")
yellowfive@57 96 self.frame:ClearAllPoints()
yellowfive@57 97 end,
yellowfive@57 98
yellowfive@57 99 ["SetText"] = function(self, text)
yellowfive@57 100 self.editbox:SetText(text)
yellowfive@57 101 end,
yellowfive@57 102
yellowfive@57 103 ["GetText"] = function(self)
yellowfive@57 104 return self.editbox:GetText()
yellowfive@57 105 end,
yellowfive@57 106
yellowfive@57 107 ["SetFont"] = function(self, font)
yellowfive@57 108 self.editbox:SetFontObject(font)
yellowfive@57 109 end,
yellowfive@57 110
yellowfive@57 111 ["SetDisabled"] = function(self, disabled)
yellowfive@57 112 self.disabled = disabled
yellowfive@57 113 if disabled then
yellowfive@57 114 self.editbox:Disable()
yellowfive@57 115 else
yellowfive@57 116 self.editbox:Enable()
yellowfive@57 117 end
yellowfive@57 118 end,
yellowfive@57 119
yellowfive@57 120 ["OnWidthSet"] = function(self, width)
yellowfive@57 121 self.editbox:SetWidth(width)
yellowfive@57 122 end,
yellowfive@57 123
yellowfive@57 124 ["OnHeightSet"] = function(self, height)
yellowfive@57 125 self.editbox:SetHeight(height)
yellowfive@57 126 end,
yellowfive@57 127
yellowfive@57 128 ["ClearFocus"] = function(self)
yellowfive@57 129 self.editbox:ClearFocus()
yellowfive@61 130 self.editbox:HighlightText(0, 0)
yellowfive@57 131 self.frame:SetScript("OnShow", nil)
yellowfive@57 132 end,
yellowfive@61 133
yellowfive@61 134 ["SetMultiLine"] = function(self, multi)
yellowfive@61 135 self.editbox:SetMultiLine(multi)
yellowfive@61 136 end,
yellowfive@57 137
yellowfive@57 138 ["SetFocus"] = function(self, highlight)
yellowfive@57 139 self.editbox:SetFocus()
yellowfive@57 140 if highlight then
yellowfive@57 141 self.editbox:HighlightText()
yellowfive@57 142 end
yellowfive@57 143 if not self.frame:IsShown() then
yellowfive@57 144 self.frame:SetScript("OnShow", function(frame)
yellowfive@57 145 frame.obj.editbox:SetFocus()
yellowfive@57 146 if highlight then
yellowfive@57 147 self.editbox:HighlightText()
yellowfive@57 148 end
yellowfive@57 149 frame:SetScript("OnShow", nil)
yellowfive@57 150 end)
yellowfive@57 151 end
yellowfive@57 152 end
yellowfive@57 153 }
yellowfive@57 154
yellowfive@57 155 --[[-----------------------------------------------------------------------------
yellowfive@57 156 Constructor
yellowfive@57 157 -------------------------------------------------------------------------------]]
yellowfive@57 158 local function Constructor()
yellowfive@57 159 local name = "AmrUiTextarea" .. AceGUI:GetNextWidgetNum(Type)
yellowfive@57 160 local frame = CreateFrame("ScrollFrame", name, UIParent)
yellowfive@57 161 frame:Hide()
yellowfive@57 162
yellowfive@57 163 frame:SetScript("OnMouseUp", scrollFrameMouseUp)
yellowfive@57 164 frame:SetScript("OnReceiveDrag", onReceiveDrag)
yellowfive@57 165 frame:HookScript("OnVerticalScroll", scrollFrameVerticalScroll)
yellowfive@57 166
yellowfive@57 167 local editbox = CreateFrame("EditBox", name .. "Edit", frame)
yellowfive@57 168 editbox:SetAllPoints()
yellowfive@57 169 editbox:EnableMouse(true)
yellowfive@57 170 editbox:SetMultiLine(true)
yellowfive@57 171 editbox:SetAutoFocus(false)
yellowfive@57 172 editbox:SetCountInvisibleLetters(false)
yellowfive@57 173 editbox:SetTextInsets(4, 4, 4, 4)
yellowfive@57 174
yellowfive@57 175 editbox:SetScript("OnMouseDown", onReceiveDrag)
yellowfive@57 176 editbox:SetScript("OnReceiveDrag", onReceiveDrag)
yellowfive@57 177
yellowfive@57 178 editbox:SetScript("OnCursorChanged", editboxCursorChanged)
yellowfive@57 179 editbox:SetScript("OnEscapePressed", editbox.ClearFocus)
yellowfive@61 180 editbox:SetScript("OnEnterPressed", editboxEnterPressed)
yellowfive@57 181 editbox:SetScript("OnEditFocusLost", editboxEditFocusLost)
yellowfive@57 182 editbox:SetScript("OnTextChanged", editboxTextChanged)
yellowfive@57 183 editbox:SetScript("OnTextSet", editboxTextSet)
yellowfive@57 184 editbox:SetScript("OnEditFocusGained", editboxEditFocusGained)
yellowfive@57 185
yellowfive@57 186 frame:SetScrollChild(editbox)
yellowfive@57 187
yellowfive@57 188 local border = frame:CreateTexture(nil, "BACKGROUND")
yellowfive@57 189 border:SetTexture(Amr.Colors.BorderGray.R, Amr.Colors.BorderGray.G, Amr.Colors.BorderGray.B, 1)
yellowfive@57 190 border:SetAllPoints(true)
yellowfive@57 191
yellowfive@57 192 local bg = frame:CreateTexture(nil, "BORDER")
yellowfive@57 193 bg:SetTexture(Amr.Colors.BgInput.R, Amr.Colors.BgInput.G, Amr.Colors.BgInput.B, 1)
yellowfive@57 194 bg:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1)
yellowfive@57 195 bg:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1)
yellowfive@57 196
yellowfive@57 197 local widget = {
yellowfive@57 198 editbox = editbox,
yellowfive@57 199 scrollFrame = frame,
yellowfive@57 200 frame = frame,
yellowfive@57 201 type = Type
yellowfive@57 202 }
yellowfive@57 203 for method, func in pairs(methods) do
yellowfive@57 204 widget[method] = func
yellowfive@57 205 end
yellowfive@57 206 editbox.obj, frame.obj = widget, widget
yellowfive@57 207
yellowfive@57 208 return AceGUI:RegisterAsWidget(widget)
yellowfive@57 209 end
yellowfive@57 210
yellowfive@57 211 AceGUI:RegisterWidgetType(Type, Constructor, Version)