Пример #1
0
        public void SetPolymorphicElementDrawCallback(StratusOdinSerializedProperty serializedProperty)
        {
            this.drawElementCallback =
                (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                if (!serializedProperty.isExpanded)
                {
                    //EditorGUI.LabelField(rect, elementType.Name, elementLabelStyle);
                    //GUI.enabled = index == count;
                    return;
                }

                // Get the drawer for the element type
                var  element     = serializedProperty.GetArrayElementAtIndex(index);
                Type elementType = element.GetType();
                SerializedSystemObject.ObjectDrawer drawer = SerializedSystemObject.GetObjectDrawer(elementType);

                // Draw the element
                Rect position = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
                if (drawElementTypeLabel)
                {
                    EditorGUI.LabelField(position, elementType.Name, elementLabelStyle);
                    position.y += StratusEditorUtility.lineHeight;
                }
                drawer.DrawEditorGUI(position, element);
            };
        }
Пример #2
0
 //------------------------------------------------------------------------/
 // Messages
 //------------------------------------------------------------------------/
 protected override void OnSelectionChanged()
 {
     base.OnSelectionChanged();
     eventObject     = (Stratus.StratusEvent)Utilities.Reflection.Instantiate(selectedClass);
     serializedEvent = new SerializedSystemObject(selectedClass, eventObject);
     //serializedEvent.Deserialize(eventDataProperty);
 }
        public StratusOdinSerializedProperty(FieldInfo field, object target)
        {
            this.field        = field;
            this.type         = this.field.FieldType;
            this.propertyType = SerializedSystemObject.DeducePropertyType(this.field);
            this.displayName  = ObjectNames.NicifyVariableName(this.field.Name);
            this.target       = target;

            // Enum
            if (this.propertyType == SerializedPropertyType.Enum)
            {
                this.enumDisplayNames = StratusSearchableEnum.GetEnumDisplayNames(this.type);
            }

            // Array
            this.isArray = typeof(IList).IsAssignableFrom(this.type);
            if (this.isArray)
            {
                this.list            = this.field.GetValue(target) as IList;
                this.listElementType = Utilities.Reflection.GetIndexedType(list);
            }

            // Set the drawer
            this.drawer = SerializedSystemObject.GetObjectDrawer(this.isArray ? this.listElementType : this.type);
        }
        void UpdateEventObject()
        {
            if (!triggerable.hasType)
            {
                return;
            }

            eventObject     = (Stratus.Event)Utilities.Reflection.Instantiate(type);
            serializedEvent = new SerializedSystemObject(type, eventObject);
            serializedEvent.Deserialize(eventDataProperty);
        }
Пример #5
0
        //------------------------------------------------------------------------/
        // Methods
        //------------------------------------------------------------------------/


        /// <summary>
        /// Draws a list of elements deriving from a base class
        /// </summary>
        /// <returns>True if the height of the list changed, which signals a repaint event</returns>
        public static bool DrawPolymorphicList <T>(List <T> list, string title, bool useTypeLabel = true)
        {
            // We need to remember this list since the height is variable depending on the
            // amount of fields being drawn
            int hashCode = list.GetHashCode();

            if (!abstractListHeights.ContainsKey(hashCode))
            {
                abstractListHeights.Add(hashCode, 0);
            }

            IntegerRef maxCount = 0;

            ReorderableListGUI.Title(title);
            ReorderableListGUI.ListField(list, (Rect position, T value) =>
            {
                // Get the drawer
                Type type = value.GetType();
                SerializedSystemObject.ObjectDrawer drawer = SerializedSystemObject.GetObjectDrawer(type);

                // We draw one line at a time
                position.height = lineHeight;

                // Calculate height for this type
                int count = drawer.fieldCount;
                if (useTypeLabel)
                {
                    EditorGUI.LabelField(position, type.Name, EditorStyles.centeredGreyMiniLabel);
                    position.y += lineHeight;
                    count++;
                }
                if (count > maxCount)
                {
                    maxCount = count;
                }

                // Draw
                drawer.DrawEditorGUI(position, value);
                return(value);
            }, abstractListHeights[hashCode], ReorderableListFlags.HideAddButton);

            float currentHeight = maxCount * lineHeight;

            if (abstractListHeights[hashCode] != currentHeight)
            {
                abstractListHeights[hashCode] = currentHeight;
                return(true);
            }
            return(false);
        }
Пример #6
0
            //------------------------------------------------------------------------/
            // CTOR
            //------------------------------------------------------------------------/
            public FieldDrawer(FieldInfo field)
            {
                this.field = field;
                this.type  = this.field.FieldType;
                this.name  = ObjectNames.NicifyVariableName(field.Name);

                this.attributesByName.AddRange(this.field.GetCustomAttributes(attributeType), (Attribute attribute) => attribute.GetType());
                this.propertyType = SerializedSystemObject.DeducePropertyType(field);
                this.isPrimitive  = OdinSerializer.FormatterUtilities.IsPrimitiveType(this.type);
                this.isArray      = typeof(IList).IsAssignableFrom(this.type);
                // Drawable unless its meant to be hidden
                this.isDrawable = !this.attributesByName.ContainsKey(hideInInspectorAttribute);
                // All currently fields are eventually of a single line
                this.height = StratusEditorUtility.lineHeight;
            }
Пример #7
0
 public void SetPolymorphicElementHeightCallback(StratusOdinSerializedProperty serializedProperty)
 {
     elementHeightCallback = (int indexer) =>
     {
         if (!serializedProperty.isExpanded)
         {
             return(0);
             //return SerializedSystemObject.ObjectDrawer.lineHeight;
         }
         else
         {
             SerializedSystemObject.ObjectDrawer drawer = SerializedSystemObject.GetObjectDrawer(serializedProperty.GetArrayElementAtIndex(indexer));
             float height = drawer.height;
             // We add an additional line of height since we are drawing a label for polymorphic list
             if (drawElementTypeLabel)
             {
                 height += SerializedSystemObject.DefaultObjectDrawer.lineHeight;
             }
             return(height);
         }
     };
 }
