/// <summary> /// Generates the source code for initializing scripting nodes for the specified state. /// </summary> /// <param name="state">The state to generate scripting nodes from.</param> /// <param name="stateVariableName">Name of the state variable.</param> private void GenerateScriptNodes(State state, string stateVariableName) { Dictionary <BaseNode, string> baseNodes = new Dictionary <BaseNode, string>(); // init script nodes and variables for (int i = 0; i < state.Nodes.Count; ++i) { if (state.Nodes[i] is Node) { Node node = state.Nodes[i] as Node; writer.WriteLine("{0} script{1} = new {0}();", node.NodeData.RealName, i); writer.WriteLine("script{0}.Container = {1};", i, stateVariableName); baseNodes[node] = String.Format("script{0}", i); } else if (state.Nodes[i] is Variable) { Variable variable = state.Nodes[i] as Variable; if (variable.NamedVariable == null) { writer.WriteLine("Variable<{0}> variable{1} = new Variable<{0}>({2});", GetVariableGameEngineType(variable.VariableType), i, GetVariableValue(variable.Value)); baseNodes[variable] = String.Format("variable{0}", i); } } } // set script nodes settings and create connections for (int i = 0; i < state.Nodes.Count; ++i) { if (state.Nodes[i] is Node) { Node node = state.Nodes[i] as Node; foreach (NodeSocket socket in node.Sockets) { if (socket.Type == NodeSocketType.SignalOut) { Debug.Assert(socket is SignalNodeSocket, "Invalid node signal out socket class."); SignalNodeSocket signalSocket = socket as SignalNodeSocket; // connect to node in sockets foreach (SignalNodeSocket inSocket in signalSocket.Connections) { writer.WriteLine("{0}.{1} += {2}.{3};", baseNodes[node], signalSocket.NodeSocketData.RealName, baseNodes[inSocket.Node], inSocket.NodeSocketData.RealName); } } else if (socket.Type == NodeSocketType.VariableIn || socket.Type == NodeSocketType.VariableOut) { Debug.Assert(socket is VariableNodeSocket, "Invalid node variable socket class."); VariableNodeSocket variableSocket = socket as VariableNodeSocket; // no connection is made so use default value if possible if (variableSocket.Connections.Count == 0) { // no value is possible if (variableSocket.NodeSocketData.CanBeEmpty || variableSocket.Type == NodeSocketType.VariableOut || GetVariableValue(variableSocket.Value) == "null") { writer.WriteLine("{0}.{1} = null;", baseNodes[node], variableSocket.NodeSocketData.RealName); } else { // default value in array if (variableSocket.NodeSocketData.IsArray) { writer.WriteLine("{0}.{1} = new Variable<{2}>[1];", baseNodes[node], variableSocket.NodeSocketData.RealName, GetVariableGameEngineType(variableSocket.VariableType)); writer.WriteLine("{0}.{1}[0] = new Variable<{2}>({3});", baseNodes[node], variableSocket.NodeSocketData.RealName, GetVariableGameEngineType(variableSocket.VariableType), GetVariableValue(variableSocket.Value)); } // default value for variable else { writer.WriteLine("{0}.{1} = new Variable<{2}>({3});", baseNodes[node], variableSocket.NodeSocketData.RealName, GetVariableGameEngineType(variableSocket.VariableType), GetVariableValue(variableSocket.Value)); } } } // connect to variables else { if (variableSocket.NodeSocketData.IsArray) { // initialize array writer.WriteLine("{0}.{1} = new Variable<{2}>[{3}];", baseNodes[node], variableSocket.NodeSocketData.RealName, GetVariableGameEngineType(variableSocket.VariableType), variableSocket.Connections.Count); } for (int j = 0; j < variableSocket.Connections.Count; ++j) { if (variableSocket.Connections[j].NamedVariable == null) { writer.WriteLine(variableSocket.NodeSocketData.IsArray ? "{0}.{1}[{2}] = {3};" : "{0}.{1} = {3};", baseNodes[node], variableSocket.NodeSocketData.RealName, j, baseNodes[variableSocket.Connections[j]]); } // local variable else if ((actor == null && variableSocket.Connections[j].NamedVariable.ScriptingComponent.Actor == null) || (actor != null && variableSocket.Connections[j].NamedVariable.ScriptingComponent == actor.Scripting)) { writer.WriteLine(variableSocket.NodeSocketData.IsArray ? @"{0}.{1}[{2}] = GetVariable<{3}>(""{4}"");" : @"{0}.{1} = GetVariable<{3}>(""{4}"");", baseNodes[node], variableSocket.NodeSocketData.RealName, j, GetVariableGameEngineType(variableSocket.VariableType), variableSocket.Connections[j].NamedVariable.Name); } // other actor variable else { writer.WriteLine(variableSocket.NodeSocketData.IsArray ? @"{0}.{1}[{2}] = {3}.GetVariable<{4}>(""{5}"");" : @"{0}.{1} = {3}.GetVariable<{4}>(""{5}"");", baseNodes[node], variableSocket.NodeSocketData.RealName, j, variableSocket.Connections[j].NamedVariable.ScriptingComponent.Actor != null ? GetActorValue(variableSocket.Connections[j].NamedVariable.ScriptingComponent.Actor) : "Screen.Actors.Get(0)", GetVariableGameEngineType(variableSocket.VariableType), variableSocket.Connections[j].NamedVariable.Name); } } } } } // event node if (node.NodeData.Type == NodeType.Event) { writer.WriteLine("{0}.Connect();", baseNodes[node]); } } } }
private void GenerateNode(Node node, XmlElement category) { // generate image SceneNode newNode = node.Clone(scriptingComponent.StateMachines[0].StartingState).CreateView(); scriptingScreen.AddSceneNodeToCenter(newNode); newNode.Location = new PointF(20, 10); bool hasVariables = false; foreach (NodeSocket nodeSocket in node.Sockets) { if (nodeSocket is VariableNodeSocket && ((VariableNodeSocket)nodeSocket).Visible) { hasVariables = true; break; } } scriptingScreen.DrawToBitmap(image, screenRect); string filenameImage = Path.Combine(outputDir, node.NodeData.RealName + ".png"); image.Clone(new RectangleF(0, 0, 20 + newNode.Bounds.Width + 20, 10 + newNode.Bounds.Height + (hasVariables ? 40 : 10)), image.PixelFormat).Save(filenameImage); ((NodeView)newNode).Node.Remove(); // generate xml element for node XmlElement nodeElement = (XmlElement)category.AppendChild(doc.CreateElement("node")); nodeElement.SetAttribute("id", Guid.NewGuid().ToString()); ElementAppendChild(nodeElement, "name", node.NodeData.Name); ElementAppendChild(nodeElement, "realName", node.NodeData.RealName); ElementAppendChild(nodeElement, "description", node.NodeData.Description); ElementAppendChild(nodeElement, "type", node.NodeData.Type.ToString()); ElementAppendChild(nodeElement, "image", node.NodeData.RealName + ".png"); XmlElement inSocketsElement = null; XmlElement outSocketsElement = null; XmlElement variableInSocketsElement = null; XmlElement variableOutSocketsElement = null; // generate sockets foreach (NodeSocket nodeSocket in node.Sockets) { // in socket if (nodeSocket is SignalNodeSocket && nodeSocket.Type == NodeSocketType.SignalIn) { if (inSocketsElement == null) { inSocketsElement = (XmlElement)nodeElement.AppendChild(doc.CreateElement("socketsIn")); } XmlElement socketElement = (XmlElement)inSocketsElement.AppendChild(doc.CreateElement("socket")); SignalNodeSocket signalNodeSocket = nodeSocket as SignalNodeSocket; ElementAppendChild(socketElement, "name", signalNodeSocket.NodeSocketData.Name); ElementAppendChild(socketElement, "realName", signalNodeSocket.NodeSocketData.RealName); ElementAppendChild(socketElement, "description", signalNodeSocket.NodeSocketData.Description); } // out socket else if (nodeSocket is SignalNodeSocket && nodeSocket.Type == NodeSocketType.SignalOut) { if (outSocketsElement == null) { outSocketsElement = (XmlElement)nodeElement.AppendChild(doc.CreateElement("socketsOut")); } XmlElement socketElement = (XmlElement)outSocketsElement.AppendChild(doc.CreateElement("socket")); SignalNodeSocket signalNodeSocket = nodeSocket as SignalNodeSocket; ElementAppendChild(socketElement, "name", signalNodeSocket.NodeSocketData.Name); ElementAppendChild(socketElement, "realName", signalNodeSocket.NodeSocketData.RealName); ElementAppendChild(socketElement, "description", signalNodeSocket.NodeSocketData.Description); } // variable in socket else if (nodeSocket is VariableNodeSocket && nodeSocket.Type == NodeSocketType.VariableIn) { if (variableInSocketsElement == null) { variableInSocketsElement = (XmlElement)nodeElement.AppendChild(doc.CreateElement("socketsVariableIn")); } XmlElement socketElement = (XmlElement)variableInSocketsElement.AppendChild(doc.CreateElement("socket")); VariableNodeSocket variableNodeSocket = nodeSocket as VariableNodeSocket; ElementAppendChild(socketElement, "name", variableNodeSocket.NodeSocketData.Name); ElementAppendChild(socketElement, "realName", variableNodeSocket.NodeSocketData.RealName); ElementAppendChild(socketElement, "description", variableNodeSocket.NodeSocketData.Description); ElementAppendChild(socketElement, "isArray", variableNodeSocket.NodeSocketData.IsArray.ToString()); ElementAppendChild(socketElement, "type", VariableTypeHelper.FriendlyName(variableNodeSocket.NodeSocketData.VariableType)); ElementAppendChild(socketElement, "defaultValue", variableNodeSocket.Value.ToString()); } // variable out socket else if (nodeSocket is VariableNodeSocket && nodeSocket.Type == NodeSocketType.VariableOut) { if (variableOutSocketsElement == null) { variableOutSocketsElement = (XmlElement)nodeElement.AppendChild(doc.CreateElement("socketsVariableOut")); } XmlElement socketElement = (XmlElement)variableOutSocketsElement.AppendChild(doc.CreateElement("socket")); VariableNodeSocket variableNodeSocket = nodeSocket as VariableNodeSocket; ElementAppendChild(socketElement, "name", variableNodeSocket.NodeSocketData.Name); ElementAppendChild(socketElement, "realName", variableNodeSocket.NodeSocketData.RealName); ElementAppendChild(socketElement, "description", variableNodeSocket.NodeSocketData.Description); ElementAppendChild(socketElement, "type", VariableTypeHelper.FriendlyName(variableNodeSocket.NodeSocketData.VariableType)); } else { Debug.Assert(true); } } }