public static void FlagStyleSheetChange()
        {
            // clear caches that depend on loaded style sheets
            StyleSheetCache.ClearCaches();

            // for now we don't bother tracking which panel depends on which style sheet
            var iterator = UIElementsUtility.GetPanelsIterator();

            while (iterator.MoveNext())
            {
                var panel = iterator.Current.Value;

                panel.DirtyStyleSheets();

                var guiView = panel.ownerObject as GUIView;
                if (guiView != null)
                {
                    guiView.Repaint();
                }
            }
        }
Exemple #2
0
        void ProcessMatchedRules(VisualElement element, List <SelectorMatchRecord> matchingSelectors)
        {
            matchingSelectors.Sort(SelectorMatchRecord.Compare);

            Int64 matchingRulesHash = element.fullTypeName.GetHashCode();

            // Let current DPI contribute to the hash so cache is invalidated when this changes
            matchingRulesHash = (matchingRulesHash * 397) ^ currentPixelsPerPoint.GetHashCode();

            foreach (var record in matchingSelectors)
            {
                StyleRule rule        = record.complexSelector.rule;
                int       specificity = record.complexSelector.specificity;
                matchingRulesHash = (matchingRulesHash * 397) ^ rule.GetHashCode();
                matchingRulesHash = (matchingRulesHash * 397) ^ specificity;
            }

            VisualElementStylesData resolvedStyles;

            if (StyleCache.TryGetValue(matchingRulesHash, out resolvedStyles))
            {
                element.SetSharedStyles(resolvedStyles);
            }
            else
            {
                resolvedStyles = new VisualElementStylesData(isShared: true);

                foreach (var record in matchingSelectors)
                {
                    StylePropertyID[] propertyIDs = StyleSheetCache.GetPropertyIDs(record.sheet, record.complexSelector.ruleIndex);
                    resolvedStyles.ApplyRule(record.sheet, record.complexSelector.specificity, record.complexSelector.rule, propertyIDs);
                }

                resolvedStyles.ApplyLayoutValues();

                StyleCache.SetValue(matchingRulesHash, resolvedStyles);

                element.SetSharedStyles(resolvedStyles);
            }
        }
Exemple #3
0
        private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            bool flag  = false;
            bool flag2 = false;

            for (int i = 0; i < importedAssets.Length; i++)
            {
                string text = importedAssets[i];
                if (text.EndsWith("uss"))
                {
                    if (!flag2)
                    {
                        flag2 = true;
                        RetainedMode.FlagStyleSheetChange();
                    }
                }
                else if (text.EndsWith("uxml"))
                {
                    if (!flag)
                    {
                        flag = true;
                        UIElementsViewImporter.logger.FinishImport();
                        StyleSheetCache.ClearCaches();
                        if (RetainedMode.UxmlLiveReloadIsEnabled)
                        {
                            Delegate arg_92_0 = EditorApplication.update;
                            if (RetainedMode.< > f__mg$cache2 == null)
                            {
                                RetainedMode.< > f__mg$cache2 = new EditorApplication.CallbackFunction(RetainedMode.OneShotUxmlLiveReload);
                            }
                            EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Combine(arg_92_0, RetainedMode.< > f__mg$cache2);
                        }
                    }
                }
                if (flag && flag2)
                {
                    break;
                }
            }
        }
