Exemple #1
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);
        }
        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);
        }
        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 = TextEditorDelegates.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);
        }