using System; using StarterNG.Application; using System.Collections.Generic; using System.Linq; using Avalonia; using Avalonia.Automation; using Avalonia.Automation.Peers; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Layout; using Avalonia.Media; using Avalonia.VisualTree; using Material.Icons; using Material.Icons.Avalonia; using StarterNG.Classes; using StarterNG.Domain; using StarterNG.Domain.Vehicles; using StarterNG.Controls; namespace StarterNG.Views; public sealed class VehicleCards { private readonly Panel consistStack; private readonly VehicleCatalog _db; private readonly Consist _consist; private readonly Cargo _cargo; private readonly MiniTextures _minis; private readonly Cursor _hand; private readonly Action _redraw; private readonly Action _refreshDetails; private readonly Action _selectInBrowser; private readonly Action _armDrag; private readonly List<(Border Frame, ConsistItem Item, Dynamic Car)> _memberChrome = new(); /// Puts copies of the vehicle on a card right after it, the given number of times. public Action? AddCopies { get; set; } /// The livery picked in the vehicle browser, offered as the card's replacement. public Func? BrowserSelection { get; set; } public Action? ReplaceWith { get; set; } private readonly List<(Button Badge, ConsistItem Item)> _driverChrome = new(); public Dynamic? PressedCar { get; set; } public VehicleCards(Panel consistStack, VehicleCatalog db, Consist consist, Cargo cargo, MiniTextures minis, Cursor hand, Action redraw, Action refreshDetails, Action selectInBrowser, Action armDrag) { this.consistStack = consistStack; _db = db; _consist = consist; _cargo = cargo; _minis = minis; _hand = hand; _redraw = redraw; _refreshDetails = refreshDetails; _selectInBrowser = selectInBrowser; _armDrag = armDrag; } public void Forget() { _memberChrome.Clear(); _driverChrome.Clear(); } public Control BuildCard(ConsistItem item, int index) { bool selected = ReferenceEquals(_consist.Selected, item); var minis = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 1, HorizontalAlignment = HorizontalAlignment.Center }; var cars = item.Flipped ? item.Cars.AsEnumerable().Reverse() : item.Cars; foreach (var car in cars) { var bmp = _minis.Get(car.MiniName, VehicleCardStyle.ThumbHeight, strict: true) ?? _minis.Get(car.SkinFile, VehicleCardStyle.ThumbHeight, strict: true) ?? _minis.Get(car.MiniName, VehicleCardStyle.ThumbHeight); if (bmp != null) { var img = MiniTextures.Sharp(new Image { Source = bmp, Height = VehicleCardStyle.ThumbHeight, Stretch = Stretch.Uniform }); if (item.Flipped) { img.RenderTransform = new ScaleTransform(-1, 1); img.RenderTransformOrigin = RelativePoint.Center; } minis.Children.Add(MemberFrame(img, item, car, CardCaption(item, car, index))); } else { minis.Children.Add(MemberFrame( MiniTextures.MissingVisual(car.SkinFile, VehicleCardStyle.ThumbHeight), item, car, CardCaption(item, car, index))); } } var controls = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 3, HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(0, 1, 0, 2) }; // Who sits in the cab - the player, the simulator, a passenger or nobody - is for the advanced mode. if (AppServices.Current.Settings.AdvancedSettings) controls.Children.Add(DriverButton(item)); controls.Children.Add(FlipButton(item)); if (item.Grouped && item.Cars.Count > 1) controls.Children.Add(SmallButton(MaterialIconKind.CallSplit, App.Loc["TipSplit"], () => _consist.Split(item))); else if (_consist.CanJoin(item, index)) controls.Children.Add(SmallButton(MaterialIconKind.Link, App.Loc["TipJoin"], () => _consist.Join(item))); var remove = SmallButton(MaterialIconKind.Close, App.Loc["RemoveSelectedVehicle"], () => _consist.Remove(item)); remove.Foreground = new SolidColorBrush(Color.Parse("#E05252")); remove.FontWeight = FontWeight.Bold; controls.Children.Add(remove); var inner = new StackPanel { Spacing = 3 }; inner.Children.Add(minis); inner.Children.Add(controls); var card = new Border { Margin = new Thickness(3, 2, 3, 6), Padding = new Thickness(selected ? 4 : 5), CornerRadius = new CornerRadius(6), BorderThickness = new Thickness(selected ? 2 : 1), BorderBrush = selected ? VehicleCardStyle.BorderSelected : VehicleCardStyle.Border, Background = selected ? VehicleCardStyle.BackgroundSelected : Brushes.Transparent, Cursor = _hand, Child = inner, Tag = item, // Focusable so the strip can be reached with Tab and walked with the // arrow keys; the focus ring itself comes from Carbon.axaml. Focusable = true }; card.Classes.Add("ConsistCard"); // The card shows pictures only; without a name a screen reader said nothing on it. AutomationProperties.SetName(card, CardName(item, index)); AutomationProperties.SetControlTypeOverride(card, AutomationControlType.ListItem); AutomationProperties.SetIsControlElementOverride(card, true); card.GotFocus += (_, _) => Select(item, null); card.KeyDown += (_, e) => OnCardKey(card, item, e); card.PointerPressed += (_, e) => { // The menu is about the card it opened on, so that card is selected - // without moving the browser, whose pick the menu offers as a replacement. if (e.GetCurrentPoint(card).Properties.IsRightButtonPressed) { Select(item, PressedCar, syncBrowser: false); PressedCar = null; return; } if (!e.GetCurrentPoint(card).Properties.IsLeftButtonPressed) return; if (e.Source is Control src && src.GetVisualAncestors().OfType