Tercio@17: --[[ $Id: AceGUIWidget-DropDown-Items.lua 1167 2017-08-29 22:08:48Z funkydude $ ]]-- tercio@0: tercio@0: local AceGUI = LibStub("AceGUI-3.0") tercio@0: tercio@0: -- Lua APIs tercio@0: local select, assert = select, assert tercio@0: tercio@0: -- WoW APIs tercio@0: local PlaySound = PlaySound tercio@0: local CreateFrame = CreateFrame tercio@0: tercio@0: local function fixlevels(parent,...) tercio@0: local i = 1 tercio@0: local child = select(i, ...) tercio@0: while child do tercio@0: child:SetFrameLevel(parent:GetFrameLevel()+1) tercio@0: fixlevels(child, child:GetChildren()) tercio@0: i = i + 1 tercio@0: child = select(i, ...) tercio@0: end tercio@0: end tercio@0: tercio@0: local function fixstrata(strata, parent, ...) tercio@0: local i = 1 tercio@0: local child = select(i, ...) tercio@0: parent:SetFrameStrata(strata) tercio@0: while child do tercio@0: fixstrata(strata, child, child:GetChildren()) tercio@0: i = i + 1 tercio@0: child = select(i, ...) tercio@0: end tercio@0: end tercio@0: tercio@0: -- ItemBase is the base "class" for all dropdown items. tercio@0: -- Each item has to use ItemBase.Create(widgetType) to tercio@0: -- create an initial 'self' value. tercio@0: -- ItemBase will add common functions and ui event handlers. tercio@0: -- Be sure to keep basic usage when you override functions. tercio@0: tercio@0: local ItemBase = { tercio@0: -- NOTE: The ItemBase version is added to each item's version number tercio@0: -- to ensure proper updates on ItemBase changes. tercio@0: -- Use at least 1000er steps. tercio@0: version = 1000, tercio@0: counter = 0, tercio@0: } tercio@0: tercio@0: function ItemBase.Frame_OnEnter(this) tercio@0: local self = this.obj tercio@0: tercio@0: if self.useHighlight then tercio@0: self.highlight:Show() tercio@0: end tercio@0: self:Fire("OnEnter") tercio@0: tercio@0: if self.specialOnEnter then tercio@0: self.specialOnEnter(self) tercio@0: end tercio@0: end tercio@0: tercio@0: function ItemBase.Frame_OnLeave(this) tercio@0: local self = this.obj tercio@0: tercio@0: self.highlight:Hide() tercio@0: self:Fire("OnLeave") tercio@0: tercio@0: if self.specialOnLeave then tercio@0: self.specialOnLeave(self) tercio@0: end tercio@0: end tercio@0: tercio@0: -- exported, AceGUI callback tercio@0: function ItemBase.OnAcquire(self) tercio@0: self.frame:SetToplevel(true) tercio@0: self.frame:SetFrameStrata("FULLSCREEN_DIALOG") tercio@0: end tercio@0: tercio@0: -- exported, AceGUI callback tercio@0: function ItemBase.OnRelease(self) tercio@0: self:SetDisabled(false) tercio@0: self.pullout = nil tercio@0: self.frame:SetParent(nil) tercio@0: self.frame:ClearAllPoints() tercio@0: self.frame:Hide() tercio@0: end tercio@0: tercio@0: -- exported tercio@0: -- NOTE: this is called by a Dropdown-Pullout. tercio@0: -- Do not call this method directly tercio@0: function ItemBase.SetPullout(self, pullout) tercio@0: self.pullout = pullout tercio@0: tercio@0: self.frame:SetParent(nil) tercio@0: self.frame:SetParent(pullout.itemFrame) tercio@0: self.parent = pullout.itemFrame tercio@0: fixlevels(pullout.itemFrame, pullout.itemFrame:GetChildren()) tercio@0: end tercio@0: tercio@0: -- exported tercio@0: function ItemBase.SetText(self, text) tercio@0: self.text:SetText(text or "") tercio@0: end tercio@0: tercio@0: -- exported tercio@0: function ItemBase.GetText(self) tercio@0: return self.text:GetText() tercio@0: end tercio@0: tercio@0: -- exported tercio@0: function ItemBase.SetPoint(self, ...) tercio@0: self.frame:SetPoint(...) tercio@0: end tercio@0: tercio@0: -- exported tercio@0: function ItemBase.Show(self) tercio@0: self.frame:Show() tercio@0: end tercio@0: tercio@0: -- exported tercio@0: function ItemBase.Hide(self) tercio@0: self.frame:Hide() tercio@0: end tercio@0: tercio@0: -- exported tercio@0: function ItemBase.SetDisabled(self, disabled) tercio@0: self.disabled = disabled tercio@0: if disabled then tercio@0: self.useHighlight = false tercio@0: self.text:SetTextColor(.5, .5, .5) tercio@0: else tercio@0: self.useHighlight = true tercio@0: self.text:SetTextColor(1, 1, 1) tercio@0: end tercio@0: end tercio@0: tercio@0: -- exported tercio@0: -- NOTE: this is called by a Dropdown-Pullout. tercio@0: -- Do not call this method directly tercio@0: function ItemBase.SetOnLeave(self, func) tercio@0: self.specialOnLeave = func tercio@0: end tercio@0: tercio@0: -- exported tercio@0: -- NOTE: this is called by a Dropdown-Pullout. tercio@0: -- Do not call this method directly tercio@0: function ItemBase.SetOnEnter(self, func) tercio@0: self.specialOnEnter = func tercio@0: end tercio@0: tercio@0: function ItemBase.Create(type) tercio@0: -- NOTE: Most of the following code is copied from AceGUI-3.0/Dropdown widget tercio@0: local count = AceGUI:GetNextWidgetNum(type) tercio@0: local frame = CreateFrame("Button", "AceGUI30DropDownItem"..count) tercio@0: local self = {} tercio@0: self.frame = frame tercio@0: frame.obj = self tercio@0: self.type = type tercio@0: tercio@0: self.useHighlight = true tercio@0: tercio@0: frame:SetHeight(17) tercio@0: frame:SetFrameStrata("FULLSCREEN_DIALOG") tercio@0: tercio@0: local text = frame:CreateFontString(nil,"OVERLAY","GameFontNormalSmall") tercio@0: text:SetTextColor(1,1,1) tercio@0: text:SetJustifyH("LEFT") tercio@0: text:SetPoint("TOPLEFT",frame,"TOPLEFT",18,0) tercio@0: text:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",-8,0) tercio@0: self.text = text tercio@0: tercio@0: local highlight = frame:CreateTexture(nil, "OVERLAY") tercio@0: highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight") tercio@0: highlight:SetBlendMode("ADD") tercio@0: highlight:SetHeight(14) tercio@0: highlight:ClearAllPoints() tercio@0: highlight:SetPoint("RIGHT",frame,"RIGHT",-3,0) tercio@0: highlight:SetPoint("LEFT",frame,"LEFT",5,0) tercio@0: highlight:Hide() tercio@0: self.highlight = highlight tercio@0: tercio@0: local check = frame:CreateTexture("OVERLAY") tercio@0: check:SetWidth(16) tercio@0: check:SetHeight(16) tercio@0: check:SetPoint("LEFT",frame,"LEFT",3,-1) tercio@0: check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check") tercio@0: check:Hide() tercio@0: self.check = check tercio@0: tercio@0: local sub = frame:CreateTexture("OVERLAY") tercio@0: sub:SetWidth(16) tercio@0: sub:SetHeight(16) tercio@0: sub:SetPoint("RIGHT",frame,"RIGHT",-3,-1) tercio@0: sub:SetTexture("Interface\\ChatFrame\\ChatFrameExpandArrow") tercio@0: sub:Hide() tercio@0: self.sub = sub tercio@0: tercio@0: frame:SetScript("OnEnter", ItemBase.Frame_OnEnter) tercio@0: frame:SetScript("OnLeave", ItemBase.Frame_OnLeave) tercio@0: tercio@0: self.OnAcquire = ItemBase.OnAcquire tercio@0: self.OnRelease = ItemBase.OnRelease tercio@0: tercio@0: self.SetPullout = ItemBase.SetPullout tercio@0: self.GetText = ItemBase.GetText tercio@0: self.SetText = ItemBase.SetText tercio@0: self.SetDisabled = ItemBase.SetDisabled tercio@0: tercio@0: self.SetPoint = ItemBase.SetPoint tercio@0: self.Show = ItemBase.Show tercio@0: self.Hide = ItemBase.Hide tercio@0: tercio@0: self.SetOnLeave = ItemBase.SetOnLeave tercio@0: self.SetOnEnter = ItemBase.SetOnEnter tercio@0: tercio@0: return self tercio@0: end tercio@0: tercio@0: -- Register a dummy LibStub library to retrieve the ItemBase, so other addons can use it. tercio@0: local IBLib = LibStub:NewLibrary("AceGUI-3.0-DropDown-ItemBase", ItemBase.version) tercio@0: if IBLib then tercio@0: IBLib.GetItemBase = function() return ItemBase end tercio@0: end tercio@0: tercio@0: --[[ tercio@0: Template for items: tercio@0: tercio@0: -- Item: tercio@0: -- tercio@0: do tercio@0: local widgetType = "Dropdown-Item-" tercio@0: local widgetVersion = 1 tercio@0: tercio@0: local function Constructor() tercio@0: local self = ItemBase.Create(widgetType) tercio@0: tercio@0: AceGUI:RegisterAsWidget(self) tercio@0: return self tercio@0: end tercio@0: tercio@0: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) tercio@0: end tercio@0: --]] tercio@0: tercio@0: -- Item: Header tercio@0: -- A single text entry. tercio@0: -- Special: Different text color and no highlight tercio@0: do tercio@0: local widgetType = "Dropdown-Item-Header" tercio@0: local widgetVersion = 1 tercio@0: tercio@0: local function OnEnter(this) tercio@0: local self = this.obj tercio@0: self:Fire("OnEnter") tercio@0: tercio@0: if self.specialOnEnter then tercio@0: self.specialOnEnter(self) tercio@0: end tercio@0: end tercio@0: tercio@0: local function OnLeave(this) tercio@0: local self = this.obj tercio@0: self:Fire("OnLeave") tercio@0: tercio@0: if self.specialOnLeave then tercio@0: self.specialOnLeave(self) tercio@0: end tercio@0: end tercio@0: tercio@0: -- exported, override tercio@0: local function SetDisabled(self, disabled) tercio@0: ItemBase.SetDisabled(self, disabled) tercio@0: if not disabled then tercio@0: self.text:SetTextColor(1, 1, 0) tercio@0: end tercio@0: end tercio@0: tercio@0: local function Constructor() tercio@0: local self = ItemBase.Create(widgetType) tercio@0: tercio@0: self.SetDisabled = SetDisabled tercio@0: tercio@0: self.frame:SetScript("OnEnter", OnEnter) tercio@0: self.frame:SetScript("OnLeave", OnLeave) tercio@0: tercio@0: self.text:SetTextColor(1, 1, 0) tercio@0: tercio@0: AceGUI:RegisterAsWidget(self) tercio@0: return self tercio@0: end tercio@0: tercio@0: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) tercio@0: end tercio@0: tercio@0: -- Item: Execute tercio@0: -- A simple button tercio@0: do tercio@0: local widgetType = "Dropdown-Item-Execute" tercio@0: local widgetVersion = 1 tercio@0: tercio@0: local function Frame_OnClick(this, button) tercio@0: local self = this.obj tercio@0: if self.disabled then return end tercio@0: self:Fire("OnClick") tercio@0: if self.pullout then tercio@0: self.pullout:Close() tercio@0: end tercio@0: end tercio@0: tercio@0: local function Constructor() tercio@0: local self = ItemBase.Create(widgetType) tercio@0: tercio@0: self.frame:SetScript("OnClick", Frame_OnClick) tercio@0: tercio@0: AceGUI:RegisterAsWidget(self) tercio@0: return self tercio@0: end tercio@0: tercio@0: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) tercio@0: end tercio@0: tercio@0: -- Item: Toggle tercio@0: -- Some sort of checkbox for dropdown menus. tercio@0: -- Does not close the pullout on click. tercio@0: do tercio@0: local widgetType = "Dropdown-Item-Toggle" Tercio@17: local widgetVersion = 4 tercio@0: tercio@0: local function UpdateToggle(self) tercio@0: if self.value then tercio@0: self.check:Show() tercio@0: else tercio@0: self.check:Hide() tercio@0: end tercio@0: end tercio@0: tercio@0: local function OnRelease(self) tercio@0: ItemBase.OnRelease(self) tercio@0: self:SetValue(nil) tercio@0: end tercio@0: tercio@0: local function Frame_OnClick(this, button) tercio@0: local self = this.obj tercio@0: if self.disabled then return end tercio@0: self.value = not self.value tercio@0: if self.value then Tercio@17: PlaySound(856) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON tercio@0: else Tercio@17: PlaySound(857) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_OFF tercio@0: end tercio@0: UpdateToggle(self) tercio@0: self:Fire("OnValueChanged", self.value) tercio@0: end tercio@0: tercio@0: -- exported tercio@0: local function SetValue(self, value) tercio@0: self.value = value tercio@0: UpdateToggle(self) tercio@0: end tercio@0: tercio@0: -- exported tercio@0: local function GetValue(self) tercio@0: return self.value tercio@0: end tercio@0: tercio@0: local function Constructor() tercio@0: local self = ItemBase.Create(widgetType) tercio@0: tercio@0: self.frame:SetScript("OnClick", Frame_OnClick) tercio@0: tercio@0: self.SetValue = SetValue tercio@0: self.GetValue = GetValue tercio@0: self.OnRelease = OnRelease tercio@0: tercio@0: AceGUI:RegisterAsWidget(self) tercio@0: return self tercio@0: end tercio@0: tercio@0: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) tercio@0: end tercio@0: tercio@0: -- Item: Menu tercio@0: -- Shows a submenu on mouse over tercio@0: -- Does not close the pullout on click tercio@0: do tercio@0: local widgetType = "Dropdown-Item-Menu" tercio@0: local widgetVersion = 2 tercio@0: tercio@0: local function OnEnter(this) tercio@0: local self = this.obj tercio@0: self:Fire("OnEnter") tercio@0: tercio@0: if self.specialOnEnter then tercio@0: self.specialOnEnter(self) tercio@0: end tercio@0: tercio@0: self.highlight:Show() tercio@0: tercio@0: if not self.disabled and self.submenu then tercio@0: self.submenu:Open("TOPLEFT", self.frame, "TOPRIGHT", self.pullout:GetRightBorderWidth(), 0, self.frame:GetFrameLevel() + 100) tercio@0: end tercio@0: end tercio@0: tercio@0: local function OnHide(this) tercio@0: local self = this.obj tercio@0: if self.submenu then tercio@0: self.submenu:Close() tercio@0: end tercio@0: end tercio@0: tercio@0: -- exported tercio@0: local function SetMenu(self, menu) tercio@0: assert(menu.type == "Dropdown-Pullout") tercio@0: self.submenu = menu tercio@0: end tercio@0: tercio@0: -- exported tercio@0: local function CloseMenu(self) tercio@0: self.submenu:Close() tercio@0: end tercio@0: tercio@0: local function Constructor() tercio@0: local self = ItemBase.Create(widgetType) tercio@0: tercio@0: self.sub:Show() tercio@0: tercio@0: self.frame:SetScript("OnEnter", OnEnter) tercio@0: self.frame:SetScript("OnHide", OnHide) tercio@0: tercio@0: self.SetMenu = SetMenu tercio@0: self.CloseMenu = CloseMenu tercio@0: tercio@0: AceGUI:RegisterAsWidget(self) tercio@0: return self tercio@0: end tercio@0: tercio@0: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) tercio@0: end tercio@0: tercio@0: -- Item: Separator tercio@0: -- A single line to separate items tercio@0: do tercio@0: local widgetType = "Dropdown-Item-Separator" Tercio@11: local widgetVersion = 2 tercio@0: tercio@0: -- exported, override tercio@0: local function SetDisabled(self, disabled) tercio@0: ItemBase.SetDisabled(self, disabled) tercio@0: self.useHighlight = false tercio@0: end tercio@0: tercio@0: local function Constructor() tercio@0: local self = ItemBase.Create(widgetType) tercio@0: tercio@0: self.SetDisabled = SetDisabled tercio@0: tercio@0: local line = self.frame:CreateTexture(nil, "OVERLAY") tercio@0: line:SetHeight(1) Tercio@17: line:SetColorTexture(.5, .5, .5) tercio@0: line:SetPoint("LEFT", self.frame, "LEFT", 10, 0) tercio@0: line:SetPoint("RIGHT", self.frame, "RIGHT", -10, 0) tercio@0: tercio@0: self.text:Hide() tercio@0: tercio@0: self.useHighlight = false tercio@0: tercio@0: AceGUI:RegisterAsWidget(self) tercio@0: return self tercio@0: end tercio@0: tercio@0: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) tercio@0: end