Example #1
0
        private void DrawComputedStyle()
        {
            // style name, style value, source

            UIStyleSet style = selectedElement.style;

            bool isSet = (selectedElement.flags & UIElementFlags.DebugLayout) != 0;

            if (EditorGUILayout.ToggleLeft("Debug Layout", isSet))
            {
                selectedElement.flags |= UIElementFlags.DebugLayout;
            }
            else
            {
                selectedElement.flags &= ~UIElementFlags.DebugLayout;
            }

            GUILayout.BeginHorizontal();
            DrawStyleStateButton("Hover", StyleState.Hover);
            DrawStyleStateButton("Focus", StyleState.Focused);
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            DrawStyleStateButton("Active", StyleState.Active);
            GUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            showAllComputedStyles = EditorGUILayout.ToggleLeft("Show All", showAllComputedStyles);
            showComputedSources   = EditorGUILayout.ToggleLeft("Show Sources", showComputedSources);
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(4);
            searchString = searchField.OnGUI(searchString);
            GUILayout.Space(4);

            List <ValueTuple <string, StyleProperty> > properties = ListPool <ValueTuple <string, StyleProperty> > .Get();

            string lowerSearch = searchString.ToLower();

            for (int i = 0; i < StyleUtil.StylePropertyIdList.Length; i++)
            {
                StylePropertyId propertyId = StyleUtil.StylePropertyIdList[i];
                if (showAllComputedStyles || style.IsDefined(propertyId))
                {
                    if (!string.IsNullOrEmpty(searchString))
                    {
                        string propertyName = StyleUtil.GetPropertyName(propertyId).ToLower();
                        if (!propertyName.Contains(lowerSearch))
                        {
                            continue;
                        }
                    }

                    string        source   = selectedElement.style.GetPropertySource(propertyId);
                    StyleProperty property = selectedElement.style.GetComputedStyleProperty(propertyId);
                    if (!property.hasValue)
                    {
                        property = DefaultStyleValues_Generated.GetPropertyValue(propertyId);
                    }

                    properties.Add(ValueTuple.Create(source, property));
                }
            }

            if (properties.Count == 0)
            {
                return;
            }

            if (showComputedSources)
            {
                properties.Sort(
                    (a, b) => {
                    if (a.Item1 == b.Item1)
                    {
                        return(0);
                    }

                    bool aInstance = a.Item1.Contains("Instance");
                    bool bInstance = b.Item1.Contains("Instance");

                    if (aInstance && bInstance)
                    {
                        return(string.Compare(a.Item1, b.Item1, StringComparison.Ordinal));
                    }

                    if (aInstance)
                    {
                        return(-1);
                    }
                    if (bInstance)
                    {
                        return(1);
                    }

                    bool aDefault = a.Item1.Contains("Default");
                    bool bDefault = b.Item1.Contains("Default");

                    if (aDefault && bDefault)
                    {
                        return(string.Compare(a.Item1, b.Item1, StringComparison.Ordinal));
                    }

                    if (aDefault)
                    {
                        return(1);
                    }
                    if (bDefault)
                    {
                        return(-1);
                    }

                    return(string.Compare(a.Item1, b.Item1, StringComparison.Ordinal));
                });

                GUILayout.Space(10);
                string currentSource = properties[0].Item1;
                EditorGUILayout.LabelField(currentSource);
                int start = 0;
                for (int i = 0; i < properties.Count; i++)
                {
                    if (currentSource != properties[i].Item1)
                    {
                        properties.Sort(start, i - start, s_StyleCompare);

                        for (int j = start; j < i; j++)
                        {
                            DrawStyleProperty(properties[j].Item2, false);
                        }

                        start         = i;
                        currentSource = properties[i].Item1;
                        GUILayout.Space(10);
                        EditorGUILayout.LabelField(currentSource);
                    }
                }

                properties.Sort(start, properties.Count - start, s_StyleCompare);
                for (int j = start; j < properties.Count; j++)
                {
                    DrawStyleProperty(properties[j].Item2, false);
                }
            }
            else
            {
                properties.Sort(0, properties.Count - 1, s_StyleCompare);
                for (int i = 0; i < properties.Count; i++)
                {
                    DrawStyleProperty(properties[i].Item2, false);
                }
            }
        }
Example #2
0
        public void SetStyleProperty(UIElement element, StyleProperty property)
        {
            if (element.isDisabled)
            {
                return;
            }

            AddToChangeSet(element, property);

            if (!StyleUtil.IsInherited(property.propertyId) || element.children == null || element.children.Count == 0)
            {
                return;
            }

            if (!property.hasValue)
            {
                UIElement ptr = element.parent;

                StyleProperty parentProperty = new StyleProperty(property.propertyId);

                while (ptr != null)
                {
                    parentProperty = ptr.style.GetPropertyValue(property.propertyId);
                    if (parentProperty.hasValue)
                    {
                        break;
                    }

                    ptr = ptr.parent;
                }

                if (!parentProperty.hasValue)
                {
                    parentProperty = DefaultStyleValues_Generated.GetPropertyValue(property.propertyId);
                }

                property = parentProperty;
            }

            for (int i = 0; i < element.children.Count; i++)
            {
                s_ElementStack.Push(element.children[i]);
            }

            while (s_ElementStack.Count > 0)
            {
                UIElement descendant = s_ElementStack.Pop();

                if (!descendant.style.SetInheritedStyle(property))
                {
                    continue;
                }

                // todo -- we might want to cache font size lookups for em values, this would be the place
                // if (property.propertyId == StylePropertyId.TextFontSize) {
                // do caching
                // }

                AddToChangeSet(descendant, property);

                if (descendant.children == null)
                {
                    continue;
                }

                for (int i = 0; i < descendant.children.Count; i++)
                {
                    s_ElementStack.Push(descendant.children[i]);
                }
            }
        }