about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-06-03 14:30:51 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-06-03 14:30:51 -0700
commit1bdb93157557236cab566dce5b4613672cc5b626 (patch)
treeb7c48719e6c5ac44aa6ec434f23345cddd04ce68
parent0d52962b3e6f91e458fbf858e62e684494373af8 (diff)
downloadtext.love-1bdb93157557236cab566dce5b4613672cc5b626.tar.gz
extract a function
-rw-r--r--select.lua24
-rw-r--r--text.lua21
2 files changed, 25 insertions, 20 deletions
diff --git a/select.lua b/select.lua
index fcef878..499c309 100644
--- a/select.lua
+++ b/select.lua
@@ -50,6 +50,30 @@ function Text.clip_selection(line_index, apos, bpos)
   end
 end
 
+-- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
+function Text.draw_highlight(line, x,y, pos, lo,hi)
+  if lo then
+    local lo_offset = utf8.offset(line.data, lo)
+    local hi_offset = utf8.offset(line.data, hi)
+    local pos_offset = utf8.offset(line.data, pos)
+    local lo_px
+    if pos == lo then
+      lo_px = 0
+    else
+      local before = line.data:sub(pos_offset, lo_offset-1)
+      local before_text = App.newText(love.graphics.getFont(), before)
+      lo_px = App.width(before_text)
+    end
+--?     print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
+    local s = line.data:sub(lo_offset, hi_offset-1)
+    local text = App.newText(love.graphics.getFont(), s)
+    local text_width = App.width(text)
+    love.graphics.setColor(0.7,0.7,0.9)
+    love.graphics.rectangle('fill', x+lo_px,y, text_width,Line_height)
+    love.graphics.setColor(0,0,0)
+  end
+end
+
 -- inefficient for some reason, so don't do it on every frame
 function Text.mouse_pos()
   local time = love.timer.getTime()
diff --git a/text.lua b/text.lua
index e57a019..1398896 100644
--- a/text.lua
+++ b/text.lua
@@ -52,26 +52,7 @@ function Text.draw(line, line_width, line_index)
     if Text.le1(Screen_top1, {line=line_index, pos=pos}) then
       if Selection1.line then
         local lo, hi = Text.clip_selection(line_index, pos, pos+frag_len)
-        if lo then
-          local lo_offset = utf8.offset(line.data, lo)
-          local hi_offset = utf8.offset(line.data, hi)
-          local pos_offset = utf8.offset(line.data, pos)
-          local lo_px
-          if pos == lo then
-            lo_px = 0
-          else
-            local before = line.data:sub(pos_offset, lo_offset-1)
-            local before_text = App.newText(love.graphics.getFont(), before)
-            lo_px = App.width(before_text)
-          end
---?           print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
-          local s = line.data:sub(lo_offset, hi_offset-1)
-          local text = App.newText(love.graphics.getFont(), s)
-          local text_width = App.width(text)
-          love.graphics.setColor(0.7,0.7,0.9)
-          love.graphics.rectangle('fill', x+lo_px,y, text_width,Line_height)
-          love.graphics.setColor(0,0,0)
-        end
+        Text.draw_highlight(line, x,y, pos, lo,hi)
       end
 --?       print('drawing '..frag)
       App.screen.draw(frag_text, x,y)