Skip to content

Commit

Permalink
Added notion of convertors and toggle bindings
Browse files Browse the repository at this point in the history
Added convertors so you can bind ReactiveProperty types which are not a direct match to the underlying bindee, also added toggle bindings and added some more examples.
  • Loading branch information
grofit committed Mar 9, 2017
1 parent 8c3aa58 commit 7151c48
Show file tree
Hide file tree
Showing 29 changed files with 272 additions and 51 deletions.
1 change: 1 addition & 0 deletions docs/binding-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Currently there is support for built in bindings on:
- Text
- Slider
- Dropdown
- Toggle
- RectTransform
- ICanvasElement implementations

Expand Down
3 changes: 3 additions & 0 deletions src/Assets/BindingsRx/Convertors.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/Assets/BindingsRx/Convertors/IConvertor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BindingsRx.Convertors
{
public interface IConvertor<T1, T2>
{
T1 From(T2 value);
T2 From(T1 value);
}
}
3 changes: 3 additions & 0 deletions src/Assets/BindingsRx/Convertors/IConvertor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Assets/BindingsRx/Convertors/IntConvertor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace BindingsRx.Convertors
{
public class TextToIntConvertor : IConvertor<string, int>
{
public string From(int value)
{ return value.ToString(); }

public int From(string value)
{
int output;
int.TryParse(value, out output);
return output;
}
}
}
3 changes: 3 additions & 0 deletions src/Assets/BindingsRx/Convertors/IntConvertor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Assets/BindingsRx/Convertors/TextToDoubleConvertor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace BindingsRx.Convertors
{
public class TextToDoubleConvertor : IConvertor<string, double>
{
public string From(double value)
{ return value.ToString(); }

public double From(string value)
{
double output;
double.TryParse(value, out output);
return output;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Assets/BindingsRx/Convertors/TextToFloatConvertor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace BindingsRx.Convertors
{
public class TextToFloatConvertor : IConvertor<string, float>
{
public string From(float value)
{ return value.ToString(); }

public float From(string value)
{
float output;
float.TryParse(value, out output);
return output;
}
}
}
3 changes: 3 additions & 0 deletions src/Assets/BindingsRx/Convertors/TextToFloatConvertor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Assets/BindingsRx/Extensions/IReactivePropertyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UniRx;

namespace BindingsRx.Extensions
{
public static class IReactivePropertyExtensions
{
public static ReactiveProperty<string> ToTextualProperty<T>(this IReactiveProperty<T> nonStringProperty)
{ return nonStringProperty.Select(x => x.ToString()).ToReactiveProperty(); }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/Assets/BindingsRx/GameObjects/MonobehaviourExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using BindingsRx.Filters;
using BindingsRx.Generic;
using BindingsRx.UI;
using UniRx;
using UnityEngine;

namespace BindingsRx.GameObjects
{
public static class MonobehaviourExtensions
public static class MonoBehaviourExtensions
{
public static IDisposable BindEnabledTo(this MonoBehaviour input, IReactiveProperty<bool> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<bool>[] filters)
{ return GenericBindings.Bind(() => input.enabled, x => input.enabled = x, property, bindingType, filters).AddTo(input); }

public static IDisposable BindActiveTo(this MonoBehaviour input, Func<bool> getter, Action<bool> setter, BindingTypes bindingType = BindingTypes.Default, params IFilter<bool>[] filters)
public static IDisposable BindEnabledTo(this MonoBehaviour input, Func<bool> getter, Action<bool> setter, BindingTypes bindingType = BindingTypes.Default, params IFilter<bool>[] filters)
{ return GenericBindings.Bind(() => input.enabled, x => input.enabled = x, getter, setter, bindingType, filters).AddTo(input); }
}
}
59 changes: 42 additions & 17 deletions src/Assets/BindingsRx/Generic/GenericBindings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BindingsRx.Convertors;
using BindingsRx.Exceptions;
using BindingsRx.Extensions;
using BindingsRx.Filters;
Expand Down Expand Up @@ -26,8 +27,8 @@ public static IDisposable Bind<T>(IReactiveProperty<T> propertyA , IReactiveProp

return new CompositeDisposable(propertyABinding, propertyBBinding);
}

public static IDisposable Bind<T>(Func<IObservable<T>> propertyAGetter, Action<T> propertyASetter, IReactiveProperty<T> propertyB, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T>[] filters)
public static IDisposable Bind<T>(Func<T> propertyAGetter, Action<T> propertyASetter, IReactiveProperty<T> propertyB, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T>[] filters)
{
var propertyBBinding = propertyB
.ApplyInputFilters(filters)
Expand All @@ -37,59 +38,82 @@ public static IDisposable Bind<T>(Func<IObservable<T>> propertyAGetter, Action<T
if (bindingTypes == BindingTypes.OneWay)
{ return propertyBBinding; }

var propertyABinding = propertyAGetter()
var propertyABinding = Observable.EveryUpdate()
.Select(x => propertyAGetter())
.ApplyOutputFilters(filters)
.DistinctUntilChanged()
.Subscribe(x => propertyB.Value = x);
.Subscribe(x => propertyB.Value = propertyAGetter());

return new CompositeDisposable(propertyABinding, propertyBBinding);
}

public static IDisposable Bind<T>(Func<T> propertyAGetter, Action<T> propertyASetter, IReactiveProperty<T> propertyB, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T>[] filters)
public static IDisposable Bind<T>(Func<T> propertyAGetter, Action<T> propertyASetter, Func<T> propertyBGetter, Action<T> propertyBSetter, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T>[] filters)
{
var propertyBBinding = propertyB
var propertyBBinding = Observable.EveryUpdate()
.Select(x => propertyBGetter())
.ApplyInputFilters(filters)
.DistinctUntilChanged()
.Subscribe(propertyASetter);

if (bindingTypes == BindingTypes.OneWay)
{ return propertyBBinding; }

if (propertyBSetter == null)
{ throw new SetterNotProvidedException(); }

var propertyABinding = Observable.EveryUpdate()
.Select(x => propertyAGetter())
.ApplyOutputFilters(filters)
.DistinctUntilChanged()
.Subscribe(x => propertyB.Value = propertyAGetter());
.Subscribe(propertyBSetter);

return new CompositeDisposable(propertyABinding, propertyBBinding);
}

public static IDisposable Bind<T>(Func<IObservable<T>> propertyAGetter, Action<T> propertyASetter, Func<T> propertyBGetter, Action<T> propertyBSetter, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T>[] filters)
public static IDisposable Bind<T1, T2>(IReactiveProperty<T1> propertyA, IReactiveProperty<T2> propertyB, IConvertor<T1, T2> convertor, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T1>[] filters)
{
var propertyBBinding = Observable.EveryUpdate()
.Select(x => propertyBGetter())
var propertyBBinding = propertyB
.Select(convertor.From)
.ApplyInputFilters(filters)
.DistinctUntilChanged()
.Subscribe(propertyASetter);
.Subscribe(x => propertyA.Value = x);

if (bindingTypes == BindingTypes.OneWay)
{ return propertyBBinding; }

if (propertyBSetter == null)
{ throw new SetterNotProvidedException(); }
var propertyABinding = propertyA
.ApplyOutputFilters(filters)
.DistinctUntilChanged()
.Select(convertor.From)
.Subscribe(x => propertyB.Value = x);

return new CompositeDisposable(propertyABinding, propertyBBinding);
}

public static IDisposable Bind<T1, T2>(Func<T1> propertyAGetter, Action<T1> propertyASetter, IReactiveProperty<T2> propertyB, IConvertor<T1, T2> convertor, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T1>[] filters)
{
var propertyBBinding = propertyB
.Select(convertor.From)
.ApplyInputFilters(filters)
.DistinctUntilChanged()
.Subscribe(propertyASetter);

if (bindingTypes == BindingTypes.OneWay)
{ return propertyBBinding; }

var propertyABinding = propertyAGetter()
var propertyABinding = Observable.EveryUpdate()
.Select(x => propertyAGetter())
.ApplyOutputFilters(filters)
.DistinctUntilChanged()
.Subscribe(propertyBSetter);
.Subscribe(x => propertyB.Value = convertor.From(propertyAGetter()));

return new CompositeDisposable(propertyABinding, propertyBBinding);
}

public static IDisposable Bind<T>(Func<T> propertyAGetter, Action<T> propertyASetter, Func<T> propertyBGetter, Action<T> propertyBSetter, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T>[] filters)
public static IDisposable Bind<T1, T2>(Func<T1> propertyAGetter, Action<T1> propertyASetter, Func<T2> propertyBGetter, Action<T2> propertyBSetter, IConvertor<T1, T2> convertor, BindingTypes bindingTypes = BindingTypes.Default, params IFilter<T1>[] filters)
{
var propertyBBinding = Observable.EveryUpdate()
.Select(x => propertyBGetter())
.Select(x => convertor.From(propertyBGetter()))
.ApplyInputFilters(filters)
.DistinctUntilChanged()
.Subscribe(propertyASetter);
Expand All @@ -104,6 +128,7 @@ public static IDisposable Bind<T>(Func<T> propertyAGetter, Action<T> propertyASe
.Select(x => propertyAGetter())
.ApplyOutputFilters(filters)
.DistinctUntilChanged()
.Select(convertor.From)
.Subscribe(propertyBSetter);

return new CompositeDisposable(propertyABinding, propertyBBinding);
Expand Down
10 changes: 10 additions & 0 deletions src/Assets/BindingsRx/UI/InputFieldExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BindingsRx.Convertors;
using BindingsRx.Filters;
using BindingsRx.Generic;
using UniRx;
Expand All @@ -12,6 +13,15 @@ public static class InputFieldExtensions
public static IDisposable BindTextTo(this InputField input, IReactiveProperty<string> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, property, bindingType, filters).AddTo(input); }

public static IDisposable BindTextTo(this InputField input, IReactiveProperty<int> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, property, new TextToIntConvertor(), bindingType, filters).AddTo(input); }

public static IDisposable BindTextTo(this InputField input, IReactiveProperty<float> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, property, new TextToFloatConvertor(), bindingType, filters).AddTo(input); }

public static IDisposable BindTextTo(this InputField input, IReactiveProperty<double> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, property, new TextToDoubleConvertor(), bindingType, filters).AddTo(input); }

public static IDisposable BindTextTo(this InputField input, Func<string> getter, Action<string> setter, BindingTypes bindingType = BindingTypes.Default, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, getter, setter, bindingType, filters).AddTo(input); }

Expand Down
4 changes: 2 additions & 2 deletions src/Assets/BindingsRx/UI/SliderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace BindingsRx.UI
public static class SliderExtensions
{
public static IDisposable BindValueTo(this Slider input, IReactiveProperty<float> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<float>[] filters)
{ return GenericBindings.Bind(() => input.OnValueChangedAsObservable(), x => input.maxValue = x, property, bindingType, filters).AddTo(input); }
{ return GenericBindings.Bind(() => input.value, x => input.maxValue = x, property, bindingType, filters).AddTo(input); }

public static IDisposable BindValueTo(this Slider input, Func<float> getter, Action<float> setter, BindingTypes bindingType = BindingTypes.Default, params IFilter<float>[] filters)
{ return GenericBindings.Bind(() => input.OnValueChangedAsObservable(), x => input.maxValue = x, getter, setter, bindingType, filters).AddTo(input); }
{ return GenericBindings.Bind(() => input.value, x => input.maxValue = x, getter, setter, bindingType, filters).AddTo(input); }

public static IDisposable BindMaxValueTo(this Slider input, IReactiveProperty<float> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<float>[] filters)
{ return GenericBindings.Bind(() => input.maxValue, x => input.maxValue = x, property, bindingType, filters).AddTo(input); }
Expand Down
16 changes: 16 additions & 0 deletions src/Assets/BindingsRx/UI/TextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BindingsRx.Convertors;
using BindingsRx.Filters;
using BindingsRx.Generic;
using UniRx;
Expand All @@ -11,10 +12,25 @@ public static class TextExtensions
{
public static IDisposable BindTextTo(this Text input, IReactiveProperty<string> property, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, property, BindingTypes.OneWay, filters).AddTo(input); }

public static IDisposable BindTextTo(this Text input, IReactiveProperty<int> property, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, property, new TextToIntConvertor(), BindingTypes.OneWay, filters).AddTo(input); }

public static IDisposable BindTextTo(this Text input, IReactiveProperty<float> property, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, property, new TextToFloatConvertor(), BindingTypes.OneWay, filters).AddTo(input); }

public static IDisposable BindTextTo(this Text input, IReactiveProperty<double> property, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, property, new TextToDoubleConvertor(), BindingTypes.OneWay, filters).AddTo(input); }

public static IDisposable BindTextTo(this Text input, Func<string> getter, params IFilter<string>[] filters)
{ return GenericBindings.Bind(() => input.text, x => input.text = x, getter, null, BindingTypes.OneWay, filters).AddTo(input); }

public static IDisposable BindFontSizeTo(this Text input, IReactiveProperty<int> property, params IFilter<int>[] filters)
{ return GenericBindings.Bind(() => input.fontSize, x => input.fontSize = x, property, BindingTypes.OneWay, filters).AddTo(input); }

public static IDisposable BindFontSizeTo(this Text input, Func<int> getter, params IFilter<int>[] filters)
{ return GenericBindings.Bind(() => input.fontSize, x => input.fontSize = x, getter, null, BindingTypes.OneWay, filters).AddTo(input); }

public static IDisposable BindColorTo(this Text input, IReactiveProperty<Color> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<Color>[] filters)
{ return GenericBindings.Bind(() => input.color, x => input.color = x, property, bindingType, filters).AddTo(input); }

Expand Down
17 changes: 17 additions & 0 deletions src/Assets/BindingsRx/UI/ToggleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using BindingsRx.Filters;
using BindingsRx.Generic;
using UniRx;
using UnityEngine.UI;

namespace BindingsRx.UI
{
public static class ToggleExtensions
{
public static IDisposable BindToggleTo(this Toggle input, IReactiveProperty<bool> property, BindingTypes bindingType = BindingTypes.Default, params IFilter<bool>[] filters)
{ return GenericBindings.Bind(() => input.isOn, x => input.isOn = x, property, bindingType, filters).AddTo(input); }

public static IDisposable BindToggleTo(this Toggle input, Func<bool> getter, Action<bool> setter, BindingTypes bindingType = BindingTypes.Default, params IFilter<bool>[] filters)
{ return GenericBindings.Bind(() => input.isOn, x => input.isOn = x, getter, setter, bindingType, filters).AddTo(input); }
}
}
3 changes: 3 additions & 0 deletions src/Assets/BindingsRx/UI/ToggleExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7151c48

Please sign in to comment.