annotate ui/AmrUiTabGroup.lua @ 168:0d037afc7128

Added tag v78 for changeset 5c68d3fccff3
author yellowfive
date Wed, 10 Jul 2019 12:53:50 -0700
parents a0894ffebd15
children
rev   line source
yellowfive@57 1 --[[-----------------------------------------------------------------------------
yellowfive@57 2 AMR TabGroup Container
yellowfive@57 3 Container that uses tabs on top to switch between groups.
yellowfive@57 4 This is adapted from AceGUIContainer-TabGroup, but has a custom look.
yellowfive@57 5 -------------------------------------------------------------------------------]]
yellowfive@57 6 local Type, Version = "AmrUiTabGroup", 1
yellowfive@57 7 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
yellowfive@57 8 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
yellowfive@57 9
yellowfive@57 10 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot")
yellowfive@57 11
yellowfive@57 12 -- Lua APIs
yellowfive@133 13 local pairs, ipairs, assert, type = pairs, ipairs, assert, type
yellowfive@57 14
yellowfive@57 15 -- WoW APIs
yellowfive@57 16 local CreateFrame, UIParent = CreateFrame, UIParent
yellowfive@57 17 local _G = _G
yellowfive@57 18
yellowfive@57 19
yellowfive@57 20 --[[-----------------------------------------------------------------------------
yellowfive@57 21 Support functions
yellowfive@57 22 -------------------------------------------------------------------------------]]
yellowfive@57 23
yellowfive@57 24 local function tabSetSelected(frame, selected)
yellowfive@57 25 frame.selected = selected
yellowfive@57 26 end
yellowfive@57 27
yellowfive@57 28 local function tabSetDisabled(frame, disabled)
yellowfive@57 29 frame.disabled = disabled
yellowfive@57 30 end
yellowfive@57 31
yellowfive@57 32 local function buildTabsOnUpdate(frame)
yellowfive@57 33 local self = frame.obj
yellowfive@57 34 self:BuildTabs()
yellowfive@57 35 frame:SetScript("OnUpdate", nil)
yellowfive@57 36 end
yellowfive@57 37
yellowfive@57 38
yellowfive@57 39 --[[-----------------------------------------------------------------------------
yellowfive@57 40 Scripts
yellowfive@57 41 -------------------------------------------------------------------------------]]
yellowfive@57 42 local function tabOnClick(frame)
yellowfive@57 43 if not (frame.selected or frame.disabled) then
yellowfive@112 44 --PlaySound("igCharacterInfoTab")
yellowfive@57 45 frame.obj:SelectTab(frame.value)
yellowfive@57 46 end
yellowfive@57 47 end
yellowfive@57 48
yellowfive@57 49
yellowfive@57 50 --[[-----------------------------------------------------------------------------
yellowfive@57 51 Methods
yellowfive@57 52 -------------------------------------------------------------------------------]]
yellowfive@57 53 local methods = {
yellowfive@57 54 ["OnAcquire"] = function(self)
yellowfive@57 55 self.tabSelector:Hide()
yellowfive@57 56 self.frame:ClearAllPoints()
yellowfive@57 57 end,
yellowfive@57 58
yellowfive@57 59 ["OnRelease"] = function(self)
yellowfive@57 60 self.status = nil
yellowfive@57 61 for k in pairs(self.localstatus) do
yellowfive@57 62 self.localstatus[k] = nil
yellowfive@57 63 end
yellowfive@57 64 self.tablist = nil
yellowfive@57 65 for _, tab in pairs(self.tabs) do
yellowfive@57 66 tab:Hide()
yellowfive@57 67 end
yellowfive@57 68 self.tabSelector:Hide()
yellowfive@57 69 end,
yellowfive@57 70
yellowfive@57 71 ["CreateTab"] = function(self, id, style)
yellowfive@57 72 local tabname = ("AmrUiTabGroup%dTab%d"):format(self.num, id)
yellowfive@57 73
yellowfive@57 74 local tab = CreateFrame("Button", tabname, self.border)
yellowfive@57 75 tab.obj = self
yellowfive@57 76 tab.id = id
yellowfive@57 77
yellowfive@57 78 if style == "bold" then
yellowfive@57 79 tab:SetNormalFontObject(Amr.CreateFont("Regular", 24, Amr.Colors.TextHeaderDisabled))
yellowfive@57 80 tab:SetHighlightFontObject(Amr.CreateFont("Regular", 24, Amr.Colors.TextHover))
yellowfive@57 81 else
yellowfive@57 82 tab:SetNormalFontObject(Amr.CreateFont("Regular", 28, Amr.Colors.Text))
yellowfive@57 83 tab:SetHighlightFontObject(Amr.CreateFont("Regular", 28, Amr.Colors.TextHover))
yellowfive@57 84 end
yellowfive@57 85
yellowfive@57 86 tab:SetScript("OnClick", tabOnClick)
yellowfive@57 87
yellowfive@57 88 tab.SetSelected = tabSetSelected
yellowfive@57 89 tab.SetDisabled = tabSetDisabled
yellowfive@57 90
yellowfive@57 91 return tab
yellowfive@57 92 end,
yellowfive@57 93
yellowfive@57 94 ["SetStatusTable"] = function(self, status)
yellowfive@57 95 assert(type(status) == "table")
yellowfive@57 96 self.status = status
yellowfive@57 97 end,
yellowfive@57 98
yellowfive@57 99 ["SelectTab"] = function(self, value)
yellowfive@57 100 local status = self.status or self.localstatus
yellowfive@57 101
yellowfive@57 102 self.tabSelector:Hide()
yellowfive@57 103
yellowfive@57 104 local found
yellowfive@57 105 for i, v in ipairs(self.tabs) do
yellowfive@57 106 if v.value == value then
yellowfive@57 107 v:SetSelected(true)
yellowfive@57 108 found = true
yellowfive@57 109
yellowfive@57 110 -- show the tab selector under the proper tab
yellowfive@57 111 if v.style == "underline" then
yellowfive@57 112 self.tabSelector:SetWidth(v:GetWidth())
yellowfive@57 113 self.tabSelector:ClearAllPoints()
yellowfive@57 114 self.tabSelector:SetPoint("TOPLEFT", v, "BOTTOMLEFT", 0, -2)
yellowfive@57 115 self.tabSelector:Show()
yellowfive@57 116 v:SetNormalFontObject(Amr.CreateFont("Regular", 28, Amr.Colors.Text))
yellowfive@57 117 v:SetHighlightFontObject(Amr.CreateFont("Regular", 28, Amr.Colors.Text))
yellowfive@57 118 elseif v.style == "bold" then
yellowfive@57 119 v:SetNormalFontObject(Amr.CreateFont("Bold", 28, Amr.Colors.TextHeaderActive))
yellowfive@57 120 v:SetHighlightFontObject(Amr.CreateFont("Bold", 28, Amr.Colors.TextHeaderActive))
yellowfive@57 121 end
yellowfive@57 122 else
yellowfive@57 123 v:SetSelected(false)
yellowfive@57 124 if v.style == "bold" then
yellowfive@57 125 v:SetNormalFontObject(Amr.CreateFont("Regular", 24, Amr.Colors.TextHeaderDisabled))
yellowfive@57 126 v:SetHighlightFontObject(Amr.CreateFont("Regular", 24, Amr.Colors.TextHover))
yellowfive@57 127 else
yellowfive@57 128 v:SetNormalFontObject(Amr.CreateFont("Regular", 28, Amr.Colors.Text))
yellowfive@57 129 v:SetHighlightFontObject(Amr.CreateFont("Regular", 28, Amr.Colors.TextHover))
yellowfive@57 130 end
yellowfive@57 131 end
yellowfive@57 132 end
yellowfive@57 133 status.selected = value
yellowfive@57 134
yellowfive@57 135 -- call this to reposition after style change
yellowfive@57 136 self:BuildTabs()
yellowfive@57 137
yellowfive@57 138 if found then
yellowfive@57 139 self:Fire("OnGroupSelected",value)
yellowfive@57 140 end
yellowfive@57 141 end,
yellowfive@57 142
yellowfive@57 143 ["SetTabs"] = function(self, tabs)
yellowfive@57 144 self.tablist = tabs
yellowfive@57 145 self:BuildTabs()
yellowfive@57 146 end,
yellowfive@57 147
yellowfive@57 148 ["BuildTabs"] = function(self)
yellowfive@57 149 local status = self.status or self.localstatus
yellowfive@57 150 local tablist = self.tablist
yellowfive@57 151 local tabs = self.tabs
yellowfive@57 152
yellowfive@57 153 if not tablist then return end
yellowfive@57 154
yellowfive@57 155 local first = true
yellowfive@57 156 for i, v in ipairs(tablist) do
yellowfive@57 157 local tab = tabs[i]
yellowfive@57 158 if not tab then
yellowfive@57 159 tab = self:CreateTab(i)
yellowfive@57 160 tabs[i] = tab
yellowfive@57 161 end
yellowfive@57 162
yellowfive@57 163 local padding = 20
yellowfive@57 164 if v.style == "bold" then padding = 0 end
yellowfive@57 165
yellowfive@57 166 tab:Show()
yellowfive@57 167 tab:SetText(v.text)
yellowfive@57 168 tab:SetWidth(tab:GetTextWidth() + padding)
yellowfive@57 169 tab:SetHeight(tab:GetTextHeight())
yellowfive@57 170 tab:SetDisabled(v.disabled)
yellowfive@57 171 tab.value = v.value
yellowfive@57 172 tab.style = v.style or "underline"
yellowfive@57 173
yellowfive@57 174 tab:ClearAllPoints()
yellowfive@57 175 if first then
yellowfive@57 176 local firstOffset = 0
yellowfive@57 177 if tab.style == "bold" then firstOffset = 4 end
yellowfive@57 178 tab:SetPoint("BOTTOMLEFT", self.border, "TOPLEFT", firstOffset, 8)
yellowfive@57 179 first = false
yellowfive@57 180 else
yellowfive@57 181 tab:SetPoint("LEFT", tabs[i - 1], "RIGHT", 30, 0)
yellowfive@57 182 end
yellowfive@57 183 end
yellowfive@57 184 end,
yellowfive@57 185
yellowfive@57 186 ["OnWidthSet"] = function(self, width)
yellowfive@57 187 local content = self.content
yellowfive@57 188 local contentwidth = width - 60
yellowfive@57 189 if contentwidth < 0 then
yellowfive@57 190 contentwidth = 0
yellowfive@57 191 end
yellowfive@57 192 content:SetWidth(contentwidth)
yellowfive@57 193 content.width = contentwidth
yellowfive@57 194 self:BuildTabs(self)
yellowfive@57 195 self.frame:SetScript("OnUpdate", buildTabsOnUpdate)
yellowfive@57 196 end,
yellowfive@57 197
yellowfive@57 198 ["OnHeightSet"] = function(self, height)
yellowfive@57 199 local content = self.content
yellowfive@57 200 local contentheight = height - (self.borderoffset + 23)
yellowfive@57 201 if contentheight < 0 then
yellowfive@57 202 contentheight = 0
yellowfive@57 203 end
yellowfive@57 204 content:SetHeight(contentheight)
yellowfive@57 205 content.height = contentheight
yellowfive@57 206 end,
yellowfive@57 207
yellowfive@57 208 ["LayoutFinished"] = function(self, width, height)
yellowfive@57 209 if self.noAutoHeight then return end
yellowfive@57 210 self:SetHeight((height or 0) + (self.borderoffset + 23))
yellowfive@57 211 end,
yellowfive@57 212
yellowfive@57 213 ["SetVisible"] = function(self, visible)
yellowfive@57 214 if visible then
yellowfive@57 215 self.frame:Show()
yellowfive@57 216 else
yellowfive@57 217 self.frame:Hide()
yellowfive@57 218 end
yellowfive@57 219 end
yellowfive@57 220 }
yellowfive@57 221
yellowfive@57 222 --[[-----------------------------------------------------------------------------
yellowfive@57 223 Constructor
yellowfive@57 224 -------------------------------------------------------------------------------]]
yellowfive@57 225 local function Constructor()
yellowfive@57 226 local num = AceGUI:GetNextWidgetNum(Type)
yellowfive@124 227 local frame = CreateFrame("Frame", nil, UIParent)
yellowfive@57 228 frame:SetHeight(100)
yellowfive@57 229 frame:SetWidth(100)
yellowfive@57 230 frame:SetFrameStrata("FULLSCREEN_DIALOG")
yellowfive@57 231
yellowfive@57 232 local border = CreateFrame("Frame", nil, frame)
yellowfive@57 233 local borderoffset = 30
yellowfive@57 234 border:SetPoint("TOPLEFT", 0, -borderoffset)
yellowfive@57 235 border:SetPoint("BOTTOMRIGHT", 0, 3)
yellowfive@57 236
yellowfive@57 237 local line = border:CreateTexture(nil, "ARTWORK")
yellowfive@57 238 line:Hide()
yellowfive@81 239 line:SetColorTexture(1, 1, 1, 1)
yellowfive@57 240 line:SetHeight(4)
yellowfive@57 241
yellowfive@57 242 local content = CreateFrame("Frame", nil, border)
yellowfive@57 243 content:SetPoint("TOPLEFT", 0, 0)
yellowfive@57 244 content:SetPoint("BOTTOMRIGHT", 0, 0)
yellowfive@57 245
yellowfive@57 246 local widget = {
yellowfive@57 247 num = num,
yellowfive@57 248 frame = frame,
yellowfive@57 249 localstatus = {},
yellowfive@57 250 alignoffset = 18,
yellowfive@57 251 border = border,
yellowfive@57 252 borderoffset = borderoffset,
yellowfive@57 253 tabs = {},
yellowfive@57 254 tabSelector = line,
yellowfive@57 255 content = content,
yellowfive@57 256 type = Type
yellowfive@57 257 }
yellowfive@57 258 for method, func in pairs(methods) do
yellowfive@57 259 widget[method] = func
yellowfive@57 260 end
yellowfive@57 261
yellowfive@57 262 return AceGUI:RegisterAsContainer(widget)
yellowfive@57 263 end
yellowfive@57 264
yellowfive@57 265 AceGUI:RegisterWidgetType(Type, Constructor, Version)