lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

[/] [trunk/] [Thurallor/] [Common/] [Utils/] [Utils.lua] - Blame information for rev 121

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 Thurallor-7095
import "Turbine";
2 Thurallor-7095
import "Turbine.UI.Lotro";
3 Thurallor-7095
 
4 16 Thurallor-7095
-- Missing enumeration values
5 Thurallor-7095
Turbine.UI.Lotro.Action.EnterKey = 162;
6 Thurallor-7095
Turbine.UI.Lotro.Action.ToggleHUD = 268435635;
7 9 Thurallor-7095
Turbine.UI.Lotro.Font.TimesRoman12 = 0x42000021;
8 Thurallor-7095
Turbine.UI.Lotro.Font.TimesRoman20 = 0x4200000e;
9 Thurallor-7095
Turbine.UI.Lotro.Font.TimesRoman22 = 0x4200000f;
10 Thurallor-7095
Turbine.UI.Lotro.Font.TimesRoman24 = 0x42000010;
11 Thurallor-7095
--Turbine.UI.Lotro.Font.Calibri14 = 0x4200002b; -- doesn't seem to work fully
12 16 Thurallor-7095
Turbine.Gameplay.EffectCategory.Tactical = 256;
13 9 Thurallor-7095
 
14 16 Thurallor-7095
-- Add the Turbine.UI.Display.SizeChanged event.
15 Thurallor-7095
displaySizeListener = Turbine.UI.Window();
16 Thurallor-7095
displaySizeListener:SetVisible(true);
17 Thurallor-7095
displaySizeListener:SetStretchMode(0);
18 Thurallor-7095
displaySizeListener:SetSize(1, 1);
19 28 Thurallor-7095
displaySizeListener:SetWantsUpdates(true);
20 16 Thurallor-7095
displaySizeListener:SetStretchMode(1);
21 28 Thurallor-7095
displaySizeListener:SetWantsUpdates(true);
22 16 Thurallor-7095
function displaySizeListener:Update()
23 28 Thurallor-7095
    displaySizeListener:SetSize(2, 2);
24 Thurallor-7095
    self.ignoreSizeChangedEvents = 0;
25 16 Thurallor-7095
    self:SetWantsUpdates(false);
26 28 Thurallor-7095
    self.Update = self._Update;
27 Thurallor-7095
    self.SizeChanged = self._SizeChanged;
28 Thurallor-7095
end
29 Thurallor-7095
function displaySizeListener:_Update()
30 Thurallor-7095
    self:SetWantsUpdates(false);
31 16 Thurallor-7095
    local sizeChangedFunc = Turbine.UI.Display.SizeChanged;
32 Thurallor-7095
    if (type(sizeChangedFunc) == "function") then
33 Thurallor-7095
        sizeChangedFunc(Turbine.UI.Display);
34 Thurallor-7095
    elseif (type(sizeChangedFunc) == "table") then
35 Thurallor-7095
        for f = 1, #sizeChangedFunc, 1 do
36 Thurallor-7095
            sizeChangedFunc[f](Turbine.UI.Display);
37 Thurallor-7095
        end
38 Thurallor-7095
    end
39 Thurallor-7095
end
40 28 Thurallor-7095
function displaySizeListener:_SizeChanged()
41 Thurallor-7095
    if (self.ignoreSizeChangedEvents > 0) then
42 Thurallor-7095
        self.ignoreSizeChangedEvents = self.ignoreSizeChangedEvents - 1;
43 Thurallor-7095
        return;
44 Thurallor-7095
    end
45 Thurallor-7095
    self:SetSize(2, 2);
46 Thurallor-7095
    self.ignoreSizeChangedEvents = 1;
47 Thurallor-7095
    -- Need to wait until the next Update() cycle before reporting.
48 Thurallor-7095
    self:SetWantsUpdates(true);
49 Thurallor-7095
end
50 16 Thurallor-7095
 
51 23 Thurallor-7095
-- Copies the source table into the destination table (or a new table, if 'destTable' is absent).
52 Thurallor-7095
-- Subtables are duplicated.
53 Thurallor-7095
-- Returns the destination table.
54 2 Thurallor-7095
function _G.DeepTableCopy(sourceTable, destTable)
55 23 Thurallor-7095
    if (destTable == nil) then
