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