about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-21 14:03:06 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-21 14:03:06 -0700
commitbb9e23a6385675fa073ec187ba84d3fd8636dfc2 (patch)
tree957d2a6072f4a634e2877ed216a67ffd9a6c522e
parent96df1874883e9d9700be394df6bcafdf9e3ad4b7 (diff)
downloadtext.love-bb9e23a6385675fa073ec187ba84d3fd8636dfc2.tar.gz
support for naming points
There's still an absence of affordance showing when you're in naming mode.
-rw-r--r--README.md3
-rw-r--r--drawing.lua16
-rw-r--r--file.lua12
-rw-r--r--help.lua2
-rw-r--r--main.lua27
-rw-r--r--manual_tests2
-rw-r--r--text.lua4
7 files changed, 62 insertions, 4 deletions
diff --git a/README.md b/README.md
index 1203f51..6e7428e 100644
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/drawing.lua b/drawing.lua
index 28794a3..611a522 100644
--- a/drawing.lua
+++ b/drawing.lua
@@ -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
diff --git a/file.lua b/file.lua
index 932d2c2..1087f55 100644
--- a/file.lua
+++ b/file.lua
@@ -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)
diff --git a/help.lua b/help.lua
index 6447e2a..ffae5e9 100644
--- a/help.lua
+++ b/help.lua
@@ -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)
diff --git a/main.lua b/main.lua
index 63f80e3..ae6f0b2 100644
--- a/main.lua
+++ b/main.lua
@@ -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
diff --git a/manual_tests b/manual_tests
index d16758f..77d4b54 100644
--- a/manual_tests
+++ b/manual_tests
@@ -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.
diff --git a/text.lua b/text.lua
index 8888644..992b449 100644
--- a/text.lua
+++ b/text.lua
@@ -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)