-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.lua
104 lines (90 loc) · 2.51 KB
/
utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function shallowcopy(orig)
local copy
if type(orig) == 'table' then
copy = {}
setmetatable(copy, getmetatable(orig))
for k, v in pairs(orig) do
copy[k] = v
end
else
copy = orig
end
return copy
end
function copy1(tab)
local copy
if type(orig) == 'table' then
copy = {}
setmetatable(copy, shallowcopy(getmetatable(orig)) )
for k, v in pairs(orig) do
copy[shallowcopy(k)] = shallowcopy(v)
end
else
copy = orig
end
return copy
end
function copy2(tab)
local copy
if type(orig) == 'table' then
copy = {}
setmetatable(copy, copy1(getmetatable(orig)))
for k, v in pairs(orig) do
copy[copy1(k)] = copy1(v)
end
else
copy = orig
end
return copy
end
function copystate(state)
local copy = {}
setmetatable(copy, shallowcopy(getmetatable(state)))
for k, v in pairs(state) do
copy[shallowcopy(k)] = shallowcopy(v)
end
return copy
end
function deepcopy(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[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
lastUnique = 0
function uniqueId()
lastUnique = lastUnique + 1
return lastUnique
end
function drawFilledBar(x, y, width, height, proportion, fgcolor, bgcolor, linecolor, caption, textcolor)
bgcolor = bgcolor or {0,0,0,255}
fgcolor = fgcolor or {255,255,255,255}
linecolor = linecolor or bgcolor
textcolor = textcolor or {255,255,255,255}
resetDraw(
function ()
love.graphics.setColor(bgcolor)
love.graphics.rectangle("fill", x, y, width, height)
love.graphics.setColor(fgcolor)
love.graphics.rectangle("fill", x, y, proportion * width, height)
love.graphics.setColor(linecolor)
love.graphics.rectangle("line", x-1, y-1, width+1, height+1)
if caption then
local strWidth = love.graphics.getFont():getWidth(caption)
love.graphics.print(caption, x + width/2 - strWidth/2, y + height/2 - 7)
end
end)
end
function resetDraw(func)
r, g, b, a = love.graphics.getColor()
func()
love.graphics.setColor(r, g, b, a)
end