Skip to content

Commit 2eb67bf

Browse files
committed
Squashed commit of the following:
commit 698cdba Author: Björn Ritzl <bjorn.ritzl@gmail.com> Date: Wed Mar 29 11:10:49 2023 +0200 Documentation commit d8de338 Author: Björn Ritzl <bjorn.ritzl@gmail.com> Date: Wed Mar 29 10:40:38 2023 +0200 Added focus and post listener setup functions commit 55910ab Author: Björn Ritzl <bjorn.ritzl@gmail.com> Date: Wed Mar 29 09:49:00 2023 +0200 Update gui.lua commit a055af0 Author: Björn Ritzl <bjorn.ritzl@gmail.com> Date: Wed Mar 29 09:19:49 2023 +0200 Improve transition setup
1 parent fe83412 commit 2eb67bf

File tree

10 files changed

+224
-67
lines changed

10 files changed

+224
-67
lines changed

README.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ For proxies the recommended setup is to create one game object per screen and pe
3737
* **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.
3838
* **Screen Keeps Input Focus When Below Popup (boolean)** - Check this if the screen should keep input focus when it is below a popup.
3939
* **Others Keep Input Focus When Below Screen (boolean)** - Check this if other screens should keep input focus when below this screen.
40-
* **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)).
41-
* **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)).
42-
* **Receiver Url (url)** - Optional URL to post messages to using `monarch.post()`.
40+
* **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)).
41+
* **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)).
42+
* **Receiver Url (url)** - **DEPRECATED** Optional URL to post messages to using `monarch.post()`.
4343
* **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.
4444

4545
![](docs/setup_proxy.png)
@@ -53,8 +53,8 @@ For factories the recommended setup is to create one game object per screen and
5353
* **Popup on Popup (boolean)** - Check this if the screen is a [popup](#popups) and it can be shown on top of other popups.
5454
* **Screen Keeps Input Focus When Below Popup (boolean)** - Check this if the screen should keep input focus when it is below a popup.
5555
* **Others Keep Input Focus When Below Screen (boolean)** - Check this if other screens should keep input focus when below this screen.
56-
* **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)).
57-
* **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)).
56+
* **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)).
57+
* **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)).
5858
* **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.
5959

6060
![](docs/setup_factory.png)
@@ -161,17 +161,29 @@ You can add optional transitions when navigating between screens. This is [descr
161161

162162

163163
## Screen focus gain/loss
164-
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:
164+
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)`
165165

166+
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.~~
167+
168+
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:
169+
170+
```lua
166171
local monarch = require "monarch.monarch"
167172

173+
function init(self)
174+
monarch.on_focus_changed("foobar", function(message_id, message, sender)
175+
if message_id == monarch.FOCUS.GAINED then
176+
print("Focus gained, previous screen: ", message.id)
177+
elseif message_id == monarch.FOCUS.LOST then
178+
print("Focus lost, next screen: ", message.id)
179+
end
180+
end)
181+
end
182+
168183
function on_message(self, message_id, message, sender)
169-
if message_id == monarch.FOCUS.GAINED then
170-
print("Focus gained, previous screen: ", message.id)
171-
elseif message_id == monarch.FOCUS.LOST then
172-
print("Focus lost, next screen: ", message.id)
173-
end
184+
monarch.on_message(message_id, message, sender)
174185
end
186+
```
175187

176188

177189
## Callbacks
@@ -180,10 +192,3 @@ Both the `monarch.show()` and `monarch.back()` functions take an optional callba
180192

181193
## Monarch API
182194
The full [Monarch API is documented here](/README_API.md).
183-
184-
185-
## Monarch FAQ
186-
187-
**Q**: Why am I getting `ERROR GAMEOBJECT: The collection 'default' could not be created since there is already a socket with the same name`?
188-
189-
**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`.

README_API.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@ Post a message to a visible screen. If the screen is created through a collectio
189189
* `error` (string|nil) - Error message if unable to send message
190190

191191

