about summary refs log tree commit diff stats
path: root/source_edit.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-06-04 12:33:23 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-06-04 12:33:23 -0700
commit637e28f300def4172459dd1d6fdf6efd53200ea8 (patch)
treeb53422ef58db46b96564b83b468e1689a1a51fba /source_edit.lua
parent9656e137742eb442e9ce013dd3f25cf6df8c9fad (diff)
downloadlines.love-637e28f300def4172459dd1d6fdf6efd53200ea8.tar.gz
port inscript's bugfix to source editor
Diffstat (limited to 'source_edit.lua')
-rw-r--r--source_edit.lua45
1 files changed, 33 insertions, 12 deletions
diff --git a/source_edit.lua b/source_edit.lua
index 58b1ccc..565b989 100644
--- a/source_edit.lua
+++ b/source_edit.lua
@@ -76,8 +76,6 @@ function edit.initialize_state(top, left, right, font_height, line_height)  -- c
     old_cursor1 = nil,
     old_selection1 = nil,
     mousepress_shift = nil,
-    -- when selecting text, avoid recomputing some state on every single frame
-    recent_mouse = {},
 
     -- cursor coordinates in pixels
     cursor_x = 0,
@@ -155,12 +153,13 @@ function edit.draw(State, hide_cursor)
   State.cursor_x = nil
   State.cursor_y = nil
   local y = State.top
+  local screen_bottom1 = {line=nil, pos=nil}
 --?   print('== draw')
   for line_index = State.screen_top1.line,#State.lines do
     local line = State.lines[line_index]
 --?     print('draw:', y, line_index, line)
     if y + State.line_height > App.screen.height then break end
-    State.screen_bottom1 = {line=line_index, pos=nil}
+    screen_bottom1.line = line_index
     if line.mode == 'text' then
 --?       print('text.draw', y, line_index)
       local startpos = 1
@@ -183,7 +182,7 @@ function edit.draw(State, hide_cursor)
                      end,
         })
       end
-      y, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor)
+      y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor)
 --?       print('=> y', y)
     elseif line.mode == 'drawing' then
       y = y+Drawing_padding_top
@@ -194,6 +193,7 @@ function edit.draw(State, hide_cursor)
       assert(false)
     end
   end
+  State.screen_bottom1 = screen_bottom1
   if State.search_term then
     Text.draw_search_bar(State)
   end
@@ -224,12 +224,23 @@ end
 
 function edit.mouse_press(State, x,y, mouse_button)
   if State.search_term then return end
---?   print('press', State.cursor1.line)
+--?   print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
   if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
     -- press on a button and it returned 'true' to short-circuit
     return
   end
 
+  if y < State.top then
+    State.old_cursor1 = State.cursor1
+    State.old_selection1 = State.selection1
+    State.mousepress_shift = App.shift_down()
+    State.selection1 = {
+        line=State.screen_top1.line,
+        pos=State.screen_top1.pos,
+    }
+    return
+  end
+
   for line_index,line in ipairs(State.lines) do
     if line.mode == 'text' then
       if Text.in_line(State, line_index, x,y) then
@@ -242,6 +253,7 @@ function edit.mouse_press(State, x,y, mouse_button)
         --  press and hold to start a selection: sets selection on press, cursor on release
         --  press and hold, then press shift: ignore shift
         --    i.e. mouse_release should never look at shift state
+--?         print_and_log(('edit.mouse_press: in line %d'):format(line_index))
         State.old_cursor1 = State.cursor1
         State.old_selection1 = State.selection1
         State.mousepress_shift = App.shift_down()
@@ -249,8 +261,7 @@ function edit.mouse_press(State, x,y, mouse_button)
             line=line_index,
             pos=Text.to_pos_on_line(State, line_index, x, y),
         }
---?         print('selection', State.selection1.line, State.selection1.pos)
-        break
+        return
       end
     elseif line.mode == 'drawing' then
       local line_cache = State.line_cache[line_index]
@@ -259,15 +270,24 @@ function edit.mouse_press(State, x,y, mouse_button)
         State.lines.current_drawing = line
         Drawing.before = snapshot(State, line_index)
         Drawing.mouse_press(State, line_index, x,y, mouse_button)
-        break
+        return
       end
     end
   end
+
+  -- still here? click is below all screen lines
+  State.old_cursor1 = State.cursor1
+  State.old_selection1 = State.selection1
+  State.mousepress_shift = App.shift_down()
+  State.selection1 = {
+      line=State.screen_bottom1.line,
+      pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
+  }
 end
 
 function edit.mouse_release(State, x,y, mouse_button)
   if State.search_term then return end
---?   print('release', State.cursor1.line)
+--?   print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
   if State.lines.current_drawing then
     Drawing.mouse_release(State, x,y, mouse_button)
     schedule_save(State)
@@ -276,15 +296,16 @@ function edit.mouse_release(State, x,y, mouse_button)
       Drawing.before = nil
     end
   else
+--?     print_and_log('edit.mouse_release: no current drawing')
     for line_index,line in ipairs(State.lines) do
       if line.mode == 'text' then
         if Text.in_line(State, line_index, x,y) then
---?           print('reset selection')
+--?           print_and_log(('edit.mouse_release: in line %d'):format(line_index))
           State.cursor1 = {
               line=line_index,
               pos=Text.to_pos_on_line(State, line_index, x, y),
           }
---?           print('cursor', State.cursor1.line, State.cursor1.pos)
+--?           print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
           if State.mousepress_shift then
             if State.old_selection1.line == nil then
               State.selection1 = State.old_cursor1
@@ -300,7 +321,7 @@ function edit.mouse_release(State, x,y, mouse_button)
         end
       end
     end
---?     print('selection:', State.selection1.line, State.selection1.pos)
+--?     print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos))
   end
 end