3
0
mirror of https://github.com/britzl/monarch.git synced 2025-06-26 18:07:46 +02:00

Initial commit

This commit is contained in:
Björn Ritzl 2017-09-04 12:01:57 +02:00
commit 0bb9b69a45
24 changed files with 1891 additions and 0 deletions

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
.externalToolBuilders
.DS_Store
.lock-wscript
build
*.pyc
.project
.cproject
builtins
.internal

98
README.md Normal file
View File

@ -0,0 +1,98 @@
# Monarch
Monarch is a screen manager for the [Defold](https://www.defold.com) game engine.
# Installation
You can use Monarch in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your game.project file and in the dependencies field under project add:
https://github.com/britzl/monarch/archive/master.zip
# Usage
Using Monarch requires that screens are created in a certain way. Ones you have one or more screens created you can start navigating between the screens.
## Creating screens
Monarch screens are created in individual collections and loaded through collection proxies. The recommended setup is to create one game objects per screen and per game object attach a collection proxy component and an instance of the ````screen.script```` provided by Monarch. The screen.script will take care of the setup of the screen. All you need to do is to make sure that the script properties on the screen.script are correct:
* **Screen Proxy (url)** - The URL to the collection proxy component containing the actual screen. Defaults to #collectionproxy
* **Screen Id (hash)** - A unique id that can be used to reference the screen when navigating your app
* **Popup (boolean)** - Check this if the screen should be treated as a popup (see the section on popups below)
* **Transition Show In (url)** - Optional URL to call when the screen is about to be shown. Use this to trigger a transition (see the section on transitions below)
* **Transition Show Out (url)** - Optional URL to call when the screen is about to be hidden. Use this to trigger a transition (see the section on transitions below)
* **Transition Back In (url)** - Optional URL to call when the screen is about to be shown when navigating back in the screen hierarchy. Use this to trigger a transition (see the section on transitions below)
* **Transition Back Out (url)** - Optional URL to call when the screen is about to be hidden when navigating back in the screen hierarchy. Use this to trigger a transition (see the section on transitions below)
## Navigating between screens
The navigation in Monarch is based around a stack of screens. When a screen is shown it is pushed to the top of the stack. When going back to a previous screen the topmost screen on the stack is removed. Example:
* Showing screen A
* Stack is A
* Showing screen B
* Stack is A, B - (B is on top)
* Going back
* Stack is A
### Showing a new screen
You show a screen in one of two ways:
1. Post a ````show```` message to the screen.script
2. Call ````monarch.show(screen_id, [clear])````
Showing a screen will push it to the top of the stack and trigger an optional transition. The previous screen will be hidden (with an optional transition) unless the screen to be shown is a popup (see below).
#### Preventing duplicates in the stack
You can pass an optional clear flag when showing a screen (either as a second argument to ````monarch.show()```` or in the message). If the clear flag is set Monarch will look search the stack for the screen in question. If the screen already exists in the stack and the clear flag is set Monarch will remove all screens between the current top and the screen in question. Example:
* Stack is A, B, C, D - (D is on top)
* A call to ````monarch.show(B, true)```` is made
* Stack is A, B
### Going back to a previous screen
You navigate back in the screen hierarchy in one of two ways:
1. Post a ````back```` message to the ````screen.script````
2. Call ````monarch.back()````
## Input focus
Monarch will acquire and release input focus on the screen and ensure that only the top-most screen will ever have input focus.
## Popups
A screen that is flagged as a popup (see list of screen properties above) will be treated slightly differently when it comes to navigation. If a popup is at the top of the stack (ie currently shown) and another screen or popup is shown then the current popup will be removed from the stack. This means that it is not possible to a popup anywhere in the stack but the top. This also means that you cannot navigate back to a popup since popups can only exist on the top of the stack. Another important difference between normal screens and popups is that when a popup is shown on top of a non-popup the current top screen will not be unloaded.
## 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 Show In/Out and Transition Back In/Out properties described above should be URLs to one or more scripts with on_message handlers for the following messages:
* transition_show_in
* transition_show_out
* transition_back_in
* transition_back_out
When a transition is completed it is up to the developer to send a ````transition_done```` message back to the sender to indicate that the transition is completed and that Monarch can continue the navigation sequence. Example:
function on_message(self, message_id, message, sender)
if message_id == hash("transition_show_in") then
-- slide in from the right
gui.set_position(self.root, self.initial_position + vmath.vector3(1000, 0, 0))
gui.animate(self.root, gui.PROP_POSITION, self.initial_position, go.EASING_INOUTQUAD, 0.6, 0, function()
msg.post(sender, "transition_done")
end)
elseif message_id == hash("transition_show_out") then
-- slide out to the left
gui.animate(self.root, gui.PROP_POSITION, self.initial_position - vmath.vector3(1000, 0, 0), go.EASING_INOUTQUAD, 0.6, 0, function()
msg.post(sender, "transition_done")
end)
end
elseif message_id == hash("transition_back_in") then
-- slide in from the left
gui.set_position(self.root, self.initial_position - vmath.vector3(1000, 0, 0))
gui.animate(self.root, gui.PROP_POSITION, self.initial_position, go.EASING_INOUTQUAD, 0.6, 0, function()
msg.post(sender, "transition_done")
end)
end
elseif message_id == hash("transition_back_out") then
-- slide out to the right
gui.animate(self.root, gui.PROP_POSITION, self.initial_position + vmath.vector3(1000, 0, 0), go.EASING_INOUTQUAD, 0.6, 0, function()
msg.post(sender, "transition_done")
end)
end
end

3
assets/example.font Normal file
View File

@ -0,0 +1,3 @@
font: "/builtins/fonts/vera_mo_bd.ttf"
material: "/builtins/fonts/font.material"
size: 15

76
example/debug.gui Normal file
View File

@ -0,0 +1,76 @@
script: "/example/debug.gui_script"
fonts {
name: "example"
font: "/assets/example.font"
}
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
nodes {
position {
x: 13.311
y: 11.647
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 400.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "<text>"
font: "example"
id: "stack"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_SW
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: true
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

5
example/debug.gui_script Normal file
View File

@ -0,0 +1,5 @@
local monarch = require "monarch.monarch"
function update(self, dt)
gui.set_text(gui.get_node("stack"), monarch.dump_stack())
end

37
example/game.collection Normal file
View File

@ -0,0 +1,37 @@
name: "game"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"game\"\n"
" component: \"/example/game.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

131
example/game.gui Normal file
View File

@ -0,0 +1,131 @@
script: "/example/game.gui_script"
fonts {
name: "example"
font: "/assets/example.font"
}
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
nodes {
position {
x: 320.0
y: 568.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 50.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "win_button"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "WIN"
font: "example"
id: "win_text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "win_button"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

18
example/game.gui_script Normal file
View File

@ -0,0 +1,18 @@
local monarch = require "monarch.monarch"
function init(self)
msg.post(".", "acquire_input_focus")
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.released then
if gui.pick_node(gui.get_node("win_button"), action.x, action.y) then
monarch.show(hash("menu"), true)
end
end
end
function on_reload(self)
-- Add input-handling code here
-- Remove this function if not needed
end

309
example/main.collection Normal file
View File

@ -0,0 +1,309 @@
name: "main"
scale_along_z: 0
embedded_instances {
id: "menu"
data: "components {\n"
" id: \"screen\"\n"
" component: \"/monarch/screen.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" properties {\n"
" id: \"screen_id\"\n"
" value: \"menu\"\n"
" type: PROPERTY_TYPE_HASH\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collectionproxy\"\n"
" type: \"collectionproxy\"\n"
" data: \"collection: \\\"/example/menu.collection\\\"\\n"
"exclude: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "main"
data: "components {\n"
" id: \"main\"\n"
" component: \"/example/main.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
"components {\n"
" id: \"main1\"\n"
" component: \"/example/debug.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "pregame"
data: "components {\n"
" id: \"screen\"\n"
" component: \"/monarch/screen.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" properties {\n"
" id: \"screen_id\"\n"
" value: \"pregame\"\n"
" type: PROPERTY_TYPE_HASH\n"
" }\n"
" properties {\n"
" id: \"transition_show_in\"\n"
" value: \"pregame:/go#pregame\"\n"
" type: PROPERTY_TYPE_URL\n"
" }\n"
" properties {\n"
" id: \"transition_show_out\"\n"
" value: \"pregame:/go#pregame\"\n"
" type: PROPERTY_TYPE_URL\n"
" }\n"
" properties {\n"
" id: \"transition_back_in\"\n"
" value: \"pregame:/go#pregame\"\n"
" type: PROPERTY_TYPE_URL\n"
" }\n"
" properties {\n"
" id: \"transition_back_out\"\n"
" value: \"pregame:/go#pregame\"\n"
" type: PROPERTY_TYPE_URL\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collectionproxy\"\n"
" type: \"collectionproxy\"\n"
" data: \"collection: \\\"/example/pregame.collection\\\"\\n"
"exclude: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "game"
data: "components {\n"
" id: \"screen\"\n"
" component: \"/monarch/screen.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" properties {\n"
" id: \"screen_id\"\n"
" value: \"game\"\n"
" type: PROPERTY_TYPE_HASH\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collectionproxy\"\n"
" type: \"collectionproxy\"\n"
" data: \"collection: \\\"/example/game.collection\\\"\\n"
"exclude: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "popup"
data: "components {\n"
" id: \"screen\"\n"
" component: \"/monarch/screen.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" properties {\n"
" id: \"screen_id\"\n"
" value: \"popup\"\n"
" type: PROPERTY_TYPE_HASH\n"
" }\n"
" properties {\n"
" id: \"popup\"\n"
" value: \"true\"\n"
" type: PROPERTY_TYPE_BOOLEAN\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collectionproxy\"\n"
" type: \"collectionproxy\"\n"
" data: \"collection: \\\"/example/popup.collection\\\"\\n"
"exclude: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

5
example/main.script Normal file
View File

@ -0,0 +1,5 @@
local monarch = require "monarch.monarch"
function init(self)
monarch.show(hash("menu"))
end

37
example/menu.collection Normal file
View File

@ -0,0 +1,37 @@
name: "menu"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"menu\"\n"
" component: \"/example/menu.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

131
example/menu.gui Normal file
View File

@ -0,0 +1,131 @@
script: "/example/menu.gui_script"
fonts {
name: "example"
font: "/assets/example.font"
}
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
nodes {
position {
x: 320.0
y: 568.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 50.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "startgame_button"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "START GAME"
font: "example"
id: "startgame_text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "startgame_button"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

19
example/menu.gui_script Normal file
View File

@ -0,0 +1,19 @@
local monarch = require "monarch.monarch"
function init(self)
msg.post(".", "acquire_input_focus")
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.released then
if gui.pick_node(gui.get_node("startgame_button"), action.x, action.y) then
print("BUTTON***************************************")
monarch.show(hash("popup"))
end
end
end
function on_reload(self)
-- Add input-handling code here
-- Remove this function if not needed
end

37
example/popup.collection Normal file
View File

@ -0,0 +1,37 @@
name: "popup"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"popup\"\n"
" component: \"/example/popup.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

304
example/popup.gui Normal file
View File

@ -0,0 +1,304 @@
script: "/example/popup.gui_script"
fonts {
name: "example"
font: "/assets/example.font"
}
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
nodes {
position {
x: 320.0
y: 568.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 400.0
y: 300.0
z: 0.0
w: 1.0
}
color {
x: 0.3019608
y: 0.3019608
z: 0.3019608
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "root"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 117.419
y: -112.414
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 150.0
y: 50.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "ok_button"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
parent: "root"
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "OK"
font: "example"
id: "ok_text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "ok_button"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: -112.214
y: -111.501
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 150.0
y: 50.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "cancel_button"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
parent: "root"
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "CANCEL"
font: "example"
id: "back_text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "cancel_button"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

20
example/popup.gui_script Normal file
View File

@ -0,0 +1,20 @@
local monarch = require "monarch.monarch"
function init(self)
msg.post(".", "acquire_input_focus")
self.ok = gui.get_node("ok_button")
self.cancel = gui.get_node("cancel_button")
gui.set_render_order(16)
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.released then
if gui.pick_node(self.ok, action.x, action.y) then
print("ok")
monarch.show(hash("pregame"))
elseif gui.pick_node(self.cancel, action.x, action.y) then
print("cancel")
monarch.back()
end
end
end

View File

@ -0,0 +1,37 @@
name: "pregame"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"pregame\"\n"
" component: \"/example/pregame.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

248
example/pregame.gui Normal file
View File

@ -0,0 +1,248 @@
script: "/example/pregame.gui_script"
fonts {
name: "example"
font: "/assets/example.font"
}
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
nodes {
position {
x: 320.0
y: 568.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 50.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "play_button"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "PLAY"
font: "example"
id: "play_text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "play_button"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: 118.666
y: 1083.814
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 50.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "back_button"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "BACK"
font: "example"
id: "back_text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "back_button"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

View File

@ -0,0 +1,37 @@
local monarch = require "monarch.monarch"
function init(self)
msg.post(".", "acquire_input_focus")
self.play = gui.get_node("play_button")
self.back = gui.get_node("back_button")
self.play_position = gui.get_position(self.play)
self.back_position = gui.get_position(self.back)
gui.set_position(self.back, self.back_position + vmath.vector3(0, 1000, 0))
gui.set_position(self.play, self.play_position + vmath.vector3(0, 1000, 0))
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.released then
if gui.pick_node(self.play, action.x, action.y) then
print("play")
monarch.show(hash("game"))
elseif gui.pick_node(self.back, action.x, action.y) then
print("back")
monarch.back()
end
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("transition_show_in") or message_id == hash("transition_back_in") then
gui.animate(self.play, gui.PROP_POSITION, self.play_position, go.EASING_INQUAD, 0.6, 0, function()
msg.post(sender, "transition_done")
end)
gui.animate(self.back, gui.PROP_POSITION, self.back_position, go.EASING_INQUAD, 0.6)
elseif message_id == hash("transition_show_out") or message_id == hash("transition_back_out") then
gui.animate(self.play, gui.PROP_POSITION, self.play_position + vmath.vector3(0, 1000, 0), go.EASING_INQUAD, 0.6, 0, function()
msg.post(sender, "transition_done")
end)
gui.animate(self.back, gui.PROP_POSITION, self.back_position + vmath.vector3(0, 1000, 0), go.EASING_INQUAD, 0.6)
end
end

49
game.project Normal file
View File

@ -0,0 +1,49 @@
[project]
title = Monarch
[bootstrap]
main_collection = /example/main.collectionc
[input]
game_binding = /input/game.input_bindingc
[display]
width = 640
height = 1136
[script]
shared_state = 1
[ios]
app_icon_120x120 = /assets/appicons/icon_120.png
app_icon_180x180 = /assets/appicons/icon_180.png
app_icon_76x76 = /assets/appicons/icon_76.png
app_icon_152x152 = /assets/appicons/icon_152.png
app_icon_57x57 = /assets/appicons/icon_57.png
app_icon_114x114 = /assets/appicons/icon_114.png
app_icon_72x72 = /assets/appicons/icon_72.png
app_icon_144x144 = /assets/appicons/icon_144.png
launch_image_320x480 = /assets/loadingscreens/screen_320x480.png
launch_image_640x960 = /assets/loadingscreens/screen_640x960.png
launch_image_640x1136 = /assets/loadingscreens/screen_640x1136.png
launch_image_750x1334 = /assets/loadingscreens/screen_750x1334.png
launch_image_768x1024 = /assets/loadingscreens/screen_768x1024.png
launch_image_1536x2048 = /assets/loadingscreens/screen_1536x2048.png
launch_image_1024x768 = /assets/loadingscreens/screen_1024x768.png
launch_image_1242x2208 = /assets/loadingscreens/screen_1242x2208.png
launch_image_2208x1242 = /assets/loadingscreens/screen_2208x1242.png
launch_image_2048x1536 = /assets/loadingscreens/screen_2048x1536.png
bundle_identifier = com.example.todo
app_icon_167x167 = /assets/appicons/icon_167.png
[osx]
bundle_identifier = com.example.todo
[android]
app_icon_36x36 = /assets/appicons/icon_36.png
app_icon_48x48 = /assets/appicons/icon_48.png
app_icon_72x72 = /assets/appicons/icon_72.png
app_icon_96x96 = /assets/appicons/icon_96.png
app_icon_144x144 = /assets/appicons/icon_144.png
app_icon_192x192 = /assets/appicons/icon_192.png

4
input/game.input_binding Normal file
View File

@ -0,0 +1,4 @@
mouse_trigger {
input: MOUSE_BUTTON_1
action: "touch"
}

198
monarch/monarch.lua Normal file
View File

@ -0,0 +1,198 @@
local M = {}
local screens = {}
local stack = {}
local function screen_from_proxy(proxy)
for id,screen in pairs(screens) do
if screen.proxy == proxy then
return screen
end
end
end
local function screen_from_script()
local url = msg.url()
for id,screen in pairs(screens) do
if screen.script == url then
return screen
end
end
end
local function in_stack(id)
for i=1,#stack do
if stack[i].id == id then
return true
end
end
return false
end
function M.register(id, proxy, popup, transitions)
assert(not screens[id], ("There is already a screen registered with id %s"):format(tostring(id)))
screens[id] = { id = id, proxy = proxy, script = msg.url(), popup = popup, transitions = transitions }
end
function M.unregister(id)
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
screens[id] = nil
end
local function show_out(screen, next_screen)
local co
co = coroutine.create(function()
screen.co = co
msg.post(screen.script, "release_input_focus")
msg.post(screen.script, "monarch_context")
coroutine.yield()
if not next_screen.popup then
msg.post(screen.transitions.show_out, "transition_show_out")
coroutine.yield()
msg.post(screen.proxy, "unload")
coroutine.yield()
end
screen.co = nil
end)
coroutine.resume(co)
end
local function show_in(screen)
local co
co = coroutine.create(function()
screen.co = co
msg.post(screen.script, "monarch_context")
coroutine.yield()
msg.post(screen.proxy, "async_load")
coroutine.yield()
msg.post(screen.proxy, "enable")
stack[#stack + 1] = screen
msg.post(screen.transitions.show_in, "transition_show_in")
coroutine.yield()
msg.post(screen.script, "acquire_input_focus")
screen.co = nil
end)
coroutine.resume(co)
end
local function back_in(screen, previous_screen)
local co
co = coroutine.create(function()
screen.co = co
msg.post(screen.script, "monarch_context")
coroutine.yield()
if not previous_screen.popup then
msg.post(screen.proxy, "async_load")
coroutine.yield()
msg.post(screen.proxy, "enable")
msg.post(screen.transitions.back_in, "transition_back_in")
coroutine.yield()
end
msg.post(screen.script, "acquire_input_focus")
screen.co = nil
end)
coroutine.resume(co)
end
local function back_out(screen)
local co
co = coroutine.create(function()
screen.co = co
msg.post(screen.script, "monarch_context")
coroutine.yield()
msg.post(screen.transitions.back_out, "transition_back_out")
coroutine.yield()
msg.post(screen.proxy, "unload")
screen.co = nil
end)
coroutine.resume(co)
end
--- Show a new screen
-- @param id Id of the screen to show
-- @param clear Set to true if the stack should be cleared down to an existing instance of the screen. Optional
function M.show(id, clear)
assert(id, "You must provide a screen id")
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
local screen = screens[id]
-- manipulate the current top
-- close popup if needed
-- transition out
local top = stack[#stack]
if top then
-- if top is popup then close it
if top.popup then
stack[#stack] = nil
show_out(top, screen)
top = stack[#stack]
end
-- unload and transition out from top
if top then
show_out(top, screen)
end
end
-- if the screen we want to show is in the stack
-- already and the clear flag is set then we need
-- to remove every screen on the stack up until and
-- including the screen itself
if clear and in_stack(id) then
while true do
if table.remove(stack).id == id then
break
end
end
end
-- show screen
show_in(screen)
end
-- Go back to the previous screen in the stack
function M.back()
local screen = table.remove(stack)
if screen then
back_out(screen)
local top = stack[#stack]
if top then
back_in(top, screen)
end
end
end
function M.on_message(message_id, message, sender)
if message_id == hash("proxy_loaded") then
local screen = screen_from_proxy(sender)
assert(screen, "Unable to find screen for loaded proxy")
coroutine.resume(screen.co)
elseif message_id == hash("proxy_unloaded") then
local screen = screen_from_proxy(sender)
assert(screen, "Unable to find screen for unloaded proxy")
elseif message_id == hash("monarch_context") then
local screen = screen_from_script()
assert(screen, "Unable to find screen for current script url")
coroutine.resume(screen.co)
elseif message_id == hash("transition_done") then
local screen = screen_from_script()
assert(screen, "Unable to find screen for current script url")
coroutine.resume(screen.co)
end
end
function M.dump_stack()
local s = ""
for i,screen in ipairs(stack) do
s = s .. ("%d = %s\n"):format(i, tostring(screen.id))
end
return s
end
return M

32
monarch/monarch.script Normal file
View File

@ -0,0 +1,32 @@
local monarch = require "monarch.monarch"
function init(self)
-- Add initialization code here
-- Remove this function if not needed
end
function final(self)
-- Add finalization code here
-- Remove this function if not needed
end
function update(self, dt)
-- Add update code here
-- Remove this function if not needed
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end
function on_input(self, action_id, action)
-- Add input-handling code here
-- Remove this function if not needed
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end

46
monarch/screen.script Normal file
View File

@ -0,0 +1,46 @@
local monarch = require "monarch.monarch"
go.property("screen_proxy", msg.url("#collectionproxy"))
go.property("screen_id", hash(""))
go.property("popup", false)
go.property("transition_show_in", msg.url())
go.property("transition_show_out", msg.url())
go.property("transition_back_in", msg.url())
go.property("transition_back_out", msg.url())
function init(self)
print("screen init", self.proxy)
monarch.register(self.screen_id, self.screen_proxy, self.popup, {
show_in = self.transition_show_in,
show_out = self.transition_show_out,
back_in = self.transition_back_in,
back_out = self.transition_back_out,
})
end
function final(self)
print("screen final", self.proxy)
monarch.unregister(self.screen_id)
end
function on_message(self, message_id, message, sender)
print("screen on_message", message_id, sender)
if message_id == hash("show") then
monarch.show(self.screen_id, message.clear)
elseif message_id == hash("hide") then
monarch.hide(self.screen_id)
elseif message_id == hash("back") then
monarch.hide(self.screen_id)
elseif message_id == hash("transition_show_in") then
msg.post(sender, "transition_done")
elseif message_id == hash("transition_show_out") then
msg.post(sender, "transition_done")
elseif message_id == hash("transition_back_in") then
msg.post(sender, "transition_done")
elseif message_id == hash("transition_back_out") then
msg.post(sender, "transition_done")
else
monarch.on_message(message_id, message, sender)
end
end