CommandTemplate

Templates of commands is description for DevTool Canvas, how it should draw finded commands with specified arguments/types.

Namespace: RenownedGames.DevTool.UIElements

Create new template

Let's take it apart creation new command template type based on CommandTemplateFloat.

[AddComponentMenu("Renowned Games/DevTool/Core Modules/Developer Tools/DevTool/Template/Command Template Float")]
[DisallowMultipleComponent]
[RequiredType(typeof(float))]
public sealed class CommandTemplateFloat : CommandTemplateSingleField<float>
{
    /// <summary>
    /// Implement this method to read parameter value from input field.
    /// </summary>
    /// <param name="field">Edited field component.</param>
    /// <param name="value">Parsed (T) value from input field.</param>
    protected override void ReadValue(InputField field, out float value)
    {
        field.text = field.text.Replace('.', ',');
        value = Convert.ToSingle(field.text);
        field.text = value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
    }

    /// <summary>
    /// Called after menu was repaint.
    /// </summary>
    /// <param name="parameter">Actual command parameters.</param>
    /// <param name="text">Convert to string parameter.</param>
    protected override void OnParameterUpdate(float parameter, out string text)
    {
        text = parameter.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
    }
}

Class inherits from CommandTemplateSingleField which in turn inherits from CommandTemplate

CommandTemplateSingleField class allow to use commands with single argument, where <float> type of argument.

[RequiredType] attribute note DevTool engine what this template can work only with one argument type of float. If you want to create command which will work with multiple arguments add multiple[RequiredType] attributes.

[RequiredType(typeof(float))]
public sealed class CommandTemplateFloat : CommandTemplateSingleField<float>

Next step implementation of ReadValue and OnParameterUpdate methods

ReadValue called before execute command. In this method you need to read value from input field and parse it to required type.

protected override void ReadValue(InputField field, out float value)
{
    // The field contains dots we translate them into commas
    field.text = field.text.Replace('.', ',');
    
    // Parse text to float and set to output value 
    value = Convert.ToSingle(field.text);

    // Reduce the number of characters after the decimal point to two for easier reading.
    field.text = value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
}

OnParameterUpdate called when DevTool Canvas become repaint. In this method you need to update value in input field to actual.

protected override void OnParameterUpdate(float parameter, out string text)
{
    // Reduce the number of characters after the decimal point to two for easier reading.
    field.text = value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
}

Last updated