Exemple #4
0
        private VisualElement CloneSetupRecursively(VisualElementAsset root,
                                                    Dictionary <int, List <VisualElementAsset> > idToChildren, CreationContext context)
        {
            VisualElement ve = Create(root, context);

            // context.target is the created templateContainer
            if (root.id == context.visualTreeAsset.contentContainerId)
            {
                if (context.target is TemplateContainer)
                {
                    ((TemplateContainer)context.target).SetContentContainer(ve);
                }
                else
                {
                    Debug.LogError(
                        "Trying to clone a VisualTreeAsset with a custom content container into a element which is not a template container");
                }
            }

            // if the current element had a slot-name attribute, put it in the resulting slot mapping
            string slotName;

            if (context.slotInsertionPoints != null && TryGetSlotInsertionPoint(root.id, out slotName))
            {
                context.slotInsertionPoints.Add(slotName, ve);
            }

            if (root.classes != null)
            {
                for (int i = 0; i < root.classes.Length; i++)
                {
                    ve.AddToClassList(root.classes[i]);
                }
            }

            if (root.ruleIndex != -1)
            {
                if (inlineSheet == null)
                {
                    Debug.LogWarning("VisualElementAsset has a RuleIndex but no inlineStyleSheet");
                }
                else
                {
                    StyleRule r          = inlineSheet.rules[root.ruleIndex];
                    var       stylesData = new VisualElementStylesData(false);
                    ve.SetInlineStyles(stylesData);
                    stylesData.ApplyRule(inlineSheet, Int32.MaxValue, r,
                                         StyleSheetCache.GetPropertyIDs(inlineSheet, root.ruleIndex));
                }
            }

            var templateAsset = root as TemplateAsset;
            List <VisualElementAsset> children;

            if (idToChildren.TryGetValue(root.id, out children))
            {
                children.Sort(CompareForOrder);

                foreach (VisualElementAsset childVea in children)
                {
                    // this will fill the slotInsertionPoints mapping
                    VisualElement childVe = CloneSetupRecursively(childVea, idToChildren, context);
                    if (childVe == null)
                    {
                        continue;
                    }

                    // if the parent is not a template asset, just add the child to whatever hierarchy we currently have
                    // if ve is a scrollView (with contentViewport as contentContainer), this will go to the right place
                    if (templateAsset == null)
                    {
                        ve.Add(childVe);
                        continue;
                    }

                    int index = templateAsset.slotUsages == null
                        ? -1
                        : templateAsset.slotUsages.FindIndex(u => u.assetId == childVea.id);
                    if (index != -1)
                    {
                        VisualElement parentSlot;
                        string        key = templateAsset.slotUsages[index].slotName;
                        Assert.IsFalse(String.IsNullOrEmpty(key),
                                       "a lost name should not be null or empty, this probably points to an importer or serialization bug");
                        if (context.slotInsertionPoints == null ||
                            !context.slotInsertionPoints.TryGetValue(key, out parentSlot))
                        {
                            Debug.LogErrorFormat("Slot '{0}' was not found. Existing slots: {1}", key,
                                                 context.slotInsertionPoints == null
                                ? String.Empty
                                : String.Join(", ",
                                              System.Linq.Enumerable.ToArray(context.slotInsertionPoints.Keys)));
                            ve.Add(childVe);
                        }
                        else
                        {
                            parentSlot.Add(childVe);
                        }
                    }
                    else
                    {
                        ve.Add(childVe);
                    }
                }
            }

            if (templateAsset != null && context.slotInsertionPoints != null)
            {
                context.slotInsertionPoints.Clear();
            }

            return(ve);
        }
 internal static int GetCursorId(StyleSheet sheet, StyleValueHandle handle)
 {
     return(StyleSheetCache.GetEnumValue <MouseCursor>(sheet, handle));
 }
        static VisualElement CloneSetupRecursively(VisualTreeAsset vta, VisualElementAsset root,
                                                   Dictionary <int, List <VisualElementAsset> > idToChildren, CreationContext context)
        {
#if UNITY_2019_3_OR_NEWER
            var resolvedSheets = new List <StyleSheet>();
            foreach (var sheetPath in root.stylesheetPaths)
            {
                resolvedSheets.Add(AssetDatabase.LoadAssetAtPath <StyleSheet>(sheetPath));
            }
            root.stylesheets = resolvedSheets;
#endif

            var ve = VisualTreeAsset.Create(root, context);

            // Linking the new element with its VisualElementAsset.
            // All this copied code for this one line!
            ve.SetProperty(BuilderConstants.ElementLinkedVisualElementAssetVEPropertyName, root);

            // context.target is the created templateContainer
            if (root.id == context.visualTreeAsset.contentContainerId)
            {
                if (context.target is TemplateContainer)
                {
                    ((TemplateContainer)context.target).SetContentContainer(ve);
                }
                else
                {
                    Debug.LogError(
                        "Trying to clone a VisualTreeAsset with a custom content container into a element which is not a template container");
                }
            }

            // if the current element had a slot-name attribute, put it in the resulting slot mapping
            string slotName;
            if (context.slotInsertionPoints != null && vta.TryGetSlotInsertionPoint(root.id, out slotName))
            {
                context.slotInsertionPoints.Add(slotName, ve);
            }

            if (root.classes != null)
            {
                for (int i = 0; i < root.classes.Length; i++)
                {
                    ve.AddToClassList(root.classes[i]);
                }
            }

            if (root.ruleIndex != -1)
            {
                if (vta.inlineSheet == null)
                {
                    Debug.LogWarning("VisualElementAsset has a RuleIndex but no inlineStyleSheet");
                }
                else
                {
                    var rule = vta.inlineSheet.rules[root.ruleIndex];
#if UNITY_2020_1_OR_NEWER
                    ve.SetInlineRule(vta.inlineSheet, rule);
#elif UNITY_2019_3_OR_NEWER
                    var stylesData = new VisualElementStylesData(false);
                    ve.SetInlineStyles(stylesData);
                    s_StylePropertyReader.SetInlineContext(vta.inlineSheet, rule, root.ruleIndex);
                    stylesData.ApplyProperties(s_StylePropertyReader, null);
#else
                    var stylesData = new VisualElementStylesData(false);
                    ve.SetInlineStyles(stylesData);
                    var propIds = StyleSheetCache.GetPropertyIDs(vta.inlineSheet, root.ruleIndex);
                    stylesData.ApplyRule(vta.inlineSheet, Int32.MaxValue, rule, propIds);
#endif
                }
            }

            var templateAsset = root as TemplateAsset;
            if (templateAsset != null)
            {
                var templatePath = vta.GetPathFromTemplateName(templateAsset.templateAlias);
                ve.SetProperty(BuilderConstants.LibraryItemLinkedTemplateContainerPathVEPropertyName, templatePath);
                var instancedTemplateVTA = vta.ResolveTemplate(templateAsset.templateAlias);
                if (instancedTemplateVTA != null)
                {
                    ve.SetProperty(BuilderConstants.ElementLinkedInstancedVisualTreeAssetVEPropertyName, instancedTemplateVTA);
                }
            }

            List <VisualElementAsset> children;
            if (idToChildren.TryGetValue(root.id, out children))
            {
                children.Sort(VisualTreeAssetUtilities.CompareForOrder);

                foreach (VisualElementAsset childVea in children)
                {
                    // this will fill the slotInsertionPoints mapping
                    VisualElement childVe = CloneSetupRecursively(vta, childVea, idToChildren, context);
                    if (childVe == null)
                    {
                        continue;
                    }

                    // if the parent is not a template asset, just add the child to whatever hierarchy we currently have
                    // if ve is a scrollView (with contentViewport as contentContainer), this will go to the right place
                    if (templateAsset == null)
                    {
                        ve.Add(childVe);
                        continue;
                    }

                    int index = templateAsset.slotUsages == null
                        ? -1
                        : templateAsset.slotUsages.FindIndex(u => u.assetId == childVea.id);
                    if (index != -1)
                    {
                        VisualElement parentSlot;
                        string        key = templateAsset.slotUsages[index].slotName;
                        Assert.IsFalse(String.IsNullOrEmpty(key),
                                       "a lost name should not be null or empty, this probably points to an importer or serialization bug");
                        if (context.slotInsertionPoints == null ||
                            !context.slotInsertionPoints.TryGetValue(key, out parentSlot))
                        {
                            Debug.LogErrorFormat("Slot '{0}' was not found. Existing slots: {1}", key,
                                                 context.slotInsertionPoints == null
                                ? String.Empty
                                : String.Join(", ",
                                              System.Linq.Enumerable.ToArray(context.slotInsertionPoints.Keys)));
                            ve.Add(childVe);
                        }
                        else
                        {
                            parentSlot.Add(childVe);
                        }
                    }
                    else
                    {
                        ve.Add(childVe);
                    }
                }
            }

            if (templateAsset != null && context.slotInsertionPoints != null)
            {
                context.slotInsertionPoints.Clear();
            }

            return(ve);
        }
