about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-17 20:29:08 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-17 20:29:08 -0700
commit476bbcdadf5bf1980398e1ee9af0e7251e25367f (patch)
treeeaeb7ac523fe0e71f37eb8f07c2c84139cb46314 /text.lua
parent40b1780bcab572ac4088343405b7a9c25500a0b3 (diff)
downloadlines.love-476bbcdadf5bf1980398e1ee9af0e7251e25367f.tar.gz
beginnings of a module for the text editor
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua54
1 files changed, 54 insertions, 0 deletions
diff --git a/text.lua b/text.lua
new file mode 100644
index 0000000..a9cfcbd
--- /dev/null
+++ b/text.lua
@@ -0,0 +1,54 @@
+-- primitives for editing text
+Text = {}
+
+function Text.draw(line, line_index, cursor_line, y, cursor_pos)
+  love.graphics.setColor(0,0,0)
+  local love_text = love.graphics.newText(love.graphics.getFont(), line.data)
+  love.graphics.draw(love_text, 25,y, 0, Zoom)
+  if line_index == cursor_line then
+    -- cursor
+    love.graphics.print('_', Text.cursor_x(line.data, cursor_pos), y+6)  -- drop the cursor down a bit to account for the increased font size
+  end
+end
+
+function Text.nearest_cursor_pos(line, x, hint)
+  if x == 0 then
+    return 1
+  end
+  local max_x = Text.cursor_x(line, #line+1)
+  if x > max_x then
+    return #line+1
+  end
+  local currx = Text.cursor_x(line, hint)
+  if currx > x-2 and currx < x+2 then
+    return hint
+  end
+  local left, right = 1, #line+1
+  if currx > x then
+    right = hint
+  else
+    left = hint
+  end
+  while left < right-1 do
+    local curr = math.floor((left+right)/2)
+    local currxmin = Text.cursor_x(line, curr)
+    local currxmax = Text.cursor_x(line, curr+1)
+    if currxmin <= x and x < currxmax then
+      return curr
+    end
+    if currxmin > x then
+      right = curr
+    else
+      left = curr
+    end
+  end
+  return right
+end
+
+function Text.cursor_x(line_data, cursor_pos)
+  local line_before_cursor = line_data:sub(1, cursor_pos-1)
+  local text_before_cursor = love.graphics.newText(love.graphics.getFont(), line_before_cursor)
+  return 25+text_before_cursor:getWidth()*Zoom
+end
+
+return Text