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) + ".");
            }
        }
        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 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) + ".");
            }
        }
Beispiel #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 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) + ".");
            }
        }
Beispiel #6
0
 public SimpleButton()
 {
     _currentColor = new DelegateProperty <Color>(DetermineColor)
                     .UpdateOn(_pointerInside)
                     .UpdateOn(_pointerDown)
                     .UpdateOn(Enabled);
 }
        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) + ".");
            }
        }
Beispiel #8
0
        public DetailMenuModel([NotNull] ILibrary library, [NotNull] IMessageRelay relay)
        {
            if (library == null)
            {
                throw new ArgumentNullException(nameof(library));
            }
            if (relay == null)
            {
                throw new ArgumentNullException(nameof(relay));
            }

            SwitchToCurrentModeCommand = new DelegateCommand(
                () => Mode != SelectionMode.Current,
                () => Mode.Value = SelectionMode.Current);

            SwitchToSelectionModeCommand = new DelegateCommand(
                () => Mode != SelectionMode.Selection,
                () => Mode.Value = SelectionMode.Selection);

            Mode.ValueChanged += ModeOnValueChanged;

            AnythingSelected = new DelegateProperty <bool>(IsSomethingSelected)
                               .UpdateOn(Mode)
                               .UpdateOn(Current)
                               .UpdateOn(Selection);

            StatsModel     = new StatsModel(this);
            TagEditorModel = new TagEditorModel(this, library);
            RotateModel    = new RotateModel(this, library);
            ViewPanelModel = new ViewPanelModel(this, library, relay);
        }
Beispiel #9
0
        /// <summary>
        /// Adds the given <paramref name="dataBinding"/> to the internal index for
        /// the given data binding target <paramref name="obj"/>
        /// with the given bound property <paramref name="property"/>.
        /// </summary>
        /// <param name="obj">Data binding target</param>
        /// <param name="property">Data binding property</param>
        /// <param name="dataBinding">Data binding to index</param>
        public void AddToIndex <TValue>(IDataBindingSupport obj, IBindableProperty <TValue> property, DataBinding dataBinding)
        {
            Tuple <IDataBindingSupport, IBindableProperty> key = Tuple.Create(obj, (IBindableProperty)property);

            lock (_globalDataBindingCache) {
                _globalDataBindingCache[key] = dataBinding;
            }
        }
Beispiel #10
0
 public void UpdateValue(TComponent component, IBindableProperty <TData> property, Action <TData> fieldChangeCb,
                         UnityEvent <TData> componentEvent, BindType bindType,
                         Func <TData, TData> property2CpntWrap, Func <TData, TData> cpnt2PropWrap)
 {
     SetValue(component, property, fieldChangeCb, componentEvent, bindType, property2CpntWrap,
              cpnt2PropWrap);
     InitCpntValue();
 }
Beispiel #11
0
 public void UnBind()
 {
     if (_source == nameof(_image.sprite) && _model == Model.OneWay)
     {
         _target.ValueChanged -= SpriteChangedOneWay;
         _target = null;
         _image  = null;
     }
 }
 public void UnBind()
 {
     if (_source == nameof(_text.text) && _model == Model.OneWay)
     {
         _target.ValueChanged -= TextChangedOneWay;
         _target = null;
         _text   = null;
     }
 }
Beispiel #13
0
        /// <summary>
        /// Returns the data binding for the given data binding target <paramref name="obj"/> for the given
        /// bound property <paramref name="property"/>. Returns NULL if there is no data binding.
        /// </summary>
        /// <param name="obj">Data binding target</param>
        /// <param name="property">Data binding property</param>
        /// <returns>Data binding or NULL</returns>
        public DataBinding GetDataBinding <TValue>(IDataBindingSupport obj, IBindableProperty <TValue> property)
        {
            Tuple <IDataBindingSupport, IBindableProperty> key = Tuple.Create(obj, (IBindableProperty)property);

            lock (_globalDataBindingCache) {
                _globalDataBindingCache.TryGetValue(key, out DataBinding dataBinding);
                return(dataBinding);
            }
        }
