void ReselectIfNecessary(VisualElement documentElement)
 {
     if (!m_Selection.selection.Contains(documentElement))
     {
         m_Selection.Select(null, documentElement);
     }
 }
        void OnClassPillDoubleClick(EventBase evt)
        {
            var pill            = evt.currentTarget as VisualElement;
            var className       = pill.userData as string;
            var selectorString  = BuilderConstants.UssSelectorClassNameSymbol + className;
            var selectorElement = pill.GetProperty(BuilderConstants.InspectorClassPillLinkedSelectorElementVEPropertyName) as VisualElement;

            if (selectorElement == null)
            {
                // Get StyleSheet.
                var mainStyleSheet = m_PaneWindow.document.firstStyleSheet;
                if (mainStyleSheet == null)
                {
                    return;
                }

                var selectorsRootElement = BuilderSharedStyles.GetSelectorContainerElement(m_Selection.documentElement);
                BuilderSharedStyles.CreateNewSelector(selectorsRootElement, mainStyleSheet, selectorString);

                m_Selection.NotifyOfStylingChange();
                m_Selection.NotifyOfHierarchyChange();
            }
            else
            {
                m_Selection.Select(null, selectorElement);
            }
        }
        protected void AddItemToTheDocument(BuilderLibraryTreeItem item)
        {
            // If this is the uxml file entry of the currently open file, don't allow
            // the user to instantiate it (infinite recursion) or re-open it.
            var  listOfOpenDocuments   = m_PaneWindow.document.openUXMLFiles;
            bool isCurrentDocumentOpen = listOfOpenDocuments.Any(doc => doc.uxmlFileName == item.name);

            if (isCurrentDocumentOpen)
            {
                return;
            }

            if (m_PaneWindow.document.WillCauseCircularDependency(item.sourceAsset))
            {
                BuilderDialogsUtility.DisplayDialog(BuilderConstants.InvalidWouldCauseCircularDependencyMessage,
                                                    BuilderConstants.InvalidWouldCauseCircularDependencyMessageDescription, null);
                return;
            }

            var newElement = item.makeVisualElementCallback?.Invoke();

            if (newElement == null)
            {
                return;
            }

            if (item.makeElementAssetCallback != null && newElement is TemplateContainer tempContainer)
            {
                if (!BuilderAssetUtilities.ValidateAsset(item.sourceAsset, item.sourceAssetPath))
                {
                    return;
                }
            }

            var activeVTARootElement = m_DocumentRootElement.Query().Where(e => e.GetVisualTreeAsset() == m_PaneWindow.document.visualTreeAsset).First();

            if (activeVTARootElement == null)
            {
                Debug.LogError("UI Builder has a bug. Could not find document root element for currently active open UXML document.");
                return;
            }
            activeVTARootElement.Add(newElement);

            if (item.makeElementAssetCallback == null)
            {
                BuilderAssetUtilities.AddElementToAsset(m_PaneWindow.document, newElement);
            }
            else
            {
                BuilderAssetUtilities.AddElementToAsset(
                    m_PaneWindow.document, newElement, item.makeElementAssetCallback);
            }

            m_Selection.NotifyOfHierarchyChange();
            m_Selection.Select(null, newElement);
        }
Exemplo n.º 4
0
        void OnItemChosen(ITreeViewItem selectedItem)
