private void SetPropertyValue(object newValue)
        {
            object val = StyleDebug.GetComputedStyleValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
            Type type = m_PropertyInfo.type;

            if (newValue == null || type == newValue.GetType())
            {
                val = newValue;
            }
            else
            {
                if (type == typeof(StyleBackground))
                {
                    val = new StyleBackground(newValue as Texture2D);
                }
                else
                {
                    var valueInfo = type.GetProperty("value");
                    try
                    {
                        valueInfo.SetValue(val, newValue, null);
                    }
                    catch (Exception)
                    {
                        Debug.LogError($"Invalid value for property '{m_PropertyName}'");
                        return;
                    }
                }
            }

            StyleDebug.SetInlineStyleValue(m_SelectedElement.style, m_PropertyInfo.id, val);
            SetSpecificity(StyleDebug.InlineSpecificity);
        }
        public void SetMatchRecords(VisualElement selectedElement, IEnumerable<SelectorMatchRecord> matchRecords)
        {
            m_SelectedElement = selectedElement;
            m_PropertySpecificityDictionary.Clear();

            if (selectedElement != null)
                StyleDebug.FindSpecifiedStyles(selectedElement.computedStyle, matchRecords, m_PropertySpecificityDictionary);

            BuildFields();
        }
        private void SetPropertyValue(object newValue)
        {
            object val  = StyleDebug.GetComputedStyleValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
            Type   type = m_PropertyInfo.type;

            if (newValue == null)
            {
                if (type == typeof(StyleBackground))
                {
                    val = new StyleBackground();
                }

                if (type == typeof(StyleFont))
                {
                    val = new StyleFont();
                }
            }
            else if (type == newValue.GetType())
            {
                val = newValue;
            }
            else
            {
                if (type == typeof(StyleBackground))
                {
                    val = new StyleBackground(newValue as Texture2D);
                }
                else if (type == typeof(StyleEnum <Overflow>) && newValue is OverflowInternal)
                {
                    OverflowInternal newV = (OverflowInternal)newValue;
                    Overflow         v    = newV == OverflowInternal.Hidden ? Overflow.Hidden : Overflow.Visible;
                    val = new StyleEnum <Overflow>(v);
                }
                else
                {
                    var valueInfo = type.GetProperty("value");
                    try
                    {
                        valueInfo.SetValue(val, newValue, null);
                    }
                    catch (Exception)
                    {
                        Debug.LogError($"Invalid value for property '{m_PropertyName}'");
                        return;
                    }
                }
            }

            StyleDebug.SetInlineStyleValue(m_SelectedElement.style, m_PropertyInfo.id, val);
            SetSpecificity(StyleDebug.InlineSpecificity);
        }
        static StylePropertyDebugger()
        {
            // Retrieve all style property infos
            var names = StyleDebug.GetStylePropertyNames();
            foreach (var name in names)
            {
                var id = StyleDebug.GetStylePropertyIdFromName(name);

                var info = new StylePropertyInfo();
                info.name = name;
                info.id = id;
                info.type = StyleDebug.GetComputedStyleType(name);
                info.longhands = StyleDebug.GetLonghandPropertyNames(name);

                s_StylePropertyInfos.Add(info);
            }
        }
        public void RefreshPropertyValue(object val, int specificity)
        {
            if (val is StyleFloat)
            {
                var        style = (StyleFloat)val;
                FloatField field = GetOrCreateField <FloatField, float>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(style.value);
                }
            }
            else if (val is StyleInt)
            {
                var          style = (StyleInt)val;
                IntegerField field = GetOrCreateField <IntegerField, int>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(style.value);
                }
            }
            else if (val is StyleLength)
            {
                var style = (StyleLength)val;
                StyleLengthField field = GetOrCreateField <StyleLengthField, StyleLength>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(style);
                }
            }
            else if (val is StyleColor)
            {
                var        style = (StyleColor)val;
                ColorField field = GetOrCreateField <ColorField, Color>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(style.value);
                }
            }
            else if (val is StyleFont)
            {
                var         style = (StyleFont)val;
                ObjectField field = GetOrCreateObjectField <Font>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(style.value);
                }
            }
            else if (val is StyleBackground)
            {
                var         background = ((StyleBackground)val).value;
                ObjectField field;
                if (background.vectorImage != null)
                {
                    field = GetOrCreateObjectField <VectorImage>();
                }
                else
                {
                    field = GetOrCreateObjectField <Texture2D>();
                }
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(background.vectorImage != null ? (UnityEngine.Object)background.vectorImage : (UnityEngine.Object)background.texture);
                }
            }
            else if (val is StyleCursor)
            {
                // Recreate the cursor fields every time since StyleCursor
                // can be made of different fields (based on the value)
                Clear();

                StyleCursor style = (StyleCursor)val;
                if (style.value.texture != null)
                {
                    var uiTextureField = new ObjectField(m_PropertyName)
                    {
                        value = style.value.texture
                    };
                    uiTextureField.RegisterValueChangedCallback(e =>
                    {
                        StyleCursor styleCursor = (StyleCursor)StyleDebug.GetComputedStyleValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
                        var currentCursor       = styleCursor.value;
                        currentCursor.texture   = e.newValue as Texture2D;
                        SetPropertyValue(new StyleCursor(currentCursor));
                    });
                    Add(uiTextureField);

                    var uiHotspotField = new Vector2Field("hotspot")
                    {
                        value = style.value.hotspot
                    };
                    uiHotspotField.RegisterValueChangedCallback(e =>
                    {
                        StyleCursor styleCursor = (StyleCursor)StyleDebug.GetComputedStyleValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
                        var currentCursor       = styleCursor.value;
                        currentCursor.hotspot   = e.newValue;
                        SetPropertyValue(new StyleCursor(currentCursor));
                    });
                    Add(uiHotspotField);
                }
                else
                {
                    int mouseId = style.value.defaultCursorId;
                    var uiField = new EnumField(m_PropertyName, (MouseCursor)mouseId);
                    uiField.RegisterValueChangedCallback(e =>
                    {
                        int cursorId = Convert.ToInt32(e.newValue);
                        var cursor   = new Cursor()
                        {
                            defaultCursorId = cursorId
                        };
                        SetPropertyValue(new StyleCursor(cursor));
                    });
                    Add(uiField);
                }
                Add(m_SpecificityLabel);
                SetSpecificity(specificity);
            }
            else
            {
                // StyleEnum<T>
                var       type      = val.GetType();
                var       propInfo  = type.GetProperty("value");
                Enum      enumValue = propInfo.GetValue(val, null) as Enum;
                EnumField field     = GetOrCreateEnumField(enumValue);
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(enumValue);
                }
            }

            SetSpecificity(specificity);
        }
        private void RefreshFields()
        {
            if (m_SelectedElement == null)
            {
                return;
            }

            FindInlineStyles();

            m_CustomPropertyFieldsContainer.Clear();
            var customProperties = m_SelectedElement.computedStyle.m_CustomProperties;

            if (customProperties != null && customProperties.Any())
            {
                foreach (KeyValuePair <string, StylePropertyValue> customProperty in customProperties)
                {
                    var       styleName = customProperty.Key;
                    var       propValue = customProperty.Value;
                    TextField textField = new TextField(styleName)
                    {
                        isReadOnly = true
                    };
                    textField.AddToClassList("unity-style-field");
                    textField.value = propValue.sheet.ReadAsString(propValue.handle).ToLower();
                    m_CustomPropertyFieldsContainer.Add(textField);
                }
            }

            foreach (var propertyInfo in s_StylePropertyInfos)
            {
                if (propertyInfo.isShorthand)
                {
                    continue;
                }

                var styleName = propertyInfo.name;
                if (!string.IsNullOrEmpty(m_SearchFilter) &&
                    styleName.IndexOf(m_SearchFilter, StringComparison.InvariantCultureIgnoreCase) == -1)
                {
                    continue;
                }

                var    id   = propertyInfo.id;
                object val  = StyleDebug.GetComputedStyleValue(m_SelectedElement.computedStyle, id);
                Type   type = propertyInfo.type;

                int specificity = 0;
                m_PropertySpecificityDictionary.TryGetValue(id, out specificity);

                StyleField sf = null;
                m_IdToFieldDictionary.TryGetValue(id, out sf);
                if (m_ShowAll || specificity != StyleDebug.UndefinedSpecificity)
                {
                    if (sf != null)
                    {
                        sf.RefreshPropertyValue(val, specificity);
                    }
                    else
                    {
                        sf = new StyleField(m_SelectedElement, propertyInfo, val, specificity);
                        m_FieldsContainer.Add(sf);
                        m_IdToFieldDictionary[id] = sf;
                    }
                }
                else if (sf != null)
                {
                    // Style property is not applied anymore, remove the field
                    m_IdToFieldDictionary.Remove(id);
                    m_FieldsContainer.Remove(sf);
                }
            }
        }
        private void SetPropertyValue(object newValue)
        {
            object val  = StyleDebug.GetComputedStyleActualValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
            Type   type = m_PropertyInfo.type;

            if (newValue == null)
            {
                if (type == typeof(StyleBackground))
                {
                    val = new StyleBackground();
                }

                if (type == typeof(StyleFont))
                {
                    val = new StyleFont();
                }
            }
            else if (type == newValue.GetType())
            {
                // For StyleLengthField
                val = newValue;
            }
            else
            {
                if (type == typeof(StyleBackground))
                {
                    val = new StyleBackground(newValue as Texture2D);
                }
                else if (type == typeof(StyleFontDefinition))
                {
                    val = new StyleFontDefinition(newValue);
                }
                else if (val is TextShadow textShadow)
                {
                    if (newValue is Color newColor)
                    {
                        textShadow.color = newColor;
                    }
                    if (newValue is Vector2 newOffset)
                    {
                        textShadow.offset = newOffset;
                    }
                    if (newValue is float newBlur)
                    {
                        textShadow.blurRadius = newBlur;
                    }

                    val = new StyleTextShadow(textShadow);
                }
                else if (type == typeof(StyleEnum <Overflow>) && newValue is OverflowInternal)
                {
                    OverflowInternal newV = (OverflowInternal)newValue;
                    Overflow         v    = newV == OverflowInternal.Hidden ? Overflow.Hidden : Overflow.Visible;
                    val = new StyleEnum <Overflow>(v);
                }
                else
                {
                    var underlyingType = type.GetProperty("value").PropertyType;
                    var ctor           = type.GetConstructor(new[] { underlyingType });
                    try
                    {
                        val = ctor.Invoke(new[] { newValue });
                    }
                    catch (Exception)
                    {
                        Debug.LogError($"Invalid value for property '{m_PropertyName}'");
                        return;
                    }
                }
            }

            StyleDebug.SetInlineStyleValue(m_SelectedElement.style, m_PropertyInfo.id, val);
            SetSpecificity(StyleDebug.InlineSpecificity);
        }
        public void RefreshPropertyValue(object val, int specificity)
        {
            if (val is float floatValue)
            {
                FloatField field = GetOrCreateField <FloatField, float>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(floatValue);
                }
            }
            else if (val is int intValue)
            {
                IntegerField field = GetOrCreateField <IntegerField, int>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(intValue);
                }
            }
            else if (val is Length lengthValue)
            {
                StyleLengthField field = GetOrCreateField <StyleLengthField, StyleLength>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(new StyleLength(lengthValue));
                }
            }
            else if (val is Color colorValue)
            {
                ColorField field = GetOrCreateField <ColorField, Color>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(colorValue);
                }
            }
            // Note: val may be null in case of reference type like "Font"
            else if (m_PropertyInfo.type == typeof(StyleFont))
            {
                ObjectField field;
                field = GetOrCreateObjectField <Font>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(val as Font);
                }
            }
            else if (val is FontDefinition fontDefinition)
            {
                ObjectField field;
                if (fontDefinition.fontAsset != null)
                {
                    if (childCount == 0)
                    {
                        field = GetObjectFieldOfTypeFontAsset();
                        SetField(field);
                    }
                    else
                    {
                        field = (ObjectField)ElementAt(0);
                    }
                }
                else
                {
                    field = GetOrCreateObjectField <Font>();
                }

                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(fontDefinition.fontAsset != null ? fontDefinition.fontAsset : fontDefinition.font);
                }
            }
            else if (val is Background bgValue)
            {
                ObjectField field;
                if (bgValue.vectorImage != null)
                {
                    field = GetOrCreateObjectField <VectorImage>();
                }
                else if (bgValue.sprite != null)
                {
                    field = GetOrCreateObjectField <Sprite>();
                }
                else if (bgValue.renderTexture != null)
                {
                    field = GetOrCreateObjectField <RenderTexture>();
                }
                else
                {
                    field = GetOrCreateObjectField <Texture2D>();
                }
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(
                        bgValue.vectorImage != null ? (UnityEngine.Object)bgValue.vectorImage :
                        (bgValue.sprite != null ? (UnityEngine.Object)bgValue.sprite :
                         (bgValue.renderTexture != null ? (UnityEngine.Object)bgValue.renderTexture :
                          (UnityEngine.Object)bgValue.texture)));
                }
            }
            else if (val is Cursor cursorValue)
            {
                // Recreate the cursor fields every time since StyleCursor
                // can be made of different fields (based on the value)
                Clear();

                if (cursorValue.texture != null)
                {
                    var uiTextureField = new ObjectField(m_PropertyName)
                    {
                        value = cursorValue.texture
                    };
                    uiTextureField.RegisterValueChangedCallback(e =>
                    {
                        var currentCursor     = (Cursor)StyleDebug.GetComputedStyleActualValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
                        currentCursor.texture = e.newValue as Texture2D;
                        SetPropertyValue(new StyleCursor(currentCursor));
                    });
                    Add(uiTextureField);

                    var uiHotspotField = new Vector2Field("hotspot")
                    {
                        value = cursorValue.hotspot
                    };
                    uiHotspotField.RegisterValueChangedCallback(e =>
                    {
                        var currentCursor     = (Cursor)StyleDebug.GetComputedStyleActualValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
                        currentCursor.hotspot = e.newValue;
                        SetPropertyValue(new StyleCursor(currentCursor));
                    });
                    Add(uiHotspotField);
                }
                else
                {
                    int mouseId = cursorValue.defaultCursorId;
                    var uiField = new EnumField(m_PropertyName, (MouseCursor)mouseId);
                    uiField.RegisterValueChangedCallback(e =>
                    {
                        int cursorId = Convert.ToInt32(e.newValue);
                        var cursor   = new Cursor()
                        {
                            defaultCursorId = cursorId
                        };
                        SetPropertyValue(new StyleCursor(cursor));
                    });
                    Add(uiField);
                }
                Add(m_SpecificityLabel);
                SetSpecificity(specificity);
            }
            else if (val is TextShadow textShadow)
            {
                ColorField colorFieldfield = GetOrCreateFields <ColorField, Color>(m_PropertyName, 0);
                if (!IsFocused(colorFieldfield))
                {
                    colorFieldfield.SetValueWithoutNotify(textShadow.color);
                }

                Vector2Field vector2Field = GetOrCreateFields <Vector2Field, Vector2>("offset", 1);
                if (!IsFocused(vector2Field))
                {
                    vector2Field.SetValueWithoutNotify(textShadow.offset);
                }

                FloatField floatField = GetOrCreateFields <FloatField, float>("blur", 2);
                if (!IsFocused(floatField))
                {
                    floatField.SetValueWithoutNotify(textShadow.blurRadius);
                }

                if (childCount == 3)
                {
                    Add(m_SpecificityLabel);
                }
            }
            else
            {
                // Enum
                Debug.Assert(val is Enum, "Expected Enum value");
                Enum      enumValue = (Enum)val;
                EnumField field     = GetOrCreateEnumField(enumValue);
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(enumValue);
                }
            }

            SetSpecificity(specificity);
        }