Beispiel #14
0
 public void UpdateValue(TComponent component, IBindableProperty <TData> property,
                         Action <TResult> fieldChangeCb,
                         Func <TData, TResult> field2CpntConvert,
                         Func <TResult, TData> cpnt2FieldConvert,
                         UnityEvent <TResult> componentEvent)
 {
     SetValue(component, property, fieldChangeCb, field2CpntConvert, cpnt2FieldConvert, componentEvent);
     InitCpntValue();
 }
Beispiel #15
0
        public static void BindTo <T>(this TMP_Text text, IBindableProperty <T> property, string formatString = null)
        {
            property.ValueChanged += OnPropertyChanged;
            OnPropertyChanged(property.Value);

            void OnPropertyChanged(T newValue) => text.text = formatString != null
                ? string.Format(formatString, newValue)
                : newValue?.ToString();
        }
Beispiel #16
0
        /// <summary> Tells the data binding to update the source value. </summary>
        /// <param name="sender">Method invocator</param>
        /// <param name="property">Property to update</param>
        /// <param name="value">New value</param>
        public void UpdateSource <TValue>(object sender, IBindableProperty <TValue> property, object value)
        {
            if (!ShallUpdateSource)
            {
                return;
            }

            ApplyValueToBindingSource(Source, SourcePropertyInfo, Target, property.PropertyInfo);
        }
Beispiel #17
0
        /// <summary>
        /// Returns the current value of the given <paramref name="property"/>
        /// of the given data binding target <paramref name="obj"/>.
        /// </summary>
        /// <param name="obj">Data binding target</param>
        /// <param name="property">Data binding property</param>
        /// <returns>Current property value</returns>
        public TValue GetValue <TValue>(IDataBindingSupport obj, IBindableProperty <TValue> property)
        {
            Tuple <IDataBindingSupport, IBindableProperty> key = Tuple.Create(obj, (IBindableProperty)property);

            object result;

            lock (_globalValueCache) {
                _globalValueCache.TryGetValue(key, out result);
            }
            return((TValue)result);
        }
Beispiel #18
0
 private void SetValue(TComponent component, IBindableProperty <TData> property, Action <TData> fieldChangeCb,
                       UnityEvent <TData> componentEvent, BindType bindType,
                       Func <TData, TData> property2CpntWrap, Func <TData, TData> cpnt2PropWrap)
 {
     _component      = component;
     _property       = property;
     _bindType       = bindType;
     _prop2CpntWrap  = property2CpntWrap;
     _cpnt2PropWrap  = cpnt2PropWrap;
     _propChangeCb   = fieldChangeCb;
     _componentEvent = componentEvent;
 }
Beispiel #19
0
 private void SetValue(TComponent component, IBindableProperty <TData> property,
                       Action <TResult> fieldChangeCb,
                       Func <TData, TResult> field2CpntConvert,
                       Func <TResult, TData> cpnt2FieldConvert,
                       UnityEvent <TResult> componentEvent)
 {
     _component         = component;
     _fieldChangeCb     = fieldChangeCb;
     _componentEvent    = componentEvent;
     _property          = property;
     _field2CpntConvert = field2CpntConvert;
     _cpnt2FieldConvert = cpnt2FieldConvert;
 }
