Exemple #1
0
        public static TemplateAsset AddTemplateInstance(
            this VisualTreeAsset vta, VisualElementAsset parent, string path)
        {
            var templateName = vta.GetTemplateNameFromPath(path);

            if (!vta.TemplateExists(templateName))
            {
                var resolvedAsset = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(path);
                if (resolvedAsset)
                {
                    vta.RegisterTemplate(templateName, resolvedAsset);
                }
                else
                {
                    vta.RegisterTemplate(templateName, path);
                }
            }

            var templateAsset = new TemplateAsset(templateName, BuilderConstants.UxmlInstanceTypeName);

            VisualTreeAssetUtilities.InitializeElement(templateAsset);

            templateAsset.AddProperty("template", templateName);

            return(VisualTreeAssetUtilities.AddElementToDocument(vta, templateAsset, parent) as TemplateAsset);
        }
Exemple #2
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 #3
0
        VisualElementAsset ResolveType(XElement elt, VisualTreeAsset visualTreeAsset)
        {
            string elementNamespaceName = elt.Name.NamespaceName;

            if (elementNamespaceName.StartsWith("UnityEditor.Experimental.UIElements") ||
                elementNamespaceName.StartsWith("UnityEngine.Experimental.UIElements"))
            {
                elementNamespaceName = elementNamespaceName.Replace(".Experimental.UIElements", ".UIElements");
            }

            var fullName = String.IsNullOrEmpty(elementNamespaceName)
                ? elt.Name.LocalName
                : elementNamespaceName + "." + elt.Name.LocalName;

            if (UxmlObjectFactoryRegistry.factories.ContainsKey(fullName))
            {
                return(new UxmlObjectAsset(fullName));
            }

            if (elt.Name.LocalName == k_TemplateInstanceNode && elementNamespaceName == typeof(TemplateContainer).Namespace)
            {
                XAttribute sourceAttr = elt.Attribute(k_TemplateInstanceSourceAttr);
                if (sourceAttr == null || String.IsNullOrEmpty(sourceAttr.Value))
                {
                    LogError(visualTreeAsset,
                             ImportErrorType.Semantic,
                             ImportErrorCode.TemplateInstanceHasEmptySource,
                             null,
                             elt);
                    return(null);
                }

                string templateName = sourceAttr.Value;
                if (!visualTreeAsset.TemplateExists(templateName))
                {
                    LogError(visualTreeAsset,
                             ImportErrorType.Semantic,
                             ImportErrorCode.UnknownTemplate,
                             templateName,
                             elt);
                    return(null);
                }

                return(new TemplateAsset(templateName, fullName));
            }

            return(new VisualElementAsset(fullName));
        }
Exemple #4
0
        public static TemplateAsset AddTemplateInstance(
            this VisualTreeAsset vta, VisualElementAsset parent, string path)
        {
            var templateName = vta.GetTemplateNameFromPath(path);
            if (!vta.TemplateExists(templateName))
                vta.RegisterTemplate(templateName, path);

#if UNITY_2019_4
            var templateAsset = new TemplateAsset(templateName);
#else
            var templateAsset = new TemplateAsset(templateName, BuilderConstants.UxmlInstanceTypeName);
#endif
            VisualTreeAssetUtilities.InitializeElement(templateAsset);

            templateAsset.AddProperty("template", templateName);

            return VisualTreeAssetUtilities.AddElementToDocument(vta, templateAsset, parent) as TemplateAsset;
        }
        private static VisualElementAsset ResolveType(XElement elt, VisualTreeAsset visualTreeAsset)
        {
            VisualElementAsset vea = null;

            if (elt.Name.LocalName == k_TemplateInstanceNode && elt.Name.NamespaceName == "UnityEngine.Experimental.UIElements")
            {
                XAttribute sourceAttr = elt.Attribute(k_TemplateInstanceSourceAttr);
                if (sourceAttr == null || String.IsNullOrEmpty(sourceAttr.Value))
                {
                    logger.LogError(ImportErrorType.Semantic, ImportErrorCode.TemplateInstanceHasEmptySource, null, Error.Level.Fatal, elt);
                }
                else
                {
                    string templateName = sourceAttr.Value;
                    if (!visualTreeAsset.TemplateExists(templateName))
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.UnknownTemplate, templateName, Error.Level.Fatal, elt);
                    }
                    else
                    {
                        vea = new TemplateAsset(templateName);
                    }
                }
            }
            else
            {
                string fullName = String.IsNullOrEmpty(elt.Name.NamespaceName)
                    ? elt.Name.LocalName
                    : elt.Name.NamespaceName + "." + elt.Name.LocalName;

                // HACK: wait for Theo's PR OR go with that
                if (fullName == "UnityEngine.Experimental.UIElements.VisualContainer")
                {
                    Debug.LogWarning("VisualContainer is obsolete, use VisualElement now");
                    fullName = typeof(VisualElement).FullName;
                }

                vea = new VisualElementAsset(fullName);
            }

            return(vea);
        }
