starting to experiment with animated pane transitions

This commit is contained in:
Kartik K. Agaram 2023-11-19 17:33:22 -08:00
parent 20f95bc766
commit 47d38eabf2
10 changed files with 71 additions and 11 deletions

View File

@ -1,8 +1,5 @@
-- A debugging aid to help animate intermediate results within f.
-- Pause animation in the current frame using loiter().
-- Try to only have one such call in your program.
-- You can have multiple, but things might get confusing if one of them indirectly calls the other,
-- or more generally if a single function ever loiters sometimes under the call tree of one and sometimes under the other.
-- Animate 'f' by smearing its work across multiple frames.
-- Pause animation in the current frame using end_frame().
animate = function(f, ...)
local args = {...}
Error_with_callstack = nil
@ -17,5 +14,5 @@ animate = function(f, ...)
if Error_with_callstack then
error(Error_with_callstack)
end
table.insert(Debug_animations_in_progress, {co=co, next_run=Current_time+0.3})
end
table.insert(Animations_in_progress, {co=co})
end

View File

@ -1,5 +1,5 @@
-- A debugging aid to animate intermediate results in computations.
-- Can only be called from functions invoked using `animate()`.
-- Can only be called from functions invoked using `enable_loiter()`.
loiter = function()
coroutine.yield()
end
end

View File

@ -1,7 +1,7 @@
-- Internal helper for animate()
-- Internal helper for animations.
-- Records the stack of function calls that led to any error within a debug animation coroutine.
-- Lua normally prints out errors with callstacks, but coroutines interrupt the stack unless we do some additional work.
save_callstack = function(err)
local callstack = debug.traceback('', --[[stack frame]]2)
Error_with_callstack = 'Error: ' .. cleaned_up_frame(tostring(err))..'\n'..cleaned_up_callstack(callstack)
end
end

View File

@ -10,4 +10,5 @@ on.draw = function()
edit.draw(Current_pane.output_editor_state, Normal_color, --[[hide cursor]] true)
draw_scrollbar(Current_pane.output_editor_state)
draw_menu()
draw_next_frames_of_animations()
end

View File

@ -25,6 +25,7 @@ draw_menu = function()
onpress1 = function()
Current_pane_index = Current_pane_index-1
Current_pane = Panes[Current_pane_index]
animate(slide_canvas(Current_pane_index+1, 'right'))
end,
})
end
@ -39,6 +40,7 @@ draw_menu = function()
table.insert(Panes, new_pane())
Current_pane_index = Current_pane_index+1
Current_pane = Panes[Current_pane_index]
animate(slide_canvas(Current_pane_index-1, 'left'))
end,
})
else
@ -50,6 +52,7 @@ draw_menu = function()
onpress1 = function()
Current_pane_index = Current_pane_index+1
Current_pane = Panes[Current_pane_index]
animate(slide_canvas(Current_pane_index-1, 'left'))
end,
})
end

21
0052-enable_loiter Normal file
View File

@ -0,0 +1,21 @@
-- A debugging aid to help animate intermediate results within f.
-- Pause animation in the current frame using loiter().
-- Try to only have one such call in your program.
-- You can have multiple, but things might get confusing if one of them indirectly calls the other,
-- or more generally if a single function ever loiters sometimes under the call tree of one and sometimes under the other.
enable_loiter = function(f, ...)
local args = {...}
Error_with_callstack = nil
local co = coroutine.create(
function()
xpcall(function()
f(unpack(args))
end,
save_callstack)
end)
coroutine.resume(co, ...)
if Error_with_callstack then
error(Error_with_callstack)
end
table.insert(Debug_animations_in_progress, {co=co, next_run=Current_time+0.3})
end

View File

@ -0,0 +1,2 @@
-- Intermediate state during animations.
Animations_in_progress = {}

4
0056-end_frame Normal file
View File

@ -0,0 +1,4 @@
-- Pause a drawing called by animate().
end_frame = function()
coroutine.yield()
end

View File

@ -0,0 +1,14 @@
-- update any in-progress animations
-- return whether any work remains
draw_next_frames_of_animations = function()
local a = Animations_in_progress
for i=#a,1,-1 do
if coroutine.status(a[i].co) == 'dead' then
table.remove(a, i)
else
local status, err = coroutine.resume(a[i].co)
if status == false then error(err) end
end
end
return #a > 0
end

18
0059-slide_canvas Normal file
View File

@ -0,0 +1,18 @@
slide_canvas = function(pane_index, dir)
return function()
end_frame()
if dir == 'right' then
for i=1,40 do
print(Current_time, i)
love.graphics.rectangle('fill', i*10,0, 30, App.screen.height)
end_frame()
end
elseif dir == 'left' then
for i=20,1,-1 do
print(i)
love.graphics.line(i*50,0, i*50, App.screen.height)
end_frame()
end
end
end
end