예제 #1
0
        void PasteUSS(string copyBuffer)
        {
            // Paste does nothing if document has no stylesheets.
            var mainStyleSheet = m_PaneWindow.document.activeStyleSheet;

            if (mainStyleSheet == null)
            {
                return;
            }

            var pasteStyleSheet = StyleSheetUtilities.CreateInstance();
            var importer        = new BuilderStyleSheetImporter(); // Cannot be cached because the StyleBuilder never gets reset.

            importer.Import(pasteStyleSheet, copyBuffer);

            // Select all pasted selectors.
            m_Selection.ClearSelection(null);
            foreach (var selector in pasteStyleSheet.complexSelectors)
            {
                BuilderAssetUtilities.AddStyleComplexSelectorToSelection(pasteStyleSheet, selector);
            }

            BuilderAssetUtilities.TransferAssetToAsset(m_PaneWindow.document, mainStyleSheet, pasteStyleSheet);

            ScriptableObject.DestroyImmediate(pasteStyleSheet);
        }
예제 #2
0
        void PasteUXML(string copyBuffer)
        {
            var importer = new BuilderVisualTreeAssetImporter(); // Cannot be cached because the StyleBuilder never gets reset.

            importer.ImportXmlFromString(copyBuffer, out var pasteVta);

            VisualElementAsset parent = null;

            if (!m_Selection.isEmpty)
            {
                parent = m_Selection.selection.First().parent?.GetVisualElementAsset();
            }

            BuilderAssetUtilities.TransferAssetToAsset(m_PaneWindow.document, parent, pasteVta);
            m_PaneWindow.document.AddStyleSheetToAllRootElements();

            var selectionParentId = parent?.id ?? m_PaneWindow.document.visualTreeAsset.GetRootUXMLElementId();
            VisualElementAsset newSelectedItem = pasteVta.templateAssets.FirstOrDefault(tpl => tpl.parentId == selectionParentId);

            if (newSelectedItem == null)
            {
                newSelectedItem = pasteVta.visualElementAssets.FirstOrDefault(asset => asset.parentId == selectionParentId);
            }

            m_Selection.ClearSelection(null);
            newSelectedItem.Select();

            ScriptableObject.DestroyImmediate(pasteVta);
        }
예제 #3
0
        void PasteUXML(string copyBuffer)
        {
            var importer = new BuilderVisualTreeAssetImporter(); // Cannot be cached because the StyleBuilder never gets reset.

            importer.ImportXmlFromString(copyBuffer, out var pasteVta);

            VisualElementAsset parent = null;

            if (!m_Selection.isEmpty)
            {
                parent = m_Selection.selection.First().parent?.GetVisualElementAsset();
                m_Selection.ClearSelection(null);
            }

            // Select all pasted elements.
            foreach (var templateAsset in pasteVta.templateAssets)
            {
                if (pasteVta.IsRootElement(templateAsset))
                {
                    templateAsset.Select();
                }
            }
            foreach (var vea in pasteVta.visualElementAssets)
            {
                if (pasteVta.IsRootElement(vea))
                {
                    vea.Select();
                }
            }

            BuilderAssetUtilities.TransferAssetToAsset(m_PaneWindow.document, parent, pasteVta);
            m_PaneWindow.document.AddStyleSheetsToAllRootElements();

            ScriptableObject.DestroyImmediate(pasteVta);
        }
        void PasteUSS(string copyBuffer)
        {
            var pasteStyleSheet = StyleSheetUtilities.CreateInstance();
            var importer        = new BuilderStyleSheetImporter(); // Cannot be cached because the StyleBuilder never gets reset.

            importer.Import(pasteStyleSheet, copyBuffer);

            BuilderAssetUtilities.TransferAssetToAsset(m_PaneWindow.document, pasteStyleSheet);

            ScriptableObject.DestroyImmediate(pasteStyleSheet);
        }
예제 #5
0
        void PasteUXML(string copyBuffer)
        {
            var importer = new BuilderVisualTreeAssetImporter(); // Cannot be cached because the StyleBuilder never gets reset.

            importer.ImportXmlFromString(copyBuffer, out var pasteVta);

            /* If the current parent element is linked to a VisualTreeAsset, it could mean
             * that our parent is the TemplateContainer belonging to our parent document and the
             * current open document is a sub-document opened in-place. In such a case, we don't
             * want to use our parent's VisualElementAsset, as that belongs to our parent document.
             * So instead, we just use no parent, indicating that we are adding this new element
             * to the root of our document. */
            VisualElementAsset parent = null;

            if (!m_Selection.isEmpty)
            {
                var selectionParent = m_Selection.selection.First().parent;
                parent = selectionParent?.GetVisualElementAsset();

                if (selectionParent?.GetVisualTreeAsset() == m_PaneWindow.document.visualTreeAsset)
                {
                    parent = null;
                }

                m_Selection.ClearSelection(null);
            }

            // Select all pasted elements.
            foreach (var templateAsset in pasteVta.templateAssets)
            {
                if (pasteVta.IsRootElement(templateAsset))
                {
                    templateAsset.Select();
                }
            }
            foreach (var vea in pasteVta.visualElementAssets)
            {
                if (pasteVta.IsRootElement(vea))
                {
                    vea.Select();
                }
            }

            BuilderAssetUtilities.TransferAssetToAsset(m_PaneWindow.document, parent, pasteVta);
            m_PaneWindow.document.AddStyleSheetsToAllRootElements();

            ScriptableObject.DestroyImmediate(pasteVta);
        }
