예제 #1
0
        public void OnNodeSwitchType(BTEditorGraphNode target, Type newType)
        {
            if (target == null || newType == null)
            {
                return;
            }

            BTEditorGraphNode parentNode  = target.Parent;
            Vector2           oldPosition = target.NodePositon;
            int oldIndex = target.Parent.GetChildIndex(target);

            BehaviourNode node = BTUtils.CreateNode(newType);

            if (node != null)
            {
                if (node is Decorator)
                {
                    Decorator original  = target.Node as Decorator;
                    Decorator decorator = node as Decorator;

                    decorator.SetChild(original.GetChild());
                }
                else if (node is Composite)
                {
                    Composite original  = target.Node as Composite;
                    Composite composite = node as Composite;

                    for (int i = 0; i < original.ChildCount; i++)
                    {
                        composite.AddChild(original.GetChild(i));
                    }
                }

                BTUndoSystem.BeginUndoGroup("Changed node type");
                BTUndoSystem.RegisterUndo(new UndoNodeDeleted(target));
                target.OnDelete();

                BTEditorGraphNode newNode = parentNode.OnInsertChild(oldIndex, node);
                if (newNode != null)
                {
                    newNode.NodePositon = oldPosition;
                    BTUndoSystem.RegisterUndo(new UndoNodeCreated(newNode));
                }

                BTUndoSystem.EndUndoGroup();
            }
        }