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