Exemple #6
0
        VisualElementAsset ResolveType(XElement elt, VisualTreeAsset visualTreeAsset)
        {
            VisualElementAsset vea = null;

            string elementNamespaceName = elt.Name.NamespaceName;

            if (elementNamespaceName.StartsWith("UnityEditor.Experimental.UIElements") || elementNamespaceName.StartsWith("UnityEngine.Experimental.UIElements"))
            {
                elementNamespaceName = elementNamespaceName.Replace(".Experimental.UIElements", ".UIElements");
            }

            if (elt.Name.LocalName == k_TemplateInstanceNode && elementNamespaceName == "UnityEngine.UIElements")
            {
                XAttribute sourceAttr = elt.Attribute(k_TemplateInstanceSourceAttr);
                if (sourceAttr == null || String.IsNullOrEmpty(sourceAttr.Value))
                {
                    logger.LogError(ImportErrorType.Semantic, ImportErrorCode.TemplateInstanceHasEmptySource, null, Error.Level.Fatal, elt);
                }
                else
                {
                    string templateName = sourceAttr.Value;
                    if (!visualTreeAsset.TemplateExists(templateName))
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.UnknownTemplate, templateName, Error.Level.Fatal, elt);
                    }
                    else
                    {
                        vea = new TemplateAsset(templateName);
                    }
                }
            }
            else
            {
                string fullName = String.IsNullOrEmpty(elementNamespaceName)
                    ? elt.Name.LocalName
                    : elementNamespaceName + "." + elt.Name.LocalName;
                vea = new VisualElementAsset(fullName);
            }

            return(vea);
        }
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);
        }
        VisualElementAsset ResolveType(XElement elt, VisualTreeAsset visualTreeAsset)
        {
            var(elementNamespaceName, fullName) = ResolveFullType(elt);

            if (UxmlObjectFactoryRegistry.factories.ContainsKey(fullName))
            {
                return(new UxmlObjectAsset(fullName));
            }

            if (elt.Name.LocalName == k_TemplateInstanceNode && elementNamespaceName == typeof(TemplateContainer).Namespace)
            {
                XAttribute sourceAttr = elt.Attribute(k_TemplateInstanceSourceAttr);
                if (sourceAttr == null || String.IsNullOrEmpty(sourceAttr.Value))
                {
                    LogError(visualTreeAsset,
                             ImportErrorType.Semantic,
                             ImportErrorCode.TemplateInstanceHasEmptySource,
                             null,
                             elt);
                    return(null);
                }

                string templateName = sourceAttr.Value;
                if (!visualTreeAsset.TemplateExists(templateName))
                {
                    LogError(visualTreeAsset,
                             ImportErrorType.Semantic,
                             ImportErrorCode.UnknownTemplate,
                             templateName,
                             elt);
                    return(null);
                }

                return(new TemplateAsset(templateName, fullName));
            }

            return(new VisualElementAsset(fullName));
        }
