lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

[/] [trunk/] [Thurallor/] [SequenceBars/] [ExportWindow.lua] - Rev 199

Compare with Previous | Blame | View Log

ExportWindow = class();

function ExportWindow:Constructor(title, exportData)
    local prevContext = L:SetContext("/ExportWindow");
    self.uncompressedData = PrettyPrint(exportData, "");
    self.compactData = Serialize(exportData);
    -- Sanity check:
    local f, e = loadstring("return " .. self.compactData);
    if (not f) then
        Puts(L:GetText("/ImportWindow/ParseError") .. tostring(e));
        return self:Close();
    end

    self.ok = L:GetText("OK");
    self.window = Alert(title, "", self.ok);
    self.window:SetSize(423, 300);
    self.window:SetMinimumSize(423, 300);
    CenterWindow(self.window);
    self.window:SetZOrder(2147483647);
    self.okButton = self.window.buttons[self.ok];
    self.okButton:SetEnabled(false);
    self.okButton.Click = function()
        self.window:Close();
    end

    -- Workaround for a bug in the Turbine.UI.Label control: It fails to display certain sequences including "<>" even when markup is disabled.
    self.window.label.SetText = function(ctl, text)
        Turbine.UI.Label.SetText(ctl, "");
        local firstPart = "";
        repeat
            Turbine.UI.Label.AppendText(ctl, firstPart);
            text = text:sub(firstPart:len() + 1);
            firstPart = text:match("^(.-<)>");
        until (not firstPart);
        Turbine.UI.Label.AppendText(ctl, text);
    end
    
    self.compressed = Turbine.UI.Lotro.CheckBox();
    self.compressed:SetParent(self.window);
    self.compressed:SetSize(100, 20);
    self.compressed:SetFont(Turbine.UI.Lotro.Font.TrajanPro14);
    self.compressed:SetText(L:GetText("Compressed"));
    self.compressed:SetEnabled(false);
    self.compressed.CheckedChanged = function(control)
        if (control:IsChecked()) then
            self.window.label:SetText(self.compressedData);
        else
            if (#self.uncompressedData > 65535) then
                Puts(L:GetText("/ExportWindow/UncompressedDataTooBig"));
                control:SetChecked(true);
            else
                self.window.label:SetText(self.uncompressedData);
            end
        end
    end
    
    -- Compress the data in the next Update cycle.
    self.window:SetWantsUpdates(true);
    self.window.updateCount = 0;
    self.window.Update = function(w)
        w.updateCount = w.updateCount + 1;
        if (w.updateCount == 1) then
            w.label:SetText(L:GetText("/ExportWindow/Compressing"));
            Puts(L:GetText("/ExportWindow/Compressing"));
        elseif (w.updateCount == 3) then
            w.compressor = Thurallor.Utils.LibCompress();
        elseif (w.updateCount == 4) then
            -- Apparently the LZW encoding function can produce incorrect results, so we'll only use Huffman.
            --self.compressedData = w.compressor:Compress(self.compactData);
            self.compressedData = w.compressor:CompressHuffman(self.compactData);

            -- Sanity check: Uncompress the compressed data and verify that the result is the same as the original data.
            if (w.compressor:Decompress(self.compressedData) ~= self.compactData) then
                Puts(L:GetText("/ExportWindow/CompressionFailed"));
                w.updateCount = -1; -- error; close window at next frame
            end
        elseif (w.updateCount == 5) then
            w.label:SetText(w.label:GetText() .. "\n" .. L:GetText("/ExportWindow/Encoding"));
            Puts(L:GetText("/ExportWindow/Encoding"));
        elseif (w.updateCount == 7) then
            self.compressedData = Bin2Text(self.compressedData);
            
--local ratio = (1 - #self.compressedData / #self.uncompressedData) * 100;
--Puts("Compressed " .. tostring(#self.uncompressedData) .. " bytes by " .. string.format("%.0f%%", ratio) .. " to " .. tostring(#self.compressedData) .. " bytes.");
            if (#self.compressedData > 65535) then
                Puts(L:GetText("/ExportWindow/ExportFailedTooBig"));
                w.updateCount = -1; -- error; close window at next frame
                return;
            end

            -- Sanity check: Decode and decompress the data and verify that the result is the same as the original data.
            if (w.compressor:Decompress(Text2Bin(self.compressedData)) ~= self.compactData) then
                Puts(L:GetText("/ExportWindow/EncodingFailed"));
                w.updateCount = -1; -- error; close window at next frame
            end
        elseif (w.updateCount == 8) then
            self.compressed:SetChecked(true); -- trigger CheckedChanged callback
        elseif (w.updateCount == 9) then
            w.compressor = nil;
            self.compressed:SetEnabled(true);
            self.okButton:SetEnabled(true);
            self.window:Activate();
            self.window.label:SelectAll();
            self.window.label:Focus();
            w:SetWantsUpdates(false);
        elseif (w.updateCount == 0) then -- error
            w:SetWantsUpdates(false);
            w:Close();
        end
    end

    AddCallback(self.window, "Closing", function()
        if (self.Closing) then
            self:Closing();
        end
    end);

    AddCallback(self.window, "SizeChanged", function()
        self:SizeChanged();
    end);
    self:SizeChanged();
    
    L:SetContext(prevContext);
end

function ExportWindow:SizeChanged()
    local left, top = self.window.buttons[self.ok]:GetPosition();
    self.compressed:SetPosition(left - 120, top);
end

function ExportWindow:Close()
    self.window:Close();
end

Compare with Previous | Blame


All times are GMT -5. The time now is 06:38 AM.


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