예제 #6
0
        void PasteUSS(string copyBuffer)
        {
            var pasteStyleSheet = StyleSheetUtilities.CreateInstance();
            var importer        = new BuilderStyleSheetImporter(); // Cannot be cached because the StyleBuilder never gets reset.

            importer.Import(pasteStyleSheet, copyBuffer);

            BuilderAssetUtilities.TransferAssetToAsset(m_PaneWindow.document, pasteStyleSheet);

            m_Selection.ClearSelection(null);
            var scs = m_PaneWindow.document.mainStyleSheet.complexSelectors.Last();

            BuilderAssetUtilities.AddStyleComplexSelectorToSelection(m_PaneWindow.document, scs);

            ScriptableObject.DestroyImmediate(pasteStyleSheet);
        }
        void PasteUXML(string copyBuffer)
        {
            VisualTreeAsset pasteVta = null;
            var             importer = new BuilderVisualTreeAssetImporter(); // Cannot be cached because the StyleBuilder never gets reset.

            importer.ImportXmlFromString(copyBuffer, out pasteVta);

            VisualElementAsset parent = null;

            if (!m_Selection.isEmpty)
            {
                parent = m_Selection.selection.First().parent?.GetVisualElementAsset();
            }

            BuilderAssetUtilities.TransferAssetToAsset(m_PaneWindow.document, parent, pasteVta);

            m_PaneWindow.document.AddStyleSheetToAllRootElements();

            ScriptableObject.DestroyImmediate(pasteVta);
        }
예제 #8
0
        public void UnpackTemplateContainer(VisualElement templateContainer, bool unpackCompletely = false)
        {
            if (templateContainer == null)
            {
                Debug.LogError("Template to unpack is null");
                return;
            }

            var elementsToUnpack = new List <VisualElement>();
            var rootVEA          = templateContainer.GetVisualElementAsset();
            var isRootElement    = true;
            VisualElementAsset rootUnpackedVEA = null;

            elementsToUnpack.Add(templateContainer);

            while (elementsToUnpack.Count > 0)
            {
                var elementToUnpack         = elementsToUnpack[0];
                var unpackedVE              = new VisualElement();
                var templateContainerParent = elementToUnpack.parent;
                var templateContainerIndex  = templateContainerParent.IndexOf(elementToUnpack);

                // Create new unpacked element and add it in the hierarchy
                templateContainerParent.Add(unpackedVE);
                BuilderAssetUtilities.AddElementToAsset(m_PaneWindow.document, unpackedVE, templateContainerIndex + 1);

                var linkedInstancedVTA   = elementToUnpack.GetProperty(BuilderConstants.ElementLinkedInstancedVisualTreeAssetVEPropertyName) as VisualTreeAsset;
                var linkedTA             = elementToUnpack.GetVisualElementAsset() as TemplateAsset;
                var linkedVTACopy        = linkedInstancedVTA.DeepCopy();
                var unpackedVEA          = unpackedVE.GetVisualElementAsset();
                var templateContainerVEA = elementToUnpack.GetVisualElementAsset();
                var attributeOverrides   = linkedTA.attributeOverrides;

                var attributes = elementToUnpack.GetOverriddenAttributes();
                foreach (var attribute in attributes)
                {
                    unpackedVEA.SetAttributeValue(attribute.Key, attribute.Value);
                }

                if (isRootElement)
                {
                    rootUnpackedVEA = unpackedVEA;
                }

                // Apply attribute overrides to elements in the unpacked element
                BuilderAssetUtilities.ApplyAttributeOverridesToTreeAsset(attributeOverrides, linkedVTACopy);

                // Move attribute overrides to new template containers
                BuilderAssetUtilities.CopyAttributeOverridesToChildTemplateAssets(attributeOverrides, linkedVTACopy);

                // Apply stylesheets to new element + inline rules
                BuilderAssetUtilities.AddStyleSheetsFromTreeAsset(unpackedVEA, linkedInstancedVTA);
                unpackedVEA.ruleIndex = templateContainerVEA.ruleIndex;

                BuilderAssetUtilities.TransferAssetToAsset(m_PaneWindow.document, unpackedVEA, linkedVTACopy, false);

                elementsToUnpack.Remove(elementToUnpack);

                if (elementToUnpack != templateContainer)
                {
                    BuilderAssetUtilities.DeleteElementFromAsset(m_PaneWindow.document, elementToUnpack, false);
                    elementToUnpack.RemoveFromHierarchy();
                }

                if (unpackCompletely && elementsToUnpack.Count == 0)
                {
                    VisualElement tree = new VisualElement();
                    m_PaneWindow.document.activeOpenUXMLFile.visualTreeAsset.LinkedCloneTree(tree);
                    var newElement   = tree.Query <VisualElement>().Where(x => x.GetVisualElementAsset() == rootUnpackedVEA).First();
                    var newTemplates = newElement.Query <TemplateContainer>().Where(x => x.GetVisualElementAsset() != null).ToList();
                    elementsToUnpack.AddRange(newTemplates);
                    isRootElement = false;
                }
            }

            m_Selection.NotifyOfHierarchyChange();
            m_PaneWindow.OnEnableAfterAllSerialization();

            // Keep hierarchy tree state in the new unpacked element
            var hierarchy = Builder.ActiveWindow.hierarchy;

            hierarchy.elementHierarchyView.CopyTreeViewItemStates(rootVEA, rootUnpackedVEA);

            // Delete old template element
            BuilderAssetUtilities.DeleteElementFromAsset(m_PaneWindow.document, templateContainer, false);
            templateContainer.RemoveFromHierarchy();

            m_Selection.ClearSelection(null);
            rootUnpackedVEA.Select();

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