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