annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-DropDown-Items.lua @ 0:98c6f55e6619

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