/// <summary>
        /// Opens the tree in a Bonsai Window.
        /// </summary>
        /// <param name="tree">The tree to open</param>
        /// <returns>
        /// The window that opens the tree. Null if already opened.
        /// </returns>
        public static BonsaiWindow OpenTree(BehaviorTree tree, BonsaiEditor.Mode mode = BonsaiEditor.Mode.Edit)
        {
            if (!tree)
            {
                return(null);
            }

            // Try to find an editor window without a canvas...
            var windows = Resources.FindObjectsOfTypeAll <BonsaiWindow>();

            bool isAlreadyOpened = windows.Any(w => w.Tree == tree);

            if (isAlreadyOpened)
            {
                return(null);
            }

            // Find a window without any tree.
            BonsaiWindow window = windows.FirstOrDefault(w => w.Tree == null);

            // No windows available, make a new one.
            if (!window)
            {
                window = CreateInstance <BonsaiWindow>();
                window.Show();
            }

            window.SetTree(tree, mode);
            return(window);
        }
        private void OpenIncludedSubTree(RunSubtree include)
        {
            if (include.RunningSubTree)
            {
                BonsaiWindow.OpenTree(include.RunningSubTree, Mode.View);
            }

            // Shortcut to open include tree from Include node.
            // Only works in edit mode. View mode is reserved for running tree instances.
            else if (include.subtreeAsset && IsEditMode)
            {
                BonsaiWindow.OpenTree(include.subtreeAsset, Mode.Edit);
            }
        }
        static bool OpenCanvasAsset(int instanceID, int line)
        {
            var          tree = EditorUtility.InstanceIDToObject(instanceID) as BehaviorTree;
            BonsaiWindow w    = OpenTree(tree);

            if (w)
            {
                // If a tree asset was created but has no blackboard, add one upon opening.
                // This is for convenience.
                BonsaiSaver.AddBlackboardIfMissing(tree);

                w.SwitchToViewModeIfRequired();
            }

            return(w);
        }
Exemple #4
0
        protected virtual void OnEnable()
        {
            var edited = target as BTNode;

            nodeTitle   = serializedObject.FindProperty("title");
            nodeComment = serializedObject.FindProperty("comment");

            // Find the the editor window with the tree associated with this behaviour.
            if (ParentWindow == null)
            {
                ParentWindow = Resources.FindObjectsOfTypeAll <BonsaiWindow>().First(w => w.ContainsNode(edited));

                if (ParentWindow.EditorMode == BonsaiEditor.Mode.View)
                {
                    runtimeHeading = new GUIContent("Runtime values");
                    runtimeFields  = GetRuntimeFields(edited);
                }
            }
        }
        private void SwitchToViewModeIfRequired()
        {
            // Cannot go to view mode.
            if (!EditorApplication.isPlaying || !Selection.activeGameObject)
            {
                return;
            }

            var          btc        = Selection.activeGameObject.GetComponent <BTComponent>();
            BehaviorTree treeToView = btc ? btc.Tree : null;

            // There must be a non-null tree to view,
            // it must be a different tree than the active tree for this window,
            // and must not be opened somewhere else.
            if (treeToView && Tree != treeToView)
            {
                var windows = Resources.FindObjectsOfTypeAll <BonsaiWindow>();

                bool alreadyInView = windows.Any(w => w.Tree == treeToView);

                if (alreadyInView)
                {
                    return;
                }

                BonsaiWindow window = windows.FirstOrDefault(w => !w.Tree);

                // Have the window without a set tree to view the tree selected.
                if (window)
                {
                    window.SetTree(treeToView, BonsaiEditor.Mode.View);
                }
                else
                {
                    // View tree in this window.
                    SetTree(treeToView, BonsaiEditor.Mode.View);
                }
            }
        }