diff --git a/example/slidingwindow/slidingwindow.collection b/example/slidingwindow/slidingwindow.collection index fcd88ef..bdca53d 100644 --- a/example/slidingwindow/slidingwindow.collection +++ b/example/slidingwindow/slidingwindow.collection @@ -58,11 +58,6 @@ embedded_instances { " value: \"window1\"\n" " type: PROPERTY_TYPE_HASH\n" " }\n" - " properties {\n" - " id: \"transition_url\"\n" - " value: \"window1:/go\"\n" - " type: PROPERTY_TYPE_URL\n" - " }\n" " property_decls {\n" " }\n" "}\n" @@ -123,11 +118,6 @@ embedded_instances { " value: \"window2\"\n" " type: PROPERTY_TYPE_HASH\n" " }\n" - " properties {\n" - " id: \"transition_url\"\n" - " value: \"window2:/go\"\n" - " type: PROPERTY_TYPE_URL\n" - " }\n" " property_decls {\n" " }\n" "}\n" diff --git a/example/slidingwindow/window1.gui b/example/slidingwindow/window1.gui index dcbd241..770393c 100644 --- a/example/slidingwindow/window1.gui +++ b/example/slidingwindow/window1.gui @@ -47,7 +47,7 @@ nodes { xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE pivot: PIVOT_CENTER - adjust_mode: ADJUST_MODE_FIT + adjust_mode: ADJUST_MODE_STRETCH layer: "" inherit_alpha: true slice9 { diff --git a/example/slidingwindow/window1.gui_script b/example/slidingwindow/window1.gui_script index 5aefe8e..73c12b0 100644 --- a/example/slidingwindow/window1.gui_script +++ b/example/slidingwindow/window1.gui_script @@ -2,12 +2,16 @@ local monarch = require "monarch.monarch" local transitions = require "monarch.transitions.gui" function init(self) - print("window2", msg.url()) - self.transition = transitions.create(gui.get_node("bg")) - .show_in(transitions.slide_in_right, gui.EASING_LINEAR, 0.3, 0) - .show_out(transitions.slide_out_left, gui.EASING_LINEAR, 0.3, 0) - .back_in(transitions.slide_in_left, gui.EASING_LINEAR, 0.3, 0) - .back_out(transitions.slide_out_right, gui.EASING_LINEAR, 0.3, 0) + print("window1", msg.url()) + + local DURATION = 1 + local transition = transitions.create(gui.get_node("bg")) + .show_in(transitions.slide_in_right, gui.EASING_LINEAR, DURATION, 0) + .show_out(transitions.slide_out_left, gui.EASING_LINEAR, DURATION, 0) + .back_in(transitions.slide_in_left, gui.EASING_LINEAR, DURATION, 0) + .back_out(transitions.slide_out_right, gui.EASING_LINEAR, DURATION, 0) + + monarch.on_transition("window1", transition) msg.post(".", "acquire_input_focus") end @@ -21,5 +25,5 @@ function on_input(self, action_id, action) end function on_message(self, message_id, message, sender) - self.transition.handle(message_id, message, sender) + monarch.on_message(message_id, message, sender) end \ No newline at end of file diff --git a/example/slidingwindow/window2.gui b/example/slidingwindow/window2.gui index a2468b3..2d600a3 100644 --- a/example/slidingwindow/window2.gui +++ b/example/slidingwindow/window2.gui @@ -47,7 +47,7 @@ nodes { xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE pivot: PIVOT_CENTER - adjust_mode: ADJUST_MODE_FIT + adjust_mode: ADJUST_MODE_STRETCH layer: "" inherit_alpha: true slice9 { diff --git a/example/slidingwindow/window2.gui_script b/example/slidingwindow/window2.gui_script index 59bb108..f81fbb2 100644 --- a/example/slidingwindow/window2.gui_script +++ b/example/slidingwindow/window2.gui_script @@ -3,11 +3,15 @@ local transitions = require "monarch.transitions.gui" function init(self) print("window2", msg.url()) - self.transition = transitions.create(gui.get_node("bg")) - .show_in(transitions.slide_in_right, gui.EASING_LINEAR, 0.3, 0) - .show_out(transitions.slide_out_left, gui.EASING_LINEAR, 0.3, 0) - .back_in(transitions.slide_in_left, gui.EASING_LINEAR, 0.3, 0) - .back_out(transitions.slide_out_right, gui.EASING_LINEAR, 0.3, 0) + + local DURATION = 1 + local transition = transitions.create(gui.get_node("bg")) + .show_in(transitions.slide_in_right, gui.EASING_LINEAR, DURATION, 0) + .show_out(transitions.slide_out_left, gui.EASING_LINEAR, DURATION, 0) + .back_in(transitions.slide_in_left, gui.EASING_LINEAR, DURATION, 0) + .back_out(transitions.slide_out_right, gui.EASING_LINEAR, DURATION, 0) + + monarch.on_transition("window2", transition) msg.post(".", "acquire_input_focus") end @@ -21,5 +25,5 @@ function on_input(self, action_id, action) end function on_message(self, message_id, message, sender) - self.transition.handle(message_id, message, sender) + monarch.on_message(message_id, message, sender) end \ No newline at end of file diff --git a/monarch/monarch.lua b/monarch/monarch.lua index 38c1077..ff5aac8 100644 --- a/monarch/monarch.lua +++ b/monarch/monarch.lua @@ -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 diff --git a/monarch/transitions/gui.lua b/monarch/transitions/gui.lua index e7eb3a6..0f67527 100644 --- a/monarch/transitions/gui.lua +++ b/monarch/transitions/gui.lua @@ -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)