mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-05 22:09:56 +03:00
Fix C# code in Input examples
And work around two other warnings, making the build warning free.
This commit is contained in:
@@ -23,7 +23,7 @@ This provides a default material with a wide variety of rendering features and p
|
|||||||
Tutorials
|
Tutorials
|
||||||
---------
|
---------
|
||||||
|
|
||||||
- :doc:`../tutorials/3d/spatial_material`
|
- :doc:`../tutorials/3d/standard_material_3d`
|
||||||
|
|
||||||
Properties
|
Properties
|
||||||
----------
|
----------
|
||||||
|
|||||||
@@ -85,9 +85,15 @@ attach the following script:
|
|||||||
|
|
||||||
.. code-tab:: csharp
|
.. code-tab:: csharp
|
||||||
|
|
||||||
public override void _Input(InputEvent inputEvent)
|
using Godot;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
public class Node : Godot.Node
|
||||||
{
|
{
|
||||||
GD.Print(inputEvent.AsText());
|
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
|
||||||
@@ -96,16 +102,17 @@ event scroll by in the output window. Here's an example of the output:
|
|||||||
::
|
::
|
||||||
|
|
||||||
A
|
A
|
||||||
InputEventMouseMotion : button_mask=0, position=(551, 338), relative=(-85, 47), speed=(0, 0)
|
InputEventMouseMotion : button_mask=0, position=(108, 108), relative=(26, 1), speed=(164.152496, 159.119843), pressure=(0), tilt=(0, 0)
|
||||||
InputEventMouseButton : button_index=BUTTON_LEFT, pressed=true, position=(551, 338), button_mask=1, doubleclick=false
|
InputEventMouseButton : button_index=BUTTON_LEFT, pressed=true, position=(108, 107), button_mask=1, doubleclick=false
|
||||||
InputEventMouseButton : button_index=BUTTON_LEFT, pressed=false, position=(551, 338), button_mask=0, doubleclick=false
|
InputEventMouseButton : button_index=BUTTON_LEFT, pressed=false, position=(108, 107), button_mask=0, doubleclick=false
|
||||||
S
|
S
|
||||||
F
|
F
|
||||||
InputEventMouseMotion : button_mask=0, position=(547, 338), relative=(-1, 0), speed=(0, 0)
|
Alt
|
||||||
InputEventMouseMotion : button_mask=0, position=(542, 338), relative=(-4, 0), speed=(0, 0)
|
InputEventMouseMotion : button_mask=0, position=(108, 107), relative=(0, -1), speed=(164.152496, 159.119843), pressure=(0), tilt=(0, 0)
|
||||||
|
|
||||||
As you can see, the results are very different for the different types of
|
As you can see, the results are very different for the different types of
|
||||||
input. Key events are even printed as their key symbols. For example, let's consider :ref:`InputEventMouseButton <class_InputEventMouseButton>`.
|
input. Key events are even printed as their key symbols. For example, let's
|
||||||
|
consider :ref:`InputEventMouseButton <class_InputEventMouseButton>`.
|
||||||
It inherits from the following classes:
|
It inherits from the following classes:
|
||||||
|
|
||||||
- :ref:`InputEvent <class_InputEvent>` - the base class for all input events
|
- :ref:`InputEvent <class_InputEvent>` - the base class for all input events
|
||||||
@@ -325,32 +332,45 @@ node:
|
|||||||
|
|
||||||
.. code-tab:: csharp
|
.. code-tab:: csharp
|
||||||
|
|
||||||
public override void _Input(InputEvent inputEvent)
|
using Godot;
|
||||||
{
|
using System;
|
||||||
var sprite = GetNodeOrNull<Sprite>("Sprite");
|
|
||||||
if (sprite == null)
|
|
||||||
return;// No suitable node was found.
|
|
||||||
|
|
||||||
if (inputEvent is InputEventMouseButton mouseEvent && (ButtonList)mouseEvent.ButtonIndex == ButtonList.Left)
|
public class Node2D : Godot.Node2D
|
||||||
|
{
|
||||||
|
private bool dragging = false;
|
||||||
|
private int clickRadius = 32; // Size of the sprite.
|
||||||
|
|
||||||
|
public override void _Input(InputEvent inputEvent)
|
||||||
{
|
{
|
||||||
if ((mouseEvent.Position - sprite.Position).Length() < clickRadius)
|
Sprite sprite = GetNodeOrNull<Sprite>("Sprite");
|
||||||
|
if (sprite == null)
|
||||||
{
|
{
|
||||||
// Start dragging if the click is on the sprite.
|
return; // No suitable node was found.
|
||||||
if (!dragging && mouseEvent.Pressed)
|
|
||||||
dragging = !dragging;
|
|
||||||
}
|
}
|
||||||
// Stop dragging if the button is released.
|
|
||||||
if (dragging && !mouseEvent.Pressed)
|
if (inputEvent is InputEventMouseButton mouseEvent && (ButtonList)mouseEvent.ButtonIndex == ButtonList.Left)
|
||||||
{
|
{
|
||||||
dragging = false;
|
if ((mouseEvent.Position - sprite.Position).Length() < clickRadius)
|
||||||
|
{
|
||||||
|
// Start dragging if the click is on the sprite.
|
||||||
|
if (!dragging && mouseEvent.Pressed)
|
||||||
|
{
|
||||||
|
dragging = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Stop dragging if the button is released.
|
||||||
|
if (dragging && !mouseEvent.Pressed)
|
||||||
|
{
|
||||||
|
dragging = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
else
|
|
||||||
{
|
|
||||||
if (inputEvent is InputEventMouseMotion motionEvent)
|
|
||||||
{
|
{
|
||||||
// While dragging, move the sprite with the mouse.
|
if (inputEvent is InputEventMouseMotion motionEvent && dragging)
|
||||||
sprite.Position = motionEvent.Position;
|
{
|
||||||
|
// While dragging, move the sprite with the mouse.
|
||||||
|
sprite.Position = motionEvent.Position;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,7 +89,11 @@ That means that in GLES2 environments you can only set:
|
|||||||
GIProbes
|
GIProbes
|
||||||
--------
|
--------
|
||||||
|
|
||||||
:ref:`GIProbes <class_GIProbe>` do not work in GLES2. Instead use :ref:`Baked Lightmaps <class_BakedLightmap>`.
|
.. FIXME: Removed reference to class_BakedLightmap in master/4.0 version to
|
||||||
|
silence warning, but the whole page will likely end up rewritten or removed
|
||||||
|
in 4.0.
|
||||||
|
|
||||||
|
:ref:`GIProbes <class_GIProbe>` do not work in GLES2. Instead use Baked Lightmaps.
|
||||||
For a description of how baked lightmaps work see the :ref:`Baked Lightmaps tutorial <doc_baked_lightmaps>`.
|
For a description of how baked lightmaps work see the :ref:`Baked Lightmaps tutorial <doc_baked_lightmaps>`.
|
||||||
|
|
||||||
Contact shadows
|
Contact shadows
|
||||||
|
|||||||
Reference in New Issue
Block a user