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-18 18:19:27 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-18 18:19:27 -0700
commitec410d5223fc7a30f69571d5a7e0bbffc26ede6b (patch)
treedd6f15deec82b2526a4f9084a468b4c8df638a61 /text.lua
parentb586c7332e9e270af5b0ea1b801d63f7a6e6c630 (diff)
downloadtext.love-ec410d5223fc7a30f69571d5a7e0bbffc26ede6b.tar.gz
bugfix: ensure Cursor_line is always on a text line
Manual test used here:

  abc
  ```lines
  {"p1":{"y":72,"x":82},"mode":"line","p2":{"y":29,"x":169}}
  ```
  def
  ```lines
  {"p1":{"y":36,"x":56},"mode":"line","p2":{"y":59,"x":163}}
  ```
  ```lines
  ```
  ghi
  jkl

Hitting page-down moves the cursor from abc to ghi. The 'ghi' line
should be fully visible on screen.
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua33
1 files changed, 33 insertions, 0 deletions
diff --git a/text.lua b/text.lua
index f7ffb63..af205b6 100644
--- a/text.lua
+++ b/text.lua
@@ -155,6 +155,39 @@ function Text.keychord_pressed(chord)
   end
 end
 
+function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary()
+  while Cursor_line <= #Lines do
+    if Lines[Cursor_line].mode == 'text' then
+      break
+    end
+    Cursor_line = Cursor_line + 1
+  end
+  -- hack: insert a text line at bottom of file if necessary
+  if Cursor_line > #Lines then
+    assert(Cursor_line == #Lines+1)
+    table.insert(Lines, {mode='text', data=''})
+  end
+  if Cursor_line > Screen_bottom_line then
+    Screen_top_line = Cursor_line
+    Text.scroll_up_while_cursor_on_screen()
+  end
+end
+
+function Text.scroll_up_while_cursor_on_screen()
+  local y = Screen_height - 15*Zoom -- for Cursor_line
+  while true do
+    if Screen_top_line == 1 then break end
+    y = y - 15*Zoom
+    if Lines[Screen_top_line].mode == 'drawing' then
+      y = y - Drawing.pixels(Lines[Screen_top_line].h)
+    end
+    if y < 15*Zoom then
+      break
+    end
+    Screen_top_line = Screen_top_line - 1
+  end
+end
+
 function Text.in_line(line, x,y)
   if line.y == nil then return false end  -- outside current page
   return x >= 16 and y >= line.y and y < line.y+15*Zoom