Переглянути джерело

Added mouse/touch example

Björn Ritzl 5 роки тому
батько
коміт
fcba7a0cee

+ 17 - 0
assets/text24.font

@@ -0,0 +1,17 @@
+font: "/assets/SourceSansPro-Semibold.ttf"
+material: "/builtins/fonts/font.material"
+size: 24
+antialias: 1
+alpha: 1.0
+outline_alpha: 0.0
+outline_width: 0.0
+shadow_alpha: 0.0
+shadow_blur: 0
+shadow_x: 0.0
+shadow_y: 0.0
+extra_characters: ""
+output_format: TYPE_BITMAP
+all_chars: false
+cache_width: 0
+cache_height: 0
+render_mode: MODE_SINGLE_LAYER

+ 18 - 0
examples/_main/loader.go

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

@@ -105,7 +105,7 @@ function init(self)
 	self.index["physics"] = { "dynamic", "kinematic", "raycast", "trigger" }
 	self.index["animation"] = { "spinner", "flipbook", "tween", "spine" }
 	self.index["gui"] = { "button", "stencil", "load_texture", "pointer_over", "color"}
-	self.index["input"] = { "move", "text", "down duration" }
+	self.index["input"] = { "move", "text", "down duration", "mouse_and_touch" }
 	self.index["particles"] = { "particlefx", "modifiers" }
 	self.index["sound"] = { "music", "fade_in_out" }
 	self.index["render"] = { "camera" }

+ 91 - 0
examples/input/mouse_and_touch/mouse_and_touch.collection

@@ -0,0 +1,91 @@
+name: "mouse_and_touch"
+scale_along_z: 0
+embedded_instances {
+  id: "go"
+  data: "components {\n"
+  "  id: \"mouse_and_touch\"\n"
+  "  component: \"/examples/input/mouse_and_touch/mouse_and_touch.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: \"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: 1.0\\n"
+  "  y: 1.0\\n"
+  "  z: 1.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "color {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.0\\n"
+  "  z: 0.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "outline {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.0\\n"
+  "  z: 0.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "shadow {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.0\\n"
+  "  z: 0.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "leading: 1.0\\n"
+  "tracking: 0.0\\n"
+  "pivot: PIVOT_SW\\n"
+  "blend_mode: BLEND_MODE_ALPHA\\n"
+  "line_break: false\\n"
+  "text: \\\"Label\\\"\\n"
+  "font: \\\"/assets/text24.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label.material\\\"\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 6.0\n"
+  "    y: 6.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"
+  ""
+  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
+  }
+}

+ 5 - 0
examples/input/mouse_and_touch/mouse_and_touch.md

@@ -0,0 +1,5 @@
+---
+title: Mouse and touch events
+brief: Shows how to read mouse/touch movement and mouse/touch button state.
+scripts: mouse_and_touch.script
+---

+ 25 - 0
examples/input/mouse_and_touch/mouse_and_touch.script

@@ -0,0 +1,25 @@
+function init(self)
+	msg.post(".", "acquire_input_focus") -- <1>
+	self.state = "-"
+end
+
+function on_input(self, action_id, action)
+	local pos = vmath.vector3(action.x, action.y, 0) -- <2>
+	if action_id == hash("touch") then  -- <3>
+		if action.pressed then -- <4>
+			self.state = "pressed"
+		elseif action.released then -- <5>
+			self.state = "-"
+		end
+	end
+	local text = ("x: %d y: %d state: %s"):format(pos.x, pos.y, self.state)
+	label.set_text("#label", text)
+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. Read the position of the mouse pointer or touch event
+3. The left mouse button in the input bindings will also be used for touch events on a phone/tablet
+4. The 'pressed' state will be true on the frame when the mouse button/finger is pressed
+5. The 'released' state will be true on the frame when the mouse button/finger is released
+--]]