annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-DropDown-Items.lua @ 51:dbf04157d63e v7.3.0.051

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