comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-DropDown-Items.lua @ 0:c6ff7ba0e8f6

Reasonably functional now. Cleaning up some stuff which might have to be reverted.
author Zerotorescue
date Thu, 07 Oct 2010 17:17:43 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c6ff7ba0e8f6
1 --[[ $Id: AceGUIWidget-DropDown-Items.lua 916 2010-03-15 12:24:36Z nevcairiel $ ]]--
2
3 local AceGUI = LibStub("AceGUI-3.0")
4
5 -- Lua APIs
6 local select, assert = select, assert
7
8 -- WoW APIs
9 local PlaySound = PlaySound
10 local CreateFrame = CreateFrame
11
12 local function fixlevels(parent,...)
13 local i = 1
14 local child = select(i, ...)
15 while child do
16 child:SetFrameLevel(parent:GetFrameLevel()+1)
17 fixlevels(child, child:GetChildren())
18 i = i + 1
19 child = select(i, ...)
20 end
21 end
22
23 local function fixstrata(strata, parent, ...)
24 local i = 1
25 local child = select(i, ...)
26 parent:SetFrameStrata(strata)
27 while child do
28 fixstrata(strata, child, child:GetChildren())
29 i = i + 1
30 child = select(i, ...)
31 end
32 end
33
34 -- ItemBase is the base "class" for all dropdown items.
35 -- Each item has to use ItemBase.Create(widgetType) to
36 -- create an initial 'self' value.
37 -- ItemBase will add common functions and ui event handlers.
38 -- Be sure to keep basic usage when you override functions.
39
40 local ItemBase = {
41 -- NOTE: The ItemBase version is added to each item's version number
42 -- to ensure proper updates on ItemBase changes.
43 -- Use at least 1000er steps.
44 version = 1000,
45 counter = 0,
46 }
47
48 function ItemBase.Frame_OnEnter(this)
49 local self = this.obj
50
51 if self.useHighlight then
52 self.highlight:Show()
53 end
54 self:Fire("OnEnter")
55
56 if self.specialOnEnter then
57 self.specialOnEnter(self)
58 end
59 end
60
61 function ItemBase.Frame_OnLeave(this)
62 local self = this.obj
63
64 self.highlight:Hide()
65 self:Fire("OnLeave")
66
67 if self.specialOnLeave then
68 self.specialOnLeave(self)
69 end
70 end
71
72 -- exported, AceGUI callback
73 function ItemBase.OnAcquire(self)
74 self.frame:SetToplevel(true)
75 self.frame:SetFrameStrata("FULLSCREEN_DIALOG")
76 end
77
78 -- exported, AceGUI callback
79 function ItemBase.OnRelease(self)
80 self:SetDisabled(false)
81 self.pullout = nil
82 self.frame:SetParent(nil)
83 self.frame:ClearAllPoints()
84 self.frame:Hide()
85 end
86
87 -- exported
88 -- NOTE: this is called by a Dropdown-Pullout.
89 -- Do not call this method directly
90 function ItemBase.SetPullout(self, pullout)
91 self.pullout = pullout
92
93 self.frame:SetParent(nil)
94 self.frame:SetParent(pullout.itemFrame)
95 self.parent = pullout.itemFrame
96 fixlevels(pullout.itemFrame, pullout.itemFrame:GetChildren())
97 end
98
99 -- exported
100 function ItemBase.SetText(self, text)
101 self.text:SetText(text or "")
102 end
103
104 -- exported
105 function ItemBase.GetText(self)
106 return self.text:GetText()
107 end
108
109 -- exported
110 function ItemBase.SetPoint(self, ...)
111 self.frame:SetPoint(...)
112 end
113
114 -- exported
115 function ItemBase.Show(self)
116 self.frame:Show()
117 end
118
119 -- exported
120 function ItemBase.Hide(self)
121 self.frame:Hide()
122 end
123
124 -- exported
125 function ItemBase.SetDisabled(self, disabled)
126 self.disabled = disabled
127 if disabled then
128 self.useHighlight = false
129 self.text:SetTextColor(.5, .5, .5)
130 else
131 self.useHighlight = true
132 self.text:SetTextColor(1, 1, 1)
133 end
134 end
135
136 -- exported
137 -- NOTE: this is called by a Dropdown-Pullout.
138 -- Do not call this method directly
139 function ItemBase.SetOnLeave(self, func)
140 self.specialOnLeave = func
141 end
142
143 -- exported
144 -- NOTE: this is called by a Dropdown-Pullout.
145 -- Do not call this method directly
146 function ItemBase.SetOnEnter(self, func)
147 self.specialOnEnter = func
148 end
149
150 function ItemBase.Create(type)
151 -- NOTE: Most of the following code is copied from AceGUI-3.0/Dropdown widget
152 local count = AceGUI:GetNextWidgetNum(type)
153 local frame = CreateFrame("Button", "AceGUI30DropDownItem"..count)
154 local self = {}
155 self.frame = frame
156 frame.obj = self
157 self.type = type
158
159 self.useHighlight = true
160
161 frame:SetHeight(17)
162 frame:SetFrameStrata("FULLSCREEN_DIALOG")
163
164 local text = frame:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
165 text:SetTextColor(1,1,1)
166 text:SetJustifyH("LEFT")
167 text:SetPoint("TOPLEFT",frame,"TOPLEFT",18,0)
168 text:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",-8,0)
169 self.text = text
170
171 local highlight = frame:CreateTexture(nil, "OVERLAY")
172 highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
173 highlight:SetBlendMode("ADD")
174 highlight:SetHeight(14)
175 highlight:ClearAllPoints()
176 highlight:SetPoint("RIGHT",frame,"RIGHT",-3,0)
177 highlight:SetPoint("LEFT",frame,"LEFT",5,0)
178 highlight:Hide()
179 self.highlight = highlight
180
181 local check = frame:CreateTexture("OVERLAY")
182 check:SetWidth(16)
183 check:SetHeight(16)
184 check:SetPoint("LEFT",frame,"LEFT",3,-1)
185 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
186 check:Hide()
187 self.check = check
188
189 local sub = frame:CreateTexture("OVERLAY")
190 sub:SetWidth(16)
191 sub:SetHeight(16)
192 sub:SetPoint("RIGHT",frame,"RIGHT",-3,-1)
193 sub:SetTexture("Interface\\ChatFrame\\ChatFrameExpandArrow")
194 sub:Hide()
195 self.sub = sub
196
197 frame:SetScript("OnEnter", ItemBase.Frame_OnEnter)
198 frame:SetScript("OnLeave", ItemBase.Frame_OnLeave)
199
200 self.OnAcquire = ItemBase.OnAcquire
201 self.OnRelease = ItemBase.OnRelease
202
203 self.SetPullout = ItemBase.SetPullout
204 self.GetText = ItemBase.GetText
205 self.SetText = ItemBase.SetText
206 self.SetDisabled = ItemBase.SetDisabled
207
208 self.SetPoint = ItemBase.SetPoint
209 self.Show = ItemBase.Show
210 self.Hide = ItemBase.Hide
211
212 self.SetOnLeave = ItemBase.SetOnLeave
213 self.SetOnEnter = ItemBase.SetOnEnter
214
215 return self
216 end
217
218 --[[
219 Template for items:
220
221 -- Item:
222 --
223 do
224 local widgetType = "Dropdown-Item-"
225 local widgetVersion = 1
226
227 local function Constructor()
228 local self = ItemBase.Create(widgetType)
229
230 AceGUI:RegisterAsWidget(self)
231 return self
232 end
233
234 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
235 end
236 --]]
237
238 -- Item: Header
239 -- A single text entry.
240 -- Special: Different text color and no highlight
241 do
242 local widgetType = "Dropdown-Item-Header"
243 local widgetVersion = 1
244
245 local function OnEnter(this)
246 local self = this.obj
247 self:Fire("OnEnter")
248
249 if self.specialOnEnter then
250 self.specialOnEnter(self)
251 end
252 end
253
254 local function OnLeave(this)
255 local self = this.obj
256 self:Fire("OnLeave")
257
258 if self.specialOnLeave then
259 self.specialOnLeave(self)
260 end
261 end
262
263 -- exported, override
264 local function SetDisabled(self, disabled)
265 ItemBase.SetDisabled(self, disabled)
266 if not disabled then
267 self.text:SetTextColor(1, 1, 0)
268 end
269 end
270
271 local function Constructor()
272 local self = ItemBase.Create(widgetType)
273
274 self.SetDisabled = SetDisabled
275
276 self.frame:SetScript("OnEnter", OnEnter)
277 self.frame:SetScript("OnLeave", OnLeave)
278
279 self.text:SetTextColor(1, 1, 0)
280
281 AceGUI:RegisterAsWidget(self)
282 return self
283 end
284
285 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
286 end
287
288 -- Item: Execute
289 -- A simple button
290 do
291 local widgetType = "Dropdown-Item-Execute"
292 local widgetVersion = 1
293
294 local function Frame_OnClick(this, button)
295 local self = this.obj
296 if self.disabled then return end
297 self:Fire("OnClick")
298 if self.pullout then
299 self.pullout:Close()
300 end
301 end
302
303 local function Constructor()
304 local self = ItemBase.Create(widgetType)
305
306 self.frame:SetScript("OnClick", Frame_OnClick)
307
308 AceGUI:RegisterAsWidget(self)
309 return self
310 end
311
312 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
313 end
314
315 -- Item: Toggle
316 -- Some sort of checkbox for dropdown menus.
317 -- Does not close the pullout on click.
318 do
319 local widgetType = "Dropdown-Item-Toggle"
320 local widgetVersion = 3
321
322 local function UpdateToggle(self)
323 if self.value then
324 self.check:Show()
325 else
326 self.check:Hide()
327 end
328 end
329
330 local function OnRelease(self)
331 ItemBase.OnRelease(self)
332 self:SetValue(nil)
333 end
334
335 local function Frame_OnClick(this, button)
336 local self = this.obj
337 if self.disabled then return end
338 self.value = not self.value
339 if self.value then
340 PlaySound("igMainMenuOptionCheckBoxOn")
341 else
342 PlaySound("igMainMenuOptionCheckBoxOff")
343 end
344 UpdateToggle(self)
345 self:Fire("OnValueChanged", self.value)
346 end
347
348 -- exported
349 local function SetValue(self, value)
350 self.value = value
351 UpdateToggle(self)
352 end
353
354 -- exported
355 local function GetValue(self)
356 return self.value
357 end
358
359 local function Constructor()
360 local self = ItemBase.Create(widgetType)
361
362 self.frame:SetScript("OnClick", Frame_OnClick)
363
364 self.SetValue = SetValue
365 self.GetValue = GetValue
366 self.OnRelease = OnRelease
367
368 AceGUI:RegisterAsWidget(self)
369 return self
370 end
371
372 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
373 end
374
375 -- Item: Menu
376 -- Shows a submenu on mouse over
377 -- Does not close the pullout on click
378 do
379 local widgetType = "Dropdown-Item-Menu"
380 local widgetVersion = 2
381
382 local function OnEnter(this)
383 local self = this.obj
384 self:Fire("OnEnter")
385
386 if self.specialOnEnter then
387 self.specialOnEnter(self)
388 end
389
390 self.highlight:Show()
391
392 if not self.disabled and self.submenu then
393 self.submenu:Open("TOPLEFT", self.frame, "TOPRIGHT", self.pullout:GetRightBorderWidth(), 0, self.frame:GetFrameLevel() + 100)
394 end
395 end
396
397 local function OnHide(this)
398 local self = this.obj
399 if self.submenu then
400 self.submenu:Close()
401 end
402 end
403
404 -- exported
405 local function SetMenu(self, menu)
406 assert(menu.type == "Dropdown-Pullout")
407 self.submenu = menu
408 end
409
410 -- exported
411 local function CloseMenu(self)
412 self.submenu:Close()
413 end
414
415 local function Constructor()
416 local self = ItemBase.Create(widgetType)
417
418 self.sub:Show()
419
420 self.frame:SetScript("OnEnter", OnEnter)
421 self.frame:SetScript("OnHide", OnHide)
422
423 self.SetMenu = SetMenu
424 self.CloseMenu = CloseMenu
425
426 AceGUI:RegisterAsWidget(self)
427 return self
428 end
429
430 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
431 end
432
433 -- Item: Separator
434 -- A single line to separate items
435 do
436 local widgetType = "Dropdown-Item-Separator"
437 local widgetVersion = 1
438
439 -- exported, override
440 local function SetDisabled(self, disabled)
441 ItemBase.SetDisabled(self, disabled)
442 self.useHighlight = false
443 end
444
445 local function Constructor()
446 local self = ItemBase.Create(widgetType)
447
448 self.SetDisabled = SetDisabled
449
450 local line = self.frame:CreateTexture(nil, "OVERLAY")
451 line:SetHeight(1)
452 line:SetTexture(.5, .5, .5)
453 line:SetPoint("LEFT", self.frame, "LEFT", 10, 0)
454 line:SetPoint("RIGHT", self.frame, "RIGHT", -10, 0)
455
456 self.text:Hide()
457
458 self.useHighlight = false
459
460 AceGUI:RegisterAsWidget(self)
461 return self
462 end
463
464 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
465 end