Exemplo n.º 1
0
 public Binding(object instance, string propertyName, BindingConverter converter = null)
 {
     this.Obj       = instance;
     this._type     = instance.GetType();
     this.Property  = propertyName;
     this.Converter = converter;
 }
        public BindingHelperForSlider(Slider slider, string source, IBindableProperty <T> target, Model model)
        {
            _slider = slider;
            _source = source;
            _target = target;
            _model  = model;

            switch (source)
            {
            case nameof(slider.value):
                if (model == Model.OneTime)
                {
                    slider.value = BindingConverter.ConvertToConcreteType <float>(target.Value);
                }
                else if (model == Model.OneWay)
                {
                    slider.value         = BindingConverter.ConvertToConcreteType <float>(target.Value);
                    target.ValueChanged += ValueChangedOneWay;
                }
                else if (model == Model.TowWay)
                {
                    slider.value         = BindingConverter.ConvertToConcreteType <float>(target.Value);
                    target.ValueChanged += ValueChangedOneWay;
                    slider.onValueChanged.AddListener(ValueChangedTowWay);
                }
                break;

            default:
                throw new AquaFrameworkException("Do not support binding for " + nameof(source) + ".");
            }
        }
        public static Action <T> CreateSetDelegate <T>(this PropertyInfo info, object instance)
        {
            Type propertyType      = info.PropertyType;
            var  generatedDelegate = info.GetSetMethod().CreateDelegate(typeof(Action <>).MakeGenericType(propertyType), instance);

            return(data => generatedDelegate.DynamicInvoke(BindingConverter.ConvertBindingValue(propertyType, data)));
        }
Exemplo n.º 4
0
        public BindingHelperForImage(Image image, string source, IBindableProperty <T> target, Model model)
        {
            _image  = image;
            _source = source;
            _target = target;
            _model  = model;
            switch (source)
            {
            case nameof(image.sprite):
                if (model == Model.OneTime)
                {
                    image.sprite = BindingConverter.ConvertToConcreteType <Sprite>(target.Value);
                }
                else if (model == Model.OneWay)
                {
                    image.sprite         = BindingConverter.ConvertToConcreteType <Sprite>(target.Value);
                    target.ValueChanged += SpriteChangedOneWay;
                }
                else if (model == Model.TowWay)
                {
                    throw new AquaFrameworkException("Do not support tow way binding for " + nameof(image.sprite) + ".");
                }
                break;

            default:
                throw new AquaFrameworkException("Do not support binding for " + nameof(source) + ".");
            }
        }
        public BindingHelperForTextMeshProUGUI(TextMeshProUGUI text, string source, IBindableProperty <T> target, Model model)
        {
            _text   = text;
            _source = source;
            _target = target;
            _model  = model;
            switch (source)
            {
            case nameof(text.text):
                if (model == Model.OneTime)
                {
                    text.text = BindingConverter.ConvertToConcreteType <string>(target.Value.ToString());
                }
                else if (model == Model.OneWay)
                {
                    text.text            = BindingConverter.ConvertToConcreteType <string>(target.Value.ToString());
                    target.ValueChanged += TextChangedOneWay;
                }
                else if (model == Model.TowWay)
                {
                    throw new AquaFrameworkException("Do not support tow way binding for " + nameof(text.text) + ".");
                }
                break;

            default:
                throw new AquaFrameworkException("Do not support binding for " + nameof(source) + ".");
            }
        }
        public BindingHelperForTMP_InputField(TMP_InputField inputField, string source, IBindableProperty <T> target, Model model)
        {
            _inputField = inputField;
            _source     = source;
            _target     = target;
            _model      = model;
            switch (source)
            {
            case nameof(inputField.text):
                if (model == Model.OneTime)
                {
                    inputField.text = BindingConverter.ConvertToConcreteType <string>(target.Value);
                }
                else if (model == Model.OneWay)
                {
                    inputField.text      = BindingConverter.ConvertToConcreteType <string>(target.Value);
                    target.ValueChanged += TextChangedOneWay;
                }
                else if (model == Model.TowWay)
                {
                    inputField.text      = BindingConverter.ConvertToConcreteType <string>(target.Value);
                    target.ValueChanged += TextChangedOneWay;
                    inputField.onValueChanged.AddListener(TextChangedTowWay);
                }
                break;

            default:
                throw new AquaFrameworkException("Do not support binding for " + nameof(source) + ".");
            }
        }
        public BindingHelperForScrollRect(ScrollRect scrollRect, string source, IBindableProperty <T> target, Model model)
        {
            _scrollRect = scrollRect;
            _source     = source;
            _target     = target;
            _model      = model;
            switch (source)
            {
            case nameof(scrollRect.normalizedPosition):
                if (model == Model.OneTime)
                {
                    scrollRect.normalizedPosition = BindingConverter.ConvertToConcreteType <Vector2>(target.Value);
                }
                else if (model == Model.OneWay)
                {
                    scrollRect.normalizedPosition = BindingConverter.ConvertToConcreteType <Vector2>(target.Value);
                    target.ValueChanged          += NormalizedPositionChangedOneWay;
                }
                else if (model == Model.TowWay)
                {
                    scrollRect.normalizedPosition = BindingConverter.ConvertToConcreteType <Vector2>(target.Value);
                    target.ValueChanged          += NormalizedPositionChangedOneWay;
                    scrollRect.onValueChanged.AddListener(NormalizedPositionChangedTowWay);
                }
                break;

            default:
                throw new AquaFrameworkException("Do not support binding for " + nameof(source) + ".");
            }
        }
        public BindingHelperForToggle(Toggle toggle, string source, IBindableProperty <T> target, Model model)
        {
            _toggle = toggle;
            _source = source;
            _target = target;
            _model  = model;
            switch (source)
            {
            case nameof(toggle.isOn):
                if (model == Model.OneTime)
                {
                    toggle.isOn = BindingConverter.ConvertToConcreteType <bool>(target.Value);
                }
                else if (model == Model.OneWay)
                {
                    toggle.isOn          = BindingConverter.ConvertToConcreteType <bool>(target.Value);
                    target.ValueChanged += IsOnChangedOneWay;
                }
                else if (model == Model.TowWay)
                {
                    toggle.isOn          = BindingConverter.ConvertToConcreteType <bool>(target.Value);
                    target.ValueChanged += IsOnChangedOneWay;
                    toggle.onValueChanged.AddListener(IsOnChangedTowWay);
                }
                break;

            default:
                throw new AquaFrameworkException("Do not support binding for " + nameof(source) + ".");
            }
        }
