view.love/select.lua

124 lines
4.5 KiB
Lua
Raw Normal View History

2022-06-03 21:14:53 +00:00
-- helpers for selecting portions of text
-- Return any intersection of the region from State.selection1 to State.cursor1 (or
2022-06-03 21:14:53 +00:00
-- 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 State.selection1 and State.cursor1 can be in any order.
2022-06-03 21:14:53 +00:00
-- 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
-- min,max = sorted(State.selection1,State.cursor1)
local minl,minp = State.selection1.line,State.selection1.pos
2022-06-03 21:14:53 +00:00
local maxl,maxp
if App.mouse_down(1) then
maxl,maxp = Text.mouse_pos(State)
2022-06-03 21:14:53 +00:00
else
maxl,maxp = State.cursor1.line,State.cursor1.pos
2022-06-03 21:14:53 +00:00
end
2022-08-18 04:18:22 +00:00
if Text.lt1({line=maxl, pos=maxp},
{line=minl, pos=minp}) then
2022-06-03 21:14:53 +00:00
minl,maxl = maxl,minl
minp,maxp = maxp,minp
end
-- check if intervals are disjoint
if line_index < minl then return nil,nil end
if line_index > maxl then return nil,nil end
if line_index == minl and bpos <= minp then return nil,nil end
if line_index == maxl and apos >= maxp then return nil,nil end
-- 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
elseif a_ge then
assert(maxl == line_index)
return apos,maxp
elseif b_lt then
assert(minl == line_index)
return minp,bpos
else
assert(minl == maxl and minl == line_index)
return minp,maxp
end
end
2022-06-03 21:30:51 +00:00
-- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
2022-06-03 21:37:32 +00:00
-- Creates text objects every time, so use this sparingly.
-- Returns some intermediate computation useful elsewhere.
function Text.draw_highlight(State, line, x,y, pos, lo,hi)
2022-06-03 21:30:51 +00:00
if lo then
local lo_offset = Text.offset(line.data, lo)
local hi_offset = Text.offset(line.data, hi)
local pos_offset = Text.offset(line.data, pos)
2022-06-03 21:30:51 +00:00
local lo_px
if pos == lo then
lo_px = 0
else
local before = line.data:sub(pos_offset, lo_offset-1)
2022-06-03 21:30:51 +00:00
local before_text = App.newText(love.graphics.getFont(), before)
lo_px = App.width(before_text)
end
--? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
local s = line.data:sub(lo_offset, hi_offset-1)
2022-06-03 21:30:51 +00:00
local text = App.newText(love.graphics.getFont(), s)
local text_width = App.width(text)
2022-07-12 06:03:27 +00:00
App.color(Highlight_color)
love.graphics.rectangle('fill', x+lo_px,y, text_width,State.line_height)
2022-07-12 06:03:27 +00:00
App.color(Text_color)
2022-06-03 21:37:32 +00:00
return lo_px
2022-06-03 21:30:51 +00:00
end
end
2022-06-03 21:14:53 +00:00
-- inefficient for some reason, so don't do it on every frame
function Text.mouse_pos(State)
2022-06-03 21:14:53 +00:00
local time = love.timer.getTime()
if State.recent_mouse.time and State.recent_mouse.time > time-0.1 then
return State.recent_mouse.line, State.recent_mouse.pos
2022-06-03 21:14:53 +00:00
end
State.recent_mouse.time = time
local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y())
2022-06-03 21:14:53 +00:00
if line then
State.recent_mouse.line = line
State.recent_mouse.pos = pos
2022-06-03 21:14:53 +00:00
end
return State.recent_mouse.line, State.recent_mouse.pos
2022-06-03 21:14:53 +00:00
end
function Text.to_pos(State, x,y)
for line_index,line in ipairs(State.lines) do
2022-08-14 16:17:53 +00:00
if Text.in_line(State, line_index, x,y) then
return line_index, Text.to_pos_on_line(State, line_index, x,y)
2022-06-03 21:14:53 +00:00
end
end
end
function Text.selection(State)
if State.selection1.line == nil then return end
-- min,max = sorted(State.selection1,State.cursor1)
local minl,minp = State.selection1.line,State.selection1.pos
local maxl,maxp = State.cursor1.line,State.cursor1.pos
2022-06-03 21:14:53 +00:00
if minl > maxl then
minl,maxl = maxl,minl
minp,maxp = maxp,minp
elseif minl == maxl then
if minp > maxp then
minp,maxp = maxp,minp
end
end
local min_offset = Text.offset(State.lines[minl].data, minp)
local max_offset = Text.offset(State.lines[maxl].data, maxp)
2022-06-03 21:14:53 +00:00
if minl == maxl then
return State.lines[minl].data:sub(min_offset, max_offset-1)
2022-06-03 21:14:53 +00:00
end
assert(minl < maxl)
local result = {State.lines[minl].data:sub(min_offset)}
2022-06-03 21:14:53 +00:00
for i=minl+1,maxl-1 do
table.insert(result, State.lines[i].data)
2022-06-03 21:14:53 +00:00
end
table.insert(result, State.lines[maxl].data:sub(1, max_offset-1))
return table.concat(result, '\n')
2022-06-03 21:14:53 +00:00
end