comparison MultiSelectWidget.lua @ 1:21c58930f74e

Refactoring
author John@Yosemite-PC
date Fri, 02 Mar 2012 22:32:12 -0500
parents
children
comparison
equal deleted inserted replaced
0:47fac96968e1 1:21c58930f74e
1
2
3 --[[----------------------------------
4 -- MultiSelect widget for AceGUI-3.0
5 -- Written by Shirokuma
6 --]]----------------------------------
7
8
9 --[[-----------------
10 -- AceGUI
11 --]]-----------------
12 local AceGUI = LibStub("AceGUI-3.0")
13
14 --[[-----------------
15 -- Lua APIs
16 --]]-----------------
17 local format, pairs, tostring = string.format, pairs, tostring
18
19 --[[-----------------
20 -- WoW APIs
21 --]]-----------------
22 local CreateFrame, UIParent = CreateFrame, UIParent
23
24 --[[-----------------
25 -- Frame Elements
26 --]]-----------------
27 local FrameBackdrop = {
28 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
29 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
30 tile = true, tileSize = 16, edgeSize = 16,
31 insets = { left = 3, right = 3, top = 3, bottom = 3 }
32 }
33
34
35 --[[-----------------
36 -- Widget Info
37 --]]-----------------
38 local widgetType = "MultiSelect"
39 local widgetVersion = 1
40
41
42 --[[-----------------
43 -- Event Code
44 --]]-----------------
45 local function Label_OnEnter(label)
46 local self = label.obj
47 local value = label
48 self:Fire("OnLabelEnter", value)
49 end
50
51 local function Label_OnLeave(label)
52 local self = label.obj
53 local value = label
54 self:Fire("OnLabelEnter", value)
55 end
56
57 local function Label_OnClick(label)
58 local self = label.obj
59 local value = label
60 self:Fire("OnLabelClick", value)
61 AceGUI:ClearFocus()
62 end
63
64
65 --[[-----------------
66 -- MultiSelect Code
67 --]]-----------------
68 do
69 local function OnAcquire(self) -- set up the default size
70 self:SetWidth(200)
71 self:SetHeight(400)
72 end
73
74 local function SetWidth(self, w) -- override the SetWidth function to include the labelframe
75 self.frame:SetWidth(w)
76 self.labelframe:SetWidth(w-33)
77 end
78
79 local function SetLabel(self, text) -- sets the multiselect label text
80 self.label:SetText(text)
81 end
82
83 local function SetMultiSelect(self, value) -- set if multiple values can be selected simultaneously
84 self.multiselect = value
85 end
86
87 local function AddItem(self, str, color) -- add an item (create a new item label object)
88 local color = color
89 local label = CreateFrame("Button", nil, self.labelframe)
90 label.selected = false
91 label.obj = self
92 label:SetHeight(18)
93 label:SetPoint("TOPLEFT", self.labelframe, "TOPLEFT", 0, -(getn(self.labels) * 18))
94 label:SetPoint("TOPRIGHT", self.labelframe, "TOPRIGHT", 0, -(getn(self.labels) * 18))
95 self.labels[getn(self.labels) + 1] = label
96 self.labelframe:SetHeight(getn(self.labels) * 18)
97
98 local text = label:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
99 text:SetJustifyH("LEFT")
100 text:SetPoint("TOPLEFT",label,"TOPLEFT",5,0)
101 text:SetPoint("BOTTOMRIGHT",label,"BOTTOMRIGHT",-5,0)
102 if color ~= nil then
103 text:SetTextColor(color.r,color.g,color.b)
104 end
105 text:SetText(str)
106 label.text = text
107
108 local highlight = label:CreateTexture(nil, "OVERLAY")
109 highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
110 highlight:SetBlendMode("ADD")
111 highlight:SetHeight(14)
112 highlight:ClearAllPoints()
113 highlight:SetPoint("RIGHT",label,"RIGHT",0,0)
114 highlight:SetPoint("LEFT",label,"LEFT",0,0)
115 highlight:Hide()
116 label.highlight = highlight
117
118 label:SetScript("OnEnter", function(this)
119 this.highlight:Show()
120 Label_OnEnter(this)
121 end)
122 label:SetScript("OnLeave", function(this)
123 if not this.selected then
124 this.highlight:Hide()
125 end
126 end)
127 label:SetScript("OnClick", function(this)
128 if not this.selected then
129 this.selected = true
130 if not self.multiselect then
131 for index, items in pairs(self.labels) do
132 if self.labels[index] ~= this and self.labels[index].selected then
133 self.labels[index].selected = false
134 self.labels[index].highlight:Hide()
135 end
136 end
137 end
138 else
139 this.selected = false
140 end
141 Label_OnClick(this)
142 end)
143 end
144
145 local function GetItem(self, text) -- find an object based on the text parameter
146 for _, value in pairs(self.labels) do
147 if value.text:GetText() == text then
148 return value
149 end
150 end
151 return nil
152 end
153
154 local function GetText(self, value) -- get the text of a label object
155 for _,item in pairs(self.labels) do
156 if value == item then
157 return item.text:GetText()
158 end
159 end
160 return nil
161 end
162
163 local function SetText(self, value, text) -- set the text of a label object
164 for _, item in pairs(self.labels) do
165 if value == item then
166 value.text:SetText(text)
167 end
168 end
169 end
170
171 local function IsSelected(self, value) -- return if the label object is currently selected
172 for _, item in pairs(self.labels) do
173 if value == item then
174 return item.selected
175 end
176 end
177 return nil
178 end
179
180 local function GetSelected(self) -- return a table of the currently selected label objects
181 local selectedList = {}
182 for _, item in pairs(self.labels) do
183 if item.selected then
184 table.insert(selectedList, item)
185 end
186 end
187 return selectedList
188 end
189
190 local function SetItemList(self, list) -- create new labels from a list of strings
191 for _,item in pairs(self.labels) do
192 item:Hide()
193 item:ClearAllPoints()
194 end
195
196 self.labels = {}
197
198 if list then
199 for _,item in pairs(list) do
200 if type(item) == "string" then
201 self:AddItem(item)
202 elseif type(item) == "table" then
203 if item.string ~= nil and type(item.string) == "string" then
204 if item.color ~= nil then
205 if type(item.color) == "table" and item.color.r ~= nil and item.color.g ~= nil and item.color.b ~= nil then
206 self:AddItem(item.string, item.color)
207 else
208 assert(false and "setitemlist: item.color is set, but nonsense")
209 end
210 else
211 self:AddItem(item.string)
212 end
213 else
214 assert( false and "setitemlist: item is table without .string member")
215 end
216 else
217 assert(false and "SetItemList: nonsense list entry")
218 end
219 end
220 end
221 end
222
223 local function RemoveItem(self, item) -- delete an item
224 local function RedrawFrame()
225 for index,value in pairs(self.labels) do
226 value:SetPoint("TOPLEFT", self.labelframe, "TOPLEFT", 0, (-(index-1) * 18))
227 value:SetPoint("TOPRIGHT", self.labelframe, "TOPRIGHT", 0,(-(index-1) * 18))
228 end
229 end
230
231 for index, value in pairs(self.labels) do
232 if value == item then
233 table.remove(self.labels, index)
234 item:Hide()
235 item:ClearAllPoints()
236 RedrawFrame()
237 end
238 end
239 end
240
241 local function SetSelected(self, item, value)
242 if value then
243 if not self.multiselect then -- test
244 for _, value in pairs(self.labels) do
245 value.selected = false
246 value.highlight:Hide()
247 end
248 end
249 item.selected = true
250 item.highlight:Show()
251 else
252 item.selected = false
253 item.highlight:Hide()
254 end
255 end
256
257 local function Constructor() -- widget constructor
258 local frame = CreateFrame("Frame", nil, UIParent)
259 local backdrop = CreateFrame("Frame", nil, frame)
260 local self = {}
261 local labels = {}
262
263 self.type = widgetType
264 self.frame = frame
265 self.backdrop = backdrop
266 self.labels = {}
267 self.multiselect = true
268 frame.obj = self
269
270 local label = frame:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
271 label:SetJustifyH("LEFT")
272 label:SetPoint("TOPLEFT", 5, 0)
273 label:SetPoint("TOPRIGHT", -5, 0)
274 label:SetHeight(14)
275 label:SetText("MultiSelect")
276 self.label = label
277
278 backdrop:SetBackdrop(FrameBackdrop)
279 backdrop:SetBackdropColor(0, 0, 0)
280 backdrop:SetBackdropBorderColor(0.4, 0.4, 0.4)
281 backdrop:SetPoint("TOPLEFT", frame, "TOPLEFT", 5, -14)
282 backdrop:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -5, 0)
283
284 local scrollframe = CreateFrame("ScrollFrame", format("%s@%s@%s", widgetType, "ScrollFrame", tostring(self)), frame, "UIPanelScrollFrameTemplate")
285 scrollframe:SetPoint("TOPLEFT", backdrop, "TOPLEFT", 5, -6)
286 scrollframe:SetPoint("BOTTOMRIGHT", backdrop, "BOTTOMRIGHT", -28, 6)
287 scrollframe.obj = self
288 self.scrollframe = scrollframe
289
290 local labelframe = CreateFrame("Frame", nil, scrollframe)
291 labelframe:SetAllPoints()
292 labelframe.obj = self
293 scrollframe:SetScrollChild(labelframe)
294 self.labelframe = labelframe
295
296 -- method listing
297 self.OnAcquire = OnAcquire
298 self.SetLabel = SetLabel
299 self.AddItem = AddItem
300 self.SetWidth = SetWidth
301 self.SetMultiSelect = SetMultiSelect
302 self.SetItemList = SetItemList
303 self.GetItem = GetItem
304 self.RemoveItem = RemoveItem
305 self.GetText = GetText
306 self.SetText = SetText
307 self.IsSelected = IsSelected
308 self.GetSelected = GetSelected
309 self.SetSelected = SetSelected
310
311 AceGUI:RegisterAsWidget(self)
312 return self
313 end
314 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion)
315 end
316