Add a vehicle several times from the browser's menu

The count is asked for in a flyout; the copies go in one after another, each
staffed as a single add would be, with one redraw.
This commit is contained in:
maj00r
2026-10-04 14:03:52 +02:00
parent bbc163b204
commit a9f4a09cf5
7 changed files with 80 additions and 17 deletions

View File

@@ -71,7 +71,10 @@ public partial class Depot : UserControl
_browser = new VehicleBrowser(categoryCombo, classCombo, vehicleListBox, searchBox,
hideArchivalCheck, miniPreview, addVehicleButton, _db, _minis,
() => TextureBaseButton_OnClick(null, null!));
() => TextureBaseButton_OnClick(null, null!))
{
AddMany = (texture, count) => AddTexture(texture, count)
};
_drag = new ConsistDragging(consistStack, consistScroll, consistOverlay,
consistDropTarget, vehicleListBox, miniPreviewPanel,
@@ -580,7 +583,7 @@ public partial class Depot : UserControl
RebuildConsist();
}
private void AddTexture(VehicleTexture texture)
private void AddTexture(VehicleTexture texture, int count = 1)
{
int at = _consist.Count;
if (_consist.Selected != null)
@@ -588,26 +591,33 @@ public partial class Depot : UserControl
int si = _consist.IndexOf(_consist.Selected);
if (si >= 0) at = si + 1;
}
InsertTextureAt(texture, at);
InsertTexturesAt(texture, at, count);
}
private void InsertTextureAt(VehicleTexture texture, int at)
private void InsertTextureAt(VehicleTexture texture, int at) => InsertTexturesAt(texture, at, 1);
/// <summary>The vehicle count times in a row from at - each staffed as added - and one redraw.</summary>
private void InsertTexturesAt(VehicleTexture texture, int at, int count)
{
var set = _db.ResolveSet(texture);
var unit = set ?? new List<VehicleTexture> { texture };
var item = new ConsistItem
{
Cars = unit.Select(NewVehicle).ToList(),
Grouped = unit.Count > 1,
Driver = eDriverType.Nobody,
Flipped = false
};
at = Math.Clamp(at, 0, _consist.Count);
_consist.Staff(item, at);
_consist.Insert(at, item);
_consist.Selected = item;
for (int n = 0; n < count; n++, at++)
{
var item = new ConsistItem
{
Cars = unit.Select(NewVehicle).ToList(),
Grouped = unit.Count > 1,
Driver = eDriverType.Nobody,
Flipped = false
};
_consist.Staff(item, at);
_consist.Insert(at, item);
_consist.Selected = item;
}
_consist.AutoConnectAll();
RebuildConsist();
}

View File