56 Thurallor-7095
        destTable = {};
57 Thurallor-7095
    end
58 2 Thurallor-7095
    if (type(sourceTable) ~= "table") then
59 121 Thurallor-7095
        error("DeepTableCopy(): sourceTable is " .. type(sourceTable), 2);
60 2 Thurallor-7095
    elseif (type(destTable) ~= "table") then
61 121 Thurallor-7095
        error("DeepTableCopy(): destTable is " .. type(destTable), 2);
62 2 Thurallor-7095
    end
63 Thurallor-7095
    for k, v in pairs(sourceTable) do
64 Thurallor-7095
        if (type(v) == "table") then
65 Thurallor-7095
            destTable[k] = { };
66 Thurallor-7095
            DeepTableCopy(v, destTable[k]);
67 Thurallor-7095
        else
68 Thurallor-7095
            destTable[k] = v;
69 Thurallor-7095
        end
70 Thurallor-7095
    end
71 23 Thurallor-7095
    return destTable;
72 2 Thurallor-7095
end
73 Thurallor-7095
 
74 23 Thurallor-7095
-- Copies the source table into the destination table (or a new table, if 'destTable' is absent).
75 Thurallor-7095
-- Subtables are shared (not duplicated).
76 Thurallor-7095
-- Returns the destination table.
77 Thurallor-7095
function _G.ShallowTableCopy(sourceTable, destTable)
78 Thurallor-7095
    if (destTable == nil) then
79 Thurallor-7095
        destTable = {};
80 Thurallor-7095
    end
81 Thurallor-7095
    if (type(sourceTable) ~= "table") then
82 121 Thurallor-7095
        error("ShallowTableCopy(): sourceTable is " .. type(sourceTable), 2);
83 23 Thurallor-7095
    elseif (type(destTable) ~= "table") then
84 121 Thurallor-7095
        error("ShallowTableCopy(): destTable is " .. type(destTable), 2);
85 23 Thurallor-7095
    end
86 Thurallor-7095
    for k, v in pairs(sourceTable) do
87 Thurallor-7095
        destTable[k] = v;
88 Thurallor-7095
    end
89 Thurallor-7095
    return destTable;
90 Thurallor-7095
end
91 Thurallor-7095
 
92 2 Thurallor-7095
function _G.Puts(str)
93 Thurallor-7095
    local prefix = "";
94 7 Thurallor-7095
    if (_G.PutsPrefix) then
95 Thurallor-7095
        prefix = _G.PutsPrefix;
96 2 Thurallor-7095
    end
97 Thurallor-7095
    Turbine.Shell.WriteLine(prefix .. tostring(str));
98 Thurallor-7095
end
99 Thurallor-7095
 
100 21 Thurallor-7095
-- Returns a compact Lua representation of a table.  Has optional 'maxdepth' arg to prevent runaway recursion.
101 Thurallor-7095
function _G.Serialize(obj, maxdepth)
102 Thurallor-7095
    if (type(maxdepth) == "number") then
103 Thurallor-7095
        maxdepth = maxdepth - 1;
104 Thurallor-7095
    end
105 2 Thurallor-7095
    if (type(obj) == "boolean") then
106 Thurallor-7095
        if (obj) then
107 Thurallor-7095
            return "true";
108 Thurallor-7095
        else
109 Thurallor-7095
            return "false";
110 Thurallor-7095
        end
111 Thurallor-7095
    elseif (type(obj) == "number") then
112 Thurallor-7095
        local text = tostring(obj);
113 Thurallor-7095
        -- Change floating-point numbers to English format
114 Thurallor-7095
        return string.gsub(text, ",", ".");
115 Thurallor-7095
    elseif (type(obj) == "string") then
116 Thurallor-7095
        return string.format("%q", obj);
117 Thurallor-7095
    elseif (type(obj) == "table") then
118 21 Thurallor-7095
        if ((type(maxdepth) == "number") and (maxdepth < 0)) then
119 Thurallor-7095
            return tostring(obj);
120 Thurallor-7095
        else
121 Thurallor-7095
            local text = "{";
122 Thurallor-7095
            local i = 1;
123 Thurallor-7095
            for key in sorted_keys(obj) do
