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;
        }
        // Rebuilds the shader
        public void Build()
        {
            if (!IsComplete)
                return;

            // Compile the shader
            HLSLCompiler hlslCompiler = new HLSLCompiler(this);
            hlslCompiler.Compile();

            // Update shader source code viewer
            File.FileView.ShaderOutput.SourceCode = hlslCompiler.SourceCode;

            // Update intermediate preview possibilities
            List<Variable> IntermediateOutputs = new List<Variable>();

            // Update preview windows
            foreach (Node n in Nodes)
                foreach (Variable v in n.Variables)
                    if (v.Type == Variable.VariableType.Input && v.InputType == Variable.InputTypes.Link && v.GetLinks().Count == 1)
                        v.GetLinks()[0].PreviewWindow.Graph_Changed();

            //
            foreach (Node node in Nodes) 
            {
                if (!node.inode.IsOutputNode())
                    foreach (Variable variable in node.Variables)
                    {
                        if (variable.Type == Variable.VariableType.Input && variable.InputType == Variable.InputTypes.Link)
                        {
                            IntermediateOutputs.Add(variable);
                        }
                    }
            }

            File.FileView.SetIntermediateOutputs(IntermediateOutputs);

            // Update the current renderer
            if (renderer != null)
            {
                renderer.SetSourceCode(hlslCompiler.SourceCode);
            }
        }
        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;
            }
        }