-
Notifications
You must be signed in to change notification settings - Fork 5
Implement menu source generation #76
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
Open
dplochcoder
wants to merge
4
commits into
main
Choose a base branch
from
gen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using System; | ||
|
|
||
| namespace Silksong.ModMenu.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Attribute to automatically generate a custom mod menu class for a given data type. | ||
| /// Can also be applied to a non-public field or property on such a class to force its inclusion. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] | ||
| public class GenerateMenuAttribute : Attribute { } | ||
|
|
||
| /// <summary> | ||
| /// Attribute to give a custom field generator for the annotated field or property. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] | ||
| public class ElementFactoryAttribute<T> : Attribute | ||
| where T : IElementFactory, new() { } | ||
|
|
||
| /// <summary> | ||
| /// Attribute to apply to any non-public field or property to ensure it gets a menu element generated. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] | ||
| public class ModMenuIncludeAttribute : Attribute { } | ||
|
|
||
| /// <summary> | ||
| /// Attribute to apply to a any numeric property, to specify a minimum and maximum. | ||
| /// Dynamic mins/maxes are not yet supported. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] | ||
| public class ModMenuRangeAttribute(object Min, object Max) : Attribute | ||
| { | ||
| /// <summary> | ||
| /// The minimum value of this element. | ||
| /// </summary> | ||
| public readonly object Min = Min; | ||
|
|
||
| /// <summary> | ||
| /// The maximum value of this element. | ||
| /// </summary> | ||
| public readonly object Max = Max; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attribute to mark a field or property of a data class as requiring its own custom sub-menu, of the parameterized type. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] | ||
| public class SubMenuAttribute<T> : Attribute | ||
| where T : ICustomMenu, new() { } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using Silksong.ModMenu.Elements; | ||
|
|
||
| namespace Silksong.ModMenu.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Event generated whenever any menu element of an ICustomMenu has its value changed. | ||
| /// </summary> | ||
| /// <param name="MemberName">The field or property name of the data class that got changed.</param> | ||
| /// <param name="Value">The new boxed value. May be null if the field is managed by a sub-menu.</param> | ||
| public record CustomMenuValueChangedEvent(string MemberName, object? Value) { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Silksong.ModMenu.Elements; | ||
|
|
||
| namespace Silksong.ModMenu.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Marker interface for a menu generated by `[GeneratedModMenu]`. | ||
| /// </summary> | ||
| public interface ICustomMenu<T> : ICustomMenu | ||
| { | ||
| /// <summary> | ||
| /// Notified whenever any element within this group has its value changed. | ||
| /// </summary> | ||
| public event Action<CustomMenuValueChangedEvent>? OnValueChanged; | ||
|
|
||
| /// <summary> | ||
| /// Set all fields and properties on `data` to to match this entity's menu values. | ||
| /// </summary> | ||
| void ExportTo(T data); | ||
|
|
||
| /// <summary> | ||
| /// Set all this entity's menu values to match the fields and properties on `data`. | ||
| /// </summary> | ||
| void ApplyFrom(T data); | ||
|
|
||
| /// <summary> | ||
| /// Return all menu elements defined by this custom menu. | ||
| /// </summary> | ||
| IEnumerable<MenuElement> Elements(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parent marker interface without generics. | ||
| /// </summary> | ||
| public interface ICustomMenu { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using Silksong.ModMenu.Elements; | ||
|
|
||
| namespace Silksong.ModMenu.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Generator for a custom menu element, for the parameterized type. | ||
| /// </summary> | ||
| public interface IElementFactory<T, E> : IElementFactory | ||
| where E : SelectableValueElement<T> | ||
| { | ||
| /// <summary> | ||
| /// Create a menu element for the parameterized type. | ||
| /// </summary> | ||
| public E CreateElement(LocalizedText name, LocalizedText description); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parent marker interface without generics. | ||
| /// </summary> | ||
| public interface IElementFactory { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using Silksong.ModMenu.Elements; | ||
| using Silksong.ModMenu.Screens; | ||
|
|
||
| namespace Silksong.ModMenu.Generator; | ||
|
|
||
| /// <summary> | ||
| /// A text button which opens a sub-menu of type M, for data of type T. | ||
| /// </summary> | ||
| public class SubMenuElement<T, M> : TextButton | ||
| where M : ICustomMenu<T> | ||
| { | ||
| /// <summary> | ||
| /// Sub menu abstraction shown by this button. | ||
| /// </summary> | ||
| public readonly M SubMenu; | ||
|
|
||
| /// <summary> | ||
| /// Construct a new SubMenuElement. | ||
| /// </summary> | ||
| public SubMenuElement(LocalizedText text, M subMenu, LocalizedText? description = null) | ||
| : base(text, description ?? "") | ||
| { | ||
| SubMenu = subMenu; | ||
|
|
||
| PaginatedMenuScreenBuilder builder = new(text); | ||
| builder.AddRange(SubMenu.Elements()); | ||
| var screen = builder.Build(); | ||
|
|
||
| OnSubmit += () => MenuScreenNavigation.Show(screen); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc article could probably do with an explanation of how to generate submenus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does, it's the last section of the doc. Sub-menus are generated the same way normal menus are generated, so it's not a very long section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I apparently failed to parse that, perhaps an example would be helpful