124 Thurallor-7095
                local value = Serialize(obj[key], maxdepth);
125 Thurallor-7095
                if (value ~= nil) then
126 Thurallor-7095
                    local item = value .. ",";
127 Thurallor-7095
                    if (key ~= i) then
128 Thurallor-7095
                        local index = Serialize(key, maxdepth);
129 Thurallor-7095
                        item = "[" .. index .. "]=" .. item;
130 Thurallor-7095
                    else
131 Thurallor-7095
                        i = i + 1;
132 Thurallor-7095
                    end
133 Thurallor-7095
                    text = text .. item;
134 2 Thurallor-7095
                end
135 Thurallor-7095
            end
136 21 Thurallor-7095
            text = string.gsub(text, ",$", "");
137 Thurallor-7095
            text = text .. "}";
138 Thurallor-7095
--text = tostring(obj) .. ":" .. text;
139 Thurallor-7095
            return text;
140 2 Thurallor-7095
        end
141 Thurallor-7095
    else
142 Thurallor-7095
--Turbine.Shell.WriteLine("unknown type " .. tostring(type));
143 Thurallor-7095
        return tostring(obj);
144 Thurallor-7095
    end
145 Thurallor-7095
end
146 Thurallor-7095
 
147 21 Thurallor-7095
-- Returns a multi-line Lua representation of a table.  Has optional 'maxdepth' arg to prevent runaway recursion.
148 Thurallor-7095
function _G.PrettyPrint(obj, prefix, maxdepth)
149 Thurallor-7095
    if (type(maxdepth) == "number") then
150 Thurallor-7095
        maxdepth = maxdepth - 1;
151 Thurallor-7095
    end
152 2 Thurallor-7095
    if (type(obj) == "boolean") then
153 Thurallor-7095
        if (obj) then
154 Thurallor-7095
            return "true";
155 Thurallor-7095
        else
156 Thurallor-7095
            return "false";
157 Thurallor-7095
        end
158 Thurallor-7095
    elseif (type(obj) == "number") then
159 Thurallor-7095
        local text = tostring(obj);
160 Thurallor-7095
        -- Change floating-point numbers to English format
161 Thurallor-7095
        return string.gsub(text, ",", ".");
162 Thurallor-7095
    elseif (type(obj) == "string") then
163 Thurallor-7095
        return string.format("%q", obj);
164 Thurallor-7095
    elseif (type(obj) == "table") then
165 21 Thurallor-7095
        if ((type(maxdepth) == "number") and (maxdepth < 0)) then
166 Thurallor-7095
            return tostring(obj);
167 Thurallor-7095
        else
168 Thurallor-7095
            local text = "{\n";
169 Thurallor-7095
            local newPrefix = prefix .. "   ";
170 Thurallor-7095
            local i = 1;
171 Thurallor-7095
            local count = 0;
172 Thurallor-7095
            for key in sorted_keys(obj) do
173 Thurallor-7095
                local value = PrettyPrint(obj[key], newPrefix, maxdepth);
174 Thurallor-7095
                if (value ~= nil) then
175 Thurallor-7095
                    local item = value .. ";";
176 Thurallor-7095
                    if (key ~= i) then
177 Thurallor-7095
                        local index = Serialize(key, maxdepth);
178 Thurallor-7095
                        item = "[" .. index .. "] = " .. item;
179 Thurallor-7095
                    else
180 Thurallor-7095
                        i = i + 1;
181 Thurallor-7095
                    end
182 Thurallor-7095
                    text = text .. newPrefix .. item .. "\n";
183 Thurallor-7095
                    count = count + 1;
184 2 Thurallor-7095
                end
185 Thurallor-7095
            end
186 21 Thurallor-7095
            if (count == 0) then
187 Thurallor-7095
                text = "{}";
188 Thurallor-7095
            else
189 Thurallor-7095
                text = string.gsub(text, ",$", "");
190 Thurallor-7095
                text = text .. prefix .. "}";
191 Thurallor-7095
            end
192 Thurallor-7095
--text = tostring(obj) .. ":" .. text;
193 Thurallor-7095
            return text;
194 2 Thurallor-7095
        end
195 Thurallor-7095
    else
