Example #1
0
        /// <summary>
        /// Creates a special storage locaiton wrapped in a BindingValue, which supports change notification.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="initialValue">The initial value to which the storage location is initialized.</param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static BindingValue <T> Variable <T>(T initialValue, string name = "?")
        {
            var storage = initialValue;
            BindingValue <T> bindingValue = null;
            Action <T>       setter       = v => {
                storage = v;
                bindingValue.NotifyChange();
            };
            Func <T> getter = () => storage;

            bindingValue = new BindingValue <T>(setter, getter, name);
            return(bindingValue);
        }
Example #2
0
        public QuickCheckbox(string name = "QuickCheckbox", Transform parent = null, UIOptionsTag prototype = null)
        {
            IEDebug.Log("Creating Checkbox : {0}", name);
            var chBox = (UIOptionsTag)Object.Instantiate(prototype ?? Prototype);

            chBox.transform.parent = parent;
            chBox.name             = name;

            chBox.transform.localScale    = new Vector3(1, 1, 1);
            chBox.transform.localPosition = new Vector3(0, 0, 0);
            GameObject      = chBox.gameObject;
            GameObject.name = name;
            IEDebug.Log("IEMod created: " + chBox.name);
            IsChecked = BindingValue.Member(() => this.isChecked).ToBindable();
            chBox.Checkbox.onStateChange += (a, b) => {
                IsChecked.NotifyChange();
            };
        }
Example #3
0
        public QuickDropdown(Transform parent = null, string name = "QuickDropdown", GameObject altPrototype = null)
        {
            var exampleDropdown = altPrototype ?? Prefabs.QuickDropdown;

            if (exampleDropdown == null)
            {
                throw IEDebug.Exception(null, "You must initialize the ExampleDropdown to create a dropdown.", null);
            }
            GameObject = (GameObject)Object.Instantiate(exampleDropdown);
            GameObject.transform.parent = parent;
            GameObject.name             = name;
            //! You have to explicitly set localPosition and localScale to something after Instantiate!!!
            //! Otherwise, the UI will broken, but no exception will be reported.
            GameObject.transform.localPosition = Vector3.zero;
            GameObject.transform.localScale    = Vector3.one;

            SelectedValue = BindingValue.Member(() => this.selectedValue).ToBindable();
            Options       = new List <DropdownChoice <T> >();
            DropdownComponent.OnDropdownOptionChangedEvent += x => SelectedValue.NotifyChange();
            IEDebug.Log("Created: " + name);
        }
Example #4
0
        /// <summary>
        /// Creates a BindingValue from an expression that accesses a property or field.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expr"></param>
        /// <param name="notifier"></param>
        /// <returns></returns>
        public static BindingValue <T> Member <T>(Expression <Func <T> > expr, INotifyPropertyChanged notifier = null)
        {
            var accessor = ReflectHelper.AnalyzeMember(expr);
            var instance = accessor.InstanceGetter();


            var instanceName = ReflectHelper.TryGetName(instance);

            notifier = notifier ?? accessor.InstanceGetter() as INotifyPropertyChanged;
            var bindingValue = new BindingValue <T>(accessor.Setter, accessor.Getter, $"{instanceName ?? "?"}.{accessor.TopmostMember.Name}");

            if (notifier != null)
            {
                notifier.PropertyChanged += (sender, e) => {
                    if (e.PropertyName == accessor.TopmostMember.Name)
                    {
                        bindingValue.NotifyChange();
                    }
                };
            }
            return(bindingValue);
        }
Example #5
0
 public static IBindingValue <T> Bind <T>(this Bindable <T> bindable, Expression <Func <T> > memberExpr, BindingMode mode = BindingMode.TwoWay)
 {
     return(bindable.Bind(BindingValue.Member(memberExpr)));
 }