forked from HDictus/hump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmentsfile.lua
More file actions
46 lines (42 loc) · 1.18 KB
/
segmentsfile.lua
File metadata and controls
46 lines (42 loc) · 1.18 KB
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
local pack = require("pack-utils")
return function(segments, filename, dirty)
local active = false
local function write()
local text = ''
for _, xs, ys, xgs, ygs in pack.ipairs(segments, 4) do
text = text .. string.format('%d %d %d %d\n', xs, ys, xgs, ygs)
end
local success, message = love.filesystem.write(filename, text)
if not success then error(message) end
end
local function read()
do
local content, message = love.filesystem.read(filename)
if content == nil then error(message) end
local exp = {}
for value in string.gmatch(content, "%d+") do
table.insert(exp, tonumber(value))
end
if #segments % 4 == 0 then
for k, _ in pairs(segments) do segments[k] = nil end
for _, value in ipairs(exp) do
table.insert(segments, value)
end
end
end
dirty()
write()
end
local segmentsfile = {}
function segmentsfile.keypressed(key)
if active then
if key == "r" then read()
elseif key == "w" then write() end
active = false
return true
end
if key == "f" then active = true return true end
return false
end
return segmentsfile
end