#endif
        {
#if UNITY_2020_1_OR_NEWER
            if (selectedItems == null || selectedItems.Count() == 0)
            {
                return;
            }

            var selectedItem = selectedItems.First();
#else
            if (selectedItem == null)
            {
                return;
            }
#endif

            var item = selectedItem as LibraryTreeItem;
            if (item.makeVisualElement == null)
            {
                return;
            }

            // If this is the uxml file entry of the currently open file, don't allow
            // the user to instantiate it (infinite recursion) or re-open it.
            if (item.name == m_PaneWindow.document.uxmlFileName)
            {
                return;
            }

            var newElement = item.makeVisualElement();
            if (newElement == null)
            {
                return;
            }

            m_DocumentElement.Add(newElement);

            if (item.makeElementAsset == null)
            {
                BuilderAssetUtilities.AddElementToAsset(m_PaneWindow.document, newElement);
            }
            else
            {
                BuilderAssetUtilities.AddElementToAsset(
                    m_PaneWindow.document, newElement, item.makeElementAsset);
            }

            // TODO: ListView bug. Does not refresh selection pseudo states after a
            // call to Refresh().
            m_Selection.NotifyOfHierarchyChange(null);
            this.schedule.Execute(() =>
            {
                m_Selection.Select(null, newElement);
            }).ExecuteLater(200);
        }
Exemplo n.º 5
0
        public void Paste()
        {
            var copyBuffer = BuilderEditorUtility.SystemCopyBuffer;

            if (string.IsNullOrEmpty(copyBuffer))
            {
                return;
            }

            var trimmedBuffer = copyBuffer.Trim();

            if (trimmedBuffer.StartsWith("<") && trimmedBuffer.EndsWith(">"))
            {
                PasteUXML(copyBuffer);
            }
            else if (trimmedBuffer.EndsWith("}"))
            {
                PasteUSS(copyBuffer);
            }
            else // Unknown string.
            {
                return;
            }

            if (m_CutElement != null)
            {
                DeleteElement(m_CutElement);
                m_CutElement = null;
                BuilderEditorUtility.SystemCopyBuffer = null;
            }

            m_PaneWindow.OnEnableAfterAllSerialization();

            // TODO: ListView bug. Does not refresh selection pseudo states after a
            // call to Refresh().
            m_PaneWindow.rootVisualElement.schedule.Execute(() =>
            {
                var currentlySelectedItem = m_Selection.selection.FirstOrDefault();
                if (currentlySelectedItem != null)
                {
                    m_Selection.Select(null, currentlySelectedItem);
                }
            }).ExecuteLater(200);

            m_PaneWindow.document.hasUnsavedChanges = true;
        }
Exemplo n.º 6
0
        protected void AddItemToTheDocument(BuilderLibraryTreeItem item)
        {
            // If this is the uxml file entry of the currently open file, don't allow
            // the user to instantiate it (infinite recursion) or re-open it.
            var  listOfOpenDocuments   = m_PaneWindow.document.openUXMLFiles;
            bool isCurrentDocumentOpen = listOfOpenDocuments.Any(doc => doc.uxmlFileName == item.name);

            if (isCurrentDocumentOpen)
            {
                return;
            }

            var newElement = item.makeVisualElementCallback?.Invoke();

            if (newElement == null)
            {
                return;
            }

            var activeVTARootElement = m_DocumentRootElement.Query().Where(e => e.GetVisualTreeAsset() == m_PaneWindow.document.visualTreeAsset).First();

            if (activeVTARootElement == null)
            {
                Debug.LogError("UI Builder has a bug. Could not find document root element for currently active open UXML document.");
                return;
            }
            activeVTARootElement.Add(newElement);

            if (item.makeElementAssetCallback == null)
            {
                BuilderAssetUtilities.AddElementToAsset(m_PaneWindow.document, newElement);
            }
            else
            {
                BuilderAssetUtilities.AddElementToAsset(
                    m_PaneWindow.document, newElement, item.makeElementAssetCallback);
            }

            // TODO: ListView bug. Does not refresh selection pseudo states after a
            // call to Refresh().
            m_Selection.NotifyOfHierarchyChange();
            schedule.Execute(() =>
            {
                m_Selection.Select(null, newElement);
            }).ExecuteLater(200);
        }
