annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-DropDown-Items.lua @ 11:371e14cd2feb

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