Example #1
0
 private void SetupReorder(ListAttribute listAttribute, ListField field, SerializedProperty property, Type declaringType)
 {
     if (field.AllowReorder)
     {
         if (!string.IsNullOrEmpty(listAttribute.ReorderCallback))
         {
             var reorderCallback = ReflectionHelper.CreateActionCallback(listAttribute.ReorderCallback, declaringType, property);
             if (reorderCallback != null)
             {
                 field.RegisterCallback <ListField.ItemReorderedEvent>(evt => reorderCallback.Invoke());
             }
             else
             {
                 var reorderCallbackFromTo = ReflectionHelper.CreateActionCallback <int, int>(listAttribute.ReorderCallback, declaringType, property);
                 if (reorderCallbackFromTo != null)
                 {
                     field.RegisterCallback <ListField.ItemReorderedEvent>(evt => reorderCallbackFromTo.Invoke(evt.FromIndex, evt.ToIndex));
                 }
                 else
                 {
                     Debug.LogWarningFormat(_invalidReorderCallbackWarning, property.propertyPath);
                 }
             }
         }
     }
 }
Example #2
0
        private void SetupAdd(ListAttribute listAttribute, PropertyListProxy proxy, ListField field, SerializedProperty property, Type declaringType, bool isReference)
        {
            if (field.AllowAdd)
            {
                if (!string.IsNullOrEmpty(listAttribute.AllowAdd))
                {
                    proxy.CanAddCallback = ReflectionHelper.CreateValueSourceFunction(listAttribute.AllowAdd, property, field, declaringType, true);
                }

                if (!string.IsNullOrEmpty(listAttribute.AddCallback))
                {
                    if (!isReference)
                    {
                        var addCallback = ReflectionHelper.CreateActionCallback(listAttribute.AddCallback, declaringType, property);
                        if (addCallback != null)
                        {
                            field.RegisterCallback <ListField.ItemAddedEvent>(evt => addCallback.Invoke());
                        }
                        else
                        {
                            var addCallbackIndex = ReflectionHelper.CreateActionCallback <int>(listAttribute.AddCallback, declaringType, property);
                            if (addCallbackIndex != null)
                            {
                                field.RegisterCallback <ListField.ItemAddedEvent>(evt => addCallbackIndex.Invoke(evt.Index));
                            }
                            else
                            {
                                Debug.LogWarningFormat(_invalidAddCallbackWarning, property.propertyPath);
                            }
                        }
                    }
                    else
                    {
                        var addCallback = ReflectionHelper.CreateActionCallback(listAttribute.AddCallback, declaringType, property);
                        if (addCallback != null)
                        {
                            field.RegisterCallback <ListField.ItemAddedEvent>(evt => addCallback.Invoke());
                        }
                        else
                        {
                            var addCallbackIndex = ReflectionHelper.CreateActionCallback <int>(listAttribute.AddCallback, declaringType, property);
                            if (addCallbackIndex != null)
                            {
                                field.RegisterCallback <ListField.ItemAddedEvent>(evt => addCallbackIndex.Invoke(evt.Index));
                            }
                            else
                            {
                                Debug.LogWarningFormat(_invalidAddReferenceCallbackWarning, property.propertyPath);
                            }
                        }
                    }
                }
            }
        }
Example #3
0
 private void SetupChange(ListAttribute listAttribute, ListField field, SerializedProperty property, Type declaringType)
 {
     if (!string.IsNullOrEmpty(listAttribute.ChangeCallback))
     {
         var changeCallback = ReflectionHelper.CreateActionCallback(listAttribute.ChangeCallback, declaringType, property);
         if (changeCallback != null)
         {
             field.RegisterCallback <ListField.ItemsChangedEvent>(evt => changeCallback.Invoke());
         }
         else
         {
             Debug.LogWarningFormat(_invalidChangeCallbackWarning, property.propertyPath);
         }
     }
 }
