intermingle freehand line drawings with text

This commit is contained in:
Kartik K. Agaram 2022-05-10 21:17:50 -07:00
parent 3019442c02
commit 475bbd70ef
2 changed files with 87 additions and 7 deletions

18
button.lua Normal file
View File

@ -0,0 +1,18 @@
button_handlers = {}
-- draw button and queue up event handlers
function button(name, params)
love.graphics.setColor(params.color[1], params.color[2], params.color[3])
love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5)
if params.icon then params.icon(params.x, params.y) end
table.insert(button_handlers, params)
end
-- process button event handlers
function propagate_to_button_handers(x, y, button)
for _,ev in ipairs(button_handlers) do
if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
if ev.onpress1 and button == 1 then ev.onpress1() end
end
end
end

View File

@ -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)