Example #1
0
        internal void SetSharedStyles(VisualElementStylesData sharedStyle)
        {
            Debug.Assert(sharedStyle.isShared);

            if (sharedStyle == m_SharedStyle)
            {
                return;
            }

            if (hasInlineStyle)
            {
                m_Style.Apply(sharedStyle, StylePropertyApplyMode.CopyIfNotInline);
            }
            else
            {
                m_Style = sharedStyle;
            }

            m_SharedStyle = sharedStyle;

            if (onStylesResolved != null)
            {
                onStylesResolved(m_Style);
            }
            OnStyleResolved(m_Style);

            // This is a pre-emptive since we do not know if style changes actually cause a repaint or a layout
            // But thouse should be the only possible type of changes needed
            IncrementVersion(VersionChangeType.Styles | VersionChangeType.Layout | VersionChangeType.Repaint);
        }
Example #2
0
        public void ResetPositionProperties()
        {
            if (!hasInlineStyle)
            {
                return;
            }
            VisualElementStylesData styleAccess = inlineStyle;

            styleAccess.positionType   = StyleValue <int> .nil;
            styleAccess.marginLeft     = StyleValue <float> .nil;
            styleAccess.marginRight    = StyleValue <float> .nil;
            styleAccess.marginBottom   = StyleValue <float> .nil;
            styleAccess.marginTop      = StyleValue <float> .nil;
            styleAccess.positionLeft   = StyleValue <float> .nil;
            styleAccess.positionTop    = StyleValue <float> .nil;
            styleAccess.positionRight  = StyleValue <float> .nil;
            styleAccess.positionBottom = StyleValue <float> .nil;
            styleAccess.width          = StyleValue <float> .nil;
            styleAccess.height         = StyleValue <float> .nil;

            // Make sure to retrieve shared styles from the shared style sheet and update CSSNode again
            m_Style.Apply(sharedStyle, StylePropertyApplyMode.CopyIfNotInline);
            FinalizeLayout();

            IncrementVersion(VersionChangeType.Layout);
        }
        internal void SetSharedStyles(VisualElementStylesData sharedStyle)
        {
            Debug.Assert(sharedStyle.isShared);

            ClearDirty(ChangeType.StylesPath | ChangeType.Styles);
            if (sharedStyle == m_SharedStyle)
            {
                return;
            }

            if (hasInlineStyle)
            {
                m_Style.Apply(sharedStyle, StylePropertyApplyMode.CopyIfNotInline);
            }
            else
            {
                m_Style = sharedStyle;
            }

            m_SharedStyle = sharedStyle;

            if (onStylesResolved != null)
            {
                onStylesResolved(m_Style);
            }
            OnStyleResolved(m_Style);
            Dirty(ChangeType.Repaint);
        }
        public InlineStyleAccess(VisualElement ve)
        {
            this.ve = ve;

            if (ve.specifiedStyle.isShared)
            {
                var inline = new VisualElementStylesData(false);
                inline.Apply(ve.m_SharedStyle, StylePropertyApplyMode.Copy);
                ve.m_Style = inline;
            }
        }
        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);
            }
        }
Example #6
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);
        }