196 Thurallor-7095
--Turbine.Shell.WriteLine("unknown type " .. tostring(type));
197 Thurallor-7095
        return tostring(obj);
198 Thurallor-7095
    end
199 Thurallor-7095
end
200 Thurallor-7095
 
201 Thurallor-7095
-- Prepares a table for saving.  Workaround for Turbine.PluginData.Save() bug.
202 Thurallor-7095
function _G.ExportTable(obj)
203 Thurallor-7095
    if (type(obj) == "number") then
204 Thurallor-7095
        local text = tostring(obj);
205 Thurallor-7095
        -- Change floating-point numbers to English format
206 Thurallor-7095
        return "#" .. string.gsub(text, ",", ".");
207 Thurallor-7095
    elseif (type(obj) == "string") then
208 Thurallor-7095
        return "$" .. obj;
209 Thurallor-7095
    elseif (type(obj) == "table") then
210 Thurallor-7095
        local newTable = {};
211 Thurallor-7095
        for i, v in pairs(obj) do
212 Thurallor-7095
            newTable[ExportTable(i)] = ExportTable(v);
213 Thurallor-7095
        end
214 Thurallor-7095
        return newTable;
215 Thurallor-7095
    else
216 Thurallor-7095
        return obj;
217 Thurallor-7095
    end
218 Thurallor-7095
end
219 Thurallor-7095
 
220 Thurallor-7095
-- Prepares a loaded table for use.  Workaround for Turbine.PluginData.Save() bug.
221 Thurallor-7095
function _G.ImportTable(obj)
222 Thurallor-7095
    if (type(obj) == "string") then
223 Thurallor-7095
        local prefix = string.sub(obj, 1, 1);
224 Thurallor-7095
        if (prefix == "$") then
225 Thurallor-7095
            return string.sub(obj, 2);
226 Thurallor-7095
        elseif (prefix == "#") then
227 Thurallor-7095
                        -- need to run it through interpreter, since tonumber() may only accept ","
228 Thurallor-7095
                        return loadstring("return " .. string.sub(obj, 2))();
229 Thurallor-7095
        else -- shouldn't happen
230 Thurallor-7095
            return obj;
231 Thurallor-7095
        end
232 Thurallor-7095
    elseif (type(obj) == "table") then
233 Thurallor-7095
        local newTable = {};
234 Thurallor-7095
        for i, v in pairs(obj) do
235 Thurallor-7095
            newTable[ImportTable(i)] = ImportTable(v);
236 Thurallor-7095
        end
237 Thurallor-7095
        return newTable;
238 Thurallor-7095
    else
239 Thurallor-7095
        return obj;
240 Thurallor-7095
    end
241 Thurallor-7095
end
242 Thurallor-7095
 
243 Thurallor-7095
function _G.GetAssetSize(id)
244 Thurallor-7095
    local temp = Turbine.UI.Control();
245 Thurallor-7095
    temp:SetBackground(id);
246 Thurallor-7095
    temp:SetStretchMode(2);
247 Thurallor-7095
    return temp:GetWidth(), temp:GetHeight();
248 Thurallor-7095
end
249 Thurallor-7095
 
250 Thurallor-7095
function _G.AddCallback(object, event, callback)
251 Thurallor-7095
    if (object[event] == nil) then
252 Thurallor-7095
        object[event] = callback;
253 Thurallor-7095
    else
254 Thurallor-7095
        if (type(object[event]) == "table") then
255 Thurallor-7095
            table.insert(object[event], callback);
256 Thurallor-7095
        else
257 Thurallor-7095
            object[event] = {object[event], callback};
258 Thurallor-7095
        end
259 Thurallor-7095
    end
260 Thurallor-7095
    return callback;
261 Thurallor-7095
end
262 Thurallor-7095
 
263 Thurallor-7095
function _G.RemoveCallback(object, event, callback)
264 36 Thurallor-7095
    if (callback == nil) then
265 Thurallor-7095
        return;
266 Thurallor-7095
    end
267 Thurallor-7095
    local handlers = object[event];
268 Thurallor-7095
    if (handlers == callback) then
269 2 Thurallor-7095
        object[event] = nil;
270 36 Thurallor-7095
    elseif (type(handlers) == "table") then
271 Thurallor-7095
        local f, i;
