using Cthangover.Core.UI.Lights;
using Godot;
namespace Cthangover.Core.UI.Tool.LightEditor
{
///
/// Draggable 24×24 coloured handle representing a light source on the preview.
/// Overrides _GuiInput (Godot virtual) for mouse drag support; the colour
/// is taken from . Raises
/// with the handle's centre position in global coordinates for the window to
/// convert back to normalised coordinates.
///
public partial class LightEditorHandle : ColorRect
{
/// Index of the light in .
public int LightIndex;
/// Invoked during drag with the centre position in global coordinates.
public System.Action DragUpdate;
/// Invoked when the handle is clicked (mouse down).
public System.Action Clicked;
private bool dragging;
private Vector2 dragOffset;
/// Creates a 24×24 handle coloured to match the light's value.
public LightEditorHandle(LightDef light, int index)
{
LightIndex = index;
Size = new Vector2(24, 24);
Color = light.ToColor();
MouseFilter = MouseFilterEnum.Stop;
}
/// Handles mouse drag and click via Godot's virtual input system (_GuiInput).
public override void _GuiInput(InputEvent @event)
{
if (@event is InputEventMouseButton mb && mb.ButtonIndex == MouseButton.Left)
{
if (mb.Pressed)
{
dragging = true;
dragOffset = GetGlobalMousePosition() - GlobalPosition;
Clicked?.Invoke();
}
else
{
dragging = false;
}
AcceptEvent();
}
else if (@event is InputEventMouseMotion && dragging)
{
GlobalPosition = GetGlobalMousePosition() - dragOffset;
DragUpdate?.Invoke(GlobalPosition + Size / 2);
}
}
}
}