From 111161a15390e46068f8f8c62ac7a7c693609baa Mon Sep 17 00:00:00 2001 From: Paul Joannon Date: Wed, 4 Apr 2018 11:33:46 +0200 Subject: [PATCH] Translate Tutorials/Inputs to C# --- tutorials/inputs/custom_mouse_cursor.rst | 31 ++++++++++++---- tutorials/inputs/inputevent.rst | 37 +++++++++++++++++-- .../inputs/mouse_and_input_coordinates.rst | 24 +++++++++++- 3 files changed, 79 insertions(+), 13 deletions(-) diff --git a/tutorials/inputs/custom_mouse_cursor.rst b/tutorials/inputs/custom_mouse_cursor.rst index a77f5dc5a..8cec6c21a 100644 --- a/tutorials/inputs/custom_mouse_cursor.rst +++ b/tutorials/inputs/custom_mouse_cursor.rst @@ -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() `. diff --git a/tutorials/inputs/inputevent.rst b/tutorials/inputs/inputevent.rst index d61320e8e..c9106db82 100644 --- a/tutorials/inputs/inputevent.rst +++ b/tutorials/inputs/inputevent.rst @@ -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 ` 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() `. 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 -------- diff --git a/tutorials/inputs/mouse_and_input_coordinates.rst b/tutorials/inputs/mouse_and_input_coordinates.rst index b1698b80c..1a5acb690 100644 --- a/tutorials/inputs/mouse_and_input_coordinates.rst +++ b/tutorials/inputs/mouse_and_input_coordinates.rst @@ -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(); \ No newline at end of file