Ejemplo n.º 1
0
        /// <summary>
        /// Spawns the node.
        /// </summary>
        /// <param name="groupArchetype">The group archetype.</param>
        /// <param name="nodeArchetype">The node archetype.</param>
        /// <param name="location">The location.</param>
        /// <param name="customValues">The custom values array. Must match node archetype <see cref="NodeArchetype.DefaultValues"/> size. Pass null to use default values.</param>
        /// <param name="beforeSpawned">The custom callback action to call after node creation but just before invoking spawn event. Can be used to initialize custom node data.</param>
        /// <returns>Created node.</returns>
        public SurfaceNode SpawnNode(GroupArchetype groupArchetype, NodeArchetype nodeArchetype, Float2 location, object[] customValues = null, Action <SurfaceNode> beforeSpawned = null)
        {
            if (groupArchetype == null || nodeArchetype == null)
            {
                throw new ArgumentNullException();
            }

            // Check if cannot use that node in this surface type (ignore NoSpawnViaGUI)
            var flags = nodeArchetype.Flags;

            nodeArchetype.Flags &= ~NodeFlags.NoSpawnViaGUI;
            nodeArchetype.Flags &= ~NodeFlags.NoSpawnViaPaste;
            if (_surface != null && !_surface.CanUseNodeType(nodeArchetype))
            {
                nodeArchetype.Flags = flags;
                Editor.LogWarning("Cannot spawn given node type. Title: " + nodeArchetype.Title);
                return(null);
            }
            nodeArchetype.Flags = flags;

            var id = GetFreeNodeID();

            // Create node
            var node = NodeFactory.CreateNode(id, this, groupArchetype, nodeArchetype);

            if (node == null)
            {
                Editor.LogWarning("Failed to create node.");
                return(null);
            }
            Nodes.Add(node);

            // Initialize
            if (customValues != null)
            {
                if (node.Values != null && node.Values.Length == customValues.Length)
                {
                    Array.Copy(customValues, node.Values, customValues.Length);
                }
                else
                {
                    throw new InvalidOperationException("Invalid node custom values.");
                }
            }
            node.Location = location;
            OnControlLoaded(node);
            beforeSpawned?.Invoke(node);
            node.OnSurfaceLoaded();
            OnControlSpawned(node);

            // Undo action
            if (Surface != null && Surface.Undo != null)
            {
                Surface.Undo.AddAction(new AddRemoveNodeAction(node, true));
            }

            MarkAsModified();

            return(node);
        }