Give up on drawing partial drawings at top of screen

There are still open questions with that:
- say there's a partial drawing up top. Do we want to support drawing on
  it? It would be jarring for it to move right as you click on it. But
  might also not be a great experience to only see part of it.

- the current problem with snapping cursor to bottom is that I only want
  to include whole screen lines in that particular call to edit.up.

  On the other hand, drawings were supporting partial screen lines,
  which added a second layer of complexity to it. Now there's at least
  only one problem.

- all my basic move operations were always maintaining yoff in drawings.
  It's meaningless and ignored in cursor for the most part. But if I
  ever copy screen_top from cursor I have to remember to reset yoff. Now
  there's one less thing to remember.
This commit is contained in:
Kartik K. Agaram 2024-07-21 09:14:01 -07:00
parent 7dd966f039
commit 58efe12ed6
3 changed files with 18 additions and 49 deletions

View File

@ -62,7 +62,7 @@ function new_editor(top, left, right, bottom, font, font_height, line_height) -
cursor = nil, -- location where editing will occur
-- Valid location values:
-- {mode='text', line=, pos=}
-- {mode='drawing', line=, yoff=}
-- {mode='drawing', line=}
selection1 = {},
-- some extra state to compute selection between mouse press and release
@ -94,8 +94,8 @@ function edit.scroll_to_top(Editor)
Editor.screen_top = {mode='text', line=1, pos=1}
Editor.cursor = {mode='text', line=1, pos=1}
else
Editor.screen_top = {mode='drawing', line=1, yoff=0}
Editor.cursor = {mode='drawing', line=1, yoff=0}
Editor.screen_top = {mode='drawing', line=1}
Editor.cursor = {mode='drawing', line=1}
end
end
@ -159,7 +159,7 @@ function edit.draw(Editor)
onpress1 = function()
Editor.drawing_before = snapshot(Editor, line_index-1, line_index)
table.insert(Editor.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
Editor.cursor = {mode='drawing', line=line_index, yoff=0}
Editor.cursor = {mode='drawing', line=line_index}
schedule_save(Editor)
record_undo_event(Editor, {before=Editor.drawing_before, after=snapshot(Editor, line_index-1, line_index+1)})
end,
@ -196,13 +196,9 @@ function edit.draw(Editor)
do_it(x,y, 0, line_index, utf8.len(line.data)+1, '')
y = y + Editor.line_height
elseif line.mode == 'drawing' then
local yoff = 0
if line_index == Editor.screen_top.line then
yoff = Editor.screen_top.yoff or 0
end
local h = Drawing_padding_height + Drawing.pixels(line.h, Editor.width)
Drawing.draw(Editor, line_index, y+Drawing_padding_top-yoff)
y = y + h - yoff
Drawing.draw(Editor, line_index, y+Drawing_padding_top)
y = y + h
else
assert(false, ('unknown line mode %s'):format(line.mode))
end

View File

@ -5,9 +5,8 @@
-- (pos counts in utf8 codepoints starting from 1)
--
-- locations within drawings look like this:
-- {mode='drawing', line=, yoff=}
-- (yoff is only meaningful for the very top-most screen line drawn on screen
-- when we need to start drawing partway down)
-- {mode='drawing', line=}
-- (drawing lines are atomic as far as the cursor is concerned)
--
-- all movements are built primarily using the following primitives, defined
-- further down:
@ -106,17 +105,11 @@ end -- 0-1 scan
function Text.pageup(Editor)
Editor.screen_top = edit.up(Editor, Editor.screen_top, Editor.bottom - Editor.top - Editor.line_height) -- scan
if Editor.screen_top.mode == 'drawing' then
Editor.screen_top.yoff = 0
end
Editor.cursor = deepcopy(Editor.screen_top)
end -- 1 scan
function Text.pagedown(Editor)
Editor.screen_top = edit.down(Editor, Editor.screen_top, Editor.bottom - Editor.top - Editor.line_height) -- scan
if Editor.screen_top.mode == 'drawing' then
Editor.screen_top.yoff = 0
end
Editor.cursor = deepcopy(Editor.screen_top)
end -- 1 scan
@ -262,14 +255,9 @@ function edit.to_loc(Editor, mx,my)
y = y + Editor.line_height
else
-- drawing
local yoff = 0
if line_index == Editor.screen_top.line then
assert(Editor.screen_top.mode == 'drawing')
yoff = Editor.screen_top.yoff or 0
end
local h = Drawing_padding_height + Drawing.pixels(line.h, Editor.width) - yoff
local h = Drawing_padding_height + Drawing.pixels(line.h, Editor.width)
if my < y+h then
return {mode='drawing', line=line_index, yoff=yoff}
return {mode='drawing', line=line_index}
end
y = y + h
end
@ -326,11 +314,7 @@ function edit.to_coord(Editor, loc) -- scans
if loc.mode == 'drawing' and loc.line == line_index then
return x, y
end
local yoff = 0
if line_index == Editor.screen_top.line then
yoff = Editor.screen_top.yoff or 0
end
y = y + Drawing.height(Editor, line) - yoff
y = y + Drawing.height(Editor, line)
else
assert(false, ('unknown line mode %s'):format(line.mode))
end
@ -357,17 +341,11 @@ function edit.down(Editor, loc, dy) -- scans
y = y + Editor.line_height
end
elseif line.mode == 'drawing' then
local yoff = 0
if line_index == loc.line then
-- yoff matters only when loc is screen_top
assert(loc.mode == 'drawing')
yoff = loc.yoff or 0
end
local h = Drawing.height(Editor, line)
if y + h - yoff >= dy then
return {mode='drawing', line=line_index, yoff=dy-y}
if y + h >= dy then
return {mode='drawing', line=line_index}
end
y = y + h - yoff
y = y + h
else
assert(false, ('unknown line mode %s'):format(line.mode))
end
@ -401,13 +379,8 @@ function edit.up(Editor, loc, dy) -- scans
end
elseif line.mode == 'drawing' then
local h = Drawing.height(Editor, line)
if line_index == loc.line then
assert(loc.mode == 'drawing')
-- _only_ when loc is screen_top, yoff bounds how much height we have to scan up within this drawing
h = loc.yoff or 0
end
if y + h >= dy then
return {mode='drawing', line=line_index, yoff=dy-y}
return {mode='drawing', line=line_index}
end
y = y + h
else
@ -425,7 +398,7 @@ function edit.top(Editor)
if Editor.lines[1].mode == 'text' then
return {mode='text', line=1, pos=1}
else
return {mode='drawing', line=1, yoff=0}
return {mode='drawing', line=1}
end
end
@ -543,7 +516,7 @@ function Drawing.height(Editor, line)
end
function edit.eq(a, b)
return a.mode == b.mode and a.line == b.line and --[[just for text mode]] a.pos == b.pos -- ignore yoff
return a.mode == b.mode and a.line == b.line and --[[just for text mode]] a.pos == b.pos
end
function edit.lt(a, b)

View File

@ -101,7 +101,7 @@ function Text.keychord_press(Editor, chord)
if Editor.lines[Editor.cursor.line].mode == 'text' then
Editor.cursor = {mode='text', line=Editor.cursor.line, pos=1}
else
Editor.cursor = {mode='drawing', line=Editor.cursor.line, yoff=0}
Editor.cursor = {mode='drawing', line=Editor.cursor.line}
end
-- no need to scroll, but screen_top may have switched mode
if Editor.screen_top.line == Editor.cursor.line then