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