lotrointerface.com
Search Downloads

LoTROInterface SVN Reminders

[/] [trunk/] [Thurallor/] [Reminders/] [ResetTimesControl.lua] - Rev 8

Compare with Previous | Blame | View Log

 ResetTimesControl = class(Turbine.UI.Control);

function ResetTimesControl:Constructor(timeSpec, allowNever)
    Turbine.UI.Control.Constructor(self);
    self.allowNever = allowNever;
    if ((not timeSpec) and (not allowNever)) then
        timeSpec = "*:3";
    end

    -- Generate ordering/encoding/translating tables for days of the week
    self.weekdays = { order = {}; encode = {}; decode = {}; toLocal = {}; fromLocal = {} };
    for _, data in ipairs({
        { "EveryDay",  "*", L:GetText("/ResetTimesControl/EveryDay") };
        { "Monday",    "2", L:GetText("/Time/DayOfWeek/Monday")     };
        { "Tuesday",   "3", L:GetText("/Time/DayOfWeek/Tuesday")    };
        { "Wednesday", "4", L:GetText("/Time/DayOfWeek/Wednesday")  };
        { "Thursday",  "5", L:GetText("/Time/DayOfWeek/Thursday")   };
        { "Friday",    "6", L:GetText("/Time/DayOfWeek/Friday")     };
        { "Saturday",  "7", L:GetText("/Time/DayOfWeek/Saturday")   };
        { "Sunday",    "1", L:GetText("/Time/DayOfWeek/Sunday")     };
    }) do
        local dayStr, encodingStr, localStr = unpack(data);
        table.insert(self.weekdays.order, localStr);
        self.weekdays.encode[dayStr] = encodingStr;
        self.weekdays.decode[encodingStr] = dayStr;
        self.weekdays.toLocal[dayStr] = localStr;
        self.weekdays.fromLocal[localStr] = dayStr;
    end

    self:SetTimeSpec(timeSpec);
end

function ResetTimesControl:SetTimeSpec(timeSpec)
    self.resetTimes = {};
    if (timeSpec) then
        local minute;
        for day, hour in string.gmatch(timeSpec, "([1-7*]):([0-9.,]+)") do
            hour, minute = math.modf(hour);
            minute = minute * 60;
            local dayLocalStr = self.weekdays.toLocal[self.weekdays.decode[day]];
            local timeLocalStr = Time.GetTimeOfDayStr(hour, minute);
            table.insert(self.resetTimes, { dayLocalStr, timeLocalStr });
        end
    end
    self.valid = true;
    self:DoLayout();
end

function ResetTimesControl:IsValid()
    return self.valid;
end

function ResetTimesControl:GetTimeSpec()
    local schedule = Schedule();
    for _, resetTime in ipairs(self.resetTimes) do
        local dayLocalStr, timeLocalStr = unpack(resetTime);
        local day = self.weekdays.encode[self.weekdays.fromLocal[dayLocalStr]];
        local hour, minute = Time.GetNumericTimeOfDay(timeLocalStr);
        schedule:AddStr(day  .. ":" .. (hour + (minute / 60)));
    end
    return schedule:GetStr(); -- will return nil in lieu of ""
end

