// Create a copy of the file state
        public FileState(FileState fileState, bool newState = true)
        {
            this.file = fileState.File;
            this.renderer = fileState.Renderer;

            // Copy the nodes
            dictionaryOldToNew = new Dictionary<Node, Node>();

            foreach (Node node in fileState.Nodes)
            {
                Node newNode = new Node(node);

                dictionaryOldToNew[node] = newNode;

                Nodes.Add(newNode);
            }

            // Copy the connections
            foreach (Variable variable in fileState.Nodes.SelectMany(x => x.Variables).Where(v => v.Type == Variable.VariableType.Output && v.InputType == Variable.InputTypes.Link)) 
            {
                foreach (Connection connection in variable.GetLinks())
                {
                    int outIndex = connection.OutputVariable.Node.Variables.IndexOf(connection.OutputVariable);
                    int inIndex = connection.InputVariable.Node.Variables.IndexOf(connection.InputVariable);

                    Connection newConnection = new Connection();
                    // newConnection.DesignArea = connection.DesignArea;
                    newConnection.OutputVariable = dictionaryOldToNew[connection.OutputVariable.Node].Variables[outIndex];
                    newConnection.InputVariable = dictionaryOldToNew[connection.InputVariable.Node].Variables[inIndex];
                }
            }

            if (newState)
            {
                PreviousState = fileState;
                fileState.NextStates.Add(this);
            }
        }
 public void RemoveConnection(Connection connection)
 {
     Canvas.Children.Remove(connection);
 }
 public void AddConnection(Connection newConnection)
 {
     Canvas.Children.Add(newConnection);
 }
        private static HitTestResultBehavior finalizeCreation(HitTestResult hitTestResult, Connection newConnection)
        {
            // Try to find the variable parent
            Variable inputVariable = null;

            if (hitTestResult.VisualHit is Ellipse)
            {
                Ellipse ellipse = hitTestResult.VisualHit as Ellipse;

                DependencyObject parent = VisualTreeHelper.GetParent(ellipse);

                while(!(parent is DesignArea))
                {
                    if (parent is Variable) 
                    {
                        Variable variable = parent as Variable;

                        if (variable.Type == Variable.VariableType.Input && variable.GetLinks().Count == 0)
                        {
                            inputVariable = variable;
                        }

                        break;
                    }

                    parent = VisualTreeHelper.GetParent(parent);
                }
            }

            if (inputVariable != null)
            {
                // Finalize the connection
                newConnection.InputVariable = inputVariable;
            }
            else
            {
                // Destroy the connection
                newConnection.OutputVariable = null;

                newConnection.DesignArea.RemoveConnection(newConnection);
            }

            return HitTestResultBehavior.Stop;
        }
        private static void endCreation(Connection newConnection)
        {
            DesignArea designArea = newConnection.DesignArea;

            // Remove the mouse move and mouse up listeners
            designArea.MouseMove -= newConnection.creationMouseMoveListener;
            designArea.MouseUp -= newConnection.creationMouseUpListener;

            // Check if the mouse is currently intersecting with a input variable
            HitTestFilterCallback filterCallback = new HitTestFilterCallback(element => element == newConnection ? HitTestFilterBehavior.ContinueSkipSelfAndChildren : HitTestFilterBehavior.Continue);

            HitTestResultCallback resultCallback = new HitTestResultCallback(element => finalizeCreation(element, newConnection));

            VisualTreeHelper.HitTest(designArea.Canvas, filterCallback, resultCallback, new PointHitTestParameters(newConnection.EndPoint));
        }
        private static void dragCreation(Connection newConnection, MouseEventArgs e) {

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                // Update the connection curve
                newConnection.updateCurve();

                newConnection.DesignArea.Canvas.UpdateLayout();
            }
            else
            {
                // Finalize creation
                endCreation(newConnection);
            }
        }
        public static void startCreation(Variable outputVariable)
        {
            DesignArea designArea = outputVariable.DesignArea;

            Connection newConnection = new Connection();
            newConnection.DesignArea = designArea;
            newConnection.OutputVariable = outputVariable;

            designArea.AddConnection(newConnection);

            DynamicCanvas.SetZIndex(newConnection, designArea.getHighestZIndex());

            // Update curve position when the mouse is moved
            newConnection.creationMouseMoveListener = new MouseEventHandler((x, y) => dragCreation(newConnection, y));
            newConnection.creationMouseUpListener = new MouseButtonEventHandler((x, y) => dragCreation(newConnection, y));
            designArea.MouseMove += newConnection.creationMouseMoveListener;
            designArea.MouseUp += newConnection.creationMouseUpListener;
        }
        public static void Parse(string code, ShaderComposer.FileManagement.File file)
        {
            StringReader sr = new StringReader(code);

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            settings.IgnoreWhitespace = true;
            
            XmlReader xmlReader = XmlReader.Create(sr, settings);
            
            NumberFormatInfo nfi = new NumberFormatInfo();
            nfi.NumberDecimalSeparator = ",";

            xmlReader.ReadToFollowing("ShaderDescription");
            xmlReader.ReadToFollowing("Nodes");

            Dictionary<String, Variable> vars = new Dictionary<string, Variable>();

            if (xmlReader.ReadToDescendant("Node"))
            {
                do
                {
                    xmlReader.MoveToAttribute("TypeID"); string typeID = xmlReader.ReadContentAsString();

                    Type t = Libraries.LibraryManager.Instance.FindNode(typeID);

                    if (t == null)
                    {
                        continue;
                    }

                    xmlReader.MoveToAttribute("X"); float x = float.Parse(xmlReader.ReadContentAsString(), nfi);
                    xmlReader.MoveToAttribute("Y"); float y = float.Parse(xmlReader.ReadContentAsString(), nfi);

                    Node n = file.ActiveState.AddNewNode(t, new Point(x, y));

                    System.Diagnostics.Debug.WriteLine("X: " + x + ", Y: " + y);

                    // Variables
                    xmlReader.ReadToFollowing("Variables");

                    if (xmlReader.ReadToDescendant("Variable"))
                    {
                        do
                        {
                            xmlReader.MoveToAttribute("ID"); string ID = xmlReader.ReadContentAsString();
                            xmlReader.MoveToAttribute("Name"); string Name = xmlReader.ReadContentAsString();
                            xmlReader.MoveToAttribute("Text"); string Text = xmlReader.ReadContentAsString();
                            xmlReader.MoveToAttribute("Type"); string Type = xmlReader.ReadContentAsString();

                            foreach (Variable v in n.Variables)
                            {
                                if (v.Name == Name)
                                {
                                    vars[ID] = v;
                                    if (Type == "Link") v.InputType = Variable.InputTypes.Link;
                                    if (Type == "Color") v.InputType = Variable.InputTypes.Color;
                                    if (Type == "Varying") v.InputType = Variable.InputTypes.Varying;
                                    if (Type == "Float1") v.InputType = Variable.InputTypes.Float1;
                                    if (Type == "Float2") v.InputType = Variable.InputTypes.Float2;
                                    if (Type == "Float3") v.InputType = Variable.InputTypes.Float3;
                                    if (Type == "Float4") v.InputType = Variable.InputTypes.Float4;
                                    if (Type == "Boolean") v.InputType = Variable.InputTypes.Boolean;

                                    if (Type == "Float1" || Type == "Float2" || Type == "Float3" || Type == "Float4") {
                                        xmlReader.MoveToAttribute("value1");
                                        v.inputFloat1.Text = xmlReader.ReadContentAsString();
                                    } 
                                    if (Type == "Float2" || Type == "Float3" || Type == "Float4") {
                                        xmlReader.MoveToAttribute("value2");
                                        v.inputFloat2.Text = xmlReader.ReadContentAsString();
                                    } 
                                    if (Type == "Float3" || Type == "Float4") {
                                        xmlReader.MoveToAttribute("value3");
                                        v.inputFloat3.Text = xmlReader.ReadContentAsString();
                                    } 
                                    if (Type == "Float4") {
                                        xmlReader.MoveToAttribute("value4");
                                        v.inputFloat4.Text = xmlReader.ReadContentAsString();
                                    } 
                                    if (Type == "Color") {
                                        xmlReader.MoveToAttribute("value1");
                                        byte r = (byte)xmlReader.ReadContentAsInt();
                                        xmlReader.MoveToAttribute("value2");
                                        byte g = (byte)xmlReader.ReadContentAsInt();
                                        xmlReader.MoveToAttribute("value3");
                                        byte b = (byte)xmlReader.ReadContentAsInt();
                                        xmlReader.MoveToAttribute("value4");
                                        byte a = (byte)xmlReader.ReadContentAsInt();

                                        v.inputColor.SelectedColor = Color.FromArgb(a, r, g, b);
                                    }
                                    if (Type == "Varying")
                                    {
                                        xmlReader.MoveToAttribute("value");
                                        string varying = xmlReader.ReadContentAsString();

                                        foreach (ComboBoxItem item in v.inputVarying.Items)
                                            if ((string)item.Content == varying)
                                            {
                                                v.inputVarying.SelectedItem = item;
                                                break;
                                            }

                                        //v.inputVarying.SelectedItem = varying;
                                    }

                                    xmlReader.MoveToAttribute("dim1");
                                    v.typeMenuFloat1.IsChecked = xmlReader.ReadContentAsString() == "True" ? true : false;
                                    xmlReader.MoveToAttribute("dim2");
                                    v.typeMenuFloat2.IsChecked = xmlReader.ReadContentAsString() == "True" ? true : false;
                                    xmlReader.MoveToAttribute("dim3");
                                    v.typeMenuFloat3.IsChecked = xmlReader.ReadContentAsString() == "True" ? true : false;
                                    xmlReader.MoveToAttribute("dim4");
                                    v.typeMenuFloat4.IsChecked = xmlReader.ReadContentAsString() == "True" ? true : false;

                                    v.updateTypeMenu();

                                    v.Text = Text;
                                }
                            }
                        } while (xmlReader.ReadToNextSibling("Variable"));
                        
                        xmlReader.ReadEndElement();
                    }

                    xmlReader.ReadEndElement();

                } while (xmlReader.Name == "Node");
            }

            // Conn
            xmlReader.ReadToFollowing("Connections");
            if (xmlReader.ReadToDescendant("Connection"))
            {
                do
                {
                    try
                    {
                        xmlReader.MoveToAttribute("SourceID"); string sourceID = xmlReader.ReadContentAsString();
                        xmlReader.MoveToAttribute("DestinationID"); string destinationID = xmlReader.ReadContentAsString();
                        xmlReader.MoveToAttribute("PreviewPinned"); bool previewPinned = xmlReader.ReadContentAsString() == "True" ? true : false;

                        // Create connection
                        Connection c = new Connection();
                        c.OutputVariable = vars[sourceID];
                        c.InputVariable = vars[destinationID];

                        file.FileView.DesignArea.AddConnection(c);
                        c.DesignArea = file.FileView.DesignArea;
                    }
                    catch { }

                } while (xmlReader.ReadToNextSibling("Connection"));
            }

            xmlReader.Close();
        }
        private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Image img = (((((IntermediatePreviewTabItem.Items[0] as FileViewTabItem).Content as Grid).Children[1] as StackPanel).Children[1] as Grid).Children[0] as Image);

            if (ir1 != null)
            {
                ir1.Destroy();
                img.Source = null;
                ir1 = null;
            }

            //
            FileState altState = new FileState(FilesManager.Instance.ActiveFile.ActiveState, false);

            // Find output
            Node outputNode = null;

            foreach (Node node in altState.Nodes)
            {
                if (node.inode.IsOutputNode())
                {
                    outputNode = node;
                    break;
                }
            }

            if ((sender as ComboBox).SelectedValue == null)
                return;

            // Find selected node
            Variable oldSelectedVariable = ((sender as ComboBox).SelectedValue as VInf).v;
            Node selectedNode = altState.dictionaryOldToNew[oldSelectedVariable.Node];

            int oldIndex = oldSelectedVariable.Node.Variables.IndexOf(oldSelectedVariable);
            Variable selectedVariable = selectedNode.Variables[oldIndex];
            
            // Remove output input if it was linked
            if (outputNode.Variables[0].InputType == Variable.InputTypes.Link)
            {
                outputNode.Variables[0].GetLinks()[0].removeConnection();
            }

            // Find var output if it was linked
            Variable outputVariable = selectedVariable.GetLinks()[0].OutputVariable;

            // Create a new connection
            Connection c = new Connection();
            c.OutputVariable = outputVariable;
            c.InputVariable = outputNode.Variables[0];

            // Compile source
            HLSLCompiler comp = new HLSLCompiler(altState);
            comp.Compile();

            ir1 = FilesManager.Instance.ActiveFile.ActiveState.Renderer.Create();
            ImageSource images = ir1.Initialize();
            ir1.SetSourceCode(comp.SourceCode);

            img.Source = images;
        }
        private void writeConnection(XmlWriter xmlWriter, Connection connection)
        {
            xmlWriter.WriteStartElement("Connection");

            xmlWriter.WriteAttributeString("SourceID", connection.OutputVariable.GetHashCode().ToString("X8"));
            xmlWriter.WriteAttributeString("DestinationID", connection.InputVariable.GetHashCode().ToString("X8"));
            xmlWriter.WriteAttributeString("PreviewPinned", connection.PreviewWindow.Pinned.ToString());

            xmlWriter.WriteEndElement();
        }
        private void updatePreview()
        {
            Image img = PreviewImage;

            if (ir0 != null)
            {
                ir0.Destroy();
                img.Source = null;
                ir0 = null;
            }

            //
            if (!FilesManager.Instance.ActiveFile.ActiveState.IsComplete)
                return;

            FileState altState = new FileState(FilesManager.Instance.ActiveFile.ActiveState, false);

            // Find output
            Node outputNode = null;

            foreach (Node node in altState.Nodes)
            {
                if (node.inode.IsOutputNode())
                {
                    outputNode = node;
                    break;
                }
            }

            // Find selected node
            oldSelectedVariable = parent.InputVariable;
            Node selectedNode = altState.dictionaryOldToNew[oldSelectedVariable.Node];

            int oldIndex = oldSelectedVariable.Node.Variables.IndexOf(oldSelectedVariable);
            Variable selectedVariable = selectedNode.Variables[oldIndex];

            // Remove output input if it was linked
            if (outputNode.Variables[0].InputType == Variable.InputTypes.Link)
            {
                outputNode.Variables[0].GetLinks()[0].removeConnection();
            }

            // Find var output if it was linked
            if (selectedVariable.GetLinks().Count > 0 && FilesManager.Instance.ActiveFile.ActiveState.Renderer != null)
            {
                Variable outputVariable = selectedVariable.GetLinks()[0].OutputVariable;

                // Create a new connection
                Connection c = new Connection();
                c.OutputVariable = outputVariable;
                c.InputVariable = outputNode.Variables[0];

                // Compile source
                HLSLCompiler comp = new HLSLCompiler(altState);
                comp.Compile();

                ir0 = FilesManager.Instance.ActiveFile.ActiveState.Renderer.Create();
                ImageSource images = ir0.Initialize();
                ir0.SetSourceCode(comp.SourceCode);

                img.Source = images;
            }
        }