Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: TabGroup Container Zerotorescue@0: Container that uses tabs on top to switch between groups. Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local Type, Version = "TabGroup", 30 Zerotorescue@0: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) Zerotorescue@0: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end Zerotorescue@0: Zerotorescue@0: -- Lua APIs Zerotorescue@0: local pairs, ipairs, assert, type, wipe = pairs, ipairs, assert, type, wipe Zerotorescue@0: Zerotorescue@0: -- WoW APIs Zerotorescue@0: local PlaySound = PlaySound Zerotorescue@0: local CreateFrame, UIParent = CreateFrame, UIParent Zerotorescue@0: local _G = _G Zerotorescue@0: Zerotorescue@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Zerotorescue@0: -- List them here for Mikk's FindGlobals script Zerotorescue@0: -- GLOBALS: PanelTemplates_TabResize, PanelTemplates_SetDisabledTabState, PanelTemplates_SelectTab, PanelTemplates_DeselectTab Zerotorescue@0: Zerotorescue@0: -- local upvalue storage used by BuildTabs Zerotorescue@0: local widths = {} Zerotorescue@0: local rowwidths = {} Zerotorescue@0: local rowends = {} Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Support functions Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local function UpdateTabLook(frame) Zerotorescue@0: if frame.disabled then Zerotorescue@0: PanelTemplates_SetDisabledTabState(frame) Zerotorescue@0: elseif frame.selected then Zerotorescue@0: PanelTemplates_SelectTab(frame) Zerotorescue@0: else Zerotorescue@0: PanelTemplates_DeselectTab(frame) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Tab_SetText(frame, text) Zerotorescue@0: frame:_SetText(text) Zerotorescue@0: local width = frame.obj.frame.width or frame.obj.frame:GetWidth() or 0 Zerotorescue@0: PanelTemplates_TabResize(frame, 0, nil, width) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Tab_SetSelected(frame, selected) Zerotorescue@0: frame.selected = selected Zerotorescue@0: UpdateTabLook(frame) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Tab_SetDisabled(frame, disabled) Zerotorescue@0: frame.disabled = disabled Zerotorescue@0: UpdateTabLook(frame) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function BuildTabsOnUpdate(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: self:BuildTabs() Zerotorescue@0: frame:SetScript("OnUpdate", nil) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Scripts Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local function Tab_OnClick(frame) Zerotorescue@0: if not (frame.selected or frame.disabled) then Zerotorescue@0: PlaySound("igCharacterInfoTab") Zerotorescue@0: frame.obj:SelectTab(frame.value) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Tab_OnEnter(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: self:Fire("OnTabEnter", self.tabs[frame.id].value, frame) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Tab_OnLeave(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: self:Fire("OnTabLeave", self.tabs[frame.id].value, frame) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Tab_OnShow(frame) Zerotorescue@0: _G[frame:GetName().."HighlightTexture"]:SetWidth(frame:GetTextWidth() + 30) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Methods Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local methods = { Zerotorescue@0: ["OnAcquire"] = function(self) Zerotorescue@0: self:SetTitle() Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["OnRelease"] = function(self) Zerotorescue@0: self.status = nil Zerotorescue@0: for k in pairs(self.localstatus) do Zerotorescue@0: self.localstatus[k] = nil Zerotorescue@0: end Zerotorescue@0: self.tablist = nil Zerotorescue@0: for _, tab in pairs(self.tabs) do Zerotorescue@0: tab:Hide() Zerotorescue@0: end Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["CreateTab"] = function(self, id) Zerotorescue@0: local tabname = ("AceGUITabGroup%dTab%d"):format(self.num, id) Zerotorescue@0: local tab = CreateFrame("Button", tabname, self.border, "OptionsFrameTabButtonTemplate") Zerotorescue@0: tab.obj = self Zerotorescue@0: tab.id = id Zerotorescue@0: Zerotorescue@0: tab.text = _G[tabname .. "Text"] Zerotorescue@0: tab.text:ClearAllPoints() Zerotorescue@0: tab.text:SetPoint("LEFT", 14, -3) Zerotorescue@0: tab.text:SetPoint("RIGHT", -12, -3) Zerotorescue@0: Zerotorescue@0: tab:SetScript("OnClick", Tab_OnClick) Zerotorescue@0: tab:SetScript("OnEnter", Tab_OnEnter) Zerotorescue@0: tab:SetScript("OnLeave", Tab_OnLeave) Zerotorescue@0: tab:SetScript("OnShow", Tab_OnShow) Zerotorescue@0: Zerotorescue@0: tab._SetText = tab.SetText Zerotorescue@0: tab.SetText = Tab_SetText Zerotorescue@0: tab.SetSelected = Tab_SetSelected Zerotorescue@0: tab.SetDisabled = Tab_SetDisabled Zerotorescue@0: Zerotorescue@0: return tab Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetTitle"] = function(self, text) Zerotorescue@0: self.titletext:SetText(text or "") Zerotorescue@0: if text and text ~= "" then Zerotorescue@0: self.alignoffset = 25 Zerotorescue@0: else Zerotorescue@0: self.alignoffset = 18 Zerotorescue@0: end Zerotorescue@0: self:BuildTabs() Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetStatusTable"] = function(self, status) Zerotorescue@0: assert(type(status) == "table") Zerotorescue@0: self.status = status Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SelectTab"] = function(self, value) Zerotorescue@0: local status = self.status or self.localstatus Zerotorescue@0: local found Zerotorescue@0: for i, v in ipairs(self.tabs) do Zerotorescue@0: if v.value == value then Zerotorescue@0: v:SetSelected(true) Zerotorescue@0: found = true Zerotorescue@0: else Zerotorescue@0: v:SetSelected(false) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: status.selected = value Zerotorescue@0: if found then Zerotorescue@0: self:Fire("OnGroupSelected",value) Zerotorescue@0: end Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetTabs"] = function(self, tabs) Zerotorescue@0: self.tablist = tabs Zerotorescue@0: self:BuildTabs() Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: ["BuildTabs"] = function(self) Zerotorescue@0: local hastitle = (self.titletext:GetText() and self.titletext:GetText() ~= "") Zerotorescue@0: local status = self.status or self.localstatus Zerotorescue@0: local tablist = self.tablist Zerotorescue@0: local tabs = self.tabs Zerotorescue@0: Zerotorescue@0: if not tablist then return end Zerotorescue@0: Zerotorescue@0: local width = self.frame.width or self.frame:GetWidth() or 0 Zerotorescue@0: Zerotorescue@0: wipe(widths) Zerotorescue@0: wipe(rowwidths) Zerotorescue@0: wipe(rowends) Zerotorescue@0: Zerotorescue@0: --Place Text into tabs and get thier initial width Zerotorescue@0: for i, v in ipairs(tablist) do Zerotorescue@0: local tab = tabs[i] Zerotorescue@0: if not tab then Zerotorescue@0: tab = self:CreateTab(i) Zerotorescue@0: tabs[i] = tab Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: tab:Show() Zerotorescue@0: tab:SetText(v.text) Zerotorescue@0: tab:SetDisabled(v.disabled) Zerotorescue@0: tab.value = v.value Zerotorescue@0: Zerotorescue@0: widths[i] = tab:GetWidth() - 6 --tabs are anchored 10 pixels from the right side of the previous one to reduce spacing, but add a fixed 4px padding for the text Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: for i = (#tablist)+1, #tabs, 1 do Zerotorescue@0: tabs[i]:Hide() Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --First pass, find the minimum number of rows needed to hold all tabs and the initial tab layout Zerotorescue@0: local numtabs = #tablist Zerotorescue@0: local numrows = 1 Zerotorescue@0: local usedwidth = 0 Zerotorescue@0: Zerotorescue@0: for i = 1, #tablist do Zerotorescue@0: --If this is not the first tab of a row and there isn't room for it Zerotorescue@0: if usedwidth ~= 0 and (width - usedwidth - widths[i]) < 0 then Zerotorescue@0: rowwidths[numrows] = usedwidth + 10 --first tab in each row takes up an extra 10px Zerotorescue@0: rowends[numrows] = i - 1 Zerotorescue@0: numrows = numrows + 1 Zerotorescue@0: usedwidth = 0 Zerotorescue@0: end Zerotorescue@0: usedwidth = usedwidth + widths[i] Zerotorescue@0: end Zerotorescue@0: rowwidths[numrows] = usedwidth + 10 --first tab in each row takes up an extra 10px Zerotorescue@0: rowends[numrows] = #tablist Zerotorescue@0: Zerotorescue@0: --Fix for single tabs being left on the last row, move a tab from the row above if applicable Zerotorescue@0: if numrows > 1 then Zerotorescue@0: --if the last row has only one tab Zerotorescue@0: if rowends[numrows-1] == numtabs-1 then Zerotorescue@0: --if there are more than 2 tabs in the 2nd last row Zerotorescue@0: if (numrows == 2 and rowends[numrows-1] > 2) or (rowends[numrows] - rowends[numrows-1] > 2) then Zerotorescue@0: --move 1 tab from the second last row to the last, if there is enough space Zerotorescue@0: if (rowwidths[numrows] + widths[numtabs-1]) <= width then Zerotorescue@0: rowends[numrows-1] = rowends[numrows-1] - 1 Zerotorescue@0: rowwidths[numrows] = rowwidths[numrows] + widths[numtabs-1] Zerotorescue@0: rowwidths[numrows-1] = rowwidths[numrows-1] - widths[numtabs-1] Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --anchor the rows as defined and resize tabs to fill thier row Zerotorescue@0: local starttab = 1 Zerotorescue@0: for row, endtab in ipairs(rowends) do Zerotorescue@0: local first = true Zerotorescue@0: for tabno = starttab, endtab do Zerotorescue@0: local tab = tabs[tabno] Zerotorescue@0: tab:ClearAllPoints() Zerotorescue@0: if first then Zerotorescue@0: tab:SetPoint("TOPLEFT", self.frame, "TOPLEFT", 0, -(hastitle and 14 or 7)-(row-1)*20 ) Zerotorescue@0: first = false Zerotorescue@0: else Zerotorescue@0: tab:SetPoint("LEFT", tabs[tabno-1], "RIGHT", -10, 0) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: -- equal padding for each tab to fill the available width, Zerotorescue@0: -- if the used space is above 75% already Zerotorescue@0: local padding = 0 Zerotorescue@0: if not (numrows == 1 and rowwidths[1] < width*0.75) then Zerotorescue@0: padding = (width - rowwidths[row]) / (endtab - starttab+1) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: for i = starttab, endtab do Zerotorescue@0: PanelTemplates_TabResize(tabs[i], padding + 4, nil, width) Zerotorescue@0: end Zerotorescue@0: starttab = endtab + 1 Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: self.borderoffset = (hastitle and 17 or 10)+((numrows)*20) Zerotorescue@0: self.border:SetPoint("TOPLEFT", 1, -self.borderoffset) Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["OnWidthSet"] = function(self, width) Zerotorescue@0: local content = self.content Zerotorescue@0: local contentwidth = width - 60 Zerotorescue@0: if contentwidth < 0 then Zerotorescue@0: contentwidth = 0 Zerotorescue@0: end Zerotorescue@0: content:SetWidth(contentwidth) Zerotorescue@0: content.width = contentwidth Zerotorescue@0: self:BuildTabs(self) Zerotorescue@0: self.frame:SetScript("OnUpdate", BuildTabsOnUpdate) Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["OnHeightSet"] = function(self, height) Zerotorescue@0: local content = self.content Zerotorescue@0: local contentheight = height - (self.borderoffset + 23) Zerotorescue@0: if contentheight < 0 then Zerotorescue@0: contentheight = 0 Zerotorescue@0: end Zerotorescue@0: content:SetHeight(contentheight) Zerotorescue@0: content.height = contentheight Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["LayoutFinished"] = function(self, width, height) Zerotorescue@0: if self.noAutoHeight then return end Zerotorescue@0: self:SetHeight((height or 0) + (self.borderoffset + 23)) Zerotorescue@0: end Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Constructor Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local PaneBackdrop = { Zerotorescue@0: bgFile = "Interface\\ChatFrame\\ChatFrameBackground", Zerotorescue@0: edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", Zerotorescue@0: tile = true, tileSize = 16, edgeSize = 16, Zerotorescue@0: insets = { left = 3, right = 3, top = 5, bottom = 3 } Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: local function Constructor() Zerotorescue@0: local num = AceGUI:GetNextWidgetNum(Type) Zerotorescue@0: local frame = CreateFrame("Frame",nil,UIParent) Zerotorescue@0: frame:SetHeight(100) Zerotorescue@0: frame:SetWidth(100) Zerotorescue@0: frame:SetFrameStrata("FULLSCREEN_DIALOG") Zerotorescue@0: Zerotorescue@0: local titletext = frame:CreateFontString(nil,"OVERLAY","GameFontNormal") Zerotorescue@0: titletext:SetPoint("TOPLEFT", 14, 0) Zerotorescue@0: titletext:SetPoint("TOPRIGHT", -14, 0) Zerotorescue@0: titletext:SetJustifyH("LEFT") Zerotorescue@0: titletext:SetHeight(18) Zerotorescue@0: titletext:SetText("") Zerotorescue@0: Zerotorescue@0: local border = CreateFrame("Frame", nil, frame) Zerotorescue@0: border:SetPoint("TOPLEFT", 1, -27) Zerotorescue@0: border:SetPoint("BOTTOMRIGHT", -1, 3) Zerotorescue@0: border:SetBackdrop(PaneBackdrop) Zerotorescue@0: border:SetBackdropColor(0.1, 0.1, 0.1, 0.5) Zerotorescue@0: border:SetBackdropBorderColor(0.4, 0.4, 0.4) Zerotorescue@0: Zerotorescue@0: local content = CreateFrame("Frame", nil, border) Zerotorescue@0: content:SetPoint("TOPLEFT", 10, -7) Zerotorescue@0: content:SetPoint("BOTTOMRIGHT", -10, 7) Zerotorescue@0: Zerotorescue@0: local widget = { Zerotorescue@0: num = num, Zerotorescue@0: frame = frame, Zerotorescue@0: localstatus = {}, Zerotorescue@0: alignoffset = 18, Zerotorescue@0: titletext = titletext, Zerotorescue@0: border = border, Zerotorescue@0: borderoffset = 27, Zerotorescue@0: tabs = {}, Zerotorescue@0: content = content, Zerotorescue@0: type = Type Zerotorescue@0: } Zerotorescue@0: for method, func in pairs(methods) do Zerotorescue@0: widget[method] = func Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: return AceGUI:RegisterAsContainer(widget) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: AceGUI:RegisterWidgetType(Type, Constructor, Version)