ah, I see the problem

Text.mouse_pos can sometimes set recent_mouse.time but not
recent_mouse.x/y. I'd assumed x/y is never nil in those situations, but
that's violated. It's most easily seen when typing C-a and then
clicking.
This commit is contained in:
Kartik K. Agaram 2023-06-01 12:33:22 -07:00
parent f1886391c5
commit 3e848efb08
2 changed files with 8 additions and 13 deletions

View File

@ -223,7 +223,7 @@ end
function edit.mouse_press(State, x,y, mouse_button)
if State.search_term then return end
print_and_log(('edit.mouse_press: cursor at %d,%d').format(State.cursor1.line, State.cursor1.pos))
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
@ -241,7 +241,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))
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,7 +249,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_and_log(('edit.mouse_press: selection now %d,%d').format(State.selection1.line, State.selection1.pos))
print_and_log(('edit.mouse_press: selection now %d,%d'):format(State.selection1.line, State.selection1.pos))
break
end
elseif line.mode == 'drawing' then
@ -267,7 +267,7 @@ end
function edit.mouse_release(State, x,y, mouse_button)
if State.search_term then return end
print_and_log(('edit.mouse_release: cursor at %d,%d').format(State.cursor1.line, State.cursor1.pos))
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)
@ -280,12 +280,12 @@ function edit.mouse_release(State, x,y, mouse_button)
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_and_log(('edit.mouse_release: in line %d').format(line_index))
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_and_log(('edit.mouse_release: cursor now %d,%d').format(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
@ -301,7 +301,7 @@ function edit.mouse_release(State, x,y, mouse_button)
end
end
end
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))
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

View File

@ -8,17 +8,13 @@
-- Result: positions spos,epos between apos,bpos.
function Text.clip_selection(State, line_index, apos, bpos)
if State.selection1.line == nil then return nil,nil end
print_and_log('text.clip_selection')
-- min,max = sorted(State.selection1,State.cursor1)
local minl,minp = State.selection1.line,State.selection1.pos
print_and_log(('text.clip_selection: one end from selection: %d,%d'):format(minl,minp))
local maxl,maxp
if App.mouse_down(1) then
maxl,maxp = Text.mouse_pos(State)
print_and_log(('text.clip_selection: other end from mouse: %d,%d'):format(maxl,maxp))
else
maxl,maxp = State.cursor1.line,State.cursor1.pos
print_and_log(('text.clip_selection: other end from cursor: %d,%d'):format(maxl,maxp))
end
if Text.lt1({line=maxl, pos=maxp},
{line=minl, pos=minp}) then
@ -33,7 +29,6 @@ function Text.clip_selection(State, line_index, apos, bpos)
-- compare bounds more carefully (start inclusive, end exclusive)
local a_ge = Text.le1({line=minl, pos=minp}, {line=line_index, pos=apos})
local b_lt = Text.lt1({line=line_index, pos=bpos}, {line=maxl, pos=maxp})
--? print(minl,line_index,maxl, '--', minp,apos,bpos,maxp, '--', a_ge,b_lt)
if a_ge and b_lt then
-- fully contained
return apos,bpos
@ -64,7 +59,6 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi)
local before = line.data:sub(pos_offset, lo_offset-1)
lo_px = App.width(before)
end
--? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
local s = line.data:sub(lo_offset, hi_offset-1)
App.color(Highlight_color)
love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height)
@ -77,6 +71,7 @@ end
function Text.mouse_pos(State)
local time = love.timer.getTime()
if State.recent_mouse.time and State.recent_mouse.time > time-0.1 then
print_and_log(('text.mouse_pos: returning recent value %d,%d'):format(State.recent_mouse.line, State.recent_mouse.pos))
return State.recent_mouse.line, State.recent_mouse.pos
end
State.recent_mouse.time = time