3
0
mirror of https://github.com/britzl/monarch.git synced 2025-09-27 18:12:22 +02:00

Improve transition setup

This commit is contained in:
Björn Ritzl
2023-03-29 09:19:49 +02:00
parent e0b0a286e3
commit a055af032f
7 changed files with 75 additions and 42 deletions

View File

@@ -6,6 +6,7 @@ local M = {}
local CONTEXT = hash("monarch_context")
local PROXY_LOADED = hash("proxy_loaded")
local PROXY_UNLOADED = hash("proxy_unloaded")
local LAYOUT_CHANGED = hash("layout_changed")
local RELEASE_INPUT_FOCUS = hash("release_input_focus")
local ACQUIRE_INPUT_FOCUS = hash("acquire_input_focus")
@@ -134,18 +135,22 @@ local function notify_transition_listeners(message_id, message)
end
end
local function screen_from_proxy(proxy)
local function find_screen(url)
local current_url = msg.url()
for _,screen in pairs(screens) do
if screen.proxy == proxy then
if screen.transition_url == current_url
or screen.script == current_url
or screen.proxy == current_url
then
return screen
end
end
end
local function screen_from_script()
local url = msg.url()
for _,screen in pairs(screens) do
if screen.script == url then
if screen.transition_url == url
or screen.script == url
or screen.proxy == url
then
return screen
end
end
@@ -612,6 +617,7 @@ local function show_in(screen, previous_screen, reload, add_to_stack, wait_for_t
notify_transition_listeners(M.SCREEN_TRANSITION_FAILED, { screen = screen.id })
return
end
cowait(0)
reset_timestep(screen)
transition(screen, M.TRANSITION.SHOW_IN, { previous_screen = previous_screen and previous_screen.id }, wait_for_transition)
screen.visible = true
@@ -1128,29 +1134,44 @@ end
function M.on_message(message_id, message, sender)
if message_id == PROXY_LOADED then
local screen = screen_from_proxy(sender)
local screen = find_screen(sender)
assert(screen, "Unable to find screen for loaded proxy")
if screen.wait_for == PROXY_LOADED then
assert(coroutine.resume(screen.co))
end
elseif message_id == PROXY_UNLOADED then
local screen = screen_from_proxy(sender)
local screen = find_screen(sender)
assert(screen, "Unable to find screen for unloaded proxy")
if screen.wait_for == PROXY_UNLOADED then
assert(coroutine.resume(screen.co))
end
elseif message_id == CONTEXT then
local screen = screen_from_script()
local screen = find_screen(sender)
assert(screen, "Unable to find screen for current script url")
if screen.wait_for == CONTEXT then
assert(coroutine.resume(screen.co))
end
elseif message_id == M.TRANSITION.DONE then
local screen = screen_from_script()
assert(screen, "Unable to find screen for current script url")
local screen = find_screen(sender)
assert(screen, "Unable to find screen for transition")
if screen.wait_for == M.TRANSITION.DONE then
assert(coroutine.resume(screen.co))
end
elseif message_id == M.TRANSITION.SHOW_IN
or message_id == M.TRANSITION.SHOW_OUT
or message_id == M.TRANSITION.BACK_IN
or message_id == M.TRANSITION.BACK_OUT
then
local screen = find_screen(sender)
assert(screen, "Unable to find screen for transition")
if screen.transition_fn then
screen.transition_fn(message_id, message, sender)
end
elseif message_id == LAYOUT_CHANGED then
local screen = find_screen(sender)
if screen and screen.transition_fn then
screen.transition_fn(message_id, message, sender)
end
end
end
@@ -1197,6 +1218,17 @@ function M.set_timestep_below_popup(id, timestep)
end
function M.on_transition(id, fn)
assert(id, "You must provide a screen id")
assert(fn, "You must provide a transition function")
id = tohash(id)
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
local screen = screens[id]
screen.transition_url = msg.url()
screen.transition_fn = fn
end
local function url_to_key(url)
return (url.socket or hash("")) .. (url.path or hash("")) .. (url.fragment or hash(""))
end

View File

@@ -20,10 +20,10 @@ local LAYOUT_CHANGED = hash("layout_changed")
function M.window_resized(width, height)
WIDTH = width
HEIGHT = height
LEFT = vmath.vector3(-WIDTH * 2, 0, 0)
RIGHT = vmath.vector3(WIDTH * 2, 0, 0)
TOP = vmath.vector3(0, HEIGHT * 2, 0)
BOTTOM = vmath.vector3(0, - HEIGHT * 2, 0)
LEFT = vmath.vector3(-WIDTH, 0, 0)
RIGHT = vmath.vector3(WIDTH, 0, 0)
TOP = vmath.vector3(0, HEIGHT, 0)
BOTTOM = vmath.vector3(0, - HEIGHT, 0)
end
M.window_resized(tonumber(sys.get_config("display.width")), tonumber(sys.get_config("display.height")))
@@ -176,7 +176,6 @@ local function create()
local t = transitions[transition_id]
table.insert(t.urls, url)
if t.in_progress_count == 0 then
table.insert(t.urls, msg.url())
current_transition = t
current_transition.id = transition_id
if #t.transitions > 0 then
@@ -254,7 +253,11 @@ local function create()
return instance
end
return instance
return setmetatable(instance, {
__call = function(t, ...)
return instance.handle(...)
end
})
end
function M.create(node)