annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-DropDown-Items.lua @ 5:c31ee4251181

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