using Godot;
using RailEnterprise.Client.App;
using RailEnterprise.Core.Common;
using RailEnterprise.Protocol.Client;
namespace RailEnterprise.Client.Map;
///
/// Etichette a dimensione costante sullo schermo: città in Big Shoulders Display con gli abitanti,
/// industrie e stazioni in IBM Plex Sans; scala grafica in basso a destra (immagine 01).
///
public partial class LabelLayer : Control
{
private ClientWorld? _world;
private Camera2D? _camera;
public void Bind(ClientWorld world, Camera2D camera)
{
_world = world;
_camera = camera;
MouseFilter = MouseFilterEnum.Ignore;
SetAnchorsPreset(LayoutPreset.FullRect);
}
public override void _Process(double delta) => QueueRedraw();
private Vector2 ToScreen(Vector2 world)
{
var vt = GetViewport().GetCanvasTransform();
return vt * world;
}
public override void _Draw()
{
var w = _world;
if (w?.Terrain is null || _camera is null) return;
float zoom = _camera.Zoom.X;
var size = GetViewportRect().Size;
bool InView(Vector2 p) => p.X > -200 && p.Y > -100 && p.X < size.X + 200 && p.Y < size.Y + 100;
foreach (var town in w.Towns.Values)
{
var p = ToScreen(MapScale.V(town.Position));
if (!InView(p)) continue;
int titleSize = town.Population >= 10000 ? 30 : town.Population >= 3000 ? 26 : 22;
Centered(UiTheme.Title, town.Name, p + new Vector2(0, -6), titleSize, new Color("2a2f2c"), outline: true);
Centered(UiTheme.Body, $"{Money.GroupThousands((ulong)town.Population)} {Localization.T("abitanti")}", p + new Vector2(0, 12), 13, new Color("3d4440"), outline: true);
}
if (zoom >= 0.02f)
{
foreach (var ind in w.Industries.Values)
{
var p = ToScreen(MapScale.V(ind.Position) + new Vector2(0, (float)(ind.FootprintCells * w.Terrain.CellSizeM * 0.5)));
if (!InView(p)) continue;
Centered(UiTheme.BodyBold, ind.Name, p + new Vector2(0, 14), 13, new Color("2a2f2c"), outline: true);
}
foreach (var hub in w.Hubs.Values)
{
var p = ToScreen(MapScale.V(hub.Position));
if (!InView(p)) continue;
Centered(UiTheme.BodyBold, hub.Name, p + new Vector2(0, 26), 13, new Color("2a2f2c"), outline: true);
Centered(UiTheme.Body, Localization.T("hub mercato mondiale"), p + new Vector2(0, 42), 12, new Color("3d4440"), outline: true);
}
}
foreach (var st in w.Stations.Values)
{
var p = ToScreen(MapScale.V(st.Position));
if (!InView(p)) continue;
Centered(UiTheme.Body, st.Name, p + new Vector2(0, -18), 12, new Color("2a2f2c"), outline: true);
}
foreach (var d in w.Depots.Values)
{
var p = ToScreen(MapScale.V(d.Position));
if (!InView(p)) continue;
Centered(UiTheme.BodyBold, Localization.T("Deposito"), p + new Vector2(0, 26), 12, new Color("2a2f2c"), outline: true);
}
DrawScaleBar(size, zoom);
}
private void Centered(Font font, string text, Vector2 at, int size, Color color, bool outline)
{
var dims = font.GetStringSize(text, HorizontalAlignment.Left, -1, size);
var pos = at - new Vector2(dims.X / 2, 0);
if (outline) DrawStringOutline(font, pos, text, HorizontalAlignment.Left, -1, size, 4, new Color(0.86f, 0.88f, 0.82f, 0.85f));
DrawString(font, pos, text, HorizontalAlignment.Left, -1, size, color);
}
/// Scala grafica con una lunghezza «tonda» di circa 100 px.
private void DrawScaleBar(Vector2 size, float zoom)
{
double meters = 100 / zoom;
double[] nice = [50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000];
double m = nice.FirstOrDefault(v => v >= meters, nice[^1]);
float px = (float)(m * zoom);
var end = new Vector2(size.X - 30, size.Y - 60);
var start = end - new Vector2(px, 0);
var c = new Color("2a2f2c");
DrawLine(start, end, c, 2);
DrawLine(start, start + new Vector2(0, -8), c, 2);
DrawLine(end, end + new Vector2(0, -8), c, 2);
DrawLine((start + end) / 2, (start + end) / 2 + new Vector2(0, -5), c, 2);
string label = m >= 1000 ? $"{m / 1000:0} km" : $"{m:0} m";
Centered(UiTheme.Body, label, (start + end) / 2 + new Vector2(0, 18), 12, c, outline: true);
}
}