272 Thurallor-7095
        repeat
273 Thurallor-7095
            i, f = next(handlers, i);
274 Thurallor-7095
            if (f == callback) then
275 Thurallor-7095
                handlers[i] = nil;
276 2 Thurallor-7095
            end
277 36 Thurallor-7095
        until (i == nil);
278 2 Thurallor-7095
    end
279 Thurallor-7095
end
280 Thurallor-7095
 
281 20 Thurallor-7095
function _G.DoCallbacks(object, event, args)
282 36 Thurallor-7095
    local handlers = object[event];
283 Thurallor-7095
    if (type(handlers) == "function") then
284 Thurallor-7095
        handlers(object, args);
285 Thurallor-7095
    elseif (type(handlers) == "table") then
286 Thurallor-7095
        if (next(handlers) == nil) then
287 Thurallor-7095
            -- If all handlers have been removed, remove the table.
288 Thurallor-7095
            object[event] = nil;
289 Thurallor-7095
        else
290 Thurallor-7095
            local f, i;
291 Thurallor-7095
            repeat
292 Thurallor-7095
                i, f = next(handlers, i);
293 Thurallor-7095
                if (type(f) == "function") then
294 Thurallor-7095
                    f(object, args);
295 Thurallor-7095
                end
296 Thurallor-7095
            until (i == nil);
297 18 Thurallor-7095
        end
298 Thurallor-7095
    end
299 Thurallor-7095
end
300 Thurallor-7095
 
301 2 Thurallor-7095
-- For iterating over the keys of a hash table in a for loop
302 Thurallor-7095
function _G.keys(tableVar)
303 Thurallor-7095
    if (type(tableVar) ~= "table") then
304 Thurallor-7095
        error("bad argument to 'keys' (table expected, got " .. type(tableVar) .. ")", 2);
305 Thurallor-7095
    end
306 Thurallor-7095
    local state = { ["tableVar"] = tableVar, ["index"] = nil };
307 Thurallor-7095
    local function iterator(state)
308 Thurallor-7095
        state.index = next(state.tableVar, state.index);
309 Thurallor-7095
        return state.index;
310 Thurallor-7095
    end
311 Thurallor-7095
    return iterator, state, nil;
312 Thurallor-7095
end
313 Thurallor-7095
 
314 Thurallor-7095
-- For iterating over the values of a hash table in a for loop
315 Thurallor-7095
function _G.values(tableVar)
316 Thurallor-7095
    if (type(tableVar) ~= "table") then
317 Thurallor-7095
        error("bad argument to 'values' (table expected, got " .. type(tableVar) .. ")", 2);
318 Thurallor-7095
    end
319 Thurallor-7095
    local state = { ["tableVar"] = tableVar, ["index"] = nil };
320 Thurallor-7095
    local function iterator(state)
321 Thurallor-7095
        state.index, value = next(state.tableVar, state.index);
322 Thurallor-7095
        return value;
323 Thurallor-7095
    end
324 Thurallor-7095
    return iterator, state, nil;
325 Thurallor-7095
end
326 Thurallor-7095
 
327 Thurallor-7095
-- For iterating over the keys of a hash table in a for loop, after sorting the keys
328 Thurallor-7095
function _G.sorted_keys(tableVar)
329 Thurallor-7095
    if (type(tableVar) ~= "table") then
330 Thurallor-7095
        error("bad argument to 'keys' (table expected, got " .. type(tableVar) .. ")", 2);
331 Thurallor-7095
    end
332 Thurallor-7095
    local state = { ["sortedKeys"] = {}, ["index"] = nil };
333 Thurallor-7095
    for key in keys(tableVar) do
334 Thurallor-7095
        table.insert(state.sortedKeys, key);
335 Thurallor-7095
    end
336 Thurallor-7095
    local sortFunc = function(a, b)
337 21 Thurallor-7095
        if ((type(a) == type(b)) and ((type(a) == "number") or (type(a) == "string"))) then
338 2 Thurallor-7095
            return a < b;
339 Thurallor-7095
        else
340 Thurallor-7095
            return tostring(a) < tostring(b);
341 Thurallor-7095
        end
342 Thurallor-7095
    end
343 Thurallor-7095
    table.sort(state.sortedKeys, sortFunc);
