about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-21 22:08:13 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-21 22:32:43 -0700
commit84898ed43a80f00f3df5acd069f808e45d85f24d (patch)
treea3d7db9dc9ddfd0008ca02225313a1382a844398
parent96dfe4f669eca35c77f4f2b4458bac8038a0bd84 (diff)
downloadtext.love-84898ed43a80f00f3df5acd069f808e45d85f24d.tar.gz
bugfix: printing the first part of a line at the bottom made it seem non-wrapping
Still lots wrong here.
-rw-r--r--main.lua26
-rw-r--r--text.lua47
2 files changed, 43 insertions, 30 deletions
diff --git a/main.lua b/main.lua
index f6f5cfe..fc27611 100644
--- a/main.lua
+++ b/main.lua
@@ -64,20 +64,22 @@ Zoom = 1.5
 
 Filename = love.filesystem.getUserDirectory()..'/lines.txt'
 
+New_foo = true
+
 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
   -- for testing line wrap
---?   Screen_width = 120
---?   Screen_height = 200
+  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
@@ -116,7 +118,9 @@ function love.draw()
     line.y = nil
   end
   local y = 15
+  if New_foo then print('== draw') end
   for line_index,line in ipairs(Lines) do
+    if New_foo then print('draw:', line_index, y) end
     if y + math.floor(15*Zoom) > Screen_height then break end
     if line_index >= Screen_top1.line then
       Screen_bottom1.line = line_index
@@ -140,12 +144,15 @@ function love.draw()
         Drawing.draw(line)
         y = y + Drawing.pixels(line.h) + 10 -- padding
       else
+        if New_foo then print('text') end
         line.y = y
         y, Screen_bottom1.pos = Text.draw(line, Line_width, line_index)
         y = y + math.floor(15*Zoom)  -- text height
+        if New_foo then print('aa', y) end
       end
     end
   end
+  New_foo = false
 --?   print('screen bottom: '..tostring(Screen_bottom1.pos)..' in '..tostring(Lines[Screen_bottom1.line].data))
 --?   os.exit(1)
 end
@@ -186,6 +193,7 @@ function love.textinput(t)
 end
 
 function keychord_pressed(chord)
+  New_foo = true
   if love.mouse.isDown('1') or chord:sub(1,2) == 'C-' then
     Drawing.keychord_pressed(chord)
   elseif chord == 'escape' and love.mouse.isDown('1') then
diff --git a/text.lua b/text.lua
index 852ff94..8e99c12 100644
--- a/text.lua
+++ b/text.lua
@@ -28,9 +28,7 @@ function Text.draw(line, line_width, line_index)
       assert(x > 25)  -- no overfull lines
       if line_index > Screen_top1.line or pos > Screen_top1.pos then
         y = y + math.floor(15*Zoom)
-        if y + math.floor(15*Zoom) > Screen_height then
-          return y, screen_line_starting_pos
-        end
+        if New_foo then print('text: new screen line', y, Screen_height, screen_line_starting_pos) end
         screen_line_starting_pos = pos
         if Debug_new_render then print('y', y) end
       end
@@ -40,6 +38,11 @@ function Text.draw(line, line_width, line_index)
       else
         table.insert(line.screen_line_starting_pos, pos)
       end
+      if line_index > Screen_top1.line or pos > Screen_top1.pos then
+        if y + math.floor(15*Zoom) >= Screen_height then
+          return y, screen_line_starting_pos
+        end
+      end
     end
     if Debug_new_render then print('checking to draw', pos, Screen_top1.pos) end
     if line_index > Screen_top1.line or pos >= Screen_top1.pos then
@@ -279,42 +282,43 @@ function Text.keychord_pressed(chord)
     end
   elseif chord == 'down' then
     assert(Lines[Cursor1.line].mode == 'text')
+    print('down', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)
     if Text.cursor_at_final_screen_line() then
       -- line is done, skip to next text line
