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); } } } } } }
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)); } }
private PropertyListProxy CreateProxy(string label, SerializedProperty property, ListAttribute listAttribute) { var drawer = this.GetNextDrawer(); var tooltip = this.GetTooltip(); var proxy = new PropertyListProxy(property, drawer) { Label = label, Tooltip = tooltip, AllowAdd = listAttribute.AllowAdd != null, AllowRemove = listAttribute.AllowRemove != null, AllowReorder = listAttribute.AllowReorder != null }; if (listAttribute.EmptyLabel != null) { proxy.EmptyLabel = listAttribute.EmptyLabel; } return(proxy); }
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); } } } } }