diff --git a/docs/binding-overview.md b/docs/binding-overview.md index 03b82da..5f2bc25 100644 --- a/docs/binding-overview.md +++ b/docs/binding-overview.md @@ -10,6 +10,7 @@ Currently there is support for built in bindings on: - Text - Slider - Dropdown +- Toggle - RectTransform - ICanvasElement implementations diff --git a/src/Assets/BindingsRx/Convertors.meta b/src/Assets/BindingsRx/Convertors.meta new file mode 100644 index 0000000..8f4aff0 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ba18d03168b940caa492e18286877b64 +timeCreated: 1489091654 \ No newline at end of file diff --git a/src/Assets/BindingsRx/Convertors/IConvertor.cs b/src/Assets/BindingsRx/Convertors/IConvertor.cs new file mode 100644 index 0000000..dc263c9 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors/IConvertor.cs @@ -0,0 +1,8 @@ +namespace BindingsRx.Convertors +{ + public interface IConvertor + { + T1 From(T2 value); + T2 From(T1 value); + } +} \ No newline at end of file diff --git a/src/Assets/BindingsRx/Convertors/IConvertor.cs.meta b/src/Assets/BindingsRx/Convertors/IConvertor.cs.meta new file mode 100644 index 0000000..d5c9fd7 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors/IConvertor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3d3c2ecd05bd462abbaa559d566fce84 +timeCreated: 1489091922 \ No newline at end of file diff --git a/src/Assets/BindingsRx/Convertors/IntConvertor.cs b/src/Assets/BindingsRx/Convertors/IntConvertor.cs new file mode 100644 index 0000000..fd70d38 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors/IntConvertor.cs @@ -0,0 +1,15 @@ +namespace BindingsRx.Convertors +{ + public class TextToIntConvertor : IConvertor + { + public string From(int value) + { return value.ToString(); } + + public int From(string value) + { + int output; + int.TryParse(value, out output); + return output; + } + } +} \ No newline at end of file diff --git a/src/Assets/BindingsRx/Convertors/IntConvertor.cs.meta b/src/Assets/BindingsRx/Convertors/IntConvertor.cs.meta new file mode 100644 index 0000000..2081ed2 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors/IntConvertor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b066c70ae6434e3ab9d643fd3d3e0fc8 +timeCreated: 1489091680 \ No newline at end of file diff --git a/src/Assets/BindingsRx/Convertors/TextToDoubleConvertor.cs b/src/Assets/BindingsRx/Convertors/TextToDoubleConvertor.cs new file mode 100644 index 0000000..69cb976 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors/TextToDoubleConvertor.cs @@ -0,0 +1,15 @@ +namespace BindingsRx.Convertors +{ + public class TextToDoubleConvertor : IConvertor + { + public string From(double value) + { return value.ToString(); } + + public double From(string value) + { + double output; + double.TryParse(value, out output); + return output; + } + } +} \ No newline at end of file diff --git a/src/Assets/BindingsRx/Convertors/TextToDoubleConvertor.cs.meta b/src/Assets/BindingsRx/Convertors/TextToDoubleConvertor.cs.meta new file mode 100644 index 0000000..99f4ef6 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors/TextToDoubleConvertor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 739f20eda0be4c49b72719ce37f2e9ba +timeCreated: 1489093316 \ No newline at end of file diff --git a/src/Assets/BindingsRx/Convertors/TextToFloatConvertor.cs b/src/Assets/BindingsRx/Convertors/TextToFloatConvertor.cs new file mode 100644 index 0000000..1dbda62 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors/TextToFloatConvertor.cs @@ -0,0 +1,15 @@ +namespace BindingsRx.Convertors +{ + public class TextToFloatConvertor : IConvertor + { + public string From(float value) + { return value.ToString(); } + + public float From(string value) + { + float output; + float.TryParse(value, out output); + return output; + } + } +} \ No newline at end of file diff --git a/src/Assets/BindingsRx/Convertors/TextToFloatConvertor.cs.meta b/src/Assets/BindingsRx/Convertors/TextToFloatConvertor.cs.meta new file mode 100644 index 0000000..b045d22 --- /dev/null +++ b/src/Assets/BindingsRx/Convertors/TextToFloatConvertor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 38c6a0db4c7642c088b83829bbb64af9 +timeCreated: 1489093285 \ No newline at end of file diff --git a/src/Assets/BindingsRx/Extensions/IReactivePropertyExtensions.cs b/src/Assets/BindingsRx/Extensions/IReactivePropertyExtensions.cs new file mode 100644 index 0000000..5902f08 --- /dev/null +++ b/src/Assets/BindingsRx/Extensions/IReactivePropertyExtensions.cs @@ -0,0 +1,10 @@ +using UniRx; + +namespace BindingsRx.Extensions +{ + public static class IReactivePropertyExtensions + { + public static ReactiveProperty ToTextualProperty(this IReactiveProperty nonStringProperty) + { return nonStringProperty.Select(x => x.ToString()).ToReactiveProperty(); } + } +} \ No newline at end of file diff --git a/src/Assets/BindingsRx/Extensions/IReactivePropertyExtensions.cs.meta b/src/Assets/BindingsRx/Extensions/IReactivePropertyExtensions.cs.meta new file mode 100644 index 0000000..cbf126e --- /dev/null +++ b/src/Assets/BindingsRx/Extensions/IReactivePropertyExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6f52a576dd2f4637a987cb39520a67b8 +timeCreated: 1489090267 \ No newline at end of file diff --git a/src/Assets/BindingsRx/GameObjects/MonobehaviourExtensions.cs b/src/Assets/BindingsRx/GameObjects/MonobehaviourExtensions.cs index 3649d5b..011a699 100644 --- a/src/Assets/BindingsRx/GameObjects/MonobehaviourExtensions.cs +++ b/src/Assets/BindingsRx/GameObjects/MonobehaviourExtensions.cs @@ -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 property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) { return GenericBindings.Bind(() => input.enabled, x => input.enabled = x, property, bindingType, filters).AddTo(input); } - public static IDisposable BindActiveTo(this MonoBehaviour input, Func getter, Action setter, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) + public static IDisposable BindEnabledTo(this MonoBehaviour input, Func getter, Action setter, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) { return GenericBindings.Bind(() => input.enabled, x => input.enabled = x, getter, setter, bindingType, filters).AddTo(input); } } } \ No newline at end of file diff --git a/src/Assets/BindingsRx/Generic/GenericBindings.cs b/src/Assets/BindingsRx/Generic/GenericBindings.cs index 7396a49..2586156 100644 --- a/src/Assets/BindingsRx/Generic/GenericBindings.cs +++ b/src/Assets/BindingsRx/Generic/GenericBindings.cs @@ -1,4 +1,5 @@ using System; +using BindingsRx.Convertors; using BindingsRx.Exceptions; using BindingsRx.Extensions; using BindingsRx.Filters; @@ -26,8 +27,8 @@ public static IDisposable Bind(IReactiveProperty propertyA , IReactiveProp return new CompositeDisposable(propertyABinding, propertyBBinding); } - - public static IDisposable Bind(Func> propertyAGetter, Action propertyASetter, IReactiveProperty propertyB, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] filters) + + public static IDisposable Bind(Func propertyAGetter, Action propertyASetter, IReactiveProperty propertyB, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] filters) { var propertyBBinding = propertyB .ApplyInputFilters(filters) @@ -37,17 +38,19 @@ public static IDisposable Bind(Func> propertyAGetter, Action propertyAGetter()) .ApplyOutputFilters(filters) .DistinctUntilChanged() - .Subscribe(x => propertyB.Value = x); + .Subscribe(x => propertyB.Value = propertyAGetter()); return new CompositeDisposable(propertyABinding, propertyBBinding); } - public static IDisposable Bind(Func propertyAGetter, Action propertyASetter, IReactiveProperty propertyB, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] filters) + public static IDisposable Bind(Func propertyAGetter, Action propertyASetter, Func propertyBGetter, Action propertyBSetter, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] filters) { - var propertyBBinding = propertyB + var propertyBBinding = Observable.EveryUpdate() + .Select(x => propertyBGetter()) .ApplyInputFilters(filters) .DistinctUntilChanged() .Subscribe(propertyASetter); @@ -55,41 +58,62 @@ public static IDisposable Bind(Func propertyAGetter, Action propertyASe 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(Func> propertyAGetter, Action propertyASetter, Func propertyBGetter, Action propertyBSetter, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] filters) + public static IDisposable Bind(IReactiveProperty propertyA, IReactiveProperty propertyB, IConvertor convertor, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] 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(Func propertyAGetter, Action propertyASetter, IReactiveProperty propertyB, IConvertor convertor, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] 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(Func propertyAGetter, Action propertyASetter, Func propertyBGetter, Action propertyBSetter, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] filters) + public static IDisposable Bind(Func propertyAGetter, Action propertyASetter, Func propertyBGetter, Action propertyBSetter, IConvertor convertor, BindingTypes bindingTypes = BindingTypes.Default, params IFilter[] filters) { var propertyBBinding = Observable.EveryUpdate() - .Select(x => propertyBGetter()) + .Select(x => convertor.From(propertyBGetter())) .ApplyInputFilters(filters) .DistinctUntilChanged() .Subscribe(propertyASetter); @@ -104,6 +128,7 @@ public static IDisposable Bind(Func propertyAGetter, Action propertyASe .Select(x => propertyAGetter()) .ApplyOutputFilters(filters) .DistinctUntilChanged() + .Select(convertor.From) .Subscribe(propertyBSetter); return new CompositeDisposable(propertyABinding, propertyBBinding); diff --git a/src/Assets/BindingsRx/UI/InputFieldExtensions.cs b/src/Assets/BindingsRx/UI/InputFieldExtensions.cs index 8070011..a3f7fde 100644 --- a/src/Assets/BindingsRx/UI/InputFieldExtensions.cs +++ b/src/Assets/BindingsRx/UI/InputFieldExtensions.cs @@ -1,4 +1,5 @@ using System; +using BindingsRx.Convertors; using BindingsRx.Filters; using BindingsRx.Generic; using UniRx; @@ -12,6 +13,15 @@ public static class InputFieldExtensions public static IDisposable BindTextTo(this InputField input, IReactiveProperty property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) { return GenericBindings.Bind(() => input.text, x => input.text = x, property, bindingType, filters).AddTo(input); } + public static IDisposable BindTextTo(this InputField input, IReactiveProperty property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] 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 property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] 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 property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] 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 getter, Action setter, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) { return GenericBindings.Bind(() => input.text, x => input.text = x, getter, setter, bindingType, filters).AddTo(input); } diff --git a/src/Assets/BindingsRx/UI/SliderExtensions.cs b/src/Assets/BindingsRx/UI/SliderExtensions.cs index bace68e..4d1a115 100644 --- a/src/Assets/BindingsRx/UI/SliderExtensions.cs +++ b/src/Assets/BindingsRx/UI/SliderExtensions.cs @@ -10,10 +10,10 @@ namespace BindingsRx.UI public static class SliderExtensions { public static IDisposable BindValueTo(this Slider input, IReactiveProperty property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] 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 getter, Action setter, BindingTypes bindingType = BindingTypes.Default, params IFilter[] 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 property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) { return GenericBindings.Bind(() => input.maxValue, x => input.maxValue = x, property, bindingType, filters).AddTo(input); } diff --git a/src/Assets/BindingsRx/UI/TextExtensions.cs b/src/Assets/BindingsRx/UI/TextExtensions.cs index 071385a..093afe5 100644 --- a/src/Assets/BindingsRx/UI/TextExtensions.cs +++ b/src/Assets/BindingsRx/UI/TextExtensions.cs @@ -1,4 +1,5 @@ using System; +using BindingsRx.Convertors; using BindingsRx.Filters; using BindingsRx.Generic; using UniRx; @@ -11,10 +12,25 @@ public static class TextExtensions { public static IDisposable BindTextTo(this Text input, IReactiveProperty property, params IFilter[] filters) { return GenericBindings.Bind(() => input.text, x => input.text = x, property, BindingTypes.OneWay, filters).AddTo(input); } + + public static IDisposable BindTextTo(this Text input, IReactiveProperty property, params IFilter[] 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 property, params IFilter[] 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 property, params IFilter[] 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 getter, params IFilter[] 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 property, params IFilter[] filters) + { return GenericBindings.Bind(() => input.fontSize, x => input.fontSize = x, property, BindingTypes.OneWay, filters).AddTo(input); } + + public static IDisposable BindFontSizeTo(this Text input, Func getter, params IFilter[] 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 property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) { return GenericBindings.Bind(() => input.color, x => input.color = x, property, bindingType, filters).AddTo(input); } diff --git a/src/Assets/BindingsRx/UI/ToggleExtensions.cs b/src/Assets/BindingsRx/UI/ToggleExtensions.cs new file mode 100644 index 0000000..2b223c9 --- /dev/null +++ b/src/Assets/BindingsRx/UI/ToggleExtensions.cs @@ -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 property, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) + { return GenericBindings.Bind(() => input.isOn, x => input.isOn = x, property, bindingType, filters).AddTo(input); } + + public static IDisposable BindToggleTo(this Toggle input, Func getter, Action setter, BindingTypes bindingType = BindingTypes.Default, params IFilter[] filters) + { return GenericBindings.Bind(() => input.isOn, x => input.isOn = x, getter, setter, bindingType, filters).AddTo(input); } + } +} \ No newline at end of file diff --git a/src/Assets/BindingsRx/UI/ToggleExtensions.cs.meta b/src/Assets/BindingsRx/UI/ToggleExtensions.cs.meta new file mode 100644 index 0000000..a527159 --- /dev/null +++ b/src/Assets/BindingsRx/UI/ToggleExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3787921598b948829dd2a3b35544e701 +timeCreated: 1489095109 \ No newline at end of file diff --git a/src/Assets/Editor/Tests/GenericBindingTests.cs b/src/Assets/Editor/Tests/GenericBindingTests.cs index 43c6ba2..2495297 100644 --- a/src/Assets/Editor/Tests/GenericBindingTests.cs +++ b/src/Assets/Editor/Tests/GenericBindingTests.cs @@ -12,13 +12,13 @@ public class GenericBindingTests public void should_correctly_do_two_way_binding_for_reactive_property() { var reactiveProperty = new ReactiveProperty(""); - var somePretendInput = new ReactiveProperty("some value"); - GenericBindings.Bind(() => somePretendInput, x => somePretendInput.Value = x, reactiveProperty); + var somePretendInput = ""; + GenericBindings.Bind(() => somePretendInput, x => somePretendInput = x, reactiveProperty); reactiveProperty.Value = "first"; - Assert.That(somePretendInput.Value, Is.EqualTo("first")); + Assert.That(somePretendInput, Is.EqualTo("first")); - somePretendInput.Value = "second"; + somePretendInput = "second"; Assert.That(reactiveProperty.Value, Is.EqualTo("second")); } @@ -26,13 +26,13 @@ public void should_correctly_do_two_way_binding_for_reactive_property() public void should_correctly_do_two_way_binding_for_simple_property() { var basicProperty = ""; - var somePretendInput = new ReactiveProperty("some value"); - GenericBindings.Bind(() => somePretendInput, x => somePretendInput.Value = x, () => basicProperty, x => basicProperty = x ); + var somePretendInput = ""; + GenericBindings.Bind(() => somePretendInput, x => somePretendInput = x, () => basicProperty, x => basicProperty = x ); basicProperty = "first"; - Assert.That(somePretendInput.Value, Is.EqualTo("first")); + Assert.That(somePretendInput, Is.EqualTo("first")); - somePretendInput.Value = "second"; + somePretendInput = "second"; Assert.That(basicProperty, Is.EqualTo("second")); } @@ -40,13 +40,13 @@ public void should_correctly_do_two_way_binding_for_simple_property() public void should_correctly_do_one_way_binding_for_reactive_property() { var reactiveProperty = new ReactiveProperty(""); - var somePretendInput = new ReactiveProperty("some value"); - GenericBindings.Bind(() => somePretendInput, x => somePretendInput.Value = x, reactiveProperty, BindingTypes.OneWay); + var somePretendInput = ""; + GenericBindings.Bind(() => somePretendInput, x => somePretendInput = x, reactiveProperty, BindingTypes.OneWay); reactiveProperty.Value = "first"; - Assert.That(somePretendInput.Value, Is.EqualTo("first")); + Assert.That(somePretendInput, Is.EqualTo("first")); - somePretendInput.Value = "second"; + somePretendInput = "second"; Assert.That(reactiveProperty.Value, Is.EqualTo("first")); } } diff --git a/src/Assets/Examples/InputBindings/SetupDropdownBinding.cs b/src/Assets/Examples/InputBindings/SetupDropdownBinding.cs index 530d8d4..2a93b9a 100644 --- a/src/Assets/Examples/InputBindings/SetupDropdownBinding.cs +++ b/src/Assets/Examples/InputBindings/SetupDropdownBinding.cs @@ -10,8 +10,8 @@ namespace Examples.InputBindings /// public class SetupDropdownBinding : MonoBehaviour { - public Dropdown DropdownOutput; - public InputField DropdownValueInput; + public Dropdown DropdownElement; + public InputField ValueInputElement; void Start() { @@ -19,8 +19,8 @@ void Start() exampleOptions.Add("Option 1"); exampleOptions.Add("Option 2"); exampleOptions.Add("Some Other Option"); - DropdownOutput.BindOptionsTo(exampleOptions); - DropdownOutput.BindValueTo(() => DropdownValueInput.text, x => DropdownValueInput.text = x); + DropdownElement.BindOptionsTo(exampleOptions); + DropdownElement.BindValueTo(() => ValueInputElement.text, x => ValueInputElement.text = x); } } } \ No newline at end of file diff --git a/src/Assets/Examples/InputBindings/SetupTextBinding.cs b/src/Assets/Examples/InputBindings/SetupTextBinding.cs index 1901e36..0729bfa 100644 --- a/src/Assets/Examples/InputBindings/SetupTextBinding.cs +++ b/src/Assets/Examples/InputBindings/SetupTextBinding.cs @@ -1,5 +1,4 @@ using BindingsRx.UI; -using UniRx; using UnityEngine; using UnityEngine.UI; @@ -10,12 +9,12 @@ namespace Examples.InputBindings /// public class SetupTextBinding : MonoBehaviour { - public InputField TextBindingInput; - public Text TextBindingOutput; - - void Start () + public InputField InputElement; + public Text TextElement; + + void Start() { - TextBindingOutput.BindTextTo(() => TextBindingInput.text); + TextElement.BindTextTo(() => InputElement.text); } } } diff --git a/src/Assets/Examples/InputBindings/SetupTextualValueBinding.cs b/src/Assets/Examples/InputBindings/SetupTextualValueBinding.cs new file mode 100644 index 0000000..5f29a8d --- /dev/null +++ b/src/Assets/Examples/InputBindings/SetupTextualValueBinding.cs @@ -0,0 +1,23 @@ +using System; +using BindingsRx.UI; +using UniRx; +using UnityEngine; +using UnityEngine.UI; + +namespace Examples.InputBindings +{ + /// + /// This shows how to easy it is to bind non strings to textual bindings in a 2-way fashion + /// + public class SetupTextualValueBinding : MonoBehaviour + { + public InputField InputElement; + public ReactiveProperty ReactiveInt = new ReactiveProperty(1); + + void Start() + { + InputElement.BindTextTo(ReactiveInt); + Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(x => ReactiveInt.Value += 1); + } + } +} \ No newline at end of file diff --git a/src/Assets/Examples/InputBindings/SetupTextualValueBinding.cs.meta b/src/Assets/Examples/InputBindings/SetupTextualValueBinding.cs.meta new file mode 100644 index 0000000..5d16cdf --- /dev/null +++ b/src/Assets/Examples/InputBindings/SetupTextualValueBinding.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0b0e8153becc4a95b681266cb6c940e1 +timeCreated: 1489094226 \ No newline at end of file diff --git a/src/Assets/Examples/InputBindings/SetupThrottledTextBinding.cs b/src/Assets/Examples/InputBindings/SetupThrottledTextBinding.cs index 42b1cf8..a99a007 100644 --- a/src/Assets/Examples/InputBindings/SetupThrottledTextBinding.cs +++ b/src/Assets/Examples/InputBindings/SetupThrottledTextBinding.cs @@ -14,16 +14,16 @@ namespace Examples.InputBindings /// public class SetupThrottledTextBinding : MonoBehaviour { - public InputField TextBindingInput; - public InputField ThrottleTextBindingInput; - public Text TextBindingOutput; + public InputField InputElement; + public InputField ThrottleInputElement; + public Text TextElement; void Start() { - var dynamicSampleFilter = new DynamicSampleFilter(TimeSpan.FromMilliseconds(500)).AddTo(TextBindingOutput); - TextBindingOutput.BindTextTo(() => TextBindingInput.text, dynamicSampleFilter); + var dynamicSampleFilter = new DynamicSampleFilter(TimeSpan.FromMilliseconds(500)).AddTo(TextElement); + TextElement.BindTextTo(() => InputElement.text, dynamicSampleFilter); - ThrottleTextBindingInput.BindTextTo(() => dynamicSampleFilter.SampleRate.Value.TotalMilliseconds.ToString(), + ThrottleInputElement.BindTextTo(() => dynamicSampleFilter.SampleRate.Value.TotalMilliseconds.ToString(), UpdateThrottleValue(dynamicSampleFilter)); } diff --git a/src/Assets/Examples/InputBindings/SetupToggleBinding.cs b/src/Assets/Examples/InputBindings/SetupToggleBinding.cs new file mode 100644 index 0000000..3a7fbf8 --- /dev/null +++ b/src/Assets/Examples/InputBindings/SetupToggleBinding.cs @@ -0,0 +1,33 @@ +using BindingsRx; +using BindingsRx.UI; +using UniRx; +using UnityEngine; +using UnityEngine.UI; + +namespace Examples.InputBindings +{ + /// + /// A simple toggle example + /// + public class SetupToggleBinding : MonoBehaviour + { + public ReactiveProperty ToggleState = new ReactiveProperty(); + public Toggle ToggleElement; + public Text TextElement; + + void Start() + { + ToggleElement.BindToggleTo(ToggleState); + + var textualRepresentation = ToggleState.Select(GetTextualState).ToReactiveProperty(); + textualRepresentation.AddTo(TextElement); + + TextElement.BindTextTo(textualRepresentation); + } + + string GetTextualState(bool state) + { + return "Magic is " + (state ? "Enabled" : "Disabled"); + } + } +} \ No newline at end of file diff --git a/src/Assets/Examples/InputBindings/SetupToggleBinding.cs.meta b/src/Assets/Examples/InputBindings/SetupToggleBinding.cs.meta new file mode 100644 index 0000000..36232f7 --- /dev/null +++ b/src/Assets/Examples/InputBindings/SetupToggleBinding.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 706707dee1ad4c8cbd07e621a002bc5b +timeCreated: 1489095685 \ No newline at end of file diff --git a/src/Assets/Examples/InputBindings/input-bindings.unity b/src/Assets/Examples/InputBindings/input-bindings.unity index 572f8f2..107aaa9 100644 Binary files a/src/Assets/Examples/InputBindings/input-bindings.unity and b/src/Assets/Examples/InputBindings/input-bindings.unity differ diff --git a/src/src.csproj b/src/src.csproj index a6a513f..baa7d8d 100644 --- a/src/src.csproj +++ b/src/src.csproj @@ -84,13 +84,18 @@ + + + + + - + @@ -99,9 +104,12 @@ + + +