diff --git a/example/game.gui b/example/game.gui index 2b89908..46d07f4 100644 --- a/example/game.gui +++ b/example/game.gui @@ -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: "" + 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 diff --git a/example/game.gui_script b/example/game.gui_script index b63bd4f..e006d70 100644 --- a/example/game.gui_script +++ b/example/game.gui_script @@ -3,6 +3,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) @@ -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 diff --git a/example/menu.gui_script b/example/menu.gui_script index 91b89f5..ecff167 100644 --- a/example/menu.gui_script +++ b/example/menu.gui_script @@ -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 diff --git a/example/popup.gui_script b/example/popup.gui_script index bb5568a..7c9c23d 100644 --- a/example/popup.gui_script +++ b/example/popup.gui_script @@ -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 diff --git a/example/pregame.gui_script b/example/pregame.gui_script index b11c98a..18c5927 100644 --- a/example/pregame.gui_script +++ b/example/pregame.gui_script @@ -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 diff --git a/monarch/monarch.lua b/monarch/monarch.lua index b82c1cf..fd8e808 100644 --- a/monarch/monarch.lua +++ b/monarch/monarch.lua @@ -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