I can't be trusted to do anything without a test.

This should fix #5. Please reopen if it doesn't.
This commit is contained in:
Kartik K. Agaram 2022-06-17 22:52:34 -07:00
parent 1ecc3f43e5
commit 26995dd62e
3 changed files with 48 additions and 10 deletions

View File

@ -243,24 +243,39 @@ function Drawing.update()
local mx,my = Drawing.coord(x-Margin_left), Drawing.coord(y-drawing.y)
drawing.pending.target_point.x = mx
drawing.pending.target_point.y = my
Drawing.relax_constraints(drawing, drawing.pending.target_point_index)
end
end
elseif Current_drawing_mode == 'move' then
if Drawing.in_drawing(drawing, x, y) then
local mx,my = Drawing.coord(x-Margin_left), Drawing.coord(y-drawing.y)
if drawing.mode == 'manhattan' then
drawing.mode = 'line'
elseif drawing.mode == 'rectangle' or drawing.mode == 'square' then
drawing.mode = 'polygon'
end
drawing.pending.target_point.x = mx
drawing.pending.target_point.y = my
Drawing.relax_constraints(drawing, drawing.pending.target_point_index)
end
else
-- do nothing
end
end
function Drawing.relax_constraints(drawing, p)
for _,shape in ipairs(drawing.shapes) do
if shape.mode == 'manhattan' then
if shape.p1 == p then
shape.mode = 'line'
elseif shape.p2 == p then
shape.mode = 'line'
end
elseif shape.mode == 'rectangle' or shape.mode == 'square' then
for _,v in ipairs(shape.vertices) do
if v == p then
shape.mode = 'polygon'
end
end
end
end
end
function Drawing.mouse_released(x,y, button)
if Current_drawing_mode == 'move' then
Current_drawing_mode = Previous_drawing_mode
@ -474,13 +489,13 @@ function Drawing.keychord_pressed(chord)
end
drawing.pending.mode = 'circle'
elseif chord == 'C-u' and not App.mouse_down(1) then
local drawing_index,drawing,_,p = Drawing.select_point_at_mouse()
local drawing_index,drawing,i,p = Drawing.select_point_at_mouse()
if drawing then
if Previous_drawing_mode == nil then
Previous_drawing_mode = Current_drawing_mode
end
Current_drawing_mode = 'move'
drawing.pending = {mode=Current_drawing_mode, target_point=p}
drawing.pending = {mode=Current_drawing_mode, target_point=p, target_point_index=i}
Lines.current_drawing_index = drawing_index
Lines.current_drawing = drawing
end

View File

@ -421,7 +421,6 @@ function test_move_point()
check_eq(p1.y, 6, 'F - test_move_point/baseline/p1:y')
check_eq(p2.x, 35, 'F - test_move_point/baseline/p2:x')
check_eq(p2.y, 36, 'F - test_move_point/baseline/p2:y')
check_nil(p2.name, 'F - test_move_point/baseline/p2:name')
-- wait until save
App.wait_fake_time(3.1)
App.update(0)
@ -458,6 +457,32 @@ function test_move_point()
check_eq(p2.y, 44, 'F - test_move_point/save/y')
end
function test_move_point_on_manhattan_line()
io.write('\ntest_move_point_on_manhattan_line')
-- create a drawing with a manhattan line
Filename = 'foo'
App.screen.init{width=Margin_left+300, height=300}
Lines = load_array{'```lines', '```', ''}
Line_width = 256 -- drawing coordinates 1:1 with pixels
Current_drawing_mode = 'manhattan'
App.draw()
App.run_after_mouse_press(Margin_left+5, Margin_top+Drawing_padding_top+6, 1)
App.run_after_mouse_release(Margin_left+35, Margin_top+Drawing_padding_top+46, 1)
local drawing = Lines[1]
check_eq(#drawing.shapes, 1, 'F - test_move_point_on_manhattan_line/baseline/#shapes')
check_eq(#drawing.points, 2, 'F - test_move_point_on_manhattan_line/baseline/#points')
check_eq(drawing.shapes[1].mode, 'manhattan', 'F - test_move_point_on_manhattan_line/baseline/shape:1')
App.draw()
-- enter 'move' mode
App.run_after_keychord('C-u')
check_eq(Current_drawing_mode, 'move', 'F - test_move_point_on_manhattan_line/mode:1')
-- move point
App.mouse_move(Margin_left+26, Margin_top+Drawing_padding_top+44)
App.update(0.05)
-- line is no longer manhattan
check_eq(drawing.shapes[1].mode, 'line', 'F - test_move_point_on_manhattan_line/baseline/shape:1')
end
function test_delete_lines_at_point()
io.write('\ntest_delete_lines_at_point')
-- create a drawing with two lines connected at a point

View File

@ -22,8 +22,6 @@ function geom.on_shape(x,y, drawing, shape)
x1,x2 = x2,x1
end
return x >= x1*0.95 and x <= x2*1.05
else
assert(false)
end
elseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' then
return geom.on_polygon(x,y, drawing, shape)