From ec21effd303d1f6158967be02b96724976c4bc18 Mon Sep 17 00:00:00 2001 From: RMROC451 Date: Sat, 13 Jul 2024 13:09:42 -0500 Subject: [PATCH] Adding consist buttons from inspector to new context menu, and squashing bug that would occur if inspector open but tags weren't active, breaking the ui panel builder reload detection for handbrake/couple/air changes. final change of Version 0.1.7 --- Directory.Build.targets | 2 +- .../CarInspector_PopulateCarPanel_Patch.cs | 7 ++- ...CarPickable_HandleShowContextMenu_Patch.cs | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 TweaksAndThings/Patches/CarPickable_HandleShowContextMenu_Patch.cs diff --git a/Directory.Build.targets b/Directory.Build.targets index 6546fe1..f80f66e 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -12,7 +12,7 @@ 0 1 - 6 + 7 $(MajorVersion).$(MinorVersion).$(PatchVersion) $(AssemblyVersion) $(AssemblyVersion) diff --git a/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs b/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs index 7f4fd68..78cbc07 100644 --- a/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs +++ b/TweaksAndThings/Patches/CarInspector_PopulateCarPanel_Patch.cs @@ -8,12 +8,14 @@ using Network; using Railloader; using RMROC451.TweaksAndThings.Enums; using RMROC451.TweaksAndThings.Extensions; +using RollingStock; using Serilog; using System; using System.Collections.Generic; using System.Linq; using UI.Builder; using UI.CarInspector; +using UI.ContextMenu; using UI.Tags; using static Model.Car; @@ -70,7 +72,7 @@ internal class CarInspector_PopulateCarPanel_Patch private static UIPanelBuilder AddCarConsistRebuildObservers(UIPanelBuilder builder, IEnumerable consist) { TagController tagController = UnityEngine.Object.FindFirstObjectByType(); - foreach (Model.Car car in consist.Where(c => c.TagCallout != null)) + foreach (Model.Car car in consist.Where(c => c.Archetype != Model.Definition.CarArchetype.Tender)) { builder = AddObserver(builder, car, PropertyChange.KeyForControl(PropertyChange.Control.Handbrake), tagController); foreach (LogicalEnd logicalEnd in ends) @@ -93,7 +95,8 @@ internal class CarInspector_PopulateCarPanel_Patch { try { - tagController.UpdateTag(car, car.TagCallout, OpsController.Shared); + if (car.TagCallout != null) tagController.UpdateTag(car, car.TagCallout, OpsController.Shared); + if (ContextMenu.IsShown && ContextMenu.Shared.centerLabel.text == car.DisplayName) CarPickable.HandleShowContextMenu(car); builder.Rebuild(); } catch(Exception ex) diff --git a/TweaksAndThings/Patches/CarPickable_HandleShowContextMenu_Patch.cs b/TweaksAndThings/Patches/CarPickable_HandleShowContextMenu_Patch.cs new file mode 100644 index 0000000..723f828 --- /dev/null +++ b/TweaksAndThings/Patches/CarPickable_HandleShowContextMenu_Patch.cs @@ -0,0 +1,51 @@ +using HarmonyLib; +using Model; +using Railloader; +using RMROC451.TweaksAndThings.Enums; +using RMROC451.TweaksAndThings.Extensions; +using RollingStock; +using System.Linq; +using UI; +using UI.ContextMenu; +using static Model.Car; + +namespace RMROC451.TweaksAndThings.Patches; + +[HarmonyPatch(typeof(CarPickable))] +[HarmonyPatch(nameof(CarPickable.HandleShowContextMenu), typeof(Car))] +[HarmonyPatchCategory("RMROC451TweaksAndThings")] +internal class CarPickable_HandleShowContextMenu_Patch +{ + private static void Postfix(Car car) + { + TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase.Shared; + if (!tweaksAndThings.IsEnabled) return; + + bool buttonsHaveCost = tweaksAndThings?.settings?.EndGearHelpersRequirePayment ?? false; + ContextMenu shared = ContextMenu.Shared; + var consist = car.EnumerateCoupled(LogicalEnd.A); + shared.AddButton(ContextMenuQuadrant.Unused2, $"{(consist.Any(c => c.HandbrakeApplied()) ? "Release " : "Set ")} Consist", SpriteName.Handbrake, delegate + { + CarInspector_PopulateCarPanel_Patch.MrocConsistHelper(car, MrocHelperType.Handbrake, buttonsHaveCost); + }); + + if (consist.Any(c => c.EndAirSystemIssue())) + { + shared.AddButton(ContextMenuQuadrant.Unused2, $"Air Up Consist", SpriteName.Select, delegate + { + CarInspector_PopulateCarPanel_Patch.MrocConsistHelper(car, MrocHelperType.GladhandAndAnglecock, buttonsHaveCost); + }); + } + + if (consist.Any(c => c.SupportsBleed())) + { + shared.AddButton(ContextMenuQuadrant.Unused2, $"Bleed Consist", SpriteName.Bleed, delegate + { + CarInspector_PopulateCarPanel_Patch.MrocConsistHelper(car, MrocHelperType.BleedAirSystem, buttonsHaveCost); + }); + } + + shared.BuildItemAngles(); + shared.StartCoroutine(shared.AnimateButtonsShown()); + } +}