Exemplo n.º 7
0
        void OnPick(MouseDownEvent evt)
        {
            var pickedElement = PickElement(evt.mousePosition);

            if (pickedElement != null)
            {
                SetInnerSelection(pickedElement);
                m_Selection.Select(this, pickedElement);
            }
            else
            {
                ClearInnerSelection();
                m_Selection.ClearSelection(this);
            }

            evt.StopPropagation();
        }
Exemplo n.º 8
0
        void SelectItemOnSingleClick(MouseUpEvent evt)
        {
            // TODO: ListView right now does not allow selecting a single
            // item that is already part of multi-selection, and having
            // only that item selected. Clicking on any already-selected
            // item in ListView does nothing. This needs to be fixed in trunk.
            //
            // In the meantime, we use this leaked mouse click hack, which
            // also accounts for another bug in ListView, to catch these
            // unhandled selection events and do the single-item selection
            // ourselves.
            //
            // See: https://unity3d.atlassian.net/browse/UIT-1011

            if (m_Selection.selectionCount <= 1)
            {
                return;
            }

            if (evt.modifiers.HasFlag(EventModifiers.Control) ||
                evt.modifiers.HasFlag(EventModifiers.Shift) ||
                evt.modifiers.HasFlag(EventModifiers.Command))
            {
                return;
            }

            var element  = evt.target as VisualElement;
            var ancestor = element is BuilderExplorerItem ? element as BuilderExplorerItem : element?.GetFirstAncestorOfType <BuilderExplorerItem>();

            if (ancestor == null)
            {
                return;
            }

            var documentElement = ancestor.GetProperty(BuilderConstants.ElementLinkedDocumentVisualElementVEPropertyName) as VisualElement;

            if (documentElement == null)
            {
                return;
            }

            m_Selection.Select(null, documentElement);
        }
Exemplo n.º 9
0
        void OnClassPillDoubleClick(EventBase evt)
        {
            var pill             = evt.target as VisualElement;
            var pillDeleteButton = pill.Q <Button>("delete-class-button");
            var className        = pillDeleteButton.userData as string;
            var selectorString   = "." + className;
            var selectorElement  = pill.GetProperty(BuilderConstants.InspectorClassPillLinkedSelectorElementVEPropertyName) as VisualElement;

            if (selectorElement == null)
            {
                var selectorsRootElement = BuilderSharedStyles.GetSelectorContainerElement(m_Selection.documentElement);
                var mainStyleSheet       = m_PaneWindow.document.mainStyleSheet;
                BuilderSharedStyles.CreateNewSelector(selectorsRootElement, mainStyleSheet, selectorString);

                m_Selection.NotifyOfStylingChange();
                m_Selection.NotifyOfHierarchyChange();
            }
            else
            {
                m_Selection.Select(null, selectorElement);
            }
        }
Exemplo n.º 10
0
        protected void AddItemToTheDocument(BuilderLibraryTreeItem item)
        {
            // If this is the uxml file entry of the currently open file, don't allow
            // the user to instantiate it (infinite recursion) or re-open it.
            var  listOfOpenDocuments   = m_PaneWindow.document.openUXMLFiles;
            bool isCurrentDocumentOpen = listOfOpenDocuments.Any(doc => doc.uxmlFileName == item.Name);

            if (isCurrentDocumentOpen)
            {
                return;
            }

            var newElement = item.MakeVisualElementCallback?.Invoke();

            if (newElement == null)
            {
                return;
            }

            m_DocumentElement.Add(newElement);

            if (item.MakeElementAssetCallback == null)
            {
                BuilderAssetUtilities.AddElementToAsset(m_PaneWindow.document, newElement);
            }
            else
            {
                BuilderAssetUtilities.AddElementToAsset(
                    m_PaneWindow.document, newElement, item.MakeElementAssetCallback);
            }

            // TODO: ListView bug. Does not refresh selection pseudo states after a
            // call to Refresh().
            m_Selection.NotifyOfHierarchyChange();
            schedule.Execute(() =>
            {
                m_Selection.Select(null, newElement);
            }).ExecuteLater(200);
        }