Пример #8
0
        /// <summary>
        /// Draws a list of elements deriving from a base class
        /// </summary>
        /// <returns>True if the height of the list changed, which signals a repaint event</returns>
        public static bool DrawPolymorphicList(FieldInfo field, object target, string title, bool useTypeLabel = true)
        {
            // We need to remember this list since the height is variable depending on the
            // amount of fields being drawn
            var   fieldValue = field.GetValue(target);
            IList list       = fieldValue as IList;


            //EditorGUILayout.BeginVertical(ReorderableListStyles.Container);

            // Draw the header
            EditorGUILayout.BeginHorizontal(ReorderableListStyles.Title);
            EditorGUILayout.PrefixLabel(title);
            if (GUILayout.Button("Add", polymorphicListLayoutOptions))
            {
                Type     baseType  = Utilities.Reflection.GetIndexedType(list); // list.GetType().GetElementType();
                var      menu      = new GenericMenu();
                string[] typeNames = Utilities.Reflection.GetSubclassNames(baseType);
                menu.AddItems(typeNames, (int index) =>
                {
                    list.Add(Utilities.Reflection.Instantiate(Utilities.Reflection.GetSubclass(baseType)[index]));
                });
                menu.ShowAsContext();
            }

            // Clear
            if (GUILayout.Button("Clear", polymorphicListLayoutOptions))
            {
                list.Clear();
            }

            EditorGUILayout.EndHorizontal();

            if (list.Count == 0)
            {
                return(false);
            }

            // Draw the elements
            bool changed = false;

            EditorGUILayout.BeginVertical(ReorderableListStyles.Container2);
            foreach (var element in list)
            {
                EditorGUILayout.Space();

                // Get the drawer for the type
                Type elementType = element.GetType();
                SerializedSystemObject.ObjectDrawer drawer = SerializedSystemObject.GetObjectDrawer(elementType);

                EditorGUILayout.BeginVertical(ReorderableListStyles.Container);
                {
                    EditorGUILayout.LabelField(elementType.Name, EditorStyles.centeredGreyMiniLabel);
                    //if (!drawer.isDrawable)
                    //  EditorGUILayout.LabelField($"There are no serialized fields for {elementType.Name}");
                    //else
                    changed |= drawer.DrawEditorGUILayout(element);
                }
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.Space();
            EditorGUILayout.EndVertical();
            //EditorGUILayout.EndVertical();
            return(changed);
        }
Пример #9
0
 /// <summary>
 /// Draws a field using EditorGUILayout based on its members,
 /// (without using SerializedProperty)
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="field"></param>
 /// <returns>True if the field was changed</returns>
 public static bool DrawField(FieldInfo field, object target)
 {
     return(SerializedSystemObject.GetFieldDrawer(field).DrawEditorGUILayout(target));
 }
Пример #10
0
        /// <summary>
        /// Draws a field using EditorGUILayout based on its members,
        /// (without using SerializedProperty)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="field"></param>
        /// <returns>True if the field was changed</returns>
        public static bool DrawField <T>(T field)
        {
            Type type = field.GetType();

            return(SerializedSystemObject.GetObjectDrawer(type).DrawEditorGUILayout(field));
        }
Пример #11
0
        public static void DrawGUI(Rect position, FieldInfo field, object target)
        {
            SerializedPropertyType propertyType = SerializedSystemObject.DeducePropertyType(field);
            string name  = ObjectNames.NicifyVariableName(field.Name);
            object value = null;

            switch (propertyType)
            {
            case SerializedPropertyType.ObjectReference:
                value = EditorGUI.ObjectField(position, name, (UnityEngine.Object)field.GetValue(target), field.FieldType, true);
                break;

            case SerializedPropertyType.Integer:
                value = EditorGUI.IntField(position, field.GetValue <int>(target));
                break;

            case SerializedPropertyType.Boolean:
                value = EditorGUI.Toggle(position, name, field.GetValue <bool>(target));
                break;

            case SerializedPropertyType.Float:
                value = EditorGUI.FloatField(position, field.GetValue <float>(target));
                break;

            case SerializedPropertyType.String:
                value = EditorGUI.TextField(position, name, field.GetValue <string>(target));
                break;

            case SerializedPropertyType.Color:
                value = EditorGUI.ColorField(position, name, field.GetValue <Color>(target));
                break;

            case SerializedPropertyType.LayerMask:
                value = EditorGUI.LayerField(position, name, field.GetValue <LayerMask>(target));
                break;

            case SerializedPropertyType.Enum:
                StratusSearchableEnum.EnumPopup(position, name, field.GetValue <Enum>(target), (Enum selected) => field.SetValue(target, selected));
                break;

            case SerializedPropertyType.Vector2:
                value = EditorGUI.Vector2Field(position, name, field.GetValue <Vector2>(target));
                break;

            case SerializedPropertyType.Vector3:
                value = EditorGUI.Vector3Field(position, name, field.GetValue <Vector3>(target));
                break;

            case SerializedPropertyType.Vector4:
                value = EditorGUI.Vector4Field(position, name, field.GetValue <Vector4>(target));
                break;

            case SerializedPropertyType.Rect:
                value = EditorGUI.RectField(position, name, field.GetValue <Rect>(target));
                break;

            default:
                EditorGUI.LabelField(position, $"No supported drawer for type {field.FieldType.Name}!");
                break;
            }
        }