left/right margin -> left/right coordinates

Editor state initialization now depends on window dimensions, so we have
to more carefully orchestrate startup.
This commit is contained in:
Kartik K. Agaram 2022-07-12 18:29:00 -07:00
parent 1ede1c3c6d
commit 3b36093553
9 changed files with 486 additions and 369 deletions

View File

@ -6,13 +6,13 @@ require 'drawing_tests'
-- into 256 parts.
function Drawing.draw(State, line)
local pmx,pmy = App.mouse_x(), App.mouse_y()
if pmx < App.screen.width-State.margin_right and pmy > line.y and pmy < line.y+Drawing.pixels(line.h) then
if pmx < State.right and pmy > line.y and pmy < line.y+Drawing.pixels(line.h) then
App.color(Icon_color)
love.graphics.rectangle('line', State.margin_left,line.y, App.screen.width-State.margin_width,Drawing.pixels(line.h))
love.graphics.rectangle('line', State.left,line.y, State.width,Drawing.pixels(line.h))
if icon[State.current_drawing_mode] then
icon[State.current_drawing_mode](App.screen.width-State.margin_right-22, line.y+4)
icon[State.current_drawing_mode](State.right-22, line.y+4)
else
icon[State.previous_drawing_mode](App.screen.width-State.margin_right-22, line.y+4)
icon[State.previous_drawing_mode](State.right-22, line.y+4)
end
if App.mouse_down(1) and love.keyboard.isDown('h') then
@ -26,7 +26,7 @@ function Drawing.draw(State, line)
return
end
local mx,my = Drawing.coord(pmx-State.margin_left), Drawing.coord(pmy-line.y)
local mx,my = Drawing.coord(pmx-State.left), Drawing.coord(pmy-line.y)
for _,shape in ipairs(line.shapes) do
assert(shape)
@ -35,20 +35,20 @@ function Drawing.draw(State, line)
else
App.color(Stroke_color)
end
Drawing.draw_shape(State.margin_left,line.y, line, shape)
Drawing.draw_shape(State.left,line.y, line, shape)
end
for i,p in ipairs(line.points) do
if p.deleted == nil then
if Drawing.near(p, mx,my) then
App.color(Focus_stroke_color)
love.graphics.circle('line', Drawing.pixels(p.x)+State.margin_left,Drawing.pixels(p.y)+line.y, 4)
love.graphics.circle('line', Drawing.pixels(p.x)+State.left,Drawing.pixels(p.y)+line.y, 4)
else
App.color(Stroke_color)
love.graphics.circle('fill', Drawing.pixels(p.x)+State.margin_left,Drawing.pixels(p.y)+line.y, 2)
love.graphics.circle('fill', Drawing.pixels(p.x)+State.left,Drawing.pixels(p.y)+line.y, 2)
end
if p.name then
-- TODO: clip
local x,y = Drawing.pixels(p.x)+State.margin_left+5, Drawing.pixels(p.y)+line.y+5
local x,y = Drawing.pixels(p.x)+State.left+5, Drawing.pixels(p.y)+line.y+5
love.graphics.print(p.name, x,y)
if State.current_drawing_mode == 'name' and i == line.pending.target_point then
-- create a faint red box for the name
@ -66,7 +66,7 @@ function Drawing.draw(State, line)
end
end
App.color(Current_stroke_color)
Drawing.draw_pending_shape(State.margin_left,line.y, line)
Drawing.draw_pending_shape(State.left,line.y, line)
end
function Drawing.draw_shape(left,top, drawing, shape)
@ -204,20 +204,20 @@ end
function Drawing.in_drawing(drawing, x,y)
if drawing.y == nil then return false end -- outside current page
return y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= Editor_state.margin_left and x < App.screen.width-Editor_state.margin_right
return y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= Editor_state.left and x < Editor_state.right
end
function Drawing.mouse_pressed(State, drawing, x,y, button)
if State.current_drawing_mode == 'freehand' then
drawing.pending = {mode=State.current_drawing_mode, points={{x=Drawing.coord(x-State.margin_left), y=Drawing.coord(y-drawing.y)}}}
drawing.pending = {mode=State.current_drawing_mode, points={{x=Drawing.coord(x-State.left), y=Drawing.coord(y-drawing.y)}}}
elseif State.current_drawing_mode == 'line' or State.current_drawing_mode == 'manhattan' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y))
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-State.left), Drawing.coord(y-drawing.y))
drawing.pending = {mode=State.current_drawing_mode, p1=j}
elseif State.current_drawing_mode == 'polygon' or State.current_drawing_mode == 'rectangle' or State.current_drawing_mode == 'square' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y))
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-State.left), Drawing.coord(y-drawing.y))
drawing.pending = {mode=State.current_drawing_mode, vertices={j}}
elseif State.current_drawing_mode == 'circle' then
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y))
local j = Drawing.insert_point(drawing.points, Drawing.coord(x-State.left), Drawing.coord(y-drawing.y))
drawing.pending = {mode=State.current_drawing_mode, center=j}
elseif State.current_drawing_mode == 'move' then
-- all the action is in mouse_released
@ -238,9 +238,9 @@ function Drawing.update(State)
if App.mouse_down(1) then
if Drawing.in_drawing(drawing, x,y) then
if drawing.pending.mode == 'freehand' then
table.insert(drawing.pending.points, {x=Drawing.coord(App.mouse_x()-State.margin_left), y=Drawing.coord(App.mouse_y()-drawing.y)})
table.insert(drawing.pending.points, {x=Drawing.coord(App.mouse_x()-State.left), y=Drawing.coord(App.mouse_y()-drawing.y)})
elseif drawing.pending.mode == 'move' then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.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)
@ -248,7 +248,7 @@ function Drawing.update(State)
end
elseif State.current_drawing_mode == 'move' then
if Drawing.in_drawing(drawing, x, y) then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.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)
@ -294,14 +294,14 @@ function Drawing.mouse_released(State, x,y, button)
Drawing.smoothen(drawing.pending)
table.insert(drawing.shapes, drawing.pending)
elseif drawing.pending.mode == 'line' then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
drawing.pending.p2 = Drawing.insert_point(drawing.points, mx,my)
table.insert(drawing.shapes, drawing.pending)
end
elseif drawing.pending.mode == 'manhattan' then
local p1 = drawing.points[drawing.pending.p1]
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
if math.abs(mx-p1.x) > math.abs(my-p1.y) then
drawing.pending.p2 = Drawing.insert_point(drawing.points, mx, p1.y)
@ -309,11 +309,11 @@ function Drawing.mouse_released(State, x,y, button)
drawing.pending.p2 = Drawing.insert_point(drawing.points, p1.x, my)
end
local p2 = drawing.points[drawing.pending.p2]
App.mouse_move(State.margin_left+Drawing.pixels(p2.x), drawing.y+Drawing.pixels(p2.y))
App.mouse_move(State.left+Drawing.pixels(p2.x), drawing.y+Drawing.pixels(p2.y))
table.insert(drawing.shapes, drawing.pending)
end
elseif drawing.pending.mode == 'polygon' then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
table.insert(drawing.pending.vertices, Drawing.insert_point(drawing.points, mx,my))
table.insert(drawing.shapes, drawing.pending)
@ -321,7 +321,7 @@ function Drawing.mouse_released(State, x,y, button)
elseif drawing.pending.mode == 'rectangle' then
assert(#drawing.pending.vertices <= 2)
if #drawing.pending.vertices == 2 then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
local first = drawing.points[drawing.pending.vertices[1]]
local second = drawing.points[drawing.pending.vertices[2]]
@ -336,7 +336,7 @@ function Drawing.mouse_released(State, x,y, button)
elseif drawing.pending.mode == 'square' then
assert(#drawing.pending.vertices <= 2)
if #drawing.pending.vertices == 2 then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
local first = drawing.points[drawing.pending.vertices[1]]
local second = drawing.points[drawing.pending.vertices[2]]
@ -347,14 +347,14 @@ function Drawing.mouse_released(State, x,y, button)
end
end
elseif drawing.pending.mode == 'circle' then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
local center = drawing.points[drawing.pending.center]
drawing.pending.radius = geom.dist(center.x,center.y, mx,my)
table.insert(drawing.shapes, drawing.pending)
end
elseif drawing.pending.mode == 'arc' then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
local center = drawing.points[drawing.pending.center]
drawing.pending.end_angle = geom.angle_with_hint(center.x,center.y, mx,my, drawing.pending.end_angle)
@ -460,12 +460,12 @@ function Drawing.keychord_pressed(State, chord)
drawing.pending.mode = 'square'
elseif App.mouse_down(1) and chord == 'p' and State.current_drawing_mode == 'polygon' then
local _,drawing = Drawing.current_drawing(State)
local mx,my = Drawing.coord(App.mouse_x()-State.margin_left), Drawing.coord(App.mouse_y()-drawing.y)
local mx,my = Drawing.coord(App.mouse_x()-State.left), Drawing.coord(App.mouse_y()-drawing.y)
local j = Drawing.insert_point(drawing.points, mx,my)
table.insert(drawing.pending.vertices, j)
elseif App.mouse_down(1) and chord == 'p' and (State.current_drawing_mode == 'rectangle' or State.current_drawing_mode == 'square') then
local _,drawing = Drawing.current_drawing(State)
local mx,my = Drawing.coord(App.mouse_x()-State.margin_left), Drawing.coord(App.mouse_y()-drawing.y)
local mx,my = Drawing.coord(App.mouse_x()-State.left), Drawing.coord(App.mouse_y()-drawing.y)
local j = Drawing.insert_point(drawing.points, mx,my)
while #drawing.pending.vertices >= 2 do
table.remove(drawing.pending.vertices)
@ -476,7 +476,7 @@ function Drawing.keychord_pressed(State, chord)
elseif App.mouse_down(1) and chord == 'a' and State.current_drawing_mode == 'circle' then
local _,drawing = Drawing.current_drawing(State)
drawing.pending.mode = 'arc'
local mx,my = Drawing.coord(App.mouse_x()-State.margin_left), Drawing.coord(App.mouse_y()-drawing.y)
local mx,my = Drawing.coord(App.mouse_x()-State.left), Drawing.coord(App.mouse_y()-drawing.y)
local center = drawing.points[drawing.pending.center]
drawing.pending.radius = geom.dist(center.x,center.y, mx,my)
drawing.pending.start_angle = geom.angle(center.x,center.y, mx,my)
@ -614,7 +614,7 @@ function Drawing.select_shape_at_mouse(State)
if drawing.mode == 'drawing' then
local x, y = App.mouse_x(), App.mouse_y()
if Drawing.in_drawing(drawing, x,y) then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
for i,shape in ipairs(drawing.shapes) do
assert(shape)
if geom.on_shape(mx,my, drawing, shape) then
@ -631,7 +631,7 @@ function Drawing.select_point_at_mouse(State)
if drawing.mode == 'drawing' then
local x, y = App.mouse_x(), App.mouse_y()
if Drawing.in_drawing(drawing, x,y) then
local mx,my = Drawing.coord(x-State.margin_left), Drawing.coord(y-drawing.y)
local mx,my = Drawing.coord(x-State.left), Drawing.coord(y-drawing.y)
for i,point in ipairs(drawing.points) do
assert(point)
if Drawing.near(point, mx,my) then
@ -700,14 +700,14 @@ end
function Drawing.near(point, x,y)
local px,py = Drawing.pixels(x),Drawing.pixels(y)
local cx,cy = Drawing.pixels(point.x), Drawing.pixels(point.y)
return (cx-px)*(cx-px) + (cy-py)*(cy-py) < Editor_state.margin_left
return (cx-px)*(cx-px) + (cy-py)*(cy-py) < Editor_state.left
end
function Drawing.pixels(n) -- parts to pixels
return math.floor(n*(App.screen.width-Editor_state.margin_width)/256)
return math.floor(n*Editor_state.width/256)
end
function Drawing.coord(n) -- pixels to parts
return math.floor(n*256/(App.screen.width-Editor_state.margin_width))
return math.floor(n*256/Editor_state.width)
end
function table.find(h, x)

View File

@ -5,11 +5,12 @@
function test_creating_drawing_saves()
io.write('\ntest_creating_drawing_saves')
App.screen.init{width=120, height=60}
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.filename = 'foo'
Editor_state.lines = load_array{}
edit.draw(Editor_state)
-- click on button to create drawing
edit.run_after_mouse_click(Editor_state, 8,Editor_state.margin_top+8, 1)
edit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1)
-- file not immediately saved
App.update(0.01)
check_nil(App.filesystem['foo'], 'F - test_creating_drawing_saves/early')
@ -24,18 +25,19 @@ function test_draw_line()
io.write('\ntest_draw_line')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_line/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_line/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_line/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_line/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_line/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_line/baseline/#shapes')
-- draw a line
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_line/#shapes')
check_eq(#drawing.points, 2, 'F - test_draw_line/#points')
@ -67,18 +69,19 @@ end
function test_draw_horizontal_line()
io.write('\ntest_draw_horizontal_line')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'manhattan'
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_horizontal_line/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_horizontal_line/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_horizontal_line/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_horizontal_line/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_horizontal_line/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_horizontal_line/baseline/#shapes')
-- draw a line that is more horizontal than vertical
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+26, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+26, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_horizontal_line/#shapes')
check_eq(#drawing.points, 2, 'F - test_draw_horizontal_line/#points')
@ -94,20 +97,21 @@ end
function test_draw_circle()
io.write('\ntest_draw_circle')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_circle/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_circle/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_circle/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_circle/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_circle/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_circle/baseline/#shapes')
-- draw a circle
App.mouse_move(Editor_state.margin_left+4, Editor_state.margin_top+Editor_state.drawing_padding_top+4) -- hover on drawing
App.mouse_move(Editor_state.left+4, Editor_state.top+Editor_state.drawing_padding_top+4) -- hover on drawing
edit.run_after_keychord(Editor_state, 'C-o')
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35+30, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35+30, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_circle/#shapes')
check_eq(#drawing.points, 1, 'F - test_draw_circle/#points')
@ -122,20 +126,21 @@ function test_cancel_stroke()
io.write('\ntest_cancel_stroke')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_cancel_stroke/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_cancel_stroke/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_cancel_stroke/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_cancel_stroke/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_cancel_stroke/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_cancel_stroke/baseline/#shapes')
-- start drawing a line
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
-- cancel
edit.run_after_keychord(Editor_state, 'escape')
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 0, 'F - test_cancel_stroke/#shapes')
end
@ -143,12 +148,13 @@ end
function test_keys_do_not_affect_shape_when_mouse_up()
io.write('\ntest_keys_do_not_affect_shape_when_mouse_up')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
-- hover over drawing and press 'o' without holding mouse
App.mouse_move(Editor_state.margin_left+4, Editor_state.margin_top+Editor_state.drawing_padding_top+4) -- hover on drawing
App.mouse_move(Editor_state.left+4, Editor_state.top+Editor_state.drawing_padding_top+4) -- hover on drawing
edit.run_after_keychord(Editor_state, 'o')
-- no change to drawing mode
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_keys_do_not_affect_shape_when_mouse_up/drawing_mode')
@ -158,20 +164,21 @@ end
function test_draw_circle_mid_stroke()
io.write('\ntest_draw_circle_mid_stroke')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_circle_mid_stroke/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_circle_mid_stroke/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_circle_mid_stroke/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_circle_mid_stroke/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_circle_mid_stroke/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_circle_mid_stroke/baseline/#shapes')
-- draw a circle
App.mouse_move(Editor_state.margin_left+4, Editor_state.margin_top+Editor_state.drawing_padding_top+4) -- hover on drawing
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
App.mouse_move(Editor_state.left+4, Editor_state.top+Editor_state.drawing_padding_top+4) -- hover on drawing
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_keychord(Editor_state, 'o')
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35+30, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35+30, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_circle_mid_stroke/#shapes')
check_eq(#drawing.points, 1, 'F - test_draw_circle_mid_stroke/#points')
@ -185,20 +192,21 @@ end
function test_draw_arc()
io.write('\ntest_draw_arc')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'circle'
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_arc/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_arc/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_arc/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_arc/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_arc/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_arc/baseline/#shapes')
-- draw an arc
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
App.mouse_move(Editor_state.margin_left+35+30, Editor_state.margin_top+Editor_state.drawing_padding_top+36)
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
App.mouse_move(Editor_state.left+35+30, Editor_state.top+Editor_state.drawing_padding_top+36)
edit.run_after_keychord(Editor_state, 'a') -- arc mode
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35+50, Editor_state.margin_top+Editor_state.drawing_padding_top+36+50, 1) -- 45°
edit.run_after_mouse_release(Editor_state, Editor_state.left+35+50, Editor_state.top+Editor_state.drawing_padding_top+36+50, 1) -- 45°
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_arc/#shapes')
check_eq(#drawing.points, 1, 'F - test_draw_arc/#points')
@ -215,23 +223,24 @@ end
function test_draw_polygon()
io.write('\ntest_draw_polygon')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
edit.draw(Editor_state)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_draw_polygon/baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'F - test_draw_polygon/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_polygon/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_polygon/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_polygon/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_polygon/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_polygon/baseline/#shapes')
-- first point
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_keychord(Editor_state, 'g') -- polygon mode
-- second point
App.mouse_move(Editor_state.margin_left+65, Editor_state.margin_top+Editor_state.drawing_padding_top+36)
App.mouse_move(Editor_state.left+65, Editor_state.top+Editor_state.drawing_padding_top+36)
edit.run_after_keychord(Editor_state, 'p') -- add point
-- final point
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+26, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+26, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_polygon/#shapes')
check_eq(#drawing.points, 3, 'F - test_draw_polygon/vertices')
@ -252,26 +261,27 @@ end
function test_draw_rectangle()
io.write('\ntest_draw_rectangle')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
edit.draw(Editor_state)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_draw_rectangle/baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'F - test_draw_rectangle/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_rectangle/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_rectangle/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_rectangle/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_rectangle/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_rectangle/baseline/#shapes')
-- first point
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_keychord(Editor_state, 'r') -- rectangle mode
-- second point/first edge
App.mouse_move(Editor_state.margin_left+42, Editor_state.margin_top+Editor_state.drawing_padding_top+45)
App.mouse_move(Editor_state.left+42, Editor_state.top+Editor_state.drawing_padding_top+45)
edit.run_after_keychord(Editor_state, 'p')
-- override second point/first edge
App.mouse_move(Editor_state.margin_left+75, Editor_state.margin_top+Editor_state.drawing_padding_top+76)
App.mouse_move(Editor_state.left+75, Editor_state.top+Editor_state.drawing_padding_top+76)
edit.run_after_keychord(Editor_state, 'p')
-- release (decides 'thickness' of rectangle perpendicular to first edge)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+15, Editor_state.margin_top+Editor_state.drawing_padding_top+26, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+15, Editor_state.top+Editor_state.drawing_padding_top+26, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_rectangle/#shapes')
check_eq(#drawing.points, 5, 'F - test_draw_rectangle/#points') -- currently includes every point added
@ -295,23 +305,24 @@ end
function test_draw_rectangle_intermediate()
io.write('\ntest_draw_rectangle_intermediate')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
edit.draw(Editor_state)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_draw_rectangle_intermediate/baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'F - test_draw_rectangle_intermediate/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_rectangle_intermediate/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_rectangle_intermediate/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_rectangle_intermediate/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_rectangle_intermediate/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_rectangle_intermediate/baseline/#shapes')
-- first point
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_keychord(Editor_state, 'r') -- rectangle mode
-- second point/first edge
App.mouse_move(Editor_state.margin_left+42, Editor_state.margin_top+Editor_state.drawing_padding_top+45)
App.mouse_move(Editor_state.left+42, Editor_state.top+Editor_state.drawing_padding_top+45)
edit.run_after_keychord(Editor_state, 'p')
-- override second point/first edge
App.mouse_move(Editor_state.margin_left+75, Editor_state.margin_top+Editor_state.drawing_padding_top+76)
App.mouse_move(Editor_state.left+75, Editor_state.top+Editor_state.drawing_padding_top+76)
edit.run_after_keychord(Editor_state, 'p')
local drawing = Editor_state.lines[1]
check_eq(#drawing.points, 3, 'F - test_draw_rectangle_intermediate/#points') -- currently includes every point added
@ -330,26 +341,27 @@ end
function test_draw_square()
io.write('\ntest_draw_square')
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
edit.draw(Editor_state)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_draw_square/baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'F - test_draw_square/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_square/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_square/baseline/y')
check_eq(Editor_state.lines[1].y, Editor_state.top+Editor_state.drawing_padding_top, 'F - test_draw_square/baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'F - test_draw_square/baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_draw_square/baseline/#shapes')
-- first point
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_keychord(Editor_state, 's') -- square mode
-- second point/first edge
App.mouse_move(Editor_state.margin_left+42, Editor_state.margin_top+Editor_state.drawing_padding_top+45)
App.mouse_move(Editor_state.left+42, Editor_state.top+Editor_state.drawing_padding_top+45)
edit.run_after_keychord(Editor_state, 'p')
-- override second point/first edge
App.mouse_move(Editor_state.margin_left+65, Editor_state.margin_top+Editor_state.drawing_padding_top+66)
App.mouse_move(Editor_state.left+65, Editor_state.top+Editor_state.drawing_padding_top+66)
edit.run_after_keychord(Editor_state, 'p')
-- release (decides which side of first edge to draw square on)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+15, Editor_state.margin_top+Editor_state.drawing_padding_top+26, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+15, Editor_state.top+Editor_state.drawing_padding_top+26, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_draw_square/#shapes')
check_eq(#drawing.points, 5, 'F - test_draw_square/#points') -- currently includes every point added
@ -373,13 +385,14 @@ function test_name_point()
io.write('\ntest_name_point')
-- create a drawing with a line
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
-- draw a line
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_name_point/baseline/#shapes')
check_eq(#drawing.points, 2, 'F - test_name_point/baseline/#points')
@ -415,12 +428,13 @@ function test_move_point()
io.write('\ntest_move_point')
-- create a drawing with a line
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_move_point/baseline/#shapes')
check_eq(#drawing.points, 2, 'F - test_move_point/baseline/#points')
@ -448,13 +462,13 @@ function test_move_point()
check_eq(drawing.pending.mode, 'move', 'F - test_move_point/mode:2')
check_eq(drawing.pending.target_point, p2, 'F - test_move_point/target')
-- move point
App.mouse_move(Editor_state.margin_left+26, Editor_state.margin_top+Editor_state.drawing_padding_top+44)
App.mouse_move(Editor_state.left+26, Editor_state.top+Editor_state.drawing_padding_top+44)
App.update(0.05)
local p2 = drawing.points[drawing.shapes[1].p2]
check_eq(p2.x, 26, 'F - test_move_point/x')
check_eq(p2.y, 44, 'F - test_move_point/y')
-- exit 'move' mode
edit.run_after_mouse_click(Editor_state, Editor_state.margin_left+26, Editor_state.margin_top+Editor_state.drawing_padding_top+44, 1)
edit.run_after_mouse_click(Editor_state, Editor_state.left+26, Editor_state.top+Editor_state.drawing_padding_top+44, 1)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_move_point/mode:3')
check_eq(drawing.pending, {}, 'F - test_move_point/pending')
-- wait until save
@ -471,12 +485,13 @@ function test_move_point_on_manhattan_line()
io.write('\ntest_move_point_on_manhattan_line')
-- create a drawing with a manhattan line
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'manhattan'
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+46, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+46, 1)
local drawing = Editor_state.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')
@ -486,7 +501,7 @@ function test_move_point_on_manhattan_line()
edit.run_after_keychord(Editor_state, 'C-u')
check_eq(Editor_state.current_drawing_mode, 'move', 'F - test_move_point_on_manhattan_line/mode:1')
-- move point
App.mouse_move(Editor_state.margin_left+26, Editor_state.margin_top+Editor_state.drawing_padding_top+44)
App.mouse_move(Editor_state.left+26, Editor_state.top+Editor_state.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')
@ -496,20 +511,21 @@ function test_delete_lines_at_point()
io.write('\ntest_delete_lines_at_point')
-- create a drawing with two lines connected at a point
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+55, Editor_state.margin_top+Editor_state.drawing_padding_top+26, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+55, Editor_state.top+Editor_state.drawing_padding_top+26, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 2, 'F - test_delete_lines_at_point/baseline/#shapes')
check_eq(drawing.shapes[1].mode, 'line', 'F - test_delete_lines_at_point/baseline/shape:1')
check_eq(drawing.shapes[2].mode, 'line', 'F - test_delete_lines_at_point/baseline/shape:2')
-- hover on the common point and delete
App.mouse_move(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36)
App.mouse_move(Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36)
edit.run_after_keychord(Editor_state, 'C-d')
check_eq(drawing.shapes[1].mode, 'deleted', 'F - test_delete_lines_at_point/shape:1')
check_eq(drawing.shapes[2].mode, 'deleted', 'F - test_delete_lines_at_point/shape:2')
@ -524,20 +540,21 @@ end
function test_delete_line_under_mouse_pointer()
io.write('\ntest_delete_line_under_mouse_pointer')
-- create a drawing with two lines connected at a point
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+55, Editor_state.margin_top+Editor_state.drawing_padding_top+26, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+55, Editor_state.top+Editor_state.drawing_padding_top+26, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 2, 'F - test_delete_line_under_mouse_pointer/baseline/#shapes')
check_eq(drawing.shapes[1].mode, 'line', 'F - test_delete_line_under_mouse_pointer/baseline/shape:1')
check_eq(drawing.shapes[2].mode, 'line', 'F - test_delete_line_under_mouse_pointer/baseline/shape:2')
-- hover on one of the lines and delete
App.mouse_move(Editor_state.margin_left+25, Editor_state.margin_top+Editor_state.drawing_padding_top+26)
App.mouse_move(Editor_state.left+25, Editor_state.top+Editor_state.drawing_padding_top+26)
edit.run_after_keychord(Editor_state, 'C-d')
-- only that line is deleted
check_eq(drawing.shapes[1].mode, 'deleted', 'F - test_delete_line_under_mouse_pointer/shape:1')
@ -547,27 +564,28 @@ end
function test_delete_point_from_polygon()
io.write('\ntest_delete_point_from_polygon')
-- create a drawing with two lines connected at a point
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
-- first point
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_keychord(Editor_state, 'g') -- polygon mode
-- second point
App.mouse_move(Editor_state.margin_left+65, Editor_state.margin_top+Editor_state.drawing_padding_top+36)
App.mouse_move(Editor_state.left+65, Editor_state.top+Editor_state.drawing_padding_top+36)
edit.run_after_keychord(Editor_state, 'p') -- add point
-- third point
App.mouse_move(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+26)
App.mouse_move(Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+26)
edit.run_after_keychord(Editor_state, 'p') -- add point
-- fourth point
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+14, Editor_state.margin_top+Editor_state.drawing_padding_top+16, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+14, Editor_state.top+Editor_state.drawing_padding_top+16, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_delete_point_from_polygon/baseline/#shapes')
check_eq(drawing.shapes[1].mode, 'polygon', 'F - test_delete_point_from_polygon/baseline/mode')
check_eq(#drawing.shapes[1].vertices, 4, 'F - test_delete_point_from_polygon/baseline/vertices')
-- hover on a point and delete
App.mouse_move(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+26)
App.mouse_move(Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+26)
edit.run_after_keychord(Editor_state, 'C-d')
-- just the one point is deleted
check_eq(drawing.shapes[1].mode, 'polygon', 'F - test_delete_point_from_polygon/shape')
@ -577,24 +595,25 @@ end
function test_delete_point_from_polygon()
io.write('\ntest_delete_point_from_polygon')
-- create a drawing with two lines connected at a point
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
-- first point
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_keychord(Editor_state, 'g') -- polygon mode
-- second point
App.mouse_move(Editor_state.margin_left+65, Editor_state.margin_top+Editor_state.drawing_padding_top+36)
App.mouse_move(Editor_state.left+65, Editor_state.top+Editor_state.drawing_padding_top+36)
edit.run_after_keychord(Editor_state, 'p') -- add point
-- third point
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+14, Editor_state.margin_top+Editor_state.drawing_padding_top+16, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+14, Editor_state.top+Editor_state.drawing_padding_top+16, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_delete_point_from_polygon/baseline/#shapes')
check_eq(drawing.shapes[1].mode, 'polygon', 'F - test_delete_point_from_polygon/baseline/mode')
check_eq(#drawing.shapes[1].vertices, 3, 'F - test_delete_point_from_polygon/baseline/vertices')
-- hover on a point and delete
App.mouse_move(Editor_state.margin_left+65, Editor_state.margin_top+Editor_state.drawing_padding_top+36)
App.mouse_move(Editor_state.left+65, Editor_state.top+Editor_state.drawing_padding_top+36)
edit.run_after_keychord(Editor_state, 'C-d')
-- there's < 3 points left, so the whole polygon is deleted
check_eq(drawing.shapes[1].mode, 'deleted', 'F - test_delete_point_from_polygon')
@ -604,13 +623,14 @@ function test_undo_name_point()
io.write('\ntest_undo_name_point')
-- create a drawing with a line
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
-- draw a line
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_undo_name_point/baseline/#shapes')
check_eq(#drawing.points, 2, 'F - test_undo_name_point/baseline/#points')
@ -649,12 +669,13 @@ function test_undo_move_point()
io.write('\ntest_undo_move_point')
-- create a drawing with a line
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_undo_move_point/baseline/#shapes')
check_eq(#drawing.points, 2, 'F - test_undo_move_point/baseline/#points')
@ -668,13 +689,13 @@ function test_undo_move_point()
check_nil(p2.name, 'F - test_undo_move_point/baseline/p2:name')
-- move p2
edit.run_after_keychord(Editor_state, 'C-u')
App.mouse_move(Editor_state.margin_left+26, Editor_state.margin_top+Editor_state.drawing_padding_top+44)
App.mouse_move(Editor_state.left+26, Editor_state.top+Editor_state.drawing_padding_top+44)
App.update(0.05)
local p2 = drawing.points[drawing.shapes[1].p2]
check_eq(p2.x, 26, 'F - test_undo_move_point/x')
check_eq(p2.y, 44, 'F - test_undo_move_point/y')
-- exit 'move' mode
edit.run_after_mouse_click(Editor_state, Editor_state.margin_left+26, Editor_state.margin_top+Editor_state.drawing_padding_top+44, 1)
edit.run_after_mouse_click(Editor_state, Editor_state.left+26, Editor_state.top+Editor_state.drawing_padding_top+44, 1)
check_eq(Editor_state.next_history, 4, 'F - test_undo_move_point/next_history')
-- undo
edit.run_after_keychord(Editor_state, 'C-z')
@ -698,20 +719,21 @@ function test_undo_delete_point()
io.write('\ntest_undo_delete_point')
-- create a drawing with two lines connected at a point
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
App.screen.init{width=Margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width) -- zero right margin
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.margin_left+55, Editor_state.margin_top+Editor_state.drawing_padding_top+26, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Editor_state.drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+55, Editor_state.top+Editor_state.drawing_padding_top+26, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 2, 'F - test_undo_delete_point/baseline/#shapes')
check_eq(drawing.shapes[1].mode, 'line', 'F - test_undo_delete_point/baseline/shape:1')
check_eq(drawing.shapes[2].mode, 'line', 'F - test_undo_delete_point/baseline/shape:2')
-- hover on the common point and delete
App.mouse_move(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36)
App.mouse_move(Editor_state.left+35, Editor_state.top+Editor_state.drawing_padding_top+36)
edit.run_after_keychord(Editor_state, 'C-d')
check_eq(drawing.shapes[1].mode, 'deleted', 'F - test_undo_delete_point/shape:1')
check_eq(drawing.shapes[2].mode, 'deleted', 'F - test_undo_delete_point/shape:2')

View File

@ -10,6 +10,10 @@ Icon_color = {r=0.7, g=0.7, b=0.7} -- color of current mode icon in drawings
Help_color = {r=0, g=0.5, b=0}
Help_background_color = {r=0, g=0.5, b=0, a=0.1}
Margin_top = 15
Margin_left = 25
Margin_right = 25
utf8 = require 'utf8'
require 'file'
@ -22,7 +26,7 @@ require 'icons'
edit = {}
-- run in both tests and a real run
function edit.initialize_state()
function edit.initialize_state(top, left, right) -- currently always draws to bottom of screen
local result = {
-- a line is either text or a drawing
-- a text is a table with:
@ -88,10 +92,10 @@ function edit.initialize_state()
-- widest possible character width
em = App.newText(love.graphics.getFont(), 'm'),
margin_top = 15,
margin_left = 25,
margin_right = 0,
margin_width = nil,
top = top,
left = left,
right = right,
width = right-left,
drawing_padding_top = 10,
drawing_padding_bottom = 10,
@ -109,7 +113,6 @@ function edit.initialize_state()
search_text = nil,
search_backup = nil, -- stuff to restore when cancelling search
}
result.margin_width = result.margin_left + result.margin_right
result.drawing_padding_height = result.drawing_padding_top + result.drawing_padding_bottom
return result
end -- App.initialize_state
@ -119,7 +122,7 @@ function edit.draw(State)
--? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
assert(Text.le1(State.screen_top1, State.cursor1))
State.cursor_y = -1
local y = State.margin_top
local y = State.top
--? print('== draw')
for line_index = State.screen_top1.line,#State.lines do
local line = State.lines[line_index]
@ -144,7 +147,7 @@ function edit.draw(State)
})
if State.search_term == nil then
if line_index == State.cursor1.line then
Text.draw_cursor(State, State.margin_left, y)
Text.draw_cursor(State, State.left, y)
end
end
State.screen_bottom1.pos = State.screen_top1.pos
@ -161,7 +164,7 @@ function edit.draw(State)
line.startpos = State.screen_top1.pos
end
--? print('text.draw', y, line_index)
y, State.screen_bottom1.pos = Text.draw(State, line, line_index, line.starty, State.margin_left, App.screen.width-State.margin_right)
y, State.screen_bottom1.pos = Text.draw(State, line, line_index, line.starty, State.left, State.right)
y = y + State.line_height
--? print('=> y', y)
end
@ -203,7 +206,7 @@ function edit.mouse_pressed(State, x,y, mouse_button)
for line_index,line in ipairs(State.lines) do
if line.mode == 'text' then
if Text.in_line(State, line, x,y, State.margin_left, App.screen.width-State.margin_right) then
if Text.in_line(State, line, x,y, State.left, State.right) then
-- delicate dance between cursor, selection and old cursor/selection
-- scenarios:
-- regular press+release: sets cursor, clears selection
@ -218,7 +221,7 @@ function edit.mouse_pressed(State, x,y, mouse_button)
State.mousepress_shift = App.shift_down()
State.selection1 = {
line=line_index,
pos=Text.to_pos_on_line(State, line, x, y, State.margin_left, App.screen.width-State.margin_right),
pos=Text.to_pos_on_line(State, line, x, y, State.left, State.right),
}
--? print('selection', State.selection1.line, State.selection1.pos)
break
@ -248,11 +251,11 @@ function edit.mouse_released(State, x,y, mouse_button)
else
for line_index,line in ipairs(State.lines) do
if line.mode == 'text' then
if Text.in_line(State, line, x,y, State.margin_left, App.screen.width-State.margin_right) then
if Text.in_line(State, line, x,y, State.left, State.right) then
--? print('reset selection')
State.cursor1 = {
line=line_index,
pos=Text.to_pos_on_line(State, line, x, y, State.margin_left, App.screen.width-State.margin_right),
pos=Text.to_pos_on_line(State, line, x, y, State.left, State.right),
}
--? print('cursor', State.cursor1.line, State.cursor1.pos)
if State.mousepress_shift then
@ -299,7 +302,7 @@ function edit.keychord_pressed(State, chord, key)
-- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
(not App.shift_down() or utf8.len(key) == 1) and
chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and backspace ~= 'delete' and not App.is_cursor_movement(chord) then
Text.delete_selection(State, State.margin_left, App.screen.width-State.margin_right)
Text.delete_selection(State, State.left, State.right)
end
if State.search_term then
if chord == 'escape' then
@ -371,7 +374,7 @@ function edit.keychord_pressed(State, chord, key)
end
elseif chord == 'C-x' then
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
local s = Text.cut_selection(State, State.margin_left, App.screen.width-State.margin_right)
local s = Text.cut_selection(State, State.left, State.right)
if s then
App.setClipboardText(s)
end
@ -392,7 +395,7 @@ function edit.keychord_pressed(State, chord, key)
end
end
if Text.cursor_past_screen_bottom(State) then
Text.snap_cursor_to_bottom_of_screen(State, State.margin_left, App.screen.height-State.margin_right)
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
end
schedule_save(State)
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})

View File

@ -1,131 +1,131 @@
function draw_help_without_mouse_pressed(State, drawing)
App.color(Help_color)
local y = drawing.y+10
love.graphics.print("Things you can do:", State.margin_left+30,y)
love.graphics.print("Things you can do:", State.left+30,y)
y = y + State.line_height
love.graphics.print("* Press the mouse button to start drawing a "..current_shape(State), State.margin_left+30,y)
love.graphics.print("* Press the mouse button to start drawing a "..current_shape(State), State.left+30,y)
y = y + State.line_height
love.graphics.print("* Hover on a point and press 'ctrl+u' to pick it up and start moving it,", State.margin_left+30,y)
love.graphics.print("* Hover on a point and press 'ctrl+u' to pick it up and start moving it,", State.left+30,y)
y = y + State.line_height
love.graphics.print("then press the mouse button to drop it", State.margin_left+30+bullet_indent(),y)
love.graphics.print("then press the mouse button to drop it", State.left+30+bullet_indent(),y)
y = y + State.line_height
love.graphics.print("* Hover on a point and press 'ctrl+n', type a name, then press 'enter'", State.margin_left+30,y)
love.graphics.print("* Hover on a point and press 'ctrl+n', type a name, then press 'enter'", State.left+30,y)
y = y + State.line_height
love.graphics.print("* Hover on a point or shape and press 'ctrl+d' to delete it", State.margin_left+30,y)
love.graphics.print("* Hover on a point or shape and press 'ctrl+d' to delete it", State.left+30,y)
y = y + State.line_height
if State.current_drawing_mode ~= 'freehand' then
love.graphics.print("* Press 'ctrl+p' to switch to drawing freehand strokes", State.margin_left+30,y)
love.graphics.print("* Press 'ctrl+p' to switch to drawing freehand strokes", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'line' then
love.graphics.print("* Press 'ctrl+l' to switch to drawing lines", State.margin_left+30,y)
love.graphics.print("* Press 'ctrl+l' to switch to drawing lines", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'manhattan' then
love.graphics.print("* Press 'ctrl+m' to switch to drawing horizontal/vertical lines", State.margin_left+30,y)
love.graphics.print("* Press 'ctrl+m' to switch to drawing horizontal/vertical lines", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'circle' then
love.graphics.print("* Press 'ctrl+o' to switch to drawing circles/arcs", State.margin_left+30,y)
love.graphics.print("* Press 'ctrl+o' to switch to drawing circles/arcs", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'polygon' then
love.graphics.print("* Press 'ctrl+g' to switch to drawing polygons", State.margin_left+30,y)
love.graphics.print("* Press 'ctrl+g' to switch to drawing polygons", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'rectangle' then
love.graphics.print("* Press 'ctrl+r' to switch to drawing rectangles", State.margin_left+30,y)
love.graphics.print("* Press 'ctrl+r' to switch to drawing rectangles", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'square' then
love.graphics.print("* Press 'ctrl+s' to switch to drawing squares", State.margin_left+30,y)
love.graphics.print("* Press 'ctrl+s' to switch to drawing squares", State.left+30,y)
y = y + State.line_height
end
love.graphics.print("* Press 'ctrl+=' or 'ctrl+-' to zoom in or out, ctrl+0 to reset zoom", State.margin_left+30,y)
love.graphics.print("* Press 'ctrl+=' or 'ctrl+-' to zoom in or out, ctrl+0 to reset zoom", State.left+30,y)
y = y + State.line_height
love.graphics.print("Press 'esc' now to hide this message", State.margin_left+30,y)
love.graphics.print("Press 'esc' now to hide this message", State.left+30,y)
y = y + State.line_height
App.color(Help_background_color)
love.graphics.rectangle('fill', State.margin_left,drawing.y, App.screen.width-State.margin_width, math.max(Drawing.pixels(drawing.h),y-drawing.y))
love.graphics.rectangle('fill', State.left,drawing.y, State.width, math.max(Drawing.pixels(drawing.h),y-drawing.y))
end
function draw_help_with_mouse_pressed(State, drawing)
App.color(Help_color)
local y = drawing.y+10
love.graphics.print("You're currently drawing a "..current_shape(State, drawing.pending), State.margin_left+30,y)
love.graphics.print("You're currently drawing a "..current_shape(State, drawing.pending), State.left+30,y)
y = y + State.line_height
love.graphics.print('Things you can do now:', State.margin_left+30,y)
love.graphics.print('Things you can do now:', State.left+30,y)
y = y + State.line_height
if State.current_drawing_mode == 'freehand' then
love.graphics.print('* Release the mouse button to finish drawing the stroke', State.margin_left+30,y)
love.graphics.print('* Release the mouse button to finish drawing the stroke', State.left+30,y)
y = y + State.line_height
elseif State.current_drawing_mode == 'line' or State.current_drawing_mode == 'manhattan' then
love.graphics.print('* Release the mouse button to finish drawing the line', State.margin_left+30,y)
love.graphics.print('* Release the mouse button to finish drawing the line', State.left+30,y)
y = y + State.line_height
elseif State.current_drawing_mode == 'circle' then
if drawing.pending.mode == 'circle' then
love.graphics.print('* Release the mouse button to finish drawing the circle', State.margin_left+30,y)
love.graphics.print('* Release the mouse button to finish drawing the circle', State.left+30,y)
y = y + State.line_height
love.graphics.print("* Press 'a' to draw just an arc of a circle", State.margin_left+30,y)
love.graphics.print("* Press 'a' to draw just an arc of a circle", State.left+30,y)
else
love.graphics.print('* Release the mouse button to finish drawing the arc', State.margin_left+30,y)
love.graphics.print('* Release the mouse button to finish drawing the arc', State.left+30,y)
end
y = y + State.line_height
elseif State.current_drawing_mode == 'polygon' then
love.graphics.print('* Release the mouse button to finish drawing the polygon', State.margin_left+30,y)
love.graphics.print('* Release the mouse button to finish drawing the polygon', State.left+30,y)
y = y + State.line_height
love.graphics.print("* Press 'p' to add a vertex to the polygon", State.margin_left+30,y)
love.graphics.print("* Press 'p' to add a vertex to the polygon", State.left+30,y)
y = y + State.line_height
elseif State.current_drawing_mode == 'rectangle' then
if #drawing.pending.vertices < 2 then
love.graphics.print("* Press 'p' to add a vertex to the rectangle", State.margin_left+30,y)
love.graphics.print("* Press 'p' to add a vertex to the rectangle", State.left+30,y)
y = y + State.line_height
else
love.graphics.print('* Release the mouse button to finish drawing the rectangle', State.margin_left+30,y)
love.graphics.print('* Release the mouse button to finish drawing the rectangle', State.left+30,y)
y = y + State.line_height
love.graphics.print("* Press 'p' to replace the second vertex of the rectangle", State.margin_left+30,y)
love.graphics.print("* Press 'p' to replace the second vertex of the rectangle", State.left+30,y)
y = y + State.line_height
end
elseif State.current_drawing_mode == 'square' then
if #drawing.pending.vertices < 2 then
love.graphics.print("* Press 'p' to add a vertex to the square", State.margin_left+30,y)
love.graphics.print("* Press 'p' to add a vertex to the square", State.left+30,y)
y = y + State.line_height
else
love.graphics.print('* Release the mouse button to finish drawing the square', State.margin_left+30,y)
love.graphics.print('* Release the mouse button to finish drawing the square', State.left+30,y)
y = y + State.line_height
love.graphics.print("* Press 'p' to replace the second vertex of the square", State.margin_left+30,y)
love.graphics.print("* Press 'p' to replace the second vertex of the square", State.left+30,y)
y = y + State.line_height
end
end
love.graphics.print("* Press 'esc' then release the mouse button to cancel the current shape", State.margin_left+30,y)
love.graphics.print("* Press 'esc' then release the mouse button to cancel the current shape", State.left+30,y)
y = y + State.line_height
y = y + State.line_height
if State.current_drawing_mode ~= 'line' then
love.graphics.print("* Press 'l' to switch to drawing lines", State.margin_left+30,y)
love.graphics.print("* Press 'l' to switch to drawing lines", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'manhattan' then
love.graphics.print("* Press 'm' to switch to drawing horizontal/vertical lines", State.margin_left+30,y)
love.graphics.print("* Press 'm' to switch to drawing horizontal/vertical lines", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'circle' then
love.graphics.print("* Press 'o' to switch to drawing circles/arcs", State.margin_left+30,y)
love.graphics.print("* Press 'o' to switch to drawing circles/arcs", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'polygon' then
love.graphics.print("* Press 'g' to switch to drawing polygons", State.margin_left+30,y)
love.graphics.print("* Press 'g' to switch to drawing polygons", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'rectangle' then
love.graphics.print("* Press 'r' to switch to drawing rectangles", State.margin_left+30,y)
love.graphics.print("* Press 'r' to switch to drawing rectangles", State.left+30,y)
y = y + State.line_height
end
if State.current_drawing_mode ~= 'square' then
love.graphics.print("* Press 's' to switch to drawing squares", State.margin_left+30,y)
love.graphics.print("* Press 's' to switch to drawing squares", State.left+30,y)
y = y + State.line_height
end
App.color(Help_background_color)
love.graphics.rectangle('fill', State.margin_left,drawing.y, App.screen.width-State.margin_width, math.max(Drawing.pixels(drawing.h),y-drawing.y))
love.graphics.rectangle('fill', State.left,drawing.y, State.width, math.max(Drawing.pixels(drawing.h),y-drawing.y))
end
function current_shape(State, shape)

View File

@ -14,7 +14,7 @@ Editor_state = {}
-- called both in tests and real run
function App.initialize_globals()
Editor_state = edit.initialize_state()
-- tests currently mostly clear their own state
-- resize
Last_resize_time = nil
@ -33,7 +33,7 @@ function App.initialize(arg)
if love.filesystem.getInfo('config') then
load_settings()
else
load_defaults()
initialize_default_settings()
end
if #arg > 0 then
@ -60,9 +60,6 @@ function App.initialize(arg)
end
love.window.setTitle('lines.love - '..Editor_state.filename)
Editor_state.margin_right = 25
Editor_state.margin_width = Editor_state.margin_left + Editor_state.margin_right
if #arg > 1 then
print('ignoring commandline args after '..arg[1])
end
@ -86,24 +83,34 @@ function load_settings()
App.screen.flags.minheight = math.min(App.screen.width, 200)
App.screen.width, App.screen.height = settings.width, settings.height
love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
Editor_state.filename = settings.filename
initialize_font_settings(settings.font_height)
Editor_state.font_height = settings.font_height
love.graphics.setFont(love.graphics.newFont(Editor_state.font_height))
Editor_state.line_height = math.floor(Editor_state.font_height*1.3)
Editor_state.em = App.newText(love.graphics.getFont(), 'm')
Editor_state.screen_top1 = settings.screen_top
Editor_state.cursor1 = settings.cursor
end
function load_defaults()
initialize_font_settings(20)
initialize_window_geometry()
function initialize_default_settings()
local font_height = 20
love.graphics.setFont(love.graphics.newFont(font_height))
local em = App.newText(love.graphics.getFont(), 'm')
initialize_window_geometry(App.width(em))
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
Editor_state.font_height = font_height
Editor_state.line_height = math.floor(font_height*1.3)
Editor_state.em = em
end
function initialize_window_geometry()
function initialize_window_geometry(em_width)
-- maximize window
love.window.setMode(0, 0) -- maximize
App.screen.width, App.screen.height, App.screen.flags = love.window.getMode()
-- shrink slightly to account for window decoration
App.screen.width = 40*App.width(Editor_state.em)
-- shrink height slightly to account for window decoration
App.screen.height = App.screen.height-100
App.screen.width = 40*em_width
App.screen.flags.resizable = true
App.screen.flags.minwidth = math.min(App.screen.width, 200)
App.screen.flags.minheight = math.min(App.screen.width, 200)
@ -115,18 +122,10 @@ function App.resize(w, h)
App.screen.width, App.screen.height = w, h
Text.redraw_all(Editor_state)
Editor_state.selection1 = {} -- no support for shift drag while we're resizing
Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.margin_left, App.screen.height-Editor_state.margin_right)
Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)
Last_resize_time = App.getTime()
end
function initialize_font_settings(font_height)
Editor_state.font_height = font_height
love.graphics.setFont(love.graphics.newFont(Editor_state.font_height))
Editor_state.line_height = math.floor(font_height*1.3)
Editor_state.em = App.newText(love.graphics.getFont(), 'm')
end
function App.filedropped(file)
-- first make sure to save edits on any existing file
if Editor_state.next_save then

View File

@ -1,8 +1,8 @@
function test_resize_window()
io.write('\ntest_resize_window')
Editor_state.filename = 'foo'
App.screen.init{width=Editor_state.margin_left+300, height=300}
check_eq(App.screen.width, Editor_state.margin_left+300, 'F - test_resize_window/baseline/width')
App.screen.init{width=Editor_state.left+300, height=300}
check_eq(App.screen.width, Editor_state.left+300, 'F - test_resize_window/baseline/width')
check_eq(App.screen.height, 300, 'F - test_resize_window/baseline/height')
App.resize(200, 400)
check_eq(App.screen.width, 200, 'F - test_resize_window/width')
@ -12,7 +12,7 @@ end
function test_drop_file()
io.write('\ntest_drop_file')
App.screen.init{width=Editor_state.margin_left+300, height=300}
App.screen.init{width=Editor_state.left+300, height=300}
App.filesystem['foo'] = 'abc\ndef\nghi\n'
local fake_dropped_file = {
opened = false,
@ -39,7 +39,7 @@ end
function test_drop_file_saves_previous()
io.write('\ntest_drop_file_saves_previous')
App.screen.init{width=Editor_state.margin_left+300, height=300}
App.screen.init{width=Editor_state.left+300, height=300}
-- initially editing a file called foo that hasn't been saved to filesystem yet
Editor_state.lines = load_array{'abc', 'def'}
Editor_state.filename = 'foo'

View File

@ -56,7 +56,7 @@ function Text.search_next(State)
end
if Text.lt1(State.cursor1, State.screen_top1) or Text.lt1(State.screen_bottom1, State.cursor1) then
State.screen_top1.line = State.cursor1.line
local _, pos = Text.pos_at_start_of_cursor_screen_line(State, State.margin_left, App.screen.width-State.margin_right)
local _, pos = Text.pos_at_start_of_cursor_screen_line(State, State.left, State.right)
State.screen_top1.pos = pos
end
end
@ -96,7 +96,7 @@ function Text.search_previous(State)
end
if Text.lt1(State.cursor1, State.screen_top1) or Text.lt1(State.screen_bottom1, State.cursor1) then
State.screen_top1.line = State.cursor1.line
local _, pos = Text.pos_at_start_of_cursor_screen_line(State, State.margin_left, App.screen.width-State.margin_right)
local _, pos = Text.pos_at_start_of_cursor_screen_line(State, State.left, State.right)
State.screen_top1.pos = pos
end
end

View File

@ -141,8 +141,8 @@ function Text.textinput(State, t)
--? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
Text.insert_at_cursor(State, t)
if State.cursor_y >= App.screen.height - State.line_height then
Text.populate_screen_line_starting_pos(State.lines[State.cursor1.line], State.margin_left, App.screen.width-State.margin_right)
Text.snap_cursor_to_bottom_of_screen(State, State.margin_left, App.screen.width-State.margin_right)
Text.populate_screen_line_starting_pos(State.lines[State.cursor1.line], State.left, State.right)
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
--? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
end
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
@ -165,7 +165,7 @@ function Text.keychord_pressed(State, chord)
Text.insert_return(State)
State.selection1 = {}
if (State.cursor_y + State.line_height) > App.screen.height then
Text.snap_cursor_to_bottom_of_screen(State, State.margin_left, App.screen.width-State.margin_right)
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
end
schedule_save(State)
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
@ -174,15 +174,15 @@ function Text.keychord_pressed(State, chord)
--? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
Text.insert_at_cursor(State, '\t')
if State.cursor_y >= App.screen.height - State.line_height then
Text.populate_screen_line_starting_pos(State.lines[State.cursor1.line], State.margin_left, App.screen.width-State.margin_right)
Text.snap_cursor_to_bottom_of_screen(State, State.margin_left, App.screen.width-State.margin_right)
Text.populate_screen_line_starting_pos(State.lines[State.cursor1.line], State.left, State.right)
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
--? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
end
schedule_save(State)
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
elseif chord == 'backspace' then
if State.selection1.line then
Text.delete_selection(State, State.margin_left, App.screen.width-State.margin_right)
Text.delete_selection(State, State.left, State.right)
schedule_save(State)
return
end
@ -212,8 +212,8 @@ function Text.keychord_pressed(State, chord)
State.cursor1.line = State.cursor1.line-1
end
if Text.lt1(State.cursor1, State.screen_top1) then
local top2 = Text.to2(State, State.screen_top1, State.margin_left, App.screen.width-State.margin_right)
top2 = Text.previous_screen_line(State, top2, State.margin_left, App.screen.width-State.margin_right)
local top2 = Text.to2(State, State.screen_top1, State.left, State.right)
top2 = Text.previous_screen_line(State, top2, State.left, State.right)
State.screen_top1 = Text.to1(State, top2)
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
end
@ -223,7 +223,7 @@ function Text.keychord_pressed(State, chord)
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
elseif chord == 'delete' then
if State.selection1.line then
Text.delete_selection(State, State.margin_left, App.screen.width-State.margin_right)
Text.delete_selection(State, State.left, State.right)
schedule_save(State)
return
end
@ -258,43 +258,43 @@ function Text.keychord_pressed(State, chord)
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
--== shortcuts that move the cursor
elseif chord == 'left' then
Text.left(State, State.margin_left, App.screen.width-State.margin_right)
Text.left(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'right' then
Text.right(State, State.margin_left, App.screen.width-State.margin_right)
Text.right(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'S-left' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.left(State, State.margin_left, App.screen.width-State.margin_right)
Text.left(State, State.left, State.right)
elseif chord == 'S-right' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.right(State, State.margin_left, App.screen.width-State.margin_right)
Text.right(State, State.left, State.right)
-- C- hotkeys reserved for drawings, so we'll use M-
elseif chord == 'M-left' then
Text.word_left(State, State.margin_left, App.screen.width-State.margin_right)
Text.word_left(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'M-right' then
Text.word_right(State, State.margin_left, App.screen.width-State.margin_right)
Text.word_right(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'M-S-left' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.word_left(State, State.margin_left, App.screen.width-State.margin_right)
Text.word_left(State, State.left, State.right)
elseif chord == 'M-S-right' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.word_right(State, State.margin_left, App.screen.width-State.margin_right)
Text.word_right(State, State.left, State.right)
elseif chord == 'home' then
Text.start_of_line(State)
State.selection1 = {}
elseif chord == 'end' then
Text.end_of_line(State, State.margin_left, App.screen.width-State.margin_right)
Text.end_of_line(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'S-home' then
if State.selection1.line == nil then
@ -305,39 +305,39 @@ function Text.keychord_pressed(State, chord)
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.end_of_line(State, State.margin_left, App.screen.width-State.margin_right)
Text.end_of_line(State, State.left, State.right)
elseif chord == 'up' then
Text.up(State, State.margin_left, App.screen.width-State.margin_right)
Text.up(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'down' then
Text.down(State, State.margin_left, App.screen.width-State.margin_right)
Text.down(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'S-up' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.up(State, State.margin_left, App.screen.width-State.margin_right)
Text.up(State, State.left, State.right)
elseif chord == 'S-down' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.down(State, State.margin_left, App.screen.width-State.margin_right)
Text.down(State, State.left, State.right)
elseif chord == 'pageup' then
Text.pageup(State, State.margin_left, App.screen.width-State.margin_right)
Text.pageup(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'pagedown' then
Text.pagedown(State, State.margin_left, App.screen.width-State.margin_right)
Text.pagedown(State, State.left, State.right)
State.selection1 = {}
elseif chord == 'S-pageup' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.pageup(State, State.margin_left, App.screen.width-State.margin_right)
Text.pageup(State, State.left, State.right)
elseif chord == 'S-pagedown' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.pagedown(State, State.margin_left, App.screen.width-State.margin_right)
Text.pagedown(State, State.left, State.right)
end
end
@ -357,7 +357,7 @@ function Text.pageup(State, left, right)
local top2 = Text.to2(State, State.screen_top1, left, right)
--? print(App.screen.height)
local y = App.screen.height - State.line_height
while y >= State.margin_top do
while y >= State.top do
--? print(y, top2.line, top2.screen_line, top2.screen_pos)
if State.screen_top1.line == 1 and State.screen_top1.pos == 1 then break end
if State.lines[State.screen_top1.line].mode == 'text' then
@ -635,7 +635,7 @@ function Text.cursor_at_final_screen_line(State, left, right)
end
function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State, left, right)
local y = State.margin_top
local y = State.top
while State.cursor1.line <= #State.lines do
if State.lines[State.cursor1.line].mode == 'text' then
break
@ -668,7 +668,7 @@ function Text.snap_cursor_to_bottom_of_screen(State, left, right)
if top2.line == 1 and top2.screen_line == 1 then break end
if top2.screen_line > 1 or State.lines[top2.line-1].mode == 'text' then
local h = State.line_height
if y - h < State.margin_top then
if y - h < State.top then
break
end
y = y - h
@ -678,7 +678,7 @@ function Text.snap_cursor_to_bottom_of_screen(State, left, right)
-- We currently can't draw partial drawings, so either skip it entirely
-- or not at all.
local h = State.drawing_padding_height + Drawing.pixels(State.lines[top2.line-1].h)
if y - h < State.margin_top then
if y - h < State.top then
break
end
--? print('skipping drawing of height', h)

File diff suppressed because it is too large Load Diff