192+
## monarch.on_transition(screen_id, fn)
193+
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.
194+
IMPORTANT! You must call `monarch.on_message(message_id, message, sender)` from the same script as this function was called.
195+
196+
**PARAMETERS**
197+
* `screen_id` (string|hash) - Id of the screen
198+
* `fn` (function) - The function to call when a transition should start
199+
200+
201+
## monarch.on_focus_change(screen_id, fn)
202+
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.
203+
IMPORTANT! You must call `monarch.on_message(message_id, message, sender)` from the same script as this function was called.
204+
205+
**PARAMETERS**
206+
* `screen_id` (string|hash) - Id of the screen
207+
* `fn` (function) - The function to call screen focus changes
208+
209+
210+
## monarch.on_post(screen_id, fn)
211+
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.
212+
213+
**PARAMETERS**
214+
* `screen_id` (string|hash) - Id of the screen
215+
* `fn` (function) - The function to call when the screen receives a message using `msg.post`
216+
217+
192218
## monarch.debug()
193219
Enable verbose logging of the internals of Monarch.
194220

README_TRANSITIONS.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Transitions
2-
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:
2+
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.
3+
4+
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:
35

46
* `transition_show_in` (constant defined as `monarch.TRANSITION.SHOW_IN`)
57
* `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
810

911
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.
1012

13+
## Transition URL
14+
This property is deprecated and will be removed in a future version of Monarch.
15+
16+
~~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.~~
17+
1118

1219
## Predefined transitions
1320
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)
2027
-- create transitions for the node 'root'
2128
-- the node will slide in/out from left and right with
2229
-- a specific easing, duration and delay
23-
self.transition = transitions.create(gui.get_node("root"))
30+
local transition = transitions.create(gui.get_node("root"))
2431
.show_in(transitions.slide_in_right, gui.EASING_OUTQUAD, 0.6, 0)
2532
.show_out(transitions.slide_out_left, gui.EASING_INQUAD, 0.6, 0)
2633
.back_in(transitions.slide_in_left, gui.EASING_OUTQUAD, 0.6, 0)
2734
.back_out(transitions.slide_out_right, gui.EASING_INQUAD, 0.6, 0)
35+
36+
monarch.on_transition("foobar", transition)
2837
end
2938

3039
function on_message(self, message_id, message, sender)
31-
self.transition.handle(message_id, message, sender)
40+
monarch.on_message(message_id, message, sender)
3241
-- you can also check when a transition has completed:
3342
if message_id == monarch.TRANSITION.DONE and message.transition == monarch.TRANSITION.SHOW_IN then
3443
print("Show in done!")
@@ -40,7 +49,7 @@ It is also possible to assign transitions to multiple nodes:
4049

4150
```lua
4251
function init(self)
43-
self.transition = transitions.create() -- note that no node is passed to transition.create()!
52+
local transition = transitions.create() -- note that no node is passed to transition.create()!
4453
.show_in(gui.get_node("node1"), transitions.slide_in_right, gui.EASING_OUTQUAD, 0.6, 0)
4554
.show_in(gui.get_node("node2"), transitions.slide_in_right, gui.EASING_OUTQUAD, 0.6, 0)
4655
end
@@ -111,7 +120,7 @@ end
111120
```
112121

113122
## Screen stack info and transitions
114-
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:
123+
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:
115124

116125
```lua
117126
function on_message(self, message_id, message, sender)

