Skip to content

Commit

Permalink
Merge pull request #599 from RedFlames/modsettings-submenu-attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
DemoJameson authored Jul 31, 2023
2 parents 5388ac7 + cbcf418 commit 4f91f68
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
36 changes: 36 additions & 0 deletions Celeste.Mod.mm/Mod/Everest/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,42 @@ public static TextMenu.Item AddDescription(this TextMenu.Item option, TextMenu c
return option;
}


/// <summary>
/// Add an Enter and Leave handler, displaying a description if selected.
/// </summary>
/// <param name="option">The input TextMenu.Item option.</param>
/// <param name="containingSubMenu">The submenu containing the TextMenu.Item option.</param>
/// <param name="parentContainer">The menu that the submenu is or will be part of.</param>
/// <param name="description"></param>
/// <returns>The passed option.</returns>
public static TextMenu.Item AddDescription(this TextMenu.Item option, TextMenuExt.SubMenu containingSubMenu, TextMenu parentContainer, string description) {
// build the description menu entry
TextMenuExt.EaseInSubHeaderExt descriptionText = new TextMenuExt.EaseInSubHeaderExt(description, false, parentContainer) {
TextColor = Color.Gray,
HeightExtra = 0f
};

if (containingSubMenu.Items.Contains(option)) {
// insert the description into item list after the option.
containingSubMenu.Insert(containingSubMenu.Items.IndexOf(option) + 1, descriptionText);
} else if (containingSubMenu.ContainsDelayedAddItem(option)) {
// insert the description into "delayed add" item list, when necessary
containingSubMenu.InsertDelayedAddItem(descriptionText, option);
}

option.OnEnter += delegate {
// make the description appear.
descriptionText.FadeVisible = true;
};
option.OnLeave += delegate {
// make the description disappear.
descriptionText.FadeVisible = false;
};

return option;
}

// Celeste already ships with this.
/*
public static string ReadNullTerminatedString(this BinaryReader stream) {
Expand Down
18 changes: 14 additions & 4 deletions Celeste.Mod.mm/Mod/Module/EverestModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,18 @@ TextMenu.Item CreateItem(PropertyInfo prop, string name = null, object settingsO
}

TextMenu.Item subMenuItem = CreateItem(subTypeProp, settingsObject: propObject);
if (subMenuItem != null)
subMenu.Add(subMenuItem);
if (subMenuItem == null)
continue;

string subsubheader = subTypeProp.GetCustomAttribute<SettingSubHeaderAttribute>()?.SubHeader;
if (subsubheader != null)
subMenu.Add(new TextMenu.SubHeader(subsubheader.DialogCleanOrNull() ?? subsubheader, false));

subMenu.Add(subMenuItem);

string subdescription = subTypeProp.GetCustomAttribute<SettingSubTextAttribute>()?.Description;
if (subdescription != null)
subMenuItem.AddDescription(subMenu, menu, subdescription.DialogCleanOrNull() ?? subdescription);
}
item = subMenu;
}
Expand All @@ -844,11 +854,11 @@ TextMenu.Item CreateItem(PropertyInfo prop, string name = null, object settingsO
menu.Add(item);

if (prop.GetCustomAttribute<SettingNeedsRelaunchAttribute>() != null)
item = item.NeedsRelaunch(menu);
item.NeedsRelaunch(menu);

string description = prop.GetCustomAttribute<SettingSubTextAttribute>()?.Description;
if (description != null)
item = item.AddDescription(menu, description.DialogCleanOrNull() ?? description);
item.AddDescription(menu, description.DialogCleanOrNull() ?? description);
}

foreach (PropertyInfo prop in type.GetProperties()) {
Expand Down
10 changes: 10 additions & 0 deletions Celeste.Mod.mm/Mod/UI/TextMenuExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,16 @@ public SubMenu Insert(int index, TextMenu.Item item) {
}
}

public bool ContainsDelayedAddItem(TextMenu.Item item) {
return Container == null && delayedAddItems.Contains(item);
}

public SubMenu InsertDelayedAddItem(TextMenu.Item item, TextMenu.Item after) {
if (Container == null && delayedAddItems.Contains(after))
delayedAddItems.Insert(delayedAddItems.IndexOf(after) + 1, item);
return this;
}

/// <summary>
/// Remove any non-submenu <see cref="TextMenu.Item"/> from the Submenu
/// </summary>
Expand Down

0 comments on commit 4f91f68

Please sign in to comment.