public static string GenerateUXML(VisualTreeAsset vta, string vtaPath, bool writingToFile = false)
        {
            var stringBuilder = new StringBuilder();

            if ((vta.visualElementAssets == null || vta.visualElementAssets.Count <= 0) &&
                (vta.templateAssets == null || vta.templateAssets.Count <= 0))
            {
                stringBuilder.Append(BuilderConstants.UxmlHeader);
                stringBuilder.Append(">");
                stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);
                stringBuilder.Append(BuilderConstants.UxmlFooter);
                stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);
                return(stringBuilder.ToString());
            }

            var idToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(vta);

            stringBuilder.Append(BuilderConstants.UxmlHeader);
#if UNITY_2020_1_OR_NEWER
            AppendHeaderAttributes(vta, stringBuilder, writingToFile);
#endif
            stringBuilder.Append(">");
            stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);

            // Templates
            AppendTemplateRegistrations(vta, vtaPath, stringBuilder);

            GenerateUXMLFromRootElements(vta, idToChildren, stringBuilder, vtaPath, writingToFile);

            stringBuilder.Append(BuilderConstants.UxmlFooter);
            stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);

            return(stringBuilder.ToString());
        }
        public static string GenerateUXML(VisualTreeAsset vta, string vtaPath, List <VisualElementAsset> veas)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.Append(BuilderConstants.UxmlHeader);
            stringBuilder.Append(">");
            stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);

            var idToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(vta);

            foreach (var vea in veas)
            {
                // Templates
                var usedTemplates = new HashSet <string>();
                GatherUsedTemplates(vta, vea, idToChildren, usedTemplates);
                AppendTemplateRegistrations(vta, vtaPath, stringBuilder, usedTemplates);

                GenerateUXMLRecursive(vta, vtaPath, vea, idToChildren, stringBuilder, 1, true);
            }

            stringBuilder.Append(BuilderConstants.UxmlFooter);
            stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);

            return(stringBuilder.ToString());
        }
        public static string GenerateUXML(VisualTreeAsset vta, string vtaPath, bool writingToFile = false)
        {
            var stringBuilder = new StringBuilder();

            if ((vta.visualElementAssets == null || vta.visualElementAssets.Count <= 0) &&
                (vta.templateAssets == null || vta.templateAssets.Count <= 0))
            {
                stringBuilder.Append(BuilderConstants.UxmlHeader);
                stringBuilder.Append(BuilderConstants.UxmlFooter);
                return(stringBuilder.ToString());
            }

            var idToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(vta);

            stringBuilder.Append(BuilderConstants.UxmlHeader);

            // Templates
            AppendTemplateRegistrations(vta, vtaPath, stringBuilder);

            GenerateUXMLFromRootElements(vta, idToChildren, stringBuilder, vtaPath, writingToFile);

            stringBuilder.Append(BuilderConstants.UxmlFooter);

            return(stringBuilder.ToString());
        }
        public static void CloneTree(
            VisualTreeAsset vta, VisualElement target,
            Dictionary <string, VisualElement> slotInsertionPoints,
            List <TemplateAsset.AttributeOverride> attributeOverrides)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if ((vta.visualElementAssets == null || vta.visualElementAssets.Count <= 0) &&
                (vta.templateAssets == null || vta.templateAssets.Count <= 0))
            {
                return;
            }

            var idToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(vta);

            List <VisualElementAsset> rootAssets;

            // Tree root has parentId == 0
            idToChildren.TryGetValue(0, out rootAssets);
            if (rootAssets == null || rootAssets.Count == 0)
            {
                return;
            }

#if !UNITY_2019_4
            var uxmlRootAsset = rootAssets[0];

            vta.AssignClassListFromAssetToElement(uxmlRootAsset, target);
            vta.AssignStyleSheetFromAssetToElement(uxmlRootAsset, target);

            // Get the first-level elements. These will be instantiated and added to target.
            idToChildren.TryGetValue(uxmlRootAsset.id, out rootAssets);
            if (rootAssets == null || rootAssets.Count == 0)
            {
                return;
            }
