From ec410d5223fc7a30f69571d5a7e0bbffc26ede6b Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 18 May 2022 18:19:27 -0700 Subject: [PATCH] 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. --- README.md | 8 ++++++++ main.lua | 7 ++++--- text.lua | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7c6ddf7..240b9a6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.lua b/main.lua index a8cfc57..a23f426 100644 --- a/main.lua +++ b/main.lua @@ -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 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