Пример #1
0
        private void SetupAdd(DictionaryAttribute dictionaryAttribute, PropertyDictionaryProxy proxy, DictionaryField field, SerializedProperty property, Type declaringType, bool isReference)
        {
            if (field.AllowAdd)
            {
                if (!string.IsNullOrEmpty(dictionaryAttribute.AllowAdd))
                {
                    proxy.CanAddKeyCallback = ReflectionHelper.CreateFunctionCallback <string, bool>(dictionaryAttribute.AllowAdd, declaringType, property);
                    if (proxy.CanAddKeyCallback == null)
                    {
                        var canAdd = ReflectionHelper.CreateValueSourceFunction(dictionaryAttribute.AllowAdd, property, field, declaringType, true);
                        proxy.CanAddKeyCallback = index => canAdd();
                    }
                }

                if (!string.IsNullOrEmpty(dictionaryAttribute.AddCallback))
                {
                    if (!isReference)
                    {
                        var addCallback = ReflectionHelper.CreateActionCallback(dictionaryAttribute.AddCallback, declaringType, property);
                        if (addCallback != null)
                        {
                            field.RegisterCallback <DictionaryField.ItemAddedEvent>(evt => addCallback.Invoke());
                        }
                        else
                        {
                            var addCallbackKey = ReflectionHelper.CreateActionCallback <string>(dictionaryAttribute.AddCallback, declaringType, property);
                            if (addCallbackKey != null)
                            {
                                field.RegisterCallback <DictionaryField.ItemAddedEvent>(evt => addCallbackKey.Invoke(evt.Key));
                            }
                            else
                            {
                                Debug.LogWarningFormat(_invalidAddCallbackWarning, property.propertyPath);
                            }
                        }
                    }
                    else
                    {
                        var addCallback = ReflectionHelper.CreateActionCallback(dictionaryAttribute.AddCallback, declaringType, property);
                        if (addCallback != null)
                        {
                            field.RegisterCallback <DictionaryField.ItemAddedEvent>(evt => addCallback.Invoke());
                        }
                        else
                        {
                            var addCallbackKey = ReflectionHelper.CreateActionCallback <string>(dictionaryAttribute.AddCallback, declaringType, property);
                            if (addCallbackKey != null)
                            {
                                field.RegisterCallback <DictionaryField.ItemAddedEvent>(evt => addCallbackKey.Invoke(evt.Key));
                            }
                            else
                            {
                                Debug.LogWarningFormat(_invalidAddReferenceCallbackWarning, property.propertyPath);
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            var keys   = property.FindPropertyRelative(SerializedDictionary <string, int> .KeyProperty);
            var values = property.FindPropertyRelative(SerializedDictionary <string, int> .ValueProperty);

            if (keys != null && keys.isArray && values != null && values.isArray && keys.arrayElementType == "string")
            {
                var isReference         = fieldInfo.FieldType.BaseType.GetGenericTypeDefinition() == typeof(ReferenceDictionary <,>);
                var referenceType       = isReference ? fieldInfo.GetFieldType() : null;
                var declaringType       = fieldInfo.DeclaringType;
                var dictionaryAttribute = attribute as DictionaryAttribute;
                var drawer = this.GetNextDrawer();
                var proxy  = new PropertyDictionaryProxy(property, keys, values, drawer);

                var field = new DictionaryField
                {
                    IsCollapsable = dictionaryAttribute.IsCollapsable,
                    bindingPath   = keys.propertyPath,
                    Label         = property.displayName
                };

                // TODO: other stuff from ConfigureField

                if (!string.IsNullOrEmpty(dictionaryAttribute.AddPlaceholder))
                {
                    field.AddPlaceholder = dictionaryAttribute.AddPlaceholder;
                }

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

                field.AllowAdd     = dictionaryAttribute.AllowAdd != DictionaryAttribute.Never;
                field.AllowRemove  = dictionaryAttribute.AllowRemove != DictionaryAttribute.Never;
                field.AllowReorder = dictionaryAttribute.AllowReorder;

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

                field.SetProxy(proxy, referenceType, true);

                return(field);
            }
            else
            {
                Debug.LogWarningFormat(_invalidTypeWarning, property.propertyPath);
                return(new FieldContainer(property.displayName));
            }
        }
Пример #3
0
        private PropertyDictionaryProxy CreateProxy(SerializedProperty property, SerializedProperty keys, SerializedProperty values, DictionaryAttribute dictionaryAttribute)
        {
            var drawer  = this.GetNextDrawer();
            var tooltip = this.GetTooltip();
            var proxy   = new PropertyDictionaryProxy(property, keys, values, drawer)
            {
                Tooltip      = tooltip,
                AllowAdd     = dictionaryAttribute.AllowAdd != null,
                AllowRemove  = dictionaryAttribute.AllowRemove != null,
                AllowReorder = dictionaryAttribute.AllowReorder != null
            };

            if (dictionaryAttribute.EmptyLabel != null)
            {
                proxy.EmptyLabel = dictionaryAttribute.EmptyLabel;
            }

            return(proxy);
        }