eliminate squirrely state State.current_drawing_index

Since we now use State.cursor.line for this, the cursor can now move on
a drawing (when we start a drawing operation).

Lots of places still crash when they encounter a drawing cursor, though.
Like arrow keys or other sorts of navigation.
This commit is contained in:
Kartik K. Agaram 2024-06-22 17:34:53 -07:00
parent a4ca53d571
commit 29c83c6175
3 changed files with 25 additions and 15 deletions

View File

@ -7,9 +7,16 @@ function Drawing.draw(State, line_index, y)
local line = State.lines[line_index]
local pmx,pmy = love.mouse.getPosition()
local starty = Text.starty(State, line_index)
if pmx < State.right and pmy > starty and pmy < starty+Drawing.pixels(line.h, State.width) then
local height = Drawing.pixels(line.h, State.width)
if line_index == State.cursor.line then
App.color(Cursor_color)
love.graphics.rectangle('line', State.left, starty, State.width, height)
elseif geom.in_rect(pmx,pmy, State.left, starty, State.width, height) then
App.color(Icon_color)
love.graphics.rectangle('line', State.left, starty, State.width, height)
end
if geom.in_rect(pmx,pmy, State.left, starty, State.width, height) then
App.color(Icon_color)
love.graphics.rectangle('line', State.left,starty, State.width,Drawing.pixels(line.h, State.width))
if icon[State.current_drawing_mode] then
icon[State.current_drawing_mode](State.right-22, starty+4)
else
@ -208,7 +215,7 @@ function Drawing.draw_pending_shape(drawing, top, left,right)
end
function Drawing.in_current_drawing(State, x,y, left,right)
return Drawing.in_drawing(State, State.lines.current_drawing_index, x,y, left,right)
return Drawing.in_drawing(State, State.cursor.line, x,y, left,right)
end
function Drawing.in_drawing(State, line_index, x,y, left,right)
@ -249,7 +256,7 @@ end
function Drawing.update(State)
if State.lines.current_drawing == nil then return end
local drawing = State.lines.current_drawing
local starty = Text.starty(State, State.lines.current_drawing_index)
local starty = Text.starty(State, State.cursor.line)
if starty == nil then
-- some event cleared starty just this frame
-- draw in this frame will soon set starty
@ -309,7 +316,7 @@ function Drawing.mouse_release(State, x,y, mouse_button)
end
elseif State.lines.current_drawing then
local drawing = State.lines.current_drawing
local starty = Text.starty(State, State.lines.current_drawing_index)
local starty = Text.starty(State, State.cursor.line)
if drawing.pending then
if drawing.pending.mode == nil then
-- nothing pending
@ -525,7 +532,7 @@ function Drawing.keychord_press(State, chord)
end
State.current_drawing_mode = 'move'
drawing.pending = {mode=State.current_drawing_mode, target_point=p, target_point_index=i}
State.lines.current_drawing_index = drawing_index
State.cursor = {mode='drawing', line=drawing_index, yoff=0}
State.lines.current_drawing = drawing
end
elseif chord == 'C-n' and not love.mouse.isDown(1) then
@ -538,7 +545,7 @@ function Drawing.keychord_press(State, chord)
State.current_drawing_mode = 'name'
p.name = ''
drawing.pending = {mode=State.current_drawing_mode, target_point=point_index}
State.lines.current_drawing_index = drawing_index
State.cursor = {mode='drawing', line=drawing_index, yoff=0}
State.lines.current_drawing = drawing
end
elseif chord == 'C-d' and not love.mouse.isDown(1) then

View File

@ -247,7 +247,7 @@ function edit.mouse_press(State, x,y, mouse_button)
end
elseif line.mode == 'drawing' then
if Drawing.in_drawing(State, line_index, x, y, State.left,State.right) then
State.lines.current_drawing_index = line_index
State.cursor.line = line_index
State.lines.current_drawing = line
State.drawing_before = snapshot(State, line_index)
Drawing.mouse_press(State, line_index, x,y, mouse_button)
@ -271,7 +271,7 @@ function edit.mouse_release(State, x,y, mouse_button)
Drawing.mouse_release(State, x,y, mouse_button)
schedule_save(State)
if State.drawing_before then
record_undo_event(State, {before=State.drawing_before, after=snapshot(State, State.lines.current_drawing_index)})
record_undo_event(State, {before=State.drawing_before, after=snapshot(State, State.cursor.line)})
State.drawing_before = nil
end
else
@ -327,7 +327,6 @@ function edit.mouse_wheel_move(State, dx,dy)
end
elseif dy < 0 then
State.cursor = Text.screen_bottom1(State)
edit.put_cursor_on_next_text_line(State)
for i=1,math.floor(-dy) do
Text.down(State)
end
@ -340,11 +339,11 @@ function edit.text_input(State, t)
State.search_term = State.search_term..t
Text.search_next(State)
elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
local before = snapshot(State, State.lines.current_drawing_index)
local before = snapshot(State, State.cursor.line)
local drawing = State.lines.current_drawing
local p = drawing.points[drawing.pending.target_point]
p.name = p.name..t
record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.line)})
else
local drawing_index, drawing = Drawing.current_drawing(State)
if drawing_index == nil then
@ -486,19 +485,19 @@ function edit.keychord_press(State, chord, key)
State.current_drawing_mode = State.previous_drawing_mode
State.previous_drawing_mode = nil
else
local before = snapshot(State, State.lines.current_drawing_index)
local before = snapshot(State, State.cursor.line)
local drawing = State.lines.current_drawing
local p = drawing.points[drawing.pending.target_point]
if chord == 'escape' then
p.name = nil
record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.line)})
elseif chord == 'backspace' then
local len = utf8.len(p.name)
if len > 0 then
local byte_offset = Text.offset(p.name, len-1)
if len == 1 then byte_offset = 0 end
p.name = string.sub(p.name, 1, byte_offset)
record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.line)})
end
end
end

View File

@ -1,5 +1,9 @@
geom = {}
function geom.in_rect(x,y, top,left,width,height)
return x >= left and x <= left+width and y >= top and y <= top+height
end
function geom.on_shape(x,y, drawing, shape)
if shape.mode == 'freehand' then
return geom.on_freehand(x,y, drawing, shape)