55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
using Godot;
|
|
|
|
namespace Newlon.Components.Plants;
|
|
|
|
public partial class DragAction : Node
|
|
{
|
|
[Signal] public delegate void DragBeginEventHandler();
|
|
[Signal] public delegate void DragEndEventHandler();
|
|
private bool dragging = false;
|
|
private bool toggle = false;
|
|
private bool can_end = false;
|
|
private bool mouseIn = false;
|
|
public override void _Ready()
|
|
{
|
|
GetParent<Area2D>().MouseEntered += OnMouseEntered;
|
|
GetParent<Area2D>().MouseExited += OnMouseExited;
|
|
}
|
|
public void OnMouseEntered()
|
|
{
|
|
mouseIn = true;
|
|
}
|
|
public void OnMouseExited()
|
|
{
|
|
mouseIn = false;
|
|
}
|
|
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
if (mouseIn && @event.IsActionPressed("primary_action"))
|
|
{
|
|
dragging = true;
|
|
toggle = false;
|
|
can_end = false;
|
|
EmitSignal(SignalName.DragBegin);
|
|
GetTree().CreateTimer(0.2, ignoreTimeScale: true).Timeout += OnToggleTimeout;
|
|
GetViewport().SetInputAsHandled();
|
|
}
|
|
if (dragging && can_end && (toggle == false && @event.IsActionReleased("primary_action") || (toggle == true && @event.IsActionPressed("primary_action"))))
|
|
{
|
|
dragging = false;
|
|
EmitSignal(SignalName.DragEnd);
|
|
GetViewport().SetInputAsHandled();
|
|
}
|
|
}
|
|
public void OnToggleTimeout()
|
|
{
|
|
can_end = true;
|
|
if (Input.IsActionPressed("primary_action") == false)
|
|
{
|
|
toggle = true;
|
|
}
|
|
|
|
}
|
|
|
|
}
|