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.
This commit is contained in:
Kartik K. Agaram 2022-05-18 18:19:27 -07:00
parent b586c7332e
commit ec410d5223
3 changed files with 45 additions and 3 deletions

View File

@ -3,3 +3,11 @@ Known issues:
On Linux, drags using the light touch get interrupted when a key is pressed.
You'll have to press down to drag.
* No support yet for Unicode graphemes spanning multiple codepoints.
* The text cursor will always stay on the screen. This can have some strange
implications:
* A long series of drawings will get silently skipped when you hit
page-down, until a line of text can be showed on screen.
* If there's no line of text at the bottom of the file, one will be
created.
So far this app isn't really designed for all-drawing files. I'm really just
targeting mostly-text files with a few drawings mixed in.

View File

@ -172,14 +172,14 @@ function keychord_pressed(chord)
Screen_top_line = Screen_bottom_line
Cursor_line = Screen_top_line
Cursor_pos = 1
Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary()
elseif chord == 'pageup' then
-- duplicate some logic from love.draw
local y = Screen_height
while y >= 0 do
if Screen_top_line == 1 then break end
if Lines[Screen_top_line].mode == 'text' then
y = y - 15*Zoom
else
y = y - 15*Zoom
if Lines[Screen_top_line].mode == 'drawing' then
y = y - Drawing.pixels(Lines[Screen_top_line].h)
end
Screen_top_line = Screen_top_line - 1
@ -188,6 +188,7 @@ function keychord_pressed(chord)
Cursor_pos = 1
end
Cursor_line = Screen_top_line
Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary()
else
Text.keychord_pressed(chord)
end

View File

@ -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