Example #4
0
        public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            var items = property.FindPropertyRelative(SerializedList <string> .ItemsProperty);

            if (items != null && items.isArray)
            {
                var isReference   = fieldInfo.FieldType.BaseType.GetGenericTypeDefinition() == typeof(ReferenceList <>);
                var referenceType = isReference ? fieldInfo.GetFieldType() : null;
                var declaringType = fieldInfo.DeclaringType;
                var listAttribute = attribute as ListAttribute;
                var drawer        = this.GetNextDrawer();
                var proxy         = new PropertyListProxy(items, drawer);

                var field = new ListField
                {
                    IsCollapsable = listAttribute.IsCollapsable,
                    bindingPath   = items.propertyPath,
                    Label         = property.displayName
                };

                // TODO: other stuff from ConfigureField

                if (!string.IsNullOrEmpty(listAttribute.EmptyLabel))
                {
                    field.EmptyLabel = listAttribute.EmptyLabel;
                }

                field.AllowAdd     = listAttribute.AllowAdd != ListAttribute.Never;
                field.AllowRemove  = listAttribute.AllowRemove != ListAttribute.Never;
                field.AllowReorder = listAttribute.AllowReorder;

                SetupAdd(listAttribute, proxy, field, property, declaringType, isReference);
                SetupRemove(listAttribute, proxy, field, property, declaringType);
                SetupReorder(listAttribute, field, property, declaringType);
                SetupChange(listAttribute, field, property, declaringType);

                field.SetProxy(proxy, referenceType, true);

                return(field);
            }
            else
            {
                Debug.LogWarningFormat(_invalidTypeWarning, property.propertyPath);
                return(new FieldContainer(property.displayName, string.Empty));
            }
        }
