annotate Libs/AceGUI-3.0/widgets/AceGUIContainer-TabGroup.lua @ 0:98c6f55e6619

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