Switch


Node Information
A statement that selects a switch section to execute from a list of candidates.
Switch can only be executed on bool, char, string, numerics and enum types.
To add a case click on the info button, to remove the last case right click on the info button and click 'Remove Last Case'
General Information
• This node has other options, you can view them by clicking on the info button, this also means according to the node you might be able to add more ports than what is already listed.
• Some of the ports of this node can change their types and accept types automatically.
Visual
Visual

Ports
Text Type DataType Accepts IsList CustomAcception
Execute IN RUN MULTIPLE FALSE FALSE
Default OUT RUN MULTIPLE FALSE FALSE
Variable IN Int32 ONE FALSE TRUE
Done OUT RUN MULTIPLE FALSE FALSE
Case1 IN Int32 ONE FALSE FALSE
Case1 OUT RUN MULTIPLE FALSE FALSE

Source
namespace Korduene.KGraph.Nodes.Conditions
{
    [IsNode("Switch")]
    public class Switch : Node
    {
        Port run = new Port(PortType.IN, typeof(RUN), AcceptsLinks.MULTIPLE, "Execute");
        Port def = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE, "Default");
        Port done = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE, "Done");
        Port casevar = new Port(PortType.IN, typeof(int), AcceptsLinks.ONE, "Variable");
        Port case1 = new Port(PortType.IN, typeof(int), AcceptsLinks.ONE, "Case1");
        Port case1run = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE, "Case1");
        public Switch()
        {
            NodeType = KGraph.NodeType.Method;
            Name = "Switch";
            AddPort(run);
            AddPort(def);
            AddPort(casevar);
            AddPort(done);
            AddPort(case1);
            AddPort(case1run);
            case1.RelatedPort = case1run.ID;
            casevar.CustomAcception = Accepts;
            System.Windows.Controls.ContextMenu cm = new System.Windows.Controls.ContextMenu();
            System.Windows.Controls.MenuItem mi = new System.Windows.Controls.MenuItem();
            mi.Header = "Remove Last Case";
            mi.Click += mi_Click;
            cm.Items.Add(mi);
            InfoContextMenu = cm;
            OptionsAvilable = true;
            InfoButton.Cursor = System.Windows.Input.Cursors.Hand;
            Information = "A statement that selects a switch section to execute from a list of candidates."
            + "\r\nSwitch can only be executed on bool, char, string, numerics and enum types."
            + "\r\nTo add a case click on the info button, to remove the last case right click on the info button and click 'Remove Last Case'";
            InfoButton.Click += infoButton_Click;
        }
        void mi_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (PortInCount() > 2)
            {
                RemovePort(GetPortByIndex(PortCount - 1));
                RemovePort(GetPortByIndex(PortCount - 1));
            }
        }
        void infoButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            string cname = "Case" + (PortInCount() - 1).ToString();
            Port c = new Port(PortType.IN, case1.DataType, AcceptsLinks.ONE, cname, true);
            Port crun = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE, cname, true);
            AddPort(c);
            AddPort(crun);
            c.RelatedPort = crun.ID;
        }
        bool Accepts(Port port)
        {
            if (AreCasesConnected() && port.DataType != casevar.DataType)
            {
                return false;
            }
            if (casevar.IsConnected)
            {
                if (casevar.DataType == port.DataType)
                {
                    return true;
                }
            }
            else
            {
                if (GraphCore.IsNumeric(port.DataType) || port.DataType == typeof(bool) || port.DataType == typeof(char) || port.DataType == typeof(string) || port.DataType.IsEnum)
                {
                    foreach (var p in Ports)
                    {
                        if (p.DataType != typeof(RUN))
                        {
                            p.DataType = port.DataType;
                        }
                    }
                    return true;
                }
            }
            return false;
        }
        bool AreCasesConnected()
        {
            foreach (var port in Ports)
            {
                if (port != casevar && port.DataType != typeof(RUN))
                {
                    if (port.IsConnected)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        public override string Code()
        {
            string code = string.Empty;
            code = "switch(" + GetValue(casevar) + ")\r\n{\r\n";
            foreach (var port in Ports)
            {
                if (port.PortType == PortType.IN && port.DataType != typeof(RUN))
                {
                    if (port != casevar)
                    {
                        code += "case " + GraphCore.GetPortSelfValue(port) + ":\r\n";
                        code += GetValue(GetPortByID(port.RelatedPort)) + Environment.NewLine;
                        code += "break;\r\n";
                    }
                }
            }
            code += "default:\r\n" + GetValue(def) + "break;\r\n" + "}\r\n";
            code += GetValue(done);
            return code;
        }
    }
}