Toggle drag
This commit is contained in:
parent
ed369cf718
commit
bab42e969a
1 changed files with 30 additions and 9 deletions
|
|
@ -7,26 +7,47 @@ 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>().InputEvent += OnInputEvent;
|
||||
GetParent<Area2D>().MouseEntered += OnMouseEntered;
|
||||
GetParent<Area2D>().MouseExited += OnMouseExited;
|
||||
}
|
||||
public void OnInputEvent(Node viewport, InputEvent @event, long shape_index)
|
||||
public void OnMouseEntered()
|
||||
{
|
||||
if (@event.IsActionPressed("primary_action"))
|
||||
{
|
||||
dragging = true;
|
||||
EmitSignal(SignalName.DragBegin);
|
||||
}
|
||||
mouseIn = true;
|
||||
}
|
||||
public void OnMouseExited()
|
||||
{
|
||||
mouseIn = false;
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (dragging && @event.IsActionReleased("primary_action"))
|
||||
if (mouseIn && @event.IsActionPressed("primary_action"))
|
||||
{
|
||||
dragging = true;
|
||||
toggle = false;
|
||||
can_end = false;
|
||||
EmitSignal(SignalName.DragBegin);
|
||||
GetTree().CreateTimer(0.2).Timeout += OnToggleTimeout;
|
||||
}
|
||||
if (dragging && can_end && (toggle == false && @event.IsActionReleased("primary_action") || (toggle == true && @event.IsActionPressed("primary_action"))))
|
||||
{
|
||||
dragging = false;
|
||||
EmitSignal(SignalName.DragEnd);
|
||||
}
|
||||
}
|
||||
public void OnToggleTimeout()
|
||||
{
|
||||
can_end = true;
|
||||
if (Input.IsActionPressed("primary_action") == false)
|
||||
{
|
||||
toggle = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue