mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-22 00:04:57 +01:00
feat(consists): the consist lists show the vehicles' minis, alike ones under one caption
The scenery's consists and the warehouse's are listed as a strip of minis at
their own 30 px, vehicles alike next to each other taken together under one
caption ("412W ×20"). A consist wider than the list scrolls sideways in its
row, with a lane for the bar under the captions. A warehouse consist's strip
follows an edit as soon as it is saved.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
68
StarterNG.Tests/ConsistRunsTests.cs
Normal file
68
StarterNG.Tests/ConsistRunsTests.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using StarterNG.Classes;
|
||||
using StarterNG.Domain;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class ConsistRunsTests
|
||||
{
|
||||
private static List<Dynamic> 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<Dynamic> vehicles, Func<Dynamic, int, string?> 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));
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -58,5 +58,9 @@ public sealed class WarehouseEdit
|
||||
|
||||
PresetStore.Save(Name, entry);
|
||||
_saved = entry;
|
||||
Saved?.Invoke(Name, entry);
|
||||
}
|
||||
|
||||
/// <summary>The consist was written under its name, with this entry - for the list showing it.</summary>
|
||||
public event Action<string, string>? Saved;
|
||||
}
|
||||
|
||||
42
StarterNG/Domain/ConsistRuns.cs
Normal file
42
StarterNG/Domain/ConsistRuns.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using StarterNG.Classes;
|
||||
|
||||
namespace StarterNG.Domain;
|
||||
|
||||
/// <summary>A stretch of a consist made of vehicles alike, shown as one picture with a count.</summary>
|
||||
public sealed record ConsistRun(Dynamic First, int Count);
|
||||
|
||||
public static class ConsistRuns
|
||||
{
|
||||
/// <summary>
|
||||
/// The consist's vehicles in order, those next to each other that look alike taken together.
|
||||
/// </summary>
|
||||
/// <param name="keyOf">
|
||||
/// What makes two vehicles alike, given the vehicle and its place in the consist.
|
||||
/// No key keeps the vehicle on its own.
|
||||
/// </param>
|
||||
public static List<ConsistRun> Of(IReadOnlyList<Dynamic> vehicles, Func<Dynamic, int, string?> keyOf)
|
||||
{
|
||||
var runs = new List<ConsistRun>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
@@ -257,13 +257,7 @@ public partial class Scenarios : UserControl
|
||||
}
|
||||
|
||||
/// <summary>A consist's label in a list of them, here and on the depot page.</summary>
|
||||
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)
|
||||
{
|
||||
|
||||
188
StarterNG/Views/TrainsetRow.cs
Normal file
188
StarterNG/Views/TrainsetRow.cs
Normal file
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
internal static class TrainsetRow
|
||||
{
|
||||
/// <summary>The height the minis are made at: shown at it they stay sharp.</summary>
|
||||
private const int MiniHeight = 30;
|
||||
|
||||
private static readonly MiniTextures Minis = new();
|
||||
|
||||
/// <summary>Room under the captions for the scroll bar of a consist wider than the list.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The vehicles' minis with their captions, scrolling sideways when wider than the room
|
||||
/// they get - here and for the warehouse's consists.
|
||||
/// </summary>
|
||||
public static Control ScrollingStrip(IReadOnlyList<Dynamic> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The minis, one next to the other with nothing between, as the vehicles stand coupled.
|
||||
/// </summary>
|
||||
private static Control Strip(IReadOnlyList<Dynamic> 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
|
||||
}
|
||||
};
|
||||
|
||||
/// <summary>A vehicle leading the consist under its own name, the rest under their class - as the composition text has them.</summary>
|
||||
private static string Caption(Dynamic v, int place) =>
|
||||
TrainsetDisplay.VehicleLabel(v, head: place == 0 && TrainsetDisplay.IsPowered(v));
|
||||
|
||||
/// <summary>The mini the consist strip shows for the vehicle.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user