using Godot;
using ViaFerrea.Client.Net;
namespace ViaFerrea.Client.Map;
///
/// Vista della mappa: Node2D con Camera2D e i livelli (terreno, opere e binari, entità, treni,
/// strumenti). Rotella = zoom attorno al cursore, tasto destro o centrale = spostamento,
/// WASD/frecce = spostamento da tastiera. Coordinate del mondo in metri (1 unità = 1 m).
///
public partial class MapView : Node2D
{
public const float MinZoom = 0.008f;
public const float MaxZoom = 6f;
public Camera2D Camera { get; } = new();
public TerrainLayer Terrain { get; } = new() { ZIndex = 0 };
public TrackLayer Track { get; } = new() { ZIndex = 2 };
public EntityLayer Entities { get; } = new() { ZIndex = 1 };
public TrainLayer Trains { get; } = new() { ZIndex = 5 };
public ToolOverlay Tools { get; } = new() { ZIndex = 8 };
/// Clic sinistro sulla mappa (posizione nel mondo).
public event Action? Clicked;
/// Clic destro senza trascinamento (annulla lo strumento).
public event Action? Cancelled;
/// Spostamento del mouse sulla mappa (posizione nel mondo).
public event Action? Hovered;
private bool _dragging;
private Vector2 _dragDistance;
private float _lastZoom = -1;
public override void _Ready()
{
AddChild(Terrain);
AddChild(Entities);
AddChild(Track);
AddChild(Trains);
AddChild(Tools);
AddChild(Camera);
Camera.Enabled = true;
Camera.MakeCurrent();
}
public void Bind(GameSession session)
{
Terrain.Bind(session.World);
Track.Bind(session.World);
Entities.Bind(session.World);
Trains.Bind(session);
Tools.Bind(session.World);
var t = session.World.Summary!;
Camera.Position = new Vector2((float)(t.WidthCells * t.CellSizeM / 2), (float)(t.HeightCells * t.CellSizeM / 2));
float fit = (float)(GetViewportRect().Size.X / (t.WidthCells * t.CellSizeM));
SetZoom(Math.Clamp(fit * 2.5f, MinZoom, MaxZoom));
}
public float Zoom => Camera.Zoom.X;
public void SetZoom(float zoom)
{
zoom = Math.Clamp(zoom, MinZoom, MaxZoom);
Camera.Zoom = new Vector2(zoom, zoom);
}
public void CenterOn(Vector2 world) => Camera.Position = world;
/// Rettangolo del mondo visibile.
public Rect2 VisibleRect()
{
var size = GetViewportRect().Size / Zoom;
return new Rect2(Camera.GetScreenCenterPosition() - size / 2, size);
}
public override void _Process(double delta)
{
var move = Vector2.Zero;
if (Input.IsKeyPressed(Key.W) || Input.IsKeyPressed(Key.Up)) move.Y -= 1;
if (Input.IsKeyPressed(Key.S) || Input.IsKeyPressed(Key.Down)) move.Y += 1;
if (Input.IsKeyPressed(Key.A) || Input.IsKeyPressed(Key.Left)) move.X -= 1;
if (Input.IsKeyPressed(Key.D) || Input.IsKeyPressed(Key.Right)) move.X += 1;
if (move != Vector2.Zero && GetViewport().GuiGetFocusOwner() is not LineEdit)
Camera.Position += move.Normalized() * (float)delta * 700f / Zoom;
if (Math.Abs(Zoom - _lastZoom) > 1e-6)
{
_lastZoom = Zoom;
Terrain.SetZoom(Zoom);
Track.SetZoom(Zoom);
Entities.SetZoom(Zoom);
Trains.SetZoom(Zoom);
Tools.SetZoom(Zoom);
}
}
public override void _UnhandledInput(InputEvent e)
{
switch (e)
{
case InputEventMouseButton { ButtonIndex: MouseButton.WheelUp, Pressed: true } wheel:
ZoomAt(wheel.Position, 1.15f);
GetViewport().SetInputAsHandled();
break;
case InputEventMouseButton { ButtonIndex: MouseButton.WheelDown, Pressed: true } wheel:
ZoomAt(wheel.Position, 1 / 1.15f);
GetViewport().SetInputAsHandled();
break;
case InputEventMouseButton { ButtonIndex: MouseButton.Right or MouseButton.Middle } b:
if (b.Pressed)
{
_dragging = true;
_dragDistance = Vector2.Zero;
}
else
{
_dragging = false;
if (b.ButtonIndex == MouseButton.Right && _dragDistance.Length() < 4) Cancelled?.Invoke();
}
GetViewport().SetInputAsHandled();
break;
case InputEventMouseButton { ButtonIndex: MouseButton.Left, Pressed: true }:
Clicked?.Invoke(GetGlobalMousePosition());
GetViewport().SetInputAsHandled();
break;
case InputEventMouseMotion motion:
if (_dragging)
{
Camera.Position -= motion.Relative / Zoom;
_dragDistance += motion.Relative;
}
Hovered?.Invoke(GetGlobalMousePosition());
break;
}
}
private void ZoomAt(Vector2 screen, float factor)
{
var before = GetCanvasTransform().AffineInverse() * screen;
SetZoom(Zoom * factor);
// la camera si sposta perché il punto sotto il cursore resti fermo
var size = GetViewportRect().Size;
var offset = (screen - size / 2) / Zoom;
Camera.Position = before - offset;
}
}