Exemplo n.º 1
0
        public StyleProperty ToStyleProperty()
        {
            var property = NameValueBase.CreateProperty <StyleProperty>(Name, Type);

            property.Value = Default;
            return((StyleProperty)property);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Snapshot after
 /// </summary>
 /// <param name="array"></param>
 public void SnapshotAfter(NameValueBase[] array)
 {
     _after.Clear();
     foreach (NameValueBase styleProperty in array)
     {
         var value = styleProperty.Value;
         if (!Equals(null, value)) // we are considering null to act as the StyleDeclaration.UNDEFINED here, because we want to fall back to hardcoded values
             _after[styleProperty.Name] = value;       // TODO: Examine why the null comparison allows this
     }
 }
        /// <summary>
        /// Extract a sub property (Int, String, Bool... etc.) from the serialized property container
        /// </summary>
        /// <param name="property"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static SerializedProperty GetSlot(SerializedProperty property, ref Type type)
        {
            SerializedProperty serializedTypeProp = property.FindPropertyRelative("SerializedType");
            var enumValue =
                (SerializedType)Enum.GetValues(typeof(SerializedType)).GetValue(serializedTypeProp.enumValueIndex);

            var propertyName = NameValueBase.GetSlotName(enumValue);

            if ("ObjectReference" == propertyName)
            {
                type = GetType(property);
            }

            return(property.FindPropertyRelative(propertyName));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads the style property value from a serialized value<br/>
        /// Used by the system when in need to read the serialized value
        /// </summary>
        /// <param name="serializedProperty"></param>
        public static object Read(SerializedProperty serializedProperty)
        {
            var typeStr        = serializedProperty.FindPropertyRelative("Type").stringValue;
            var serializedType = (SerializedType)serializedProperty.FindPropertyRelative("SerializedType").enumValueIndex;

            //var type = value.GetType();
            if (!GlobalTypeDictionary.Instance.ContainsKey(typeStr))
            {
                throw new Exception("Couldn't get type from GlobalTypeDictionary: " + typeStr);
            }

            var type      = GlobalTypeDictionary.Instance[typeStr];
            var slotName  = NameValueBase.GetSlotName(serializedType);
            var valueProp = serializedProperty.FindPropertyRelative(slotName);

            if (type == typeof(int))
            {
                return(valueProp.intValue);
            }
            if (type == typeof(bool))
            {
                return(valueProp.boolValue);
            }
            if (type == typeof(float))
            {
                return(valueProp.floatValue);
            }
            if (type == typeof(string))
            {
                return(valueProp.stringValue);
            }
            if (type == typeof(Color))
            {
                return(valueProp.colorValue);
            }

            /*else if (type == typeof(LayerMask))
             *  valueProp.objectReferenceValue = (LayerMask)styleProperty.Value;*/
            if (type.IsEnum)  // == typeof(Enum))
            {
                return(Enum.Parse(GlobalTypeDictionary.Instance.Get(typeStr), valueProp.stringValue));
            }
            if (type == typeof(Vector2))
            {
                return(valueProp.vector2Value);
            }
            if (type == typeof(Vector3))
            {
                return(valueProp.vector3Value);
            }
            if (type == typeof(Rect))
            {
                return(valueProp.rectValue);
            }

            /*else if (type == typeof(Char))
             *  valueProp.stringValue = (Char)styleProperty.Value;*/
            if (type == typeof(AnimationCurve))
            {
                return(valueProp.animationCurveValue);
            }
            if (type == typeof(Bounds))
            {
                return(valueProp.boundsValue);
            }

            /*else if (type == typeof(Gradient))
             *  valueProp.Gradient = (Gradient)styleProperty.Value;*/
            if (type == typeof(Quaternion))
            {
                return(valueProp.quaternionValue);
            }
            if (type == typeof(UnityEngine.Object))
            {
                return(valueProp.objectReferenceValue);
            }

            return(null);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Applies the style property value to a serialized value<br/>
        /// Used by the system when in need to update the serialized value (either just created or edited)
        /// </summary>
        /// <param name="serializedProperty"></param>
        /// <param name="styleProperty"></param>
        public static void Apply(SerializedProperty serializedProperty, NameValueBase styleProperty)
        {
            serializedProperty.FindPropertyRelative("Type").stringValue =
                styleProperty.Type;
            serializedProperty.FindPropertyRelative("SerializedType").enumValueIndex =
                (int)styleProperty.SerializedType;

            var value = styleProperty.Value;

            if (null != value)
            {
                //var type = value.GetType();
                if (!GlobalTypeDictionary.Instance.ContainsKey(styleProperty.Type))
                {
                    throw new Exception("Couldn't get type from GlobalTypeDictionary: " + styleProperty.Type);
                }

                var type      = GlobalTypeDictionary.Instance[styleProperty.Type];
                var slotName  = NameValueBase.GetSlotName(styleProperty.SerializedType);
                var valueProp = serializedProperty.FindPropertyRelative(slotName);

                if (type == typeof(int))
                {
                    valueProp.intValue = (int)styleProperty.Value;
                }
                else if (type == typeof(bool))
                {
                    valueProp.boolValue = (bool)styleProperty.Value;
                }
                else if (type == typeof(float))
                {
                    valueProp.floatValue = (float)styleProperty.Value;
                }
                else if (type == typeof(string))
                {
                    valueProp.stringValue = (string)styleProperty.Value;
                }
                else if (type == typeof(Color))
                {
                    valueProp.colorValue = (Color)styleProperty.Value;
                }

                /*else if (type == typeof(LayerMask))
                 *  valueProp.objectReferenceValue = (LayerMask)styleProperty.Value;*/
                else if (type.IsEnum)
                {// == typeof(Enum))
                    //valueProp.intValue = (int)styleProperty.Value;
                    valueProp.stringValue = Enum.GetName(GlobalTypeDictionary.Instance.Get(styleProperty.Type), value);
                }
                else if (type == typeof(Vector2))
                {
                    valueProp.vector2Value = (Vector2)styleProperty.Value;
                }
                else if (type == typeof(Vector3))
                {
                    valueProp.vector3Value = (Vector3)styleProperty.Value;
                }
                else if (type == typeof(Rect))
                {
                    valueProp.rectValue = (Rect)styleProperty.Value;
                }

                /*else if (type == typeof(Char))
                 *  valueProp.stringValue = (Char)styleProperty.Value;*/
                else if (type == typeof(AnimationCurve))
                {
                    valueProp.animationCurveValue = (AnimationCurve)styleProperty.Value;
                }
                else if (type == typeof(Bounds))
                {
                    valueProp.boundsValue = (Bounds)styleProperty.Value;
                }

                /*else if (type == typeof(Gradient))
                 *  valueProp.Gradient = (Gradient)styleProperty.Value;*/
                else if (type == typeof(Quaternion))
                {
                    valueProp.quaternionValue = (Quaternion)styleProperty.Value;
                }
                else if (type == typeof(UnityEngine.Object))
                {
                    valueProp.objectReferenceValue = (UnityEngine.Object)styleProperty.Value;
                }
            }
        }
Exemplo n.º 6
0
 public StyleProperty ToStyleProperty()
 {
     return((StyleProperty)NameValueBase.CreateProperty <StyleProperty>(Name, Type));
 }
Exemplo n.º 7
0
        /// <summary>
        /// Re-scanns all the media queries on demand
        /// </summary>
        /// <exception cref="Exception"></exception>
        public void Rescan()
        {
            _methodInfos = new Dictionary <string, MethodInfo>();
            _queries     = new Dictionary <string, MediaQuery>();

            /*foreach (Type type in GlobalTypeDictionary.Instance.Values)
             * {
             *  if (typeof(MediaQueryBase).IsAssignableFrom(type) && typeof(MediaQueryBase) != type)
             *  {
             *      MediaQueryBase query = (MediaQueryBase)Activator.CreateInstance(type);
             *      _queries[query.Id] = query;
             *  }
             * }*/

            var queries = GuiReflector.GetMethodsInAllLoadedTypesDecoratedWith(typeof(MediaQueryAttribute));

#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Found {0} media queries", queries.Count));
            }
#endif

            foreach (var methodInfo in queries)
            {
                if (null == methodInfo.ReturnParameter || methodInfo.ReturnType != typeof(bool))
                {
                    throw new Exception(@"Method decorated with MediaQuery attribute should return a boolean value:
" + methodInfo);
                }

                var p          = methodInfo.GetParameters();
                var parameters = new List <Type>();
                foreach (var parameterInfo in p)
                {
                    parameters.Add(parameterInfo.ParameterType);
                }

                // get attribute
                var attributes = CoreReflector.GetMethodAttributes <MediaQueryAttribute>(methodInfo);
                if (attributes.Count == 0)
                {
                    throw new Exception(@"Cannot find MediaQuery attribute:
" + methodInfo);
                }

                var attribute = attributes[0];

                /**
                 * If there is already an existing query with a same ID, allow overriding
                 * only if this is an editor override. This way we'll get all the editor overrides
                 * override the Play mode media queries.
                 * */
                if (_methodInfos.ContainsKey(attribute.Id) && !attribute.EditorOverride)
                {
                    continue;
                }

                _methodInfos[attribute.Id] = methodInfo;

                MediaQuery query;
                if (parameters.Count > 0)
                {
                    var type = parameters[0];
                    query = (MediaQuery)NameValueBase.CreateProperty <MediaQuery>(attribute.Id, type);
                    //query.Value = type.
                }
                else
                {
                    query = new MediaQuery
                    {
                        Name       = attribute.Id,
                        Parameters = parameters.ToArray()
                    };
                }
                _queries[attribute.Id] = query;
            }

#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Number of queries after overrides: {0}", _queries.Keys.Count));
            }
#endif

#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Retrieved {0} media queries:
{1}", _queries.Count, DictionaryUtil <string, MediaQuery> .Format(_queries)));
            }
#endif
        }
        /// <summary>
        /// Applies the style property value to a serialized value<br/>
        /// Used by the system when in need to update the serialized value (either just created or edited)
        /// </summary>
        /// <param name="serializedProperty"></param>
        /// <param name="styleProperty"></param>
        public static void Apply(SerializedProperty serializedProperty, NameValueBase styleProperty)
        {
            serializedProperty.FindPropertyRelative("Type").stringValue =
                styleProperty.Type;
            serializedProperty.FindPropertyRelative("SerializedType").enumValueIndex =
                (int)styleProperty.SerializedType;

            var value = styleProperty.Value;
            if (null != value)
            {
                //var type = value.GetType();
                if (!GlobalTypeDictionary.Instance.ContainsKey(styleProperty.Type))
                    throw new Exception("Couldn't get type from GlobalTypeDictionary: " + styleProperty.Type);

                var type = GlobalTypeDictionary.Instance[styleProperty.Type];
                var slotName = NameValueBase.GetSlotName(styleProperty.SerializedType);
                var valueProp = serializedProperty.FindPropertyRelative(slotName);

                if (type == typeof(int))
                    valueProp.intValue = (int)styleProperty.Value;
                else if (type == typeof(bool))
                    valueProp.boolValue = (bool)styleProperty.Value;
                else if (type == typeof(float))
                    valueProp.floatValue = (float)styleProperty.Value;
                else if (type == typeof(string))
                    valueProp.stringValue = (string)styleProperty.Value;
                else if (type == typeof(Color))
                    valueProp.colorValue = (Color)styleProperty.Value;
                /*else if (type == typeof(LayerMask))
                    valueProp.objectReferenceValue = (LayerMask)styleProperty.Value;*/
                else if (type.IsEnum)
                {// == typeof(Enum)) 
                    //valueProp.intValue = (int)styleProperty.Value;
                    valueProp.stringValue = Enum.GetName(GlobalTypeDictionary.Instance.Get(styleProperty.Type), value);
                }
                else if (type == typeof(Vector2))
                    valueProp.vector2Value = (Vector2)styleProperty.Value;
                else if (type == typeof(Vector3))
                    valueProp.vector3Value = (Vector3)styleProperty.Value;
                else if (type == typeof(Rect))
                    valueProp.rectValue = (Rect)styleProperty.Value;
                /*else if (type == typeof(Char))
                    valueProp.stringValue = (Char)styleProperty.Value;*/
                else if (type == typeof(AnimationCurve))
                    valueProp.animationCurveValue = (AnimationCurve)styleProperty.Value;
                else if (type == typeof(Bounds))
                    valueProp.boundsValue = (Bounds)styleProperty.Value;
                /*else if (type == typeof(Gradient))
                    valueProp.Gradient = (Gradient)styleProperty.Value;*/
                else if (type == typeof(Quaternion))
                    valueProp.quaternionValue = (Quaternion)styleProperty.Value;
                else if (type == typeof(UnityEngine.Object))
                    valueProp.objectReferenceValue = (UnityEngine.Object)styleProperty.Value;
            }
        }