using Godot;
using RailEnterprise.Client.App;
namespace RailEnterprise.Client.Panels;
/// Anello dell'efficienza (immagine 07, in alto a destra): arco ambra sulla percentuale.
public partial class EfficiencyRing : Control
{
private float _value;
private Color _color = Palette.Amber;
public EfficiencyRing() => CustomMinimumSize = new Vector2(112, 112);
public void Set(float value, Color color)
{
_value = Math.Clamp(value, 0, 1);
_color = color;
QueueRedraw();
}
public override void _Draw()
{
var c = Size / 2;
float r = Math.Min(Size.X, Size.Y) / 2 - 8;
DrawArc(c, r, 0, Mathf.Tau, 64, Palette.Inner, 10, true);
if (_value > 0) DrawArc(c, r, -Mathf.Pi / 2, -Mathf.Pi / 2 + Mathf.Tau * _value, 64, _color, 10, true);
string text = Fmt.Pct(_value, 0);
var font = UiTheme.BodyBold;
const int size = 24;
var s = font.GetStringSize(text, HorizontalAlignment.Left, -1, size);
DrawString(font, new Vector2(c.X - s.X / 2, c.Y + s.Y / 4), text, HorizontalAlignment.Left, -1, size, Palette.Text);
}
}
///
/// Barra del punteggio di crescita: il riempimento è il punteggio del trimestre in corso, le tacche
/// sono le soglie di declassamento (rossa) e di promozione (verde), la lineetta l'ultimo trimestre.
///
public partial class ScoreBar : Control
{
private float _score, _last = -1, _down, _up;
public ScoreBar() => CustomMinimumSize = new Vector2(0, 26);
public void Set(float score, float last, float down, float up)
{
(_score, _last, _down, _up) = (Math.Clamp(score, 0, 1), last, down, up);
QueueRedraw();
}
public override void _Draw()
{
float h = 12, y = 7, w = Size.X;
DrawRect(new Rect2(0, y, w, h), Palette.Inner);
var fill = _score >= _up ? Palette.Ok : _score < _down ? Palette.Danger : Palette.Amber;
DrawRect(new Rect2(0, y, w * _score, h), fill);
DrawLine(new Vector2(w * _down, 2), new Vector2(w * _down, y + h + 5), Palette.Danger, 2);
DrawLine(new Vector2(w * _up, 2), new Vector2(w * _up, y + h + 5), Palette.Ok, 2);
if (_last >= 0) DrawLine(new Vector2(w * Math.Clamp(_last, 0, 1), y - 3), new Vector2(w * Math.Clamp(_last, 0, 1), y + h + 3), Palette.Text, 2);
}
}
/// Come si disegna un collegamento della catena produttiva (legenda dell'immagine 07).
public enum ChainEdge
{
/// Merci su una linea della compagnia: linea piena ambra.
Goods,
/// Lavoratori pendolari: tratteggio blu.
Workers,
/// Personale qualificato: puntinato azzurro.
Staff,
/// Partner possibile ma non collegato: tratteggio grigio.
Unlinked,
}
/// Nodo del grafo: titolo, riga secondaria, stile del collegamento, industria da aprire (−1 se nessuna).
public sealed record ChainNode(string Title, string Subtitle, ChainEdge Edge, int Industry = -1);
///
/// Grafo della catena produttiva: fornitori e città dei pendolari a sinistra, l'industria al centro,
/// clienti a destra. I nodi delle industrie si aprono con un clic.
///
public partial class ChainGraph : Control
{
private const float NodeW = 210, NodeH = 50, Gap = 10;
private readonly List<(Rect2 Rect, ChainNode Node)> _hits = [];
private List _left = [], _right = [];
private ChainNode? _center;
public event Action? IndustryClicked;
public ChainGraph()
{
CustomMinimumSize = new Vector2(0, 260);
MouseFilter = MouseFilterEnum.Stop;
}
public void Set(List left, ChainNode center, List right)
{
(_left, _center, _right) = (left, center, right);
int rows = Math.Max(3, Math.Max(left.Count, right.Count));
CustomMinimumSize = new Vector2(0, rows * (NodeH + Gap) + 10);
QueueRedraw();
}
public override void _Draw()
{
_hits.Clear();
if (_center is null) return;
float w = Size.X, h = Size.Y;
float nodeW = Math.Min(NodeW, (w - 80) / 3);
var centerRect = new Rect2(w / 2 - nodeW / 2, h / 2 - NodeH * 0.7f, nodeW, NodeH * 1.4f);
var leftRects = Column(_left, 0, nodeW, h);
var rightRects = Column(_right, w - nodeW, nodeW, h);
for (int i = 0; i < _left.Count; i++)
Edge(new Vector2(leftRects[i].End.X, leftRects[i].GetCenter().Y), new Vector2(centerRect.Position.X, centerRect.GetCenter().Y), _left[i].Edge);
for (int i = 0; i < _right.Count; i++)
Edge(new Vector2(centerRect.End.X, centerRect.GetCenter().Y), new Vector2(rightRects[i].Position.X, rightRects[i].GetCenter().Y), _right[i].Edge);
for (int i = 0; i < _left.Count; i++) Node(leftRects[i], _left[i], false);
for (int i = 0; i < _right.Count; i++) Node(rightRects[i], _right[i], false);
Node(centerRect, _center, true);
}
private static List Column(List nodes, float x, float width, float height)
{
float total = nodes.Count * NodeH + Math.Max(0, nodes.Count - 1) * Gap;
float y = (height - total) / 2;
var rects = new List(nodes.Count);
foreach (var _ in nodes)
{
rects.Add(new Rect2(x, y, width, NodeH));
y += NodeH + Gap;
}
return rects;
}
private void Edge(Vector2 from, Vector2 to, ChainEdge edge)
{
switch (edge)
{
case ChainEdge.Goods:
DrawLine(from, to, Palette.Amber, 3, true);
break;
case ChainEdge.Workers:
DrawDashedLine(from, to, Palette.Workers, 2.5f, 10);
break;
case ChainEdge.Staff:
DrawDashedLine(from, to, Palette.Staff, 2.5f, 3);
break;
default:
// grigio leggibile sul fondo della scheda (il bordo dei pannelli sparirebbe)
DrawDashedLine(from, to, Palette.TextSecondary, 2, 8);
break;
}
}
private void Node(Rect2 rect, ChainNode node, bool center)
{
bool unlinked = !center && node.Edge == ChainEdge.Unlinked;
var border = center ? Palette.Amber : node.Edge switch
{
ChainEdge.Goods => Palette.AmberDark,
ChainEdge.Workers => Palette.Workers,
ChainEdge.Staff => Palette.Staff,
_ => Palette.Inner,
};
DrawStyleBox(UiTheme.Box(center ? Palette.AmberDark : Palette.Inner, border, 8, 0), rect);
// partner non collegato: bordo tratteggiato grigio, come «Opportunità non collegata» nell'immagine 07
if (unlinked) DashedRect(rect.Grow(-1), Palette.TextSecondary);
var font = UiTheme.BodyBold;
float width = rect.Size.X - 16;
DrawString(font, rect.Position + new Vector2(8, center ? 26 : 20), node.Title, HorizontalAlignment.Left, width, center ? 16 : 14,
center ? Palette.AmberLight : Palette.Text);
DrawString(UiTheme.Body, rect.Position + new Vector2(8, center ? 48 : 39), node.Subtitle, HorizontalAlignment.Left, width, 12, Palette.TextSecondary);
if (node.Industry >= 0) _hits.Add((rect, node));
}
private void DashedRect(Rect2 r, Color color)
{
var (a, b, c, d) = (r.Position, new Vector2(r.End.X, r.Position.Y), r.End, new Vector2(r.Position.X, r.End.Y));
DrawDashedLine(a, b, color, 1.5f, 6);
DrawDashedLine(b, c, color, 1.5f, 6);
DrawDashedLine(c, d, color, 1.5f, 6);
DrawDashedLine(d, a, color, 1.5f, 6);
}
public override void _GuiInput(InputEvent e)
{
if (e is not InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.Left } click) return;
foreach (var (rect, node) in _hits)
{
if (!rect.HasPoint(click.Position)) continue;
IndustryClicked?.Invoke(node.Industry);
AcceptEvent();
return;
}
}
}