diff --git a/StarterNG.Tests/ConsistRunsTests.cs b/StarterNG.Tests/ConsistRunsTests.cs new file mode 100644 index 0000000..bbc1b45 --- /dev/null +++ b/StarterNG.Tests/ConsistRunsTests.cs @@ -0,0 +1,68 @@ +using StarterNG.Classes; +using StarterNG.Domain; + +namespace StarterNG.Tests; + +public class ConsistRunsTests +{ + private static List Vehicles(params string[] skins) => + new Trainset(string.Concat( + "trainset a tor 0 0\n", + string.Concat(skins.Select((s, i) => $"node -1 0 v{i} dynamic d {s} m 0 nobody 3 0 enddynamic\n")), + "endtrainset")).Vehicles; + + private static List<(string Skin, int Count)> Runs(List vehicles, Func keyOf) => + ConsistRuns.Of(vehicles, keyOf).Select(r => (r.First.SkinFile!, r.Count)).ToList(); + + [Fact] + public void Vehicles_alike_next_to_each_other_make_one_run() + { + var vehicles = Vehicles("eu07", "412w", "412w", "412w", "eaos", "eaos"); + + Assert.Equal( + [("eu07", 1), ("412w", 3), ("eaos", 2)], + Runs(vehicles, (v, _) => v.SkinFile)); + } + + [Fact] + public void Vehicles_alike_apart_stay_apart() + { + var vehicles = Vehicles("412w", "eaos", "412w"); + + Assert.Equal( + [("412w", 1), ("eaos", 1), ("412w", 1)], + Runs(vehicles, (v, _) => v.SkinFile)); + } + + [Fact] + public void Alike_goes_by_the_key_whatever_its_case() + { + var vehicles = Vehicles("412W", "412w"); + + Assert.Equal([("412W", 2)], Runs(vehicles, (v, _) => v.SkinFile)); + } + + [Fact] + public void A_vehicle_with_no_key_stands_alone() + { + var vehicles = Vehicles("x", "x", "x"); + + Assert.Equal( + [("x", 1), ("x", 1), ("x", 1)], + Runs(vehicles, (_, _) => null)); + } + + [Fact] + public void The_place_in_the_consist_can_set_a_vehicle_apart() + { + var vehicles = Vehicles("sm42", "sm42", "sm42"); + + Assert.Equal( + [("sm42", 1), ("sm42", 2)], + Runs(vehicles, (v, i) => i == 0 ? "head" : v.SkinFile)); + } + + [Fact] + public void No_vehicles_no_runs() => + Assert.Empty(ConsistRuns.Of([], (v, _) => v.SkinFile)); +} diff --git a/StarterNG.Tests/WarehouseEditTests.cs b/StarterNG.Tests/WarehouseEditTests.cs index 2ffbcaa..ff56e5d 100644 --- a/StarterNG.Tests/WarehouseEditTests.cs +++ b/StarterNG.Tests/WarehouseEditTests.cs @@ -98,6 +98,23 @@ public class WarehouseEditTests Assert.Null(Stored("edit-changed")); } + [Fact] + public void A_write_tells_the_list_the_name_and_the_entry_and_no_write_tells_nothing() + { + var told = new List<(string Name, string Entry)>(); + _edit.Saved += (name, entry) => told.Add((name, entry)); + var trainset = _edit.Open(Preset("edit-told")); + + _edit.Save(); + Assert.Empty(told); + + trainset.Vehicles.Add(trainset.Vehicles[0].Clone()); + _edit.Save(); + + Assert.Equal([("edit-told", ConsistText.Serialize(trainset))], told); + PresetStore.Delete("edit-told"); + } + [Fact] public void Nothing_is_written_with_nothing_open() { diff --git a/StarterNG/Application/WarehouseEdit.cs b/StarterNG/Application/WarehouseEdit.cs index 04d5a34..2a90f3e 100644 --- a/StarterNG/Application/WarehouseEdit.cs +++ b/StarterNG/Application/WarehouseEdit.cs @@ -58,5 +58,9 @@ public sealed class WarehouseEdit PresetStore.Save(Name, entry); _saved = entry; + Saved?.Invoke(Name, entry); } + + /// The consist was written under its name, with this entry - for the list showing it. + public event Action? Saved; } diff --git a/StarterNG/Domain/ConsistRuns.cs b/StarterNG/Domain/ConsistRuns.cs new file mode 100644 index 0000000..ff5c404 --- /dev/null +++ b/StarterNG/Domain/ConsistRuns.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using StarterNG.Classes; + +namespace StarterNG.Domain; + +/// A stretch of a consist made of vehicles alike, shown as one picture with a count. +public sealed record ConsistRun(Dynamic First, int Count); + +public static class ConsistRuns +{ + /// + /// The consist's vehicles in order, those next to each other that look alike taken together. + /// + /// + /// What makes two vehicles alike, given the vehicle and its place in the consist. + /// No key keeps the vehicle on its own. + /// + public static List Of(IReadOnlyList vehicles, Func keyOf) + { + var runs = new List(); + string? prevKey = null; + + for (int i = 0; i < vehicles.Count; i++) + { + string? key = keyOf(vehicles[i], i); + if (key is not null && prevKey is not null && runs.Count > 0 && + string.Equals(key, prevKey, StringComparison.OrdinalIgnoreCase)) + { + var last = runs[^1]; + runs[^1] = last with { Count = last.Count + 1 }; + } + else + { + runs.Add(new ConsistRun(vehicles[i], 1)); + } + prevKey = key; + } + + return runs; + } +} diff --git a/StarterNG/Views/Depot.axaml.cs b/StarterNG/Views/Depot.axaml.cs index a17e2ba..ea3ac4e 100644 --- a/StarterNG/Views/Depot.axaml.cs +++ b/StarterNG/Views/Depot.axaml.cs @@ -77,6 +77,7 @@ public partial class Depot : UserControl EditWarehouseSelection(); }; _warehouse.Renamed += _warehouseEdit.Renamed; + _warehouseEdit.Saved += _warehouse.RefreshEntry; _browser = new VehicleBrowser(categoryCombo, classCombo, vehicleListBox, searchBox, hideArchivalCheck, miniPreview, addVehicleButton, _db, _minis, OpenVehicleBase) diff --git a/StarterNG/Views/Depot/Warehouse.cs b/StarterNG/Views/Depot/Warehouse.cs index 1c9d927..5f9b6da 100644 --- a/StarterNG/Views/Depot/Warehouse.cs +++ b/StarterNG/Views/Depot/Warehouse.cs @@ -62,11 +62,7 @@ public sealed class Warehouse var entry = new ListBoxItem { Tag = preset.Name, - Content = new TextBlock - { - Text = preset.Name, - TextTrimming = TextTrimming.CharacterEllipsis - } + Content = EntryContent(preset.Name, preset.Entry) }; _list.Items.Add(entry); if (current is not null && string.Equals(current, preset.Name, StringComparison.Ordinal)) @@ -81,6 +77,48 @@ public sealed class Warehouse SelectionChanged?.Invoke(); } + /// + /// The consist's name, and under it its vehicles as minis - as the scenery's consists + /// are listed - so one is told from another without opening it. + /// + private static Control EntryContent(string presetName, string entry) + { + var name = new TextBlock + { + Text = presetName, + TextTrimming = TextTrimming.CharacterEllipsis + }; + if (ConsistText.VehiclesFrom(entry) is not { } vehicles) + return name; + + var content = new StackPanel + { + Spacing = 2, + Margin = new Thickness(0, 3), + Children = { name, TrainsetRow.ScrollingStrip(vehicles) } + }; + // Read by its name, not by every caption under it. + Avalonia.Automation.AutomationProperties.SetName(content, presetName); + return content; + } + + /// + /// Redraws one consist's entry after it was edited and saved, so its minis follow the + /// edit at once - the rest of the list, its selection and where it is scrolled stay. + /// + public void RefreshEntry(string name, string entry) + { + foreach (var obj in _list.Items) + { + if (obj is ListBoxItem { Tag: string tag } item && + string.Equals(tag, name, StringComparison.OrdinalIgnoreCase)) + { + item.Content = EntryContent(tag, entry); + return; + } + } + } + private string? SelectedName() => (_list.SelectedItem as ListBoxItem)?.Tag as string; public TrainsetPreset? SelectedPreset() => SelectedName() is { } name ? PresetNamed(name) : null; diff --git a/StarterNG/Views/Scenarios.axaml.cs b/StarterNG/Views/Scenarios.axaml.cs index 326a283..77967e9 100644 --- a/StarterNG/Views/Scenarios.axaml.cs +++ b/StarterNG/Views/Scenarios.axaml.cs @@ -257,13 +257,7 @@ public partial class Scenarios : UserControl } /// A consist's label in a list of them, here and on the depot page. - internal static TextBlock TrainsetListContent(Trainset trainset) => - new Avalonia.Controls.TextBlock - { - Text = TrainsetDisplay.ListLabel(trainset), - TextTrimming = Avalonia.Media.TextTrimming.CharacterEllipsis, - TextWrapping = Avalonia.Media.TextWrapping.NoWrap - }; + internal static Control TrainsetListContent(Trainset trainset) => TrainsetRow.Build(trainset); private void PopulateVehicleList(Scenery scn) { diff --git a/StarterNG/Views/TrainsetRow.cs b/StarterNG/Views/TrainsetRow.cs new file mode 100644 index 0000000..90969f1 --- /dev/null +++ b/StarterNG/Views/TrainsetRow.cs @@ -0,0 +1,188 @@ +using System.Collections.Generic; +using Avalonia; +using Avalonia.Automation; +using Avalonia.Controls; +using Avalonia.Layout; +using Avalonia.Media; +using StarterNG.Application; +using StarterNG.Classes; +using StarterNG.Domain; + +namespace StarterNG.Views; + +/// +/// A consist's row in a list of them: its vehicles as minis, each stretch of vehicles +/// alike as one picture with its name and count under it, so the consist reads at a glance. +/// +internal static class TrainsetRow +{ + /// The height the minis are made at: shown at it they stay sharp. + private const int MiniHeight = 30; + + private static readonly MiniTextures Minis = new(); + + /// Room under the captions for the scroll bar of a consist wider than the list. + private const double ScrollBarLane = 10; + + private static readonly IBrush AiTagBack = new SolidColorBrush(Color.Parse("#9098A0")); + private static readonly IBrush AiTagText = new SolidColorBrush(Color.Parse("#15171B")); + + public static Control Build(Trainset trainset) + { + string label = TrainsetDisplay.ListLabel(trainset); + + Control row; + if (TrainsetDisplay.IsEmpty(trainset)) + { + row = new TextBlock + { + Text = label, + FontStyle = FontStyle.Italic, + Opacity = 0.7, + TextTrimming = TextTrimming.CharacterEllipsis, + TextWrapping = TextWrapping.NoWrap, + VerticalAlignment = VerticalAlignment.Center + }; + } + else + { + bool ai = TrainsetDisplay.IsAi(trainset); + var panel = new DockPanel { Margin = new Thickness(0, 3), LastChildFill = true }; + if (ai) + { + var tag = AiTag(); + tag.Margin = new Thickness(8, 0, 0, 0); + DockPanel.SetDock(tag, Dock.Right); + panel.Children.Add(tag); + } + // A consist longer than the list is wide scrolls sideways in its own row. The wheel + // still scrolls the list: only shift with the wheel, or a touchpad, moves the strip. + panel.Children.Add(ScrollingStrip(trainset.Vehicles, ai)); + row = panel; + } + + ToolTip.SetTip(row, label); + AutomationProperties.SetName(row, label); + return row; + } + + /// + /// The vehicles' minis with their captions, scrolling sideways when wider than the room + /// they get - here and for the warehouse's consists. + /// + public static Control ScrollingStrip(IReadOnlyList vehicles, bool dimmed = false) + { + var strip = Strip(vehicles, dimmed); + var scroller = new ScrollViewer + { + HorizontalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Auto, + VerticalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Disabled, + Content = strip + }; + // The bar lies over the content; a consist that scrolls gets a lane under its + // captions for it, so the bar does not cross them. One that fits stays as low as it was. + scroller.ScrollChanged += (_, _) => + { + double below = scroller.Extent.Width > scroller.Viewport.Width + 0.5 ? ScrollBarLane : 0; + if (strip.Margin.Bottom != below) + strip.Margin = new Thickness(0, 0, 0, below); + }; + return scroller; + } + + /// + /// The minis, one next to the other with nothing between, as the vehicles stand coupled. + /// + private static Control Strip(IReadOnlyList vehicles, bool dimmed) + { + var strip = new StackPanel { Orientation = Orientation.Horizontal, Opacity = dimmed ? 0.6 : 1 }; + + var runs = ConsistRuns.Of(vehicles, (v, i) => $"{MiniName(v)}|{Caption(v, i)}"); + int place = 0; + foreach (var run in runs) + { + strip.Children.Add(RunView(run, place)); + place += run.Count; + } + return strip; + } + + private static Control RunView(ConsistRun run, int place) + { + var v = run.First; + var bmp = Minis.Get(MiniName(v), MiniHeight); + + Control picture; + double width; + if (bmp is not null) + { + width = bmp.Size.Height > 0 ? bmp.Size.Width * MiniHeight / bmp.Size.Height : MiniHeight; + // Fill at a width worked out here, not Uniform: rounding at a scale such as 125 % + // left a line of the background between two vehicles. + picture = MiniTextures.Sharp(new Image + { + Source = bmp, + Width = width, + Height = MiniHeight, + Stretch = Stretch.Fill + }); + } + else + { + picture = MiniTextures.MissingVisual(v.SkinFile, MiniHeight); + width = picture.Width; + } + + string caption = Caption(v, place); + if (run.Count > 1) + caption += $" ×{run.Count}"; + + return new StackPanel + { + Orientation = Orientation.Vertical, + Spacing = 2, + Width = width, + Children = + { + picture, + new TextBlock + { + Text = caption, + FontSize = 11, + Padding = new Thickness(3, 0), + HorizontalAlignment = HorizontalAlignment.Center, + TextTrimming = TextTrimming.CharacterEllipsis, + TextWrapping = TextWrapping.NoWrap + } + } + }; + } + + private static Control AiTag() => + new Border + { + Background = AiTagBack, + Padding = new Thickness(4, 0), + VerticalAlignment = VerticalAlignment.Center, + Child = new TextBlock + { + Text = "AI", + FontSize = 10, + FontWeight = FontWeight.Bold, + Foreground = AiTagText + } + }; + + /// A vehicle leading the consist under its own name, the rest under their class - as the composition text has them. + private static string Caption(Dynamic v, int place) => + TrainsetDisplay.VehicleLabel(v, head: place == 0 && TrainsetDisplay.IsPowered(v)); + + /// The mini the consist strip shows for the vehicle. + private static string? MiniName(Dynamic v) + { + string? mini = AppServices.Current.Library.Vehicles.MiniFor(v.DataFolder, v.SkinFile, v.MmdFile) ?? v.SkinFile; + if (!AppServices.Current.MiniTextures.Has(mini) && AppServices.Current.MiniTextures.Has(v.SkinFile)) + mini = v.SkinFile; + return mini; + } +}