diff --git a/Aetheria.State.Verify/Program.cs b/Aetheria.State.Verify/Program.cs index da57a657b..54c8ad507 100644 --- a/Aetheria.State.Verify/Program.cs +++ b/Aetheria.State.Verify/Program.cs @@ -4654,11 +4654,12 @@ static void RequireTradeFilterAndRowActionsUseEveSurface(string root) "AetheriaRuntimeTradeInteractionSurfaceBuilder.BuildFilter(ProjectTradeFilterSurfaceState(", "ProjectTradeFilterSurfaceState(", "RenderRowActionSurface(", - "BuildRowActionSurfaceCommands(", "HandleRowActionSurfaceCommand(", "AetheriaEveUnitySurfaceHost.Hide(_rowActionSurfaceDocument)", - "AetheriaRuntimeTradeInteractionSurfaceBuilder.BuildRowActions(ProjectTradeRowActionSurfaceState(", - "ProjectTradeRowActionSurfaceState(", + "AetheriaRuntimeTradeInteractionSurfaceBuilder.ProjectRowActions(", + "AetheriaRuntimeTradeInteractionSurfaceBuilder.BuildRowActions(_rowActionSurfaceProjection.State)", + "_rowActionSurfaceProjection?.TryResolve(command.Command, out var selection) == true", + "new AetheriaRuntimeTradeRowActionOption(index, action.Label)", "ShowBuyQuantityDialog(", "AetheriaRuntimeTradeInteractionSurfaceCommands.TryReadFilter(request, out var command)", "AetheriaRuntimeTradeInteractionSurfaceCommands.TryReadRowAction(request, out var command)", @@ -4699,7 +4700,10 @@ static void RequireTradeFilterAndRowActionsUseEveSurface(string root) "string.Equals(request.Command, AetheriaRuntimeTradeInteractionSurfaceBuilder.CloseFilter", "string.Equals(request.Command, AetheriaRuntimeTradeInteractionSurfaceBuilder.CloseRowAction", "_filterSurfaceCommands.TryGetValue(request.Command", - "_rowActionSurfaceCommands.TryGetValue(request.Command" + "_rowActionSurfaceCommands.TryGetValue(request.Command", + "private readonly Dictionary _rowActionSurfaceCommands", + "BuildRowActionSurfaceCommands(", + "ProjectTradeRowActionSurfaceState(" }; var hits = forbiddenSymbols @@ -4727,10 +4731,15 @@ static void RequireTradeFilterAndRowActionsUseEveSurface(string root) "public static bool TryReadRowAction(", "AetheriaRuntimeTradeFilterSurfaceState", "AetheriaRuntimeTradeRowActionSurfaceState", + "AetheriaRuntimeTradeRowActionOption", + "AetheriaRuntimeTradeRowActionSelection", + "AetheriaRuntimeTradeRowActionSurfaceProjection", "AetheriaRuntimeTradeSurfaceGroup", "AetheriaRuntimeTradeSurfaceOption", "public static string HardpointFilterCommand(", "public static string RowActionCommand(", + "public static AetheriaRuntimeTradeRowActionSurfaceProjection ProjectRowActions(", + "public bool TryResolve(string command, out AetheriaRuntimeTradeRowActionSelection selection)", "public static AetheriaRuntimeSurfaceDocument BuildFilter(", "public static AetheriaRuntimeSurfaceDocument BuildRowActions(", "providerKind: \"trade.menu\"" diff --git a/Assets/Scripts/UI/Menu/TradeMenu.cs b/Assets/Scripts/UI/Menu/TradeMenu.cs index a9f99562f..9654254d1 100644 --- a/Assets/Scripts/UI/Menu/TradeMenu.cs +++ b/Assets/Scripts/UI/Menu/TradeMenu.cs @@ -30,7 +30,6 @@ public class TradeMenu : MonoBehaviour private (ItemFilter filter, CompoundCommodityCategory type) _compoundCommodityFilter; private List _behaviorFilters = new List(); private readonly Dictionary _filterSurfaceCommands = new Dictionary(StringComparer.Ordinal); - private readonly Dictionary _rowActionSurfaceCommands = new Dictionary(StringComparer.Ordinal); private readonly Dictionary _cargoSelectionCommands = new Dictionary(StringComparer.Ordinal); private UIDocument _cargoSelectorSurfaceDocument; @@ -41,7 +40,8 @@ public class TradeMenu : MonoBehaviour private readonly AetheriaEveUnitySurfaceChrome _filterSurfaceChrome = PanelChrome(420f, 520f, Align.FlexStart); private readonly AetheriaEveUnitySurfaceChrome _rowActionSurfaceChrome = PanelChrome(320f, 360f, Align.FlexStart); private readonly AetheriaEveUnitySurfaceChrome _tradeItemSurfaceChrome = PanelChrome(420f, 520f, Align.FlexStart); - private string _rowActionTitle = "Trade Actions"; + private Action[] _rowActionCallbacks = Array.Empty(); + private AetheriaRuntimeTradeRowActionSurfaceProjection _rowActionSurfaceProjection; public EquippedCargoBay Inventory { get; set; } @@ -794,30 +794,24 @@ public class TradeMenu : MonoBehaviour private void RenderRowActionSurface(string title, params (string Label, Action Action)[] actions) { - _rowActionTitle = title; - BuildRowActionSurfaceCommands(actions); + _rowActionCallbacks = actions + .Select(action => action.Action) + .ToArray(); + _rowActionSurfaceProjection = AetheriaRuntimeTradeInteractionSurfaceBuilder.ProjectRowActions( + title, + actions.Select((action, index) => new AetheriaRuntimeTradeRowActionOption(index, action.Label)), + DateTime.UtcNow.ToString("O")); _rowActionSurfaceDocument = AetheriaEveUnitySurfaceHost.RenderRuntime( transform, _rowActionSurfaceDocument, "Aetheria Trade Row Action Surface", - AetheriaRuntimeTradeInteractionSurfaceBuilder.BuildRowActions(ProjectTradeRowActionSurfaceState()), + AetheriaRuntimeTradeInteractionSurfaceBuilder.BuildRowActions(_rowActionSurfaceProjection.State), HandleRowActionSurfaceCommand, _rowActionSurfaceChrome, sortingOrder: 1002); } - private void BuildRowActionSurfaceCommands(IEnumerable<(string Label, Action Action)> actions) - { - _rowActionSurfaceCommands.Clear(); - - foreach (var actionEntry in actions.Select((entry, index) => (entry, index))) - { - var command = AetheriaRuntimeTradeInteractionSurfaceBuilder.RowActionCommand(actionEntry.index); - _rowActionSurfaceCommands[command] = actionEntry.entry.Action; - } - } - private void HandleRowActionSurfaceCommand(EveSurfaceCommandRequest request) { if (!AetheriaRuntimeTradeInteractionSurfaceCommands.TryReadRowAction(request, out var command)) @@ -833,9 +827,11 @@ public class TradeMenu : MonoBehaviour } if (command.Kind == AetheriaRuntimeTradeInteractionCommandKind.Select && - _rowActionSurfaceCommands.TryGetValue(command.Command, out var action)) + _rowActionSurfaceProjection?.TryResolve(command.Command, out var selection) == true && + selection.Index >= 0 && + selection.Index < _rowActionCallbacks.Length) { - action(); + _rowActionCallbacks[selection.Index]?.Invoke(); HideRowActionSurface(); return; } @@ -851,22 +847,6 @@ public class TradeMenu : MonoBehaviour AetheriaEveUnitySurfaceHost.Hide(_rowActionSurfaceDocument); } - private AetheriaRuntimeTradeRowActionSurfaceState ProjectTradeRowActionSurfaceState() - { - var actions = _rowActionSurfaceCommands.Keys - .OrderBy(command => command, StringComparer.Ordinal) - .Select(command => new AetheriaRuntimeTradeSurfaceOption( - command, - command.EndsWith("action_0", StringComparison.Ordinal) ? "Buy Quantity" : command.Split('.').Last(), - command)) - .ToArray(); - - return new AetheriaRuntimeTradeRowActionSurfaceState( - _rowActionTitle, - actions, - DateTime.UtcNow.ToString("O")); - } - private void ApplyHardpointFilter(HardpointType hardpointType) { EnsureHardpointFilter(); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeTradeInteractionSurfaceBuilder.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeTradeInteractionSurfaceBuilder.cs index 4381e0029..8fccc1555 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeTradeInteractionSurfaceBuilder.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeTradeInteractionSurfaceBuilder.cs @@ -70,6 +70,52 @@ namespace GameCult.Aetheria.State.Verse public string UpdatedAtUtc { get; } } + public sealed class AetheriaRuntimeTradeRowActionOption + { + public AetheriaRuntimeTradeRowActionOption(int index, string label) + { + Index = index; + Label = label ?? ""; + } + + public int Index { get; } + public string Label { get; } + } + + public readonly struct AetheriaRuntimeTradeRowActionSelection + { + public AetheriaRuntimeTradeRowActionSelection(string command, int index) + { + Command = command ?? ""; + Index = index; + } + + public string Command { get; } + public int Index { get; } + } + + public sealed class AetheriaRuntimeTradeRowActionSurfaceProjection + { + public AetheriaRuntimeTradeRowActionSurfaceProjection( + AetheriaRuntimeTradeRowActionSurfaceState state, + IReadOnlyDictionary selections) + { + State = state ?? new AetheriaRuntimeTradeRowActionSurfaceState( + "", + Array.Empty(), + ""); + Selections = selections ?? new Dictionary(StringComparer.Ordinal); + } + + public AetheriaRuntimeTradeRowActionSurfaceState State { get; } + public IReadOnlyDictionary Selections { get; } + + public bool TryResolve(string command, out AetheriaRuntimeTradeRowActionSelection selection) + { + return Selections.TryGetValue(command ?? "", out selection); + } + } + public static class AetheriaRuntimeTradeInteractionSurfaceBuilder { public const string FilterSurfaceId = "aetheria.trade.filter_selector"; @@ -112,6 +158,38 @@ namespace GameCult.Aetheria.State.Verse return $"{RowActionSurfaceId}.action_{index}"; } + public static AetheriaRuntimeTradeRowActionSurfaceProjection ProjectRowActions( + string title, + IEnumerable actions, + string updatedAtUtc) + { + var options = new List(); + var selections = new Dictionary(StringComparer.Ordinal); + + foreach (var action in actions ?? Array.Empty()) + { + if (action == null || action.Index < 0) + continue; + + var command = RowActionCommand(action.Index); + var label = string.IsNullOrWhiteSpace(action.Label) + ? $"Action {action.Index + 1}" + : action.Label; + options.Add(new AetheriaRuntimeTradeSurfaceOption( + $"{RowActionSurfaceId}.action_{action.Index}", + label, + command)); + selections[command] = new AetheriaRuntimeTradeRowActionSelection(command, action.Index); + } + + return new AetheriaRuntimeTradeRowActionSurfaceProjection( + new AetheriaRuntimeTradeRowActionSurfaceState( + title, + options, + updatedAtUtc), + selections); + } + public static AetheriaRuntimeSurfaceDocument BuildFilter( AetheriaRuntimeTradeFilterSurfaceState state, long version = 1)