Skip to content

Commit

Permalink
Don't run prepost actions in flight scene for crew update dialogs. Ad…
Browse files Browse the repository at this point in the history
…d NonFlight versions of PrePostActions and HideGUIsWhilePopup. Make those methods return the dialog so it can still be captured. Close #2292
  • Loading branch information
NathanKell committed Nov 5, 2023
1 parent dc64f93 commit 230e5c3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Source/RP0/Crew/CrewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,9 @@ private void VesselRecoveryProcessing(ProtoVessel v, MissionRecoveryDialog mrDia
sb.ToString(),
KSP.Localization.Localizer.GetStringByTag("#autoLOC_190905"),
true,
HighLogic.UISkin)
.PrePostActions(ControlTypes.KSC_ALL | ControlTypes.UI_MAIN, "RP0CrewUpdate", OnDialogSpawn, OnDialogDismiss);
HighLogic.UISkin,
!HighLogic.LoadedSceneIsFlight)
.PrePostActionsNonFlight(ControlTypes.KSC_ALL | ControlTypes.UI_MAIN, "RP0CrewUpdate", OnDialogSpawn, OnDialogDismiss);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/RP0/Crew/TrainingCourse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void CompleteCourse()
KSP.Localization.Localizer.GetStringByTag("#autoLOC_190905"),
true,
HighLogic.UISkin,
!HighLogic.LoadedSceneIsFlight).HideGUIsWhilePopup();
!HighLogic.LoadedSceneIsFlight).HideGUIsWhilePopupNonFlight();
}
}

Expand Down
34 changes: 30 additions & 4 deletions Source/RP0/Utilities/KSPUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public static List<Type> GetAllLoadedTypes<T>(bool instantiableOnly = true)
return list;
}

/// <summary>
/// Like PrePostActions, but does nothing in the Flight scene
/// </summary>
/// <param name="dialog"></param>
/// <param name="lockType"></param>
/// <param name="lockName"></param>
/// <param name="onCreateAction"></param>
/// <param name="onDestroyAction"></param>
public static PopupDialog PrePostActionsNonFlight(this PopupDialog dialog, ControlTypes lockType = ControlTypes.None, string lockName = null, Callback onCreateAction = null, Callback onDestroyAction = null)
{
if (HighLogic.LoadedSceneIsFlight)
return dialog;

return PrePostActions(dialog, lockType, lockName, onCreateAction, onDestroyAction);
}

/// <summary>
/// Adds a way for a PopupDialog to perform actions on spawn/despawn, like locking input for a true modal.
/// </summary>
Expand All @@ -54,10 +70,10 @@ public static List<Type> GetAllLoadedTypes<T>(bool instantiableOnly = true)
/// <param name="lockName">optional (will use default if not specified and locking controls)</param>
/// <param name="onCreateAction">optional: runs on dialog spawn</param>
/// <param name="onDestroyAction">optional: runs when dialog is destroyed</param>
public static void PrePostActions(this PopupDialog dialog, ControlTypes lockType = ControlTypes.None, string lockName = null, Callback onCreateAction = null, Callback onDestroyAction = null)
public static PopupDialog PrePostActions(this PopupDialog dialog, ControlTypes lockType = ControlTypes.None, string lockName = null, Callback onCreateAction = null, Callback onDestroyAction = null)
{
if (dialog == null)
return;
return null;

if (onCreateAction != null)
onCreateAction();
Expand All @@ -69,6 +85,8 @@ public static void PrePostActions(this PopupDialog dialog, ControlTypes lockType
InputLockManager.SetControlLock(lockType, lockName);
}
dialog.gameObject.AddComponent<LockRemover>().Setup(lockName, onDestroyAction);

return dialog;
}

public class LockRemover : MonoBehaviour
Expand Down Expand Up @@ -130,9 +148,17 @@ public void OnDestroy()
}
}

public static void HideGUIsWhilePopup(this PopupDialog dialog)
public static PopupDialog HideGUIsWhilePopupNonFlight(this PopupDialog dialog)
{
if (HighLogic.LoadedSceneIsFlight)
return dialog;

return HideGUIsWhilePopup(dialog);
}

public static PopupDialog HideGUIsWhilePopup(this PopupDialog dialog)
{
PrePostActions(dialog, ControlTypes.KSC_ALL | ControlTypes.UI_MAIN | ControlTypes.EDITOR_SOFT_LOCK, "RP0GenericPopupDialogLock", OnDialogSpawn, OnDialogDismiss);
return PrePostActions(dialog, ControlTypes.KSC_ALL | ControlTypes.UI_MAIN | ControlTypes.EDITOR_SOFT_LOCK, "RP0GenericPopupDialogLock", OnDialogSpawn, OnDialogDismiss);
}

private static void OnDialogSpawn()
Expand Down

0 comments on commit 230e5c3

Please sign in to comment.