Forráskód Böngészése

add down duration example

Alexey Gulev 8 éve
szülő
commit
4fb0c7900e

+ 18 - 0
examples/_main/loader.go

@@ -352,3 +352,21 @@ embedded_components {
     w: 1.0
   }
 }
+embedded_components {
+  id: "input/down duration"
+  type: "collectionproxy"
+  data: "collection: \"/examples/input/down_duration/down_duration.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
+  }
+}

+ 1 - 1
examples/_main/menu.gui_script

@@ -104,7 +104,7 @@ function init(self)
 	self.index["physics"] = { "dynamic", "kinematic", "trigger" }
 	self.index["animation"] = { "spinner", "flipbook", "tween", "spine" }
 	self.index["gui"] = { "button", "stencil" }
-	self.index["input"] = { "move", "text" }
+	self.index["input"] = { "move", "text", "down duration" }
 	self.index["audio"] = { "lorem ipsum" }
 	self.index["render"] = { "lorem ipsum" }
 	self.index["camera"] = { "lorem ipsum" }

BIN
examples/input/down_duration/down_click.png


+ 111 - 0
examples/input/down_duration/down_duration.collection

@@ -0,0 +1,111 @@
+name: "default"
+scale_along_z: 0
+embedded_instances {
+  id: "bunny"
+  data: "components {\n"
+  "  id: \"script\"\n"
+  "  component: \"/examples/input/down_duration/down_duration.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"
+  "embedded_components {\n"
+  "  id: \"sprite\"\n"
+  "  type: \"sprite\"\n"
+  "  data: \"tile_set: \\\"/assets/sprites.atlas\\\"\\n"
+  "default_animation: \\\"bunny2_ready\\\"\\n"
+  "material: \\\"/builtins/materials/sprite.material\\\"\\n"
+  "blend_mode: BLEND_MODE_ALPHA\\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"
+  "embedded_components {\n"
+  "  id: \"label\"\n"
+  "  type: \"label\"\n"
+  "  data: \"size {\\n"
+  "  x: 128.0\\n"
+  "  y: 32.0\\n"
+  "  z: 0.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "scale {\\n"
+  "  x: 0.5\\n"
+  "  y: 0.5\\n"
+  "  z: 1.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "color {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.5647059\\n"
+  "  z: 0.99215686\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "outline {\\n"
+  "  x: 1.0\\n"
+  "  y: 1.0\\n"
+  "  z: 1.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "shadow {\\n"
+  "  x: 1.0\\n"
+  "  y: 1.0\\n"
+  "  z: 1.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "leading: 1.0\\n"
+  "tracking: 0.0\\n"
+  "pivot: PIVOT_W\\n"
+  "blend_mode: BLEND_MODE_ALPHA\\n"
+  "line_break: false\\n"
+  "text: \\\"Click...\\\"\\n"
+  "font: \\\"/assets/text48.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label.material\\\"\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 20.653\n"
+  "    y: 118.002\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: 132.102
+    y: 360.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
+  }
+}

+ 9 - 0
examples/input/down_duration/down_duration.md

@@ -0,0 +1,9 @@
+---
+title: Down duration
+brief: Listens to text input trigger "click" and count mouse down duration in update method.
+scripts: down_duration.script
+---
+
+![input bindings](input_binding.png)
+
+![text](down_click.png)

+ 49 - 0
examples/input/down_duration/down_duration.script

@@ -0,0 +1,49 @@
+function init(self)
+	msg.post(".", "acquire_input_focus") -- <1>
+	self.message = "Duration: %f . Last duration: %f" -- <2>
+	self.duration = 0
+	self.last_duration = 0
+end
+
+local function update_text(self) -- <3>
+	local msg = string.format(self.message, self.duration, self.last_duration) -- <4>
+	label.set_text("#label", msg) -- <5>
+end
+
+function on_input(self, action_id, action)
+	if action_id == hash("click") then -- <6>
+		if action.pressed then -- <7>
+			self.is_start_timer = true -- <8>
+		elseif action.released then -- <9>
+			self.is_start_timer = false -- <10>
+			self.last_duration = self.duration -- <11>
+			self.duration = 0
+			update_text(self) -- <12>
+		end
+	end
+end
+
+function update(self, dt)
+	if self.is_start_timer then -- <13>
+		self.duration = self.duration + dt -- <14>
+		update_text(self) -- <15>
+	end
+end
+
+--[[
+1. Tell the engine that this object ("." is shorthand for the current game object) wants to receive input. The function `on_input()` will be called whenever input is received.
+2. Prepare format of output included two float
+3. Create method for updating the text label
+4. Create a formatted string from the format and duration and last_duration arguments
+5. Set the label component to the stored text.
+6. Check if we receive an input action named "click".
+7. Check if it is pressed then run the following.
+8. Change flag for starting a timer
+9. Check if it is released then run the following.
+10. Change flag for stopping a timer
+11. Save duration as last_duration
+12. Run method for updating text
+13. Check if tamer is started
+14. Add dt (delta time from the last frame) to the duration variable
+15. Run method for updating text
+--]]

BIN
examples/input/down_duration/input_binding.png