Parse Enum Value


Node Information
Parses enum value from string to a specified enum type.
General Information
• This node doesn't have other available options.
• Some of the ports of this node can change their types and accept types automatically.
Visual
Visual

Ports
Text Type DataType Accepts IsList CustomAcception
Enum IN Object ONE FALSE TRUE
Value IN String MULTIPLE FALSE FALSE
ParsedValue OUT Object MULTIPLE FALSE FALSE

Source
namespace Korduene.KGraph.Nodes.Utilities
{
    [IsNode("Parse Enum Value")]
    public class ParseEnumValue : Node
    {
        Port enu = new Port(PortType.IN, typeof(object), AcceptsLinks.ONE, "Enum");
        Port val = new Port(PortType.IN, typeof(string), AcceptsLinks.MULTIPLE, "Value");
        Port valout = new Port(PortType.OUT, typeof(object), AcceptsLinks.MULTIPLE, "ParsedValue");
        public ParseEnumValue()
        {
            Name = "Parse Enum Value";
            NodeType = NodeType.Function;
            AddPort(enu);
            AddPort(val);
            AddPort(valout);
            ReturnType = typeof(object);
            enu.EditorEnabled = false;
            enu.CustomAcception = Accept;
            Information = "Parses enum value from string to a specified enum type.";
        }
        bool Accept(Port port)
        {
            if (port == null)
            {
                return false;
            }
            if (enu.IsConnected)
            {
                return false;
            }
            if (port.DataType != null && port.DataType.IsEnum)
            {
                enu.DataType = port.DataType;
                valout.DataType = port.DataType;
                ReturnType = port.DataType;
                return true;
            }
            return false;
        }
        public override string Code()
        {
            if (enu.IsConnected)
            {
                return "return (" + enu.DataType.FullName + ")Enum.Parse(typeof(" + enu.DataType.FullName + ")," + GetValue(val) + ", true);";
            }
            return string.Empty;
        }
    }
}