From 4da492861f7a8c8ca4554d3c8b23f9dab4ef3c0b Mon Sep 17 00:00:00 2001 From: DRVeyl Date: Sat, 27 Mar 2021 12:25:44 -0400 Subject: [PATCH] Handle PAW Field Caching Fixes #255 KSP after 1.8 is cacheing more PAW fields. In 1.10, the created event buttons hang around and don't get cleared by PAW creation, only by refresh. Catch the PAW show event and request a refresh if one is requested. Refactor UpdateUsedBy a little. Every tank PartModule iterating over every vessel PartModule to build the UsedBy mapping may need analysis --- Source/Tanks/ModuleFuelTanks.cs | 50 ++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Source/Tanks/ModuleFuelTanks.cs b/Source/Tanks/ModuleFuelTanks.cs index 357233d9..b577e676 100644 --- a/Source/Tanks/ModuleFuelTanks.cs +++ b/Source/Tanks/ModuleFuelTanks.cs @@ -35,6 +35,7 @@ public UnmanagedResource(string name, double amount, double maxAmount) bool compatible = true; bool started; + private bool windowDirty = false; public bool fueledByLaunchClamp = false; @@ -385,6 +386,7 @@ public override void OnStart(StartState state) GameEvents.onPartRemove.Add(onPartRemove); GameEvents.onEditorShipModified.Add(onEditorShipModified); GameEvents.onPartActionUIDismiss.Add(OnPartActionGuiDismiss); + GameEvents.onPartActionUIShown.Add(OnPartActionUIShown); if (part.symmetryCounterparts.Count > 0) { UpdateTankType(false); @@ -409,6 +411,7 @@ void OnDestroy () GameEvents.onPartRemove.Remove (onPartRemove); GameEvents.onEditorShipModified.Remove (onEditorShipModified); GameEvents.onPartActionUIDismiss.Remove (OnPartActionGuiDismiss); + GameEvents.onPartActionUIShown.Remove(OnPartActionUIShown); TankWindow.HideGUI(); } @@ -485,6 +488,16 @@ private void onPartRemove (GameEvents.HostTargetAction hostTarget) } } + private void OnPartActionUIShown(UIPartActionWindow window, Part p) + { + if (p == part && windowDirty) + { + windowDirty = false; // Un-flag state + window.displayDirty = true; // Signal refresh + //MonoUtilities.RefreshPartContextWindow(part); + } + } + private void OnPartActionGuiDismiss(Part p) { if (p == part) @@ -1030,16 +1043,11 @@ internal void MarkWindowDirty () if (!started) { return; } - UIPartActionWindow action_window; - if (UIPartActionController.Instance == null) { - // no controller means no window to mark dirty - return; - } - action_window = UIPartActionController.Instance.GetItem(part); - if (action_window == null) { - return; - } - action_window.displayDirty = true; + if (UIPartActionController.Instance?.GetItem(part) is UIPartActionWindow paw) + paw.displayDirty = true; + else + windowDirty = true; // The PAW isn't open, so request refresh later + //MonoUtilities.RefreshPartContextWindow(part); } @@ -1078,27 +1086,19 @@ public void UpdateUsedBy () parts = vessel.parts; else parts = new List(); - FuelInfo f; - string title; - PartModule m; - for(int i = 0; i < parts.Count; ++i) + foreach(Part p in parts) { - title = parts[i].partInfo.title; - for(int j = 0; j < parts[i].Modules.Count; ++j) + string title = p.partInfo.title; + for(int j = 0; j < p.Modules.Count; ++j) { - m = parts[i].Modules[j]; + FuelInfo f = null; + PartModule m = p.Modules[j]; if (m is ModuleEngines) - { f = new FuelInfo((m as ModuleEngines).propellants, this, title); - if(f.ratioFactor > 0d) - UpdateFuelInfo(f, title); - } else if (m is ModuleRCS) - { f = new FuelInfo((m as ModuleRCS).propellants, this, title); - if (f.ratioFactor > 0d) - UpdateFuelInfo(f, title); - } + if (f?.ratioFactor > 0d) + UpdateFuelInfo(f, title); } }