Exemple #1
0
        /// <summary>
        /// 将InputField包装为事件属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="text"></param>
        /// <returns></returns>
        public static EventProperty <string> GetEventText(this InputField intput)
        {
            var prop = new EventProperty <string>(() => { return(intput.text); }, (x) => { intput.text = x; },
                                                  null, intput.onValueChanged);

            return(prop);
        }
Exemple #2
0
        // <summary>
        /// 绑定UI
        /// </summary>
        public static BaseCommand[] Bind <T>(this InputField text, EventProperty <T> property,
                                             Action <InputField, EventProperty <T> > actOnChanged = null, Action <InputField, T> actOnValueChange = null)
        {
            if (actOnChanged == null)
            {
                actOnChanged = (txt, str) => property.propValue = txt.text.GetValue <T>();
            }

            if (actOnValueChange == null)
            {
                actOnValueChange = (txt, str) => txt.text = str == null ? "" : str.ToString();
            }

            BaseCommand[] commands = new BaseCommand[2];

            var getListenable = text.onValueChanged.GetListenable();

            commands[0] = getListenable.Listen((str) =>
            {
                actOnChanged(text, property);
            }, text)
                          .RemoveWhenDestroy(text.gameObject)
                          .WhenMonobehaviorDestory(text.gameObject, (cmd) => { getListenable.RemoveAllListen(); });

            var setListenable = property.GetSetListenable();

            commands[1] = setListenable.Listen((str) => actOnValueChange(text, str), text)
                          .RemoveWhenDestroy(text.gameObject)
                          .WhenMonobehaviorDestory(text.gameObject, (cmd) => { setListenable.RemoveAllListen(); });

            return(commands);
        }
Exemple #3
0
        /// <summary>
        /// 绑定属性
        /// </summary>
        /// <typeparam name="V"></typeparam>
        /// <param name="text"></param>
        /// <param name="property"></param>
        /// <param name="converter"></param>
        /// <returns></returns>
        public static Binding <string, V> Bind <V>(this Text text, EventProperty <V> property, IValueConverter <string, V> converter)
        {
            var binding = text.GetEventText().Bind(property, converter);

            text.gameObject.GetCallbacks().onDestroy += binding.Unbind;
            return(binding);
        }
Exemple #4
0
        // <summary>
        /// 绑定UI
        /// </summary>
        public static BaseCommand Bind <T>(this Button btn, EventProperty <T> property, Action <Button, T> act)
        {
            var setListenable = property.GetSetListenable();

            return(setListenable.Listen((str) => act(btn, str), btn)
                   .RemoveWhenDestroy(btn.gameObject)
                   .WhenMonobehaviorDestory(btn.gameObject, (cmd) => { setListenable.RemoveAllListen(); }));
        }
Exemple #5
0
        /// <summary>
        /// 绑定UI
        /// </summary>
        public static BaseCommand Bind <T>(this Image img, EventProperty <T> property, Action <Image, T> actOnPropertyValurChange)
        {
            var setListenable = property.GetSetListenable();

            return(setListenable.Listen((str) => actOnPropertyValurChange(img, str), img)
                   .RemoveWhenDestroy(img.gameObject)
                   .WhenMonobehaviorDestory(img.gameObject, (cmd) => { setListenable.RemoveAllListen(); }));
        }
Exemple #6
0
        /// <summary>
        /// 绑定UI
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="text"></param>
        /// <param name="property"></param>
        /// <param name="act"></param>
        public static BaseCommand Bind <T>(this Text text, EventProperty <T> property, Action <Text, T> actOnPropertyChange = null)
        {
            if (actOnPropertyChange == null)
            {
                actOnPropertyChange = (txt, str) => txt.text = str == null ? "" : str.ToString();
            }

            var setListenable = property.GetSetListenable();

            return(setListenable.Listen((str) => actOnPropertyChange(text, str), text)
                   .RemoveWhenDestroy(text.gameObject)
                   .WhenMonobehaviorDestory(text.gameObject, (cmd) => { setListenable.RemoveAllListen(); }));
        }
Exemple #7
0
        /// <summary>
        /// 绑定UI
        /// </summary>
        /// <returns></returns>
        public static Binding <T, V> Bind(EventProperty <T> propertyA, EventProperty <V> propertyB,
                                          IValueConverter <T, V> converter)
        {
            Binding <T, V> binding = new Binding <T, V>();

            binding.propertyA = propertyA;
            binding.propertyB = propertyB;

            binding.a2bConverter = converter.Convert;
            binding.b2aConverter = converter.ConvertBack;

            propertyA.GetSetListenable().Listen(binding.A2B);
            propertyB.GetSetListenable().Listen(binding.B2A);

            return(binding);
        }
Exemple #8
0
        /// <summary>
        /// 绑定UI
        /// </summary>
        /// <returns></returns>
        public static Binding <T, V> Bind(EventProperty <T> propertyA, EventProperty <V> propertyB,
                                          Func <T, V> a2bConverter = null, Func <V, T> b2aConverter = null)
        {
            Binding <T, V> binding = new Binding <T, V>();

            binding.propertyA = propertyA;
            binding.propertyB = propertyB;

            binding.a2bConverter = a2bConverter ?? binding.DefaultA2BConverter;
            binding.b2aConverter = b2aConverter ?? binding.DefaultB2AConverter;

            binding.a2bCommand = propertyA.GetSetListenable().Listen(binding.A2B);
            binding.b2aCommand = propertyB.GetSetListenable().Listen(binding.B2A);

            return(binding);
        }
Exemple #9
0
        /// <summary>
        /// 将Text中的text包装为事件属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="text"></param>
        /// <returns></returns>
        public static EventProperty <string> GetEventText(this Text text)
        {
            var prop = new EventProperty <string>(() => { return(text.text); }, (x) => { text.text = x; });

            return(prop);
        }
Exemple #10
0
 /// <summary>
 /// 将两个属性之间进行绑定
 /// </summary>
 /// <typeparam name="V"></typeparam>
 /// <param name="prop"></param>
 /// <returns></returns>
 public Binding <T, V> Bind <V>(EventProperty <V> prop, IValueConverter <T, V> converter)
 {
     return(Binding <T, V> .Bind(this, prop, converter));
 }
Exemple #11
0
 /// <summary>
 /// 将两个属性之间进行绑定
 /// </summary>
 /// <typeparam name="V"></typeparam>
 /// <param name="prop"></param>
 /// <returns></returns>
 public Binding <T, V> Bind <V>(EventProperty <V> prop)
 {
     return(Binding <T, V> .Bind(this, prop));
 }