Esempio n. 9
0
        private void SetPropertyValue(object newValue)
        {
            object val  = StyleDebug.GetComputedStyleValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
            Type   type = m_PropertyInfo.type;

            if (newValue == null)
            {
                if (type == typeof(StyleBackground))
                {
                    val = new StyleBackground();
                }

                if (type == typeof(StyleFont))
                {
                    val = new StyleFont();
                }

                if (type == typeof(StyleFontDefinition))
                {
                    val = new StyleFontDefinition();
                }
            }
            else if (type == newValue.GetType())
            {
                // For StyleLengthField
                val = newValue;
            }
            else
            {
                if (type == typeof(StyleBackground))
                {
                    val = new StyleBackground(newValue as Texture2D);
                }
                else if (type == typeof(StyleFontDefinition))
                {
                    val = new StyleFontDefinition(newValue);
                }
                else if (val is TextShadow textShadow)
                {
                    if (newValue is Color newColor)
                    {
                        textShadow.color = newColor;
                    }
                    if (newValue is Vector2 newOffset)
                    {
                        textShadow.offset = newOffset;
                    }
                    if (newValue is float newBlur)
                    {
                        textShadow.blurRadius = newBlur;
                    }

                    val = new StyleTextShadow(textShadow);
                }
                else if (type == typeof(StyleEnum <Overflow>) && newValue is OverflowInternal)
                {
                    OverflowInternal newV = (OverflowInternal)newValue;
                    Overflow         v    = newV == OverflowInternal.Hidden ? Overflow.Hidden : Overflow.Visible;
                    val = new StyleEnum <Overflow>(v);
                }
                else if (val is Scale scale && newValue is Vector3 newScale)
                {
                    val = new StyleScale(new Scale(newScale));
                }
Esempio n. 10
0
        public void RefreshPropertyValue(object val, int specificity)
        {
            if (val is float floatValue)
            {
                FloatField field = GetOrCreateField <FloatField, float>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(floatValue);
                }
            }
            else if (val is int intValue)
            {
                IntegerField field = GetOrCreateField <IntegerField, int>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(intValue);
                }
            }
            else if (val is Length lengthValue)
            {
                StyleLengthField field = GetOrCreateField <StyleLengthField, StyleLength>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(new StyleLength(lengthValue));
                }
            }
            else if (val is Color colorValue)
            {
                ColorField field = GetOrCreateField <ColorField, Color>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(colorValue);
                }
            }
            // Note: val may be null in case of reference type like "Font"
            else if (m_PropertyInfo.type == typeof(StyleFont))
            {
                ObjectField field;
                field = GetOrCreateObjectField <Font>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(val as Font);
                }
            }
            else if (val is FontDefinition fontDefinition)
            {
                ObjectField field;
                if (fontDefinition.fontAsset != null)
                {
                    if (childCount == 0)
                    {
                        var o = new ObjectField();
                        o.objectType = typeof(FontAsset);
                        field        = o;

                        SetField(field);
                    }
                    else
                    {
                        field = (ObjectField)ElementAt(0);
                    }
                }
                else
                {
                    field = GetOrCreateObjectField <Font>();
                }

                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(fontDefinition.fontAsset != null ? (UnityEngine.Object)fontDefinition.fontAsset : (UnityEngine.Object)fontDefinition.font);
                }
            }
            else if (val is Background bgValue)
            {
                ObjectField field;
                if (bgValue.vectorImage != null)
                {
                    field = GetOrCreateObjectField <VectorImage>();
                }
                else if (bgValue.sprite != null)
                {
                    field = GetOrCreateObjectField <Sprite>();
                }
                else if (bgValue.renderTexture != null)
                {
                    field = GetOrCreateObjectField <RenderTexture>();
                }
                else
                {
                    field = GetOrCreateObjectField <Texture2D>();
                }
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(
                        bgValue.vectorImage != null ? (UnityEngine.Object)bgValue.vectorImage :
                        (bgValue.sprite != null ? (UnityEngine.Object)bgValue.sprite :
                         (bgValue.renderTexture != null ? (UnityEngine.Object)bgValue.renderTexture :
                          (UnityEngine.Object)bgValue.texture)));
                }
            }
            else if (val is Cursor cursorValue)
            {
                // Recreate the cursor fields every time since StyleCursor
                // can be made of different fields (based on the value)
                Clear();

                if (cursorValue.texture != null)
                {
                    var uiTextureField = new ObjectField(m_PropertyName)
                    {
                        value = cursorValue.texture
                    };
                    uiTextureField.RegisterValueChangedCallback(e =>
                    {
                        var currentCursor     = (Cursor)StyleDebug.GetComputedStyleValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
                        currentCursor.texture = e.newValue as Texture2D;
                        SetPropertyValue(new StyleCursor(currentCursor));
                    });
                    Add(uiTextureField);

                    var uiHotspotField = new Vector2Field("hotspot")
                    {
                        value = cursorValue.hotspot
                    };
                    uiHotspotField.RegisterValueChangedCallback(e =>
                    {
                        var currentCursor     = (Cursor)StyleDebug.GetComputedStyleValue(m_SelectedElement.computedStyle, m_PropertyInfo.id);
                        currentCursor.hotspot = e.newValue;
                        SetPropertyValue(new StyleCursor(currentCursor));
                    });
                    Add(uiHotspotField);
                }
                else
                {
                    int mouseId = cursorValue.defaultCursorId;
                    var uiField = new EnumField(m_PropertyName, (MouseCursor)mouseId);
                    uiField.RegisterValueChangedCallback(e =>
                    {
                        int cursorId = Convert.ToInt32(e.newValue);
                        var cursor   = new Cursor()
                        {
                            defaultCursorId = cursorId
                        };
                        SetPropertyValue(new StyleCursor(cursor));
                    });
                    Add(uiField);
                }
                Add(m_SpecificityLabel);
                SetSpecificity(specificity);
            }
            else if (val is TextShadow textShadow)
            {
                ColorField colorFieldfield = GetOrCreateFields <ColorField, Color>(m_PropertyName, 0);
                if (!IsFocused(colorFieldfield))
                {
                    colorFieldfield.SetValueWithoutNotify(textShadow.color);
                }

                Vector2Field vector2Field = GetOrCreateFields <Vector2Field, Vector2>("offset", 1);
                if (!IsFocused(vector2Field))
                {
                    vector2Field.SetValueWithoutNotify(textShadow.offset);
                }

                FloatField floatField = GetOrCreateFields <FloatField, float>("blur", 2);
                if (!IsFocused(floatField))
                {
                    floatField.SetValueWithoutNotify(textShadow.blurRadius);
                }

                if (childCount == 3)
                {
                    Add(m_SpecificityLabel);
                }
            }
            else if (val is Rotate rotate)
            {
                FloatField floatField = GetOrCreateFields <FloatField, float>(m_PropertyName, 0);
                if (!IsFocused(floatField))
                {
                    floatField.SetValueWithoutNotify(rotate.angle.value);
                }
                Add(m_SpecificityLabel);
                SetSpecificity(specificity);
            }
            else if (val is Scale scale)
            {
                Vector3Field vector3Field = GetOrCreateFields <Vector3Field, Vector3>(m_PropertyName, 0);
                if (!IsFocused(vector3Field))
                {
                    vector3Field.SetValueWithoutNotify(scale.value);
                }
                Add(m_SpecificityLabel);
                SetSpecificity(specificity);
            }
            else if (val is Translate translate)
            {
                StyleLengthField fieldx = GetOrCreateFields <StyleLengthField, StyleLength>(m_PropertyName, 0);
                if (!IsFocused(fieldx))
                {
                    fieldx.SetValueWithoutNotify(translate.x);
                }
                StyleLengthField fieldy = GetOrCreateFields <StyleLengthField, StyleLength>("y", 1);
                if (!IsFocused(fieldy))
                {
                    fieldy.SetValueWithoutNotify(translate.x);
                }
                FloatField floatField = GetOrCreateFields <FloatField, float>("z", 2);
                if (!IsFocused(floatField))
                {
                    floatField.SetValueWithoutNotify(translate.z);
                }
                Add(m_SpecificityLabel);
                SetSpecificity(specificity);
                SetEnabled(false);
            }
            else if (val is TransformOrigin transformOrigin)
            {
                StyleLengthField fieldx = GetOrCreateFields <StyleLengthField, StyleLength>(m_PropertyName, 0);
                if (!IsFocused(fieldx))
                {
                    fieldx.SetValueWithoutNotify(transformOrigin.x);
                }
                StyleLengthField fieldy = GetOrCreateFields <StyleLengthField, StyleLength>("y", 1);
                if (!IsFocused(fieldy))
                {
                    fieldy.SetValueWithoutNotify(transformOrigin.x);
                }
                FloatField floatField = GetOrCreateFields <FloatField, float>("z", 2);
                if (!IsFocused(floatField))
                {
                    floatField.SetValueWithoutNotify(transformOrigin.z);
                }
                Add(m_SpecificityLabel);
                SetSpecificity(specificity);
                SetEnabled(false);
            }
            else if (val is Enum)
            {
                Enum      enumValue = (Enum)val;
                EnumField field     = GetOrCreateEnumField(enumValue);
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(enumValue);
                }
            }
            else
            {
                var type = val.GetType();
                Debug.Assert(type.IsArrayOrList(), "Expected List type");

                var listValue   = val as System.Collections.IList;
                var valueString = listValue[0].ToString();
                for (int i = 1; i < listValue.Count; i++)
                {
                    valueString += $", {listValue[i]}";
                }
                TextField field = GetOrCreateField <TextField, string>();
                if (!IsFocused(field))
                {
                    field.SetValueWithoutNotify(valueString);
                }
            }

            SetSpecificity(specificity);
        }
