3
0
mirror of https://github.com/britzl/monarch.git synced 2025-06-27 10:27:49 +02:00

Allow data to be passed when showing or going back

This commit is contained in:
Björn Ritzl 2017-09-25 18:15:55 +02:00
parent 149c50221c
commit 09e2478378
6 changed files with 87 additions and 6 deletions

View File

@ -181,6 +181,69 @@ nodes {
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: 320.0
y: 1050.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: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "<text>"
font: "example"
id: "level"
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: "root"
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

@ -4,6 +4,9 @@ local transitions = require "monarch.transitions.gui"
function init(self)
msg.post(".", "acquire_input_focus")
local data = monarch.data(hash("game"))
gui.set_text(gui.get_node("level"), tostring(data.level))
self.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)
@ -14,7 +17,7 @@ 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"), { clear = true }, function()
monarch.show(hash("menu"), { clear = true }, nil, function()
print("showing menu done")
end)
end

View File

@ -14,7 +14,7 @@ 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
monarch.show(hash("popup"), nil, function()
monarch.show(hash("popup"), nil, nil, function()
print("showing popup done")
end)
end

View File

@ -18,7 +18,7 @@ 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"), nil, function()
monarch.show(hash("pregame"), nil, nil, function()
print("pregame show done")
end)
elseif gui.pick_node(self.cancel, action.x, action.y) then

View File

@ -17,7 +17,7 @@ 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"), nil, function()
monarch.show(hash("game"), nil, { level = 1 }, function()
print("showing game done")
end)
elseif gui.pick_node(self.back, action.x, action.y) then

View File

@ -116,16 +116,27 @@ local function back_out(screen, cb)
end
--- Get data associated with a screen
-- @param id Id of the screen to get data for
-- @return Data associated with the screen
function M.data(id)
assert(id, "You must provide a screen id")
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
return screens[id].data
end
--- Show a new screen
-- @param id Id of the screen to show
-- @param options Table with options when showing the screen (can be nil). Valid values:
-- * clear - Set to true if the stack should be cleared down to an existing instance of the screen
-- @param data Optional data to set on the screen. Can be retrieved by the data() function
-- @ param cb Optional callback to invoke when screen is shown
function M.show(id, options, cb)
function M.show(id, options, data, cb)
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]
screen.data = data
-- manipulate the current top
-- close popup if needed
@ -163,13 +174,17 @@ end
-- Go back to the previous screen in the stack
-- @param data Optional data to set for the previous screen
-- @param cb Optional callback to invoke when the previous screen is visible again
function M.back(cb)
function M.back(data, cb)
local screen = table.remove(stack)
if screen then
back_out(screen, cb)
local top = stack[#stack]
if top then
if data then
screen.data = data
end
back_in(top, screen)
end
elseif cb then