diff --git a/README.md b/README.md index 5e98ee3..aa68523 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,9 @@ For proxies the recommended setup is to create one game object per screen and pe * **Timestep below Popup (number)** - Timestep to set on screen proxy when it is below a popup. This is useful when pausing animations and gameplay while a popup is open. * **Screen Keeps Input Focus When Below Popup (boolean)** - Check this if the screen should keep input focus when it is below a popup. * **Others Keep Input Focus When Below Screen (boolean)** - Check this if other screens should keep input focus when below this screen. -* **Transition Url (url)** - Optional URL to post messages to when the screen is about to be shown/hidden. Use this to trigger a transition (see the section on [transitions](#transitions)). -* **Focus Url (url)** - Optional URL to post messages to when the screen gains or loses focus (see the section on [screen focus](#screen-focus-gainloss)). -* **Receiver Url (url)** - Optional URL to post messages to using `monarch.post()`. +* **Transition Url (url)** - **DEPRECATED** Optional URL to post messages to when the screen is about to be shown/hidden. Use this to trigger a transition (see the section on [transitions](#transitions)). +* **Focus Url (url)** - **DEPRECATED** Optional URL to post messages to when the screen gains or loses focus (see the section on [screen focus](#screen-focus-gainloss)). +* **Receiver Url (url)** - **DEPRECATED** Optional URL to post messages to using `monarch.post()`. * **Preload (boolean)** - Check this if the screen should be preloaded and kept loaded at all times. For a collection proxy it means that it will be async loaded but not enabled at all times while not visible. This can also temporarily be achieved through the `monarch.preload()` function. ![](docs/setup_proxy.png) @@ -53,8 +53,8 @@ For factories the recommended setup is to create one game object per screen and * **Popup on Popup (boolean)** - Check this if the screen is a [popup](#popups) and it can be shown on top of other popups. * **Screen Keeps Input Focus When Below Popup (boolean)** - Check this if the screen should keep input focus when it is below a popup. * **Others Keep Input Focus When Below Screen (boolean)** - Check this if other screens should keep input focus when below this screen. -* **Transition Id (hash)** - Optional id of the game object to send a message to when the screen is about to be shown/hidden. Use this to trigger a transition (see the section on [transitions](#transitions)). -* **Focus Id (hash)** - Optional id of the game object to send a message to when the screen gains or loses focus (see the section on [screen focus](#screen-focus-gainloss)). +* **Transition Id (hash)** - **DEPRECATED** Optional id of the game object to send a message to when the screen is about to be shown/hidden. Use this to trigger a transition (see the section on [transitions](#transitions)). +* **Focus Id (hash)** - **DEPRECATED** Optional id of the game object to send a message to when the screen gains or loses focus (see the section on [screen focus](#screen-focus-gainloss)). * **Preload (boolean)** - Check this if the screen should be preloaded and kept loaded at all times. For a collection factory this means that its resources will be dynamically loaded at all times. This can also temporarily be achieved through the `monarch.preload()` function. ![](docs/setup_factory.png) @@ -161,18 +161,30 @@ You can add optional transitions when navigating between screens. This is [descr ## Screen focus gain/loss -Monarch will send focus gain and focus loss messages if a `Focus Url` (proxy) or `Focus Id` (collectionfactory) was provided when the screen was created. The focus gained message will contain the id of the previous screen and the focus loss message will contain the id of the next screen. Example: +Monarch will send focus gain and focus loss messages if a focus change listener has been set using `monarch.on_focus_change(screen_id, fn)` +DEPRECATED: ~~Monarch will send focus gain and focus loss messages if a `Focus Url` (proxy) or `Focus Id` (collectionfactory) was provided when the screen was created.~~ + +The focus gained message will contain the id of the previous screen and the focus loss message will contain the id of the next screen. Example: + +```lua local monarch = require "monarch.monarch" - function on_message(self, message_id, message, sender) - if message_id == monarch.FOCUS.GAINED then - print("Focus gained, previous screen: ", message.id) - elseif message_id == monarch.FOCUS.LOST then - print("Focus lost, next screen: ", message.id) - end + function init(self) + monarch.on_focus_changed("foobar", function(message_id, message, sender) + if message_id == monarch.FOCUS.GAINED then + print("Focus gained, previous screen: ", message.id) + elseif message_id == monarch.FOCUS.LOST then + print("Focus lost, next screen: ", message.id) + end + end) end + function on_message(self, message_id, message, sender) + monarch.on_message(message_id, message, sender) + end +``` + ## Callbacks Both the `monarch.show()` and `monarch.back()` functions take an optional callback function that will be invoked when the `transition_show_in` (or the `transition_back_in` in the case of a `monarch.back()` call) transition is completed. The transition is considered completed when a `transition_done` message has been received (see section on [transitions](#transitions) above). @@ -180,10 +192,3 @@ Both the `monarch.show()` and `monarch.back()` functions take an optional callba ## Monarch API The full [Monarch API is documented here](/README_API.md). - - -## Monarch FAQ - -**Q**: Why am I getting `ERROR GAMEOBJECT: The collection 'default' could not be created since there is already a socket with the same name`? - -**A**: Each collection that you use must be given a unique id. In this case you have more than one collection loaded with the id `default`. Select the root of each collection in the Outline panel and change the Name field in the properties panel from the default value of `default`. diff --git a/README_API.md b/README_API.md index cfdf941..815e9cd 100644 --- a/README_API.md +++ b/README_API.md @@ -189,6 +189,32 @@ Post a message to a visible screen. If the screen is created through a collectio * `error` (string|nil) - Error message if unable to send message +## monarch.on_transition(screen_id, fn) +Set a function to be called when a screen should transition in our out. The function will receive (message_id, message, sender) with `message_id` being one of the transition constants. +IMPORTANT! You must call `monarch.on_message(message_id, message, sender)` from the same script as this function was called. + +**PARAMETERS** +* `screen_id` (string|hash) - Id of the screen +* `fn` (function) - The function to call when a transition should start + + +## monarch.on_focus_change(screen_id, fn) +Set a function to be called when a screen gains or loses focus. The function will receive (message_id, message, sender) with `message_id` being one of the focus change constants. +IMPORTANT! You must call `monarch.on_message(message_id, message, sender)` from the same script as this function was called. + +**PARAMETERS** +* `screen_id` (string|hash) - Id of the screen +* `fn` (function) - The function to call screen focus changes + + +## monarch.on_post(screen_id, fn) +Set a function to be called when `msg.post()` is called on a specific screen. IMPORTANT! You must call `monarch.on_message(message_id, message, sender)` from the same script as this function was called. + +**PARAMETERS** +* `screen_id` (string|hash) - Id of the screen +* `fn` (function) - The function to call when the screen receives a message using `msg.post` + + ## monarch.debug() Enable verbose logging of the internals of Monarch. diff --git a/README_TRANSITIONS.md b/README_TRANSITIONS.md index f2f9789..c21ae7b 100644 --- a/README_TRANSITIONS.md +++ b/README_TRANSITIONS.md @@ -1,5 +1,7 @@ # Transitions -You can add optional transitions when navigating between screens. The default behavior is that screen navigation is instant but if you have defined a transition for a screen Monarch will wait until the transition is completed before proceeding. The `Transition Url` (proxy) or `Transition Id` (collectionfactory) property described above should be the URL/Id to a script with an `on_message` handlers for the following messages: +You can add optional transitions when navigating between screens. The default behavior is that screen navigation is instant but if you have defined a transition for a screen Monarch will wait until the transition is completed before proceeding. + +Transitions are configured through the `monarch.on_transition(screen_id, fn)` function. The function defines for which screen to configure transitions and sets a function to be called when a transition should be started. This function must accept (message_id, message, sender) as arguments, with `message_id` defining which type of transition to start: * `transition_show_in` (constant defined as `monarch.TRANSITION.SHOW_IN`) * `transition_show_out` (constant defined as `monarch.TRANSITION.SHOW_OUT`) @@ -8,6 +10,11 @@ You can add optional transitions when navigating between screens. The default be When a transition is completed it is up to the developer to send a `transition_done` (constant `monarch.TRANSITION.DONE`) message back to the sender to indicate that the transition is completed and that Monarch can continue the navigation sequence. +## Transition URL +This property is deprecated and will be removed in a future version of Monarch. + +~~It is also possible to configure transitions through the `Transition Url` (proxy) or `Transition Id` (collectionfactory) property. This property must be the URL/Id to a script with an `on_message` handlers for the transition messages mentioned above.~~ + ## Predefined transitions Monarch comes with a system for setting up transitions easily in a gui_script using the `monarch.transitions.gui` module. Example: @@ -20,15 +27,17 @@ function init(self) -- create transitions for the node 'root' -- the node will slide in/out from left and right with -- a specific easing, duration and delay - self.transition = transitions.create(gui.get_node("root")) + local transition = transitions.create(gui.get_node("root")) .show_in(transitions.slide_in_right, gui.EASING_OUTQUAD, 0.6, 0) .show_out(transitions.slide_out_left, gui.EASING_INQUAD, 0.6, 0) .back_in(transitions.slide_in_left, gui.EASING_OUTQUAD, 0.6, 0) .back_out(transitions.slide_out_right, gui.EASING_INQUAD, 0.6, 0) + + monarch.on_transition("foobar", transition) end function on_message(self, message_id, message, sender) - self.transition.handle(message_id, message, sender) + monarch.on_message(message_id, message, sender) -- you can also check when a transition has completed: if message_id == monarch.TRANSITION.DONE and message.transition == monarch.TRANSITION.SHOW_IN then print("Show in done!") @@ -40,7 +49,7 @@ It is also possible to assign transitions to multiple nodes: ```lua function init(self) - self.transition = transitions.create() -- note that no node is passed to transition.create()! + local transition = transitions.create() -- note that no node is passed to transition.create()! .show_in(gui.get_node("node1"), transitions.slide_in_right, gui.EASING_OUTQUAD, 0.6, 0) .show_in(gui.get_node("node2"), transitions.slide_in_right, gui.EASING_OUTQUAD, 0.6, 0) end @@ -111,7 +120,7 @@ end ``` ## Screen stack info and transitions -The transition message sent to the Transition Url specified in the screen configuration contains additional information about the transition. For the `transition_show_in` and `transition_back_out` messages the message contains the previous screen id: +The transition message sent transition listener script contains additional information about the transition. For the `transition_show_in` and `transition_back_out` messages the message contains the previous screen id: ```lua function on_message(self, message_id, message, sender) 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..fef770b 100644 --- a/example/slidingwindow/window1.gui_script +++ b/example/slidingwindow/window1.gui_script @@ -2,14 +2,24 @@ 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) - msg.post(".", "acquire_input_focus") + + local DURATION = 0.3 + 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) + + monarch.on_focus_changed("window1", function(message_id, message) + if message_id == monarch.FOCUS.GAINED then + print("window1 gained focus") + elseif message_id == monarch.FOCUS.LOST then + print("window1 lost focus") + end + end) end function on_input(self, action_id, action) @@ -21,5 +31,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..15bd842 100644 --- a/example/slidingwindow/window2.gui_script +++ b/example/slidingwindow/window2.gui_script @@ -2,14 +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) - msg.post(".", "acquire_input_focus") + + local DURATION = 0.3 + 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) end function on_input(self, action_id, action) @@ -21,5 +23,6 @@ function on_input(self, action_id, action) end function on_message(self, message_id, message, sender) - self.transition.handle(message_id, message, sender) + print("window2", 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..0e6fa64 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,23 +135,49 @@ local function notify_transition_listeners(message_id, message) end end -local function screen_from_proxy(proxy) - for _,screen in pairs(screens) do - if screen.proxy == proxy then - return screen +local function find_screen(url_to_find) + local function find(url) + for _,screen in pairs(screens) do + if screen.script == url or screen.proxy == url then + return screen + end end end + return find(msg.url()) or find(url_to_find) end -local function screen_from_script() - local url = msg.url() - for _,screen in pairs(screens) do - if screen.script == url then - return screen +local function find_transition_screen(url_to_find) + local function find(url) + for _,screen in pairs(screens) do + if screen.transition_url == url or screen.script == url or screen.proxy == url then + return screen + end end end + return find(msg.url()) or find(url_to_find) end +local function find_focus_screen(url_to_find) + local function find(url) + for _,screen in pairs(screens) do + if screen.focus_url == url or screen.script == url or screen.proxy == url then + return screen + end + end + end + return find(msg.url()) or find(url_to_find) +end + +local function find_post_receiver_screen(url_to_find) + local function find(url) + for _,screen in pairs(screens) do + if screen.receiver_url == url or screen.script == url or screen.proxy == url then + return screen + end + end + end + return find(msg.url()) or find(url_to_find) +end --- Check if a screen exists in the current screen stack -- @param id (string|hash) @@ -612,6 +639,8 @@ 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 + -- wait one frame so that the init() of any script have time to run before starting transitions + 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 @@ -636,6 +665,8 @@ local function back_in(screen, previous_screen, wait_for_transition, cb) notify_transition_listeners(M.SCREEN_TRANSITION_FAILED, { screen = screen.id }) return end + -- wait one frame so that the init() of any script have time to run before starting transitions + cowait(0) reset_timestep(screen) if previous_screen and not previous_screen.popup then transition(screen, M.TRANSITION.BACK_IN, { previous_screen = previous_screen.id }, wait_for_transition) @@ -1128,29 +1159,57 @@ 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_transition_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_transition_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 + elseif message_id == M.FOCUS.GAINED + or message_id == M.FOCUS.LOST + then + local screen = find_focus_screen(sender) + assert(screen, "Unable to find screen for focus change") + if screen.focus_fn then + screen.focus_fn(message_id, message, sender) + end + else + local screen = find_post_receiver_screen(sender) + if screen and screen.receiver_fn then + screen.receiver_fn(message_id, message, sender) + end end end @@ -1197,6 +1256,57 @@ function M.set_timestep_below_popup(id, timestep) end +--- +-- Set a function to call when a transition should be started +-- The function will receive (message_id, message, sender) +-- IMPORTANT! You must call monarch.on_message() from the same script as +-- this function was called +-- @param id Screen id to associate transition with +-- @param fn Transition handler function +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 + +--- +-- Set a function to call when a screen gains or loses focus +-- The function will receive (message_id, message, sender) +-- IMPORTANT! You must call monarch.on_message() from the same script as +-- this function was called +-- @param id Screen id to associate focus listener function with +-- @param fn Focus listener function +function M.on_focus_changed(id, fn) + assert(id, "You must provide a screen id") + assert(fn, "You must provide a focus change function") + id = tohash(id) + assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id))) + local screen = screens[id] + screen.focus_url = msg.url() + screen.focus_fn = fn +end + +--- +-- Set a function to call when a screen is sent a message using monarch.post() +-- The function will receive (message_id, message, sender) +-- IMPORTANT! You must call monarch.on_message() from the same script as +-- this function was called +-- @param id Screen id to associate the message listener function with +-- @param fn Message listener function +function M.on_post(id, fn) + assert(id, "You must provide a screen id") + assert(fn, "You must provide a post receiver function") + id = tohash(id) + assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id))) + local screen = screens[id] + screen.receiver_url = msg.url() + screen.receiver_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 12bb0e8..25c495b 100644 --- a/monarch/transitions/gui.lua +++ b/monarch/transitions/gui.lua @@ -173,10 +173,10 @@ local function create() end local function start_transition(transition_id, url) + url = url or msg.url() 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 +254,11 @@ local function create() return instance end - return instance + return setmetatable(instance, { + __call = function(t, ...) + return instance.handle(...) + end + }) end function M.create(node)