3
0
mirror of https://github.com/britzl/monarch.git synced 2025-06-27 10:27:49 +02:00

Simplified coroutine usage when running screen code

This commit is contained in:
Björn Ritzl 2019-03-08 09:11:37 +01:00
parent 66bdde41ed
commit 1bc0ae09ee

View File

@ -382,11 +382,21 @@ local function reset_timestep(screen)
end end
end end
local function disable(screen, next_screen) local function run_coroutine(screen, cb, fn)
log("disable()", screen.id)
local co local co
co = coroutine.create(function() co = coroutine.create(function()
screen.co = co screen.co = co
fn()
screen.co = nil
pcallfn(cb)
notify_if_idle()
end)
assert(coroutine.resume(co))
end
local function disable(screen, next_screen)
log("disable()", screen.id)
run_coroutine(screen, nil, function()
change_context(screen) change_context(screen)
release_input(screen) release_input(screen)
focus_lost(screen, next_screen) focus_lost(screen, next_screen)
@ -395,33 +405,23 @@ local function disable(screen, next_screen)
else else
reset_timestep(screen) reset_timestep(screen)
end end
screen.co = nil
if cb then cb() end
end) end)
assert(coroutine.resume(co))
end end
local function enable(screen, previous_screen) local function enable(screen, previous_screen)
log("enable()", screen.id) log("enable()", screen.id)
local co run_coroutine(screen, nil, function()
co = coroutine.create(function()
screen.co = co
change_context(screen) change_context(screen)
acquire_input(screen) acquire_input(screen)
focus_gained(screen, previous_screen) focus_gained(screen, previous_screen)
reset_timestep(screen) reset_timestep(screen)
screen.co = nil
if cb then cb() end
end) end)
assert(coroutine.resume(co))
end end
local function show_out(screen, next_screen, cb) local function show_out(screen, next_screen, cb)
log("show_out()", screen.id) log("show_out()", screen.id)
local co run_coroutine(screen, cb, function()
co = coroutine.create(function()
active_transition_count = active_transition_count + 1 active_transition_count = active_transition_count + 1
screen.co = co
notify_transition_listeners(M.SCREEN_TRANSITION_OUT_STARTED, { screen = screen.id, next_screen = next_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_OUT_STARTED, { screen = screen.id, next_screen = next_screen.id })
change_context(screen) change_context(screen)
release_input(screen) release_input(screen)
@ -437,20 +437,15 @@ local function show_out(screen, next_screen, cb)
elseif next_is_popup then elseif next_is_popup then
change_timestep(screen) change_timestep(screen)
end end
screen.co = nil
active_transition_count = active_transition_count - 1 active_transition_count = active_transition_count - 1
if cb then cb() end
notify_transition_listeners(M.SCREEN_TRANSITION_OUT_FINISHED, { screen = screen.id, next_screen = next_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_OUT_FINISHED, { screen = screen.id, next_screen = next_screen.id })
end) end)
coroutine.resume(co)
end end
local function show_in(screen, previous_screen, reload, add_to_stack, cb) local function show_in(screen, previous_screen, reload, add_to_stack, cb)
log("show_in()", screen.id) log("show_in()", screen.id)
local co run_coroutine(screen, cb, function()
co = coroutine.create(function()
active_transition_count = active_transition_count + 1 active_transition_count = active_transition_count + 1
screen.co = co
notify_transition_listeners(M.SCREEN_TRANSITION_IN_STARTED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_IN_STARTED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id })
change_context(screen) change_context(screen)
if reload and screen.loaded then if reload and screen.loaded then
@ -465,20 +460,15 @@ local function show_in(screen, previous_screen, reload, add_to_stack, cb)
transition(screen, M.TRANSITION.SHOW_IN, { previous_screen = previous_screen and previous_screen.id }) transition(screen, M.TRANSITION.SHOW_IN, { previous_screen = previous_screen and previous_screen.id })
acquire_input(screen) acquire_input(screen)
focus_gained(screen, previous_screen) focus_gained(screen, previous_screen)
screen.co = nil
active_transition_count = active_transition_count - 1 active_transition_count = active_transition_count - 1
if cb then cb() end
notify_transition_listeners(M.SCREEN_TRANSITION_IN_FINISHED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_IN_FINISHED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id })
end) end)
coroutine.resume(co)
end end
local function back_in(screen, previous_screen, cb) local function back_in(screen, previous_screen, cb)
log("back_in()", screen.id) log("back_in()", screen.id)
local co run_coroutine(screen, cb, function()
co = coroutine.create(function()
active_transition_count = active_transition_count + 1 active_transition_count = active_transition_count + 1
screen.co = co
notify_transition_listeners(M.SCREEN_TRANSITION_IN_STARTED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_IN_STARTED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id })
change_context(screen) change_context(screen)
load(screen) load(screen)
@ -488,21 +478,16 @@ local function back_in(screen, previous_screen, cb)
end end
acquire_input(screen) acquire_input(screen)
focus_gained(screen, previous_screen) focus_gained(screen, previous_screen)
screen.co = nil
active_transition_count = active_transition_count - 1 active_transition_count = active_transition_count - 1
if cb then cb() end
notify_transition_listeners(M.SCREEN_TRANSITION_IN_FINISHED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_IN_FINISHED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id })
end) end)
coroutine.resume(co)
end end
local function back_out(screen, next_screen, cb) local function back_out(screen, next_screen, cb)
log("back_out()", screen.id) log("back_out()", screen.id)
local co run_coroutine(screen, cb, function()
co = coroutine.create(function()
notify_transition_listeners(M.SCREEN_TRANSITION_OUT_STARTED, { screen = screen.id, next_screen = next_screen and next_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_OUT_STARTED, { screen = screen.id, next_screen = next_screen and next_screen.id })
active_transition_count = active_transition_count + 1 active_transition_count = active_transition_count + 1
screen.co = co
change_context(screen) change_context(screen)
release_input(screen) release_input(screen)
focus_lost(screen, next_screen) focus_lost(screen, next_screen)
@ -511,12 +496,9 @@ local function back_out(screen, next_screen, cb)
end end
transition(screen, M.TRANSITION.BACK_OUT, { next_screen = next_screen and next_screen.id }) transition(screen, M.TRANSITION.BACK_OUT, { next_screen = next_screen and next_screen.id })
unload(screen) unload(screen)
screen.co = nil
active_transition_count = active_transition_count - 1 active_transition_count = active_transition_count - 1
if cb then cb() end
notify_transition_listeners(M.SCREEN_TRANSITION_OUT_FINISHED, { screen = screen.id, next_screen = next_screen and next_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_OUT_FINISHED, { screen = screen.id, next_screen = next_screen and next_screen.id })
end) end)
coroutine.resume(co)
end end
@ -722,15 +704,10 @@ function M.preload(id, cb)
pcallfn(cb) pcallfn(cb)
return true return true
end end
local co run_coroutine(screen, cb, function()
co = coroutine.create(function()
screen.co = co
change_context(screen) change_context(screen)
preload(screen) preload(screen)
log("preload() done", screen.id)
if cb then cb() end
end) end)
assert(coroutine.resume(co))
return true return true
end end
@ -755,14 +732,10 @@ function M.unload(id, cb)
pcallfn(cb) pcallfn(cb)
return true return true
end end
local co run_coroutine(screen, cb, function()
co = coroutine.create(function()
screen.co = co
change_context(screen) change_context(screen)
unload(screen) unload(screen)
if cb then cb() end
end) end)
assert(coroutine.resume(co))
return true return true
end end