starting to load/save
This commit is contained in:
parent
87a3753796
commit
64072bfdf4
69
main.lua
69
main.lua
|
@ -49,7 +49,13 @@ end
|
||||||
|
|
||||||
exec_payload = nil
|
exec_payload = nil
|
||||||
|
|
||||||
function love.load()
|
filename = nil
|
||||||
|
|
||||||
|
function love.load(arg)
|
||||||
|
if #arg > 0 then
|
||||||
|
filename = arg[1]
|
||||||
|
lines = load_from_disk(filename)
|
||||||
|
end
|
||||||
table.insert(lines, '')
|
table.insert(lines, '')
|
||||||
love.window.setMode(0, 0) -- maximize
|
love.window.setMode(0, 0) -- maximize
|
||||||
screenw, screenh, screenflags = love.window.getMode()
|
screenw, screenh, screenflags = love.window.getMode()
|
||||||
|
@ -159,9 +165,7 @@ function love.mousereleased(x,y, button)
|
||||||
if current_mode == 'move' then
|
if current_mode == 'move' then
|
||||||
current_mode = previous_mode
|
current_mode = previous_mode
|
||||||
previous_mode = nil
|
previous_mode = nil
|
||||||
return
|
elseif lines.current then
|
||||||
end
|
|
||||||
if lines.current then
|
|
||||||
if lines.current.pending then
|
if lines.current.pending then
|
||||||
if lines.current.pending.mode == 'freehand' then
|
if lines.current.pending.mode == 'freehand' then
|
||||||
-- the last point added during update is good enough
|
-- the last point added during update is good enough
|
||||||
|
@ -214,6 +218,7 @@ function love.mousereleased(x,y, button)
|
||||||
lines.current = nil
|
lines.current = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
save_to_disk(lines, filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
function propagate_to_drawings(x,y, button)
|
function propagate_to_drawings(x,y, button)
|
||||||
|
@ -752,3 +757,59 @@ function math.angle(x1,y1, x2,y2)
|
||||||
end
|
end
|
||||||
|
|
||||||
function math.dist(x1,y1, x2,y2) return ((x2-x1)^2+(y2-y1)^2)^0.5 end
|
function math.dist(x1,y1, x2,y2) return ((x2-x1)^2+(y2-y1)^2)^0.5 end
|
||||||
|
|
||||||
|
function load_from_disk(filename)
|
||||||
|
local result = {}
|
||||||
|
local infile = io.open(filename)
|
||||||
|
if infile then
|
||||||
|
while true do
|
||||||
|
local line = infile:read()
|
||||||
|
if line == nil then break end
|
||||||
|
if line == '```lines' then -- inflexible with whitespace since these files are always autogenerated
|
||||||
|
table.insert(result, load_drawing(infile))
|
||||||
|
else
|
||||||
|
table.insert(result, line)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function save_to_disk(lines, filename)
|
||||||
|
local outfile = io.open(filename, 'w')
|
||||||
|
for _,line in ipairs(lines) do
|
||||||
|
if type(line) == 'table' then
|
||||||
|
store_drawing(outfile, line)
|
||||||
|
else
|
||||||
|
outfile:write(line..'\n')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
json = require 'json'
|
||||||
|
function load_drawing(infile)
|
||||||
|
local drawing = {h=256/2, points={}, shapes={}, pending={}}
|
||||||
|
while true do
|
||||||
|
local line = infile:read()
|
||||||
|
assert(line)
|
||||||
|
if line == '```' then break end
|
||||||
|
local shape = json.decode(line)
|
||||||
|
if shape.mode == 'line' then
|
||||||
|
shape.p1 = insert_point(drawing.points, shape.p1.x, shape.p1.y)
|
||||||
|
shape.p2 = insert_point(drawing.points, shape.p2.x, shape.p2.y)
|
||||||
|
end
|
||||||
|
table.insert(drawing.shapes, shape)
|
||||||
|
end
|
||||||
|
return drawing
|
||||||
|
end
|
||||||
|
|
||||||
|
function store_drawing(outfile, drawing)
|
||||||
|
outfile:write('```lines\n')
|
||||||
|
for _,shape in ipairs(drawing.shapes) do
|
||||||
|
if shape.mode == 'line' then
|
||||||
|
local line = json.encode({mode=shape.mode, p1=drawing.points[shape.p1], p2=drawing.points[shape.p2]})
|
||||||
|
outfile:write(line..'\n')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
outfile:write('```\n')
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue