comparison ui/AmrUiTabGroup.lua @ 57:01b63b8ed811 v21

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