Esempio n. 1
0
        public BuilderExplorer(
            BuilderPaneWindow paneWindow,
            BuilderViewport viewport,
            BuilderSelection selection,
            BuilderClassDragger classDragger,
            BuilderExplorerDragger explorerDragger,
            BuilderElementContextMenu contextMenuManipulator,
            VisualElement documentElementRoot,
            bool includeDocumentElementRoot,
            HighlightOverlayPainter highlightOverlayPainter,
            string toolbarUxmlPath)
        {
            m_PaneWindow                 = paneWindow;
            m_Viewport                   = viewport;
            m_DocumentElementRoot        = documentElementRoot;
            m_IncludeDocumentElementRoot = includeDocumentElementRoot;
            m_DocumentElement            = viewport.documentRootElement;
            AddToClassList(s_UssClassName);

            m_ClassDragger    = classDragger;
            m_ExplorerDragger = explorerDragger;

            m_SelectionMadeExternally = false;

            m_Selection = selection;

            // Query the UI
            if (!string.IsNullOrEmpty(toolbarUxmlPath))
            {
                var template = BuilderPackageUtilities.LoadAssetAtPath <VisualTreeAsset>(toolbarUxmlPath);
                template.CloneTree(this);
            }

            // Create the Hierarchy View.
            m_ElementHierarchyView = new ElementHierarchyView(
                m_PaneWindow,
                m_DocumentElement,
                selection, classDragger, explorerDragger,
                contextMenuManipulator, ElementSelectionChanged, highlightOverlayPainter);
            m_ElementHierarchyView.style.flexGrow = 1;
            Add(m_ElementHierarchyView);
            // Make sure the Hierarchy View gets focus when the pane gets focused.
            primaryFocusable = m_ElementHierarchyView;

            UpdateHierarchyAndSelection(false);
            m_ShouldRebuildHierarchyOnStyleChange = true;
        }
        public ElementHierarchyView(
            BuilderPaneWindow paneWindow,
            VisualElement documentRootElement,
            BuilderSelection selection,
            BuilderClassDragger classDragger,
            BuilderExplorerDragger explorerDragger,
            BuilderElementContextMenu contextMenuManipulator,
            Action <List <VisualElement> > selectElementCallback,
            HighlightOverlayPainter highlightOverlayPainter)
        {
            m_PaneWindow             = paneWindow;
            m_DocumentRootElement    = documentRootElement;
            m_Selection              = selection;
            m_ClassDragger           = classDragger;
            m_ExplorerDragger        = explorerDragger;
            m_ContextMenuManipulator = contextMenuManipulator;

            this.focusable = true;

            m_SelectElementCallback = selectElementCallback;
            hierarchyHasChanged     = true;
            hasUnsavedChanges       = false;

            m_SearchResultsHightlights = new List <VisualElement>();

            this.RegisterCallback <FocusEvent>(e => m_TreeView?.Focus());

            // HACK: ListView/TreeView need to clear their selections when clicking on nothing.
            this.RegisterCallback <MouseDownEvent>(e =>
            {
                var leafTarget = e.leafTarget as VisualElement;
                if (leafTarget.parent is ScrollView)
                {
                    ClearSelection();
                }
            });

            m_TreeViewHoverOverlay = highlightOverlayPainter;

            m_Container                            = new VisualElement();
            m_Container.name                       = "explorer-container";
            m_Container.style.flexGrow             = 1;
            m_ClassDragger.builderHierarchyRoot    = m_Container;
            m_ExplorerDragger.builderHierarchyRoot = m_Container;
            Add(m_Container);

            m_ClassPillTemplate = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(
                BuilderConstants.UIBuilderPackagePath + "/BuilderClassPill.uxml");

            // Create TreeView.
            m_TreeRootItems = new List <ITreeViewItem>();
            m_TreeView      = new TreeView(m_TreeRootItems, 20, MakeItem, FillItem);
#if UNITY_2020_1_OR_NEWER
            m_TreeView.selectionType = SelectionType.Multiple;
#else
            m_TreeView.selectionType = SelectionType.Single; // ListView/TreeView do not support selecting mutliple items via code.
#endif
            m_TreeView.viewDataKey    = "unity-builder-explorer-tree";
            m_TreeView.style.flexGrow = 1;
#if UNITY_2020_1_OR_NEWER
            m_TreeView.onSelectionChange += OnSelectionChange;
#else
            m_TreeView.onSelectionChanged += OnSelectionChange;
#endif

#if UNITY_2019_3_OR_NEWER
            m_TreeView.RegisterCallback <MouseDownEvent>(OnLeakedMouseClick);
#endif

            m_Container.Add(m_TreeView);

            m_ContextMenuManipulator.RegisterCallbacksOnTarget(m_Container);
        }
        public ElementHierarchyView(
            BuilderPaneWindow paneWindow,
            VisualElement documentRootElement,
            BuilderSelection selection,
            BuilderClassDragger classDragger,
            BuilderExplorerDragger explorerDragger,
            BuilderElementContextMenu contextMenuManipulator,
            Action <List <VisualElement> > selectElementCallback,
            HighlightOverlayPainter highlightOverlayPainter)
        {
            m_PaneWindow             = paneWindow;
            m_DocumentRootElement    = documentRootElement;
            m_Selection              = selection;
            m_ClassDragger           = classDragger;
            m_ExplorerDragger        = explorerDragger;
            m_ContextMenuManipulator = contextMenuManipulator;

            this.focusable = true;

            m_SelectElementCallback = selectElementCallback;
            hierarchyHasChanged     = true;
            hasUnsavedChanges       = false;

            m_SearchResultsHightlights = new List <VisualElement>();

            this.RegisterCallback <FocusEvent>(e => m_TreeView?.Focus());

            // HACK: ListView/TreeView need to clear their selections when clicking on nothing.
            this.RegisterCallback <MouseDownEvent>(e =>
            {
                var leafTarget = e.leafTarget as VisualElement;
                if (leafTarget.parent is ScrollView)
                {
                    m_PaneWindow.primarySelection.ClearSelection(null);
                }
            });

            m_TreeViewHoverOverlay = highlightOverlayPainter;

            m_Container                            = new VisualElement();
            m_Container.name                       = "explorer-container";
            m_Container.style.flexGrow             = 1;
            m_ClassDragger.builderHierarchyRoot    = m_Container;
            m_ExplorerDragger.builderHierarchyRoot = m_Container;
            Add(m_Container);

            m_ClassPillTemplate = BuilderPackageUtilities.LoadAssetAtPath <VisualTreeAsset>(
                BuilderConstants.UIBuilderPackagePath + "/BuilderClassPill.uxml");

            // Create TreeView.
            m_TreeRootItems = new List <TreeViewItem>();
            m_TreeView      = new TreeView(20, MakeItem, FillItem);
            m_TreeView.SetRootItems(m_TreeRootItems);
            m_TreeViewController = m_TreeView.viewController as DefaultTreeViewController <VisualElement>;

            m_TreeView.selectionType            = SelectionType.Multiple;
            m_TreeView.viewDataKey              = "unity-builder-explorer-tree";
            m_TreeView.style.flexGrow           = 1;
            m_TreeView.onSelectedIndicesChange += OnSelectionChange;

            m_TreeView.RegisterCallback <MouseDownEvent>(OnLeakedMouseClick);
            m_Container.Add(m_TreeView);

            m_ContextMenuManipulator.RegisterCallbacksOnTarget(m_Container);
        }