diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2022-06-03 13:22:03 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2022-06-03 13:22:03 -0700 |
commit | b69801bdf139301aff883038d487c3d32219fc55 (patch) | |
tree | 08c632a97f15c6273ab47fdcf07a4423d87cbc48 | |
parent | 73cc12047e7eb8162c1f1a2f19be77bb821be85a (diff) | |
download | view.love-b69801bdf139301aff883038d487c3d32219fc55.tar.gz |
highlight selection while dragging
Mouse stuff is pretty strenuous. For the first time I have to be careful not to recompute too often. And I ran into a race condition for the first time where resetting line.y within App.draw meant mouse clicks were extremely unlikely to see line.y set.
-rw-r--r-- | main.lua | 12 | ||||
-rw-r--r-- | text.lua | 52 |
2 files changed, 47 insertions, 17 deletions
diff --git a/main.lua b/main.lua index d20fd2c..25afc7e 100644 --- a/main.lua +++ b/main.lua @@ -55,6 +55,7 @@ Cursor1 = {line=1, pos=1} -- position of cursor Screen_bottom1 = {line=1, pos=1} -- position of start of screen line at bottom of screen Selection1 = {} +Recent_mouse = {} -- when selecting text, avoid recomputing some state on every single frame Cursor_x, Cursor_y = 0, 0 -- in pixels @@ -138,9 +139,6 @@ function App.draw() love.graphics.setColor(1, 1, 1) love.graphics.rectangle('fill', 0, 0, App.screen.width-1, App.screen.height-1) love.graphics.setColor(0, 0, 0) - for line_index,line in ipairs(Lines) do - line.y = nil - end assert(Text.le1(Screen_top1, Cursor1)) local y = Margin_top --? print('== draw') @@ -198,7 +196,8 @@ 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 - Text.move_cursor(line_index, line, x, y) + Cursor1.line = line_index + Cursor1.pos = Text.to_pos_on_line(line, x, y) Selection1 = {line=Cursor1.line, pos=Cursor1.pos} end elseif line.mode == 'drawing' then @@ -217,7 +216,8 @@ function App.mousereleased(x,y, button) for line_index,line in ipairs(Lines) do if line.mode == 'text' then if Text.in_line(line, x,y) then - Text.move_cursor(line_index, line, x, y) + Cursor1.line = line_index + Cursor1.pos = Text.to_pos_on_line(line, x, y) if Text.eq1(Cursor1, Selection1) then Selection1 = {} end @@ -228,6 +228,7 @@ function App.mousereleased(x,y, button) end function App.textinput(t) + for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll if Search_term then Search_term = Search_term..t Search_text = nil @@ -243,6 +244,7 @@ function App.textinput(t) end function App.keychord_pressed(chord) + for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll if Search_term then if chord == 'escape' then Search_term = nil diff --git a/text.lua b/text.lua index a2e22ab..81c73e5 100644 --- a/text.lua +++ b/text.lua @@ -213,15 +213,22 @@ function rfind(s, pat, i) return endpos-#pat+1 end --- Return any intersection of the region from Selection1 to Cursor1 with the --- region between {line=line_index, pos=apos} and {line=line_index, pos=bpos}. +-- Return any intersection of the region from Selection1 to Cursor1 (or +-- current mouse, if mouse is pressed; or recent mouse if mouse is pressed and +-- currently over a drawing) with the region between {line=line_index, pos=apos} +-- and {line=line_index, pos=bpos}. -- apos must be less than bpos. However Selection1 and Cursor1 can be in any order. -- Result: positions spos,epos between apos,bpos. function Text.clip_selection(line_index, apos, bpos) if Selection1.line == nil then return nil,nil end -- min,max = sorted(Selection1,Cursor1) local minl,minp = Selection1.line,Selection1.pos - local maxl,maxp = Cursor1.line,Cursor1.pos + local maxl,maxp + if love.mouse.isDown('1') then + maxl,maxp = Text.mouse_pos() + else + maxl,maxp = Cursor1.line,Cursor1.pos + end if minl > maxl then minl,maxl = maxl,minl minp,maxp = maxp,minp @@ -254,6 +261,31 @@ function Text.clip_selection(line_index, apos, bpos) end end +-- inefficient for some reason, so don't do it on every frame +function Text.mouse_pos() + local time = love.timer.getTime() + if Recent_mouse.time and Recent_mouse.time > time-0.1 then + return Recent_mouse.line, Recent_mouse.pos + end + Recent_mouse.time = time + local line,pos = Text.to_pos(love.mouse.getX(), love.mouse.getY()) + if line then + Recent_mouse.line = line + Recent_mouse.pos = pos + end + return Recent_mouse.line, Recent_mouse.pos +end + +function Text.to_pos(x,y) + for line_index,line in ipairs(Lines) do + if line.mode == 'text' then + if Text.in_line(line, x,y) then + return line_index, Text.to_pos_on_line(line, x,y) + end + end + end +end + function Text.delete_selection() local minl,maxl = minmax(Selection1.line, Cursor1.line) local before = snapshot(minl, maxl) @@ -1829,12 +1861,10 @@ function Text.in_line(line, x,y) return y < line.y + #line.screen_line_starting_pos * Line_height end --- mx,my in pixels -function Text.move_cursor(line_index, line, mx, my) - Cursor1.line = line_index +-- convert mx,my in pixels to schema-1 coordinates +function Text.to_pos_on_line(line, mx, my) if line.screen_line_starting_pos == nil then - Cursor1.pos = Text.nearest_cursor_pos(line.data, mx) - return + return Text.nearest_cursor_pos(line.data, mx) end assert(line.fragments) assert(my >= line.y) @@ -1847,12 +1877,10 @@ function Text.move_cursor(line_index, line, mx, my) -- line position cursor on final character of screen line. -- (The final screen line positions past end of screen line as always.) if mx > Line_width and screen_line_index < #line.screen_line_starting_pos then - Cursor1.pos = line.screen_line_starting_pos[screen_line_index+1] - return + return line.screen_line_starting_pos[screen_line_index+1] end local s = string.sub(line.data, screen_line_starting_pos) - Cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, mx) - 1 - return + return screen_line_starting_pos + Text.nearest_cursor_pos(s, mx) - 1 end y = nexty end |