function ResetTimesControl:DoLayout()

    function AddXButton(parent)
        local button = Turbine.UI.Control();
        button:SetParent(parent);
        button:SetSize(16, 16);
        button:SetBackground(0x41000196);
        button:SetBlendMode(Turbine.UI.BlendMode.Overlay);
        button.MouseEnter = function(ctl) ctl:SetBackground(0x41000198) end;
        button.MouseLeave = function(ctl) ctl:SetBackground(0x41000196) end;
        button.MouseDown = function(ctl) ctl:SetBackground(0x41000197) end;
        button.MouseUp = function(ctl) ctl:SetBackground(0x41000198) end;
        Thurallor.UI.Tooltip(L:GetText("/ResetTimesControl/DeleteThisResetTime")):Attach(button);
        return button;
    end

    function AddDayDropDown(parent, dayOfWeek)
        local dropDown = Thurallor.UI.DropDown(self.weekdays.order, dayOfWeek);
        dropDown:SetParent(parent);
        dropDown:SetFont(Turbine.UI.Lotro.Font.Verdana14);
        dropDown:SetSize(110, 22);
        dropDown:SetAlignment(Turbine.UI.ContentAlignment.MiddleLeft);
        return dropDown;
    end

    local function AddLabel(parent, text, width)
        local label = Turbine.UI.Label();
        label:SetFont(Turbine.UI.Lotro.Font.Verdana14);
        label:SetFontStyle(Turbine.UI.FontStyle.Outline);
        label:SetOutlineColor(Turbine.UI.Color.Black);
        label:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
        label:SetText(text);
        label:AutoSize();
        label:SetParent(parent);
        return label;
    end

    local function AddTimeTextBox(parent, timeOfDay)
        local textBox = Turbine.UI.Lotro.TextBox();
        textBox:SetParent(parent);
        textBox:SetSelectable(true);
        textBox:SetMultiline(false);
        textBox:SetFont(Turbine.UI.Lotro.Font.Verdana14);
        textBox:SetForeColor(Turbine.UI.Color.PaleGoldenrod);
        textBox:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
        textBox:SetText(timeOfDay);
        textBox:SetSize(60, 22);
        return textBox;
    end

    local function AddResetTime(top, dayOfWeek, timeOfDay, n)
        local container = Turbine.UI.Control();
        container.n = n;
        container:SetParent(self);
        container:SetPosition(0, top);
        container:SetHeight(24);

        local left = 0;
        container.xButton = AddXButton(container);
        container.xButton:SetPosition(left, 4);
        container.xButton.MouseClick = function(_, args)
            if (args.Button == Turbine.UI.MouseButton.Left) then
                table.remove(self.resetTimes, n);
                self:DoLayout();
            end
        end
        left = left + container.xButton:GetWidth() + 4;
        
        container.dayDropDown = AddDayDropDown(container, dayOfWeek);
        container.dayDropDown:SetPosition(left, 0);
        if (not n) then
            container.dayDropDown.SelectionChanged = function(_, args)
                local prevTime = Time.GetTimeOfDayStr(3, 0);
                if (next(self.resetTimes)) then
                    prevTime = self.resetTimes[#self.resetTimes][2];
                end
                table.insert(self.resetTimes, { args.Item, prevTime });
                self:DoLayout();
            end
        else
            container.dayDropDown.SelectionChanged = function(ctl, args)
                local n = ctl:GetParent().n;
                self.resetTimes[n][1] = args.Item;
            end
        end
        left = left + container.dayDropDown:GetWidth();
        
        container.atLabel = AddLabel(container, L:GetText("/ResetTimesControl/At"));
        container.atLabel:SetPosition(left, 4);
        left = left + container.atLabel:GetWidth();
        
        container.timeTextBox = AddTimeTextBox(container, timeOfDay);
        container.timeTextBox:SetPosition(left, 0);
        container.timeTextBox.TextChanged = function(ctl)
            local hour, minute = Time.GetNumericTimeOfDay(ctl:GetText());
            local valid = (hour and (tonumber(hour) < 24)) and (minute and (tonumber(minute) < 60));
            if (valid) then
                local n = ctl:GetParent().n;
                self.resetTimes[n][2] = Time.GetTimeOfDayStr(hour, minute);
            end
            local validChanged = (valid ~= self.valid);
            self.valid = valid;
            if (validChanged) then
                DoCallbacks(self, "ValidChanged", valid);
            end
        end
        left = left + container.timeTextBox:GetWidth();
        
        container.oclockLabel = AddLabel(container, L:GetText("/ResetTimesControl/OclockServerTime"));
        container.oclockLabel:SetPosition(left, 4);
        left = left + container.oclockLabel:GetWidth();

        container:SetWidth(left);
        return container, top + 24;
    end

    self:GetControls():Clear();
    collectgarbage();

    local top, width, box = 0, 0;
    for n, data in ipairs(self.resetTimes) do
        local dayOfWeek, timeOfDay = unpack(data);
        box, top = AddResetTime(top, dayOfWeek, timeOfDay, n);
    end
    if ((not self.allowNever) and (#self.resetTimes == 1)) then
        box.xButton:SetParent(nil);
    end
    local addBox, top = AddResetTime(top, "...", "03:00");
    for _, ctl in pairs({ "xButton", "atLabel", "timeTextBox", "oclockLabel" }) do
        addBox[ctl]:SetParent(nil);
    end
    width = addBox:GetWidth();
    
    self:SetSize(width, top);
end

Compare with Previous | Blame


All times are GMT -5. The time now is 07:15 AM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui