-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTabSupport.lua
244 lines (214 loc) · 9.05 KB
/
TabSupport.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
-- ===========================================================================
-- Takes a bunch of (Grid)Button controls in a container and gives them
-- tab-like functionality.
--
-- ===========================================================================
-- ===========================================================================
-- tabContainerControl The container (parent) control for the tabs
-- sizeX (optional) Size of texture X length
-- sizeY (optional) Size of texture Y height
-- selectedFontColor (optional) AGBR Font color of the
--
-- RETURNS: tab object for working with controls in a tabby manner.
-- ===========================================================================
function CreateTabs( tabContainerControl, sizeX, sizeY, selectedFontColor)
-- Set default options if not passed in.
if sizeX == nil then sizeX = 64; end;
if sizeY == nil then sizeY = 32; end;
local defaultFontColor:number = 0xffffffff;
local tabs = {};
tabs.containerControl = tabContainerControl;
tabs.tabControls = {};
tabs.selectedControl = nil;
tabs.prevSelectedControl= nil;
tabs.defaultFontColor = defaultFontColor;
tabs.selectedFontColor = selectedFontColor;
tabs.decoAnim = nil;
tabs.decoOffset = 0;
-- Set size, used in operation to get the selected state from sprite sheet.
tabs.textureSizeX = sizeX;
tabs.textureSizeY = sizeY;
-- ===========================================================================
-- (PRIVATE) Sets the visual style of the selected tab.
-- ===========================================================================
tabs.SetSelectedTabVisually =
function ( tabControl )
if ( tabs.decoAnim ~= nil ) then
tabs.decoAnim:SetEndVal(tabControl:GetOffsetX() + (tabControl:GetSizeX()/2) - tabs.decoOffset,0);
tabs.decoAnim:Play();
end
tabs.prevSelectedControl = tabs.selectedControl; -- Record last tab selected
if ( tabs.selectedControl ~= nil ) then
local oldSelectedTabID = tabs.selectedControl:GetID() .. "Selected";
if(Controls[oldSelectedTabID] ~= nil) then
Controls[oldSelectedTabID]:SetHide(true);
Controls[oldSelectedTabID]:SetToBeginning();
end
if (tabs.selectedControl:GetTextControl() ~= nil and tabs.defaultFontColor ~= nil) then
tabs.selectedControl:GetTextControl():SetColor(0xFFefe7e1);
end
tabs.selectedControl:SetTextureOffsetVal( 0, 0 );
tabs.selectedControl = nil;
end
if ( tabControl ~= nil ) then
local newSelectedTabID = tabControl:GetID() .. "Selected";
if(Controls[newSelectedTabID] ~= nil) then
Controls[newSelectedTabID]:SetHide(false);
Controls[newSelectedTabID]:Play();
end
if (tabControl:GetTextControl() ~= nil and tabs.selectedFontColor ~= nil) then
tabControl:GetTextControl():SetColor(tabs.selectedFontColor);
end
-- Change parent back to container, this will re-add it to the end of the
-- child list, thereby drawing last (on top of other tabs if they overlap).
tabControl:ChangeParent( tabs.containerControl );
tabs.selectedControl = tabControl;
end
end
-- ===========================================================================
-- ===========================================================================
tabs.SelectTab =
function ( tabControlOrIndex )
-- If NIL select no tabs.
local callback :ifunction = nil;
if tabControlOrIndex == nil then
tabs.SetSelectedTabVisually( nil );
elseif type(tabControlOrIndex) == "number" then
callback = tabs.tabControls[tabControlOrIndex]["CallbackFunc"]; -- Actually an index
else
callback = tabControlOrIndex["CallbackFunc"];
end
if callback ~= nil then
callback();
end
end
-- ===========================================================================
-- Add a sliding decor piece
-- decoAnim A SlideAnim containing the decor image which will slide the decor over the selected tab
-- decoControl An Image control which will be centered over the selected tab
-- ===========================================================================
tabs.AddAnimDeco =
function ( decoAnim, decoControl )
-- Reset to a new beginning
function setNewBegin()
if(tabs.selectedControl ~= nil) then
tabs.decoAnim:SetBeginVal(tabs.selectedControl:GetOffsetX() + (tabs.selectedControl:GetSizeX()/2) - tabs.decoOffset,0);
tabs.decoAnim:SetToBeginning();
end
end
if(decoAnim ~= nil and decoControl ~= nil) then
if(tabs.selectedControl ~= nil) then
decoAnim:SetEndVal(tabs.selectedControl:GetOffsetX() + (tabs.selectedControl:GetSizeX()/2) - (decoControl:GetSizeX()/2),0);
decoAnim:SetBeginVal(tabs.selectedControl:GetOffsetX() + (tabs.selectedControl:GetSizeX()/2) - (decoControl:GetSizeX()/2),0);
decoAnim:SetToBeginning();
decoAnim:Stop();
decoAnim:SetHide(false);
tabs.decoAnim = decoAnim;
tabs.decoOffset = decoControl:GetSizeX()/2;
else
tabs.decoAnim = decoAnim;
tabs.decoOffset = decoControl:GetSizeX()/2;
decoAnim:SetHide(true);
end
decoAnim:RegisterEndCallback( setNewBegin );
end
end
-- ===========================================================================
-- Add a tab control to be managed by tab manager.
-- tabContainerControl The group of tabs to add this tab to
-- tabControl The tab UI element to be managed.
-- focusCallback A callback to call when the tab is focused
-- ===========================================================================
tabs.AddTab =
function ( tabControl, focusCallBack )
if ( tabControl:GetTextControl() ~= nil ) then
--tabs.defaultFontColor = tabControl:GetTextControl():GetColor();
tabs.defaultFontColor = 0xffffff;
end
-- Protect the flock
if ( focusCallBack == nil ) then
print("ERROR: NIL focusCallback for tabControl");
end
tabControl["CallbackFunc"] = function()
tabs.SetSelectedTabVisually( tabControl ); -- Our function to change UI appearance
focusCallBack( tabControl ); -- User's function to do stuff when tab is clicked
end;
-- Register callback when tab is click/pressed.
tabControl:RegisterCallback( Mouse.eLClick, tabControl["CallbackFunc"] );
table.insert( tabs.tabControls, tabControl ); -- Add to list of controls in tabs.
end
-- ===========================================================================
-- Coverts all the tabs to be the same size as the largest.
-- Call this before a layout function.
-- ===========================================================================
tabs.SameSizedTabs =
function( padding:number )
if padding == nil then padding = 0; end
local largestSize = 0;
local tabNum = table.count(tabs.tabControls);
for _,control in ipairs(tabs.tabControls) do
if control:GetSizeX() > largestSize then
largestSize = control:GetSizeX();
end
end
largestSize = largestSize + padding;
for _,control in ipairs(tabs.tabControls) do
control:SetSizeX( largestSize );
end
end
-- ===========================================================================
-- Spreads out the tabs evenly, from end-to-end using maths!
-- ===========================================================================
tabs.EvenlySpreadTabs =
function( )
local width = tabs.containerControl:GetSizeX();
local tabNum = table.count(tabs.tabControls);
if ( tabNum < 1 ) then
UI.DataError("Attempting to evenly spread tabs but no tabs to spread in: ", tabs.containerControl );
return;
end
-- Adjust with to starting X offsets with the last offset being the width of the last tab.
if(tabNum > 1) then
local totalSize:number = 0;
for i,control in ipairs(tabs.tabControls) do
totalSize = totalSize + control:GetSizeX();
end
local step:number = (width - totalSize) / (tabNum - 1);
local nextX:number = 0;
for i,control in ipairs(tabs.tabControls) do
control:SetOffsetX(nextX);
nextX = nextX + control:GetSizeX() + step;
end
else
local control:table = tabs.tabControls[1];
control:SetOffsetX((width / 2) - (control:GetSizeX() / 2));
end
end
-- ===========================================================================
-- Spreads out the tabs within the center of the given area
-- ===========================================================================
tabs.CenterAlignTabs =
function( tabOverlapSpace )
local DEFAULT_TAB_OVERLAP_SPACE = 20;
local width = tabs.containerControl:GetSizeX();
local tabNum = table.count(tabs.tabControls);
if tabOverlapSpace == nil then
tabOverlapSpace = DEFAULT_TAB_OVERLAP_SPACE;
end
if ( tabNum < 1 ) then
UI.DataError("Attempting to center align tabs but no tabs to center in: ", tabs.containerControl );
return;
end
-- Determine total width of tabs
local totalTabsWidth = tabOverlapSpace;
for _,control in ipairs(tabs.tabControls) do
totalTabsWidth = totalTabsWidth + control:GetSizeX() - tabOverlapSpace;
end
local nextX = (width/2) - (totalTabsWidth/2 );
for i,control in ipairs(tabs.tabControls) do
control:SetOffsetX( nextX );
nextX = nextX + control:GetSizeX() - tabOverlapSpace;
end
end
return tabs;
end