Browse Source

Merge branch 'master' into cancel_timer_example

Pawel 2 years ago
parent
commit
cd5c3cc18f

+ 18 - 0
examples/_main/loader.go

@@ -946,6 +946,24 @@ embedded_components {
     w: 1.0
   }
 }
+embedded_components {
+  id: "timer/repeating_timer"
+  type: "collectionproxy"
+  data: "collection: \"/examples/timer/repeating_timer/repeating_timer.collection\"\n"
+  "exclude: false\n"
+  ""
+  position {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+}
 embedded_components {
   id: "timer/cancel_timer"
   type: "collectionproxy"

+ 1 - 1
examples/_main/menu.gui_script

@@ -115,7 +115,7 @@ function init(self)
 	self.index["sprite"] = { "size", "tint", "flip" }
 	self.index["file"] = { "sys_save_load" }
 	self.index["tilemap"] = { "collisions", "get_set_tile" }
-	self.index["timer"] = { "cancel_timer" }
+	self.index["timer"] = { "repeating_timer", "cancel_timer" }
 	local categories = {}
 	for k,_ in pairs(self.index) do
 		categories[#categories + 1] = k

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

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

+ 134 - 0
examples/timer/repeating_timer/repeating_timer.gui

@@ -0,0 +1,134 @@
+script: "/examples/timer/repeating_timer/repeating_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: 363.0
+    y: 400.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: "0s"
+  font: "text24"
+  id: "numeric"
+  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: 400.0
+    z: 0.0
+    w: 1.0
+  }
+  rotation {
+    x: 0.0
+    y: 180.0
+    z: 90.0
+    w: 1.0
+  }
+  scale {
+    x: 1.0
+    y: 1.0
+    z: 1.0
+    w: 1.0
+  }
+  size {
+    x: 200.0
+    y: 200.0
+    z: 0.0
+    w: 1.0
+  }
+  color {
+    x: 0.0
+    y: 1.0
+    z: 0.0
+    w: 1.0
+  }
+  type: TYPE_PIE
+  blend_mode: BLEND_MODE_ALPHA
+  texture: ""
+  id: "radial"
+  xanchor: XANCHOR_NONE
+  yanchor: YANCHOR_NONE
+  pivot: PIVOT_CENTER
+  adjust_mode: ADJUST_MODE_FIT
+  layer: ""
+  inherit_alpha: true
+  outerBounds: PIEBOUNDS_ELLIPSE
+  innerRadius: 90.0
+  perimeterVertices: 120
+  pieFillAngle: 360.0
+  clipping_mode: CLIPPING_MODE_NONE
+  clipping_visible: true
+  clipping_inverted: false
+  alpha: 1.0
+  template_node_child: false
+  size_mode: SIZE_MODE_MANUAL
+  custom_type: 0
+  enabled: true
+  visible: true
+}
+material: "/builtins/materials/gui.material"
+adjust_reference: ADJUST_REFERENCE_PARENT
+max_nodes: 512

+ 36 - 0
examples/timer/repeating_timer/repeating_timer.gui_script

@@ -0,0 +1,36 @@
+-- set value of numeric time indicator (from 0 to 60s)
+local function update_numeric(p)
+	local node = gui.get_node("numeric")
+	gui.set_text(node, tostring(p) .. "s")
+end
+
+-- update radial/circle time indicator by changing the fill angle
+local function update_radial(p)
+	local node = gui.get_node("radial")
+	local angle = p * 6
+	gui.set_fill_angle(node, angle)
+end
+
+function init(self)
+	self.count = 0			-- <1>
+	local interval = 1		-- <2>
+	local repeating = true	-- <3>
+
+	timer.delay(interval, repeating, function()		-- <4>
+		self.count = self.count + 1					-- <5>
+		local p = self.count % 60					-- <6>
+		update_numeric(p)							-- <7>
+		update_radial(p)							-- <8>
+	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.
+5. The function will be called every 1s, so increase the count by 1 each time.
+6. Get the modulo of 60, because the timer will be reset every 60s.
+7. Update the numeric display of seconds passed.
+8. Update the radial indicator of seconds passed.
+--]]

+ 14 - 0
examples/timer/repeating_timer/repeating_timer.md

@@ -0,0 +1,14 @@
+---
+title: Repeating timer example
+brief: This example shows how to create timer that repeats endlessly every second
+scripts: repeating_timer.gui_script
+---
+
+The example shows how to use Defold built-in timer and uses two indicators:
+
+1. A numerical time text with seconds counter created using a text node
+2. A circular time indicator created using a pie node
+
+The timer trigers updates of those two indicators every 1s to form a counter.
+
+![repeating_timer](repeating_timer.png)

BIN
examples/timer/repeating_timer/repeating_timer.png


+ 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