@@ -51,6 +51,11 @@ public sealed class VehicleBrowser
public Action<VehicleTexture>? TextureSelected { get; set; }
/// <summary>Adds the vehicle to the consist the given number of times.</summary>
public Action<VehicleTexture, int>? AddMany { get; set; }
private const int MaxAddMany = 100;
private bool _suppress;
private bool _syncingCombos;
@@ -358,6 +363,38 @@ public sealed class VehicleBrowser
}
}
private void ShowAddManyFlyout(Control anchor, VehicleTexture texture)
{
var count = new NumericUpDown
{
Minimum = 1, Maximum = MaxAddMany, Increment = 1, FormatString = "0", Value = 2, MinWidth = 120
};
var add = new Button { Content = App.Loc["AddVehicle"], HorizontalAlignment = HorizontalAlignment.Right };
add.Classes.Add("Flat");
add.Classes.Add("Accent");
var panel = new StackPanel { Spacing = 6, Margin = new Thickness(8), MinWidth = 160 };
panel.Children.Add(new TextBlock { Text = App.Loc["AddManyCount"], FontWeight = FontWeight.Bold, FontSize = 12 });
panel.Children.Add(count);
panel.Children.Add(add);
var flyout = new Flyout { Content = panel, Placement = PlacementMode.Right };
void Commit()
{
flyout.Hide();
AddMany?.Invoke(texture, Math.Clamp((int)(count.Value ?? 1), 1, MaxAddMany));
}
add.Click += (_, _) => Commit();
count.KeyDown += (_, e) =>
{
if (e.Key != Key.Enter) return;
e.Handled = true;
Commit();
};
flyout.ShowAt(anchor);
count.Focus();
}
private void List_OnContextRequested(object? sender, ContextRequestedEventArgs e)
{
var hit = e.Source as Control;
@@ -367,13 +404,19 @@ public sealed class VehicleBrowser
if (hit is not ListBoxItem { Tag: VehicleTexture texture })
return;
BuildBrowserMenu(texture).Open(hit);
BuildBrowserMenu(texture, hit).Open(hit);
e.Handled = true;
}
private ContextMenu BuildBrowserMenu(VehicleTexture texture)
private ContextMenu BuildBrowserMenu(VehicleTexture texture, Control anchor)
{
var menu = new ContextMenu();
var addMany = new MenuItem { Header = App.Loc["AddMany"], IsEnabled = AddMany is not null };
addMany.Click += (_, _) => ShowAddManyFlyout(anchor, texture);
menu.Items.Add(addMany);
menu.Items.Add(new Separator());
var copy = new MenuItem { Header = App.Loc["CopyTextureName"] };
copy.Click += async (_, _) =>
{

View File

@@ -281,6 +281,8 @@
<String key="TipJoin">Spojit toto vozidlo s následujícím do jedné jednotky</String>
<String key="TipReorder">Přetáhněte pro změnu pořadí</String>
<String key="AddVehicle">Přidat</String>
<String key="AddMany">Přidat vícekrát…</String>
<String key="AddManyCount">Kolikrát vozidlo přidat</String>
<String key="TipAddVehicle">Přidat toto vozidlo do soupravy</String>
<String key="TipAddUnit">Přidat celou jednotku do soupravy</String>

View File

@@ -281,6 +281,8 @@
<String key="TipJoin">Couple this vehicle with the next one into a single unit</String>
<String key="TipReorder">Drag to reorder</String>
<String key="AddVehicle">Add</String>
<String key="AddMany">Add several…</String>
<String key="AddManyCount">How many times to add it</String>
<String key="TipAddVehicle">Add this vehicle to the consist</String>
<String key="TipAddUnit">Add the whole unit to the consist</String>

View File

@@ -281,6 +281,8 @@
<String key="TipJoin">A jármű összekapcsolása a következővel egy egységgé</String>
<String key="TipReorder">Húzza az átrendezéshez</String>
<String key="AddVehicle">Hozzáadás</String>
<String key="AddMany">Hozzáadás többször…</String>
<String key="AddManyCount">Hányszor add hozzá a járművet</String>
<String key="TipAddVehicle">Jármű hozzáadása a szerelvényhez</String>
<String key="TipAddUnit">A teljes egység hozzáadása a szerelvényhez</String>

View File

@@ -281,6 +281,8 @@
<String key="TipJoin">Połącz ten pojazd z następnym w jeden zespół</String>
<String key="TipReorder">Przeciągnij, aby zmienić kolejność</String>
<String key="AddVehicle">Dodaj</String>
<String key="AddMany">Dodaj wiele…</String>
<String key="AddManyCount">Ile razy dodać pojazd</String>
<String key="TipAddVehicle">Dodaj ten pojazd do składu</String>
<String key="TipAddUnit">Dodaj cały zespół do składu</String>

View File

@@ -281,6 +281,8 @@
<String key="TipJoin">Соединить этот вагон со следующим в один состав</String>
<String key="TipReorder">Перетащите для изменения порядка</String>
<String key="AddVehicle">Добавить</String>
<String key="AddMany">Добавить несколько…</String>
<String key="AddManyCount">Сколько раз добавить единицу</String>
<String key="TipAddVehicle">Добавить эту единицу в состав</String>
<String key="TipAddUnit">Добавить весь сцеп в состав</String>