Exemple #7
0
            static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets,
                                               string[] movedFromAssetPaths)
            {
                // Early exit: no imported or deleted assets.
                var uxmlImportedAssets = new HashSet <string>(importedAssets.Where(x => MatchesFileExtension(x, k_UxmlExtension)));
                var uxmlDeletedAssets  = new HashSet <string>(deletedAssets.Where(x => MatchesFileExtension(x, k_UxmlExtension)));
                var ussImportedAssets  = new HashSet <string>(importedAssets.Where(x => MatchesFileExtension(x, k_UssExtension)));
                var ussDeletedAssets   = new HashSet <string>(deletedAssets.Where(x => MatchesFileExtension(x, k_UssExtension)));

                if (uxmlImportedAssets.Count == 0 && uxmlDeletedAssets.Count == 0 &&
                    ussImportedAssets.Count == 0 && ussDeletedAssets.Count == 0)
                {
                    return;
                }

                HashSet <VisualTreeAsset> uxmlModifiedAssets = null;

                if (uxmlImportedAssets.Count > 0)
                {
                    UXMLImporterImpl.logger.FinishImport();

                    // the inline stylesheet cache might get out of date.
                    // Usually called by the USS importer, which might not get called here
                    StyleSheetCache.ClearCaches();

                    uxmlModifiedAssets = new HashSet <VisualTreeAsset>();
                    foreach (var assetPath in uxmlImportedAssets)
                    {
                        VisualTreeAsset asset = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(assetPath);
                        if (asset != null) // Shouldn't be!
                        {
                            uxmlModifiedAssets.Add(asset);
                        }
                    }
                }

                HashSet <StyleSheet> ussModifiedAssets = null;

                var iterator = UIElementsUtility.GetPanelsIterator();

                while (iterator.MoveNext())
                {
                    var panel    = iterator.Current.Value;
                    var trackers = panel.GetVisualTreeAssetTrackersListCopy();

                    if (trackers != null)
                    {
                        foreach (var tracker in trackers)
                        {
                            tracker.OnAssetsImported(uxmlModifiedAssets, uxmlDeletedAssets);
                        }
                    }

                    var styleSheetTracker = (panel as BaseVisualElementPanel)?.m_LiveReloadStyleSheetAssetTracker;

                    if (styleSheetTracker != null)
                    {
                        // ussModifiedAssets is null but we don't care for those, only deleted ones (that we'll stop tracking).
                        styleSheetTracker.OnAssetsImported(ussModifiedAssets, ussDeletedAssets);
                    }
                }

                if (ussImportedAssets.Count > 0 || ussDeletedAssets.Count > 0)
                {
                    FlagStyleSheetChange();
                }
            }
        private VisualElement CloneSetupRecursively(VisualElementAsset root, Dictionary <int, List <VisualElementAsset> > idToChildren, CreationContext context)
        {
            VisualElement visualElement = root.Create(context);

            if (root.id == context.visualTreeAsset.contentContainerId)
            {
                if (context.target is TemplateContainer)
                {
                    ((TemplateContainer)context.target).SetContentContainer(visualElement);
                }
                else
                {
                    Debug.LogError("Trying to clone a VisualTreeAsset with a custom content container into a element which is not a template container");
                }
            }
            visualElement.name = root.name;
            string key;

            if (context.slotInsertionPoints != null && this.TryGetSlotInsertionPoint(root.id, out key))
            {
                context.slotInsertionPoints.Add(key, visualElement);
            }
            if (root.classes != null)
            {
                for (int i = 0; i < root.classes.Length; i++)
                {
                    visualElement.AddToClassList(root.classes[i]);
                }
            }
            if (root.ruleIndex != -1)
            {
                if (this.inlineSheet == null)
                {
                    Debug.LogWarning("VisualElementAsset has a RuleIndex but no inlineStyleSheet");
                }
                else
                {
                    StyleRule rule = this.inlineSheet.rules[root.ruleIndex];
                    VisualElementStylesData visualElementStylesData = new VisualElementStylesData(false);
                    visualElement.SetInlineStyles(visualElementStylesData);
                    visualElementStylesData.ApplyRule(this.inlineSheet, 2147483647, rule, StyleSheetCache.GetPropertyIDs(this.inlineSheet, root.ruleIndex));
                }
            }
            TemplateAsset             templateAsset = root as TemplateAsset;
            List <VisualElementAsset> list;

            if (idToChildren.TryGetValue(root.id, out list))
            {
                using (List <VisualElementAsset> .Enumerator enumerator = list.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        VisualElementAsset childVea       = enumerator.Current;
                        VisualElement      visualElement2 = this.CloneSetupRecursively(childVea, idToChildren, context);
                        if (visualElement2 != null)
                        {
                            if (templateAsset == null)
                            {
                                visualElement.Add(visualElement2);
                            }
                            else
                            {
                                int num = (templateAsset.slotUsages != null) ? templateAsset.slotUsages.FindIndex((VisualTreeAsset.SlotUsageEntry u) => u.assetId == childVea.id) : -1;
                                if (num != -1)
                                {
                                    string slotName = templateAsset.slotUsages[num].slotName;
                                    Assert.IsFalse(string.IsNullOrEmpty(slotName), "a lost name should not be null or empty, this probably points to an importer or serialization bug");
                                    VisualElement visualElement3;
                                    if (context.slotInsertionPoints == null || !context.slotInsertionPoints.TryGetValue(slotName, out visualElement3))
                                    {
                                        Debug.LogErrorFormat("Slot '{0}' was not found. Existing slots: {1}", new object[]
                                        {
                                            slotName,
                                            (context.slotInsertionPoints != null) ? string.Join(", ", context.slotInsertionPoints.Keys.ToArray <string>()) : string.Empty
                                        });
                                        visualElement.Add(visualElement2);
                                    }
                                    else
                                    {
                                        visualElement3.Add(visualElement2);
                                    }
                                }
                                else
                                {
                                    visualElement.Add(visualElement2);
                                }
                            }
                        }
                    }
                }
            }
            if (templateAsset != null && context.slotInsertionPoints != null)
            {
                context.slotInsertionPoints.Clear();
            }
            return(visualElement);
        }