alexguirre / RAGENativeUI

MIT License
115 stars 38 forks source link

Menu Templating #35

Closed alexguirre closed 4 years ago

alexguirre commented 5 years ago

See menu-template branch. Allows the user to create a Menu based on the properties and methods of a class that inherits MenuTemplate via attributes. All item attributes specifiy at least three properties: Name, Text and Description. Name is used to reference the MenuItem (for example, to access the MenuItem instance with MenuTemplate.GetItemByName), if null, the property name will be used.

API

MenuTemplate

Base class for menu templates. Implements INotifyPropertyChanged. The method BuildMenu creates the menu and sets the property Menu.

MenuAttribute

Defines the basic properties of a Menu: Title, Subtitle and Theme. Required to build the menu from a template class.

[Menu(Title = "My Menu", Subtitle = "Subtitle for my menu")]
private class TestMenu : MenuTemplate

MenuItemAttribute

Defines a property as a MenuItem. The property type must be a delegate or a type that has an Invoke method with either of the following signatures:

void Invoke();
void Invoke(MenuItem);

The delegate or method will be executed when the MenuItem.Activated event is raised.

[MenuItem(Text = "ItemText", Description "ItemDescription")]
private Action BasicItem { get; }

[MenuItem(Text = "ItemText", Description "ItemDescription")]
private Action<MenuItem> BasicItem2 { get; }

[MenuItem(Text = "ItemText", Description "ItemDescription")]
private InvokableClass ItemWithCustomClass { get; } = new InvokableClass();

// ...
private class InvokableClass
{
    public void Invoke()
    {
        // ...
    }
}

MenuItemCheckboxAttribute

Defines a property as a MenuItemCheckbox. The property type must be bool. If the create checkbox item is checked or unchecked the property value will be updated. And in the other way around, if the property setter raises the INotifyPropertyChanged.PropertyChanged event, changing the value via the property setter the checkbox item IsChecked property will be updated too.

[MenuItemCheckbox(Text = "ItemText")]
public bool BoolValue
{
    get => boolValue;
    set => SetProperty(ref doSomeOtherThing, value);
}

Note, SetProperty is a method provided by MenuTemplate that raises the PropertyChanged event.

MenuItemNumericScrollerAttribute

Similar to MenuItemCheckboxAttribute but for properties of numeric types. Also provides properties to customize the MenuItemNumericScroller created: Minimum, Maximum, Increment, ThousandsSeparator, Hexadecimal and DecimalPlaces.

[MenuItemNumericScroller(Text = "ItemText")]
public float FloatValue
{
    get => floatValue;
    set => SetProperty(ref floatValue, value);
}

[MenuItemNumericScroller(Text = "ItemText", Increment = 1.0, DecimalPlaces = 0)]
public int IntValue
{
    get => intValue;
    set => SetProperty(ref intValue, value, nameof(IntValue), nameof(IntValueAsHex));
}

[MenuItemNumericScroller(Text = "Int Value as Hex", Increment = 1.0, DecimalPlaces = 0, Hexadecimal = true)]
private int IntValueAsHex
{
    get => IntValue;
    set => IntValue = value;
}

MenuItemEnumScroller

Similar to MenuItemCheckboxAttribute and MenuItemNumericScrollerAttribute but for properties of enum types.

[MenuItemEnumScroller(Text = "ItemText")]
public GameControl EnumValue
{
    get => enumValue;
    set => SetProperty(ref enumValue, value);
}

MenuItemActivatedHandlerAttribute

Defines a method as a MenuItem.Activated event handler for a specific set of items. The Items string array contains the names of the wanted menu items.

[MenuItemActivatedHandler(nameof(IntValue), nameof(FloatValue))]
private void OnIntOrFloatValueActivated(MenuItem sender, ActivatedEventArgs e)
{
}

TBD