Exemple #9
0
        void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child)
        {
            bool   hasPath = false;
            bool   hasSrc  = false;
            string name    = null;
            string path    = null;
            string src     = null;

            foreach (var xAttribute in child.Attributes())
            {
                switch (xAttribute.Name.LocalName)
                {
                case k_GenericPathAttr:
                    hasPath = true;
                    path    = xAttribute.Value;
                    break;

                case k_GenericSrcAttr:
                    hasSrc = true;
                    src    = xAttribute.Value;
                    break;

                case k_TemplateNameAttr:
                    name = xAttribute.Value;
                    if (String.IsNullOrEmpty(name))
                    {
                        logger.LogError(ImportErrorType.Semantic,
                                        ImportErrorCode.TemplateHasEmptyName,
                                        child,
                                        Error.Level.Fatal,
                                        child
                                        );
                    }
                    break;

                default:
                    logger.LogError(ImportErrorType.Semantic,
                                    ImportErrorCode.UnknownAttribute,
                                    xAttribute.Name.LocalName,
                                    Error.Level.Fatal,
                                    child
                                    );
                    break;
                }
            }

            if (hasPath == hasSrc)
            {
                logger.LogError(ImportErrorType.Semantic,
                                hasPath ? ImportErrorCode.TemplateSrcAndPathBothSpecified : ImportErrorCode.TemplateMissingPathOrSrcAttribute,
                                null,
                                Error.Level.Fatal,
                                elt
                                );
                return;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = Path.GetFileNameWithoutExtension(path);
            }

            if (vta.TemplateExists(name))
            {
                logger.LogError(ImportErrorType.Semantic,
                                ImportErrorCode.DuplicateTemplateName,
                                name,
                                Error.Level.Fatal,
                                elt
                                );
                return;
            }

            if (hasPath)
            {
                vta.RegisterTemplate(name, path);
            }
            else if (hasSrc)
            {
                string errorMessage, projectRelativePath;

                URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, src, out errorMessage, out projectRelativePath);

                if (result != URIValidationResult.OK)
                {
                    logger.LogError(ImportErrorType.Semantic, ConvertErrorCode(result), errorMessage, Error.Level.Fatal, elt);
                }
                else
                {
                    Object asset = DeclareDependencyAndLoad(projectRelativePath);

                    if (asset is VisualTreeAsset)
                    {
                        vta.RegisterTemplate(name, asset as VisualTreeAsset);
                    }
                    else
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, projectRelativePath, Error.Level.Fatal, elt);
                    }
                }
            }
        }
        void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child)
        {
            bool   hasPath = false;
            string name    = null;
            string path    = null;

            foreach (var xAttribute in child.Attributes())
            {
                switch (xAttribute.Name.LocalName)
                {
                case k_TemplatePathAttr:
                    hasPath = true;
                    path    = xAttribute.Value;
                    break;

                case k_TemplateNameAttr:
                    name = xAttribute.Value;
                    if (String.IsNullOrEmpty(name))
                    {
                        logger.LogError(ImportErrorType.Semantic,
                                        ImportErrorCode.TemplateHasEmptyName,
                                        child,
                                        Error.Level.Fatal,
                                        child
                                        );
                    }
                    break;

                default:
                    logger.LogError(ImportErrorType.Semantic,
                                    ImportErrorCode.UnknownAttribute,
                                    xAttribute.Name.LocalName,
                                    Error.Level.Fatal,
                                    child
                                    );
                    break;
                }
            }

            if (!hasPath)
            {
                logger.LogError(ImportErrorType.Semantic,
                                ImportErrorCode.MissingPathAttributeOnTemplate,
                                null,
                                Error.Level.Fatal,
                                elt
                                );
                return;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = Path.GetFileNameWithoutExtension(path);
            }

            if (vta.TemplateExists(name))
            {
                logger.LogError(ImportErrorType.Semantic,
                                ImportErrorCode.DuplicateTemplateName,
                                name,
                                Error.Level.Fatal,
                                elt
                                );
                return;
            }

            vta.RegisterTemplate(name, path);
        }
