-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.lua
73 lines (57 loc) · 1.61 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
local util = {}
local uuidLib = require 'vendor.uuid'
uuidLib.seed()
function util.unpackPairs(t)
local rets, nArgs = {}, 0
for k, v in pairs(t) do
rets[nArgs + 1], rets[nArgs + 2] = k, v
nArgs = nArgs + 2
end
return unpack(rets, 1, nArgs)
end
function util.deepCopyTable(t)
local typ = type(t)
if typ == 'nil' or typ == 'boolean' or typ == 'number' or typ == 'string' or typ == 'function' then
return t
elseif typ == 'table' then
local u = {}
for k, v in pairs(t) do
u[util.deepCopyTable(k)] = util.deepCopyTable(v)
end
return u
else
error('deepCopyTable: bad type')
end
end
function util.quantize(value, divisor, start)
if divisor == 0 then
return value
end
start = start or 0
return divisor * math.floor(0.5 + (value - start) / divisor) + start
end
function util.uuid()
return uuidLib()
end
function util.stacktrace(message)
local stack = debug.traceback(message, 2)
for chunkName, filename in pairs(CHUNK_NAME_TO_FILE_NAME) do
local pattern = '%[string "' .. chunkName .. '"%]'
stack = stack:gsub(pattern, filename)
end
return stack
end
local ui = castle.ui
function util.uiRow(id, ...)
local nArgs = select('#', ...)
local args = { ... }
ui.box(id, { flexDirection = 'row', alignItems = 'flex-start' }, function()
for i = 1, nArgs do
ui.box(tostring(i), { flex = 1 }, args[i])
if i < nArgs then
ui.box('space', { width = 16 }, function() end)
end
end
end)
end
return util