Example #7
0
        private void DrawProperties()
        {
            EditorGUILayout.LabelField(Styles.elementStylesContent, Styles.KInspectorTitle);

            m_SelectedElement.name = EditorGUILayout.TextField("Name", m_SelectedElement.name);
            var textElement = m_SelectedElement as BaseTextElement;

            if (textElement != null)
            {
                textElement.text = EditorGUILayout.TextField("Text", textElement.text);
            }
            m_SelectedElement.clippingOptions = (VisualElement.ClippingOptions)EditorGUILayout.EnumPopup("Clipping Option", m_SelectedElement.clippingOptions);
            m_SelectedElement.pickingMode     = (PickingMode)EditorGUILayout.EnumPopup("Picking Mode", m_SelectedElement.pickingMode);

            EditorGUILayout.LabelField("Layout", m_SelectedElement.layout.ToString());
            EditorGUILayout.LabelField("World Bound", m_SelectedElement.worldBound.ToString());
            m_SelectedElement.pickingMode = (PickingMode)EditorGUILayout.EnumPopup("Picking Mode", m_SelectedElement.pickingMode);

            if (m_ClassList == null)
            {
                InitClassList();
            }
            m_ClassList.DoLayoutList();

            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            m_DetailFilter = EditorGUILayout.ToolbarSearchField(m_DetailFilter);
            m_ShowDefaults = GUILayout.Toggle(m_ShowDefaults, Styles.showDefaultsContent, EditorStyles.toolbarButton);
            m_Sort         = GUILayout.Toggle(m_Sort, Styles.sortContent, EditorStyles.toolbarButton);
            GUILayout.EndHorizontal();

            VisualElementStylesData styles = m_SelectedElement.effectiveStyle;
            bool anyChanged = false;

            if (styles.m_CustomProperties != null && styles.m_CustomProperties.Any())
            {
                foreach (KeyValuePair <string, CustomProperty> customProperty in styles.m_CustomProperties)
                {
                    foreach (StyleValueHandle handle in customProperty.Value.handles)
                    {
                        EditorGUILayout.LabelField(customProperty.Key, customProperty.Value.data.ReadAsString(handle));
                    }
                }
            }

            foreach (PropertyInfo field in m_Sort ? k_SortedFieldInfos : k_FieldInfos)
            {
                if (!string.IsNullOrEmpty(m_DetailFilter) &&
                    field.Name.IndexOf(m_DetailFilter, StringComparison.InvariantCultureIgnoreCase) == -1)
                {
                    continue;
                }

                if (!field.PropertyType.IsGenericType || field.PropertyType.GetGenericTypeDefinition() != typeof(StyleValue <>))
                {
                    continue;
                }

                object val = field.GetValue(m_SelectedElement, null);
                EditorGUILayout.BeginHorizontal();
                EditorGUI.BeginChangeCheck();
                int specificity;

                if (val is StyleValue <float> )
                {
                    StyleValue <float> style = (StyleValue <float>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        style.specificity = Int32.MaxValue;
                        style.value       = EditorGUILayout.FloatField(field.Name, ((StyleValue <float>)val).value);
                        val = style;
                    }
                }
                else if (val is StyleValue <int> )
                {
                    StyleValue <int> style = (StyleValue <int>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        style.specificity = Int32.MaxValue;
                        style.value       = EditorGUILayout.IntField(field.Name, ((StyleValue <int>)val).value);
                        val = style;
                    }
                }
                else if (val is StyleValue <bool> )
                {
                    StyleValue <bool> style = (StyleValue <bool>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        style.specificity = Int32.MaxValue;
                        style.value       = EditorGUILayout.Toggle(field.Name, ((StyleValue <bool>)val).value);
                        val = style;
                    }
                }
                else if (val is StyleValue <Color> )
                {
                    StyleValue <Color> style = (StyleValue <Color>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        style.specificity = Int32.MaxValue;
                        style.value       = EditorGUILayout.ColorField(field.Name, ((StyleValue <Color>)val).value);
                        val = style;
                    }
                }
                else if (val is StyleValue <Font> )
                {
                    specificity = HandleReferenceProperty <Font>(field, ref val);
                }
                else if (val is StyleValue <Texture2D> )
                {
                    specificity = HandleReferenceProperty <Texture2D>(field, ref val);
                }
                else
                {
                    Type type = val.GetType();
                    if (type.GetGenericArguments()[0].IsEnum)
                    {
                        specificity = (int)type.GetField("specificity", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(val);
                        if (m_ShowDefaults || specificity > 0)
                        {
                            FieldInfo fieldInfo    = type.GetField("value");
                            Enum      enumValue    = fieldInfo.GetValue(val) as Enum;
                            Enum      newEnumValue = EditorGUILayout.EnumPopup(field.Name, enumValue);
                            if (!Equals(enumValue, newEnumValue))
                            {
                                fieldInfo.SetValue(val, newEnumValue);
                            }
                        }
                    }
                    else
                    {
                        EditorGUILayout.LabelField(field.Name, val == null ? "null" : val.ToString());
                        specificity = -1;
                    }
                }

                if (EditorGUI.EndChangeCheck())
                {
                    anyChanged = true;
                    field.SetValue(m_SelectedElement, val, null);
                }

                if (specificity > 0)
                {
                    GUILayout.Label(specificity == int.MaxValue ? "inline" : specificity.ToString());
                }

                EditorGUILayout.EndHorizontal();
            }

            if (anyChanged)
            {
                m_CurPanel.Value.Panel.visualTree.Dirty(ChangeType.Transform);
                m_CurPanel.Value.Panel.visualTree.Dirty(ChangeType.Styles);
                m_CurPanel.Value.Panel.visualTree.Dirty(ChangeType.Layout);
                m_CurPanel.Value.Panel.visualTree.Dirty(ChangeType.Repaint);
                m_CurPanel.Value.View.RepaintImmediately();
            }
        }
        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();

            int oldVariablesHash      = m_StyleMatchingContext.variableContext.GetVariableHash();
            int customPropertiesCount = 0;

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

                if (rule.customPropertiesCount > 0)
                {
                    customPropertiesCount += rule.customPropertiesCount;
                    ProcessMatchedVariables(record.sheet, rule);
                }
            }

            int variablesHash = customPropertiesCount > 0 ? m_ProcessVarContext.GetVariableHash() : oldVariablesHash;

            matchingRulesHash = (matchingRulesHash * 397) ^ variablesHash;

            if (oldVariablesHash != variablesHash)
            {
                StyleVariableContext ctx;
                if (!StyleCache.TryGetValue(variablesHash, out ctx))
                {
                    ctx = new StyleVariableContext(m_ProcessVarContext);
                    StyleCache.SetValue(variablesHash, ctx);
                }

                m_StyleMatchingContext.variableContext = ctx;
            }
            element.variableContext = m_StyleMatchingContext.variableContext;

            VisualElementStylesData resolvedStyles;

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

                foreach (var record in matchingSelectors)
                {
                    m_StylePropertyReader.SetContext(record.sheet, record.complexSelector, m_StyleMatchingContext.variableContext);
                    resolvedStyles.ApplyProperties(m_StylePropertyReader, m_StyleMatchingContext.inheritedStyle);
                }

                resolvedStyles.ApplyLayoutValues();

                StyleCache.SetValue(matchingRulesHash, resolvedStyles);

                element.SetSharedStyles(resolvedStyles);
            }
        }
 public static void SetValue(Int64 hash, VisualElementStylesData data)
 {
     s_StyleDataCache[hash] = data;
 }
 public static bool TryGetValue(Int64 hash, out VisualElementStylesData data)
 {
     return(s_StyleDataCache.TryGetValue(hash, out data));
 }
        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);
        }
