TabCardStack = class(Turbine.UI.Control);
function TabCardStack:Constructor()
Turbine.UI.Control.Constructor(self);
self:SetMouseVisible(false);
self.tabCards = {};
end
function TabCardStack:AddTabCard(tabCard)
table.insert(self.tabCards, tabCard);
tabCard:SetParent(self);
self:BringToFront(tabCard);
self:_UpdateAddTabButton();
-- Default behavior when the tab is clicked is to bring the card to the front.
AddCallback(tabCard, "Click", function(ctl, args)
if (args.Button == Turbine.UI.MouseButton.Left) then
self:BringToFront(ctl);
end
end);
end
function TabCardStack:SetAddTabButtonEnabled(enable, tooltip)
local button = self.addTabButton;
if (enable) then
if (not button) then
button = Turbine.UI.Label();
button:SetFont(Turbine.UI.Lotro.Font.Verdana16);
button:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
button:SetText("+");
button:SetSize(12, 18);
button:SetZOrder(2); -- in front of all tabs
button.MouseEnter = function(ctl)
ctl.mouseInside = true;
self:_UpdateAddTabButton();
end
button.MouseLeave = function(ctl)
ctl.mouseInside = false;
self:_UpdateAddTabButton();
end
button.MouseDown = function(ctl, args)
if (args.Button == Turbine.UI.MouseButton.Left) then
ctl.mouseDown = true;
self:_UpdateAddTabButton();
end
end
button.MouseUp = function(ctl, args)
if (args.Button == Turbine.UI.MouseButton.Left) then
ctl.mouseDown = false;
self:_UpdateAddTabButton();
end
end
button.MouseClick = function(ctl, args)
DoCallbacks(self, "AddTabButtonClicked", args);
end
if (tooltip) then
Thurallor.UI.Tooltip(tooltip):Attach(button);
end
self.addTabButton = button;
end
button:SetParent(self);
self:_UpdateAddTabButton();
else
if (button) then
button:SetParent(nil);
end
end
end
function TabCardStack:_UpdateAddTabButton()
local button = self.addTabButton;
if (button) then
local left = 0;
if (#self.tabCards > 0) then
local lastTab = self.tabCards[#self.tabCards];
left = lastTab:GetTabLeft() + lastTab:GetTabWidth();
end
button:SetPosition(left, 2);
if (button.mouseDown) then
button:SetForeColor(Turbine.UI.Color.White);
elseif (button.mouseInside) then
button:SetForeColor(Turbine.UI.Color(0.75, 1, 1, 1));
else
button:SetForeColor(Turbine.UI.Color(0.5, 1, 1, 1));
end
end
end
function TabCardStack:BringToFront(tabCard)
for t = 1, #self.tabCards do
local otherTabCard = self.tabCards[t];
if (otherTabCard == tabCard) then
tabCard:BringToFront(true);
tabCard:SetSize(self:GetSize());
self.frontTabCard = tabCard;
else
otherTabCard:SendToBack(true);
end
end
end
function TabCardStack:RedistributeTabs()
local width = self:GetWidth();
if (self.addTabButton) then
width = width - self.addTabButton:GetWidth();
end
-- Sort the tabCards by left edge
local function compare(a, b)
return (a:GetTabLeft() < b:GetTabLeft());
end
table.sort(self.tabCards, compare);
-- Spread out the tabCards to eliminate overlap
local left = 0;
for t = 1, #self.tabCards do
local tabCard = self.tabCards[t];
tabCard:SetTabLeft(left);
left = left + tabCard:GetTabWidth();
end
if (left <= width) then
-- All the tabs fit without overlapping. Done.
self:_UpdateAddTabButton();
return;
end
-- Some overlap is unavoidable. Distribute the left edges evenly.
local rightTab = self.tabCards[#self.tabCards];
local rightTabWidth = rightTab:GetTabWidth();
local newRightTabLeft = math.max(#self.tabCards, (width - rightTabWidth)); -- need at least as many pixels as tabs
local squeezeRatio = newRightTabLeft / rightTab:GetTabLeft();
local frontTab;
for t = 1, #self.tabCards do
local tabCard = self.tabCards[t];
local left = math.floor(0.5 + tabCard:GetTabLeft() * squeezeRatio);
tabCard:SetTabLeft(left);
if (tabCard:IsInFront()) then
frontTab = tabCard;
end
end
-- Bring front tabCard to the front again so its tab isn't concealed if there's overlap
frontTab:SetZOrder(1);
frontTab:SetZOrder(0);
self:_UpdateAddTabButton();
end
function TabCardStack:SetSize(width, height)
Turbine.UI.Control.SetSize(self, width, height);
for t = 1, #self.tabCards do
local tabCard = self.tabCards[t];
tabCard:SetSize(width, height);
end
self:_UpdateAddTabButton();
end
function TabCardStack:SetWidth(width)
self:SetSize(width, self:GetHeight());
end
function TabCardStack:SetHeight(height)
self:SetSize(self:GetWidth(), height);
end