Skip to content

Commit

Permalink
Prevent leftovers MenuButtons when rebuilding menu
Browse files Browse the repository at this point in the history
  • Loading branch information
DashingCat committed Nov 3, 2024
1 parent 5d259c6 commit 3e3b39f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Celeste.Mod.mm/Mod/Core/CoreModuleSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public bool DebugModeInEverest {
Engine.Commands.Enabled = false;
}

((patch_OuiMainMenu) (Engine.Scene as Overworld)?.GetUI<OuiMainMenu>())?.RebuildMainAndTitle();
((patch_OuiMainMenu) (Engine.Scene as Overworld)?.GetUI<OuiMainMenu>())?.NeedsRebuild();
}
}

Expand Down Expand Up @@ -236,7 +236,7 @@ public string MainMenuMode {
_MainMenuMode = value;
if (value != originalValue) {
// the main menu mode was changed; rebuild the main menu
((patch_OuiMainMenu) (Engine.Scene as Overworld)?.GetUI<OuiMainMenu>())?.RebuildMainAndTitle();
((patch_OuiMainMenu) (Engine.Scene as Overworld)?.GetUI<OuiMainMenu>())?.NeedsRebuild();
}
}
}
Expand Down Expand Up @@ -265,7 +265,7 @@ public bool WarnOnEverestYamlErrors {
_WarnOnEverestYamlErrors = value;

// rebuild the main menu to make sure we show/hide the yaml error notice.
((patch_OuiMainMenu) (Engine.Scene as Overworld)?.GetUI<OuiMainMenu>())?.RebuildMainAndTitle();
((patch_OuiMainMenu) (Engine.Scene as Overworld)?.GetUI<OuiMainMenu>())?.NeedsRebuild();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Celeste.Mod.mm/Mod/Everest/Everest.Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private static void LoadAutoUpdated(object source, FileSystemEventArgs e) {
LoadDir(e.FullPath);
else if (e.FullPath.EndsWith(".zip"))
LoadZip(e.FullPath);
((patch_OuiMainMenu) (AssetReloadHelper.ReturnToScene as Overworld)?.GetUI<OuiMainMenu>())?.RebuildMainAndTitle();
((patch_OuiMainMenu) (AssetReloadHelper.ReturnToScene as Overworld)?.GetUI<OuiMainMenu>())?.NeedsRebuild();
})));
}

Expand Down
2 changes: 1 addition & 1 deletion Celeste.Mod.mm/Mod/UI/OuiDependencyDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public override void Update() {
public void Exit() {
task = null;
Lines.Clear();
MainThreadHelper.Schedule(() => ((patch_OuiMainMenu) Overworld.GetUI<OuiMainMenu>())?.RebuildMainAndTitle());
MainThreadHelper.Schedule(() => ((patch_OuiMainMenu) Overworld.GetUI<OuiMainMenu>())?.NeedsRebuild());
Audio.Play(SFX.ui_main_button_back);
Overworld.Goto<OuiModOptions>();
}
Expand Down
21 changes: 20 additions & 1 deletion Celeste.Mod.mm/Patches/OuiMainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class patch_OuiMainMenu : OuiMainMenu {
public List<MenuButton> Buttons => buttons;
private MainMenuClimb climbButton;

private bool needsRebuild = false;

public extern void orig_CreateButtons();
public new void CreateButtons() {
orig_CreateButtons();
Expand All @@ -27,7 +29,22 @@ class patch_OuiMainMenu : OuiMainMenu {
UpdateLayout();
}

public void RebuildMainAndTitle() {
public void NeedsRebuild() {
needsRebuild = true;
}

public extern void orig_Update();
public new void Update() {
// rebuild only on update to prevent multiple rebuilds per frame
if (needsRebuild) {
RebuildMainAndTitle();
needsRebuild = false;
}

orig_Update();
}

private void RebuildMainAndTitle() {
Overworld oui = Overworld;
oui.UIs.Remove(oui.GetUI<OuiTitleScreen>());
Oui title = new OuiTitleScreen() {
Expand All @@ -45,6 +62,8 @@ public void RebuildMainAndTitle() {
break;
}

// this cannot be called more than once per frame, otherwise the
// scene keeps orphaned menu buttons which messes with selection
CreateButtons();

if (selected is MainMenuClimb) {
Expand Down

0 comments on commit 3e3b39f

Please sign in to comment.