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-16 15:26:22 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-16 15:26:22 -0700
commit8f06b74eabf6b8aaff5552287b93a277f5997858 (patch)
treecdd84c674a0f90d100c1f1eec8c2aff9a3e33575 /main.lua
parent34f9670215c4504a218864679b84adfe31f60a54 (diff)
downloadlines.love-8f06b74eabf6b8aaff5552287b93a277f5997858.tar.gz
to open a file without a terminal, drag it on!
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua23
1 files changed, 18 insertions, 5 deletions
diff --git a/main.lua b/main.lua
index e062187..d31ce5b 100644
--- a/main.lua
+++ b/main.lua
@@ -69,6 +69,12 @@ function love.load(arg)
   end
 end
 
+function love.filedropped(file)
+  filename = file:getFilename()
+  file:open('r')
+  lines = load_from_file(file)
+end
+
 function love.draw()
   button_handlers = {}
   love.graphics.setColor(1, 1, 1)
@@ -818,14 +824,21 @@ 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)
+  local result = load_from_file(io.open(filename))
+  infile:close()
+  return result
+end
+
+function load_from_file(infile)
+  local result = {}
   if infile then
+    local infile_next_line = infile:lines()  -- works with both Lua files and LÖVE Files (https://www.love2d.org/wiki/File)
     while true do
-      local line = infile:read()
+      local line = infile_next_line()
       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))
+        table.insert(result, load_drawing(infile_next_line))
       else
         table.insert(result, line)
       end
@@ -847,10 +860,10 @@ function save_to_disk(lines, filename)
 end
 
 json = require 'json'
-function load_drawing(infile)
+function load_drawing(infile_next_line)
   local drawing = {h=256/2, points={}, shapes={}, pending={}}
   while true do
-    local line = infile:read()
+    local line = infile_next_line()
     assert(line)
     if line == '```' then break end
     local shape = json.decode(line)