split mouse_pressed events between Text and Drawing

This commit is contained in:
Kartik K. Agaram 2022-05-17 22:53:09 -07:00
parent 222a11a8dd
commit 92bd6839c7
3 changed files with 44 additions and 27 deletions

View File

@ -49,6 +49,26 @@ function Drawing.draw(line, y)
Drawing.draw_pending_shape(16,line.y, line) Drawing.draw_pending_shape(16,line.y, line)
end end
function Drawing.in_drawing(drawing, x,y)
return y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= 16 and x < 16+Drawing_width
end
function Drawing.mouse_pressed(drawing, x,y, button)
if Current_drawing_mode == 'freehand' then
drawing.pending = {mode=Current_drawing_mode, points={{x=Drawing.coord(x-16), y=Drawing.coord(y-drawing.y)}}}
elseif Current_drawing_mode == 'line' or Current_drawing_mode == 'manhattan' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-16), Drawing.coord(y-drawing.y))
drawing.pending = {mode=Current_drawing_mode, p1=j}
elseif Current_drawing_mode == 'polygon' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-16), Drawing.coord(y-drawing.y))
drawing.pending = {mode=Current_drawing_mode, vertices={j}}
elseif Current_drawing_mode == 'circle' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-16), Drawing.coord(y-drawing.y))
drawing.pending = {mode=Current_drawing_mode, center=j}
end
Lines.current = drawing
end
function Drawing.keychord_pressed(chord) function Drawing.keychord_pressed(chord)
if chord == 'C-=' then if chord == 'C-=' then
Drawing_width = Drawing_width/Zoom Drawing_width = Drawing_width/Zoom

View File

@ -158,28 +158,12 @@ function love.mousepressed(x,y, button)
for line_index,line in ipairs(Lines) do for line_index,line in ipairs(Lines) do
if line.mode == 'text' then if line.mode == 'text' then
-- move cursor if Text.in_line(line, x,y) then
if x >= 16 and y >= line.y and y < line.y+15*Zoom then Text.move_cursor(line_index, line, x)
Cursor_line = line_index
Cursor_pos = Text.nearest_cursor_pos(line.data, x, 1)
end end
elseif line.mode == 'drawing' then elseif line.mode == 'drawing' then
local drawing = line if Drawing.in_drawing(line, x, y) then
local x, y = love.mouse.getX(), love.mouse.getY() Drawing.mouse_pressed(line, x,y, button)
if y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= 16 and x < 16+Drawing_width then
if Current_drawing_mode == 'freehand' then
drawing.pending = {mode=Current_drawing_mode, points={{x=Drawing.coord(x-16), y=Drawing.coord(y-drawing.y)}}}
elseif Current_drawing_mode == 'line' or Current_drawing_mode == 'manhattan' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-16), Drawing.coord(y-drawing.y))
drawing.pending = {mode=Current_drawing_mode, p1=j}
elseif Current_drawing_mode == 'polygon' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-16), Drawing.coord(y-drawing.y))
drawing.pending = {mode=Current_drawing_mode, vertices={j}}
elseif Current_drawing_mode == 'circle' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-16), Drawing.coord(y-drawing.y))
drawing.pending = {mode=Current_drawing_mode, center=j}
end
Lines.current = drawing
end end
end end
end end

View File

@ -143,6 +143,15 @@ function Text.keychord_pressed(chord)
end end
end end
function Text.in_line(line, x,y)
return x >= 16 and y >= line.y and y < line.y+15*Zoom
end
function Text.move_cursor(line_index, line, x, hint)
Cursor_line = line_index
Cursor_pos = Text.nearest_cursor_pos(line.data, x)
end
function Text.nearest_cursor_pos(line, x, hint) function Text.nearest_cursor_pos(line, x, hint)
if x == 0 then if x == 0 then
return 1 return 1
@ -151,15 +160,19 @@ function Text.nearest_cursor_pos(line, x, hint)
if x > max_x then if x > max_x then
return #line+1 return #line+1
end end
local currx = Text.cursor_x(line, hint) if hint then
if currx > x-2 and currx < x+2 then local currx = Text.cursor_x(line, hint)
return hint if currx > x-2 and currx < x+2 then
return hint
end
end end
local left, right = 1, #line+1 local left, right = 1, #line+1
if currx > x then if hint then
right = hint if currx > x then
else right = hint
left = hint else
left = hint
end
end end
while left < right-1 do while left < right-1 do
local curr = math.floor((left+right)/2) local curr = math.floor((left+right)/2)