/// <summary>
 /// Initializes a new instance of the <see cref="VariableNodeSocket"/> class.
 /// </summary>
 /// <param name="node">The node where the script node socket will be used.</param>
 /// <param name="nodeSocketData">The node socket data of the script node socket.</param>
 /// <param name="value">Default value of the script variable node socket.</param>
 public VariableNodeSocket(Node node, NodeSocketData nodeSocketData, IVariable value)
     : base(node, nodeSocketData)
 {
     _value       = value;
     _visible     = nodeSocketData.Visible;
     _connections = new List <Variable>();
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="VariableNodeSocket"/> class.
        /// </summary>
        /// <param name="node">The node where the script node socket will be used.</param>
        /// <param name="nodeSocketData">The node socket data of the script node socket.</param>
        public VariableNodeSocket(Node node, NodeSocketData nodeSocketData)
            : base(node, nodeSocketData)
        {
            _value       = VarFactory.Create(nodeSocketData.VariableType);
            _visible     = nodeSocketData.Visible;
            _connections = new List <Variable>();

            // set default value
            if (NodeSocketData.DefaultValue != null)
            {
                Value.SetValue(NodeSocketData.DefaultValue);
            }
        }
        /// <summary>
        /// Deserialize object.
        /// </summary>
        /// <param name="info">The <see cref="System.Runtime.Serialization.SerializationInfo"/> to retrieve data.</param>
        /// <param name="ctxt">The source (see <see cref="System.Runtime.Serialization.StreamingContext"/>) for this deserialization.</param>
        protected NodeSocket(SerializationInfo info, StreamingContext ctxt)
        {
            _node = (Node)info.GetValue("Node", typeof(Node));

            string         nodeSocketDataRealName = info.GetString("NodeSocketDataRealName");
            NodeSocketType nodeSocketDataType     = (NodeSocketType)info.GetValue("NodeSocketDataType", typeof(NodeSocketType));

            foreach (NodeSocketData nodeSocketData in Node.NodeData.Sockets)
            {
                if (nodeSocketData.RealName == nodeSocketDataRealName && nodeSocketData.Type == nodeSocketDataType)
                {
                    _nodeSocketData = nodeSocketData;
                    break;
                }
            }

            Debug.Assert(_nodeSocketData != null, "Cannot find correct node socket data.");
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="NodeSocket"/> class.
 /// </summary>
 /// <param name="node">The node where the script node socket will be used.</param>
 /// <param name="nodeSocketData">The node socket data of the script node socket.</param>
 protected NodeSocket(Node node, NodeSocketData nodeSocketData)
 {
     _nodeSocketData = nodeSocketData;
     _node           = node;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SignalNodeSocket"/> class.
 /// </summary>
 /// <param name="node">The node where the script node socket will be used.</param>
 /// <param name="nodeSocketData">The node socket data of the script node socket.</param>
 public SignalNodeSocket(Node node, NodeSocketData nodeSocketData)
     : base(node, nodeSocketData)
 {
     _connections = new List <SignalNodeSocket>();
 }
Beispiel #6
0
        /// <summary>
        /// Loads all scripting data and stores them sorted at the tree structure.
        /// </summary>
        private void CreateRoot()
        {
            root = new CategoryData();

            bool isAction, isEvent;

            foreach (Type type in typeof(GameEngine.Scripting.ScriptNode).Assembly.GetTypes())
            {
                isAction = isEvent = false;

                if (type.IsSubclassOf(typeof(GameEngine.Scripting.Actions.ActionNode)))
                {
                    isAction = true;
                }
                else if (type.IsSubclassOf(typeof(GameEngine.Scripting.Events.EventNode)))
                {
                    isEvent = true;
                }

                // get all subclasses from ActionNode or EventNode
                if ((isAction || isEvent) && !type.IsAbstract && type.IsPublic)
                {
                    // script node (event or action)
                    NodeData scriptNode = new NodeData();

                    scriptNode.Name        = GetFriendlyName(type, type.Name);
                    scriptNode.RealName    = type.FullName;
                    scriptNode.Description = GetDescription(type);
                    scriptNode.Type        = isAction ? NodeType.Action : NodeType.Event;

                    // add script node to the correct category
                    CreateCategory(root, GetCategory(type)).Items.Add(scriptNode);

                    // get all public non-static fields from class
                    foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
                    {
                        // variable socket is an array of generic class GameEngine.Variable<T>
                        // or generic variable GameEngine.Variable<T>
                        if ((fieldInfo.FieldType.IsArray && fieldInfo.FieldType.HasElementType && fieldInfo.FieldType.GetElementType().IsGenericType&&
                             fieldInfo.FieldType.GetElementType().GetGenericTypeDefinition() == typeof(GameEngine.Scripting.Variable <>)) ||
                            (fieldInfo.FieldType.IsGenericType && fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(GameEngine.Scripting.Variable <>)))
                        {
                            // get argument for GameEngine.Variable<T>
                            Type[] arguments = fieldInfo.FieldType.IsArray ? fieldInfo.FieldType.GetElementType().GetGenericArguments() : fieldInfo.FieldType.GetGenericArguments();

                            if (arguments != null && arguments.Length == 1)
                            {
                                // try to get VariableSocketTypeAttribute
                                GameEngine.Scripting.VariableSocketAttribute variableSocketAttribute = (GameEngine.Scripting.VariableSocketAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(GameEngine.Scripting.VariableSocketAttribute));

                                if (variableSocketAttribute != null)
                                {
                                    // variable socket
                                    NodeSocketData variableSocket = new NodeSocketData();

                                    variableSocket.Name         = GetFriendlyName(fieldInfo, fieldInfo.Name);
                                    variableSocket.RealName     = fieldInfo.Name;
                                    variableSocket.Description  = GetDescription(fieldInfo);
                                    variableSocket.Type         = variableSocketAttribute.Type == GameEngine.Scripting.VariableSocketType.In ? NodeSocketType.VariableIn : NodeSocketType.VariableOut;
                                    variableSocket.VariableType = VariableTypeHelper.FromType(arguments[0]);
                                    variableSocket.IsArray      = fieldInfo.FieldType.IsArray;
                                    variableSocket.DefaultValue = GetDefaultValue(fieldInfo);
                                    variableSocket.Visible      = variableSocketAttribute.Visible;
                                    variableSocket.CanBeEmpty   = variableSocketAttribute.CanBeEmpty;

                                    // variable out socket must be an array
                                    if (variableSocket.Type == NodeSocketType.VariableOut && !variableSocket.IsArray)
                                    {
                                        continue;
                                    }

                                    scriptNode.Sockets.Add(variableSocket);
                                }
                            }
                        }
                        // signal out socket is ScriptSocketHandler delegate
                        else if (fieldInfo.FieldType.IsSubclassOf(typeof(Delegate)) && fieldInfo.FieldType == typeof(GameEngine.Scripting.ScriptSocketHandler))
                        {
                            // signal out socket
                            NodeSocketData signalOutSocket = new NodeSocketData();

                            signalOutSocket.Name        = GetFriendlyName(fieldInfo, fieldInfo.Name);
                            signalOutSocket.RealName    = fieldInfo.Name;
                            signalOutSocket.Description = GetDescription(fieldInfo);
                            signalOutSocket.Type        = NodeSocketType.SignalOut;

                            scriptNode.Sockets.Add(signalOutSocket);
                        }
                    }

                    // get all public non-static method from class
                    foreach (MethodInfo methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
                    {
                        // signal in socket is method with none parameter, returns void,
                        // is defined in script node (event or action, not base classes) and it is not Update and Connect method (from event)
                        if (methodInfo.GetParameters().Length == 0 && methodInfo.ReturnType == typeof(void) && methodInfo.Name != "Update" && methodInfo.Name != "Connect")
                        {
                            // signal in socket
                            NodeSocketData signalInSocket = new NodeSocketData();

                            signalInSocket.Name        = GetFriendlyName(methodInfo, methodInfo.Name);
                            signalInSocket.RealName    = methodInfo.Name;
                            signalInSocket.Description = GetDescription(methodInfo);
                            signalInSocket.Type        = NodeSocketType.SignalIn;

                            scriptNode.Sockets.Add(signalInSocket);
                        }
                    }
                }
            }

            SortCategory(root);
        }