Files
Rail_Enterprise/client/ViaFerrea.Client/scripts/Panels/LinesPanel.cs
T
Claude 9a4ada6d9d F3: client Godot minimo, replay dei comandi e collaudi automatici
- Client Godot 4.7 .NET (Godot.NET.Sdk 4.7.2): menu, collegamento, partita
  locale con server figlio; mappa con terreno (fasce, ombreggiatura, rive
  curve con shader, curve di livello), opere, entità, alberi, treni
  interpolati con fumo; strumenti binari con profilo, stazione, deposito,
  segnali, demolizione; pannelli treni, linee, mercato, finanze, schede;
  barra superiore, minimappa, notiziario; italiano e inglese.
- Pilota automatico del client e scripts/client-autopilot.sh: partita locale,
  linea con ponte, treni in moto e schermate (docs/screenshots).
- Registro dei comandi del server (--record) e viaferrea-tools replay.
- PROTOCOL.md normativo, ADR 0009, ARCHITECTURE, STATE e README aggiornati.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
2026-10-02 00:47:42 +00:00

118 lines
4.4 KiB
C#

using Godot;
using ViaFerrea.Client.App;
using ViaFerrea.Client.Net;
using ViaFerrea.Core.Commands;
using ViaFerrea.Core.Common;
using ViaFerrea.Core.Rail;
using static ViaFerrea.Client.App.Localization;
namespace ViaFerrea.Client.Panels;
/// <summary>Linee: fermate scelte con un clic sulle stazioni, nome e colore, attesa del pieno alla prima fermata.</summary>
public partial class LinesPanel : VBoxContainer
{
private static readonly (string Name, string Hex)[] Colors =
[
("Ambra", "#e0a526"), ("Blu", "#3f86d1"), ("Rosso coppo", "#b3573e"), ("Verde", "#5fc39b"), ("Celeste", "#9cc9f2"), ("Grano", "#d9c26a"),
];
private readonly GameSession _session;
private readonly ItemList _stopsList = new() { CustomMinimumSize = new Vector2(0, 110) };
private readonly LineEdit _name = new() { PlaceholderText = T("Nome della linea") };
private readonly OptionButton _color = new();
private readonly CheckBox _waitFull = new() { Text = T("Attendi il pieno alla prima fermata (max 2 giorni)") };
private readonly ItemList _lines = new() { CustomMinimumSize = new Vector2(0, 120) };
private readonly Label _status = UiTheme.Text("", 13, Palette.TextSecondary);
private int _version = -1;
public List<int> Stops { get; } = [];
public event Action? StopsChanged;
public LinesPanel(GameSession session)
{
_session = session;
AddThemeConstantOverride("separation", 8);
AddChild(UiTheme.TitleLabel(T("Linee"), 34));
var help = UiTheme.Text(T("Clic sulle stazioni della mappa per aggiungere le fermate in ordine; la linea torna alla prima."), 13, Palette.TextSecondary);
help.AutowrapMode = TextServer.AutowrapMode.WordSmart;
AddChild(help);
AddChild(_stopsList);
foreach (var (name, _) in Colors) _color.AddItem(T(name));
_color.Selected = 0;
AddChild(_name);
AddChild(_color);
AddChild(_waitFull);
var row = PanelKit.Row();
var create = UiTheme.Primary(T("Crea linea"));
create.SizeFlagsHorizontal = SizeFlags.ExpandFill;
create.Pressed += Create;
var clear = new Button { Text = T("Svuota") };
clear.Pressed += () =>
{
Stops.Clear();
RefreshStops();
};
row.AddChild(create);
row.AddChild(clear);
AddChild(row);
_status.AutowrapMode = TextServer.AutowrapMode.WordSmart;
AddChild(_status);
AddChild(PanelKit.Separator());
AddChild(UiTheme.Text(T("Linee della compagnia"), 14, bold: true));
AddChild(_lines);
}
public void AddStop(int station)
{
if (Stops.Count > 0 && Stops[^1] == station) return;
Stops.Add(station);
RefreshStops();
}
private void RefreshStops()
{
_stopsList.Clear();
for (int i = 0; i < Stops.Count; i++)
{
string name = _session.World.Stations.TryGetValue(Stops[i], out var s) ? s.Name : $"#{Stops[i]}";
_stopsList.AddItem($"{i + 1}. {name}");
}
StopsChanged?.Invoke();
}
public override void _Process(double delta)
{
var w = _session.World;
if (w.EntityVersion == _version) return;
_version = w.EntityVersion;
_lines.Clear();
int company = w.Summary?.CompanyId ?? 0;
foreach (var l in w.Lines.Values.Where(l => l.Owner == company).OrderBy(l => l.Id))
{
int trains = w.Trains.Values.Count(t => t.Line == l.Id);
string stops = string.Join(" → ", l.Stops.Select(s => w.Stations.TryGetValue(s.Station.Value, out var st) ? st.Name : "?"));
_lines.AddItem($"{l.Name} ({trains} {T("treni")}): {stops}");
}
}
private void Create()
{
if (Stops.Count < 2)
{
_status.Text = T("Servono almeno due fermate");
return;
}
var stops = Stops.Select((s, i) => new LineStop(new StationId(s), WaitFull: i == 0 && _waitFull.ButtonPressed, MaxWaitDays: i == 0 && _waitFull.ButtonPressed ? 2 : 0)).ToList();
string name = string.IsNullOrWhiteSpace(_name.Text) ? $"{T("Linea")} {_session.World.Lines.Count + 1}" : _name.Text.Trim();
_session.Command(new CreateLineCommand(name, Colors[Math.Max(0, _color.Selected)].Hex, stops), r =>
{
_status.Text = r.Message;
if (!r.Ok) return;
Stops.Clear();
_name.Text = "";
RefreshStops();
});
}
}