From 9004e721dd27b2d1c330cf1ce35771ca3c91ac82 Mon Sep 17 00:00:00 2001 From: piernov Date: Thu, 13 Mar 2014 16:14:10 +0100 Subject: Importation du projet --- Utils.lua | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 Utils.lua (limited to 'Utils.lua') diff --git a/Utils.lua b/Utils.lua new file mode 100755 index 0000000..5e06f97 --- /dev/null +++ b/Utils.lua @@ -0,0 +1,56 @@ +local Utils = {} + +function Utils.percentCoordinates(x, y) + x = (x/100)*Config.width + y = (y/100)*Config.height + return x, y +end + +function Utils.tprint(tbl, indent) +if not indent then indent = 0 end +for k, v in pairs(tbl) do +formatting = string.rep(" ", indent) .. k .. ": " +if type(v) == "table" then +print(formatting) +Utils.tprint(v, indent+1) +else +print(formatting .. v) +end +end +end + +function Utils.tuple2ToIndexHash(table) + local tmp = {} + for k,v in pairs(table) do + tmp[v[1]] = k + end + return tmp +end + +-- From http://lua-users.org/wiki/CopyTable +function Utils.copyTable(orig) + local orig_type = type(orig) + local copy + if orig_type == 'table' then + copy = {} + for orig_key, orig_value in next, orig, nil do + copy[Utils.copyTable(orig_key)] = Utils.copyTable(orig_value) + end + setmetatable(copy, Utils.copyTable(getmetatable(orig))) + else -- number, string, boolean, etc + copy = orig + end + return copy +end + +function Utils.sortByArtistAndTitle(a, b) + if a.artist:lower() < b.artist:lower() then + return true + elseif a.artist:lower() == b.artist:lower() and a.title:lower() < b.title:lower() then + return true + else + return false + end +end + +return Utils -- cgit v1.2.3-54-g00ecf