344 Thurallor-7095
    local function iterator(state)
345 Thurallor-7095
        state.index, value = next(state.sortedKeys, state.index);
346 Thurallor-7095
        return value;
347 Thurallor-7095
    end
348 Thurallor-7095
    return iterator, state, nil;
349 Thurallor-7095
end
350 Thurallor-7095
 
351 7 Thurallor-7095
-- Searches the given table for the specified value, returning the corresponding key
352 15 Thurallor-7095
function _G.Search(tableVar, value)
353 7 Thurallor-7095
    if (type(tableVar) ~= "table") then
354 15 Thurallor-7095
        error("bad argument to 'Search' (table expected, got " .. type(tableVar) .. ")", 2);
355 7 Thurallor-7095
    end
356 Thurallor-7095
    local i = nil;
357 Thurallor-7095
    repeat
358 Thurallor-7095
        i, v = next(tableVar, i);
359 Thurallor-7095
        if (v == value) then
360 Thurallor-7095
            return i;
361 Thurallor-7095
        end
362 Thurallor-7095
    until (not i);
363 Thurallor-7095
    return nil;
364 Thurallor-7095
end
365 Thurallor-7095
 
366 20 Thurallor-7095
-- Returns true if an item is equipped.
367 Thurallor-7095
-- If 'itemSlot' is specified, the search is restricted to a single equipment slot.
368 Thurallor-7095
-- If 'itemName' is not specified, any item will be accepted.
369 12 Thurallor-7095
-- Note: There is currently no way to distinguish between items with the same
370 20 Thurallor-7095
--       name..
371 Thurallor-7095
function _G.IsEquipped(itemName, itemSlot)
372 Thurallor-7095
    local slot, item = Thurallor.Utils.Watcher.GetEquippedItem(itemName, itemSlot);
373 Thurallor-7095
    return (slot ~= nil);
374 12 Thurallor-7095
end
375 Thurallor-7095
 
376 6 Thurallor-7095
function _G.Unequip(itemSlot, itemSlotName, targetBagSlot)
377 2 Thurallor-7095
    local lp = Turbine.Gameplay.LocalPlayer:GetInstance();
378 Thurallor-7095
    local bags = lp:GetBackpack();
379 Thurallor-7095
    local equippedItems = lp:GetEquipment();
380 Thurallor-7095
    local item = equippedItems:GetItem(itemSlot);
381 Thurallor-7095
    if (item == nil) then
382 6 Thurallor-7095
--        Turbine.Shell.WriteLine("You're not wearing a " .. itemSlotName .. ".");
383 2 Thurallor-7095
        return false;
384 Thurallor-7095
    end
385 114 Thurallor-7095
    if (targetBagSlot == nil) then
386 Thurallor-7095
        for index = 1, bags:GetSize() do
387 6 Thurallor-7095
            if (bags:GetItem(index) == nil) then
388 114 Thurallor-7095
                targetBagSlot = index;
389 6 Thurallor-7095
                break;
390 Thurallor-7095
            end
391 Thurallor-7095
        end
392 Thurallor-7095
    end
393 114 Thurallor-7095
    if ((targetBagSlot == nil) or (bags:GetItem(targetBagSlot) ~= nil)) then
394 6 Thurallor-7095
--        Turbine.Shell.WriteLine("You do not have enough room in your bags to unequip your " .. itemSlotName .. ".");
395 2 Thurallor-7095
        return false;
396 Thurallor-7095
    end
397 114 Thurallor-7095
    bags:PerformItemDrop(item, targetBagSlot, false);
398 Thurallor-7095
--    Turbine.Shell.WriteLine("You unequip your " .. itemSlotName .. " into bag slot " .. tostring(bagSlot) .. ".");
399 Thurallor-7095
    return true;
400 2 Thurallor-7095
end
401 Thurallor-7095
 
402 Thurallor-7095
function _G.CenterWindow(window)
403 Thurallor-7095
    local displayWidth, displayHeight = Turbine.UI.Display.GetSize();
404 Thurallor-7095
    local windowWidth, windowHeight = window:GetSize();
405 20 Thurallor-7095
    local left = math.floor((displayWidth - windowWidth) / 2);
406 Thurallor-7095
    local top = math.floor((displayHeight - windowHeight) / 2);
