From 1326914d7bda65a5791c2121dae6d3987a907994 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 4 Jun 2022 14:14:26 -0700 Subject: [PATCH] select text with shift + mouseclick It's still a bit simple-minded. Most software will keep the first bound fixed and move the second. Lines currently has the bounds in a queue of sorts. But I have a test to indicate the behavior that is definitely desired. We'll see if we need it to get more complex. --- app.lua | 16 ++++++++++++++++ keychord.lua | 10 +++++----- main.lua | 10 ++++++++-- text_tests.lua | 28 ++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/app.lua b/app.lua index 99c1cd2..b00506b 100644 --- a/app.lua +++ b/app.lua @@ -186,6 +186,18 @@ function App.setClipboardText(s) App.clipboard = s end +App.modifier_keys = {} +function App.keypress(key) + App.modifier_keys[key] = true +end +function App.keyrelease(key) + App.modifier_keys[key] = nil +end + +function App.modifier_down(key) + return App.modifier_keys[key] +end + function App.run_after_textinput(t) App.textinput(t) App.screen.contents = {} @@ -267,6 +279,9 @@ function App.disable_tests() App.filesystem = nil App.run_after_textinput = nil App.run_after_keychord = nil + App.keypress = nil + App.keyrelease = nil + App.modifier_keys = nil -- other methods dispatch to real hardware App.screen.print = love.graphics.print App.newText = love.graphics.newText @@ -275,4 +290,5 @@ function App.disable_tests() App.open_for_writing = function(filename) return io.open(filename, 'w') end App.getClipboardText = love.system.getClipboardText App.setClipboardText = love.system.setClipboardText + App.modifier_down = love.keyboard.isDown end diff --git a/keychord.lua b/keychord.lua index 4cdee2b..31db16d 100644 --- a/keychord.lua +++ b/keychord.lua @@ -28,24 +28,24 @@ function App.combine_modifiers(key) return result end -function App.modifier_down() +function App.any_modifier_down() return App.ctrl_down() or App.alt_down() or App.shift_down() or App.cmd_down() end function App.ctrl_down() - return love.keyboard.isDown('lctrl') or love.keyboard.isDown('rctrl') + return App.modifier_down('lctrl') or App.modifier_down('rctrl') end function App.alt_down() - return love.keyboard.isDown('lalt') or love.keyboard.isDown('ralt') + return App.modifier_down('lalt') or App.modifier_down('ralt') end function App.shift_down() - return love.keyboard.isDown('lshift') or love.keyboard.isDown('rshift') + return App.modifier_down('lshift') or App.modifier_down('rshift') end function App.cmd_down() - return love.keyboard.isDown('lgui') or love.keyboard.isDown('rgui') + return App.modifier_down('lgui') or App.modifier_down('rgui') end array = {} diff --git a/main.lua b/main.lua index 3e60d19..3801a3f 100644 --- a/main.lua +++ b/main.lua @@ -196,9 +196,14 @@ function App.mousepressed(x,y, mouse_button) for line_index,line in ipairs(Lines) do if line.mode == 'text' then if Text.in_line(line, x,y) then + if App.shift_down() then + Selection1 = {line=Cursor1.line, pos=Cursor1.pos} + end Cursor1.line = line_index Cursor1.pos = Text.to_pos_on_line(line, x, y) - Selection1 = {line=Cursor1.line, pos=Cursor1.pos} + if not App.shift_down() then + Selection1 = {line=Cursor1.line, pos=Cursor1.pos} + end end elseif line.mode == 'drawing' then if Drawing.in_drawing(line, x, y) then @@ -218,12 +223,13 @@ function App.mousereleased(x,y, button) if Text.in_line(line, x,y) then Cursor1.line = line_index Cursor1.pos = Text.to_pos_on_line(line, x, y) - if Text.eq1(Cursor1, Selection1) then + if Text.eq1(Cursor1, Selection1) and not App.shift_down() then Selection1 = {} end end end end +--? print('select:', Selection1.line, Selection1.pos) end end diff --git a/text_tests.lua b/text_tests.lua index 91b3b46..8272faf 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -159,11 +159,39 @@ function test_move_cursor_using_mouse() Cursor1 = {line=1, pos=1} Screen_top1 = {line=1, pos=1} Screen_bottom1 = {} + Selection1 = {} App.draw() -- populate line.y for each line in Lines local screen_left_margin = 25 -- pixels App.run_after_mouserelease(screen_left_margin+8,Margin_top+5, '1') check_eq(Cursor1.line, 1, 'F - test_move_cursor_using_mouse/cursor:line') check_eq(Cursor1.pos, 2, 'F - test_move_cursor_using_mouse/cursor:pos') + check_nil(Selection1.line, 'F - test_move_cursor_using_mouse/selection:line') + check_nil(Selection1.pos, 'F - test_move_cursor_using_mouse/selection:pos') +end + +function test_select_text_using_mouse() + io.write('\ntest_select_text_using_mouse') + App.screen.init{width=50, height=60} + Lines = load_array{'abc', 'def', 'xyz'} + Line_width = App.screen.width + Cursor1 = {line=1, pos=1} + Screen_top1 = {line=1, pos=1} + Screen_bottom1 = {} + Selection1 = {} + App.draw() -- populate line.y for each line in Lines + local screen_left_margin = 25 -- pixels + -- click on first location + App.run_after_mousepress(screen_left_margin+8,Margin_top+5, '1') + App.run_after_mouserelease(screen_left_margin+8,Margin_top+5, '1') + -- hold down shift and click somewhere else + App.keypress('lshift') + App.run_after_mousepress(screen_left_margin+20,Margin_top+5, '1') + App.run_after_mouserelease(screen_left_margin+20,Margin_top+Line_height+5, '1') + App.keyrelease('lshift') + check_eq(Cursor1.line, 2, 'F - test_select_text_using_mouse/cursor:line') + check_eq(Cursor1.pos, 4, 'F - test_select_text_using_mouse/cursor:pos') + check_eq(Selection1.line, 1, 'F - test_select_text_using_mouse/selection:line') + check_eq(Selection1.pos, 2, 'F - test_select_text_using_mouse/selection:pos') end function test_pagedown()