Browse Source

added some c# input examples (#3158)

cbaal83 5 years ago
parent
commit
cbdb90f894
1 changed files with 124 additions and 3 deletions
  1. 124 3
      tutorials/inputs/input_examples.rst

+ 124 - 3
tutorials/inputs/input_examples.rst

@@ -31,17 +31,34 @@ Examples:
 .. tabs::
 .. tabs::
  .. code-tab:: gdscript GDScript
  .. code-tab:: gdscript GDScript
 
 
-    # input event - runs when the input happens
     func _input(event):
     func _input(event):
         if event.is_action_pressed("jump"):
         if event.is_action_pressed("jump"):
             jump()
             jump()
 
 
-    # polling - runs every frame
     func _physics_process(delta):
     func _physics_process(delta):
         if Input.is_action_pressed("move_right"):
         if Input.is_action_pressed("move_right"):
-            # move as long as the key/button is pressed
+            # Move as long as the key/button is pressed.
             position.x += speed * delta
             position.x += speed * delta
 
 
+ .. code-tab:: csharp
+
+    public override void _Input(InputEvent inputEvent)
+    {
+        if (inputEvent.IsActionPressed("jump"))
+        {
+            Jump();
+        }
+    }
+
+    public override void _PhysicsProcess(float delta)
+    {
+        if (Input.IsActionPressed("move_right"))
+        {
+            // Move as long as the key/button is pressed.
+            position.x += speed * delta;
+        }
+    }
+
 This gives you the flexibility to mix-and-match the type of input processing
 This gives you the flexibility to mix-and-match the type of input processing
 you do.
 you do.
 
 
@@ -64,6 +81,13 @@ attach the following script:
     func _input(event):
     func _input(event):
         print(event.as_text())
         print(event.as_text())
 
 
+ .. code-tab:: csharp
+
+    public override void _Input(InputEvent inputEvent)
+    {
+        GD.Print(inputEvent.AsText());
+    }
+
 As you press keys, move the mouse, and perform other inputs, you'll see each
 As you press keys, move the mouse, and perform other inputs, you'll see each
 event scroll by in the output window. Here's an example of the output:
 event scroll by in the output window. Here's an example of the output:
 
 
@@ -102,6 +126,15 @@ avoid this, make sure to test the event type first:
         if event is InputEventMouseButton:
         if event is InputEventMouseButton:
             print("mouse button event at ", event.position)
             print("mouse button event at ", event.position)
 
 
+ .. code-tab:: csharp
+
+    public override void _Input(InputEvent inputEvent)
+    {
+        if (inputEvent is InputEventMouseButton mouseEvent)
+        {
+            GD.Print($"mouse button event at {mouseEvent.Position}");
+        }
+    }
 
 
 InputMap
 InputMap
 --------
 --------
@@ -129,6 +162,16 @@ the action you're looking for:
         if event.is_action_pressed("my_action"):
         if event.is_action_pressed("my_action"):
             print("my_action occurred!")
             print("my_action occurred!")
 
 
+ .. code-tab:: csharp
+
+    public override void _Input(InputEvent inputEvent)
+    {
+        if (inputEvent.IsActionPressed("my_action"))
+        {
+            GD.Print("my_action occurred!");
+        }
+    }
+
 Keyboard events
 Keyboard events
 ---------------
 ---------------
 
 
@@ -145,6 +188,19 @@ the "T" key:
             if event.scancode == KEY_T:
             if event.scancode == KEY_T:
                 print("T was pressed")
                 print("T was pressed")
 
 
+ .. code-tab:: csharp
+
+    public override void _Input(InputEvent inputEvent)
+    {
+        if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
+        {
+            if ((Keylist)keyEvent.Scancode == KeyList.T)
+            {
+                GD.Print("T was pressed");
+            }
+        }
+    }
+
 .. tip:: See :ref:`@GlobalScope_KeyList <enum_@GlobalScope_KeyList>` for a list of scancode
 .. tip:: See :ref:`@GlobalScope_KeyList <enum_@GlobalScope_KeyList>` for a list of scancode
         constants.
         constants.
 
 
@@ -168,6 +224,21 @@ different when it's "Shift+T":
                 else:
                 else:
                     print("T was pressed")
                     print("T was pressed")
 
 
+ .. code-tab:: csharp
+
+    public override void _Input(InputEvent inputEvent)
+    {
+        if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
+        {
+            switch ((KeyList)keyEvent.Scancode)
+            {
+                case KeyList.T:
+                    GD.Print(keyEvent.Shift ? "Shift+T was pressed" : "T was pressed");
+                    break;
+            }
+        }
+    }
+
 .. tip:: See :ref:`@GlobalScope_KeyList <enum_@GlobalScope_KeyList>` for a list of scancode
 .. tip:: See :ref:`@GlobalScope_KeyList <enum_@GlobalScope_KeyList>` for a list of scancode
         constants.
         constants.
 
 
@@ -198,6 +269,24 @@ also counts as a button - two buttons, to be precise, with both
             if event.button_index == BUTTON_WHEEL_UP and event.pressed:
             if event.button_index == BUTTON_WHEEL_UP and event.pressed:
                 print("Wheel up")
                 print("Wheel up")
 
 
+ .. code-tab:: csharp
+
+    public override void _Input(InputEvent inputEvent)
+    {
+        if (inputEvent as InputEventMouseButton mouseEvent && mouseEvent.Pressed)
+        {
+            switch ((ButtonList)mouseEvent.ButtonIndex)
+            {
+                case ButtonList.Left:
+                    GD.Print($"Left button was clicked at {mouseEvent.Position}");
+                    break;
+                case ButtonList.WheelUp:
+                    GD.Print("Wheel up");
+                    break;
+            }
+        }
+    }
+
 Mouse motion
 Mouse motion
 ~~~~~~~~~~~~
 ~~~~~~~~~~~~
 
 
@@ -230,6 +319,38 @@ node:
             # While dragging, move the sprite with the mouse.
             # While dragging, move the sprite with the mouse.
             $Sprite.position = event.position
             $Sprite.position = event.position
 
 
+ .. code-tab:: csharp
+
+    public override void _Input(InputEvent inputEvent)
+    {
+        var sprite = GetNodeOrNull<Sprite>("Sprite");
+        if (sprite == null)
+            return;// No suitable node was found.
+
+        if (inputEvent is InputEventMouseButton mouseEvent && (ButtonList)mouseEvent.ButtonIndex == ButtonList.Left)
+        {
+            if ((mouseEvent.Position - sprite.Position).Length() < clickRadius)
+            {
+                // Start dragging if the click is on the sprite.
+                if (!dragging && mouseEvent.Pressed)
+                    dragging = !dragging;
+            }
+            // Stop dragging if the button is released.
+            if (dragging && !mouseEvent.Pressed)
+            {
+                dragging = false;
+            }
+        }
+        else
+        {
+            if (inputEvent is InputEventMouseMotion motionEvent)
+            {
+                // While dragging, move the sprite with the mouse.
+                sprite.Position = motionEvent.Position;
+            }
+        }
+    }
+
 Touch events
 Touch events
 ------------
 ------------