Esempio n. 11
0
        /// <summary>
        /// This will iterate over the current UI Builder hierarchy and extract the supported animatable properties.
        /// This will allow to display the options to users in the same order that they are in the builder.
        /// </summary>
        void GenerateTransitionPropertiesContent()
        {
            var content = new CategoryDropdownContent();

            content.AppendValue(new CategoryDropdownContent.ValueItem
            {
                value       = "all",
                displayName = "all"
            });

            foreach (var kvp in m_StyleCategories)
            {
                var groupName = kvp.Key.text;
                if (string.IsNullOrWhiteSpace(groupName) || groupName == "Transition Animations")
                {
                    continue;
                }

                content.AppendCategory(new CategoryDropdownContent.Category {
                    name = groupName
                });

                foreach (var element in kvp.Value)
                {
                    var styleName = element.GetProperty(BuilderConstants.InspectorStylePropertyNameVEPropertyName) as string;

                    if (!string.IsNullOrWhiteSpace(styleName))
                    {
                        var styleId = StyleDebug.GetStylePropertyIdFromName(styleName);
                        if (!StylePropertyUtil.IsAnimatable(styleId))
                        {
                            continue;
                        }

                        if (!string.IsNullOrWhiteSpace(styleId.ToString()))
                        {
                            content.AppendValue(
                                new CategoryDropdownContent.ValueItem
                            {
                                categoryName = groupName,
                                value        = styleName,
                                displayName  = ObjectNames.NicifyVariableName(styleId.ToString())
                            });
                        }
                    }

                    if (!(element is FoldoutField foldoutField))
                    {
                        continue;
                    }
                    var hashSet = HashSetPool <StylePropertyId> .Get();

                    try
                    {
                        foreach (var bindingPath in foldoutField.bindingPathArray)
                        {
                            var shortHandId = StyleDebug.GetStylePropertyIdFromName(bindingPath).GetShorthandProperty();
                            if (shortHandId == StylePropertyId.Unknown || !hashSet.Add(shortHandId))
                            {
                                continue;
                            }

                            if (!StylePropertyUtil.IsAnimatable(shortHandId))
                            {
                                continue;
                            }

                            if (!string.IsNullOrWhiteSpace(shortHandId.ToString()))
                            {
                                content.AppendValue(
                                    new CategoryDropdownContent.ValueItem
                                {
                                    categoryName = groupName,
                                    value        = BuilderNameUtilities.ConvertStyleCSharpNameToUssName(
                                        shortHandId.ToString()),
                                    displayName = ObjectNames.NicifyVariableName(shortHandId.ToString())
                                });
                            }
                        }
                    }
                    finally
                    {
                        HashSetPool <StylePropertyId> .Release(hashSet);
                    }
                }
            }

            content.AppendSeparator();
            content.AppendValue(new CategoryDropdownContent.ValueItem
            {
                value       = "none",
                displayName = "none"
            });

            content.AppendValue(new CategoryDropdownContent.ValueItem
            {
                value       = "initial",
                displayName = "initial"
            });

            content.AppendValue(new CategoryDropdownContent.ValueItem
            {
                value       = "ignored",
                displayName = "ignored"
            });

            TransitionPropertyDropdownContent.Content = content;
        }