stop caching starty

This is quite useful because I used to have a long list of places in
which to invalidate the cache.
This commit is contained in:
Kartik K. Agaram 2024-06-11 10:37:58 -07:00
parent f2299cb422
commit 69c88da98c
12 changed files with 148 additions and 107 deletions

View File

@ -131,7 +131,7 @@ function App.run_tests()
end
table.sort(sorted_names)
--? App.initialize_for_test() -- debug: run a single test at a time like these 2 lines
--? test_pagedown_skips_drawings()
--? test_click_moves_cursor()
for _,name in ipairs(sorted_names) do
App.initialize_for_test()
--? print('=== '..name)

View File

@ -6,16 +6,15 @@ require 'drawing_tests'
-- into 256 parts.
function Drawing.draw(State, line_index, y)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
line_cache.starty = y
local pmx,pmy = App.mouse_x(), App.mouse_y()
if pmx < State.right and pmy > line_cache.starty and pmy < line_cache.starty+Drawing.pixels(line.h, State.width) then
local starty = Text.starty(State, line_index)
if pmx < State.right and pmy > starty and pmy < starty+Drawing.pixels(line.h, State.width) then
App.color(Icon_color)
love.graphics.rectangle('line', State.left,line_cache.starty, State.width,Drawing.pixels(line.h, State.width))
love.graphics.rectangle('line', State.left,starty, State.width,Drawing.pixels(line.h, State.width))
if icon[State.current_drawing_mode] then
icon[State.current_drawing_mode](State.right-22, line_cache.starty+4)
icon[State.current_drawing_mode](State.right-22, starty+4)
else
icon[State.previous_drawing_mode](State.right-22, line_cache.starty+4)
icon[State.previous_drawing_mode](State.right-22, starty+4)
end
if App.mouse_down(1) and love.keyboard.isDown('h') then
@ -30,7 +29,7 @@ function Drawing.draw(State, line_index, y)
end
local mx = Drawing.coord(pmx-State.left, State.width)
local my = Drawing.coord(pmy-line_cache.starty, State.width)
local my = Drawing.coord(pmy-starty, State.width)
for _,shape in ipairs(line.shapes) do
if geom.on_shape(mx,my, line, shape) then
@ -38,11 +37,11 @@ function Drawing.draw(State, line_index, y)
else
App.color(Stroke_color)
end
Drawing.draw_shape(line, shape, line_cache.starty, State.left,State.right)
Drawing.draw_shape(line, shape, starty, State.left,State.right)
end
local function px(x) return Drawing.pixels(x, State.width)+State.left end
local function py(y) return Drawing.pixels(y, State.width)+line_cache.starty end
local function py(y) return Drawing.pixels(y, State.width)+starty end
for i,p in ipairs(line.points) do
if p.deleted == nil then
if Drawing.near(p, mx,my, State.width) then
@ -71,7 +70,7 @@ function Drawing.draw(State, line_index, y)
end
end
App.color(Current_stroke_color)
Drawing.draw_pending_shape(line, line_cache.starty, State.left,State.right)
Drawing.draw_pending_shape(line, starty, State.left,State.right)
end
function Drawing.draw_shape(drawing, shape, top, left,right)
@ -209,17 +208,24 @@ function Drawing.draw_pending_shape(drawing, top, left,right)
end
end
function Drawing.in_drawing(drawing, line_cache, x,y, left,right)
if line_cache.starty == nil then return false end -- outside current page
function Drawing.in_current_drawing(State, x,y, left,right)
return Drawing.in_drawing(State, State.lines.current_drawing_index, x,y, left,right)
end
function Drawing.in_drawing(State, line_index, x,y, left,right)
assert(State.lines[line_index].mode == 'drawing')
local starty = Text.starty(State, line_index)
if starty == nil then return false end -- outside current page
local drawing = State.lines[line_index]
local width = right-left
return y >= line_cache.starty and y < line_cache.starty + Drawing.pixels(drawing.h, width) and x >= left and x < right
return y >= starty and y < starty + Drawing.pixels(drawing.h, width) and x >= left and x < right
end
function Drawing.mouse_press(State, drawing_index, x,y, mouse_button)
local drawing = State.lines[drawing_index]
local line_cache = State.line_cache[drawing_index]
local starty = Text.starty(State, drawing_index)
local cx = Drawing.coord(x-State.left, State.width)
local cy = Drawing.coord(y-line_cache.starty, State.width)
local cy = Drawing.coord(y-starty, State.width)
if State.current_drawing_mode == 'freehand' then
drawing.pending = {mode=State.current_drawing_mode, points={{x=cx, y=cy}}}
elseif State.current_drawing_mode == 'line' or State.current_drawing_mode == 'manhattan' then
@ -244,8 +250,8 @@ end
function Drawing.update(State)
if State.lines.current_drawing == nil then return end
local drawing = State.lines.current_drawing
local line_cache = State.line_cache[State.lines.current_drawing_index]
if line_cache.starty == nil then
local starty = Text.starty(State, State.lines.current_drawing_index)
if starty == nil then
-- some event cleared starty just this frame
-- draw in this frame will soon set starty
-- just skip this frame
@ -254,9 +260,9 @@ function Drawing.update(State)
assert(drawing.mode == 'drawing', 'Drawing.update: line is not a drawing')
local pmx, pmy = App.mouse_x(), App.mouse_y()
local mx = Drawing.coord(pmx-State.left, State.width)
local my = Drawing.coord(pmy-line_cache.starty, State.width)
local my = Drawing.coord(pmy-starty, State.width)
if App.mouse_down(1) then
if Drawing.in_drawing(drawing, line_cache, pmx,pmy, State.left,State.right) then
if Drawing.in_current_drawing(State, pmx,pmy, State.left,State.right) then
if drawing.pending.mode == 'freehand' then
table.insert(drawing.pending.points, {x=mx, y=my})
elseif drawing.pending.mode == 'move' then
@ -266,7 +272,7 @@ function Drawing.update(State)
end
end
elseif State.current_drawing_mode == 'move' then
if Drawing.in_drawing(drawing, line_cache, pmx, pmy, State.left,State.right) then
if Drawing.in_current_drawing(State, pmx, pmy, State.left,State.right) then
drawing.pending.target_point.x = mx
drawing.pending.target_point.y = my
Drawing.relax_constraints(drawing, drawing.pending.target_point_index)
@ -304,7 +310,7 @@ function Drawing.mouse_release(State, x,y, mouse_button)
end
elseif State.lines.current_drawing then
local drawing = State.lines.current_drawing
local line_cache = State.line_cache[State.lines.current_drawing_index]
local starty = Text.starty(State, State.lines.current_drawing_index)
if drawing.pending then
if drawing.pending.mode == nil then
-- nothing pending
@ -313,14 +319,14 @@ function Drawing.mouse_release(State, x,y, mouse_button)
Drawing.smoothen(drawing.pending)
table.insert(drawing.shapes, drawing.pending)
elseif drawing.pending.mode == 'line' then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
drawing.pending.p2 = Drawing.find_or_insert_point(drawing.points, mx,my, State.width)
table.insert(drawing.shapes, drawing.pending)
end
elseif drawing.pending.mode == 'manhattan' then
local p1 = drawing.points[drawing.pending.p1]
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
if math.abs(mx-p1.x) > math.abs(my-p1.y) then
drawing.pending.p2 = Drawing.find_or_insert_point(drawing.points, mx, p1.y, State.width)
@ -328,11 +334,11 @@ function Drawing.mouse_release(State, x,y, mouse_button)
drawing.pending.p2 = Drawing.find_or_insert_point(drawing.points, p1.x, my, State.width)
end
local p2 = drawing.points[drawing.pending.p2]
App.mouse_move(State.left+Drawing.pixels(p2.x, State.width), line_cache.starty+Drawing.pixels(p2.y, State.width))
App.mouse_move(State.left+Drawing.pixels(p2.x, State.width), starty+Drawing.pixels(p2.y, State.width))
table.insert(drawing.shapes, drawing.pending)
end
elseif drawing.pending.mode == 'polygon' then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
table.insert(drawing.pending.vertices, Drawing.find_or_insert_point(drawing.points, mx,my, State.width))
table.insert(drawing.shapes, drawing.pending)
@ -340,7 +346,7 @@ function Drawing.mouse_release(State, x,y, mouse_button)
elseif drawing.pending.mode == 'rectangle' then
assert(#drawing.pending.vertices <= 2, 'Drawing.mouse_release: rectangle has too many pending vertices')
if #drawing.pending.vertices == 2 then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
local first = drawing.points[drawing.pending.vertices[1]]
local second = drawing.points[drawing.pending.vertices[2]]
@ -355,7 +361,7 @@ function Drawing.mouse_release(State, x,y, mouse_button)
elseif drawing.pending.mode == 'square' then
assert(#drawing.pending.vertices <= 2, 'Drawing.mouse_release: square has too many pending vertices')
if #drawing.pending.vertices == 2 then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
local first = drawing.points[drawing.pending.vertices[1]]
local second = drawing.points[drawing.pending.vertices[2]]
@ -366,14 +372,14 @@ function Drawing.mouse_release(State, x,y, mouse_button)
end
end
elseif drawing.pending.mode == 'circle' then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
local center = drawing.points[drawing.pending.center]
drawing.pending.radius = round(geom.dist(center.x,center.y, mx,my))
table.insert(drawing.shapes, drawing.pending)
end
elseif drawing.pending.mode == 'arc' then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h then
local center = drawing.points[drawing.pending.center]
drawing.pending.end_angle = geom.angle_with_hint(center.x,center.y, mx,my, drawing.pending.end_angle)
@ -477,13 +483,15 @@ function Drawing.keychord_press(State, chord)
end
drawing.pending.mode = 'square'
elseif App.mouse_down(1) and chord == 'p' and State.current_drawing_mode == 'polygon' then
local _,drawing,line_cache = Drawing.current_drawing(State)
local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-line_cache.starty, State.width)
local drawing_index,drawing = Drawing.current_drawing(State)
local starty = Text.starty(State, drawing_index)
local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-starty, State.width)
local j = Drawing.find_or_insert_point(drawing.points, mx,my, State.width)
table.insert(drawing.pending.vertices, j)
elseif App.mouse_down(1) and chord == 'p' and (State.current_drawing_mode == 'rectangle' or State.current_drawing_mode == 'square') then
local _,drawing,line_cache = Drawing.current_drawing(State)
local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-line_cache.starty, State.width)
local drawing_index,drawing = Drawing.current_drawing(State)
local starty = Text.starty(State, drawing_index)
local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-starty, State.width)
local j = Drawing.find_or_insert_point(drawing.points, mx,my, State.width)
while #drawing.pending.vertices >= 2 do
table.remove(drawing.pending.vertices)
@ -492,9 +500,10 @@ function Drawing.keychord_press(State, chord)
elseif chord == 'C-o' and not App.mouse_down(1) then
State.current_drawing_mode = 'circle'
elseif App.mouse_down(1) and chord == 'a' and State.current_drawing_mode == 'circle' then
local _,drawing,line_cache = Drawing.current_drawing(State)
local drawing_index,drawing = Drawing.current_drawing(State)
local starty = Text.starty(State, drawing_index)
drawing.pending.mode = 'arc'
local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-line_cache.starty, State.width)
local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-starty, State.width)
local center = drawing.points[drawing.pending.center]
drawing.pending.radius = round(geom.dist(center.x,center.y, mx,my))
drawing.pending.start_angle = geom.angle(center.x,center.y, mx,my)
@ -510,7 +519,7 @@ function Drawing.keychord_press(State, chord)
end
drawing.pending.mode = 'circle'
elseif chord == 'C-u' and not App.mouse_down(1) then
local drawing_index,drawing,line_cache,i,p = Drawing.select_point_at_mouse(State)
local drawing_index,drawing,_,i,p = Drawing.select_point_at_mouse(State)
if drawing then
if State.previous_drawing_mode == nil then
State.previous_drawing_mode = State.current_drawing_mode
@ -521,7 +530,7 @@ function Drawing.keychord_press(State, chord)
State.lines.current_drawing = drawing
end
elseif chord == 'C-n' and not App.mouse_down(1) then
local drawing_index,drawing,line_cache,point_index,p = Drawing.select_point_at_mouse(State)
local drawing_index,drawing,_,point_index,p = Drawing.select_point_at_mouse(State)
if drawing then
if State.previous_drawing_mode == nil then
-- don't clobber
@ -619,9 +628,8 @@ function Drawing.current_drawing(State)
local x, y = App.mouse_x(), App.mouse_y()
for drawing_index,drawing in ipairs(State.lines) do
if drawing.mode == 'drawing' then
local line_cache = State.line_cache[drawing_index]
if Drawing.in_drawing(drawing, line_cache, x,y, State.left,State.right) then
return drawing_index,drawing,line_cache
if Drawing.in_drawing(State, drawing_index, x,y, State.left,State.right) then
return drawing_index,drawing
end
end
end
@ -632,12 +640,12 @@ function Drawing.select_shape_at_mouse(State)
for drawing_index,drawing in ipairs(State.lines) do
if drawing.mode == 'drawing' then
local x, y = App.mouse_x(), App.mouse_y()
local line_cache = State.line_cache[drawing_index]
if Drawing.in_drawing(drawing, line_cache, x,y, State.left,State.right) then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local starty = Text.starty(State, drawing_index)
if Drawing.in_drawing(State, drawing_index, x,y, State.left,State.right) then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
for i,shape in ipairs(drawing.shapes) do
if geom.on_shape(mx,my, drawing, shape) then
return drawing,line_cache,i,shape
return drawing,starty,i,shape
end
end
end
@ -649,12 +657,12 @@ function Drawing.select_point_at_mouse(State)
for drawing_index,drawing in ipairs(State.lines) do
if drawing.mode == 'drawing' then
local x, y = App.mouse_x(), App.mouse_y()
local line_cache = State.line_cache[drawing_index]
if Drawing.in_drawing(drawing, line_cache, x,y, State.left,State.right) then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-line_cache.starty, State.width)
local starty = Text.starty(State, drawing_index)
if Drawing.in_drawing(State, drawing_index, x,y, State.left,State.right) then
local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)
for i,point in ipairs(drawing.points) do
if Drawing.near(point, mx,my, State.width) then
return drawing_index,drawing,line_cache,i,point
return drawing_index,drawing,starty,i,point
end
end
end
@ -666,8 +674,7 @@ function Drawing.select_drawing_at_mouse(State)
for drawing_index,drawing in ipairs(State.lines) do
if drawing.mode == 'drawing' then
local x, y = App.mouse_x(), App.mouse_y()
local line_cache = State.line_cache[drawing_index]
if Drawing.in_drawing(drawing, line_cache, x,y, State.left,State.right) then
if Drawing.in_drawing(State, drawing_index, x,y, State.left,State.right) then
return drawing
end
end

