support for naming points

There's still an absence of affordance showing when you're in naming mode.
This commit is contained in:
Kartik K. Agaram 2022-05-21 14:03:06 -07:00
parent 96df187488
commit bb9e23a638
7 changed files with 62 additions and 4 deletions

View File

@ -16,6 +16,9 @@ Known issues:
So far this app isn't really designed for drawing-heavy files. For now I'm
targeting mostly-text files with a few drawings mixed in.
* No clipping yet for drawings. In particular, circles and point labels can
overflow a drawing.
* Insufficient handling of constraints when moving points. For example, if you
draw a manhattan line and then move one of the points, you may not be able
to hover on it anymore.

View File

@ -46,6 +46,10 @@ function Drawing.draw(line)
love.graphics.setColor(0,0,0)
love.graphics.circle('fill', Drawing.pixels(p.x)+16,Drawing.pixels(p.y)+line.y, 2)
end
if p.name then
-- todo: clip
love.graphics.print(p.name, Drawing.pixels(p.x)+16+5,Drawing.pixels(p.y)+line.y+5, 0, Zoom)
end
end
end
love.graphics.setColor(0.75,0.75,0.75)
@ -78,6 +82,7 @@ function Drawing.draw_shape(left,top, drawing, shape)
local curr = drawing.points[shape.vertices[1]]
love.graphics.line(Drawing.pixels(prev.x)+left,Drawing.pixels(prev.y)+top, Drawing.pixels(curr.x)+left,Drawing.pixels(curr.y)+top)
elseif shape.mode == 'circle' then
-- todo: clip
local center = drawing.points[shape.center]
love.graphics.circle('line', Drawing.pixels(center.x)+left,Drawing.pixels(center.y)+top, Drawing.pixels(shape.radius))
elseif shape.mode == 'arc' then
@ -173,6 +178,8 @@ function Drawing.draw_pending_shape(left,top, drawing)
love.graphics.arc('line', 'open', cx,cy, Drawing.pixels(shape.radius), shape.start_angle, shape.end_angle, 360)
elseif shape.mode == 'move' then
-- nothing pending; changes are immediately committed
elseif shape.mode == 'name' then
-- nothing pending; changes are immediately committed
else
print(shape.mode)
assert(false)
@ -468,6 +475,15 @@ function Drawing.keychord_pressed(chord)
drawing.pending = {mode=Current_drawing_mode, target_point=p}
Lines.current = drawing
end
elseif chord == 'C-n' and not love.mouse.isDown('1') then
local drawing,point_index,p = Drawing.select_point_at_mouse()
if drawing then
Previous_drawing_mode = Current_drawing_mode
Current_drawing_mode = 'name'
p.name = ''
drawing.pending = {mode=Current_drawing_mode, target_point=point_index}
Lines.current = drawing
end
elseif chord == 'C-d' and not love.mouse.isDown('1') then
local drawing,i,p = Drawing.select_point_at_mouse()
if drawing then

View File

@ -48,15 +48,25 @@ function load_drawing(infile_next_line)
assert(line)
if line == '```' then break end
local shape = json.decode(line)
if shape.mode == 'line' or shape.mode == 'manhattan' then
if shape.mode == 'freehand' then
-- no changes needed
elseif shape.mode == 'line' or shape.mode == 'manhattan' then
local name = shape.p1.name
shape.p1 = Drawing.insert_point(drawing.points, shape.p1.x, shape.p1.y)
drawing.points[shape.p1].name = name
name = shape.p2.name
shape.p2 = Drawing.insert_point(drawing.points, shape.p2.x, shape.p2.y)
drawing.points[shape.p2].name = name
elseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' then
for i,p in ipairs(shape.vertices) do
local name = p.name
shape.vertices[i] = Drawing.insert_point(drawing.points, p.x,p.y)
drawing.points[shape.vertices[i]].name = name
end
elseif shape.mode == 'circle' or shape.mode == 'arc' then
local name = shape.center.name
shape.center = Drawing.insert_point(drawing.points, shape.center.x,shape.center.y)
drawing.point[shape.center].name = name
else
print(shape.mode)
assert(false)

View File

@ -7,6 +7,8 @@ function draw_help_without_mouse_pressed(drawing)
y = y + math.floor(15*Zoom)
love.graphics.print("* Hover on a point and press 'ctrl+v' to start moving it,", 16+30,y, 0, Zoom)
y = y + math.floor(15*Zoom)
love.graphics.print("* Hover on a point and press 'ctrl+n' to name it,", 16+30,y, 0, Zoom)
y = y + math.floor(15*Zoom)
love.graphics.print("then press the mouse button to finish", 16+30+bullet_indent(),y, 0, Zoom)
y = y + math.floor(15*Zoom)
love.graphics.print("* Hover on a point or shape and press 'ctrl+d' to delete it", 16+30,y, 0, Zoom)

View File

@ -167,6 +167,17 @@ function love.mousereleased(x,y, button)
Drawing.mouse_released(x,y, button)
end
function love.textinput(t)
if Current_drawing_mode == 'name' then
local drawing = Lines.current
local p = drawing.points[drawing.pending.target_point]
p.name = p.name..t
else
Text.textinput(t)
end
save_to_disk(Lines, Filename)
end
function keychord_pressed(chord)
if love.mouse.isDown('1') or chord:sub(1,2) == 'C-' then
Drawing.keychord_pressed(chord)
@ -175,6 +186,22 @@ function keychord_pressed(chord)
if drawing then
drawing.pending = {}
end
elseif Current_drawing_mode == 'name' then
if chord == 'return' then
Current_drawing_mode = Previous_drawing_mode
Previous_drawing_mode = nil
else
local drawing = Lines.current
local p = drawing.points[drawing.pending.target_point]
if chord == 'escape' then
p.name = nil
elseif chord == 'backspace' then
local len = utf8.len(p.name)
local byte_offset = utf8.offset(p.name, len-1)
p.name = string.sub(p.name, 1, byte_offset)
end
end
save_to_disk(Lines, Filename)
elseif chord == 'pagedown' then
Screen_top_line = Screen_bottom_line
Cursor_line = Screen_top_line

View File

@ -15,6 +15,7 @@ backspace
drawing
draw a line, circle, rectangle, square, polygon
select a point and move it
select a point and name it
enter
cursor_pos == 0 -> insert empty line above current line
@ -33,3 +34,4 @@ scrolling:
persistence:
draw a line, circle, rectangle, square, polygon, quit, restart. All the shapes you drew should still be visible.
select a point and name it, quit, restart. Name is still visible.

View File

@ -102,11 +102,9 @@ function Text.compute_fragments(line, line_width)
end
end
function love.textinput(t)
function Text.textinput(t)
if love.mouse.isDown('1') then return end
if Lines[Cursor_line].mode == 'drawing' then return end
Text.insert_at_cursor(t)
save_to_disk(Lines, Filename)
end
function Text.insert_at_cursor(t)