protected override void OnEnable() { Target = target as TwoWayPropertyBinding; base.OnEnable(); ViewAdapterOptions = serializedObject.FindProperty("_viewAdapterOptions"); ViewModelAdapterOptions = serializedObject.FindProperty("_viewModelAdapterOptions"); }
/// <summary> /// Show dropdown for selecting a UnityEvent to bind to. /// </summary> private int ShowEventSelector(TwoWayPropertyBinding targetScript) { return(EditorGUILayout.Popup( new GUIContent("Event to bind to"), events.Select(evt => evt.Name) .ToList() .IndexOf(targetScript.uiEventName), events.Select(evt => new GUIContent(evt.DeclaringType + "." + evt.Name + "(" + evt.GetEventTypes()[0].ToString() + ")")).ToArray())); }
protected override void OnEnabled() { targetScript = (TwoWayPropertyBinding)target; viewAdapterOptionsFade = new AnimBool(ShouldShowAdapterOptions(targetScript.ViewAdapterId, out _)); viewModelAdapterOptionsFade = new AnimBool(ShouldShowAdapterOptions(targetScript.ViewModelAdapterId, out _)); exceptionAdapterOptionsFade = new AnimBool(ShouldShowAdapterOptions(targetScript.ExceptionAdapterTypeName, out _)); viewAdapterOptionsFade.valueChanged.AddListener(Repaint); viewModelAdapterOptionsFade.valueChanged.AddListener(Repaint); exceptionAdapterOptionsFade.valueChanged.AddListener(Repaint); }
/// <summary> /// Shows a dropdown for selecting a property in the UI to bind to. /// </summary> private int ShowUIPropertySelector(TwoWayPropertyBinding targetScript) { return(EditorGUILayout.Popup( new GUIContent("Property to bind to"), properties.Select(prop => prop.PropertyInfo.Name) .ToList() .IndexOf(targetScript.uiPropertyName), properties.Select(prop => new GUIContent(prop.PropertyInfo.ReflectedType.Name + "/" + prop.PropertyInfo.Name + " : " + prop.PropertyInfo.PropertyType.Name)).ToArray())); }
private void ShowViewModelPropertySelector(TwoWayPropertyBinding target, PropertyInfo[] bindableProperties, Type viewPropertyType) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("View model property"); var dropdownPosition = GUILayoutUtility.GetLastRect(); dropdownPosition.x += dropdownPosition.width; if (GUILayout.Button(new GUIContent(target.viewModelPropertyName), EditorStyles.popup)) { ShowViewModelPropertyMenu(target, bindableProperties, viewPropertyType, dropdownPosition); } EditorGUILayout.EndHorizontal(); }
/// <summary> /// Draws the dropdown menu for picking a property in the view model to bind to. /// </summary> private void ShowViewModelPropertyMenu(TwoWayPropertyBinding target, PropertyInfo[] bindableProperties, Type viewPropertyType, Rect position) { var selectedIndex = Array.IndexOf( bindableProperties.Select(p => p.ReflectedType + p.Name).ToArray(), target.viewModelName + target.viewModelPropertyName ); var options = bindableProperties.Select(p => new InspectorUtils.MenuItem( new GUIContent(p.ReflectedType + "/" + p.Name + " : " + p.PropertyType.Name), p.PropertyType == viewPropertyType ) ).ToArray(); InspectorUtils.ShowCustomSelectionMenu(index => SetViewModelProperty(target, bindableProperties[index]), options, selectedIndex, position); }
private void OnEnable() { targetScript = (TwoWayPropertyBinding)target; Type adapterType; viewAdapterOptionsFade = new AnimBool( ShouldShowAdapterOptions(targetScript.viewAdapterTypeName, out adapterType) ); viewModelAdapterOptionsFade = new AnimBool( ShouldShowAdapterOptions(targetScript.viewModelAdapterTypeName, out adapterType) ); exceptionAdapterOptionsFade = new AnimBool( ShouldShowAdapterOptions(targetScript.exceptionAdapterTypeName, out adapterType) ); viewAdapterOptionsFade.valueChanged.AddListener(Repaint); viewModelAdapterOptionsFade.valueChanged.AddListener(Repaint); exceptionAdapterOptionsFade.valueChanged.AddListener(Repaint); }
static void Main() { #region Plain Property to Plain Property Binding Console.WriteLine("Plain Prop to Plain Prop Two Way Binding Test"); // source object Address address = new Address { City = "Boston" }; // target object PropDisplayerAndModifyer cityPropertyDisplayerAndModifier = new PropDisplayerAndModifyer("City"); TwoWayPropertyBinding<string, string> cityBinding = new TwoWayPropertyBinding<string, string>(); // set the forward getters and setters cityBinding.ForwardSourcePropertyGetter = new PlainPropWithDefaultGetter<string>("City") { TheObj = address }; cityBinding.ForwardTargetPropertySetter = new PlainPropertySetter<string>("PropValue") { TheObj = cityPropertyDisplayerAndModifier }; // set the reverse getters and setters cityBinding.ReverseSourcePropertyGetter = new PlainPropWithDefaultGetter<string>("PropValue") { TheObj = cityPropertyDisplayerAndModifier }; cityBinding.ReverseTargetPropertySetter = new PlainPropertySetter<string>("City") { TheObj = address }; Console.WriteLine("Before binding is set the City property should be null on printProp object"); cityPropertyDisplayerAndModifier.Print(); // bind the source to the target cityBinding.Bind(); Console.WriteLine("After binding is set the City property should be 'Boston' on printProp object"); cityPropertyDisplayerAndModifier.Print(); address.City = "Brookline"; Console.WriteLine("After source's property was changed to 'Brookline', the target property also changes"); cityPropertyDisplayerAndModifier.Print(); cityPropertyDisplayerAndModifier.PropValue = "Allston"; Console.WriteLine("Address: '" + address.City + "'"); Console.WriteLine("After target's property was changed to 'Allston', the source property also changes"); #endregion Plain Property to Plain Property Bindin }
/// <summary> /// Get a list of all the methods in the bound view model of a specific type that we can bind to. /// </summary> private PropertyInfo[] GetBindableViewModelProperties(TwoWayPropertyBinding target) { return(target.GetAvailableViewModelTypes() .SelectMany(type => type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) .ToArray()); }
/// <summary> /// Set up the viewModelName and viewModelPropertyname in the TwoWayPropertyBinding we're editing. /// </summary> private void SetViewModelProperty(TwoWayPropertyBinding target, PropertyInfo propertyInfo) { target.viewModelName = propertyInfo.ReflectedType.Name; target.viewModelPropertyName = propertyInfo.Name; }