about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-09-05 11:28:03 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-09-05 11:29:39 -0700
commit528c64d690c2d1cb4b3b70c37008b3ba37d904b9 (patch)
tree6a05d6d291276b5a3c63475686e02648f3091da6 /text.lua
parent9f94470f9dd56c03c66d98bbeff2fc60995cc0ae (diff)
downloadlines.love-528c64d690c2d1cb4b3b70c37008b3ba37d904b9.tar.gz
support drawings in the source editor
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua39
1 files changed, 22 insertions, 17 deletions
diff --git a/text.lua b/text.lua
index c2c633b..6be260d 100644
--- a/text.lua
+++ b/text.lua
@@ -115,7 +115,7 @@ function Text.compute_fragments(State, line_index)
   for frag in line.data:gmatch('%S*%s*') do
     local frag_text = App.newText(love.graphics.getFont(), frag)
     local frag_width = App.width(frag_text)
---?     print('x: '..tostring(x)..'; '..tostring(State.right-x)..'px to go')
+--?     print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go')
     while x + frag_width > State.right do
 --?       print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x))
       if (x-State.left) < 0.8 * (State.right-State.left) then
@@ -365,8 +365,7 @@ function Text.insert_return(State)
   table.insert(State.line_cache, State.cursor1.line+1, {})
   State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
   Text.clear_screen_line_cache(State, State.cursor1.line)
-  State.cursor1.line = State.cursor1.line+1
-  State.cursor1.pos = 1
+  State.cursor1 = {line=State.cursor1.line+1, pos=1}
 end
 
 function Text.pageup(State)
@@ -386,8 +385,7 @@ function Text.pageup(State)
     top2 = Text.previous_screen_line(State, top2)
   end
   State.screen_top1 = Text.to1(State, top2)
-  State.cursor1.line = State.screen_top1.line
-  State.cursor1.pos = State.screen_top1.pos
+  State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
   Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
 --?   print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
 --?   print('pageup end')
@@ -406,12 +404,10 @@ function Text.pagedown(State)
   if Text.lt1(State.screen_top1, new_top1) then
     State.screen_top1 = new_top1
   else
-    State.screen_top1.line = State.screen_bottom1.line
-    State.screen_top1.pos = State.screen_bottom1.pos
+    State.screen_top1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
   end
 --?   print('setting top to', State.screen_top1.line, State.screen_top1.pos)
-  State.cursor1.line = State.screen_top1.line
-  State.cursor1.pos = State.screen_top1.pos
+  State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
   Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
 --?   print('top now', State.screen_top1.line)
   Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
@@ -430,7 +426,7 @@ function Text.up(State)
       new_cursor_line = new_cursor_line-1
       if State.lines[new_cursor_line].mode == 'text' then
 --?         print('found previous text line')
-        State.cursor1.line = new_cursor_line
+        State.cursor1 = {line=State.cursor1.line-1, pos=nil}
         Text.populate_screen_line_starting_pos(State, State.cursor1.line)
         -- previous text line found, pick its final screen line
 --?         print('has multiple screen lines')
@@ -469,8 +465,10 @@ function Text.down(State)
     while new_cursor_line < #State.lines do
       new_cursor_line = new_cursor_line+1
       if State.lines[new_cursor_line].mode == 'text' then
-        State.cursor1.line = new_cursor_line
-        State.cursor1.pos = Text.nearest_cursor_pos(State.lines[State.cursor1.line].data, State.cursor_x, State.left)
+        State.cursor1 = {
+          line = new_cursor_line,
+          pos = Text.nearest_cursor_pos(State.lines[new_cursor_line].data, State.cursor_x, State.left),
+        }
 --?         print(State.cursor1.pos)
         break
       end
@@ -486,6 +484,7 @@ function Text.down(State)
     local scroll_down = Text.le1(State.screen_bottom1, State.cursor1)
 --?     print('cursor is NOT at final screen line of its line')
     local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
+    Text.populate_screen_line_starting_pos(State, State.cursor1.line)
     local new_screen_line_starting_pos = State.line_cache[State.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))
     local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
@@ -582,8 +581,10 @@ function Text.left(State)
     while new_cursor_line > 1 do
       new_cursor_line = new_cursor_line-1
       if State.lines[new_cursor_line].mode == 'text' then
-        State.cursor1.line = new_cursor_line
-        State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
+        State.cursor1 = {
+          line = new_cursor_line,
+          pos = utf8.len(State.lines[new_cursor_line].data) + 1,
+        }
         break
       end
     end
@@ -611,8 +612,7 @@ function Text.right_without_scroll(State)
     while new_cursor_line <= #State.lines-1 do
       new_cursor_line = new_cursor_line+1
       if State.lines[new_cursor_line].mode == 'text' then
-        State.cursor1.line = new_cursor_line
-        State.cursor1.pos = 1
+        State.cursor1 = {line=new_cursor_line, pos=1}
         break
       end
     end
@@ -663,8 +663,12 @@ end
 
 -- should never modify State.cursor1
 function Text.snap_cursor_to_bottom_of_screen(State)
+--?   print('to2:', State.cursor1.line, State.cursor1.pos)
   local top2 = Text.to2(State, State.cursor1)
+--?   print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
+  -- slide to start of screen line
   top2.screen_pos = 1  -- start of screen line
+--?   print('snap', State.screen_top1.line, State.screen_top1.pos, State.screen_top1.posB, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
 --?   print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
   local y = App.screen.height - State.line_height
   -- duplicate some logic from love.draw
@@ -694,6 +698,7 @@ function Text.snap_cursor_to_bottom_of_screen(State)
 --?   print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
   State.screen_top1 = Text.to1(State, top2)
 --?   print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
+--?   print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
   Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
 end
 
@@ -853,7 +858,7 @@ function Text.to2(State, loc1)
   if State.lines[loc1.line].mode == 'drawing' then
     return {line=loc1.line, screen_line=1, screen_pos=1}
   end
-  local result = {line=loc1.line, screen_line=1}
+  local result = {line=loc1.line}
   local line_cache = State.line_cache[loc1.line]
   Text.populate_screen_line_starting_pos(State, loc1.line)
   for i=#line_cache.screen_line_starting_pos,1,-1 do