annotate Libs/AceGUI-3.0/widgets/AceGUIContainer-TabGroup.lua @ 25:ac501d71c890 tip

Added tag v8.2.0.024 for changeset 389dcaeebc47
author Tercioo
date Fri, 28 Jun 2019 20:07:27 -0300
parents 3000eccbf1a0
children
rev   line source
tercio@0 1 --[[-----------------------------------------------------------------------------
tercio@0 2 TabGroup Container
tercio@0 3 Container that uses tabs on top to switch between groups.
tercio@0 4 -------------------------------------------------------------------------------]]
Tercio@17 5 local Type, Version = "TabGroup", 36
tercio@0 6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
tercio@0 7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
tercio@0 8
tercio@0 9 -- Lua APIs
tercio@0 10 local pairs, ipairs, assert, type, wipe = pairs, ipairs, assert, type, wipe
tercio@0 11
tercio@0 12 -- WoW APIs
tercio@0 13 local PlaySound = PlaySound
tercio@0 14 local CreateFrame, UIParent = CreateFrame, UIParent
tercio@0 15 local _G = _G
tercio@0 16
tercio@0 17 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
tercio@0 18 -- List them here for Mikk's FindGlobals script
tercio@0 19 -- GLOBALS: PanelTemplates_TabResize, PanelTemplates_SetDisabledTabState, PanelTemplates_SelectTab, PanelTemplates_DeselectTab
tercio@0 20
tercio@0 21 -- local upvalue storage used by BuildTabs
tercio@0 22 local widths = {}
tercio@0 23 local rowwidths = {}
tercio@0 24 local rowends = {}
tercio@0 25
tercio@0 26 --[[-----------------------------------------------------------------------------
tercio@0 27 Support functions
tercio@0 28 -------------------------------------------------------------------------------]]
tercio@0 29 local function UpdateTabLook(frame)
tercio@0 30 if frame.disabled then
tercio@0 31 PanelTemplates_SetDisabledTabState(frame)
tercio@0 32 elseif frame.selected then
tercio@0 33 PanelTemplates_SelectTab(frame)
tercio@0 34 else
tercio@0 35 PanelTemplates_DeselectTab(frame)
tercio@0 36 end
tercio@0 37 end
tercio@0 38
tercio@0 39 local function Tab_SetText(frame, text)
tercio@0 40 frame:_SetText(text)
tercio@0 41 local width = frame.obj.frame.width or frame.obj.frame:GetWidth() or 0
tercio@0 42 PanelTemplates_TabResize(frame, 0, nil, nil, width, frame:GetFontString():GetStringWidth())
tercio@0 43 end
tercio@0 44
tercio@0 45 local function Tab_SetSelected(frame, selected)
tercio@0 46 frame.selected = selected
tercio@0 47 UpdateTabLook(frame)
tercio@0 48 end
tercio@0 49
tercio@0 50 local function Tab_SetDisabled(frame, disabled)
tercio@0 51 frame.disabled = disabled
tercio@0 52 UpdateTabLook(frame)
tercio@0 53 end
tercio@0 54
tercio@0 55 local function BuildTabsOnUpdate(frame)
tercio@0 56 local self = frame.obj
tercio@0 57 self:BuildTabs()
tercio@0 58 frame:SetScript("OnUpdate", nil)
tercio@0 59 end
tercio@0 60
tercio@0 61 --[[-----------------------------------------------------------------------------
tercio@0 62 Scripts
tercio@0 63 -------------------------------------------------------------------------------]]
tercio@0 64 local function Tab_OnClick(frame)
tercio@0 65 if not (frame.selected or frame.disabled) then
Tercio@17 66 PlaySound(841) -- SOUNDKIT.IG_CHARACTER_INFO_TAB
tercio@0 67 frame.obj:SelectTab(frame.value)
tercio@0 68 end
tercio@0 69 end
tercio@0 70
tercio@0 71 local function Tab_OnEnter(frame)
tercio@0 72 local self = frame.obj
tercio@0 73 self:Fire("OnTabEnter", self.tabs[frame.id].value, frame)
tercio@0 74 end
tercio@0 75
tercio@0 76 local function Tab_OnLeave(frame)
tercio@0 77 local self = frame.obj
tercio@0 78 self:Fire("OnTabLeave", self.tabs[frame.id].value, frame)
tercio@0 79 end
tercio@0 80
tercio@0 81 local function Tab_OnShow(frame)
tercio@0 82 _G[frame:GetName().."HighlightTexture"]:SetWidth(frame:GetTextWidth() + 30)
tercio@0 83 end
tercio@0 84
tercio@0 85 --[[-----------------------------------------------------------------------------
tercio@0 86 Methods
tercio@0 87 -------------------------------------------------------------------------------]]
tercio@0 88 local methods = {
tercio@0 89 ["OnAcquire"] = function(self)
tercio@0 90 self:SetTitle()
tercio@0 91 end,
tercio@0 92
tercio@0 93 ["OnRelease"] = function(self)
tercio@0 94 self.status = nil
tercio@0 95 for k in pairs(self.localstatus) do
tercio@0 96 self.localstatus[k] = nil
tercio@0 97 end
tercio@0 98 self.tablist = nil
tercio@0 99 for _, tab in pairs(self.tabs) do
tercio@0 100 tab:Hide()
tercio@0 101 end
tercio@0 102 end,
tercio@0 103
tercio@0 104 ["CreateTab"] = function(self, id)
tercio@0 105 local tabname = ("AceGUITabGroup%dTab%d"):format(self.num, id)
tercio@0 106 local tab = CreateFrame("Button", tabname, self.border, "OptionsFrameTabButtonTemplate")
tercio@0 107 tab.obj = self
tercio@0 108 tab.id = id
tercio@0 109
tercio@0 110 tab.text = _G[tabname .. "Text"]
tercio@0 111 tab.text:ClearAllPoints()
tercio@0 112 tab.text:SetPoint("LEFT", 14, -3)
tercio@0 113 tab.text:SetPoint("RIGHT", -12, -3)
tercio@0 114
tercio@0 115 tab:SetScript("OnClick", Tab_OnClick)
tercio@0 116 tab:SetScript("OnEnter", Tab_OnEnter)
tercio@0 117 tab:SetScript("OnLeave", Tab_OnLeave)
tercio@0 118 tab:SetScript("OnShow", Tab_OnShow)
tercio@0 119
tercio@0 120 tab._SetText = tab.SetText
tercio@0 121 tab.SetText = Tab_SetText
tercio@0 122 tab.SetSelected = Tab_SetSelected
tercio@0 123 tab.SetDisabled = Tab_SetDisabled
tercio@0 124
tercio@0 125 return tab
tercio@0 126 end,
tercio@0 127
tercio@0 128 ["SetTitle"] = function(self, text)
tercio@0 129 self.titletext:SetText(text or "")
tercio@0 130 if text and text ~= "" then
tercio@0 131 self.alignoffset = 25
tercio@0 132 else
tercio@0 133 self.alignoffset = 18
tercio@0 134 end
tercio@0 135 self:BuildTabs()
tercio@0 136 end,
tercio@0 137
tercio@0 138 ["SetStatusTable"] = function(self, status)
tercio@0 139 assert(type(status) == "table")
tercio@0 140 self.status = status
tercio@0 141 end,
tercio@0 142
tercio@0 143 ["SelectTab"] = function(self, value)
tercio@0 144 local status = self.status or self.localstatus
tercio@0 145 local found
tercio@0 146 for i, v in ipairs(self.tabs) do
tercio@0 147 if v.value == value then
tercio@0 148 v:SetSelected(true)
tercio@0 149 found = true
tercio@0 150 else
tercio@0 151 v:SetSelected(false)
tercio@0 152 end
tercio@0 153 end
tercio@0 154 status.selected = value
tercio@0 155 if found then
tercio@0 156 self:Fire("OnGroupSelected",value)
tercio@0 157 end
tercio@0 158 end,
tercio@0 159
tercio@0 160 ["SetTabs"] = function(self, tabs)
tercio@0 161 self.tablist = tabs
tercio@0 162 self:BuildTabs()
tercio@0 163 end,
tercio@0 164
tercio@0 165
tercio@0 166 ["BuildTabs"] = function(self)
tercio@0 167 local hastitle = (self.titletext:GetText() and self.titletext:GetText() ~= "")
tercio@0 168 local status = self.status or self.localstatus
tercio@0 169 local tablist = self.tablist
tercio@0 170 local tabs = self.tabs
tercio@0 171
tercio@0 172 if not tablist then return end
tercio@0 173
tercio@0 174 local width = self.frame.width or self.frame:GetWidth() or 0
tercio@0 175
tercio@0 176 wipe(widths)
tercio@0 177 wipe(rowwidths)
tercio@0 178 wipe(rowends)
tercio@0 179
tercio@0 180 --Place Text into tabs and get thier initial width
tercio@0 181 for i, v in ipairs(tablist) do
tercio@0 182 local tab = tabs[i]
tercio@0 183 if not tab then
tercio@0 184 tab = self:CreateTab(i)
tercio@0 185 tabs[i] = tab
tercio@0 186 end
tercio@0 187
tercio@0 188 tab:Show()
tercio@0 189 tab:SetText(v.text)
tercio@0 190 tab:SetDisabled(v.disabled)
tercio@0 191 tab.value = v.value
tercio@0 192
tercio@0 193 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@0 194 end
tercio@0 195
tercio@0 196 for i = (#tablist)+1, #tabs, 1 do
tercio@0 197 tabs[i]:Hide()
tercio@0 198 end
tercio@0 199
tercio@0 200 --First pass, find the minimum number of rows needed to hold all tabs and the initial tab layout
tercio@0 201 local numtabs = #tablist
tercio@0 202 local numrows = 1
tercio@0 203 local usedwidth = 0
tercio@0 204
tercio@0 205 for i = 1, #tablist do
tercio@0 206 --If this is not the first tab of a row and there isn't room for it
tercio@0 207 if usedwidth ~= 0 and (width - usedwidth - widths[i]) < 0 then
tercio@0 208 rowwidths[numrows] = usedwidth + 10 --first tab in each row takes up an extra 10px
tercio@0 209 rowends[numrows] = i - 1
tercio@0 210 numrows = numrows + 1
tercio@0 211 usedwidth = 0
tercio@0 212 end
tercio@0 213 usedwidth = usedwidth + widths[i]
tercio@0 214 end
tercio@0 215 rowwidths[numrows] = usedwidth + 10 --first tab in each row takes up an extra 10px
tercio@0 216 rowends[numrows] = #tablist
tercio@0 217
tercio@0 218 --Fix for single tabs being left on the last row, move a tab from the row above if applicable
tercio@0 219 if numrows > 1 then
tercio@0 220 --if the last row has only one tab
tercio@0 221 if rowends[numrows-1] == numtabs-1 then
tercio@0 222 --if there are more than 2 tabs in the 2nd last row
tercio@0 223 if (numrows == 2 and rowends[numrows-1] > 2) or (rowends[numrows] - rowends[numrows-1] > 2) then
tercio@0 224 --move 1 tab from the second last row to the last, if there is enough space
tercio@0 225 if (rowwidths[numrows] + widths[numtabs-1]) <= width then
tercio@0 226 rowends[numrows-1] = rowends[numrows-1] - 1
tercio@0 227 rowwidths[numrows] = rowwidths[numrows] + widths[numtabs-1]
tercio@0 228 rowwidths[numrows-1] = rowwidths[numrows-1] - widths[numtabs-1]
tercio@0 229 end
tercio@0 230 end
tercio@0 231 end
tercio@0 232 end
tercio@0 233
tercio@0 234 --anchor the rows as defined and resize tabs to fill thier row
tercio@0 235 local starttab = 1
tercio@0 236 for row, endtab in ipairs(rowends) do
tercio@0 237 local first = true
tercio@0 238 for tabno = starttab, endtab do
tercio@0 239 local tab = tabs[tabno]
tercio@0 240 tab:ClearAllPoints()
tercio@0 241 if first then
tercio@0 242 tab:SetPoint("TOPLEFT", self.frame, "TOPLEFT", 0, -(hastitle and 14 or 7)-(row-1)*20 )
tercio@0 243 first = false
tercio@0 244 else
tercio@0 245 tab:SetPoint("LEFT", tabs[tabno-1], "RIGHT", -10, 0)
tercio@0 246 end
tercio@0 247 end
tercio@0 248
tercio@0 249 -- equal padding for each tab to fill the available width,
tercio@0 250 -- if the used space is above 75% already
tercio@0 251 -- the 18 pixel is the typical width of a scrollbar, so we can have a tab group inside a scrolling frame,
tercio@0 252 -- and not have the tabs jump around funny when switching between tabs that need scrolling and those that don't
tercio@0 253 local padding = 0
tercio@0 254 if not (numrows == 1 and rowwidths[1] < width*0.75 - 18) then
tercio@0 255 padding = (width - rowwidths[row]) / (endtab - starttab+1)
tercio@0 256 end
tercio@0 257
tercio@0 258 for i = starttab, endtab do
tercio@0 259 PanelTemplates_TabResize(tabs[i], padding + 4, nil, nil, width, tabs[i]:GetFontString():GetStringWidth())
tercio@0 260 end
tercio@0 261 starttab = endtab + 1
tercio@0 262 end
tercio@0 263
tercio@0 264 self.borderoffset = (hastitle and 17 or 10)+((numrows)*20)
tercio@0 265 self.border:SetPoint("TOPLEFT", 1, -self.borderoffset)
tercio@0 266 end,
tercio@0 267
tercio@0 268 ["OnWidthSet"] = function(self, width)
tercio@0 269 local content = self.content
tercio@0 270 local contentwidth = width - 60
tercio@0 271 if contentwidth < 0 then
tercio@0 272 contentwidth = 0
tercio@0 273 end
tercio@0 274 content:SetWidth(contentwidth)
tercio@0 275 content.width = contentwidth
tercio@0 276 self:BuildTabs(self)
tercio@0 277 self.frame:SetScript("OnUpdate", BuildTabsOnUpdate)
tercio@0 278 end,
tercio@0 279
tercio@0 280 ["OnHeightSet"] = function(self, height)
tercio@0 281 local content = self.content
tercio@0 282 local contentheight = height - (self.borderoffset + 23)
tercio@0 283 if contentheight < 0 then
tercio@0 284 contentheight = 0
tercio@0 285 end
tercio@0 286 content:SetHeight(contentheight)
tercio@0 287 content.height = contentheight
tercio@0 288 end,
tercio@0 289
tercio@0 290 ["LayoutFinished"] = function(self, width, height)
tercio@0 291 if self.noAutoHeight then return end
tercio@0 292 self:SetHeight((height or 0) + (self.borderoffset + 23))
tercio@0 293 end
tercio@0 294 }
tercio@0 295
tercio@0 296 --[[-----------------------------------------------------------------------------
tercio@0 297 Constructor
tercio@0 298 -------------------------------------------------------------------------------]]
tercio@0 299 local PaneBackdrop = {
tercio@0 300 bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
tercio@0 301 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tercio@0 302 tile = true, tileSize = 16, edgeSize = 16,
tercio@0 303 insets = { left = 3, right = 3, top = 5, bottom = 3 }
tercio@0 304 }
tercio@0 305
tercio@0 306 local function Constructor()
tercio@0 307 local num = AceGUI:GetNextWidgetNum(Type)
tercio@0 308 local frame = CreateFrame("Frame",nil,UIParent)
tercio@0 309 frame:SetHeight(100)
tercio@0 310 frame:SetWidth(100)
tercio@0 311 frame:SetFrameStrata("FULLSCREEN_DIALOG")
tercio@0 312
tercio@0 313 local titletext = frame:CreateFontString(nil,"OVERLAY","GameFontNormal")
tercio@0 314 titletext:SetPoint("TOPLEFT", 14, 0)
tercio@0 315 titletext:SetPoint("TOPRIGHT", -14, 0)
tercio@0 316 titletext:SetJustifyH("LEFT")
tercio@0 317 titletext:SetHeight(18)
tercio@0 318 titletext:SetText("")
tercio@0 319
tercio@0 320 local border = CreateFrame("Frame", nil, frame)
tercio@0 321 border:SetPoint("TOPLEFT", 1, -27)
tercio@0 322 border:SetPoint("BOTTOMRIGHT", -1, 3)
tercio@0 323 border:SetBackdrop(PaneBackdrop)
tercio@0 324 border:SetBackdropColor(0.1, 0.1, 0.1, 0.5)
tercio@0 325 border:SetBackdropBorderColor(0.4, 0.4, 0.4)
tercio@0 326
tercio@0 327 local content = CreateFrame("Frame", nil, border)
tercio@0 328 content:SetPoint("TOPLEFT", 10, -7)
tercio@0 329 content:SetPoint("BOTTOMRIGHT", -10, 7)
tercio@0 330
tercio@0 331 local widget = {
tercio@0 332 num = num,
tercio@0 333 frame = frame,
tercio@0 334 localstatus = {},
tercio@0 335 alignoffset = 18,
tercio@0 336 titletext = titletext,
tercio@0 337 border = border,
tercio@0 338 borderoffset = 27,
tercio@0 339 tabs = {},
tercio@0 340 content = content,
tercio@0 341 type = Type
tercio@0 342 }
tercio@0 343 for method, func in pairs(methods) do
tercio@0 344 widget[method] = func
tercio@0 345 end
tercio@0 346
tercio@0 347 return AceGUI:RegisterAsContainer(widget)
tercio@0 348 end
tercio@0 349
tercio@0 350 AceGUI:RegisterWidgetType(Type, Constructor, Version)