1- using MicroMod ;
1+ using BepInEx . Logging ;
2+ using MicroMod ;
23using UnityEngine ;
34using UnityEngine . UIElements ;
45
@@ -20,6 +21,9 @@ public class StageInfoOABController : MonoBehaviour
2021 public VisualElement TotalVacDeltaVContainer { get ; set ; }
2122 public VisualElement TotalBurnTimeContainer { get ; set ; }
2223
24+ private static ManualLogSource _logger = BepInEx . Logging . Logger . CreateLogSource ( "MicroEngineer.StageInfoOABController" ) ;
25+ private bool _lockUiRefresh ;
26+
2327 public StageInfoOABController ( )
2428 { }
2529
@@ -92,9 +96,26 @@ private void BuildStages(List<DeltaVStageInfo_OAB> stages)
9296 MicroCelestialBodies . Instance . Bodies . Select ( b => b . DisplayName ) . ToList ( ) ,
9397 stage . CelestialBody . Name
9498 ) ;
99+
95100 var celestialBodyDropdown = control . Q < DropdownField > ( "body-dropdown" ) ;
101+ celestialBodyDropdown . RegisterCallback < MouseDownEvent > ( evt =>
102+ {
103+ // When user clicks on the celestialBodyDropdown control we lock stage info refresh from happening
104+ // and unlock it again when user selects something from the dropdown.
105+ // We do this because if VesselDeltaVCalculationMessage is triggered between the user first clicking
106+ // on the control and selection something from it, the event will cause the UI to refresh (destroy
107+ // and rebuild the controls) and thus the original dropdown control will no longer exist and
108+ // celestialBody will not be selectable.
109+ _lockUiRefresh = true ;
110+ } ) ;
96111 // Update entry's CelestialBody at the same index as the stage
97- celestialBodyDropdown . RegisterValueChangedCallback ( evt => StageEntry . UpdateCelestialBodyAtIndex ( stage . Index , celestialBodyDropdown . value ) ) ;
112+ celestialBodyDropdown . RegisterValueChangedCallback ( evt =>
113+ {
114+ StageEntry . UpdateCelestialBodyAtIndex ( stage . Index , celestialBodyDropdown . value ) ;
115+ _lockUiRefresh = false ;
116+ StageEntry . RefreshData ( ) ;
117+ } ) ;
118+
98119 Body . Add ( control ) ;
99120 }
100121
@@ -108,6 +129,8 @@ private void BuildStages(List<DeltaVStageInfo_OAB> stages)
108129
109130 private void HandleStageInfoChanged ( List < DeltaVStageInfo_OAB > stages )
110131 {
132+ if ( _lockUiRefresh ) return ;
133+
111134 Body . Clear ( ) ;
112135 BuildStages ( stages ) ;
113136 }
0 commit comments