Skip to content

Commit

Permalink
Fix game crashes after exiting a submenu which has all items disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
WEGFan committed Dec 30, 2021
1 parent a5c21f3 commit 82e785e
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions Celeste.Mod.mm/Mod/UI/TextMenuExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,12 @@ public void RecalculateSize() {

/// <inheritdoc cref="TextMenu.GetYOffsetOf(TextMenu.Item)"/>
public float GetYOffsetOf(TextMenu.Item item) {
float offset = Container.GetYOffsetOf(this) - Height() * 0.5f;
if (item == null) {
return 0f;
// common case is all items in submenu are disabled when item is null
return offset + TitleHeight * 0.5f;
}
float offset = 0f;
offset += TitleHeight;
foreach (TextMenu.Item child in Items) {
if (child.Visible) {
offset += child.Height() + ItemSpacing;
Expand All @@ -722,11 +724,11 @@ public float GetYOffsetOf(TextMenu.Item item) {
break;
}
}
return offset - item.Height() * 0.5f - ItemSpacing + Container.GetYOffsetOf(this) - Height() * 0.5f + TitleHeight;
return offset - item.Height() * 0.5f - ItemSpacing;
}

public void Exit() {
Current.OnLeave?.Invoke();
Current?.OnLeave?.Invoke();
Focused = false;
if (!Input.MenuUp.Repeating && !Input.MenuDown.Repeating)
Audio.Play(SFX.ui_main_button_back);
Expand Down Expand Up @@ -1121,19 +1123,21 @@ public void RecalculateSize() {
}

public float GetYOffsetOf(TextMenu.Item item) {
float offset = Container.GetYOffsetOf(this) - Height() * 0.5f;
if (item == null) {
return 0f;
// common case is all items in submenu are disabled when item is null
return offset + TitleHeight * 0.5f;
}
float offset = 0f;
foreach (TextMenu.Item item2 in CurrentMenu) {
if (item2.Visible) {
offset += item2.Height() + ItemSpacing;
offset += TitleHeight;
foreach (TextMenu.Item child in CurrentMenu) {
if (child.Visible) {
offset += child.Height() + ItemSpacing;
}
if (item2 == item) {
if (child == item) {
break;
}
}
return offset - item.Height() * 0.5f - ItemSpacing + Container.GetYOffsetOf(this) - Height() * 0.5f + TitleHeight;
return offset - item.Height() * 0.5f - ItemSpacing;
}

#endregion
Expand Down Expand Up @@ -1239,7 +1243,7 @@ public override void Update() {
}
if (!Input.MenuConfirm.Pressed) {
if (Input.MenuCancel.Pressed || Input.ESC.Pressed || Input.Pause.Pressed) {
Current.OnLeave?.Invoke();
Current?.OnLeave?.Invoke();
Focused = false;
Audio.Play(SFX.ui_main_button_back);
Container.AutoScroll = containerAutoScroll;
Expand Down

0 comments on commit 82e785e

Please sign in to comment.