Exemplo n.º 11
0
        void ElementSelected(VisualElement element)
        {
            if (m_SelectionMadeExternally)
            {
                return;
            }

            if (element == null)
            {
                m_Selection.ClearSelection(this);
                return;
            }
            else if (element.ClassListContains(BuilderConstants.ExplorerItemUnselectableClassName))
            {
                m_SelectionMadeExternally = true;
                m_ElementHierarchyView.ClearSelection();
                m_SelectionMadeExternally = false;
                m_Selection.ClearSelection(this);
                return;
            }

            m_Selection.Select(this, element);
        }
        void OnPick(MouseDownEvent evt)
        {
            // Do not prevent zoom and pan
            if (evt.button == 2 || (evt.ctrlKey && evt.altKey || (evt.button == (int)MouseButton.RightMouse && evt.altKey)))
            {
                return;
            }

            var pickedElement = PickElement(evt.mousePosition);

            if (pickedElement != null)
            {
                SetInnerSelection(pickedElement);
                m_Selection.Select(this, pickedElement);
            }
            else
            {
                ClearInnerSelection();
                m_Selection.ClearSelection(this);
            }

            if (evt.button == (int)MouseButton.RightMouse)
            {
                if (pickedElement != null && m_ContextMenuManipulator != null)
                {
                    pickedElement.SetProperty(BuilderConstants.ElementLinkedDocumentVisualElementVEPropertyName, pickedElement);
                    m_ContextMenuManipulator.RegisterCallbacksOnTarget(pickedElement);
                    m_ContextMenuManipulator.DisplayContextMenu(evt, pickedElement);
                    evt.StopPropagation();
                }
            }
            else
            {
                evt.StopPropagation();
            }
        }
Exemplo n.º 13
0
 void OnCanvasHeaderClick(EventBase obj)
 {
     m_Selection.Select(null, documentElement);
 }
        public void BuildElementContextualMenu(ContextualMenuPopulateEvent evt, VisualElement target)
        {
            var documentElement = target.GetProperty(BuilderConstants.ElementLinkedDocumentVisualElementVEPropertyName) as VisualElement;

            var isValidTarget = documentElement != null && (documentElement.IsPartOfCurrentDocument() || documentElement.GetStyleComplexSelector() != null);

            if (isValidTarget)
            {
                evt.StopImmediatePropagation();
            }

            evt.menu.AppendAction(
                "Copy",
                a =>
            {
                m_Selection.Select(null, documentElement);
                if (documentElement.IsPartOfCurrentDocument() || documentElement.GetStyleComplexSelector() != null)
                {
                    m_PaneWindow.commandHandler.PerformActionOnSelection(
                        m_PaneWindow.commandHandler.CopyElement,
                        m_PaneWindow.commandHandler.ClearCopyBuffer);
                }
            },
                isValidTarget
                    ? DropdownMenuAction.Status.Normal
                    : DropdownMenuAction.Status.Disabled);

            evt.menu.AppendAction(
                "Paste",
                a =>
            {
                m_Selection.Select(null, documentElement);
                m_PaneWindow.commandHandler.Paste();
            },
                string.IsNullOrEmpty(EditorGUIUtility.systemCopyBuffer)
                    ? DropdownMenuAction.Status.Disabled
                    : DropdownMenuAction.Status.Normal);

            evt.menu.AppendSeparator();

            evt.menu.AppendAction(
                "Rename",
                a =>
            {
                m_Selection.Select(null, documentElement);
                var explorerItemElement = documentElement.GetProperty(BuilderConstants.ElementLinkedExplorerItemVEPropertyName) as BuilderExplorerItem;
                if (explorerItemElement == null)
                {
                    return;
                }

                explorerItemElement.ActivateRenameElementMode();
            },
                documentElement != null && documentElement.IsPartOfCurrentDocument()
                    ? DropdownMenuAction.Status.Normal
                    : DropdownMenuAction.Status.Disabled);

            evt.menu.AppendAction(
                "Duplicate",
                a =>
            {
                m_Selection.Select(null, documentElement);
                if (documentElement.IsPartOfCurrentDocument() || documentElement.GetStyleComplexSelector() != null)
                {
                    m_PaneWindow.commandHandler.PerformActionOnSelection(
                        m_PaneWindow.commandHandler.DuplicateElement,
                        m_PaneWindow.commandHandler.ClearCopyBuffer,
                        m_PaneWindow.commandHandler.Paste);
                }
            },
                isValidTarget
                    ? DropdownMenuAction.Status.Normal
                    : DropdownMenuAction.Status.Disabled);

            evt.menu.AppendSeparator();

            evt.menu.AppendAction(
                "Delete",
                a =>
                { m_Selection.Select(null, documentElement);
                  m_PaneWindow.commandHandler.DeleteElement(documentElement);
                  m_PaneWindow.commandHandler.ClearSelectionNotify(); },
                isValidTarget
                    ? DropdownMenuAction.Status.Normal
                    : DropdownMenuAction.Status.Disabled);
        }
