Translate Tutorials/Inputs to C#

This commit is contained in:
Paul Joannon
2018-04-04 11:33:46 +02:00
parent fdd32778cc
commit 111161a153
3 changed files with 79 additions and 13 deletions

View File

@@ -6,7 +6,7 @@ Customizing mouse cursor
You might want to change the appearance of the mouse cursor in your game in order to suit the overall design. There are two ways to customize the mouse cursor:
1. Using project settings
2. Using GDScript
2. Using a script
Using project settings is a simpler but more limited way to customize the mouse cursor. The second way is more customizable but involves scripting.
@@ -22,7 +22,7 @@ Custom Hotspot is the point in the image that you would like to use as the curso
.. note:: The custom image should be a PNG file and the size **must** be 32x32.
Using GDScript
Using a script
--------------
Create a Node and attach the following script.
@@ -37,13 +37,28 @@ Create a Node and attach the following script.
var beam = load("res://beam.png")
func _ready():
# This changes only the arrow case of the cursor
# This is same as setting it in the project settings
Input.set_custom_mouse_cursor(arrow)
# Changes only the arrow shape of the cursor
# This is similar to changing it in the project settings
Input.set_custom_mouse_cursor(arrow)
# Change the appearance of the cursor in different cases
# This changes the Ibeam case
Input.set_custom_mouse_cursor(beam, Input.CURSOR_IBEAM)
# Changes a specific shape of the cursor (here the IBeam shape)
Input.set_custom_mouse_cursor(beam, Input.CURSOR_IBEAM)
.. code-tab:: csharp
public override void _Ready()
{
// Load the custom images for the mouse cursor
var arrow = ResourceLoader.Load("res://arrow.png");
var beam = ResourceLoader.Load("res://beam.png");
// Changes only the arrow shape of the cursor
// This is similar to changing it in the project settings
Input.SetCustomMouseCursor(arrow);
// Changes a specific shape of the cursor (here the IBeam shape)
Input.SetCustomMouseCursor(beam, Input.CursorShape.Ibeam);
}
.. note::
Check :ref:`Input.set_custom_mouse_cursor() <class_Input_set_custom_mouse_cursor>`.

View File

@@ -14,13 +14,23 @@ multiple locations, depending on the purpose.
Here is a quick example, closing your game if the escape key is hit:
::
.. tabs::
.. code-tab:: gdscript GDScript
func _unhandled_input(event):
if event is InputEventKey:
if event.pressed and event.scancode == KEY_ESCAPE:
get_tree().quit()
.. code-tab:: csharp
public override void _UnhandledInput(InputEvent @event)
{
if (@event is InputEventKey eventKey)
if (eventKey.Pressed && eventKey.Scancode == (int)KeyList.Escape)
GetTree().Quit();
}
However, it is cleaner and more flexible to use the provided :ref:`InputMap <class_InputMap>` feature,
which allows you to define input actions and assign them different keys. This way,
you can define multiple keys for the same action (e.g. they keyboard escape key and the start button on a gamepad).
@@ -29,12 +39,23 @@ and even build a key mapping feature on top of it to allow your game to change t
You can setup your InputMap under **Project > Project Settings > Input Map** and then use those actions like this:
::
.. tabs::
.. code-tab:: gdscript GDScript
func _process(delta):
if Input.is_action_pressed("ui_right"):
# Move right
.. code-tab:: csharp
public override void _Process(float delta)
{
if (Input.IsActionPressed("ui_right"))
{
// Move right
}
}
How does it work?
-----------------
@@ -146,7 +167,8 @@ from the game code (a good example of this is detecting gestures).
The Input singleton has a method for this:
:ref:`Input.parse_input_event() <class_input_parse_input_event>`. You would normally use it like this:
::
.. tabs::
.. code-tab:: gdscript GDScript
var ev = InputEventAction.new()
# set as move_left, pressed
@@ -154,6 +176,15 @@ The Input singleton has a method for this:
# feedback
Input.parse_input_event(ev)
.. code-tab:: csharp
var ev = new InputEventAction();
// set as move_left, pressed
ev.SetAction("move_left");
ev.SetPressed(true);
// feedback
Input.ParseInputEvent(ev);
InputMap
--------

View File

@@ -25,7 +25,8 @@ several options (see :ref:`doc_multiple_resolutions` tutorial). Use, then, the
functions in nodes to obtain the mouse coordinates and viewport size,
for example:
::
.. tabs::
.. code-tab:: gdscript GDScript
func _input(event):
# Mouse in viewport coordinates
@@ -37,8 +38,27 @@ for example:
# Print the size of the viewport
print("Viewport Resolution is: ", get_viewport_rect().size)
.. code-tab:: csharp
public override void _Input(InputEvent @event)
{
// Mouse in viewport coordinates
if (@event is InputEventMouseButton eventMouseButton)
GD.Print("Mouse Click/Unclick at: ", eventMouseButton.Position);
else if (@event is InputEventMouseMotion eventMouseMotion)
GD.Print("Mouse Motion at: ", eventMouseMotion.Position);
// Print the size of the viewport
GD.Print("Viewport Resolution is: ", GetViewPortRect().Size);
}
Alternatively it's possible to ask the viewport for the mouse position:
::
.. tabs::
.. code-tab:: gdscript GDScript
get_viewport().get_mouse_position()
.. code-tab:: csharp
GetViewport().GetMousePosition();