Drag and drop works
This commit is contained in:
154
DragAndDrop.cs
154
DragAndDrop.cs
@@ -1,95 +1,83 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public class DragAndDrop : KinematicBody2D
|
||||
namespace lettergodot
|
||||
{
|
||||
public class DragAndDrop : KinematicBody2D
|
||||
{
|
||||
private static ulong dragging = ulong.MaxValue;
|
||||
|
||||
/*
|
||||
Convert this code to C#
|
||||
|
||||
extends KinematicBody2D
|
||||
private bool Dragging
|
||||
{
|
||||
get => GetInstanceId() == dragging;
|
||||
set => dragging = value ? GetInstanceId() : ulong.MaxValue;
|
||||
}
|
||||
|
||||
|
||||
var dragging = false
|
||||
public override void _Ready()
|
||||
{
|
||||
Connect("dragsignal", this, "DragSignalImplemenmtation");
|
||||
}
|
||||
|
||||
signal dragsignal;
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
if (Dragging)
|
||||
{
|
||||
var mousepos = GetViewport().GetMousePosition();
|
||||
Position = new Vector2(mousepos.x, mousepos.y);
|
||||
}
|
||||
}
|
||||
|
||||
func _ready():
|
||||
connect("dragsignal",self,"_set_drag_pc")
|
||||
|
||||
|
||||
func _process(delta):
|
||||
if dragging:
|
||||
var mousepos = get_viewport().get_mouse_position()
|
||||
self.position = Vector2(mousepos.x, mousepos.y)
|
||||
[Signal]
|
||||
public delegate void dragsignal();
|
||||
|
||||
|
||||
|
||||
func _set_drag_pc():
|
||||
dragging=!dragging
|
||||
private void DragSignalImplemenmtation()
|
||||
{
|
||||
Dragging = !Dragging;
|
||||
}
|
||||
|
||||
|
||||
func _on_KinematicBody2D_input_event(viewport, event, shape_idx):
|
||||
if event is InputEventMouseButton:
|
||||
if event.button_index == BUTTON_LEFT and event.pressed:
|
||||
emit_signal("dragsignal")
|
||||
elif event.button_index == BUTTON_LEFT and !event.pressed:
|
||||
emit_signal("dragsignal")
|
||||
elif event is InputEventScreenTouch:
|
||||
if event.pressed and event.get_index() == 0:
|
||||
self.position = event.get_position()
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
|
||||
if (@event is InputEventMouseButton)
|
||||
{
|
||||
var eventMouseButton = (InputEventMouseButton)@event;
|
||||
|
||||
|
||||
if (eventMouseButton.ButtonIndex == (int)ButtonList.Left && eventMouseButton.Pressed)
|
||||
{
|
||||
|
||||
|
||||
// log Position and Position
|
||||
GD.Print("Position: " + Position);
|
||||
GD.Print("MousePosition:" + eventMouseButton.Position);
|
||||
GD.Print("Length:" + (eventMouseButton.Position - Position).Length());
|
||||
if ((eventMouseButton.Position - Position).Length() < (60) )
|
||||
{
|
||||
EmitSignal("dragsignal");
|
||||
}
|
||||
}
|
||||
else if (eventMouseButton.ButtonIndex == (int)ButtonList.Left && !eventMouseButton.Pressed)
|
||||
{
|
||||
if ((eventMouseButton.Position - Position).Length() < (60))
|
||||
{
|
||||
EmitSignal("dragsignal");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (@event is InputEventScreenTouch)
|
||||
{
|
||||
var eventScreenTouch = (InputEventScreenTouch)@event;
|
||||
|
||||
|
||||
*/
|
||||
|
||||
private bool dragging = false;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Connect("dragsignal", this, "DragSignalImplemenmtation");
|
||||
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
if (dragging)
|
||||
{
|
||||
var mousepos = GetViewport().GetMousePosition();
|
||||
Position = new Vector2(mousepos.x, mousepos.y);
|
||||
}
|
||||
}
|
||||
|
||||
[Signal]
|
||||
public delegate void dragsignal();
|
||||
|
||||
private void DragSignalImplemenmtation()
|
||||
{
|
||||
dragging = !dragging;
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event is InputEventMouseButton)
|
||||
{
|
||||
var eventMouseButton = (InputEventMouseButton)@event;
|
||||
if (eventMouseButton.ButtonIndex == (int)ButtonList.Left && eventMouseButton.Pressed)
|
||||
{
|
||||
EmitSignal("dragsignal");
|
||||
}
|
||||
else if (eventMouseButton.ButtonIndex == (int)ButtonList.Left && !eventMouseButton.Pressed)
|
||||
{
|
||||
EmitSignal("dragsignal");
|
||||
}
|
||||
}
|
||||
else if (@event is InputEventScreenTouch)
|
||||
{
|
||||
var eventScreenTouch = (InputEventScreenTouch)@event;
|
||||
if (eventScreenTouch.Pressed && eventScreenTouch.Index == 0)
|
||||
{
|
||||
Position = eventScreenTouch.Position;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
if ((eventScreenTouch.Position - Position).Length() < (60))
|
||||
{
|
||||
if (eventScreenTouch.Pressed && eventScreenTouch.Index == 0)
|
||||
{
|
||||
Position = eventScreenTouch.Position;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user