예제 #1
0
        /*
         * AddNode
         */

        public void AddNode(NodeBase nodeToAdd)
        {
            if (nodeToAdd == null)
            {
                throw new ArgumentNullException("nodeToAdd");
            }

            nodeToAdd.ContainingProgram = this;

            /* If we are adding an input node, we need to set its index starting at 1 because of the on/off node. */

            Int32        inputCount   = 1;
            ProgramInput programInput = nodeToAdd as ProgramInput;

            if (programInput != null)
            {
                for (Int32 i = 0; i < _nodes.Count; i++)
                {
                    if (_nodes[i] is ProgramInput)
                    {
                        inputCount++;
                    }
                }

                programInput.InputIndex = inputCount;
            }

            /* If we are adding an output node, we need to set its index. */

            Int32         outputCount   = 0;
            ProgramOutput programOutput = nodeToAdd as ProgramOutput;

            if (programOutput != null)
            {
                for (Int32 i = 0; i < _nodes.Count; i++)
                {
                    if (_nodes[i] is ProgramOutput)
                    {
                        outputCount++;
                    }
                }

                programOutput.OutputIndex = outputCount;
            }

            _nodes.Add(nodeToAdd);
            UpdateParentNode();
        }
예제 #2
0
        /// <summary>
        /// This is the method where user can type "run","clear" and "reset " command to execute, clear and reset commandline simulteneouly.
        /// </summary>
        /// <param name="sender">Reference to the control object</param>
        /// <param name="e">event data</param>
        private void executeInput_TextChanged(object sender, EventArgs e)
        {
            if (executeInput.Text.ToLower().Trim() == "run")
            {
                g = ShapeOutput.CreateGraphics();
                string   command     = ProgramInput.Text.ToLower();
                string[] commandline = command.Split(new String[] { "\n" },
                                                     StringSplitOptions.RemoveEmptyEntries);

                for (int k = 0; k < commandline.Length; k++)
                {
                    string[] cmd = commandline[k].Split(' ');

                    if (cmd[0].Equals("moveto") == true)
                    {
                        ShapeOutput.Refresh();
                        string[] param = cmd[1].Split(',');

                        if (param.Length != 2)
                        {
                            MessageBox.Show("Please Input Valid Parameter!!!");
                        }
                        else
                        {
                            Int32.TryParse(param[0], out x);
                            Int32.TryParse(param[1], out y);
                            MoveTo(x, y);
                        }
                    }
                    else if (cmd[0].Equals("drawto") == true)
                    {
                        string[] param = cmd[1].Split(',');
                        int      x = 0, y = 0;
                        if (param.Length != 2)
                        {
                            MessageBox.Show("Please Input Valid Parameter!!!");
                        }
                        else
                        {
                            Int32.TryParse(param[0], out x);
                            Int32.TryParse(param[1], out y);
                            DrawTo(x, y);
                        }
                    }
                    else if (cmd[0].Equals("rectangle") == true)
                    {
                        if (cmd.Length < 2)
                        {
                            MessageBox.Show("Please Input Valid Parameter!!!");
                        }
                        else
                        {
                            string[] param = cmd[1].Split(',');
                            if (param.Length < 2)
                            {
                                MessageBox.Show("Please Input Valid Parameter!!!");
                            }
                            else
                            {
                                Int32.TryParse(param[0], out width);
                                Int32.TryParse(param[1], out height);
                                IShapes   rectangle = factory.getShape("rectangle");
                                Rectangle r         = new Rectangle();
                                r.set(Color.Black, x, y, width, height);
                                r.Draw(g);
                            }
                        }
                    }

                    else if (cmd[0].Equals("circle") == true)
                    {
                        if (cmd.Length != 2)
                        {
                            MessageBox.Show("Please Input Valid Parameter!!!");
                        }
                        else
                        {
                            if (cmd[1].Equals("radius") == true)
                            {
                                IShapes circle = factory.getShape("circle");
                                Circle  c      = new Circle();
                                c.set(Color.AliceBlue, x, y, radius);
                                c.Draw(g);
                            }
                            else
                            {
                                Int32.TryParse(cmd[1], out radius);
                                IShapes circle = factory.getShape("circle");
                                Circle  c      = new Circle();
                                c.set(Color.AliceBlue, x, y, radius);
                                c.Draw(g);
                            }
                        }
                    }

                    else if (cmd[0].Equals("triangle") == true)
                    {
                        string[] param = cmd[1].Split(',');
                        if (param.Length != 2)
                        {
                            MessageBox.Show("Please Input Valid Parameter!!!");
                        }
                        else
                        {
                            Int32.TryParse(param[0], out width);
                            Int32.TryParse(param[1], out height);
                            IShapes  triangle = factory.getShape("triangle");
                            Triangle r        = new Triangle();
                            r.set(Color.AliceBlue, x, y, width, height);
                            r.Draw(g);
                        }
                    }
                    else if (cmd[0].Equals("filltriangle") == true)
                    {
                        string[] param = cmd[1].Split(',');
                        if (param.Length != 2)
                        {
                            MessageBox.Show("Please Input Valid Parameter!!!");
                        }
                        else
                        {
                            Int32.TryParse(param[0], out width);
                            Int32.TryParse(param[1], out height);
                            IShapes  circle = factory.getShape("filltriangle");
                            Triangle r      = new Triangle();
                            r.set(Color.AliceBlue, x, y, width, height);
                            r.draw(g);
                        }
                    }
                    else if (cmd[0].Equals("pen") == true)
                    {
                        if (cmd[1] == "red")//if red then color changes to red
                        {
                            c = Color.Red;
                        }
                        else if (cmd[1] == "blue")//if blue then color changes to blue
                        {
                            c = Color.Blue;
                        }
                        else if (cmd[1] == "yellow")//if yellow then color changes to yellow
                        {
                            c = Color.Yellow;
                        }
                        else
                        {
                            c = Color.AliceBlue;//default color
                        }
                    }
                    else if (cmd[0].Equals("fillrectangle") == true)
                    {
                        if (cmd.Length < 2)
                        {
                            MessageBox.Show("Please Input Valid Parameter!!!");
                        }
                        else
                        {
                            string[] param = cmd[1].Split(',');
                            if (param.Length < 2)
                            {
                                MessageBox.Show("Please Input Valid Parameter!!!");
                            }
                            else
                            {
                                Int32.TryParse(param[0], out width);
                                Int32.TryParse(param[1], out height);
                                IShapes   rectangle = factory.getShape("fillrectangle");
                                Rectangle r         = new Rectangle();
                                r.set(Color.Black, x, y, width, height);
                                r.draw(g);
                            }
                        }
                    }
                    else if (cmd[0].Equals("fillcircle") == true)
                    {
                        if (cmd.Length != 2)
                        {
                            MessageBox.Show("Please Input Valid Parameter!!!");
                        }
                        else
                        {
                            if (cmd[1].Equals("radius") == true)
                            {
                                IShapes circle = factory.getShape("fillcircle");
                                Circle  c      = new Circle();
                                c.set(Color.AliceBlue, x, y, radius);
                                c.draw(g);
                            }
                            else
                            {
                                Int32.TryParse(cmd[1], out radius);
                                IShapes circle = factory.getShape("fillcircle");
                                Circle  c      = new Circle();
                                c.set(Color.AliceBlue, x, y, radius);
                                c.draw(g);
                            }
                        }
                    }

                    else if (!cmd[0].Equals(null))
                    {
                        int errorLine = k + 1;
                        MessageBox.Show("Invalid command recognised on line " + errorLine, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else if (executeInput.Text.ToLower().Trim() == "clear")
            {
                ShapeOutput.Invalidate();
            }
            else if (executeInput.Text.ToLower().Trim() == "reset")
            {
                ProgramInput.Clear();
                MultilineProgramInput.Clear();
                ShapeOutput.Invalidate();

                size1       = 0;
                size2       = 0;
                xlabel.Text = size1.ToString();
                ylabel.Text = size2.ToString();
            }
        }
예제 #3
0
        /// <summary>
        /// </summary>
        /// <param name="nodeToRemove"></param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="nodeToRemove"/> is <see langword="null"/>.</para>
        /// </exception>
        public void RemoveNode(NodeBase nodeToRemove)
        {
            if (nodeToRemove == null)
            {
                throw new ArgumentNullException("nodeToRemove");
            }

            /* Disconnect from other nodes. */

            for (Int32 i = 0; i < _nodes.Count; i++)
            {
                NodeBase currentNode = _nodes[i];

                for (Int32 j = 0; j < currentNode.InputsLength; j++)
                {
                    if (currentNode.GetInput(j).Connection != null)
                    {
                        if (Object.ReferenceEquals(nodeToRemove, currentNode.GetInput(j).Connection.Node))
                        {
                            currentNode.GetInput(j).Connection = null;
                        }
                    }
                }
            }

            /* If it is a program output, disconnect the parent connection. */

            ProgramOutput programOutput = nodeToRemove as ProgramOutput;

            if (programOutput != null)
            {
                _lastRemovedOutputIndex = programOutput.OutputIndex;
            }

            _nodes.Remove(nodeToRemove);

            ProgramInput programInput = nodeToRemove as ProgramInput;

            if (programInput != null)
            {
                Int32 inputCount = 1;
                _lastRemovedInputIndex = programInput.InputIndex;

                for (Int32 i = 0; i < _nodes.Count; i++)
                {
                    ProgramInput currentProgramInput = _nodes[i] as ProgramInput;

                    if (currentProgramInput != null)
                    {
                        if (currentProgramInput.InputIndex > _lastRemovedInputIndex)
                        {
                            programInput.InputIndex = programInput.InputIndex - 1;
                        }

                        inputCount++;
                    }
                }
            }

            programOutput = nodeToRemove as ProgramOutput;

            if (programOutput != null)
            {
                Int32 outputCount = 0;

                for (Int32 i = 0; i < _nodes.Count; i++)
                {
                    ProgramOutput currentProgramOutput = _nodes[i] as ProgramOutput;

                    if (currentProgramOutput != null)
                    {
                        if (currentProgramOutput.OutputIndex > _lastRemovedOutputIndex)
                        {
                            currentProgramOutput.OutputIndex = currentProgramOutput.OutputIndex - 1;
                        }

                        outputCount++;
                    }
                }

                if (nodeToRemove.ContainingProgram.ParentNode != null)
                {
                    NodePort removedPort = nodeToRemove.ContainingProgram.ParentNode.GetOutput(outputCount);

                    foreach (NodeBase node in nodeToRemove.ContainingProgram.ParentNode.ContainingProgram.Nodes)
                    {
                        for (Int32 i = 0; i < node.InputsLength; i++)
                        {
                            if (node.GetInput(i).Connection != null)
                            {
                                if (Object.ReferenceEquals(node.GetInput(i).Connection, removedPort))
                                {
                                    node.GetInput(i).Connection = null;
                                }
                            }
                        }
                    }
                }
            }

            UpdateParentNode();
        }