Beispiel #20
0
        protected void Bind <T>(Action <T> action, IBindableProperty <T> target, Model model)
        {
            if (action is null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (target is null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            _bindingHelpers.Add(new BindingHelperForAction <T>(action, target, model));
        }
 public virtual void SetBindableProperty(string keyward, IBindableProperty value)
 {
     if (this[keyward] == null)
     {
         this[keyward] = value;
     }
     else if (this[keyward] == value)
     {
         this[keyward].ValueBoxed = value.ValueBoxed;
     }
     else
     {
         this[keyward] = value;
     }
 }
 public void UnBind()
 {
     if (_source == nameof(_slider.value) && _model == Model.OneWay)
     {
         _target.ValueChanged -= ValueChangedOneWay;
         _slider = null;
         _target = null;
     }
     if (_source == nameof(_slider.value) && _model == Model.TowWay)
     {
         _target.ValueChanged -= ValueChangedOneWay;
         _slider.onValueChanged.RemoveListener(ValueChangedTowWay);
         _slider = null;
         _target = null;
     }
 }
 public void UnBind()
 {
     if (_source == nameof(_toggle.isOn) && _model == Model.OneWay)
     {
         _target.ValueChanged -= IsOnChangedOneWay;
         _target = null;
         _toggle = null;
     }
     if (_source == nameof(_toggle.isOn) && _model == Model.TowWay)
     {
         _target.ValueChanged -= IsOnChangedOneWay;
         _toggle.onValueChanged.RemoveListener(IsOnChangedTowWay);
         _toggle = null;
         _target = null;
     }
 }
 public void UnBind()
 {
     if (_source == nameof(_scrollRect.normalizedPosition) && _model == Model.OneWay)
     {
         _target.ValueChanged -= NormalizedPositionChangedOneWay;
         _target     = null;
         _scrollRect = null;
     }
     if (_source == nameof(_scrollRect.normalizedPosition) && _model == Model.TowWay)
     {
         _target.ValueChanged -= NormalizedPositionChangedOneWay;
         _scrollRect.onValueChanged.RemoveListener(NormalizedPositionChangedTowWay);
         _target     = null;
         _scrollRect = null;
     }
 }
 public void UnBind()
 {
     if (_source == nameof(_inputField.text) && _model == Model.OneWay)
     {
         _target.ValueChanged -= TextChangedOneWay;
         _inputField           = null;
         _target = null;
     }
     if (_source == nameof(_inputField.text) && _model == Model.TowWay)
     {
         _target.ValueChanged -= TextChangedOneWay;
         _inputField.onValueChanged.RemoveListener(TextChangedTowWay);
         _inputField = null;
         _target     = null;
     }
 }
Beispiel #26
0
        /// <summary>
        /// Applies the default value of the given <paramref name="property"/> to the given binding target <paramref name="target"/>.
        /// </summary>
        /// <param name="target">Data binding target</param>
        /// <param name="property">Data binding property</param>
        private void ApplyDefaultPropertyValue <TValue>(IDataBindingSupport target, IBindableProperty <TValue> property)
        {
            try {
                if (target == null)
                {
                    throw new ArgumentNullException(nameof(target));
                }
                if (property == null)
                {
                    throw new ArgumentNullException(nameof(property));
                }

                ApplyValueChange(target, property.PropertyInfo, property.DefaultValue);
            } catch (Exception ex) {
                _log.Error("Error on applying default property value to the binding target.", ex);
            }
        }
Beispiel #27
0
        /// <summary>
        /// Sets the given <paramref name="value"/> as current property value of the bound <paramref name="property"/> for
        /// the given binding target <paramref name="obj"/>. Returns TRUE if the (new) given value is not equal to the current one.
        /// Only in this case the new value is set.
        /// </summary>
        /// <param name="obj">Data binding target</param>
        /// <param name="property">Data binding property</param>
        /// <param name="value">New property value</param>
        /// <returns>TRUE if the new value has been set</returns>
        public bool SetValue <TValue>(IDataBindingSupport obj, IBindableProperty <TValue> property, TValue value)
        {
            Tuple <IDataBindingSupport, IBindableProperty> key = Tuple.Create(obj, (IBindableProperty)property);

            lock (_globalValueCache) {
                // Only update real new values
                _globalValueCache.TryGetValue(key, out object currentValue);
                if (Equals(currentValue, value))
                {
                    return(false);
                }

                // Update the new value
                _globalValueCache[key] = value;
                return(true);
            }
        }
Beispiel #28
0
 public BindingHelperForAction(Action <T> action, IBindableProperty <T> target, Model model)
 {
     _action = action;
     _target = target;
     if (model == Model.OneTime)
     {
         action?.Invoke(target.Value);
     }
     else if (model == Model.OneWay)
     {
         action?.Invoke(target.Value);
         target.ValueChanged += action;
     }
     else if (model == Model.TowWay)
     {
         throw new AquaFrameworkException("Do not support tow way binding for action.");
     }
 }
Beispiel #29
0
        /// <summary>
        /// Applies the given <paramref name="dataBinding"/> to the given <paramref name="uiElement"/> for the
        /// given <paramref name="property"/>.
        /// </summary>
        /// <param name="uiElement">UI Element that is used as target of the binding</param>
        /// <param name="property">Property that is being bound</param>
        /// <param name="dataBinding">Data binding</param>
        /// <exception cref="ArgumentNullException">If any parameter is NULL</exception>
        public static void Apply <TValue>(IDataBindingSupport uiElement, IBindableProperty <TValue> property, DataBinding dataBinding)
        {
            if (uiElement == null)
            {
                throw new ArgumentNullException(nameof(uiElement));
            }
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }
            if (dataBinding == null)
            {
                throw new ArgumentNullException(nameof(dataBinding));
            }

            dataBinding.Connect(uiElement, property);
            GlobalDataBindingIndex.Instance.AddToIndex(uiElement, property, dataBinding);
        }
Beispiel #30
0
        /// <summary>
        /// Connects this instance to the given data binding <paramref name="target"/> for the <paramref name="property"/>.
        /// </summary>
        /// <param name="target">Data binding target to connect</param>
        /// <param name="property">Target property to bind</param>
        public void Connect <TValue>(IDataBindingSupport target, IBindableProperty <TValue> property)
        {
            Target             = target ?? throw new ArgumentNullException(nameof(target));
            TargetPropertyInfo = (property ?? throw new ArgumentNullException(nameof(property))).PropertyInfo;

            // Apply default value to the target
            ApplyDefaultPropertyValue(target, property);

            // Selected binding mode
            EDataBindingMode bindingMode = BindingMode;

            if (bindingMode == EDataBindingMode.Default)
            {
                bindingMode = (property.BindsTwoWayByDefault ? EDataBindingMode.TwoWay : EDataBindingMode.OneWay);
            }

            // Set property
            ShallUpdateSource = (bindingMode == EDataBindingMode.OneWayToSource || bindingMode == EDataBindingMode.TwoWay);

            // OneWayToSource
            if (bindingMode == EDataBindingMode.OneWayToSource)
            {
                // Update source immediately
                ApplyValueToBindingSource(Source, SourcePropertyInfo, Target, property.PropertyInfo);
                return;
            }

            // Update immediately the target
            ApplyValueToBindingTarget(target, property.PropertyInfo, Source, SourcePropertyInfo);

            // In this case we're finished here
            if (bindingMode == EDataBindingMode.OneTime)
            {
                return;
            }

            // OneWay and/or TwoWay

            // Add update listener to the source (if supported)
            if (Source is INotifyPropertyChanged propertyChangedDispatcher)
            {
                propertyChangedDispatcher.PropertyChanged += OnSourcePropertyChanged;
            }
        }
        internal static void TestBindableProperty(ControlTestClass instance, IBindableProperty<ControlTestClass, string> property)
        {
            Assert.Equal("First", property.Value);
            Assert.Equal(instance.Text, property.Value);

            instance.Text = "Second";
            Assert.Equal("Second", property.Value);

            property.Value = "Third";
            Assert.Equal( "Third", instance.Text);

            int textChangedCount = 0;
            int propertyChangedCount = 0;
            instance.TextChanged += (sender, args) => textChangedCount++;
            property.PropertyChanged += (sender, args) =>
                {
                    Assert.Equal("Text", args.PropertyName);
                    propertyChangedCount++;
                };

            instance.Text = "Forth";
            Assert.Equal(1, textChangedCount);
            Assert.Equal(1, propertyChangedCount);

            property.Value = "Fifth";
            Assert.Equal(2, textChangedCount);
            Assert.Equal(2, propertyChangedCount);
        }