96 lines
1.9 KiB
C#
96 lines
1.9 KiB
C#
|
|
using Godot;
|
||
|
|
using System;
|
||
|
|
|
||
|
|
public class DragAndDrop : KinematicBody2D
|
||
|
|
{
|
||
|
|
|
||
|
|
/*
|
||
|
|
Convert this code to C#
|
||
|
|
|
||
|
|
extends KinematicBody2D
|
||
|
|
|
||
|
|
var dragging = false
|
||
|
|
|
||
|
|
signal dragsignal;
|
||
|
|
|
||
|
|
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)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
func _set_drag_pc():
|
||
|
|
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()
|
||
|
|
|
||
|
|
|
||
|
|
*/
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|