diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2022-05-20 16:41:21 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2022-05-20 16:56:04 -0700 |
commit | c28f6ba031b041818f751564a5c7b2d9dc343e06 (patch) | |
tree | a1f1d9ff12702a6b6e3a7f2fdd1d02bfa1c6baf5 | |
parent | ce94f8bf799342782c9ca28da850ceae452e7a41 (diff) | |
download | text.love-c28f6ba031b041818f751564a5c7b2d9dc343e06.tar.gz |
snapshot - no, that's all wrong
I've been only thinking about up arrow when cursor is at top of screen. Hopefully this is better.
-rw-r--r-- | main.lua | 20 | ||||
-rw-r--r-- | manual_tests | 4 | ||||
-rw-r--r-- | text.lua | 36 |
3 files changed, 43 insertions, 17 deletions
diff --git a/main.lua b/main.lua index f53f990..8c8bf71 100644 --- a/main.lua +++ b/main.lua @@ -38,7 +38,7 @@ require 'icons' -- once, and read them passively thereafter. Lines = {{mode='text', data=''}} Cursor_line = 1 -Cursor_pos = 1 -- in Unicode codepoints, from 1 to utf8.len(line) + 1 +Cursor_pos = 15 -- in Unicode codepoints, from 1 to utf8.len(line) + 1 Screen_width, Screen_height, Screen_flags = 0, 0, nil @@ -47,7 +47,7 @@ Cursor_x, Cursor_y = 0, 0 -- in pixels -- scrolling support Screen_top_line = 1 Screen_bottom_line = 1 -Top_screen_line_starting_pos = 1 -- when top of screen starts in between a wrapped line +Top_screen_line_starting_pos = 6 -- when top of screen starts in between a wrapped line Bottom_screen_line_starting_pos = 1 -- when bottom of screen starts in between a wrapped line Current_drawing_mode = 'line' @@ -61,15 +61,17 @@ Filename = 'lines.txt' function love.load(arg) -- maximize window - love.window.setMode(0, 0) -- maximize - Screen_width, Screen_height, Screen_flags = love.window.getMode() - -- shrink slightly to account for window decoration - Screen_width = Screen_width-100 - Screen_height = Screen_height-100 +--? love.window.setMode(0, 0) -- maximize +--? Screen_width, Screen_height, Screen_flags = love.window.getMode() +--? -- shrink slightly to account for window decoration +--? Screen_width = Screen_width-100 +--? Screen_height = Screen_height-100 + Screen_width = 120 + Screen_height = 200 love.window.setMode(Screen_width, Screen_height) love.window.setTitle('Text with Lines') ---? Line_width = 100 - Line_width = math.floor(Screen_width/2/40)*40 + Line_width = 100 +--? Line_width = math.floor(Screen_width/2/40)*40 love.keyboard.setTextInput(true) -- bring up keyboard on touch screen love.keyboard.setKeyRepeat(true) if #arg > 0 then diff --git a/manual_tests b/manual_tests index bdb35db..eb0936a 100644 --- a/manual_tests +++ b/manual_tests @@ -22,7 +22,7 @@ scrolling: cursor remains on screen cursor remains on text line 'up' arrow with cursor at top of screen scrolls up one line (drawings still fully in or out) - if top line wrapped before, it scrolls up by only one screen line + if cursor line wrapped before, it scrolls up by only one screen line if previous line (above top of screen) wrapped, it scrolls up by only one screen line 'down' arrow with cursor at bottom of screen scrolls down one line (drawings still fully in or out) - if top line wrapped before, it scrolls down by only one screen line + if cursor line wrapped before, it scrolls down by only one screen line diff --git a/text.lua b/text.lua index f0c8dfe..b24db96 100644 --- a/text.lua +++ b/text.lua @@ -219,7 +219,9 @@ function Text.keychord_pressed(chord) save_to_disk(Lines, Filename) elseif chord == 'up' then assert(Lines[Cursor_line].mode == 'text') - if Top_screen_line_starting_pos == 1 then + local screen_line_index,screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line() + if screen_line_starting_pos == 1 then + print('cursor is at first screen line of its line') -- top line is done; skip to previous text line local new_cursor_line = Cursor_line while new_cursor_line > 1 do @@ -233,7 +235,10 @@ function Text.keychord_pressed(chord) end local screen_line_starting_pos = Lines[Cursor_line].screen_line_starting_pos screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos] - Top_screen_line_starting_pos = screen_line_starting_pos + print('previous screen line starts at '..tostring(screen_line_starting_pos)..' of its line') + if Screen_top_line == Cursor_line and Top_screen_line_starting_pos == screen_line_starting_pos then + Top_screen_line_starting_pos = screen_line_starting_pos + end local s = string.sub(Lines[Cursor_line].data, screen_line_starting_pos) Cursor_pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, old_x) - 1 break @@ -244,11 +249,17 @@ function Text.keychord_pressed(chord) end else -- scroll up just one screen line in current line - local screen_line_index = table.find(Lines[Cursor_line].screen_line_starting_pos, Top_screen_line_starting_pos) + print('cursor is NOT at first screen line of its line') assert(screen_line_index > 1) - Top_screen_line_starting_pos = Lines[Cursor_line].screen_line_starting_pos[screen_line_index-1] - local s = string.sub(Lines[Cursor_line].data, Top_screen_line_starting_pos) - Cursor_pos = Text.nearest_cursor_pos(s, Cursor_x) + new_screen_line_starting_pos = Lines[Cursor_line].screen_line_starting_pos[screen_line_index-1] + print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos)) + if Screen_top_line == Cursor_line and Top_screen_line_starting_pos == screen_line_starting_pos then + Top_screen_line_starting_pos = new_screen_line_starting_pos + print('also setting pos of top of screen to '..tostring(Top_screen_line_starting_pos)) + end + local s = string.sub(Lines[Cursor_line].data, new_screen_line_starting_pos) + Cursor_pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1 + print('cursor pos is now '..tostring(Cursor_pos)) end elseif chord == 'down' then assert(Lines[Cursor_line].mode == 'text') @@ -269,6 +280,19 @@ function Text.keychord_pressed(chord) end end +function Text.pos_at_start_of_cursor_screen_line() + if Lines[Cursor_line].screen_line_starting_pos == nil then + return 1,1 + end + for i=#Lines[Cursor_line].screen_line_starting_pos,1,-1 do + local spos = Lines[Cursor_line].screen_line_starting_pos[i] + if spos <= Cursor_pos then + return i,spos + end + end + assert(false) +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 |