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-10 21:17:50 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-11 13:01:13 -0700
commit475bbd70efec08f4279f42b044dfe3bb9f35d63e (patch)
tree82434df44d4754cc3235d41414fff7a4a282d78f /main.lua
parent3019442c02bd301dd9ce61f76c2a007aed202577 (diff)
downloadtext.love-475bbd70efec08f4279f42b044dfe3bb9f35d63e.tar.gz
intermingle freehand line drawings with text
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua76
1 files changed, 69 insertions, 7 deletions
diff --git a/main.lua b/main.lua
index c3aa8f4..6290441 100644
--- a/main.lua
+++ b/main.lua
@@ -1,4 +1,5 @@
 require 'keychord'
+require 'button'
 local utf8 = require 'utf8'
 
 lines = {}
@@ -13,18 +14,56 @@ function love.load()
 end
 
 function love.draw()
+  button_handlers = {}
   love.graphics.setColor(1, 1, 1)
   love.graphics.rectangle('fill', 1, 1, width-1, height-1)
-  love.graphics.setColor(1, 1, 0)
-  love.graphics.rectangle('fill', 1, 1, 400, 10*12)
   love.graphics.setColor(0, 0, 0)
   local text
+  local y = 0
   for i, line in ipairs(lines) do
+    y = y+25
     text = love.graphics.newText(love.graphics.getFont(), line)
-    love.graphics.draw(text, 12, i*15)
+    if line == '' then
+      button('draw', {x=4,y=y+4, w=12,h=12, color={1,1,0},
+        icon = function(x, y)
+                 love.graphics.setColor(0.7,0.7,0.7)
+                 love.graphics.rectangle('line', x,y, 12,12)
+                 love.graphics.line(4,y+6, 16,y+6)
+                 love.graphics.line(10,y, 10,y+12)
+                 love.graphics.setColor(0, 0, 0)
+               end,
+        onpress1 = function()
+                     table.insert(lines, i, {y=y, w=400, h=200, pending={}, shapes={}})
+                   end})
+    elseif type(line) == 'table' then
+      -- line drawing
+      love.graphics.setColor(0,0,0)
+      line.y = y
+      love.graphics.rectangle('line', 12,y, line.w,line.h)
+      y = y+line.h
+
+      for _,shape in ipairs(line.shapes) do
+        prev = nil
+        for _,point in ipairs(shape) do
+          if prev then
+            love.graphics.line(prev.x,prev.y, point.x,point.y)
+          end
+          prev = point
+        end
+      end
+      prev = nil
+      for _,point in ipairs(line.pending) do
+        if prev then
+          love.graphics.line(prev.x,prev.y, point.x,point.y)
+        end
+        prev = point
+      end
+    else
+      love.graphics.draw(text, 25,y, 0, 1.5)
+    end
   end
   -- cursor
-  love.graphics.print('_', 12+text:getWidth(), #lines*15)
+  love.graphics.print('_', 25+text:getWidth()*1.5, y)
 
   if exec_payload then
     call_gather(exec_payload)
@@ -32,6 +71,31 @@ function love.draw()
 end
 
 function love.update(dt)
+  if love.mouse.isDown('1') then
+    for i, line in ipairs(lines) do
+      if type(line) == 'table' then
+        local y = love.mouse.getY()
+        if y >= line.y and y < line.y + line.h then
+          lines.current = line
+          process_drag(line,love.mouse.getX(),love.mouse.getY())
+        end
+      end
+    end
+  end
+end
+
+function process_drag(drawing, x,y)
+  table.insert(drawing.pending, {x=x, y=y})
+end
+
+function love.mousereleased(x,y, button)
+  if lines.current then
+    if lines.current.pending then
+      table.insert(lines.current.shapes, lines.current.pending)
+      lines.current.pending = {}
+      lines.current = nil
+    end
+  end
 end
 
 function love.textinput(t)
@@ -62,9 +126,7 @@ function love.keyreleased(key, scancode)
 end
 
 function love.mousepressed(x, y, button)
-end
-
-function love.mousereleased(x, y, button)
+  propagate_to_button_handers(x, y, button)
 end
 
 function eval(buf)