// // Convert from the "ViewModel" to the "Model" // // We don't maintain the ShaderPatcherLayer.NodeGraph representation // permanently... But we need this for serialization and shader // generation operations // // So, let's just build it from the graph control object. // public static ShaderPatcherLayer.NodeGraph ToShaderPatcherLayer(HyperGraph.GraphControl graphControl) { ShaderPatcherLayer.NodeGraph nodeGraph = new ShaderPatcherLayer.NodeGraph(); Dictionary <Node, int> nodeToVisualNodeId = new Dictionary <Node, int>(); foreach (Node n in graphControl.Nodes) { if (n.Tag is ShaderFragmentNodeTag) { ShaderFragmentNodeTag nTag = (ShaderFragmentNodeTag)n.Tag; ShaderPatcherLayer.Node resultNode = new ShaderPatcherLayer.Node(); resultNode.FragmentArchiveName = nTag.ArchiveName; resultNode.NodeId = nTag.Id; if (n.Tag is ShaderParameterNodeTag) { // This is a hack... But there should be a drop down list that // can tell us the type of this parameter struct box. foreach (var i in n.Items) { if (i is HyperGraph.Items.NodeDropDownItem) { var dropDown = (HyperGraph.Items.NodeDropDownItem)i; var stringForm = dropDown.Items[dropDown.SelectedIndex]; var parameterSource = ShaderFragmentNodeCreator.AsSourceType(stringForm); resultNode.NodeType = AsNodeType(parameterSource); } } } else { resultNode.NodeType = ShaderPatcherLayer.Node.Type.Procedure; } { if (nodeToVisualNodeId.ContainsKey(n)) { resultNode.VisualNodeId = nodeToVisualNodeId[n]; } else { resultNode.VisualNodeId = nodeGraph.VisualNodes.Count(); nodeToVisualNodeId.Add(n, resultNode.VisualNodeId); var visualNode = new ShaderPatcherLayer.VisualNode(); visualNode.Location = n.Location; if (n.Collapsed) { visualNode.State = ShaderPatcherLayer.VisualNode.StateType.Collapsed; } else { visualNode.State = ShaderPatcherLayer.VisualNode.StateType.Normal; } nodeGraph.VisualNodes.Add(visualNode); } } nodeGraph.Nodes.Add(resultNode); foreach (NodeConnection connection in n.Connections) { if (connection.From == null) { // this is a direct constant connection. It connects the value either to a constant value, or some named variable if (connection.To != null) { Node destination = connection.To.Node; if (destination.Tag is ShaderFragmentNodeTag) { ShaderFragmentNodeTag dTag = (ShaderFragmentNodeTag)destination.Tag; ShaderPatcherLayer.NodeConstantConnection resultConnection = new ShaderPatcherLayer.NodeConstantConnection(); resultConnection.Value = connection.Name; resultConnection.OutputNodeID = dTag.Id; if (connection.To.Item is ShaderFragmentNodeItem) { ShaderFragmentNodeItem destinationItem = (ShaderFragmentNodeItem)connection.To.Item; resultConnection.OutputParameterName = destinationItem.Name; } nodeGraph.NodeConstantConnections.Add(resultConnection); } } } else if (connection.From.Node == n) { // This is an output to the next node Node destination = connection.To.Node; if (destination.Tag is ShaderFragmentNodeTag) { ShaderFragmentNodeTag dTag = (ShaderFragmentNodeTag)destination.Tag; ShaderPatcherLayer.NodeConnection resultConnection = new ShaderPatcherLayer.NodeConnection(); resultConnection.InputNodeID = nTag.Id; resultConnection.OutputNodeID = dTag.Id; if (connection.To.Item is ShaderFragmentNodeItem) { ShaderFragmentNodeItem destinationItem = (ShaderFragmentNodeItem)connection.To.Item; resultConnection.OutputParameterName = destinationItem.Name; resultConnection.OutputType = TypeFromNodeItem(destinationItem); } if (connection.From.Item is ShaderFragmentNodeItem) { ShaderFragmentNodeItem sourceItem = (ShaderFragmentNodeItem)connection.From.Item; resultConnection.InputParameterName = sourceItem.Name; resultConnection.InputType = TypeFromNodeItem(sourceItem); } nodeGraph.NodeConnections.Add(resultConnection); } } } } } return(nodeGraph); }
// // Convert from the "ViewModel" to the "Model" // // We don't maintain the ShaderPatcherLayer.NodeGraph representation // permanently... But we need this for serialization and shader // generation operations // // So, let's just build it from the graph control object. // public static ShaderPatcherLayer.NodeGraph ToShaderPatcherLayer(HyperGraph.GraphControl graphControl) { ShaderPatcherLayer.NodeGraph nodeGraph = new ShaderPatcherLayer.NodeGraph(); Dictionary<Node, int> nodeToVisualNodeId = new Dictionary<Node, int>(); foreach (Node n in graphControl.Nodes) { if (n.Tag is ShaderFragmentNodeTag) { ShaderFragmentNodeTag nTag = (ShaderFragmentNodeTag)n.Tag; ShaderPatcherLayer.Node resultNode = new ShaderPatcherLayer.Node(); resultNode.FragmentArchiveName = nTag.ArchiveName; resultNode.NodeId = nTag.Id; if (n.Tag is ShaderParameterNodeTag) { // This is a hack... But there should be a drop down list that // can tell us the type of this parameter struct box. foreach (var i in n.Items) { if (i is HyperGraph.Items.NodeDropDownItem) { var dropDown = (HyperGraph.Items.NodeDropDownItem)i; var stringForm = dropDown.Items[dropDown.SelectedIndex]; var parameterSource = ShaderFragmentNodeCreator.AsSourceType(stringForm); resultNode.NodeType = AsNodeType(parameterSource); } } } else { resultNode.NodeType = ShaderPatcherLayer.Node.Type.Procedure; } { if (nodeToVisualNodeId.ContainsKey(n)) { resultNode.VisualNodeId = nodeToVisualNodeId[n]; } else { resultNode.VisualNodeId = nodeGraph.VisualNodes.Count(); nodeToVisualNodeId.Add(n, resultNode.VisualNodeId); var visualNode = new ShaderPatcherLayer.VisualNode(); visualNode.Location = n.Location; if (n.Collapsed) { visualNode.State = ShaderPatcherLayer.VisualNode.StateType.Collapsed; } else { visualNode.State = ShaderPatcherLayer.VisualNode.StateType.Normal; } nodeGraph.VisualNodes.Add(visualNode); } } nodeGraph.Nodes.Add(resultNode); foreach (NodeConnection connection in n.Connections) { if (connection.From == null) { // this is a direct constant connection. It connects the value either to a constant value, or some named variable if (connection.To != null) { Node destination = connection.To.Node; if (destination.Tag is ShaderFragmentNodeTag) { ShaderFragmentNodeTag dTag = (ShaderFragmentNodeTag)destination.Tag; ShaderPatcherLayer.NodeConstantConnection resultConnection = new ShaderPatcherLayer.NodeConstantConnection(); resultConnection.Value = connection.Name; resultConnection.OutputNodeID = dTag.Id; if (connection.To.Item is ShaderFragmentNodeItem) { ShaderFragmentNodeItem destinationItem = (ShaderFragmentNodeItem)connection.To.Item; resultConnection.OutputParameterName = destinationItem.Name; } nodeGraph.NodeConstantConnections.Add(resultConnection); } } } else if (connection.From.Node == n) { // This is an output to the next node Node destination = connection.To.Node; if (destination.Tag is ShaderFragmentNodeTag) { ShaderFragmentNodeTag dTag = (ShaderFragmentNodeTag)destination.Tag; ShaderPatcherLayer.NodeConnection resultConnection = new ShaderPatcherLayer.NodeConnection(); resultConnection.InputNodeID = nTag.Id; resultConnection.OutputNodeID = dTag.Id; if (connection.To.Item is ShaderFragmentNodeItem) { ShaderFragmentNodeItem destinationItem = (ShaderFragmentNodeItem)connection.To.Item; resultConnection.OutputParameterName = destinationItem.Name; resultConnection.OutputType = TypeFromNodeItem(destinationItem); } if (connection.From.Item is ShaderFragmentNodeItem) { ShaderFragmentNodeItem sourceItem = (ShaderFragmentNodeItem)connection.From.Item; resultConnection.InputParameterName = sourceItem.Name; resultConnection.InputType = TypeFromNodeItem(sourceItem); } nodeGraph.NodeConnections.Add(resultConnection); } } } } } return nodeGraph; }