407 Thurallor-7095
    window:SetPosition(left, top);
408 2 Thurallor-7095
end
409 Thurallor-7095
 
410 Thurallor-7095
-- Creates an alert window with a scrollable text box in it.
411 27 Thurallor-7095
function _G.Alert(title, contents, okButton, font)
412 2 Thurallor-7095
    local window = Turbine.UI.Lotro.Window();
413 Thurallor-7095
    window:SetVisible(true);
414 Thurallor-7095
    window:SetSize(400, 300);
415 Thurallor-7095
    window:SetText(title);
416 Thurallor-7095
    window:SetResizable(true);
417 Thurallor-7095
 
418 Thurallor-7095
    window.label = Turbine.UI.Label();
419 Thurallor-7095
    window.label:SetParent(window);
420 27 Thurallor-7095
    if (not font) then
421 Thurallor-7095
        font = Turbine.UI.Lotro.Font.Verdana10;
422 Thurallor-7095
    end
423 Thurallor-7095
    window.label:SetFont(font);
424 2 Thurallor-7095
    window.label:SetBackground(0x411348A7);
425 Thurallor-7095
    window.label:SetPosition(14, 47);
426 Thurallor-7095
    window.label:SetText(contents);
427 Thurallor-7095
    window.label:SetSelectable(true);
428 Thurallor-7095
 
429 Thurallor-7095
    window.scrollBar = Turbine.UI.Lotro.ScrollBar();
430 Thurallor-7095
    window.scrollBar:SetParent(window);
431 Thurallor-7095
    window.scrollBar:SetOrientation(Turbine.UI.Orientation.Vertical);
432 Thurallor-7095
    window.scrollBar:SetWidth(10);
433 Thurallor-7095
    window.scrollBar:SetTop(47);
434 Thurallor-7095
    window.label:SetVerticalScrollBar(window.scrollBar);
435 Thurallor-7095
 
436 Thurallor-7095
    window.buttons = {};
437 Thurallor-7095
    if (okButton) then
438 Thurallor-7095
        local button = Turbine.UI.Lotro.Button();
439 Thurallor-7095
        window.buttons[okButton] = button;
440 Thurallor-7095
        button:SetText(okButton);
441 Thurallor-7095
        button:SetParent(window);
442 Thurallor-7095
        button:SetSize(100, 20);
443 Thurallor-7095
        button.leftOffset = -50;
444 Thurallor-7095
    end
445 Thurallor-7095
 
446 Thurallor-7095
    window.SizeChanged = function(w)
447 Thurallor-7095
        width, height = w:GetSize();
448 Thurallor-7095
        w.label:SetSize(width - 27, height - 80);
449 Thurallor-7095
        w.scrollBar:SetHeight(height - 80);
450 Thurallor-7095
        w.scrollBar:SetLeft(width - 14);
451 Thurallor-7095
        for name, button in pairs(w.buttons) do
452 Thurallor-7095
            local center = math.floor(0.5 + width / 2);
453 Thurallor-7095
            button:SetPosition(center + button.leftOffset, height - 30);
454 Thurallor-7095
        end
455 Thurallor-7095
    end
456 Thurallor-7095
    window:SizeChanged();
457 Thurallor-7095
 
458 Thurallor-7095
    if (not _G.alertWindows) then _G.alertWindows = {} end
459 Thurallor-7095
    window.winIndex = #_G.alertWindows + 1;
460 Thurallor-7095
    window:SetPosition(window.winIndex * 6, window.winIndex * 27);
461 Thurallor-7095
    table.insert(_G.alertWindows, window.winIndex, window);
462 Thurallor-7095
 
463 Thurallor-7095
    window.Closing = function(sender)
464 Thurallor-7095
        table.remove(_G.alertWindows, sender.winIndex);
465 Thurallor-7095
    end
466 Thurallor-7095
 
467 Thurallor-7095
    return window;
468 Thurallor-7095
end
469 Thurallor-7095
 
470 Thurallor-7095
-- Encodes binary into printable characters (using only ASCII codes 46-110).
471 Thurallor-7095
-- Starting at 46 skips past the hyphen, which causes word-wrapping in TextBoxes.
472 Thurallor-7095
function _G.Bin2Text(data)
473 Thurallor-7095
    data = data .. string.rep(string.char(0), 2); -- pad with null characters, if needed
