ComplexLabel = class(Turbine.UI.Control);
-- A control that contains sub-labels, each of which can have its own font, color, and mouse behaviors.
-- If the content width exceeds the label width, an ellipsis ("...") is shown at the end.
-- Currently Multiline is not supported.
function ComplexLabel:Constructor()
Turbine.UI.Control.Constructor(self);
self.font = Turbine.UI.Lotro.Font.Verdana14;
self.foreColor = Turbine.UI.Color.White;
self.fontStyle = Turbine.UI.FontStyle.Outline;
self.outlineColor = Turbine.UI.Color.Black;
self.alignment = Turbine.UI.ContentAlignment.MiddleCenter;
self.interiorAlignment = Turbine.UI.ContentAlignment.MiddleLeft;
self.content = Turbine.UI.Control();
--self.content:SetBackColor(Turbine.UI.Color.Silver);
self.content:SetParent(self);
self.content:SetSize(0, 0);
self.content.width = 0;
self.ellipsis = Turbine.UI.Label();
self.ellipsis:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
self.ellipsis:SetMultiline(false);
self.ellipsis:SetMouseVisible(false);
end
function ComplexLabel:SetFont(font)
self.font = font;
self:_RealignContent();
end
function ComplexLabel:SetForeColor(foreColor)
self.foreColor = foreColor;
self:_RealignContent();
end
function ComplexLabel:SetFontStyle(fontStyle)
self.fontStyle = fontStyle;
self:_RealignContent();
end
function ComplexLabel:SetOutlineColor(outlineColor)
self.outlineColor = outlineColor;
self:_RealignContent();
end
function ComplexLabel:AddText(text)
-- Create new label control
local label = Turbine.UI.Label();
label:SetTextAlignment(self.interiorAlignment);
label:SetMarkupEnabled(true);
label:SetMultiline(false);
label:SetFont(self.font);
--label:SetBackColor(Turbine.UI.Color.Yellow);
label:SetForeColor(self.foreColor);
label:SetFontStyle(self.fontStyle);
label:SetOutlineColor(self.outlineColor);
label:SetText(text);
label:AutoSize();
-- Add new label to content control
label:SetParent(self.content);
label:SetLeft(self.content.width);
self:_ResizeContent();
return label;
end
function ComplexLabel:SetText(text)
self.content:GetControls():Clear();
self.content.width = 0;
return self:AddText(text);
end
function ComplexLabel:GetText()
local text = "";
local labels = self.content:GetControls();
for n = 1, labels:GetCount() do
local label = labels:Get(n);
text = text .. label:GetText();
end
return text;
end
function ComplexLabel:SetTextAlignment(alignment)
self.alignment = alignment;
self:_RealignContent();
end
function ComplexLabel:SetInteriorAlignment(alignment)
self.interiorAlignment = alignment;
self:_RealignContent();
end
function ComplexLabel:SetWidth(width)
Turbine.UI.Control.SetWidth(self, width);
self:_RealignContent();
end
function ComplexLabel:SetHeight(height)
Turbine.UI.Control.SetHeight(self, height);
self:_RealignContent();
end
function ComplexLabel:SetSize(width, height)
Turbine.UI.Control.SetSize(self, width, height);
self:_RealignContent();
end
function ComplexLabel:_ResizeContent()
-- Resize the content control to display all of the contained labels
local contentWidth, contentHeight = 0, 0;
local labels = self.content:GetControls();
for n = 1, labels:GetCount() do
local label = labels:Get(n);
local left, top = label:GetPosition();
local width, height = label:GetSize();
local right = left + width;
local bottom = top + height;
-- Rightmost label determines the content width
contentWidth = math.max(contentWidth, right);
-- Bottom-most label determines the content height
contentHeight = math.max(contentHeight, bottom);
end
self.content:SetSize(contentWidth, contentHeight);
self.content.width = contentWidth;
-- Set the height of all labels
for n = 1, labels:GetCount() do
local label = labels:Get(n);
label:SetHeight(contentHeight);
end
self:_RealignContent();
end
function ComplexLabel:_FormatEllipsis()
self.ellipsis:SetFont(self.font);
self.ellipsis:SetForeColor(self.foreColor);
self.ellipsis:SetFontStyle(self.fontStyle);
self.ellipsis:SetOutlineColor(self.outlineColor);
self.ellipsis:SetText("...");
self.ellipsis:AutoSize();
self.ellipsis:SetHeight(self.content:GetHeight());
end
function ComplexLabel:_RealignContent()
-- Do placement of the content control as per SetTextAlignment()
width, height = self:GetSize();
contentWidth, contentHeight = self.content.width, self.content:GetHeight();
local left, top;
if (
(contentWidth >= width) or
(self.alignment == Turbine.UI.ContentAlignment.TopLeft) or
(self.alignment == Turbine.UI.ContentAlignment.MiddleLeft) or
(self.alignment == Turbine.UI.ContentAlignment.BottomLeft)
) then
left = 0;
elseif (
(self.alignment == Turbine.UI.ContentAlignment.TopCenter) or
(self.alignment == Turbine.UI.ContentAlignment.MiddleCenter) or
(self.alignment == Turbine.UI.ContentAlignment.BottomCenter)
) then
left = math.floor(0.5 + (width - contentWidth) / 2);
else -- Turbine.UI.ContentAlignment.[...]Right
left = width - contentWidth;
end
if (
(contentHeight >= height) or
(self.alignment == Turbine.UI.ContentAlignment.TopLeft) or
(self.alignment == Turbine.UI.ContentAlignment.TopCenter) or
(self.alignment == Turbine.UI.ContentAlignment.TopRight)
) then
top = 0;
elseif (
(self.alignment == Turbine.UI.ContentAlignment.MiddleLeft) or
(self.alignment == Turbine.UI.ContentAlignment.MiddleCenter) or
(self.alignment == Turbine.UI.ContentAlignment.MiddleRight)
) then
top = math.floor(0.5 + (height - contentHeight) / 2);
else -- Turbine.UI.ContentAlignment.Bottom[...]
top = height - contentHeight;
end
self.content:SetPosition(left, top);
-- to do: display "..."
if (contentWidth > width) then
self:_FormatEllipsis();
self.ellipsis:SetParent(self);
left = width - self.ellipsis:GetWidth();
self.content:SetWidth(left);
self.ellipsis:SetPosition(left, top);
else
self.content:SetWidth(self.content.width);
self.ellipsis:SetParent(nil);
end
end
Thurallor = Thurallor or {};
Thurallor.UI = Thurallor.UI or {};
Thurallor.UI.ComplexLabel = ComplexLabel;