Beispiel #1
0
        string GetRemapAttributeNameToCSProperty(string attributeName)
        {
            if (currentVisualElement is ObjectField && attributeName == "type")
            {
                return("objectType");
            }
            else if (attributeName == "readonly")
            {
                return("isReadOnly");
            }

            var camel = BuilderNameUtilities.ConvertDashToCamel(attributeName);

            return(camel);
        }
        public static Dictionary <string, string> GetOverriddenAttributes(this VisualElement ve)
        {
            var attributeList        = ve.GetAttributeDescriptions();
            var overriddenAttributes = new Dictionary <string, string>();

            foreach (var attribute in attributeList)
            {
                if (attribute == null || attribute.name == null)
                {
                    continue;
                }

                var veType    = ve.GetType();
                var camel     = BuilderNameUtilities.ConvertDashToCamel(attribute.name);
                var fieldInfo = veType.GetProperty(camel);
                if (fieldInfo != null)
                {
                    var veValueAbstract = fieldInfo.GetValue(ve, null);
                    if (veValueAbstract == null)
                    {
                        continue;
                    }

                    var veValueStr = veValueAbstract.ToString();
                    if (veValueStr == "False")
                    {
                        veValueStr = "false";
                    }
                    else if (veValueStr == "True")
                    {
                        veValueStr = "true";
                    }

                    var attributeValueStr = attribute.defaultValueAsString;
                    if (veValueStr == attributeValueStr)
                    {
                        continue;
                    }

                    overriddenAttributes.Add(attribute.name, veValueStr);
                }
            }

            return(overriddenAttributes);
        }
        public static Dictionary <string, string> GetOverriddenAttributes(this VisualElement ve)
        {
            var attributeList        = ve.GetAttributeDescriptions();
            var overriddenAttributes = new Dictionary <string, string>();

            foreach (var attribute in attributeList)
            {
                if (attribute?.name == null)
                {
                    continue;
                }

                var veType    = ve.GetType();
                var camel     = BuilderNameUtilities.ConvertDashToCamel(attribute.name);
                var fieldInfo = veType.GetProperty(camel);
                if (fieldInfo != null)
                {
                    var veValueAbstract = fieldInfo.GetValue(ve, null);
                    if (veValueAbstract == null)
                    {
                        continue;
                    }

                    var veValueStr = veValueAbstract.ToString();
                    if (veValueStr == "False")
                    {
                        veValueStr = "false";
                    }
                    else if (veValueStr == "True")
                    {
                        veValueStr = "true";
                    }

                    // The result of Type.ToString is not enough for us to find the correct Type.
                    if (veValueAbstract is Type type)
                    {
                        veValueStr = $"{type.FullName}, {type.Assembly.GetName().Name}";
                    }

                    var attributeValueStr = attribute.defaultValueAsString;
                    if (veValueStr == attributeValueStr)
                    {
                        continue;
                    }
                    overriddenAttributes.Add(attribute.name, veValueStr);
                }
                // This is a special patch that allows to search for built-in elements' attribute specifically
                // without needing to add to the public API.
                // Allowing to search for internal/private properties in all cases could lead to unforeseen issues.
                else if (ve is EnumField && camel == "type")
                {
                    fieldInfo = veType.GetProperty(camel, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    var veValueAbstract = fieldInfo.GetValue(ve, null);
                    if (!(veValueAbstract is Type type))
                    {
                        continue;
                    }

                    var veValueStr        = $"{type.FullName}, {type.Assembly.GetName().Name}";
                    var attributeValueStr = attribute.defaultValueAsString;
                    if (veValueStr == attributeValueStr)
                    {
                        continue;
                    }
                    overriddenAttributes.Add(attribute.name, veValueStr);
                }
            }

            return(overriddenAttributes);
        }
        void RefreshAttributeField(BindableElement fieldElement)
        {
            var styleRow  = fieldElement.GetProperty(BuilderConstants.InspectorLinkedStyleRowVEPropertyName) as VisualElement;
            var attribute = fieldElement.GetProperty(BuilderConstants.InspectorLinkedAttributeDescriptionVEPropertyName) as UxmlAttributeDescription;

            var veType = currentVisualElement.GetType();
            var camel  = BuilderNameUtilities.ConvertDashToCamel(attribute.name);

            var fieldInfo = veType.GetProperty(camel, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase);

            object veValueAbstract = null;

            if (fieldInfo == null)
            {
                veValueAbstract = GetCustomValueAbstract(attribute.name);
            }
            else
            {
                veValueAbstract = fieldInfo.GetValue(currentVisualElement);
            }
            if (veValueAbstract == null)
            {
                return;
            }

            var attributeType = attribute.GetType();
            var vea           = currentVisualElement.GetVisualElementAsset();

            if (attribute is UxmlStringAttributeDescription && fieldElement is TextField)
            {
                string value;
                if (veValueAbstract is Enum)
                {
                    value = (veValueAbstract as Enum).ToString();
                }
                else
                {
                    value = (string)veValueAbstract;
                }

                (fieldElement as TextField).SetValueWithoutNotify(value);
            }
            else if (attribute is UxmlFloatAttributeDescription && fieldElement is FloatField)
            {
                (fieldElement as FloatField).SetValueWithoutNotify((float)veValueAbstract);
            }
            else if (attribute is UxmlDoubleAttributeDescription && fieldElement is DoubleField)
            {
                (fieldElement as DoubleField).SetValueWithoutNotify((double)veValueAbstract);
            }
            else if (attribute is UxmlIntAttributeDescription && fieldElement is IntegerField)
            {
                if (veValueAbstract is int)
                {
                    (fieldElement as IntegerField).SetValueWithoutNotify((int)veValueAbstract);
                }
                else if (veValueAbstract is float)
                {
                    (fieldElement as IntegerField).SetValueWithoutNotify(Convert.ToInt32(veValueAbstract));
                }
            }
            else if (attribute is UxmlLongAttributeDescription && fieldElement is LongField)
            {
                (fieldElement as LongField).SetValueWithoutNotify((long)veValueAbstract);
            }
            else if (attribute is UxmlBoolAttributeDescription && fieldElement is Toggle)
            {
                (fieldElement as Toggle).SetValueWithoutNotify((bool)veValueAbstract);
            }
            else if (attribute is UxmlColorAttributeDescription && fieldElement is ColorField)
            {
                (fieldElement as ColorField).SetValueWithoutNotify((Color)veValueAbstract);
            }
            else if (attributeType.IsGenericType &&
                     !attributeType.GetGenericArguments()[0].IsEnum &&
                     attributeType.GetGenericArguments()[0] is Type &&
                     fieldElement is TextField textField &&
                     veValueAbstract is Type veTypeValue)
            {
                var fullTypeName      = veTypeValue.AssemblyQualifiedName;
                var fullTypeNameSplit = fullTypeName.Split(',');
                textField.SetValueWithoutNotify($"{fullTypeNameSplit[0]},{fullTypeNameSplit[1]}");
            }
Beispiel #5
0
        void RefreshAttributeField(BindableElement fieldElement)
        {
            var styleRow  = fieldElement.GetProperty(BuilderConstants.InspectorLinkedStyleRowVEPropertyName) as VisualElement;
            var attribute = fieldElement.GetProperty(BuilderConstants.InspectorLinkedAttributeDescriptionVEPropertyName) as UxmlAttributeDescription;

            var veType = currentVisualElement.GetType();
            var camel  = BuilderNameUtilities.ConvertDashToCamel(attribute.name);

            var fieldInfo = veType.GetProperty(camel, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);

            object veValueAbstract = null;

            if (fieldInfo == null)
            {
                veValueAbstract = GetCustomValueAbstract(attribute.name);
            }
            else
            {
                veValueAbstract = fieldInfo.GetValue(currentVisualElement);
            }
            if (veValueAbstract == null)
            {
                return;
            }

            var attributeType = attribute.GetType();
            var vea           = currentVisualElement.GetVisualElementAsset();

            if (attribute is UxmlStringAttributeDescription && fieldElement is TextField)
            {
                string value;
                if (veValueAbstract is Enum)
                {
                    value = (veValueAbstract as Enum).ToString();
                }
                else
                {
                    value = (string)veValueAbstract;
                }

                (fieldElement as TextField).SetValueWithoutNotify(value);
            }
            else if (attribute is UxmlFloatAttributeDescription && fieldElement is FloatField)
            {
                (fieldElement as FloatField).SetValueWithoutNotify((float)veValueAbstract);
            }
            else if (attribute is UxmlDoubleAttributeDescription && fieldElement is DoubleField)
            {
                (fieldElement as DoubleField).SetValueWithoutNotify((double)veValueAbstract);
            }
            else if (attribute is UxmlIntAttributeDescription && fieldElement is IntegerField)
            {
                (fieldElement as IntegerField).SetValueWithoutNotify((int)veValueAbstract);
            }
            else if (attribute is UxmlLongAttributeDescription && fieldElement is LongField)
            {
                (fieldElement as LongField).SetValueWithoutNotify((long)veValueAbstract);
            }
            else if (attribute is UxmlBoolAttributeDescription && fieldElement is Toggle)
            {
                (fieldElement as Toggle).SetValueWithoutNotify((bool)veValueAbstract);
            }
            else if (attribute is UxmlColorAttributeDescription && fieldElement is ColorField)
            {
                (fieldElement as ColorField).SetValueWithoutNotify((Color)veValueAbstract);
            }
            else if (attributeType.IsGenericType &&
                     attributeType.GetGenericArguments()[0].IsEnum &&
                     fieldElement is EnumField)
            {
                var propInfo  = attributeType.GetProperty("defaultValue");
                var enumValue = propInfo.GetValue(attribute, null) as Enum;

                // Create and initialize the EnumField.
                var uiField = fieldElement as EnumField;

                // Set the value from the UXML attribute.
                var enumAttributeValueStr = vea?.GetAttributeValue(attribute.name);
                if (!string.IsNullOrEmpty(enumAttributeValueStr))
                {
                    var parsedValue = Enum.Parse(enumValue.GetType(), enumAttributeValueStr, true) as Enum;
                    uiField.SetValueWithoutNotify(parsedValue);
                }
            }
            else if (fieldElement is TextField)
            {
                (fieldElement as TextField).SetValueWithoutNotify(veValueAbstract.ToString());
            }

            // Determine if overridden.
            styleRow.RemoveFromClassList(BuilderConstants.InspectorLocalStyleOverrideClassName);
            if (vea != null && attribute.name == "picking-mode")
            {
                var veaAttributeValue = vea.GetAttributeValue(attribute.name);
                if (veaAttributeValue != null && veaAttributeValue.ToLower() != attribute.defaultValueAsString.ToLower())
                {
                    styleRow.AddToClassList(BuilderConstants.InspectorLocalStyleOverrideClassName);
                }
            }
            else if (attribute.name == "name")
            {
                if (!string.IsNullOrEmpty(currentVisualElement.name))
                {
                    styleRow.AddToClassList(BuilderConstants.InspectorLocalStyleOverrideClassName);
                }
            }
            else if (vea != null && vea.HasAttribute(attribute.name))
            {
                styleRow.AddToClassList(BuilderConstants.InspectorLocalStyleOverrideClassName);
            }
        }