diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2023-06-04 15:11:24 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2023-06-04 15:11:24 -0700 |
commit | bf2c2555d662865b94256e6e854acc0715fda6d1 (patch) | |
tree | 5c444b3e2abe7c3b220e4c03c09ece70a9e04946 /edit.lua | |
parent | 074f4d9b0a51dc99df9183a45a9c2301f575b35b (diff) | |
parent | 637e28f300def4172459dd1d6fdf6efd53200ea8 (diff) | |
download | text.love-bf2c2555d662865b94256e6e854acc0715fda6d1.tar.gz |
Merge lines.love
Diffstat (limited to 'edit.lua')
-rw-r--r-- | edit.lua | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/edit.lua b/edit.lua index 45aa879..3c7a5ad 100644 --- a/edit.lua +++ b/edit.lua @@ -43,8 +43,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, @@ -103,20 +101,22 @@ function edit.draw(State) 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 --? print('text.draw', y, line_index) local startpos = 1 if line_index == State.screen_top1.line then startpos = State.screen_top1.pos end - y, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos) + y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos) --? print('=> y', y) end + State.screen_bottom1 = screen_bottom1 if State.search_term then Text.draw_search_bar(State) end @@ -146,7 +146,18 @@ 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 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 Text.in_line(State, line_index, x,y) then -- delicate dance between cursor, selection and old cursor/selection @@ -157,7 +168,8 @@ function edit.mouse_press(State, x,y, mouse_button) -- sets cursor -- 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_released should never look at shift state + -- 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() @@ -165,23 +177,31 @@ 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 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(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos)) for line_index,line in ipairs(State.lines) do 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 @@ -196,7 +216,7 @@ function edit.mouse_release(State, x,y, mouse_button) break 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 function edit.mouse_wheel_move(State, dx,dy) |