Files
Rail_Enterprise/client/RailEnterprise.Client/scripts/Panels/IndustryWidgets.cs
T
Alby96andClaude Opus 5.5 45462bf9ec F6: scheda industria e schermata di connessione definitive
La scheda dell'industria e la schermata di connessione si allineano alle
immagini di riferimento 07 e 09. I dati che mancavano li calcola il Core
con una funzione pura, e viaggiano in blocchi compatibili del protocollo.
Il formato dei salvataggi non cambia (ADR 0021).

- IndustryOutlook calcola la scheda in sola lettura:
  - fattori dell'efficienza e quello che la limita;
  - ingressi e uscite al mese, con giorni di scorta o di magazzino;
  - fornitori e clienti con prezzo, distanza e linea che li collega;
  - forza lavoro dai contratti dei pendolari;
  - punteggio di crescita del trimestre con le soglie;
  - prezzo medio, utile annuo, stazioni che la servono, sblocco
    dell'acquisto.
  IndustryMath.GrowthScore è ora l'unica formula del punteggio e la usa
  anche IndustrySystem: gli hash non cambiano. Nuovi parametri in
  balance.json, industry.outlook.
- Protocollo:
  - IndustryCard: blocco 1 IndustrySheetInfo;
  - Welcome: blocco 1 ServerInfo (versione, sistema, Docker, tempo di
    attività, passo del ciclo, client collegati);
  - ClientWorld conserva lo stato del server e l'elenco dei salvataggi.
- Client:
  - IndustryPanel a tutto schermo, con tre schede, il grafo della
    catena, impianti e crescita, e i comandi del proprietario;
  - ConnectScreen sostituisce MenuScreen: server locale o in rete,
    stato del server, partite salvate, «Connetti e riprendi» e «Nuova
    partita sul server» su una sessione di prova;
  - testi in strings.csv;
  - pilota automatico: schermate 19_scheda_industria e 20_connessione.
- Test:
  - IndustryOutlookTests;
  - scheda e stato del server via WebSocket;
  - campioni del protocollo con i blocchi nuovi.
  200 test, 181 senza i lenti.

Refs #42.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:04:28 +01:00

186 lines
7.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Godot;
using RailEnterprise.Client.App;
namespace RailEnterprise.Client.Panels;
/// <summary>Anello dell'efficienza (immagine 07, in alto a destra): arco ambra sulla percentuale.</summary>
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);
}
}
/// <summary>
/// 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.
/// </summary>
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);
}
}
/// <summary>Come si disegna un collegamento della catena produttiva (legenda dell'immagine 07).</summary>
public enum ChainEdge
{
/// <summary>Merci su una linea della compagnia: linea piena ambra.</summary>
Goods,
/// <summary>Lavoratori pendolari: tratteggio blu.</summary>
Workers,
/// <summary>Personale qualificato: puntinato azzurro.</summary>
Staff,
/// <summary>Partner possibile ma non collegato: tratteggio grigio.</summary>
Unlinked,
}
/// <summary>Nodo del grafo: titolo, riga secondaria, stile del collegamento, industria da aprire (−1 se nessuna).</summary>
public sealed record ChainNode(string Title, string Subtitle, ChainEdge Edge, int Industry = -1);
/// <summary>
/// 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.
/// </summary>
public partial class ChainGraph : Control
{
private const float NodeW = 210, NodeH = 50, Gap = 10;
private readonly List<(Rect2 Rect, ChainNode Node)> _hits = [];
private List<ChainNode> _left = [], _right = [];
private ChainNode? _center;
public event Action<int>? IndustryClicked;
public ChainGraph()
{
CustomMinimumSize = new Vector2(0, 260);
MouseFilter = MouseFilterEnum.Stop;
}
public void Set(List<ChainNode> left, ChainNode center, List<ChainNode> 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<Rect2> Column(List<ChainNode> 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<Rect2>(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:
DrawDashedLine(from, to, Palette.Border, 2, 8);
break;
}
}
private void Node(Rect2 rect, ChainNode node, bool center)
{
var border = center ? Palette.Amber : node.Edge switch
{
ChainEdge.Goods => Palette.AmberDark,
ChainEdge.Workers => Palette.Workers,
ChainEdge.Staff => Palette.Staff,
_ => Palette.Border,
};
DrawStyleBox(UiTheme.Box(center ? Palette.AmberDark : Palette.Inner, border, 8, 0), rect);
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));
}
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;
}
}
}