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