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); }
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); }
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.GetComputedStyleActualValue(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); } } }