80 lines
1.7 KiB
C#
80 lines
1.7 KiB
C#
using System;
|
|
using Godot;
|
|
|
|
public partial class Meeting : RigidBody2D
|
|
{
|
|
private bool _mouseInside;
|
|
private bool _isDragging;
|
|
private Vector2 _dragTarget;
|
|
|
|
public override void _Ready()
|
|
{
|
|
FreezeMode = FreezeModeEnum.Kinematic;
|
|
|
|
MouseEntered += () =>
|
|
{
|
|
_mouseInside = true;
|
|
CanSleep = false;
|
|
};
|
|
MouseExited += () =>
|
|
{
|
|
_mouseInside = false;
|
|
CanSleep = true;
|
|
};
|
|
}
|
|
|
|
public override void _UnhandledInput(InputEvent @event)
|
|
{
|
|
// TODO: probably memory problem
|
|
if (@event is InputEventMouse mouseEvent)
|
|
{
|
|
if (_mouseInside)
|
|
{
|
|
if (mouseEvent.IsPressed() && mouseEvent.ButtonMask == MouseButtonMask.Left)
|
|
{
|
|
GD.Print("Start Dragging");
|
|
StartDragging();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (_isDragging && @event is InputEventMouseButton { Pressed: false })
|
|
{
|
|
GD.Print("Let me free");
|
|
StopDragging();
|
|
}
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
_dragTarget = GetGlobalMousePosition();
|
|
}
|
|
|
|
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
|
{
|
|
if (!_isDragging)
|
|
return;
|
|
|
|
var t = state.Transform;
|
|
t.Origin = _dragTarget;
|
|
|
|
state.Transform = t;
|
|
state.LinearVelocity = Vector2.Zero;
|
|
state.AngularVelocity = 0;
|
|
}
|
|
|
|
private void StartDragging()
|
|
{
|
|
Freeze = true;
|
|
_isDragging = true;
|
|
CollisionLayer = 0;
|
|
}
|
|
|
|
private void StopDragging()
|
|
{
|
|
Freeze = false;
|
|
_isDragging = false;
|
|
CollisionLayer = 1;
|
|
}
|
|
}
|