474 Thurallor-7095
    local string_byte, string_char = string.byte, string.char; -- local is faster than global
475 Thurallor-7095
    local minChar = 46;
476 Thurallor-7095
    local result, j, X1, X2, X3, Y1, Y2, Y3, Y4 = "", 1;
477 Thurallor-7095
    for i = 1, #data - 2, 3 do
478 Thurallor-7095
        X1, X2, X3 = string_byte(data, i, i + 2);
479 Thurallor-7095
        Y1 = bit.brshift(X1, 2) + minChar;
480 Thurallor-7095
        Y2 = bit.blshift(bit.band(X1, 3), 4) + bit.brshift(X2, 4) + minChar;
481 Thurallor-7095
        Y3 = bit.blshift(bit.band(X2, 15), 2) + bit.brshift(X3, 6) + minChar;
482 Thurallor-7095
        Y4 = bit.band(X3, 63) + minChar;
483 Thurallor-7095
        result = result .. string_char(Y1, Y2, Y3, Y4);
484 Thurallor-7095
        j = j + 4;
485 Thurallor-7095
    end
486 Thurallor-7095
    return result;
487 Thurallor-7095
end
488 Thurallor-7095
 
489 Thurallor-7095
-- Decodes a string encoded with Bin2Text() back into binary.
490 Thurallor-7095
function _G.Text2Bin(data)
491 Thurallor-7095
    local string_byte, string_char = string.byte, string.char; -- local is faster than global
492 Thurallor-7095
    local minChar = 46;
493 Thurallor-7095
    local result, j, X1, X2, X3, X4, Y1, Y2, Y3 = "", 1;
494 Thurallor-7095
    for i = 1, #data - 3, 4 do
495 Thurallor-7095
        X1, X2, X3, X4 = string_byte(data, i, i + 3);
496 Thurallor-7095
        X1, X2, X3, X4 = X1 - minChar, X2 - minChar, X3 - minChar, X4 - minChar;
497 Thurallor-7095
        Y1 = bit.blshift(X1, 2) + bit.brshift(X2, 4);
498 Thurallor-7095
        Y2 = bit.blshift(bit.band(X2, 15), 4) + bit.brshift(X3, 2);
499 Thurallor-7095
        Y3 = bit.blshift(bit.band(X3, 3), 6) + X4;
500 Thurallor-7095
        result = result .. string_char(Y1, Y2, Y3);
501 Thurallor-7095
        j = j + 3;
502 Thurallor-7095
    end
503 Thurallor-7095
    -- Remove up to two trailing null characters.
504 Thurallor-7095
    return string.gsub(result, "%z$", "", 2);
505 Thurallor-7095
end
506 Thurallor-7095
 
507 Thurallor-7095
-- Decodes a string encoded with Bin2Text() back into binary.
508 Thurallor-7095
function _G.Text2Bin_old(data)
509 Thurallor-7095
    local X = {string.byte(data, 1, -1)};
510 Thurallor-7095
    local result, j, Y1, Y2, Y3 = "", 1;
511 Thurallor-7095
    local minChar = 46;
512 Thurallor-7095
    for i = 1, #X - 3, 4 do
513 Thurallor-7095
        local X1, X2, X3, X4 = X[i] - minChar, X[i + 1] - minChar, X[i + 2] - minChar, X[i + 3] - minChar;
514 Thurallor-7095
        Y1 = bit.blshift(X1, 2) + bit.brshift(X2, 4);
515 Thurallor-7095
        Y2 = bit.blshift(bit.band(X2, 15), 4) + bit.brshift(X3, 2);
516 Thurallor-7095
        Y3 = bit.blshift(bit.band(X3, 3), 6) + X4;
517 Thurallor-7095
        result = result .. string.char(Y1, Y2, Y3);
518 Thurallor-7095
        j = j + 3;
519 Thurallor-7095
    end
520 Thurallor-7095
    -- Remove up to two trailing null characters.
521 Thurallor-7095
    return string.gsub(result, "%z$", "", 2);
522 Thurallor-7095
end

All times are GMT -5. The time now is 06:11 PM.


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