Exemplo n.º 15
0
        public void CreateTemplateFromHierarchy(VisualElement ve, VisualTreeAsset vta, string path = "")
        {
            var veas = new List <VisualElementAsset>();
            var vea  = ve.GetVisualElementAsset();

            veas.Add(vea);

            if (string.IsNullOrEmpty(path))
            {
                path = BuilderDialogsUtility.DisplaySaveFileDialog("Save UXML", null, ve.name, "uxml");

                if (string.IsNullOrEmpty(path))
                {
                    // Save dialog cancelled
                    return;
                }
            }

            if (path == m_PaneWindow.document.activeOpenUXMLFile.uxmlPath)
            {
                // Path is the same as the active open uxml file. Abort!
                BuilderDialogsUtility.DisplayDialog(
                    BuilderConstants.InvalidCreateTemplatePathTitle,
                    BuilderConstants.InvalidCreateTemplatePathMessage,
                    BuilderConstants.DialogOkOption);

                return;
            }

            var uxml = VisualTreeAssetToUXML.GenerateUXML(vta, null, veas);

            if (!m_PaneWindow.document.SaveNewTemplateFileFromHierarchy(path, uxml))
            {
                // New template wasn't saved
                return;
            }

            var parent    = ve.parent;
            var parentVEA = parent.GetVisualElementAsset();
            var index     = parent.IndexOf(ve);

            // Delete old element
            BuilderAssetUtilities.DeleteElementFromAsset(m_PaneWindow.document, ve);
            ve.RemoveFromHierarchy();

            // Replace with new template
            var newTemplateVTA       = EditorGUIUtility.Load(path) as VisualTreeAsset;
            var newTemplateContainer = newTemplateVTA.CloneTree();

            newTemplateContainer.SetProperty(BuilderConstants.LibraryItemLinkedTemplateContainerPathVEPropertyName, path);
            newTemplateContainer.name = newTemplateVTA.name;

            parent.Insert(index, newTemplateContainer);

            BuilderAssetUtilities.AddElementToAsset(m_PaneWindow.document, newTemplateContainer, (inVta, inParent, ve) =>
            {
                var vea = inVta.AddTemplateInstance(inParent, path) as VisualElementAsset;
                vea.AddProperty("name", newTemplateVTA.name);
                ve.SetProperty(BuilderConstants.ElementLinkedInstancedVisualTreeAssetVEPropertyName, newTemplateVTA);
                return(vea);
            }, index);

            m_Selection.Select(null, newTemplateContainer);

            // Refresh
            m_Selection.NotifyOfHierarchyChange();
            m_PaneWindow.OnEnableAfterAllSerialization();
        }