Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent leftovers MenuButtons when rebuilding menu #845

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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