about summary refs log tree commit diff stats
path: root/file.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-22 23:17:06 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-22 23:17:06 -0700
commit46d4c4de10b31a82664364364c911e1d8a08c0e5 (patch)
tree0b29abc7e7a8ae1aee396928da96e3e6d533ed51 /file.lua
parent8aa72be226e13b1af7ab09a7dc1eec4a95a82f82 (diff)
downloadlines.love-46d4c4de10b31a82664364364c911e1d8a08c0e5.tar.gz
first test!
Diffstat (limited to 'file.lua')
-rw-r--r--file.lua56
1 files changed, 56 insertions, 0 deletions
diff --git a/file.lua b/file.lua
index 1087f55..0dcc369 100644
--- a/file.lua
+++ b/file.lua
@@ -102,3 +102,59 @@ function store_drawing(outfile, drawing)
   end
   outfile:write('```\n')
 end
+
+-- for tests
+function load_array(a)
+  local result = {}
+  local next_line = ipairs(a)
+  local i,line = 0, ''
+  while true do
+    i,line = next_line(a, i)
+    if i == nil then break end
+    if line == '```lines' then  -- inflexible with whitespace since these files are always autogenerated
+      table.insert(result, load_drawing_from_array(next_line, a, i))
+    else
+      table.insert(result, {mode='text', data=line})
+    end
+  end
+  if #result == 0 then
+    table.insert(result, {mode='text', data=''})
+  end
+  return result
+end
+
+function load_drawing_from_array(iter, a, i)
+  local drawing = {mode='drawing', h=256/2, points={}, shapes={}, pending={}}
+  local line
+  while true do
+    i, line = iter(a, i)
+    assert(i)
+    if line == '```' then break end
+    local shape = json.decode(line)
+    if shape.mode == 'freehand' then
+      -- no changes needed
+    elseif shape.mode == 'line' or shape.mode == 'manhattan' then
+      local name = shape.p1.name
+      shape.p1 = Drawing.insert_point(drawing.points, shape.p1.x, shape.p1.y)
+      drawing.points[shape.p1].name = name
+      name = shape.p2.name
+      shape.p2 = Drawing.insert_point(drawing.points, shape.p2.x, shape.p2.y)
+      drawing.points[shape.p2].name = name
+    elseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' then
+      for i,p in ipairs(shape.vertices) do
+        local name = p.name
+        shape.vertices[i] = Drawing.insert_point(drawing.points, p.x,p.y)
+        drawing.points[shape.vertices[i]].name = name
+      end
+    elseif shape.mode == 'circle' or shape.mode == 'arc' then
+      local name = shape.center.name
+      shape.center = Drawing.insert_point(drawing.points, shape.center.x,shape.center.y)
+      drawing.point[shape.center].name = name
+    else
+      print(shape.mode)
+      assert(false)
+    end
+    table.insert(drawing.shapes, shape)
+  end
+  return drawing
+end