example/slidingwindow/slidingwindow.collection

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ embedded_instances {
5858
" value: \"window1\"\n"
5959
" type: PROPERTY_TYPE_HASH\n"
6060
" }\n"
61-
" properties {\n"
62-
" id: \"transition_url\"\n"
63-
" value: \"window1:/go\"\n"
64-
" type: PROPERTY_TYPE_URL\n"
65-
" }\n"
6661
" property_decls {\n"
6762
" }\n"
6863
"}\n"
@@ -123,11 +118,6 @@ embedded_instances {
123118
" value: \"window2\"\n"
124119
" type: PROPERTY_TYPE_HASH\n"
125120
" }\n"
126-
" properties {\n"
127-
" id: \"transition_url\"\n"
128-
" value: \"window2:/go\"\n"
129-
" type: PROPERTY_TYPE_URL\n"
130-
" }\n"
131121
" property_decls {\n"
132122
" }\n"
133123
"}\n"

example/slidingwindow/window1.gui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ nodes {
4747
xanchor: XANCHOR_NONE
4848
yanchor: YANCHOR_NONE
4949
pivot: PIVOT_CENTER
50-
adjust_mode: ADJUST_MODE_FIT
50+
adjust_mode: ADJUST_MODE_STRETCH
5151
layer: ""
5252
inherit_alpha: true
5353
slice9 {

example/slidingwindow/window1.gui_script

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ local monarch = require "monarch.monarch"
22
local transitions = require "monarch.transitions.gui"
33

44
function init(self)
5-
print("window2", msg.url())
6-
self.transition = transitions.create(gui.get_node("bg"))
7-
.show_in(transitions.slide_in_right, gui.EASING_LINEAR, 0.3, 0)
8-
.show_out(transitions.slide_out_left, gui.EASING_LINEAR, 0.3, 0)
9-
.back_in(transitions.slide_in_left, gui.EASING_LINEAR, 0.3, 0)
10-
.back_out(transitions.slide_out_right, gui.EASING_LINEAR, 0.3, 0)
11-
125
msg.post(".", "acquire_input_focus")
6+
7+
local DURATION = 0.3
8+
local transition = transitions.create(gui.get_node("bg"))
9+
.show_in(transitions.slide_in_right, gui.EASING_LINEAR, DURATION, 0)
10+
.show_out(transitions.slide_out_left, gui.EASING_LINEAR, DURATION, 0)
11+
.back_in(transitions.slide_in_left, gui.EASING_LINEAR, DURATION, 0)
12+
.back_out(transitions.slide_out_right, gui.EASING_LINEAR, DURATION, 0)
13+
14+
monarch.on_transition("window1", transition)
15+
16+
monarch.on_focus_changed("window1", function(message_id, message)
17+
if message_id == monarch.FOCUS.GAINED then
18+
print("window1 gained focus")
19+
elseif message_id == monarch.FOCUS.LOST then
20+
print("window1 lost focus")
21+
end
22+
end)
1323
end
1424

1525
function on_input(self, action_id, action)
@@ -21,5 +31,5 @@ function on_input(self, action_id, action)
2131
end
2232

2333
function on_message(self, message_id, message, sender)
24-
self.transition.handle(message_id, message, sender)
34+
monarch.on_message(message_id, message, sender)
2535
end

example/slidingwindow/window2.gui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ nodes {
4747
xanchor: XANCHOR_NONE
4848
yanchor: YANCHOR_NONE
4949
pivot: PIVOT_CENTER
50-
adjust_mode: ADJUST_MODE_FIT
50+
adjust_mode: ADJUST_MODE_STRETCH
5151
layer: ""
5252
inherit_alpha: true
5353
slice9 {

example/slidingwindow/window2.gui_script

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ local monarch = require "monarch.monarch"
22
local transitions = require "monarch.transitions.gui"
33

44
function init(self)
5-
print("window2", msg.url())
6-
self.transition = transitions.create(gui.get_node("bg"))
7-
.show_in(transitions.slide_in_right, gui.EASING_LINEAR, 0.3, 0)
8-
.show_out(transitions.slide_out_left, gui.EASING_LINEAR, 0.3, 0)
9-
.back_in(transitions.slide_in_left, gui.EASING_LINEAR, 0.3, 0)
10-
.back_out(transitions.slide_out_right, gui.EASING_LINEAR, 0.3, 0)
11-
125
msg.post(".", "acquire_input_focus")
6+
7+
local DURATION = 0.3
8+
local transition = transitions.create(gui.get_node("bg"))
9+
.show_in(transitions.slide_in_right, gui.EASING_LINEAR, DURATION, 0)
10+
.show_out(transitions.slide_out_left, gui.EASING_LINEAR, DURATION, 0)
11+
.back_in(transitions.slide_in_left, gui.EASING_LINEAR, DURATION, 0)
12+
.back_out(transitions.slide_out_right, gui.EASING_LINEAR, DURATION, 0)
13+
14+
monarch.on_transition("window2", transition)
1315
end
1416

1517
function on_input(self, action_id, action)
@@ -21,5 +23,6 @@ function on_input(self, action_id, action)
2123
end
2224

2325
function on_message(self, message_id, message, sender)
24-
self.transition.handle(message_id, message, sender)
26+
print("window2", message_id, message, sender)
27+
monarch.on_message(message_id, message, sender)
2528
end

0 commit comments

Comments
 (0)