Example #5
0
        public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            var items = property.FindPropertyRelative(SerializedList <string> .ItemsProperty);

            if (items != null && items.isArray)
            {
                var listAttribute = attribute as ListAttribute;
                var parent        = property.GetOwner <object>();
                var proxy         = CreateProxy(property.displayName, items, listAttribute);

                if (TryGetMethod(listAttribute.AllowAdd, _missingAllowAddMethodWarning, property.propertyPath, out var allowAddMethod))
                {
                    var owner = allowAddMethod.IsStatic ? null : parent;
                    var count = CheckSignature(allowAddMethod, typeof(bool), false, false, _invalidAllowAddMethodWarning, property.propertyPath);
                    if (count == 0)
                    {
                        proxy.CanAddCallback += () => NoneConditional(allowAddMethod, owner);
                    }
                }

                if (TryGetMethod(listAttribute.AllowRemove, _missingAllowRemoveMethodWarning, property.propertyPath, out var allowRemoveMethod))
                {
                    var owner = allowRemoveMethod.IsStatic ? null : parent;
                    var count = CheckSignature(allowRemoveMethod, typeof(bool), true, false, _invalidAllowRemoveMethodWarning, property.propertyPath);
                    if (count == 0)
                    {
                        proxy.CanRemoveCallback += index => NoneConditional(allowRemoveMethod, owner);
                    }
                    else if (count == 1)
                    {
                        proxy.CanRemoveCallback += index => OneConditional(index, allowRemoveMethod, owner);
                    }
                }

                if (TryGetMethod(listAttribute.AllowReorder, _missingAllowReorderMethodWarning, property.propertyPath, out var allowReorderMethod))
                {
                    var owner = allowReorderMethod.IsStatic ? null : parent;
                    var count = CheckSignature(allowReorderMethod, typeof(bool), true, true, _invalidAllowReorderMethodWarning, property.propertyPath);
                    if (count == 0)
                    {
                        proxy.CanReorderCallback += (from, to) => NoneConditional(allowReorderMethod, owner);
                    }
                    else if (count == 1)
                    {
                        proxy.CanReorderCallback += (from, to) => OneConditional(to, allowReorderMethod, owner);
                    }
                    else if (count == 2)
                    {
                        proxy.CanReorderCallback += (from, to) => TwoConditional(from, to, allowReorderMethod, owner);
                    }
                }

                if (TryGetMethod(listAttribute.AddCallback, _missingAddMethodWarning, property.propertyPath, out var addMethod))
                {
                    var owner = addMethod.IsStatic ? null : parent;
                    var count = CheckSignature(addMethod, null, false, false, _invalidAddMethodWarning, property.propertyPath);
                    if (count == 0)
                    {
                        proxy.AddCallback += () => NoneCallback(addMethod, owner);
                    }
                }

                if (TryGetMethod(listAttribute.RemoveCallback, _missingRemoveMethodWarning, property.propertyPath, out var removeMethod))
                {
                    var owner = removeMethod.IsStatic ? null : parent;
                    var count = CheckSignature(removeMethod, null, true, false, _invalidRemoveMethodWarning, property.propertyPath);
                    if (count == 0)
                    {
                        proxy.RemoveCallback += index => NoneCallback(removeMethod, owner);
                    }
                    else if (count == 1)
                    {
                        proxy.RemoveCallback += index => OneCallback(index, removeMethod, owner);
                    }
                }

                if (TryGetMethod(listAttribute.ReorderCallback, _missingReorderMethodWarning, property.propertyPath, out var reorderMethod))
                {
                    var owner = reorderMethod.IsStatic ? null : parent;
                    var count = CheckSignature(reorderMethod, null, true, true, _invalidReorderMethodWarning, property.propertyPath);
                    if (count == 0)
                    {
                        proxy.ReorderCallback += (from, to) => NoneCallback(reorderMethod, owner);
                    }
                    else if (count == 1)
                    {
                        proxy.ReorderCallback += (from, to) => OneCallback(to, reorderMethod, owner);
                    }
                    else if (count == 2)
                    {
                        proxy.ReorderCallback += (from, to) => TwoCallback(from, to, reorderMethod, owner);
                    }
                }

                var field = new ListField();
                field.Setup(items, proxy);

                return(field);
            }
            else
            {
                Debug.LogWarningFormat(_invalidTypeWarning, property.propertyPath);
                return(new FieldContainer(property.displayName, string.Empty));
            }
        }
Example #6
0
        private void SetupRemove(ListAttribute listAttribute, PropertyListProxy proxy, ListField field, SerializedProperty property, Type declaringType)
        {
            if (field.AllowRemove)
            {
                if (!string.IsNullOrEmpty(listAttribute.AllowRemove))
                {
                    proxy.CanRemoveCallback = ReflectionHelper.CreateFunctionCallback <int, bool>(listAttribute.AllowRemove, declaringType, property);

                    if (proxy.CanRemoveCallback == null)
                    {
                        var canRemove = ReflectionHelper.CreateValueSourceFunction(listAttribute.AllowRemove, property, field, declaringType, true);
                        proxy.CanRemoveCallback = index => canRemove();
                    }
                }

                if (!string.IsNullOrEmpty(listAttribute.RemoveCallback))
                {
                    var removeCallback = ReflectionHelper.CreateActionCallback(listAttribute.RemoveCallback, declaringType, property);
                    if (removeCallback != null)
                    {
                        field.RegisterCallback <ListField.ItemRemovedEvent>(evt => removeCallback.Invoke());
                    }
                    else
                    {
                        var removeCallbackIndex = ReflectionHelper.CreateActionCallback <int>(listAttribute.RemoveCallback, declaringType, property);
                        if (removeCallbackIndex != null)
                        {
                            field.RegisterCallback <ListField.ItemRemovedEvent>(evt => removeCallbackIndex.Invoke(evt.Index));
                        }
                        else
                        {
                            Debug.LogWarningFormat(_invalidRemoveCallbackWarning, property.propertyPath);
                        }
                    }
                }
            }
        }