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

@@ -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 (_, _) =>
{