-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathutil.lua
271 lines (206 loc) · 6.33 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
--[[------------------------------------------------
-- LÖVE Frames --
-- By Nikolai Resokav --
--]]------------------------------------------------
-- util library
loveframes.util = {}
--[[---------------------------------------------------------
- func: SetActiveSkin(name)
- desc: sets the active skin
--]]---------------------------------------------------------
function loveframes.util.SetActiveSkin(name)
loveframes.config["ACTIVESKIN"] = name
end
--[[---------------------------------------------------------
- func: GetActiveSkin()
- desc: gets the active skin
--]]---------------------------------------------------------
function loveframes.util.GetActiveSkin()
local index = loveframes.config["ACTIVESKIN"]
local skin = loveframes.skins.available[index]
return skin
end
--[[---------------------------------------------------------
- func: BoundingBox(x1, x2, y1, y2, w1, w2, h1, h2)
- desc: checks for a collision between two boxes
- note: i take no credit for this function
--]]---------------------------------------------------------
function loveframes.util.BoundingBox(x1, x2, y1, y2, w1, w2, h1, h2)
if x1 > x2 + w2 - 1 or y1 > y2 + h2 - 1 or x2 > x1 + w1 - 1 or y2 > y1 + h1 - 1 then
return false
else
return true
end
end
--[[---------------------------------------------------------
- func: loveframes.util.GetCollisions(object, table)
- desc: gets all objects colliding with the mouse
--]]---------------------------------------------------------
function loveframes.util.GetCollisions(object, t)
local x, y = love.mouse.getPosition()
local object = object or loveframes.base
local t = t or {}
-- add the current object if colliding
if object.visible == true then
local col = loveframes.util.BoundingBox(x, object.x, y, object.y, 1, object.width, 1, object.height)
if col == true and object.collide ~= false then
if object.clickbounds then
local clickcol = loveframes.util.BoundingBox(x, object.clickbounds.x, y, object.clickbounds.y, 1, object.clickbounds.width, 1, object.clickbounds.height)
if clickcol == true then
table.insert(t, object)
end
else
table.insert(t, object)
end
end
end
-- check for internals
if object.internals then
for k, v in ipairs(object.internals) do
if v.visible == true then
loveframes.util.GetCollisions(v, t)
end
end
end
-- check for children
if object.children then
for k, v in ipairs(object.children) do
if v.visible == true then
loveframes.util.GetCollisions(v, t)
end
end
end
return t
end
--[[---------------------------------------------------------
- func: loveframes.util.GetAllObjects(object, table)
- desc: gets all active objects
--]]---------------------------------------------------------
function loveframes.util.GetAllObjects(object, t)
local object = object or loveframes.base
local t = t or {}
table.insert(t, object)
if object.internals then
for k, v in ipairs(object.internals) do
loveframes.util.GetAllObjects(v, t)
end
end
if object.children then
for k, v in ipairs(object.children) do
loveframes.util.GetAllObjects(v, t)
end
end
return t
end
--[[---------------------------------------------------------
- func: GetDirContents(directory, table)
- desc: gets the contents of a directory and all of
it's subdirectories
--]]---------------------------------------------------------
function loveframes.util.GetDirContents(dir, t)
local dir = dir
local t = t or {}
local files = love.filesystem.enumerate(dir)
local dirs = {}
-- local function to restore a string to it's original state after being split
local function restore(t)
local s = ""
t[#t] = nil
for k, v in ipairs(t) do
if k ~= #t then
s = s .. v .. "."
else
s = s .. v
end
end
return s
end
for k, v in ipairs(files) do
local isdir = love.filesystem.isDirectory(dir.. "/" ..v)
if isdir == true then
table.insert(dirs, dir.. "/" ..v)
else
local parts = loveframes.util.SplitSring(v, "([.])")
local extension = parts[#parts]
local name = restore(parts)
table.insert(t, {path = dir, fullpath = dir.. "/" ..v, name = name, extension = extension})
end
end
if #dirs > 0 then
for k, v in ipairs(dirs) do
t = loveframes.util.GetDirContents(v, t)
end
end
return t
end
--[[---------------------------------------------------------
- func: Round(val, decimal)
- desc: rounds a number based on the decimal limit
- note: i take no credit for this function
--]]---------------------------------------------------------
function loveframes.util.Round(val, decimal)
if (decimal) then
return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
else
return math.floor(val+0.5)
end
end
--[[---------------------------------------------------------
- func: Split(string, pattern)
- desc: splits a string into a table based on a given pattern
- note: i take no credit for this function
--]]---------------------------------------------------------
function loveframes.util.SplitSring(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
if pat == " " then
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= #str then
cap = cap .. " "
end
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
else
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
end
return t
end
--[[---------------------------------------------------------
- func: Trim(string)
- desc: trims spaces off of the beginning and end of
a string
- note: i take no credit for this function
--]]---------------------------------------------------------
function loveframes.util.TrimString(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
--[[---------------------------------------------------------
- func: RemoveAll()
- desc: removes all gui elements
--]]---------------------------------------------------------
function loveframes.util.RemoveAll()
loveframes.base.children = {}
end