Files
Alby96 69416e7db8
CI / Build e test (pull_request) Successful in 1m37s
CI / Immagine Docker (pull_request) Successful in 35s
Merge remote-tracking branch 'origin/main' into claude/f6-tooltip
# Conflicts:
#	client/RailEnterprise.Client/scripts/Hud/TopBar.cs
#	docs/STATE.md
2026-11-08 15:41:01 +01:00

178 lines
8.7 KiB
C#
Raw Permalink 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;
using RailEnterprise.Client.Net;
using RailEnterprise.Client.Panels;
using RailEnterprise.Core.Common;
using RailEnterprise.Core.Economy;
using RailEnterprise.Protocol.Messages;
using static RailEnterprise.Client.App.Localization;
namespace RailEnterprise.Client.Hud;
/// <summary>Barra superiore (immagine 01): compagnia e livello, data, velocità, cassa, patrimonio, rating, stato del server, audio.</summary>
public partial class TopBar : PanelContainer
{
private GameSession? _session;
private readonly Label _company = UiTheme.TitleLabel("", 30);
private readonly PanelContainer _tier = UiTheme.Badge("");
private readonly Label _climb = UiTheme.Text("", 12, Palette.TextSecondary);
private long _climbRequestedMonth = long.MinValue;
private double _sinceClimbRequest = double.MaxValue;
/// <summary>Clic sul livello: apre «La tua scalata».</summary>
public event Action? ClimbRequested;
private readonly Label _date = UiTheme.Text("", 17, bold: true);
private readonly Label _cash = UiTheme.Text("", 20, Palette.AmberLight, bold: true);
private readonly Label _worth = UiTheme.Text("", 20, bold: true);
private readonly Label _rating = UiTheme.Text("", 20, bold: true);
private readonly Label _server = UiTheme.Text("", 14);
private readonly ColorRect _serverDot = new() { CustomMinimumSize = new Vector2(10, 10) };
private readonly List<Button> _speeds = [];
private VBoxContainer? _tierBox;
private HBoxContainer? _speedBox;
/// <summary>Pulsanti della velocità (per l'evidenziazione del tutorial).</summary>
public Control? SpeedBox => _speedBox;
/// <summary>Livello e avanzamento (per l'evidenziazione del tutorial).</summary>
public Control? TierBox => _tierBox;
public override void _Ready()
{
AddThemeStyleboxOverride("panel", UiTheme.Box(Palette.Panel, Palette.Border, 0, 14));
CustomMinimumSize = new Vector2(0, 68);
var row = new HBoxContainer();
row.AddThemeConstantOverride("separation", 18);
AddChild(row);
row.AddChild(new Logo { CustomMinimumSize = new Vector2(40, 40) });
row.AddChild(_company);
// livello attuale e avanzamento verso il prossimo (dal quadro della scalata): un clic apre il pannello
var tierBox = new VBoxContainer { Alignment = BoxContainer.AlignmentMode.Center, MouseFilter = MouseFilterEnum.Stop };
tierBox.AddThemeConstantOverride("separation", 2);
tierBox.MouseDefaultCursorShape = CursorShape.PointingHand;
var badgeRow = new HBoxContainer();
badgeRow.AddChild(_tier);
tierBox.AddChild(badgeRow);
tierBox.AddChild(_climb);
tierBox.GuiInput += e =>
{
if (e is InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.Left }) ClimbRequested?.Invoke();
};
_tier.MouseFilter = MouseFilterEnum.Pass;
_climb.MouseFilter = MouseFilterEnum.Pass;
row.AddChild(tierBox);
row.AddChild(new Control { SizeFlagsHorizontal = SizeFlags.ExpandFill });
row.AddChild(_date);
var speeds = new HBoxContainer();
speeds.AddThemeConstantOverride("separation", 4);
foreach (byte speed in new byte[] { 0, 1, 2, 4, 8 })
{
var b = new Button { Text = speed == 0 ? "II" : $"{speed}×", ToggleMode = true, CustomMinimumSize = new Vector2(46, 40) };
b.Pressed += () => _session?.SetSpeed(speed);
b.TooltipText = speed == 0 ? T("Pausa") : T("Velocità");
_speeds.Add(b);
speeds.AddChild(b);
}
row.AddChild(speeds);
_speedBox = speeds;
row.AddChild(new Control { SizeFlagsHorizontal = SizeFlags.ExpandFill });
row.AddChild(Stat(T("Cassa"), _cash));
row.AddChild(Stat(T("Patrimonio netto"), _worth));
row.AddChild(Stat(T("Rating"), _rating));
// tooltip «perché» (ADR 0024): cassa con i movimenti del mese, rating, avanzamento del livello
// la cassa della barra è quella dell'ultimo aggiornamento delle entità (inizio giornata o dopo un comando),
// la spiegazione è di adesso: se differiscono il tooltip lo dice
WhyTooltip.Attach(_cash, () => new ExplainTarget(ExplainSubject.Cash), shown: () => ShownCash()?.Cents);
WhyTooltip.Attach(_rating, () => new ExplainTarget(ExplainSubject.Rating));
WhyTooltip.Attach(tierBox, () => new ExplainTarget(ExplainSubject.TierProgress), T("Clic per aprire «La tua scalata»"));
var chip = new PanelContainer();
chip.AddThemeStyleboxOverride("panel", UiTheme.Box(Palette.Inner, Palette.Border, 16, 12));
var chipRow = new HBoxContainer();
chipRow.AddThemeConstantOverride("separation", 8);
var dotBox = new CenterContainer();
dotBox.AddChild(_serverDot);
chipRow.AddChild(dotBox);
chipRow.AddChild(_server);
chip.AddChild(chipRow);
var chipCenter = new CenterContainer();
chipCenter.AddChild(chip);
row.AddChild(chipCenter);
var audio = new CenterContainer();
audio.AddChild(new Audio.SpeakerButton());
row.AddChild(audio);
}
private static VBoxContainer Stat(string caption, Label value)
{
var box = new VBoxContainer();
box.AddThemeConstantOverride("separation", -2);
var c = UiTheme.Text(caption, 12, Palette.TextSecondary);
c.HorizontalAlignment = HorizontalAlignment.Right;
value.HorizontalAlignment = HorizontalAlignment.Right;
box.AddChild(c);
box.AddChild(value);
return box;
}
public void Bind(GameSession session) => _session = session;
/// <summary>
/// Cassa mostrata: quella della compagnia nelle entità, aggiornata a inizio giornata e dopo ogni comando
/// (un prestito, un acquisto), altrimenti quella del resoconto del giorno.
/// </summary>
private Money? ShownCash()
{
var w = _session?.World;
if (w?.Summary is null) return null;
return w.Companies.GetValueOrDefault(w.Summary.CompanyId)?.Cash ?? w.LastDigest?.Cash;
}
public override void _Process(double delta)
{
var w = _session?.World;
if (w?.Summary is null) return;
var me = w.Companies.GetValueOrDefault(w.Summary.CompanyId);
_company.Text = me?.Name ?? w.Summary.CompanyName;
int tier = Math.Clamp(me?.Tier ?? 1, 1, w.Data.Tiers.Tiers.Count);
((Label)_tier.GetChild(0)).Text = $"{tier} · {Fmt.Tier(w, tier)}";
// il quadro della scalata si chiede una volta al mese di gioco (il pannello lo chiede ogni giorno), e
// di nuovo dopo 5 s se manca: uno stato completo (ripresa della sessione, nuova partita) lo azzera
_sinceClimbRequest += delta;
long month = w.Calendar is { } cal ? Fmt.Today(w) / cal.DaysPerMonth : 0;
if ((month != _climbRequestedMonth || (w.Climb is null && _sinceClimbRequest > 5)) && _session!.IsOpen)
{
_climbRequestedMonth = month;
_sinceClimbRequest = 0;
_session.Send(new RequestDetail { Kind = DetailKind.Climb, Target = 0 });
}
var climb = w.Climb;
bool top = tier >= w.Data.Tiers.Tiers.Count;
_climb.Text = top ? T("livello più alto")
: climb is not null && climb.Tier == tier ? $"{Fmt.Progress(climb.Progress)} {T("verso")} {Fmt.Tier(w, tier + 1)}"
: $"{T("prossimo")}: {Fmt.Tier(w, tier + 1)}";
_date.Text = w.LastDigest?.Date ?? w.Summary.Date;
_cash.Text = (ShownCash() ?? default).ToString();
_worth.Text = (w.LastDigest?.NetWorth ?? me?.NetWorth ?? default).ToString("short");
_rating.Text = w.LastDigest?.Rating ?? me?.Rating ?? "—";
byte speed = _session!.RequestedSpeed ?? w.LastDigest?.Speed ?? w.Speed;
for (int i = 0; i < _speeds.Count; i++) _speeds[i].SetPressedNoSignal(new byte[] { 0, 1, 2, 4, 8 }[i] == speed);
bool open = _session!.IsOpen;
_serverDot.Color = !open ? Palette.Danger : w.LastDigest?.Slow == true ? Palette.Amber : Palette.Ok;
_server.Text = open ? $"{_session.ServerLabel} {_session.LatencyMs:0} ms" : T("Disconnesso");
}
/// <summary>Marchio: ruota stilizzata ambra (immagine 01).</summary>
private sealed partial class Logo : Control
{
public override void _Draw()
{
var c = Size / 2;
float r = Math.Min(Size.X, Size.Y) / 2 - 3;
DrawArc(c, r, 0, MathF.Tau, 48, Palette.Amber, 3, true);
DrawLine(c - new Vector2(r, 0), c + new Vector2(r, 0), Palette.Amber, 3);
DrawLine(c - new Vector2(0, r), c + new Vector2(0, r), Palette.Amber, 3);
DrawCircle(c, r * 0.28f, Palette.Amber);
}
}
}