using Godot; using RailEnterprise.Client.App; using RailEnterprise.Core.Rail; using RailEnterprise.Protocol.Client; namespace RailEnterprise.Client.Map; /// Sagome degli strumenti: tracciato in corso con l'anteprima delle opere, stazione o deposito da posare, fermate di una linea. public partial class ToolOverlay : Node2D { public enum Ghost { None, Station, Depot, Demolish } private ClientWorld? _world; private float _zoom = 1; private List _points = []; private Vector2? _mouse; private TrackPlan? _plan; private Ghost _ghost; private Vector2? _ghostAt; private List _stops = []; public void Bind(ClientWorld world) => _world = world; public void SetZoom(float zoom) { _zoom = zoom; QueueRedraw(); } public void SetTrack(List points, Vector2? mouse, TrackPlan? plan) { _points = points; _mouse = mouse; _plan = plan; QueueRedraw(); } public void SetGhost(Ghost ghost, Vector2? at) { _ghost = ghost; _ghostAt = at; QueueRedraw(); } public void SetStops(List stops) { _stops = stops; QueueRedraw(); } public void Clear() { _points = []; _mouse = null; _plan = null; _ghost = Ghost.None; _ghostAt = null; _stops = []; QueueRedraw(); } public static Color WorkColor(WorkKind kind) => kind switch { WorkKind.Embankment => new Color("e8e0c2"), WorkKind.Cutting => new Color("8f8a73"), WorkKind.Bridge => new Color("5b8fb0"), WorkKind.Tunnel => new Color("4a4740"), _ => Palette.Ballast, }; public override void _Draw() { float s = MapScale.Symbol(_zoom); if (_plan is { Samples.Count: > 1 } plan) { var color = plan.Valid ? Palette.Ok : Palette.Danger; for (int i = 0; i < plan.Samples.Count - 1; i++) { var a = MapScale.V(plan.Samples[i].Point); var b = MapScale.V(plan.Samples[i + 1].Point); DrawLine(a, b, Palette.WithAlpha(color, 0.45f), 14f * s); if (plan.Samples[i].Kind == WorkKind.Tunnel) DrawDashedLine(a, b, WorkColor(WorkKind.Tunnel), 5f * s, 6f * s); else DrawLine(a, b, WorkColor(plan.Samples[i].Kind), 7f * s); } } var pts = new List(_points); if (_mouse is { } m && pts.Count > 0) pts.Add(m); for (int i = 0; i < pts.Count - 1; i++) DrawDashedLine(pts[i], pts[i + 1], Palette.Amber, 2f * s, 8f * s); foreach (var p in _points) DrawCircle(p, 4f * s, Palette.Amber); if (_ghostAt is { } g && _world is not null) { switch (_ghost) { case Ghost.Station: float radius = (float)(_world.Data.Construction.Stations.CatchmentKm * 1000); for (int k = 0; k < 48; k += 2) { float a0 = k * MathF.Tau / 48, a1 = (k + 1) * MathF.Tau / 48; DrawLine(g + new Vector2(MathF.Cos(a0), MathF.Sin(a0)) * radius, g + new Vector2(MathF.Cos(a1), MathF.Sin(a1)) * radius, Palette.Amber, 2.5f * s); } DrawCircle(g, radius, Palette.WithAlpha(Palette.Amber, 0.08f)); DrawRect(new Rect2(g - new Vector2(12, 6) * s, new Vector2(24, 12) * s), Palette.WithAlpha(Palette.TileRoof, 0.8f)); break; case Ghost.Depot: DrawRect(new Rect2(g - new Vector2(16, 9) * s, new Vector2(32, 18) * s), Palette.WithAlpha(new Color("5a6670"), 0.8f)); break; case Ghost.Demolish: DrawCircle(g, 30f * s, Palette.WithAlpha(Palette.Danger, 0.3f)); DrawLine(g - new Vector2(10, 10) * s, g + new Vector2(10, 10) * s, Palette.Danger, 3f * s); DrawLine(g + new Vector2(-10, 10) * s, g + new Vector2(10, -10) * s, Palette.Danger, 3f * s); break; } } for (int i = 0; i < _stops.Count; i++) { if (i > 0) DrawDashedLine(_stops[i - 1], _stops[i], Palette.Amber, 3f * s, 10f * s); DrawCircle(_stops[i], 9f * s, Palette.Amber); DrawString(UiTheme.BodyBold, _stops[i] + new Vector2(-4, 5) * s, (i + 1).ToString(), HorizontalAlignment.Left, -1, (int)Math.Max(8, 12 * s), Palette.AppBackground); } } }