Gamepad support
This commit is contained in:
parent
87f841e8e1
commit
ed369cf718
24 changed files with 240 additions and 78 deletions
10
scripts/gamepad/ExplosionVibration.cs
Normal file
10
scripts/gamepad/ExplosionVibration.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class ExplosionVibration : Node
|
||||
{
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Input.StartJoyVibration(GamepadHandler.Instance.CurrentDevice, 0, 1,0.25f);
|
||||
}
|
||||
}
|
||||
1
scripts/gamepad/ExplosionVibration.cs.uid
Normal file
1
scripts/gamepad/ExplosionVibration.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b8v8xrsyswmg4
|
||||
52
scripts/gamepad/GamepadHandler.cs
Normal file
52
scripts/gamepad/GamepadHandler.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using Godot;
|
||||
using Newlon;
|
||||
using System;
|
||||
|
||||
public partial class GamepadHandler : Node
|
||||
{
|
||||
public static GamepadHandler Instance { get; private set; }
|
||||
public int CurrentDevice { get; private set; } = 0;
|
||||
public bool IsGamepadControlled => focused && controlled;
|
||||
private bool focused;
|
||||
private bool controlled;
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public override void _Notification(int what)
|
||||
{
|
||||
if (what == NotificationApplicationFocusIn)
|
||||
{
|
||||
focused = true;
|
||||
}
|
||||
if (what == NotificationApplicationFocusOut)
|
||||
{
|
||||
focused = false;
|
||||
}
|
||||
}
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event is InputEventJoypadButton || @event is InputEventJoypadMotion)
|
||||
{
|
||||
SetControlled(true);
|
||||
}
|
||||
if ((@event is InputEventMouse && Input.GetVector("cursor_left", "cursor_right", "cursor_up", "cursor_down") == Vector2.Zero) || @event is InputEventKey)
|
||||
{
|
||||
SetControlled(false);
|
||||
}
|
||||
}
|
||||
private void SetControlled(bool to)
|
||||
{
|
||||
controlled = to;
|
||||
if (controlled)
|
||||
{
|
||||
Cursor.Mode = Cursor.CursorMode.Gamepad;
|
||||
}
|
||||
else
|
||||
{
|
||||
Cursor.Mode = Cursor.CursorMode.Mouse;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
1
scripts/gamepad/GamepadHandler.cs.uid
Normal file
1
scripts/gamepad/GamepadHandler.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cyw4fh4x0fdjg
|
||||
Loading…
Add table
Add a link
Reference in a new issue