private void OnTaskCreatedEvent(TaskCreatedEvent e)
        {
            Repository.States[e.NewTask.Id] = e.NewTask;

            RootTaskId = e.NewTask.Desc.Id;
            NextTaskId = Math.Max(NextTaskId, e.NewTask.Desc.Id) + 1;

            NodeLayoutHelper.Calculate(this);
        }
        private void OnTaskMovedEvent(TaskMovedEvent e)
        {
            var parentTaskDesc = (CompositeTaskDescWrapper)ParentTask.Desc;
            var index          = parentTaskDesc.ChildTaskIds.IndexOf(Desc.Id);
            var moveIndex      = index + e.Offset;

            parentTaskDesc.ChildTaskIds.RemoveAt(index);
            parentTaskDesc.ChildTaskIds.Insert(moveIndex, Desc.Id);

            NodeLayoutHelper.Calculate(Tree);
        }
        public BehaviorTreeView(EditorDomain domain, BehaviorTreeState tree, Vector2 windowSize)
            : base(domain, null, string.Format("{0}-View", tree.Id))
        {
            Tree         = tree;
            Size         = windowSize;
            scrollOffset = new Vector2(windowSize.x / 2, EditorConfiguration.NodeSize.y);

            Children.Add(new BehaviorTreeNode(domain, this));

            NodeLayoutHelper.Calculate(Tree);
        }
        private void OnTaskRemovedEvent(TaskRemovedEvent e)
        {
            var parent = Repository.States[GetId(ParentTaskId)];

            if (parent is BehaviorTreeState)
            {
                var s = (BehaviorTreeState)parent;
                s.RootTaskId = 0;
            }
            else if (parent is TaskState)
            {
                var desc = ((TaskState)parent).Desc;
                if (desc is DecoratorTaskDescWrapper)
                {
                    var d = (DecoratorTaskDescWrapper)desc;
                    d.ChildTaskId = 0;
                }
                else if (desc is CompositeTaskDescWrapper)
                {
                    var d = (CompositeTaskDescWrapper)desc;
                    d.ChildTaskIds.Remove(Desc.Id);
                }
            }

            Repository.States.Remove(Id);

            if (Desc is DecoratorTaskDescWrapper)
            {
                var d = (DecoratorTaskDescWrapper)Desc;
                if (d.ChildTaskId > 0)
                {
                    var taskId = GetId(d.ChildTaskId);
                    Repository.States[taskId].ApplyEvent(new TaskRemovedEvent(taskId));
                }
            }
            else if (Desc is CompositeTaskDescWrapper)
            {
                var d = (CompositeTaskDescWrapper)Desc;

                foreach (var childTaskId in d.ChildTaskIds)
                {
                    var taskId = GetId(childTaskId);
                    Repository.States[taskId].ApplyEvent(new TaskRemovedEvent(taskId));
                }
            }

            NodeLayoutHelper.Calculate(Tree);
        }
        private void OnTaskCreatedEvent(TaskCreatedEvent e)
        {
            Repository.States[e.NewTask.Id] = e.NewTask;

            if (Desc is DecoratorTaskDescWrapper)
            {
                var desc = (DecoratorTaskDescWrapper)Desc;
                desc.ChildTaskId = e.NewTask.Desc.Id;
            }
            else if (Desc is CompositeTaskDescWrapper)
            {
                var desc = (CompositeTaskDescWrapper)Desc;
                desc.ChildTaskIds.Add(e.NewTask.Desc.Id);
            }

            var tree = (BehaviorTreeState)Repository.States[BehaviorTreeState.GetId()];

            tree.NextTaskId = Math.Max(tree.NextTaskId, e.NewTask.Desc.Id) + 1;
            NodeLayoutHelper.Calculate(tree);
        }
        private void OnTaskCollapseChangedEvent(TaskCollapseChangedEvent e)
        {
            IsCollapsed = e.IsCollapsed;

            NodeLayoutHelper.Calculate(Tree);
        }