about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-20 05:40:42 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-20 05:40:42 -0700
commit5f2ef2faafa19e095ec5d447d0e24f98c473d31a (patch)
tree7479c5254e951f3351d3abd63538f011a32411f5
parente223182ad55ca79f864c93641664ce85b389582b (diff)
downloadtext.love-5f2ef2faafa19e095ec5d447d0e24f98c473d31a.tar.gz
extract a function
-rw-r--r--main.lua5
-rw-r--r--text.lua16
2 files changed, 9 insertions, 12 deletions
diff --git a/main.lua b/main.lua
index 5c1abd9..19f9b21 100644
--- a/main.lua
+++ b/main.lua
@@ -116,10 +116,7 @@ function love.draw()
                        end
                      end})
           if line_index == Cursor_line then
-            -- cursor
-            love.graphics.setColor(1,0,0)
-            love.graphics.circle('fill', 25,y+math.floor(15*Zoom), 2)
-            love.graphics.setColor(0,0,0)
+            Text.draw_cursor(25, y)
           end
         y = y + math.floor(15*Zoom)  -- text height
       elseif line.mode == 'drawing' then
diff --git a/text.lua b/text.lua
index eac4818..44abc67 100644
--- a/text.lua
+++ b/text.lua
@@ -32,20 +32,14 @@ function Text.draw(line, line_width, line_index, cursor_line, cursor_pos)
     local frag_len = utf8.len(frag)
     if line_index == cursor_line then
       if pos <= cursor_pos and pos + frag_len > cursor_pos then
-        -- cursor
-        love.graphics.setColor(1,0,0)
-        love.graphics.circle('fill', x+Text.cursor_x2(frag, cursor_pos-pos+1),y+math.floor(15*Zoom), 2)
-        love.graphics.setColor(0,0,0)
+        Text.draw_cursor(x+Text.cursor_x2(frag, cursor_pos-pos+1), y)
       end
     end
     x = x + frag_width
     pos = pos + frag_len
   end
   if cursor_pos == pos then
-    -- cursor
-    love.graphics.setColor(1,0,0)
-    love.graphics.circle('fill', x,y+math.floor(15*Zoom), 2)
-    love.graphics.setColor(0,0,0)
+    Text.draw_cursor(x, y)
   end
   return y
 end
@@ -54,6 +48,12 @@ end
 --  short words break on spaces
 --  long words break when they must
 
+function Text.draw_cursor(x, y)
+  love.graphics.setColor(1,0,0)
+  love.graphics.circle('fill', x,y+math.floor(15*Zoom), 2)
+  love.graphics.setColor(0,0,0)
+end
+
 function Text.compute_fragments(line, line_width)
   line.fragments = {}
   local x = 25