using Godot; using RailEnterprise.Client.App; using RailEnterprise.Client.Net; using RailEnterprise.Core.Commands; using RailEnterprise.Core.Common; using RailEnterprise.Core.Rail; using static RailEnterprise.Client.App.Localization; namespace RailEnterprise.Client.Panels; /// Linee: fermate scelte con un clic sulle stazioni, nome e colore, attesa del pieno alla prima fermata. 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 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(); }); } }