Exemple #11
0
        void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child)
        {
            bool   hasPath = false;
            bool   hasSrc  = false;
            string name    = null;
            string path    = null;
            string src     = null;

            foreach (var xAttribute in child.Attributes())
            {
                switch (xAttribute.Name.LocalName)
                {
                case k_GenericPathAttr:
                    hasPath = true;
                    path    = xAttribute.Value;
                    break;

                case k_GenericSrcAttr:
                    hasSrc = true;
                    src    = xAttribute.Value;
                    break;

                case k_TemplateNameAttr:
                    name = xAttribute.Value;
                    if (String.IsNullOrEmpty(name))
                    {
                        LogError(vta,
                                 ImportErrorType.Semantic,
                                 ImportErrorCode.TemplateHasEmptyName,
                                 child,
                                 child
                                 );
                    }
                    break;

                default:
                    LogError(vta,
                             ImportErrorType.Semantic,
                             ImportErrorCode.UnknownAttribute,
                             xAttribute.Name.LocalName,
                             child
                             );
                    break;
                }
            }

            if (hasPath == hasSrc)
            {
                LogError(vta,
                         ImportErrorType.Semantic,
                         hasPath ? ImportErrorCode.TemplateSrcAndPathBothSpecified : ImportErrorCode.TemplateMissingPathOrSrcAttribute,
                         null,
                         elt
                         );
                return;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = Path.GetFileNameWithoutExtension(path);
            }

            if (vta.TemplateExists(name))
            {
                LogError(vta,
                         ImportErrorType.Semantic,
                         ImportErrorCode.DuplicateTemplateName,
                         name,
                         elt
                         );
                return;
            }

            if (hasPath)
            {
                vta.RegisterTemplate(name, path);
            }
            else if (hasSrc)
            {
                var response            = URIHelpers.ValidateAssetURL(assetPath, src);
                var result              = response.result;
                var projectRelativePath = response.resolvedProjectRelativePath;

                if (response.hasWarningMessage)
                {
                    logger.LogError(ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidURIProjectAssetPath,
                                    response.warningMessage, Error.Level.Warning, elt);
                }

                if (result != URIValidationResult.OK)
                {
                    LogError(vta, ImportErrorType.Semantic, ConvertErrorCode(result), response.errorToken, elt);
                }
                else
                {
                    var asset = response.resolvedQueryAsset;
                    if (asset && m_Context != null)
                    {
                        m_Context.DependsOnSourceAsset(projectRelativePath);
                    }
                    else
                    {
                        asset = DeclareDependencyAndLoad(projectRelativePath);
                    }

                    if (asset is VisualTreeAsset treeAsset)
                    {
                        vta.RegisterTemplate(name, treeAsset);
                    }
                    else
                    {
                        LogError(vta, ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, projectRelativePath, elt);
                    }
                }
            }
        }
        void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child)
        {
            bool   hasPath = false;
            bool   hasSrc  = false;
            string name    = null;
            string path    = null;
            string src     = null;

            foreach (var xAttribute in child.Attributes())
            {
                switch (xAttribute.Name.LocalName)
                {
                case k_GenericPathAttr:
                    hasPath = true;
                    path    = xAttribute.Value;
                    break;

                case k_GenericSrcAttr:
                    hasSrc = true;
                    src    = xAttribute.Value;
                    break;

                case k_TemplateNameAttr:
                    name = xAttribute.Value;
                    if (String.IsNullOrEmpty(name))
                    {
                        LogError(vta,
                                 ImportErrorType.Semantic,
                                 ImportErrorCode.TemplateHasEmptyName,
                                 child,
                                 child
                                 );
                    }
                    break;

                default:
                    LogError(vta,
                             ImportErrorType.Semantic,
                             ImportErrorCode.UnknownAttribute,
                             xAttribute.Name.LocalName,
                             child
                             );
                    break;
                }
            }

            if (hasPath == hasSrc)
            {
                LogError(vta,
                         ImportErrorType.Semantic,
                         hasPath ? ImportErrorCode.TemplateSrcAndPathBothSpecified : ImportErrorCode.TemplateMissingPathOrSrcAttribute,
                         null,
                         elt
                         );
                return;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = Path.GetFileNameWithoutExtension(path);
            }

            if (vta.TemplateExists(name))
            {
                LogError(vta,
                         ImportErrorType.Semantic,
                         ImportErrorCode.DuplicateTemplateName,
                         name,
                         elt
                         );
                return;
            }

            if (hasPath)
            {
                vta.RegisterTemplate(name, path);
            }
            else if (hasSrc)
            {
                var(validationResponse, asset) = ValidateAndLoadResource(elt, vta, src);
                if (validationResponse.result == URIValidationResult.OK)
                {
                    if (asset is VisualTreeAsset treeAsset)
                    {
                        vta.RegisterTemplate(name, treeAsset);
                    }
                    else
                    {
                        LogError(vta, ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, validationResponse.resolvedProjectRelativePath, elt);
                    }
                }
            }
        }