aboutsummaryrefslogtreecommitdiffstats
path: root/Utils.lua
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2014-03-13 16:14:10 +0100
committerpiernov <piernov@piernov.org>2014-03-13 16:14:10 +0100
commit9004e721dd27b2d1c330cf1ce35771ca3c91ac82 (patch)
treeb45e7ce8775e3badc84233a0079fd9628303559b /Utils.lua
downloadMaiLÖVE-9004e721dd27b2d1c330cf1ce35771ca3c91ac82.tar.gz
MaiLÖVE-9004e721dd27b2d1c330cf1ce35771ca3c91ac82.tar.bz2
MaiLÖVE-9004e721dd27b2d1c330cf1ce35771ca3c91ac82.tar.xz
MaiLÖVE-9004e721dd27b2d1c330cf1ce35771ca3c91ac82.zip
Importation du projet
Diffstat (limited to 'Utils.lua')
-rwxr-xr-xUtils.lua56
1 files changed, 56 insertions, 0 deletions
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