annotate ui/AmrUiTextarea.lua @ 57:01b63b8ed811 v21

total rewrite to version 21
author yellowfive
date Fri, 05 Jun 2015 11:05:15 -0700
parents
children cf2b6b9a8337
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@57 63 -- works for both the scrollframe and the editbox, handles e.g. dragging a spell link into the textarea
yellowfive@57 64 local function onReceiveDrag(self)
yellowfive@57 65 local type, id, info = GetCursorInfo()
yellowfive@57 66 if type == "spell" then
yellowfive@57 67 info = GetSpellInfo(id, info)
yellowfive@57 68 elseif type ~= "item" then
yellowfive@57 69 return
yellowfive@57 70 end
yellowfive@57 71 ClearCursor()
yellowfive@57 72 self = self.obj
yellowfive@57 73 local editbox = self.editbox
yellowfive@57 74 if not editbox:HasFocus() then
yellowfive@57 75 editbox:SetFocus()
yellowfive@57 76 editbox:SetCursorPosition(editbox:GetNumLetters())
yellowfive@57 77 end
yellowfive@57 78 editbox:Insert(info)
yellowfive@57 79 end
yellowfive@57 80
yellowfive@57 81 --[[-----------------------------------------------------------------------------
yellowfive@57 82 Methods
yellowfive@57 83 -------------------------------------------------------------------------------]]
yellowfive@57 84 local methods = {
yellowfive@57 85 ["OnAcquire"] = function(self)
yellowfive@57 86 -- restore default values
yellowfive@57 87 self:SetWidth(200)
yellowfive@57 88 self:SetHeight(24)
yellowfive@57 89 self:SetFont(Amr.CreateFont("Regular", 16, Amr.Colors.Text))
yellowfive@57 90 self:SetText("")
yellowfive@57 91 self.frame:ClearAllPoints()
yellowfive@57 92 end,
yellowfive@57 93
yellowfive@57 94 ["SetText"] = function(self, text)
yellowfive@57 95 self.editbox:SetText(text)
yellowfive@57 96 end,
yellowfive@57 97
yellowfive@57 98 ["GetText"] = function(self)
yellowfive@57 99 return self.editbox:GetText()
yellowfive@57 100 end,
yellowfive@57 101
yellowfive@57 102 ["SetFont"] = function(self, font)
yellowfive@57 103 self.editbox:SetFontObject(font)
yellowfive@57 104 end,
yellowfive@57 105
yellowfive@57 106 ["SetDisabled"] = function(self, disabled)
yellowfive@57 107 self.disabled = disabled
yellowfive@57 108 if disabled then
yellowfive@57 109 self.editbox:Disable()
yellowfive@57 110 else
yellowfive@57 111 self.editbox:Enable()
yellowfive@57 112 end
yellowfive@57 113 end,
yellowfive@57 114
yellowfive@57 115 ["OnWidthSet"] = function(self, width)
yellowfive@57 116 self.editbox:SetWidth(width)
yellowfive@57 117 end,
yellowfive@57 118
yellowfive@57 119 ["OnHeightSet"] = function(self, height)
yellowfive@57 120 self.editbox:SetHeight(height)
yellowfive@57 121 end,
yellowfive@57 122
yellowfive@57 123 ["ClearFocus"] = function(self)
yellowfive@57 124 self.editbox:ClearFocus()
yellowfive@57 125 self.frame:SetScript("OnShow", nil)
yellowfive@57 126 end,
yellowfive@57 127
yellowfive@57 128 ["SetFocus"] = function(self, highlight)
yellowfive@57 129 self.editbox:SetFocus()
yellowfive@57 130 if highlight then
yellowfive@57 131 self.editbox:HighlightText()
yellowfive@57 132 end
yellowfive@57 133 if not self.frame:IsShown() then
yellowfive@57 134 self.frame:SetScript("OnShow", function(frame)
yellowfive@57 135 frame.obj.editbox:SetFocus()
yellowfive@57 136 if highlight then
yellowfive@57 137 self.editbox:HighlightText()
yellowfive@57 138 end
yellowfive@57 139 frame:SetScript("OnShow", nil)
yellowfive@57 140 end)
yellowfive@57 141 end
yellowfive@57 142 end
yellowfive@57 143 }
yellowfive@57 144
yellowfive@57 145 --[[-----------------------------------------------------------------------------
yellowfive@57 146 Constructor
yellowfive@57 147 -------------------------------------------------------------------------------]]
yellowfive@57 148 local function Constructor()
yellowfive@57 149 local name = "AmrUiTextarea" .. AceGUI:GetNextWidgetNum(Type)
yellowfive@57 150 local frame = CreateFrame("ScrollFrame", name, UIParent)
yellowfive@57 151 frame:Hide()
yellowfive@57 152
yellowfive@57 153 frame:SetScript("OnMouseUp", scrollFrameMouseUp)
yellowfive@57 154 frame:SetScript("OnReceiveDrag", onReceiveDrag)
yellowfive@57 155 frame:HookScript("OnVerticalScroll", scrollFrameVerticalScroll)
yellowfive@57 156
yellowfive@57 157 local editbox = CreateFrame("EditBox", name .. "Edit", frame)
yellowfive@57 158 editbox:SetAllPoints()
yellowfive@57 159 editbox:EnableMouse(true)
yellowfive@57 160 editbox:SetMultiLine(true)
yellowfive@57 161 editbox:SetAutoFocus(false)
yellowfive@57 162 editbox:SetCountInvisibleLetters(false)
yellowfive@57 163 editbox:SetTextInsets(4, 4, 4, 4)
yellowfive@57 164
yellowfive@57 165 editbox:SetScript("OnMouseDown", onReceiveDrag)
yellowfive@57 166 editbox:SetScript("OnReceiveDrag", onReceiveDrag)
yellowfive@57 167
yellowfive@57 168 editbox:SetScript("OnCursorChanged", editboxCursorChanged)
yellowfive@57 169 editbox:SetScript("OnEscapePressed", editbox.ClearFocus)
yellowfive@57 170 editbox:SetScript("OnEditFocusLost", editboxEditFocusLost)
yellowfive@57 171 editbox:SetScript("OnTextChanged", editboxTextChanged)
yellowfive@57 172 editbox:SetScript("OnTextSet", editboxTextSet)
yellowfive@57 173 editbox:SetScript("OnEditFocusGained", editboxEditFocusGained)
yellowfive@57 174
yellowfive@57 175 frame:SetScrollChild(editbox)
yellowfive@57 176
yellowfive@57 177 local border = frame:CreateTexture(nil, "BACKGROUND")
yellowfive@57 178 border:SetTexture(Amr.Colors.BorderGray.R, Amr.Colors.BorderGray.G, Amr.Colors.BorderGray.B, 1)
yellowfive@57 179 border:SetAllPoints(true)
yellowfive@57 180
yellowfive@57 181 local bg = frame:CreateTexture(nil, "BORDER")
yellowfive@57 182 bg:SetTexture(Amr.Colors.BgInput.R, Amr.Colors.BgInput.G, Amr.Colors.BgInput.B, 1)
yellowfive@57 183 bg:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1)
yellowfive@57 184 bg:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1)
yellowfive@57 185
yellowfive@57 186 local widget = {
yellowfive@57 187 editbox = editbox,
yellowfive@57 188 scrollFrame = frame,
yellowfive@57 189 frame = frame,
yellowfive@57 190 type = Type
yellowfive@57 191 }
yellowfive@57 192 for method, func in pairs(methods) do
yellowfive@57 193 widget[method] = func
yellowfive@57 194 end
yellowfive@57 195 editbox.obj, frame.obj = widget, widget
yellowfive@57 196
yellowfive@57 197 return AceGUI:RegisterAsWidget(widget)
yellowfive@57 198 end
yellowfive@57 199
yellowfive@57 200 AceGUI:RegisterWidgetType(Type, Constructor, Version)