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