-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtabcontainer.lua
489 lines (420 loc) · 19.2 KB
/
tabcontainer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
local detailsFramework = DetailsFramework
if (not detailsFramework or not DetailsFrameworkCanLoad) then
return
end
local unpack = unpack
local CreateFrame = CreateFrame
local PixelUtil = PixelUtil
---@class df_tabinfotable : table
---@field name string
---@field text string
---@field createOnDemandFunc function?
---@class df_tabcontainer : frame
---@field AllFrames df_tabcontainerframe[]
---@field AllButtons df_tabcontainerbutton[]
---@field AllFramesByName table<string, df_tabcontainerframe>
---@field AllButtonsByName table<string, df_tabcontainerbutton>
---@field hookList table
---@field options df_tabcontaineroptions
---@field CurrentIndex number
---@field IsContainer boolean
---@field ButtonSelectedBorderColor table
---@field ButtonNotSelectedBorderColor table
---@field CanCloseWithRightClick boolean
---@field CallOnEachTab fun(self: df_tabcontainer, callback: function, ...)
---@field SetIndex fun(self: df_tabcontainer, index: number)
---@field SelectTabByIndex fun(self: df_tabcontainer, menuIndex: number)
---@field SelectTabByName fun(self: df_tabcontainer, name: string)
---@field CreateUnderlineGlow fun(button: button)
---@field OnShow fun(self: df_tabcontainer)
---@field GetTabFrameByName fun(self: df_tabcontainer, name: string): df_tabcontainerframe
---@field GetTabFrameByIndex fun(self: df_tabcontainer, index: number): df_tabcontainerframe
---@field GetTabButtonByName fun(self: df_tabcontainer, name: string): df_tabcontainerbutton
---@field GetTabButtonByIndex fun(self: df_tabcontainer, index: number): df_tabcontainerbutton
---@class df_tabcontainerframe : frame
---@field bIsFrontPage boolean
---@field titleText fontstring
---@field tabIndex number
---@field OnMouseDown fun(self: df_tabcontainerframe, button: string)
---@field OnMouseUp fun(self: df_tabcontainerframe, button: string)
---@field RefreshOptions fun(self: df_tabcontainerframe)|nil
---@class df_tabcontainerbutton : button
---@field selectedUnderlineGlow texture
---@field textsize number
---@field mainFrame df_tabcontainer
---@field leftSelectionIndicator texture
--create a template for the tab buttons
local tabTemplate = detailsFramework.table.copy({}, detailsFramework:GetTemplate("button", "OPTIONS_BUTTON_TEMPLATE"))
tabTemplate.backdropbordercolor = nil
detailsFramework.TabContainerMixin = {
CallOnEachTab = function(self, callback, ...)
for _, tabFrame in ipairs(self.AllFrames) do
detailsFramework:Dispatch(callback, tabFrame, ...)
end
end,
---@param self df_tabcontainer
---@param tabIndex number
---@return df_tabcontainerframe
GetTabFrameByIndex = function(self, tabIndex)
return self.AllFrames[tabIndex]
end,
---@param self df_tabcontainer
---@param name string
---@return df_tabcontainerframe
GetTabFrameByName = function(self, name)
return self.AllFramesByName[name]
end,
---@param self df_tabcontainer
---@param tabIndex number
---@return df_tabcontainerbutton
GetTabButtonByIndex = function(self, tabIndex)
return self.AllButtons[tabIndex]
end,
---@param self df_tabcontainer
---@param name string
---@return df_tabcontainerbutton
GetTabButtonByName = function(self, name)
return self.AllButtonsByName[name]
end,
---@param self df_tabcontainer
---@param backdropTable backdrop|nil
---@param backdropColorTable table|string|nil
---@param backdropBorderColorTable table|string|nil
SetTabFramesBackdrop = function(self, backdropTable, backdropColorTable, backdropBorderColorTable)
for tabIndex, tabFrame in ipairs(self.AllFrames) do
if (backdropTable) then
tabFrame:SetBackdrop(backdropTable)
end
if (backdropColorTable) then
local r, g, b, a = detailsFramework:ParseColors(backdropColorTable)
tabFrame:SetBackdropColor(r, g, b, a)
end
if (backdropBorderColorTable) then
local r, g, b, a = detailsFramework:ParseColors(backdropColorTable)
tabFrame:SetBackdropBorderColor(r, g, b, a)
end
end
end,
---create a underglow texture for the selected tab, this texture is a small yellow bright gradient below the button
---@param self df_tabcontainerbutton
CreateUnderlineGlow = function(self)
local selectedGlow = self:CreateTexture(nil, "background", nil, -4)
selectedGlow:SetPoint("topleft", self["widget"], "bottomleft", -7, 0)
selectedGlow:SetPoint("topright", self["widget"], "bottomright", 7, 0)
selectedGlow:SetTexture([[Interface\BUTTONS\UI-Panel-Button-Glow]])
selectedGlow:SetTexCoord(0, 95/128, 30/64, 38/64)
selectedGlow:SetBlendMode("ADD")
selectedGlow:SetHeight(8)
selectedGlow:SetAlpha(.75)
selectedGlow:Hide()
self.selectedUnderlineGlow = selectedGlow
end,
---@param tabContainer df_tabcontainer
---@param menuIndex number
SelectTabByIndex = function(tabContainer, menuIndex)
---@type df_tabcontainerbutton
local tabButton = tabContainer.AllButtons[menuIndex]
---@type df_tabcontainerframe
local tabFrame = tabContainer.AllFrames[menuIndex]
if (not tabFrame) then
return
end
--hide all tab frame and hide the selection glow from tab buttons
for i = 1, #tabContainer.AllFrames do
---@type df_tabcontainerframe
local thisTabFrame = tabContainer.AllFrames[i]
thisTabFrame:Hide()
---@type df_tabcontainerbutton
local thisTabButton = tabContainer.AllButtons[i]
if (tabContainer.ButtonNotSelectedBorderColor) then
thisTabButton:SetBackdropBorderColor(unpack(tabContainer.ButtonNotSelectedBorderColor))
end
if (thisTabButton.selectedUnderlineGlow) then
thisTabButton.selectedUnderlineGlow:Hide()
end
end
tabFrame:Show()
if (tabFrame.RefreshOptions) then
tabFrame:RefreshOptions()
end
if (tabContainer.ButtonSelectedBorderColor) then
tabButton:SetBackdropBorderColor(unpack(tabContainer.ButtonSelectedBorderColor))
end
if (tabButton.selectedUnderlineGlow) then
tabButton.selectedUnderlineGlow:Show()
end
tabContainer.CurrentIndex = menuIndex
if (tabContainer.hookList.OnSelectIndex) then
detailsFramework:QuickDispatch(tabContainer.hookList.OnSelectIndex, tabContainer, tabButton)
end
end,
---@param tabContainer df_tabcontainer
---@param name string
SelectTabByName = function(tabContainer, name)
---@type df_tabcontainerframe
local tabFrame = tabContainer.AllFramesByName[name]
if (tabFrame) then
local tabIndex = tabFrame.tabIndex
tabContainer:SelectTabByIndex(tabIndex)
else
error("df_tabcontainer:SelectTabByName(name): param #2 'name' not found within 'tabContainer.AllFramesByName'.")
end
end,
---@param self df_tabcontainer
---@param index number
SetIndex = function(self, index)
self.CurrentIndex = index
end,
---@param self df_tabcontainer
OnShow = function(self)
local index = self.CurrentIndex
self:SelectTabByIndex(index)
end
}
detailsFramework.TabContainerFrameMixin = {
---@param self df_tabcontainerframe
---@param button string
OnMouseDown = function(self, button)
if (self:GetParent().options.can_move_parent) then
--search for UIParent
---@type frame
local highestParent = detailsFramework:FindHighestParent(self)
local tabContainer = self:GetParent()
---@cast tabContainer df_tabcontainer
if (button == "LeftButton") then
if (not highestParent.IsMoving and highestParent:IsMovable()) then
highestParent:StartMoving()
highestParent.IsMoving = true
end
elseif (button == "RightButton") then
if (not highestParent.IsMoving and tabContainer.IsContainer) then
if (self.bIsFrontPage) then
if (tabContainer.CanCloseWithRightClick) then
if (highestParent["CloseFunction"]) then
highestParent["CloseFunction"](highestParent)
else
highestParent:Hide()
end
end
else
--goes back to front page
tabContainer:SelectTabByIndex(1)
end
end
end
end
end,
---@param self df_tabcontainerframe
---@param button string
OnMouseUp = function(self, button)
if (self:GetParent().options.can_move_parent) then
local frame = detailsFramework:FindHighestParent(self)
if (frame.IsMoving) then
frame:StopMovingOrSizing()
frame.IsMoving = false
end
end
end,
}
---@class df_tabcontaineroptions : table
---@field width number?
---@field height number?
---@field button_border_color table?
---@field button_selected_border_color table?
---@field right_click_y number?
---@field hide_click_label boolean?
---@field close_text_alpha number?
---@field rightbutton_always_close boolean?
---@field right_click_interact boolean?
---@field y_offset number?
---@field button_width number?
---@field button_height number?
---@field button_x number?
---@field button_y number?
---@field button_text_size number?
---@field container_width_offset number?
---@field can_move_parent boolean?
---creates a frame called tabContainer which is used as base for the tab container object
---the function receives a table called tabList which contains sub tables with two keys 'name' and 'text', name is the frame name and text is the text displayed on the button
---then the function iterate amongst the tabList and create a frame and a button for each entry using the value of the 'text' key as the text for the button and 'name' for the name of the frame
---when the user click on a button, the tabContainer hide all frames and show the frame which was created together with that button
---@param parent frame the parent frame
---@param title string a string to use as the title of the tab container, the title is always shown
---@param frameName string the frame name to pass into the CreateFrame function
---@param tabList df_tabinfotable[] the list of tabs to create, each entry has a 'name' and 'text' keys
---@param optionsTable df_tabcontaineroptions?
---@param hookList table<string, function>?
---@param languageInfo any
---@return df_tabcontainer
function detailsFramework:CreateTabContainer(parent, title, frameName, tabList, optionsTable, hookList, languageInfo)
optionsTable = optionsTable or {}
local parentFrameWidth = parent:GetWidth()
local yOffset = optionsTable.y_offset or 0
local buttonWidth = optionsTable.button_width or 160
local buttonHeight = optionsTable.button_height or 20
local buttonAnchorX = optionsTable.button_x or 230
local buttonAnchorY = optionsTable.button_y or 0
local buttonTextSize = optionsTable.button_text_size or 10
local containerWidthOffset = optionsTable.container_width_offset or 0
if (optionsTable.can_move_parent == nil) then
optionsTable.can_move_parent = true
end
local bFirstTabIsCreateOnDemand = false
--create the base frame
---@type df_tabcontainer
local tabContainer = CreateFrame("frame", frameName, parent["widget"] or parent, "BackdropTemplate")
tabContainer.hookList = hookList or {}
tabContainer:SetSize(optionsTable.width or 750, optionsTable.height or 450)
tabContainer.options = optionsTable
detailsFramework:Mixin(tabContainer, detailsFramework.TabContainerMixin)
--create the fontstring which show the title
---@type fontstring
local mainTitle = detailsFramework:CreateLabel(tabContainer, title, 24, "white")
mainTitle:SetPoint("topleft", tabContainer, "topleft", 10, -30 + yOffset)
tabContainer.AllFrames = {}
tabContainer.AllButtons = {}
tabContainer.AllFramesByName = {}
tabContainer.AllButtonsByName = {}
tabContainer.CurrentIndex = 1
tabContainer.IsContainer = true
tabContainer.ButtonSelectedBorderColor = optionsTable.button_selected_border_color or {1, 1, 0, 1}
tabContainer.ButtonNotSelectedBorderColor = optionsTable.button_border_color or {0, 0, 0, 0}
if (optionsTable.right_click_interact ~= nil) then
tabContainer.CanCloseWithRightClick = optionsTable.right_click_interact
else
tabContainer.CanCloseWithRightClick = true
end
--languageInfo
local addonId = languageInfo and languageInfo.language_addonId or "none"
for tabIndex, tabInfo in ipairs(tabList) do
--create a frame which will be shown when the tabButton is clicked
--when this tab isn't selected, this frame is hidden
---@type df_tabcontainerframe
local tabFrame = CreateFrame("frame", "$parent" .. tabInfo.name, tabContainer, "BackdropTemplate")
detailsFramework:Mixin(tabFrame, detailsFramework.TabContainerFrameMixin)
tabFrame:SetAllPoints()
tabFrame:SetFrameLevel(210)
tabFrame:SetScript("OnMouseDown", tabFrame.OnMouseDown)
tabFrame:SetScript("OnMouseUp", tabFrame.OnMouseUp)
tabFrame.tabIndex = tabIndex
tabFrame:Hide()
if (tabInfo.createOnDemandFunc) then
tabFrame:SetScript("OnShow", function()
if (tabInfo.createOnDemandFunc) then
detailsFramework:Dispatch(tabInfo.createOnDemandFunc, tabFrame, tabContainer, parent)
tabInfo.createOnDemandFunc = nil
end
end)
if (tabIndex == 1) then
bFirstTabIsCreateOnDemand = true
end
end
--attempt to get the localized text from the language system using the addonId and the frameInfo.text
local phraseId = tabInfo.text
local bIsLanguagePrahseID = detailsFramework.Language.DoesPhraseIDExistsInDefaultLanguage(addonId, phraseId)
--create the fontstring which show this tab text, this text is only shown when the tab is shown
local titleLabel = detailsFramework:CreateLabel(tabFrame, "", 16, "silver")
if (bIsLanguagePrahseID) then
DetailsFramework.Language.RegisterObjectWithDefault(addonId, titleLabel, tabInfo.text, tabInfo.text)
else
titleLabel:SetText(tabInfo.text)
end
titleLabel:SetPoint("topleft", mainTitle, "bottomleft", 0, 0)
tabFrame.titleText = titleLabel
---@type df_tabcontainerbutton
local tabButton = detailsFramework:CreateButton(tabContainer, function() tabContainer:SelectTabByIndex(tabIndex) end, buttonWidth, buttonHeight, tabInfo.text, tabIndex, nil, nil, nil, "$parentTabButton" .. tabInfo.name, false, tabTemplate)
PixelUtil.SetSize(tabButton, buttonWidth, buttonHeight)
tabButton:SetFrameLevel(220)
tabButton.textsize = buttonTextSize
tabButton.mainFrame = tabContainer
tabContainer.CreateUnderlineGlow(tabButton)
--register the fontstring with the language system
if (bIsLanguagePrahseID) then
DetailsFramework.Language.RegisterObjectWithDefault(addonId, tabButton["widget"], tabInfo.text, tabInfo.text)
end
local rightClickToBack
if (tabIndex == 1 or optionsTable.rightbutton_always_close) then
rightClickToBack = detailsFramework:CreateLabel(tabFrame, "right click to close", 10, "gray")
rightClickToBack:SetPoint("bottomright", tabFrame, "bottomright", -1, optionsTable.right_click_y or 0)
if (optionsTable.close_text_alpha) then
rightClickToBack:SetAlpha(optionsTable.close_text_alpha)
end
tabFrame.bIsFrontPage = true
else
rightClickToBack = detailsFramework:CreateLabel(tabFrame, "right click to go back to main menu", 10, "gray")
rightClickToBack:SetPoint("bottomright", tabFrame, "bottomright", -1, optionsTable.right_click_y or 0)
if (optionsTable.close_text_alpha) then
rightClickToBack:SetAlpha(optionsTable.close_text_alpha)
end
end
if (optionsTable.hide_click_label) then
rightClickToBack:Hide()
end
table.insert(tabContainer.AllFrames, tabFrame)
table.insert(tabContainer.AllButtons, tabButton)
tabContainer.AllFramesByName[tabInfo.name] = tabFrame
tabContainer.AllFramesByName[tabInfo.text] = tabFrame
tabContainer.AllButtonsByName[tabInfo.name] = tabButton
tabContainer.AllButtonsByName[tabInfo.text] = tabButton
end
--order buttons
local x = buttonAnchorX
local y = buttonAnchorY
local spaceBetweenButtons = 2
local allocatedSpaceForButtons = parentFrameWidth - ((#tabList - 2) * spaceBetweenButtons) - buttonAnchorX + containerWidthOffset
local amountButtonsPerRow = math.floor(allocatedSpaceForButtons / buttonWidth)
if (tabContainer.AllButtons[1]) then
tabContainer.AllButtons[1]:SetPoint("topleft", mainTitle, "topleft", x, y)
end
x = x + buttonWidth + 2
for i = 2, #tabContainer.AllButtons do
local button = tabContainer.AllButtons[i]
PixelUtil.SetPoint(button, "topleft", mainTitle, "topleft", x, y)
x = x + buttonWidth + 2
if (i % amountButtonsPerRow == 0) then
x = buttonAnchorX
y = y - buttonHeight - 1
end
end
--when show the frame, reset to the current internal index
tabContainer:SetScript("OnShow", tabContainer.OnShow)
--select the first frame
local defaultTab = 1
if (bFirstTabIsCreateOnDemand) then
C_Timer.After(0, function() tabContainer:SelectTabByIndex(defaultTab) end)
else
tabContainer:SelectTabByIndex(defaultTab)
end
return tabContainer
end
--[=[example:
local parent = UIParent
local title = "My AddOn Options"
local frameName = "MyAddOnOptionsFrame"
local tabList = {
{name = "GeneralSettings", text = "General Settings"},
{name = "AdvancedSettings", text = "Advanced Settings"},
{name = "AboutTheAddon", text = "Addon Info"},
}
local optionsTable = {}
local hookList = {}
local languageInfo = {language_addonId = "MyAddOnTocName"}
local tabContainer = DetailsFramework:CreateTabContainer(parent, title, frameName, tabList, optionsTable, hookList, languageInfo)
tabContainer:SetPoint("center", UIParent, "center", 0, 0)
tabContainer:SetSize(750, 450)
tabContainer:Show()
--ways for getting a tab frame and start to create widgets inside it
local tabIndex = 1
local generalSettingsTabFrame = tabContainer:GetTabFrameByIndex(tabIndex) --using a tabIndex
local advancedSettingsTabFrame = tabContainer:GetTabFrameByName("Advanced Settings") --using the tab text
local aboutTabFrame = tabContainer:GetTabFrameByName("AboutTheAddon") --using the tab name
--clicking on tab buttons will automatically show the tab frame, to select a tab frame without clicking on the button, use:
tabContainer:SelectTabByIndex(tabIndex) --using a tabIndex
tabContainer:SelectTabByName("Advanced Settings") --using the tab text
tabContainer:SelectTabByName("AdvancedSettings") --using the tab name
--modify the background color by applying a backdrop
local backdropTable = {edgeFile = [[Interface\Buttons\WHITE8X8]], edgeSize = 1, bgFile = [[Interface\Tooltips\UI-Tooltip-Background]], tileSize = 64, tile = true}
local backdropColor = {DetailsFramework:GetDefaultBackdropColor()}
local backdropBorderColor = {0, 0, 0, 1}
tabContainer:SetTabFramesBackdrop(backdropTable, backdropColor, backdropBorderColor)
--]=]