TrickButton = class(Turbine.UI.Control);
-- A button that executes a chat command when the user clicks it, and observes the result.
-- Constructor arguments:
-- command -- the command to be executed
-- channel -- the chat channel on which the expected result will be received
-- regexp -- a regular expression to identify the expected result
-- When the button is clicked, the captured result will be passed via a "Click" event.
function TrickButton:Constructor(command, channel, regexp)
Turbine.UI.Control.Constructor(self);
local lotroButton = Turbine.UI.Lotro.Button()
lotroButton.defaultColor = lotroButton:GetForeColor();
lotroButton:SetEnabled(false);
lotroButton.disabledColor = lotroButton:GetForeColor();
lotroButton:SetEnabled(true);
lotroButton:SetParent(self);
local quickslots = Turbine.UI.Control();
quickslots:SetParent(self);
self.command = command;
self.channel = channel;
self.regexp = regexp;
self.lotroButton = lotroButton;
self.quickslots = quickslots;
self.hoverTime = 0.5; -- seconds; pretty arbitrary
self.chatReceived = function(_, args)
if (self.regexp and (args.ChatType == self.channel)) then
local matches = {string.match(args.Message, self.regexp)};
if (matches[1]) then
DoCallbacks(self, "Click", unpack(matches));
end
end
end
self:SetSize(50, 20); -- same default size as a Lotro.Button
end
function TrickButton:SetCommand(command)
self.command = command;
self:_RebuildQuickslots();
end
function TrickButton:SetChannel(channel)
self.channel = channel;
end
function TrickButton:SetRegexp(regexp)
self.regexp = regexp;
end
function TrickButton:SetHoverTime(hoverTime)
self.hoverTime = hoverTime;
end
function TrickButton:SetBackground(background)
Turbine.UI.Control.SetBackground(self, background);
self.lotroButton:SetParent(nil);
end
function TrickButton:SetSize(width, height)
Turbine.UI.Control.SetSize(self, width, height);
self.lotroButton:SetSize(width, height);
self.quickslots:SetSize(width, height);
self:_RebuildQuickslots();
end
function TrickButton:_RebuildQuickslots()
local width, height = self:GetSize();
-- Create an array of quickslots, each only 2 pixels high so they'll be invisible
self.quickslots:GetControls():Clear();
local shortcut = Turbine.UI.Lotro.Shortcut(Turbine.UI.Lotro.ShortcutType.Alias, self.command);
for y = 1, height, 2 do
local quickslot = Turbine.UI.Lotro.Quickslot();
quickslot:SetShortcut(shortcut);
quickslot:SetParent(self.quickslots);
quickslot:SetTop(y);
quickslot:SetSize(width, 2);
quickslot.MouseDown = function(_, args) DoCallbacks(self, "MouseDown", args) end;
quickslot.MouseUp = function(_, args) DoCallbacks(self, "MouseUp", args) end;
-- Quickslots don't seem to generate these events:
--quickslot.MouseMove = function(_, args) DoCallbacks(self, "MouseMove", args) end;
--quickslot.MouseHover = function(_, args) DoCallbacks(self, "MouseHover", args) end;
end
end
function TrickButton:SetEnabled(enabled)
self.lotroButton:SetEnabled(enabled);
if (enabled) then
-- Recreate the quickslot controls
self:SetWidth(self:GetWidth());
self.lotroButton:SetForeColor(self.lotroButton.defaultColor);
else
-- Destroy the quickslot controls
self.quickslots:GetControls():Clear();
self.lotroButton:SetForeColor(self.lotroButton.disabledColor);
end
end
function TrickButton:SetWidth(width)
self:SetSize(width, self:GetHeight());
end
function TrickButton:SetHeight(height)
self:SetSize(self:GetWidth(), height);
end
function TrickButton:MouseDown(args)
if (args.Button == Turbine.UI.MouseButton.Left) then
self.lotroButton:SetForeColor(Turbine.UI.Color.Gray);
end
end
function TrickButton:MouseUp(args)
if (args.Button == Turbine.UI.MouseButton.Left) then
self.lotroButton:SetForeColor(Turbine.UI.Color.White);
end
end
function TrickButton:MouseEnter()
if (self.lotroButton:IsEnabled()) then
self.lotroButton:SetForeColor(Turbine.UI.Color.White);
end
RemoveCallback(Turbine.Chat, "Received", self.chatReceived);
AddCallback(Turbine.Chat, "Received", self.chatReceived);
-- For tooltip. Quickslots don't generate this event, so we do our best
-- to emulate it, albeit without the expected hover delay.
self.mouseEnterTime = Turbine.Engine.GetGameTime();
self:SetWantsUpdates(true);
end
function TrickButton:Update()
local gameTime = Turbine.Engine.GetGameTime();
if (gameTime - self.mouseEnterTime >= self.hoverTime) then
DoCallbacks(self, "MouseHover");
self:SetWantsUpdates(false);
end
end
function TrickButton:MouseLeave()
if (self.lotroButton:IsEnabled()) then
self.lotroButton:SetForeColor(self.lotroButton.defaultColor);
end
RemoveCallback(Turbine.Chat, "Received", self.chatReceived);
-- Abort the "hover timer".
self:SetWantsUpdates(false);
end
-- Forward the usual Button configuration functions to the inert Lotro button
function TrickButton:SetFont(font) self.lotroButton:SetFont(font); end
function TrickButton:SetText(text) self.lotroButton:SetText(text); end
Thurallor = Thurallor or {};
Thurallor.UI = Thurallor.UI or {};
Thurallor.UI.TrickButton = TrickButton;