about summary refs log tree commit diff stats
path: root/main.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-15 14:00:49 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-15 14:00:49 -0700
commit64072bfdf4f46b1a2bb82fe043d9f0ba06fe79d6 (patch)
tree550dac694b2146c392156eb4693218ae2c3ba756 /main.lua
parent87a3753796bec4818aca390c215ed0a2821cff1c (diff)
downloadtext.love-64072bfdf4f46b1a2bb82fe043d9f0ba06fe79d6.tar.gz
starting to load/save
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua69
1 files changed, 65 insertions, 4 deletions
diff --git a/main.lua b/main.lua
index 61a2777..2f11e75 100644
--- a/main.lua
+++ b/main.lua
@@ -49,7 +49,13 @@ end
 
 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, '')
   love.window.setMode(0, 0)  -- maximize
   screenw, screenh, screenflags = love.window.getMode()
@@ -159,9 +165,7 @@ function love.mousereleased(x,y, button)
   if current_mode == 'move' then
     current_mode = previous_mode
     previous_mode = nil
-    return
-  end
-  if lines.current then
+  elseif lines.current then
     if lines.current.pending then
       if lines.current.pending.mode == 'freehand' then
         -- the last point added during update is good enough
@@ -214,6 +218,7 @@ function love.mousereleased(x,y, button)
       lines.current = nil
     end
   end
+  save_to_disk(lines, filename)
 end
 
 function propagate_to_drawings(x,y, button)
@@ -752,3 +757,59 @@ function math.angle(x1,y1, x2,y2)
 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