RotationControl = class(Turbine.UI.Control);
function RotationControl:Constructor(settings)
Turbine.UI.Control.Constructor(self);
self.settings = settings or "1/3:3";
self.schedule = Schedule(self.settings);
self.endOfToday = Time():GetNextServerResetTime():GetGameTime();
self:SetWantsUpdates(true); -- monitor for daily reset time
end
function RotationControl:Update()
local currentTime = Turbine.Engine.GetGameTime();
if (currentTime > self.endOfToday) then
-- Daily reset time passed. Need to update the selected day.
self.endOfToday = self.endOfToday + Time.lengthOfDay();
self:Redraw();
end
end
function RotationControl:Redraw()
self:GetControls():Clear();
local font = Turbine.UI.Lotro.Font.Verdana14;
local prevContext = L:SetContext("/RotationControl");
local function AddNudger(left, value, minValue, maxValue)
local nudger = Nudger(self, font, value, minValue, maxValue);
AddCallback(nudger, "FocusGained", function()
DoCallbacks(self, "FocusGained");
end);
AddCallback(nudger, "ValueChanged", function()
DoCallbacks(self, "DelayChanged");
end);
nudger:SetPosition(left, 0);
nudger:SetWidth(30);
return nudger, left + 30;
end
local function AddLabel(left, text)
local label = Turbine.UI.Label();
label:SetFont(font);
label:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
label:SetText(text);
label:SetMultiline(false);
label:AutoSize();
label:SetSize(label:GetWidth() + 8, 20)
label:SetParent(self);
label:SetPosition(left, 13);
return label, left + label:GetWidth();
end
local left = 0;
local label;
label, left = AddLabel(left, L:GetText("OnceInEvery"));
self.nudger, left = AddNudger(left, self.settings.days, 2, 10);
AddCallback(self.nudger, "ValueChanged", function()
self.settings.days = tonumber(self.nudger:GetText());
self:Redraw();
end);
label, left = AddLabel(left, L:GetText("DaysStarting"));
self.dayOfWeekCtl = DayOfWeekControl(self.settings.days, self.selectedDay);
self.dayOfWeekCtl:SetParent(self);
self.dayOfWeekCtl:SetPosition(left, 14);
left = left + self.dayOfWeekCtl:GetWidth();
AddCallback(self.dayOfWeekCtl, "SelectionChanged", function(_, index)
self.selectedDay = index;
self.settings.availableTime = self:_ConvertSelectionToTime(index);
end);
self:SetSize(left, self.nudger:GetHeight());
L:SetContext(prevContext);
end
function RotationControl:GetSettings()
return self.settings;
end
-- "Normalize" the time so it appears within the period of the current rotation, starting today
function RotationControl:_NormalizeTime(availableTime)
Puts("RotationControl:_NormalizeTime(" .. availableTime ..") = ");
local lengthOfDay = 24 * 60 * 60;
local lengthOfRotation = self.settings.days * lengthOfDay;
local rotationStart = self.startOfToday;
local rotationEnd = rotationStart + lengthOfRotation;
if (availableTime > rotationEnd) then
local rotationsNeeded = math.ceil((availableTime - rotationEnd) / lengthOfRotation);
availableTime = availableTime - rotationsNeeded * lengthOfRotation;
elseif (availableTime < rotationStart) then
local rotationsNeeded = math.ceil((rotationStart - availableTime) / lengthOfRotation);
availableTime = availableTime + rotationsNeeded * lengthOfRotation;
end
Puts(" " .. availableTime);
return availableTime;
end
function RotationControl:_ConvertTimeToSelection(availableTime)
local lengthOfDay = 24 * 60 * 60;
return math.ceil((availableTime - self.startOfToday) / lengthOfDay);
end
function RotationControl:_ConvertSelectionToTime(index)
local lengthOfDay = 24 * 60 * 60;
return self.startOfToday + (index - 1) * lengthOfDay;
end
function RotationControl:SetAvailableTime(availableTime)
availableTime = self:_NormalizeTime(availableTime);
self.selectedDay = self:_ConvertTimeToSelection(availableTime);
self.settings.availableTime = self:_ConvertSelectionToTime(self.selectedDay);
self:Redraw();
end
function RotationControl:SetDays(numDays)
self.settings.days = numDays;
self:SetAvailableTime(self.settings.availableTime);
end