Exemplo n.º 9
0
        public static void BindProperty(this VisualTreeElement element, PropertyInfo elementProperty, BindablePropertyAttribute attribute, BindingExpression binding)
        {
            if (!binding.IsDatabound)
            {
                elementProperty.SetValue(element, BindingConverter.ConvertBindingValue(elementProperty.PropertyType, binding.Source));
                return;
            }

            Action <Action> subscriber;
            BindingMode     mode;

            if (string.IsNullOrEmpty(attribute.ChangeHandler))
            {
                subscriber = handler => { };
                mode       = BindingMode.FromViewModel;
            }
            else
            {
                PropertyInfo viewChangeHandler = element.GetType()
                                                 .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                                 .Single(p => p.Name == attribute.ChangeHandler);
                subscriber = handler =>
                {
                    Action current = (Action)viewChangeHandler.GetValue(element);
                    current += handler;
                    viewChangeHandler.SetValue(element, current);
                };

                mode = BindingMode.TwoWay;
            }

            element.BindingContext.BindProperty(
                subscriber,
                elementProperty.CreateGetDelegate <dynamic>(element),
                elementProperty.CreateSetDelegate <dynamic>(element),
                elementProperty.Name,
                binding.Target,
                mode);
        }
 void TextChangedOneWay(T value)
 {
     _text.text = BindingConverter.ConvertToConcreteType <string>(value.ToString());
 }
 void IsOnChangedTowWay(bool value)
 {
     _target.Value = BindingConverter.ConvertToGenericType <T, bool>(value);
 }
 void IsOnChangedOneWay(T value)
 {
     _toggle.isOn = BindingConverter.ConvertToConcreteType <bool>(value);
 }
 void ValueChangedOneWay(T value)
 {
     _slider.value = BindingConverter.ConvertToConcreteType <float>(value);
 }
 void TextChangedOneWay(T value)
 {
     _inputField.text = BindingConverter.ConvertToConcreteType <string>(value);
 }
 void NormalizedPositionChangedTowWay(Vector2 value)
 {
     _target.Value = BindingConverter.ConvertToGenericType <T, Vector2>(value);
 }
 void NormalizedPositionChangedOneWay(T value)
 {
     _scrollRect.normalizedPosition = BindingConverter.ConvertToConcreteType <Vector2>(value);
 }
Exemplo n.º 17
0
 void SpriteChangedOneWay(T value)
 {
     _image.sprite = BindingConverter.ConvertToConcreteType <Sprite>(value);
 }
 void TextChangedTowWay(string value)
 {
     _target.Value = BindingConverter.ConvertToGenericType <T, string>(value);
 }
 void ValueChangedTowWay(float value)
 {
     _target.Value = BindingConverter.ConvertToGenericType <T, float>(value);
 }