From 2aa8c67a22e26e9b4bdf733904be6fa6cfdab0d1 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 17 May 2022 19:41:42 -0700 Subject: extract a module I want to use `drawing` for locals, so I'll use uppercase the module name just like globals. --- drawing.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 50 +++----------------------------------------------- 2 files changed, 54 insertions(+), 47 deletions(-) create mode 100644 drawing.lua diff --git a/drawing.lua b/drawing.lua new file mode 100644 index 0000000..928325f --- /dev/null +++ b/drawing.lua @@ -0,0 +1,51 @@ +-- primitives for editing drawings +Drawing = {} + +function Drawing.draw(line, y) + local pmx,pmy = love.mouse.getX(), love.mouse.getY() + if pmx < 16+Drawing_width and pmy > line.y and pmy < line.y+pixels(line.h) then + love.graphics.setColor(0.75,0.75,0.75) + love.graphics.rectangle('line', 16,line.y, Drawing_width,pixels(line.h)) + if icon[Current_mode] then + icon[Current_mode](16+Drawing_width-20, line.y+4) + else + icon[Previous_mode](16+Drawing_width-20, line.y+4) + end + + if love.mouse.isDown('1') and love.keyboard.isDown('h') then + draw_help_with_mouse_pressed(line) + return + end + end + + if line.show_help then + draw_help_without_mouse_pressed(line) + return + end + + local mx,my = coord(love.mouse.getX()-16), coord(love.mouse.getY()-line.y) + + for _,shape in ipairs(line.shapes) do + assert(shape) + if on_shape(mx,my, line, shape) then + love.graphics.setColor(1,0,0) + else + love.graphics.setColor(0,0,0) + end + draw_shape(16,line.y, line, shape) + end + for _,p in ipairs(line.points) do + if p.deleted == nil then + if near(p, mx,my) then + love.graphics.setColor(1,0,0) + love.graphics.circle('line', pixels(p.x)+16,pixels(p.y)+line.y, 4) + else + love.graphics.setColor(0,0,0) + love.graphics.circle('fill', pixels(p.x)+16,pixels(p.y)+line.y, 2) + end + end + end + draw_pending_shape(16,line.y, line) +end + +return Drawing diff --git a/main.lua b/main.lua index 688c4e8..ce5b5ed 100644 --- a/main.lua +++ b/main.lua @@ -1,6 +1,7 @@ +local utf8 = require 'utf8' require 'keychord' require 'button' -local utf8 = require 'utf8' +local Drawing = require 'drawing' -- a line is either text or a drawing -- a text is a table with: @@ -116,53 +117,8 @@ function love.draw() love.graphics.print('_', 25, y+6) -- drop the cursor down a bit to account for the increased font size end elseif line.mode == 'drawing' then - -- line drawing y = y+pixels(line.h) - - local pmx,pmy = love.mouse.getX(), love.mouse.getY() - if pmx < 16+Drawing_width and pmy > line.y and pmy < line.y+pixels(line.h) then - love.graphics.setColor(0.75,0.75,0.75) - love.graphics.rectangle('line', 16,line.y, Drawing_width,pixels(line.h)) - if icon[Current_mode] then - icon[Current_mode](16+Drawing_width-20, line.y+4) - else - icon[Previous_mode](16+Drawing_width-20, line.y+4) - end - - if love.mouse.isDown('1') and love.keyboard.isDown('h') then - draw_help_with_mouse_pressed(line) - return - end - end - - if line.show_help then - draw_help_without_mouse_pressed(line) - return - end - - local mx,my = coord(love.mouse.getX()-16), coord(love.mouse.getY()-line.y) - - for _,shape in ipairs(line.shapes) do - assert(shape) - if on_shape(mx,my, line, shape) then - love.graphics.setColor(1,0,0) - else - love.graphics.setColor(0,0,0) - end - draw_shape(16,line.y, line, shape) - end - for _,p in ipairs(line.points) do - if p.deleted == nil then - if near(p, mx,my) then - love.graphics.setColor(1,0,0) - love.graphics.circle('line', pixels(p.x)+16,pixels(p.y)+line.y, 4) - else - love.graphics.setColor(0,0,0) - love.graphics.circle('fill', pixels(p.x)+16,pixels(p.y)+line.y, 2) - end - end - end - draw_pending_shape(16,line.y, line) + Drawing.draw(line, y) else love.graphics.setColor(0,0,0) local text = love.graphics.newText(love.graphics.getFont(), line.data) -- cgit 1.4.1-2-gfad0