Skip to content

Commit

Permalink
add cleanup methods
Browse files Browse the repository at this point in the history
  • Loading branch information
misternebula committed Mar 22, 2024
1 parent 8837789 commit 4cd8ffa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/OWML.Common/Interfaces/IModBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,33 @@ public interface IModBehaviour
/// </summary>
void SetupTitleMenu(ITitleMenuManager titleManager);

/// <summary>
/// Called when leaving the title menu scene. Put any event unsubscriptions here.
/// </summary>
void CleanupTitleMenu();

/// <summary>
/// Called when the SolarSystem or EyeOfTheUniverse scene has loaded in.
/// Put any code that edits the pause menu here.
/// </summary>
void SetupPauseMenu(IPauseMenuManager pauseManager);

/// <summary>
/// Called when leaving either game scene. Put any event unsubscriptions here.
/// </summary>
void CleanupPauseMenu();

/// <summary>
/// Called when the main menu or game has loaded in.
/// Put any code that edits the options menu here.
/// </summary>
void SetupOptionsMenu(IOptionsMenuManager optionsManager);

/// <summary>
/// Called when leaving the title or either game scene. Put any event unsubscriptions here.
/// </summary>
void CleanupOptionsMenu();

void Init(IModHelper helper);
}
}
17 changes: 17 additions & 0 deletions src/OWML.ModHelper.Menus/NewMenuSystem/MenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ public MenuManager(
var harmonyInstance = harmony.GetValue<Harmony>("_harmony");
harmonyInstance.PatchAll(typeof(Patches));

LoadManager.OnStartSceneLoad += (oldScene, newScene) =>
{
foreach (var mod in ((IMenuManager)this).ModList)
{
if (oldScene == OWScene.TitleScreen)
{
mod.CleanupTitleMenu();
mod.CleanupOptionsMenu();
}
else if (oldScene is OWScene.SolarSystem or OWScene.EyeOfTheUniverse)
{
mod.CleanupPauseMenu();
mod.CleanupOptionsMenu();
}
}
};

LoadManager.OnCompleteSceneLoad += (_, newScene) =>
{
_hasSetupMenusThisScene = false;
Expand Down
15 changes: 6 additions & 9 deletions src/OWML.ModHelper/ModBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ public virtual void Configure(IModConfig config)

public virtual object GetApi() => null;

public virtual void SetupTitleMenu(ITitleMenuManager titleManager)
{
}
public virtual void SetupTitleMenu(ITitleMenuManager titleManager) { }
public virtual void CleanupTitleMenu() { }

public virtual void SetupPauseMenu(IPauseMenuManager pauseManager)
{
}
public virtual void SetupPauseMenu(IPauseMenuManager pauseManager) { }
public virtual void CleanupPauseMenu() { }

public virtual void SetupOptionsMenu(IOptionsMenuManager optionsManager)
{
}
public virtual void SetupOptionsMenu(IOptionsMenuManager optionsManager) { }
public virtual void CleanupOptionsMenu() { }

public IList<IModBehaviour> GetDependants() =>
ModHelper.Interaction.GetDependants(ModHelper.Manifest.UniqueName);
Expand Down
30 changes: 28 additions & 2 deletions src/SampleMods/OWML.LoadCustomAssets/LoadCustomAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public override void SetupTitleMenu(ITitleMenuManager titleManager)

}

public override void CleanupTitleMenu()
{
ModHelper.Console.WriteLine($"CLEANUP TITLE MENU");
}

public override void SetupPauseMenu(IPauseMenuManager pauseManager)
{
var listMenu = pauseManager.MakePauseListMenu("TEST");
Expand All @@ -93,8 +98,24 @@ public override void SetupPauseMenu(IPauseMenuManager pauseManager)
var button2 = pauseManager.MakeSimpleButton("2", 1, true, listMenu);
var button3 = pauseManager.MakeSimpleButton("3", 2, true, listMenu);

pauseManager.PauseMenuOpened += () => ModHelper.Console.WriteLine($"PAUSE MENU OPENED!", MessageType.Success);
pauseManager.PauseMenuClosed += () => ModHelper.Console.WriteLine($"PAUSE MENU CLOSED!", MessageType.Success);
pauseManager.PauseMenuOpened += LogOpened;
pauseManager.PauseMenuClosed += LogClosed;
}

public override void CleanupPauseMenu()
{
ModHelper.MenuHelper.PauseMenuManager.PauseMenuOpened -= LogOpened;
ModHelper.MenuHelper.PauseMenuManager.PauseMenuClosed -= LogClosed;
}

private void LogOpened()
{
ModHelper.Console.WriteLine($"PAUSE MENU OPENED!", MessageType.Success);
}

private void LogClosed()
{
ModHelper.Console.WriteLine($"PAUSE MENU CLOSED!", MessageType.Success);
}

public override void SetupOptionsMenu(IOptionsMenuManager optionsManager)
Expand All @@ -120,6 +141,11 @@ public override void SetupOptionsMenu(IOptionsMenuManager optionsManager)
var slider = optionsManager.AddSliderInput(subTab2Menu, "Test Slider", 0, 100, "* It's a test slider.", 50);
}

public override void CleanupOptionsMenu()
{
ModHelper.Console.WriteLine($"CLEANUP OPTIONS MENU");
}

private void TestAPI()
{
var api = ModHelper.Interaction.TryGetModApi<IAPI>("_nebula.ExampleAPI");
Expand Down

0 comments on commit 4cd8ffa

Please sign in to comment.