View File

@ -32,7 +32,7 @@ function test_draw_line()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- draw a line
@ -77,7 +77,7 @@ function test_draw_horizontal_line()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- draw a line that is more horizontal than vertical
@ -105,7 +105,7 @@ function test_draw_circle()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- draw a circle
@ -134,7 +134,7 @@ function test_cancel_stroke()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- start drawing a line
@ -172,7 +172,7 @@ function test_draw_circle_mid_stroke()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- draw a circle
@ -200,7 +200,7 @@ function test_draw_arc()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- draw an arc
@ -231,7 +231,7 @@ function test_draw_polygon()
check_eq(Editor_state.current_drawing_mode, 'line', 'baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- first point
@ -269,7 +269,7 @@ function test_draw_rectangle()
check_eq(Editor_state.current_drawing_mode, 'line', 'baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- first point
@ -313,7 +313,7 @@ function test_draw_rectangle_intermediate()
check_eq(Editor_state.current_drawing_mode, 'line', 'baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- first point
@ -349,7 +349,7 @@ function test_draw_square()
check_eq(Editor_state.current_drawing_mode, 'line', 'baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode')
check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')
check_eq(Editor_state.lines[1].h, 128, 'baseline/y')
check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')
-- first point

View File

@ -31,7 +31,6 @@ function edit.initialize_state(top, left, right, font, font_height, line_height)
-- string data,
-- a drawing is a table with:
-- mode = 'drawing'
-- a (y) coord in pixels (updated while painting screen),
-- a (h)eight,
-- an array of points, and
-- an array of shapes
@ -52,7 +51,6 @@ function edit.initialize_state(top, left, right, font, font_height, line_height)
-- rendering wrapped text lines needs some additional short-lived data per line:
-- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
-- starty, the y coord in pixels the line starts rendering from
-- fragments: snippets of the line guaranteed to not straddle screen lines
-- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
line_cache = {},
@ -184,7 +182,6 @@ function edit.draw(State)
Drawing.before = snapshot(State, line_index-1, line_index)
table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
table.insert(State.line_cache, line_index, {})
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end
if State.cursor1.line >= line_index then
State.cursor1.line = State.cursor1.line+1
end
@ -276,8 +273,7 @@ function edit.mouse_press(State, x,y, mouse_button)
return
end
elseif line.mode == 'drawing' then
local line_cache = State.line_cache[line_index]
if Drawing.in_drawing(line, line_cache, x, y, State.left,State.right) then
if Drawing.in_drawing(State, line_index, x, y, State.left,State.right) then
State.lines.current_drawing_index = line_index
State.lines.current_drawing = line
Drawing.before = snapshot(State, line_index)
@ -395,7 +391,6 @@ function edit.keychord_press(State, chord, key)
Text.delete_selection(State, State.left, State.right)
end
if State.search_term then
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
if chord == 'escape' then
State.search_term = nil
State.cursor1 = State.search_backup.cursor
@ -500,7 +495,6 @@ function edit.keychord_press(State, chord, key)
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
-- dispatch to drawing or text
elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
-- DON'T reset line_cache.starty here
local drawing_index, drawing = Drawing.current_drawing(State)
if drawing_index then
local before = snapshot(State, drawing_index)
@ -537,7 +531,6 @@ function edit.keychord_press(State, chord, key)
end
schedule_save(State)
else
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
Text.keychord_press(State, chord)
end
end

View File

@ -1,8 +1,8 @@
function draw_help_without_mouse_pressed(State, drawing_index)
local drawing = State.lines[drawing_index]
local line_cache = State.line_cache[drawing_index]
local starty = Text.starty(State, drawing_index)
App.color(Help_color)
local y = line_cache.starty+10
local y = starty+10
love.graphics.print("Things you can do:", State.left+30,y)
y = y + State.line_height
love.graphics.print("* Press the mouse button to start drawing a "..current_shape(State), State.left+30,y)
@ -48,14 +48,14 @@ function draw_help_without_mouse_pressed(State, drawing_index)
love.graphics.print("Press 'esc' now to hide this message", State.left+30,y)
y = y + State.line_height
App.color(Help_background_color)
love.graphics.rectangle('fill', State.left,line_cache.starty, State.width, math.max(Drawing.pixels(drawing.h, State.width),y-line_cache.starty))
love.graphics.rectangle('fill', State.left,starty, State.width, math.max(Drawing.pixels(drawing.h, State.width),y-starty))
end
function draw_help_with_mouse_pressed(State, drawing_index)
local drawing = State.lines[drawing_index]
local line_cache = State.line_cache[drawing_index]
local starty = Text.starty(State, drawing_index)
App.color(Help_color)
local y = line_cache.starty+10
local y = starty+10
love.graphics.print("You're currently drawing a "..current_shape(State, drawing.pending), State.left+30,y)
y = y + State.line_height
love.graphics.print('Things you can do now:', State.left+30,y)
@ -129,7 +129,7 @@ function draw_help_with_mouse_pressed(State, drawing_index)
y = y + State.line_height
end
App.color(Help_background_color)
love.graphics.rectangle('fill', State.left,line_cache.starty, State.width, math.max(Drawing.pixels(drawing.h, State.width),y-line_cache.starty))
love.graphics.rectangle('fill', State.left,starty, State.width, math.max(Drawing.pixels(drawing.h, State.width),y-starty))
end
function current_shape(State, shape)

View File

@ -69,7 +69,7 @@ end
function Text.mouse_pos(State)
local x,y = App.mouse_x(), App.mouse_y()
if y < State.line_cache[State.screen_top1.line].starty then
if y < State.top then
return State.screen_top1.line, State.screen_top1.pos
end
for line_index,line in ipairs(State.lines) do

View File

@ -307,7 +307,6 @@ function source.mouse_press(x,y, mouse_button)
return
end
log_browser.mouse_press(Log_browser_state, x,y, mouse_button)
for _,line_cache in ipairs(Editor_state.line_cache) do line_cache.starty = nil end -- just in case we scroll
end
end

View File

@ -33,7 +33,6 @@ function edit.initialize_state(top, left, right, font, font_height, line_height)
-- string data,
-- a drawing is a table with:
-- mode = 'drawing'
-- a (y) coord in pixels (updated while painting screen),
-- a (h)eight,
-- an array of points, and
-- an array of shapes
@ -54,7 +53,6 @@ function edit.initialize_state(top, left, right, font, font_height, line_height)
-- rendering wrapped text lines needs some additional short-lived data per line:
-- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
-- starty, the y coord in pixels the line starts rendering from
-- fragments: snippets of the line guaranteed to not straddle screen lines
-- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
line_cache = {},
@ -189,7 +187,6 @@ function edit.draw(State, hide_cursor, show_line_numbers)
Drawing.before = snapshot(State, line_index-1, line_index)
table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
table.insert(State.line_cache, line_index, {})
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end
if State.cursor1.line >= line_index then
State.cursor1.line = State.cursor1.line+1
end
@ -280,8 +277,7 @@ function edit.mouse_press(State, x,y, mouse_button)
return
end
elseif line.mode == 'drawing' then
local line_cache = State.line_cache[line_index]
if Drawing.in_drawing(line, line_cache, x, y, State.left,State.right) then
if Drawing.in_drawing(State, line_index, x, y, State.left,State.right) then
State.lines.current_drawing_index = line_index
State.lines.current_drawing = line
Drawing.before = snapshot(State, line_index)
@ -399,7 +395,6 @@ function edit.keychord_press(State, chord, key)
Text.delete_selection(State, State.left, State.right)
end
if State.search_term then
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
if chord == 'escape' then
State.search_term = nil
State.cursor1 = State.search_backup.cursor
@ -504,7 +499,6 @@ function edit.keychord_press(State, chord, key)
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
-- dispatch to drawing or text
elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
-- DON'T reset line_cache.starty here
local drawing_index, drawing = Drawing.current_drawing(State)
if drawing_index then
local before = snapshot(State, drawing_index)
@ -541,7 +535,6 @@ function edit.keychord_press(State, chord, key)
end
schedule_save(State)
else
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
Text.keychord_press(State, chord)
end
end

View File

@ -6,7 +6,6 @@ Text = {}
function Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
line_cache.starty = y
line_cache.startpos = startpos
-- wrap long lines
Text.populate_screen_line_starting_pos(State, line_index)
@ -431,6 +430,28 @@ function Text.pageup(State)
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
end
-- return the top y coordinate of a given line_index,
-- or nil if no part of it is on screen
function Text.starty(State, line_index)
-- duplicate some logic from love.draw
-- does not modify State (except to populate line_cache)
if line_index < State.screen_top1.line then return end
local loc2 = Text.to2(State, State.screen_top1)
local y = State.top
while true do
if loc2.line == line_index then return y end
if State.lines[loc2.line].mode == 'text' then
y = y + State.line_height
elseif State.lines[loc2.line].mode == 'drawing' then
y = y + Drawing_padding_height + Drawing.pixels(State.lines[loc2.line].h, State.width)
end
if y + State.line_height > App.screen.height then break end
local next_loc2 = Text.next_screen_line(State, loc2)
if Text.eq2(next_loc2, loc2) then break end -- end of file
loc2 = next_loc2
end
end
function Text.previous_screen_top1(State)
-- duplicate some logic from love.draw
-- does not modify State (except to populate line_cache)
@ -811,19 +832,21 @@ end
function Text.in_line(State, line_index, x,y)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
if line_cache.starty == nil then return false end -- outside current page
if y < line_cache.starty then return false end
local starty = Text.starty(State, line_index)
if starty == nil then return false end -- outside current page
if y < starty then return false end
Text.populate_screen_line_starting_pos(State, line_index)
return y < line_cache.starty + State.line_height*(#line_cache.screen_line_starting_pos - Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos) + 1)
return y < starty + State.line_height*(#line_cache.screen_line_starting_pos - Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos) + 1)
end
-- convert mx,my in pixels to schema-1 coordinates
function Text.to_pos_on_line(State, line_index, mx, my)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
assert(my >= line_cache.starty, 'failed to map y pixel to line')
local starty = Text.starty(State, line_index)
assert(my >= starty, 'failed to map y pixel to line')
-- duplicate some logic from Text.draw
local y = line_cache.starty
local y = starty
local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]

View File

@ -255,7 +255,7 @@ function test_click_moves_cursor()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line
edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
@ -812,7 +812,7 @@ function test_select_text_using_mouse()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line
-- press and hold on first location
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
-- drag and release somewhere else
@ -831,7 +831,7 @@ function test_select_text_using_mouse_starting_above_text()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line
-- press mouse above first line of text
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
@ -890,7 +890,7 @@ function test_select_text_using_mouse_and_shift()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line
-- click on first location
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
@ -914,7 +914,7 @@ function test_select_text_repeatedly_using_mouse_and_shift()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line
-- click on first location
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)

View File

@ -7,7 +7,6 @@ function Text.draw(State, line_index, y, startpos)
--? print('text.draw', line_index, y)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
line_cache.starty = y
line_cache.startpos = startpos
-- wrap long lines
Text.populate_screen_line_starting_pos(State, line_index)
@ -357,6 +356,31 @@ function Text.pageup(State)
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
end
-- return the top y coordinate of a given line_index,
-- or nil if no part of it is on screen
function Text.starty(State, line_index)
-- duplicate some logic from love.draw
-- does not modify State (except to populate line_cache)
if line_index < State.screen_top1.line then return end
local loc2 = Text.to2(State, State.screen_top1)
local y = State.top
while true do
if State.lines[loc2.line].mode == 'drawing' then
y = y + Drawing_padding_top
end
if loc2.line == line_index then return y end
if State.lines[loc2.line].mode == 'text' then
y = y + State.line_height
elseif State.lines[loc2.line].mode == 'drawing' then
y = y + Drawing.pixels(State.lines[loc2.line].h, State.width) + Drawing_padding_bottom
end
if y + State.line_height > App.screen.height then break end
local next_loc2 = Text.next_screen_line(State, loc2)
if Text.eq2(next_loc2, loc2) then break end -- end of file
loc2 = next_loc2
end
end
function Text.previous_screen_top1(State)
-- duplicate some logic from love.draw
-- does not modify State (except to populate line_cache)
@ -737,19 +761,21 @@ end
function Text.in_line(State, line_index, x,y)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
if line_cache.starty == nil then return false end -- outside current page
if y < line_cache.starty then return false end
local starty = Text.starty(State, line_index)
if starty == nil then return false end -- outside current page
if y < starty then return false end
Text.populate_screen_line_starting_pos(State, line_index)
return y < line_cache.starty + State.line_height*(#line_cache.screen_line_starting_pos - Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos) + 1)
return y < starty + State.line_height*(#line_cache.screen_line_starting_pos - Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos) + 1)
end
-- convert mx,my in pixels to schema-1 coordinates
function Text.to_pos_on_line(State, line_index, mx, my)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
assert(my >= line_cache.starty, 'failed to map y pixel to line')
local starty = Text.starty(State, line_index)
assert(my >= starty, 'failed to map y pixel to line')
-- duplicate some logic from Text.draw
local y = line_cache.starty
local y = starty
local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]

View File

@ -255,7 +255,7 @@ function test_click_moves_cursor()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line Editor_state.line_cache
edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
@ -812,7 +812,7 @@ function test_select_text_using_mouse()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line Editor_state.line_cache
-- press and hold on first location
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
-- drag and release somewhere else
@ -831,7 +831,7 @@ function test_select_text_using_mouse_starting_above_text()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line
-- press mouse above first line of text
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
@ -890,7 +890,7 @@ function test_select_text_using_mouse_and_shift()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line
-- click on first location
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
@ -914,7 +914,7 @@ function test_select_text_repeatedly_using_mouse_and_shift()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.selection1 = {}
edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
edit.draw(Editor_state) -- populate line_cache.startpos for each line
-- click on first location
edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)