#endif

            rootAssets.Sort(VisualTreeAssetUtilities.CompareForOrder);
            foreach (VisualElementAsset rootElement in rootAssets)
            {
                Assert.IsNotNull(rootElement);

                // Don't try to instatiate the special selection tracking element.
                if (rootElement.fullTypeName == BuilderConstants.SelectedVisualTreeAssetSpecialElementTypeName)
                {
                    continue;
                }

                VisualElement rootVe = CloneSetupRecursively(vta, rootElement, idToChildren,
                                                             new CreationContext(slotInsertionPoints, attributeOverrides, vta, target));

                // if contentContainer == this, the shadow and the logical hierarchy are identical
                // otherwise, if there is a CC, we want to insert in the shadow
                target.hierarchy.Add(rootVe);
            }
        }
Exemple #5
0
        public static void Swallow(this VisualTreeAsset vta, VisualElementAsset parent, VisualTreeAsset other)
        {
            var otherIdToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(other);

            if (parent == null)
            {
                parent = vta.GetRootUXMLElement();
            }

            var nextOrderInDocument = (vta.visualElementAssets.Count + vta.templateAssets.Count) * BuilderConstants.VisualTreeAssetOrderIncrement;
            var assetsList          = new List <VisualElementAsset>();

            assetsList.AddRange(other.visualElementAssets);
            assetsList.AddRange(other.templateAssets);
            assetsList = assetsList.OrderBy(x => x.orderInDocument).ToList();

            foreach (var asset in assetsList)
            {
                if (other.IsRootUXMLElement(asset))
                {
                    continue;
                }

                ReinitElementWithNewParentAsset(
                    vta, parent, other, otherIdToChildren, asset, ref nextOrderInDocument);
            }

            foreach (var vea in other.visualElementAssets)
            {
                if (other.IsRootUXMLElement(vea))
                {
                    continue;
                }

                vta.visualElementAssets.Add(vea);
            }

            foreach (var vea in other.templateAssets)
            {
                if (!vta.TemplateExists(vea.templateAlias))
                {
                    vta.RegisterTemplate(vea.templateAlias, other.ResolveTemplate(vea.templateAlias));
                }

                vta.templateAssets.Add(vea);
            }

            VisualTreeAssetUtilities.ReOrderDocument(vta);
        }
Exemple #6
0
        public static string GenerateUXML(VisualTreeAsset vta, string vtaPath, VisualElementAsset vea)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.Append(BuilderConstants.UxmlHeader);

            var idToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(vta);

            // Templates
            var usedTemplates = new HashSet<string>();
            GatherUsedTemplates(vta, vea, idToChildren, usedTemplates);
            AppendTemplateRegistrations(vta, vtaPath, stringBuilder, usedTemplates);

            GenerateUXMLRecursive(vta, vtaPath, vea, idToChildren, stringBuilder, 1, true, true);

            stringBuilder.Append(BuilderConstants.UxmlFooter);

            return stringBuilder.ToString();
        }
Exemple #7
0
        public static void Swallow(this VisualTreeAsset vta, VisualElementAsset parent, VisualTreeAsset other)
        {
            var otherIdToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(other);

            if (parent == null)
            {
                parent = vta.GetRootUXMLElement();
            }

            var nextOrderInDocument = (vta.visualElementAssets.Count + vta.templateAssets.Count) * BuilderConstants.VisualTreeAssetOrderIncrement;

            foreach (var vea in other.visualElementAssets)
            {
                if (other.IsRootUXMLElement(vea))
                {
                    continue;
                }

                ReinitElementWithNewParentAsset(
                    vta, parent, other, otherIdToChildren, vea, ref nextOrderInDocument);

                vta.visualElementAssets.Add(vea);
            }

            foreach (var vea in other.templateAssets)
            {
                ReinitElementWithNewParentAsset(
                    vta, parent, other, otherIdToChildren, vea, ref nextOrderInDocument);

                if (!vta.TemplateExists(vea.templateAlias))
                {
                    var path = other.GetPathFromTemplateName(vea.templateAlias);
                    vta.RegisterTemplate(vea.templateAlias, path);
                }

                vta.templateAssets.Add(vea);
            }

            VisualTreeAssetUtilities.ReOrderDocument(vta);
        }