Parcourir la source

Added example to trigger timer's callback asynchronously using built-in API.

Pawel Jarosz il y a 2 ans
Parent
commit
c4f571e4e8

+ 39 - 0
examples/timer/trigger_timer/trigger_timer.collection

@@ -0,0 +1,39 @@
+name: "trigger_timer"
+scale_along_z: 0
+embedded_instances {
+  id: "go"
+  data: "components {\n"
+  "  id: \"trigger_timer\"\n"
+  "  component: \"/examples/timer/trigger_timer/trigger_timer.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"
+  "  property_decls {\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
+  }
+}

+ 145 - 0
examples/timer/trigger_timer/trigger_timer.gui

@@ -0,0 +1,145 @@
+script: "/examples/timer/trigger_timer/trigger_timer.gui_script"
+fonts {
+  name: "text24"
+  font: "/assets/text24.font"
+}
+background_color {
+  x: 0.0
+  y: 0.0
+  z: 0.0
+  w: 0.0
+}
+nodes {
+  position {
+    x: 360.0
+    y: 430.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: 100.0
+    y: 40.0
+    z: 0.0
+    w: 1.0
+  }
+  color {
+    x: 0.101960786
+    y: 0.101960786
+    z: 0.101960786
+    w: 1.0
+  }
+  type: TYPE_TEXT
+  blend_mode: BLEND_MODE_ALPHA
+  text: "0"
+  font: "text24"
+  id: "counter"
+  xanchor: XANCHOR_NONE
+  yanchor: YANCHOR_NONE
+  pivot: PIVOT_CENTER
+  outline {
+    x: 0.0
+    y: 0.0
+    z: 0.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
+  layer: ""
+  inherit_alpha: true
+  alpha: 1.0
+  outline_alpha: 0.0
+  shadow_alpha: 0.0
+  template_node_child: false
+  text_leading: 1.0
+  text_tracking: 0.0
+  custom_type: 0
+  enabled: true
+  visible: true
+}
+nodes {
+  position {
+    x: 360.0
+    y: 260.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: 600.0
+    y: 80.0
+    z: 0.0
+    w: 1.0
+  }
+  color {
+    x: 0.101960786
+    y: 0.101960786
+    z: 0.101960786
+    w: 1.0
+  }
+  type: TYPE_TEXT
+  blend_mode: BLEND_MODE_ALPHA
+  text: "Counter ticks every 1s.\n"
+  "Click to trigger timer callback asynchronously."
+  font: "text24"
+  id: "info"
+  xanchor: XANCHOR_NONE
+  yanchor: YANCHOR_NONE
+  pivot: PIVOT_CENTER
+  outline {
+    x: 0.0
+    y: 0.0
+    z: 0.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
+  layer: ""
+  inherit_alpha: true
+  alpha: 1.0
+  outline_alpha: 0.0
+  shadow_alpha: 0.0
+  template_node_child: false
+  text_leading: 1.0
+  text_tracking: 0.0
+  custom_type: 0
+  enabled: true
+  visible: true
+}
+material: "/builtins/materials/gui.material"
+adjust_reference: ADJUST_REFERENCE_PARENT
+max_nodes: 512

+ 33 - 0
examples/timer/trigger_timer/trigger_timer.gui_script

@@ -0,0 +1,33 @@
+function init(self)
+	self.count = 0			-- <1>
+	local interval = 1		-- <2>
+	local repeating = true	-- <3>
+
+	self.timer = timer.delay(interval, repeating, function()	-- <4>
+		self.count = self.count + 1								-- <5>
+		local node = gui.get_node("counter")					-- <6>
+		gui.set_text(node, self.count)							-- <7>
+	end)
+
+	msg.post(".", "acquire_input_focus")	-- <8>
+end
+
+function on_input(self, action_id, action)
+	if action_id == hash("touch") and action.pressed then	-- <9>
+		timer.trigger(self.timer)							-- <10>
+	end
+end
+
+--[[
+1. Start the count with value 0.
+2. We will use interval of 1 [s].
+3. We will be repeating the timer endlessly.
+4. Start the timer with interval (1s) and repeating (true) and pass a callback function.
+	Store the handle to the timer in self.timer.
+5. The function will be called every 1s, so increase the count by 1 each time.
+6. Get the counter text node.
+7. Update the counter text node with an increased count.
+8. Tell the engine that this game object wants to receive input.
+9. If the user clicks.
+10. Trigger the timer's callback function asynchronously using the saved self.timer handle.
+--]]

+ 15 - 0
examples/timer/trigger_timer/trigger_timer.md

@@ -0,0 +1,15 @@
+---
+title: Trigger timer example
+brief: This example shows how to create timer that triggers counting every 1s and can be triggered manually and asynchronously as a reaction to user input.
+scripts: trigger_timer.gui_script
+---
+
+The example shows how to use Defold built-in timer and trigger it asynchronously and uses two indicators:
+
+1. A counter text increased every 1s created using a text node.
+2. A text node with information displayed.
+
+The timer triggers update of the counter every 1s.
+Click anywhere to trigger the callback of the timer asynchronously.
+
+![trigger_timer](trigger_timer.png)

BIN
examples/timer/trigger_timer/trigger_timer.png