---?       print('down: cursor at final screen line of its line')
+      print('cursor at final screen line of its line')
+--?       os.exit(1)
       local new_cursor_line = Cursor1.line
       while new_cursor_line < #Lines do
         new_cursor_line = new_cursor_line+1
         if Lines[new_cursor_line].mode == 'text' then
           Cursor1.line = new_cursor_line
           Cursor1.pos = Text.nearest_cursor_pos(Lines[Cursor1.line].data, Cursor_x)
---?           print(Cursor1.pos)
+          print(Cursor1.pos)
           break
         end
       end
---?       print(Cursor1.line, Cursor1.pos, Screen_bottom1.line)
       if Cursor1.line > Screen_bottom1.line then
---?         print('screen top before:', Screen_top1.line, Screen_top1.pos)
+        print('screen top before:', Screen_top1.line, Screen_top1.pos)
         Screen_top1.line = Cursor1.line
---?         print('scroll up preserving cursor')
+        print('scroll up preserving cursor')
         Text.scroll_up_while_cursor_on_screen()
---?         print('screen top after:', Screen_top1.line, Screen_top1.pos)
+        print('screen top after:', Screen_top1.line, Screen_top1.pos)
       end
---?       print('=>', Cursor1.line, Cursor1.pos, Screen_bottom1.line)
     else
       -- move down one screen line in current line
---?       print('cursor is NOT at final screen line of its line')
+      print('cursor is NOT at final screen line of its line')
       local screen_line_index, screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line()
       new_screen_line_starting_pos = Lines[Cursor1.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))
+      print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
       local s = string.sub(Lines[Cursor1.line].data, new_screen_line_starting_pos)
       Cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1
---?       print('cursor pos is now '..tostring(Cursor1.pos))
+      print('cursor pos is now '..tostring(Cursor1.pos))
       Screen_top1.line = Cursor1.line
---?       print('scroll up preserving cursor')
+      print('scroll up preserving cursor')
       Text.scroll_up_while_cursor_on_screen()
---?       print('screen top after:', Screen_top1.line, Screen_top1.pos)
+      print('screen top after:', Screen_top1.line, Screen_top1.pos)
     end
+    print('=>', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)
   end
 end
 
@@ -336,6 +340,7 @@ function Text.cursor_at_final_screen_line()
     return true
   end
   screen_lines = Lines[Cursor1.line].screen_line_starting_pos
+  print(screen_lines[#screen_lines], Cursor1.pos)
   return screen_lines[#screen_lines] <= Cursor1.pos
 end
 
@@ -358,13 +363,13 @@ function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necess
 end
 
 function Text.scroll_up_while_cursor_on_screen()
-  local cursor_pos_screen_lines = Text.pos_at_start_of_cursor_screen_line()
---?   print('cursor pos '..tostring(Cursor1.pos)..' is on the #'..tostring(cursor_pos_screen_lines)..' screen line down')
-  local y = Screen_height - cursor_pos_screen_lines*math.floor(15*Zoom)
+  local cursor2 = Text.to2(Cursor1)
+  print('cursor pos '..tostring(Cursor1.pos)..' is on the #'..tostring(cursor2.screen_line)..' screen line down')
+  local y = Screen_height - cursor2.screen_pos*math.floor(15*Zoom)
   -- duplicate some logic from love.draw
   while true do
     if Screen_top1.line == 1 then break end
---?     print('y', y)
+    print('y', y)
     local h = 0
     if Lines[Screen_top1.line-1].mode == 'drawing' then
       h = 20 + Drawing.pixels(Lines[Screen_top1.line-1].h)
@@ -374,7 +379,7 @@ function Text.scroll_up_while_cursor_on_screen()
       local n = #Lines[Screen_top1.line-1].screen_line_starting_pos
       h = h + n*math.floor(15*Zoom)  -- text height
     end
---?     print('height:', h)
+    print('height:', h)
     if y - h < 0 then
       break
     end
@@ -482,7 +487,7 @@ end
 
 function Text.to2(pos1)
   local result = {line=pos1.line, screen_line=1}
-  if Line[pos1.line].screen_line_starting_pos == nil then
+  if Lines[pos1.line].screen_line_starting_pos == nil then
     result.screen_pos = pos1.pos
   else
     for i=#Lines[pos1.line].screen_line_starting_pos,1,-1 do