Example #12
0
        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);
        }
Example #13
0
 // for internal use only, used by asset instantiation to push local styles
 // likely can be replaced by merging VisualContainer and VisualElement
 // and then storing the inline sheet in the list held by VisualContainer
 internal void SetInlineStyles(VisualElementStylesData inlineStyle)
 {
     Debug.Assert(!inlineStyle.isShared);
     inlineStyle.Apply(m_Style, StylePropertyApplyMode.CopyIfEqualOrGreaterSpecificity);
     m_Style = inlineStyle;
 }
        private void DrawProperties()
        {
            EditorGUILayout.LabelField(UIElementsDebugger.Styles.elementStylesContent, UIElementsDebugger.Styles.KInspectorTitle, new GUILayoutOption[0]);
            this.m_SelectedElement.name = EditorGUILayout.TextField("Name", this.m_SelectedElement.name, new GUILayoutOption[0]);
            BaseTextElement baseTextElement = this.m_SelectedElement as BaseTextElement;

            if (baseTextElement != null)
            {
                baseTextElement.text = EditorGUILayout.TextField("Text", baseTextElement.text, new GUILayoutOption[0]);
            }
            this.m_SelectedElement.clippingOptions = (VisualElement.ClippingOptions)EditorGUILayout.EnumPopup("Clipping Option", this.m_SelectedElement.clippingOptions, new GUILayoutOption[0]);
            this.m_SelectedElement.visible         = EditorGUILayout.Toggle("Visible", this.m_SelectedElement.visible, new GUILayoutOption[0]);
            EditorGUILayout.LabelField("Layout", this.m_SelectedElement.layout.ToString(), new GUILayoutOption[0]);
            EditorGUILayout.LabelField("World Bound", this.m_SelectedElement.worldBound.ToString(), new GUILayoutOption[0]);
            if (this.m_ClassList == null)
            {
                this.InitClassList();
            }
            this.m_ClassList.DoLayoutList();
            GUILayout.BeginHorizontal(EditorStyles.toolbar, new GUILayoutOption[0]);
            this.m_DetailFilter = EditorGUILayout.ToolbarSearchField(this.m_DetailFilter, new GUILayoutOption[0]);
            this.m_ShowDefaults = GUILayout.Toggle(this.m_ShowDefaults, UIElementsDebugger.Styles.showDefaultsContent, EditorStyles.toolbarButton, new GUILayoutOption[0]);
            this.m_Sort         = GUILayout.Toggle(this.m_Sort, UIElementsDebugger.Styles.sortContent, EditorStyles.toolbarButton, new GUILayoutOption[0]);
            GUILayout.EndHorizontal();
            VisualElementStylesData effectiveStyle = this.m_SelectedElement.effectiveStyle;
            bool flag = false;

            PropertyInfo[] array = (!this.m_Sort) ? UIElementsDebugger.k_FieldInfos : UIElementsDebugger.k_SortedFieldInfos;
            for (int i = 0; i < array.Length; i++)
            {
                PropertyInfo propertyInfo = array[i];
                if (string.IsNullOrEmpty(this.m_DetailFilter) || propertyInfo.Name.IndexOf(this.m_DetailFilter, StringComparison.InvariantCultureIgnoreCase) != -1)
                {
                    if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(StyleValue <>))
                    {
                        object obj = propertyInfo.GetValue(this.m_SelectedElement, null);
                        EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
                        EditorGUI.BeginChangeCheck();
                        int num;
                        if (obj is StyleValue <float> )
                        {
                            StyleValue <float> styleValue = (StyleValue <float>)obj;
                            num = UIElementsDebugger.GetSpecificity <float>(styleValue);
                            if (this.m_ShowDefaults || num > 0)
                            {
                                styleValue.specificity = 2147483647;
                                styleValue.value       = EditorGUILayout.FloatField(propertyInfo.Name, ((StyleValue <float>)obj).value, new GUILayoutOption[0]);
                                obj = styleValue;
                            }
                        }
                        else if (obj is StyleValue <int> )
                        {
                            StyleValue <int> styleValue2 = (StyleValue <int>)obj;
                            num = UIElementsDebugger.GetSpecificity <int>(styleValue2);
                            if (this.m_ShowDefaults || num > 0)
                            {
                                styleValue2.specificity = 2147483647;
                                styleValue2.value       = EditorGUILayout.IntField(propertyInfo.Name, ((StyleValue <int>)obj).value, new GUILayoutOption[0]);
                                obj = styleValue2;
                            }
                        }
                        else if (obj is StyleValue <bool> )
                        {
                            StyleValue <bool> styleValue3 = (StyleValue <bool>)obj;
                            num = UIElementsDebugger.GetSpecificity <bool>(styleValue3);
                            if (this.m_ShowDefaults || num > 0)
                            {
                                styleValue3.specificity = 2147483647;
                                styleValue3.value       = EditorGUILayout.Toggle(propertyInfo.Name, ((StyleValue <bool>)obj).value, new GUILayoutOption[0]);
                                obj = styleValue3;
                            }
                        }
                        else if (obj is StyleValue <Color> )
                        {
                            StyleValue <Color> styleValue4 = (StyleValue <Color>)obj;
                            num = UIElementsDebugger.GetSpecificity <Color>(styleValue4);
                            if (this.m_ShowDefaults || num > 0)
                            {
                                styleValue4.specificity = 2147483647;
                                styleValue4.value       = EditorGUILayout.ColorField(propertyInfo.Name, ((StyleValue <Color>)obj).value, new GUILayoutOption[0]);
                                obj = styleValue4;
                            }
                        }
                        else if (obj is StyleValue <Font> )
                        {
                            num = this.HandleReferenceProperty <Font>(propertyInfo, ref obj);
                        }
                        else if (obj is StyleValue <Texture2D> )
                        {
                            num = this.HandleReferenceProperty <Texture2D>(propertyInfo, ref obj);
                        }
                        else
                        {
                            Type type = obj.GetType();
                            if (type.GetGenericArguments()[0].IsEnum)
                            {
                                num = (int)type.GetField("specificity", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(obj);
                                if (this.m_ShowDefaults || num > 0)
                                {
                                    FieldInfo field = type.GetField("value");
                                    Enum      @enum = field.GetValue(obj) as Enum;
                                    Enum      enum2 = EditorGUILayout.EnumPopup(propertyInfo.Name, @enum, new GUILayoutOption[0]);
                                    if (!object.Equals(@enum, enum2))
                                    {
                                        field.SetValue(obj, enum2);
                                    }
                                }
                            }
                            else
                            {
                                EditorGUILayout.LabelField(propertyInfo.Name, (obj != null) ? obj.ToString() : "null", new GUILayoutOption[0]);
                                num = -1;
                            }
                        }
                        if (EditorGUI.EndChangeCheck())
                        {
                            flag = true;
                            propertyInfo.SetValue(this.m_SelectedElement, obj, null);
                        }
                        if (num > 0)
                        {
                            GUILayout.Label((num != 2147483647) ? num.ToString() : "inline", new GUILayoutOption[0]);
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                }
            }
            if (flag)
            {
                this.m_CurPanel.Value.Panel.visualTree.Dirty(ChangeType.Transform);
                this.m_CurPanel.Value.Panel.visualTree.Dirty(ChangeType.Styles);
                this.m_CurPanel.Value.Panel.visualTree.Dirty(ChangeType.Layout);
                this.m_CurPanel.Value.Panel.visualTree.Dirty(ChangeType.Repaint);
                this.m_CurPanel.Value.View.RepaintImmediately();
            }
        }
        private void DrawProperties()
        {
            EditorGUILayout.LabelField(Styles.elementStylesContent, Styles.KInspectorTitle);

            m_SelectedElement.name = EditorGUILayout.TextField("Name", m_SelectedElement.name);
            var textElement = m_SelectedElement as ITextElement;

            if (textElement != null)
            {
                textElement.text = EditorGUILayout.TextField("Text", textElement.text);
            }

            // Suppress "use of obsolete enum" warning
            #pragma warning disable 0618
            bool cacheContents = EditorGUILayout.Toggle("Cache Contents", m_SelectedElement.clippingOptions == VisualElement.ClippingOptions.ClipAndCacheContents);
            m_SelectedElement.clippingOptions = cacheContents ? VisualElement.ClippingOptions.ClipAndCacheContents : VisualElement.ClippingOptions.NoClipping;
            #pragma warning restore 0618

            m_SelectedElement.pickingMode = (PickingMode)EditorGUILayout.EnumPopup("Picking Mode", m_SelectedElement.pickingMode);

            EditorGUILayout.LabelField("Layout", m_SelectedElement.layout.ToString());
            EditorGUILayout.LabelField("World Bound", m_SelectedElement.worldBound.ToString());

            if (m_ClassList == null)
            {
                InitClassList();
            }
            m_ClassList.DoLayoutList();

            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            m_DetailFilter = EditorGUILayout.ToolbarSearchField(m_DetailFilter);
            m_ShowDefaults = GUILayout.Toggle(m_ShowDefaults, Styles.showDefaultsContent, EditorStyles.toolbarButton);
            m_Sort         = GUILayout.Toggle(m_Sort, Styles.sortContent, EditorStyles.toolbarButton);
            GUILayout.EndHorizontal();

            VisualElementStylesData styles = m_SelectedElement.effectiveStyle;
            bool anyChanged = false;

            if (styles.m_CustomProperties != null && styles.m_CustomProperties.Any())
            {
                foreach (KeyValuePair <string, CustomProperty> customProperty in styles.m_CustomProperties)
                {
                    foreach (StyleValueHandle handle in customProperty.Value.handles)
                    {
                        EditorGUILayout.LabelField(customProperty.Key, customProperty.Value.data.ReadAsString(handle));
                    }
                }
            }

            foreach (PropertyInfo field in m_Sort ? k_SortedFieldInfos : k_FieldInfos)
            {
                if (!string.IsNullOrEmpty(m_DetailFilter) &&
                    field.Name.IndexOf(m_DetailFilter, StringComparison.InvariantCultureIgnoreCase) == -1)
                {
                    continue;
                }

                if (!field.PropertyType.IsGenericType || field.PropertyType.GetGenericTypeDefinition() != typeof(StyleValue <>))
                {
                    continue;
                }

                object val = field.GetValue(m_SelectedElement, null);
                if (val is StyleValue <Flex> )
                {
                    // The properties of Flex (flexBasis, flexGrow, flexShrink) are already displayed individually.
                    continue;
                }
                EditorGUILayout.BeginHorizontal();
                EditorGUI.BeginChangeCheck();
                int specificity;

                if (val is StyleValue <float> )
                {
                    StyleValue <float> style = (StyleValue <float>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        style.specificity = Int32.MaxValue;
                        style.value       = EditorGUILayout.FloatField(field.Name, ((StyleValue <float>)val).value);
                        val = style;
                    }
                }
                else if (val is StyleValue <int> )
                {
                    StyleValue <int> style = (StyleValue <int>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        style.specificity = Int32.MaxValue;
                        style.value       = EditorGUILayout.IntField(field.Name, ((StyleValue <int>)val).value);
                        val = style;
                    }
                }
                else if (val is StyleValue <bool> )
                {
                    StyleValue <bool> style = (StyleValue <bool>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        style.specificity = Int32.MaxValue;
                        style.value       = EditorGUILayout.Toggle(field.Name, ((StyleValue <bool>)val).value);
                        val = style;
                    }
                }
                else if (val is StyleValue <Color> )
                {
                    StyleValue <Color> style = (StyleValue <Color>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        style.specificity = Int32.MaxValue;
                        style.value       = EditorGUILayout.ColorField(field.Name, ((StyleValue <Color>)val).value);
                        val = style;
                    }
                }
                else if (val is StyleValue <Font> )
                {
                    specificity = HandleReferenceProperty <Font>(field, ref val);
                }
                else if (val is StyleValue <Texture2D> )
                {
                    specificity = HandleReferenceProperty <Texture2D>(field, ref val);
                }
                else if (val is StyleValue <CursorStyle> )
                {
                    StyleValue <CursorStyle> style = (StyleValue <CursorStyle>)val;
                    specificity = GetSpecificity(style);
                    if (m_ShowDefaults || specificity > 0)
                    {
                        if (style.value.texture != null)
                        {
                            style.specificity   = Int32.MaxValue;
                            style.value.texture = EditorGUILayout.ObjectField(field.Name + "'s texture2D", style.value.texture, typeof(Texture2D), false) as Texture2D;
                            EditorGUILayout.EndHorizontal();

                            EditorGUILayout.BeginHorizontal();
                            EditorGUIUtility.wideMode = true;
                            style.value.hotspot       = EditorGUILayout.Vector2Field(field.Name + "'s hotspot", style.value.hotspot);

                            val = style;
                        }
                        else
                        {
                            int  mouseId      = style.value.defaultCursorId;
                            Enum newEnumValue = EditorGUILayout.EnumPopup(field.Name, (MouseCursor)mouseId);

                            int toCompare = Convert.ToInt32(newEnumValue);
                            if (!Equals(mouseId, toCompare))
                            {
                                style.specificity           = Int32.MaxValue;
                                style.value.defaultCursorId = toCompare;
                                val = style;
                            }
                        }
                    }
                }
                else
                {
                    Type type = val.GetType();
                    if (type.GetGenericArguments()[0].IsEnum)
                    {
                        specificity = (int)type.GetField("specificity", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(val);
                        if (m_ShowDefaults || specificity > 0)
                        {
                            FieldInfo fieldInfo    = type.GetField("value");
                            Enum      enumValue    = fieldInfo.GetValue(val) as Enum;
                            Enum      newEnumValue = EditorGUILayout.EnumPopup(field.Name, enumValue);
                            if (!Equals(enumValue, newEnumValue))
                            {
                                fieldInfo.SetValue(val, newEnumValue);
                            }
                        }
                    }
                    else
                    {
                        EditorGUILayout.LabelField(field.Name, val == null ? "null" : val.ToString());
                        specificity = -1;
                    }
                }

                if (EditorGUI.EndChangeCheck())
                {
                    anyChanged = true;
                    field.SetValue(m_SelectedElement, val, null);
                }

                if (specificity > 0)
                {
                    GUILayout.Label(specificity == int.MaxValue ? "inline" : specificity.ToString());
                }

                EditorGUILayout.EndHorizontal();
            }

            if (anyChanged)
            {
                m_CurPanel.Value.Panel.visualTree.IncrementVersion(VersionChangeType.Transform);
                m_CurPanel.Value.Panel.visualTree.IncrementVersion(VersionChangeType.Styles | VersionChangeType.StyleSheet);
                m_CurPanel.Value.Panel.visualTree.IncrementVersion(VersionChangeType.Layout);
                m_CurPanel.Value.Panel.visualTree.IncrementVersion(VersionChangeType.Repaint);
            }
        }