public static IDisposable BindInputButton(this ViewBase t, ICommand commandSelector, string buttonName,
                                                  InputButtonEventType buttonEventType = InputButtonEventType.ButtonDown)
        {
            if (buttonEventType == InputButtonEventType.Button)
            {
                return
                    (t.AddBinding(
                         t.UpdateAsObservable()
                         .Where(p => Input.GetButton(buttonName))
                         .Subscribe(_ => t.ExecuteCommand(commandSelector))));
            }
            else if (buttonEventType == InputButtonEventType.ButtonDown)
            {
                return
                    (t.AddBinding(
                         t.UpdateAsObservable()
                         .Where(p => Input.GetButtonDown(buttonName))
                         .Subscribe(_ => t.ExecuteCommand(commandSelector))));
            }

            return
                (t.AddBinding(
                     t.UpdateAsObservable()
                     .Where(p => Input.GetButtonUp(buttonName))
                     .Subscribe(_ => t.ExecuteCommand(commandSelector))));
        }
Beispiel #2
0
    public static ModelViewModelCollectionBinding BindToViewCollection <TCollectionType>(this ViewBase view,
                                                                                         ModelCollection <TCollectionType> viewModelCollection, Func <ViewModel,
                                                                                                                                                      ViewBase> createView,
                                                                                         Action <ViewBase> added,
                                                                                         Action <ViewBase> removed,
                                                                                         Transform parent,
                                                                                         bool viewFirst = false)
    {
        var binding = new ModelViewModelCollectionBinding()
        {
            SourceView            = view,
            ModelPropertySelector = () => viewModelCollection
        };

        binding.SetParent(parent);
        binding.SetAddHandler(added);
        binding.SetRemoveHandler(removed);
        binding.SetCreateHandler(createView);
        if (viewFirst)
        {
            binding.ViewFirst();
        }
        view.AddBinding(binding);
        binding.Bind();

        return(binding);
    }
 public static IDisposable BindKey(this ViewBase t, ICommand commandSelector, KeyCode key,
                                   object parameter = null)
 {
     return
         (t.AddBinding(
              t.UpdateAsObservable()
              .Where(p => Input.GetKey(key))
              .Subscribe(_ => t.ExecuteCommand(commandSelector, parameter))));
 }
Beispiel #4
0
    public static ModelCollectionBinding <TCollectionItemType> BindCollection <TCollectionItemType>(this ViewBase t, Func <ModelCollection <TCollectionItemType> > collectionSelector)
    {
        var binding = new ModelCollectionBinding <TCollectionItemType>()
        {
            ModelPropertySelector = () => collectionSelector(),
        };

        t.AddBinding(binding);
        binding.Bind();
        return(binding);
    }
Beispiel #5
0
 /// <summary>
 /// Bind a key to a ViewModel Command
 /// </summary>
 /// <param name="t">The view that owns the binding</param>
 /// <param name="commandSelector"></param>
 /// <param name="key"></param>
 /// <returns>The binding class that allows chaining extra options.</returns>
 public static IDisposable BindKey <TCommandType>(this ViewBase t, Signal <TCommandType> commandSelector,
                                                  KeyCode key, TCommandType parameter = null) where TCommandType : ViewModelCommand, new()
 {
     return
         (t.AddBinding(
              t.UpdateAsObservable()
              .Where(p => Input.GetKey(key))
              .Subscribe(
                  _ => commandSelector.OnNext(parameter ?? new TCommandType()
     {
         Sender = t.ViewModelObject
     }))));
 }
Beispiel #6
0
    public static ModelViewModelCollectionBinding BindToViewCollection <TCollectionType>(this ViewBase view,
                                                                                         Func <ModelCollection <TCollectionType> > viewModelCollection, bool viewFirst = false)
    {
        var binding = new ModelViewModelCollectionBinding()
        {
            SourceView            = view,
            ModelPropertySelector = () => viewModelCollection()
        };

        if (viewFirst)
        {
            binding.ViewFirst();
        }
        view.AddBinding(binding);
        return(binding);
    }
Beispiel #7
0
    public static ModelViewPropertyBinding BindToView <TBindingType>(this ViewBase view, Func <P <TBindingType> > sourceViewModelSelector, Action <ViewBase> setLocal = null, Func <ViewBase> getLocal = null)
        where TBindingType : ViewModel
    {
        var binding = new ModelViewPropertyBinding()
        {
            SourceView            = view,
            ModelPropertySelector = () => (IObservableProperty)sourceViewModelSelector(),
            TwoWay = false
        };

        if (getLocal != null)
        {
            binding.GetTargetValueDelegate = () => getLocal();
            if (setLocal == null)
            {
                throw new Exception("When using a BindToView you must set the setLocal parameter and getLocal parameter.");
            }
            binding.SetTargetValueDelegate = (o) => setLocal((ViewBase)o);
        }

        view.AddBinding(binding);
        binding.Bind();
        return(binding);
    }
Beispiel #8
0
    public static ModelViewModelCollectionBinding BindToViewCollection <TView, TViewModelType>(
        this ViewBase view,
        Func <ModelCollection <TViewModelType> > sourceViewModelCollection,
        ICollection <TView> viewCollection, bool viewFirst = false
        )
        where TView : ViewBase
        where TViewModelType : ViewModel
    {
        var binding = new ModelViewModelCollectionBinding()
        {
            SourceView            = view,
            ModelPropertySelector = () => sourceViewModelCollection() as IObservableProperty
        }
        .SetAddHandler(v => viewCollection.Add(v as TView))
        .SetRemoveHandler(v => viewCollection.Remove(v as TView));

        if (viewFirst)
        {
            binding.ViewFirst();
        }
        view.AddBinding(binding);
        binding.Bind();
        return(binding);
    }
Beispiel #9
0
        /// <summary>
        /// Bind a key to a ViewModel Command
        /// </summary>
        /// <param name="t">The view that owns the binding</param>
        /// <param name="commandSelector"></param>
        /// <param name="key"></param>
        /// <param name="throttle"></param>
        /// <returns>The binding class that allows chaining extra options.</returns>
        public static ModelKeyBinding BindKey(this ViewBase t, Func <ICommand> commandSelector, string keyName, object parameter = null)
        {
            if (commandSelector == null)
            {
                throw new ArgumentNullException("commandSelector");
            }
            var kb = t.gameObject.AddComponent <KeyBinding>();

            kb._KeyName  = keyName;
            kb.hideFlags = HideFlags.HideInInspector;
            var binding = kb.Binding as ModelKeyBinding;

            binding.Source          = t;
            binding.CommandDelegate = commandSelector;
            binding.Component       = kb;

            if (parameter != null)
            {
                binding.SetParameter(parameter);
            }

            t.AddBinding(binding);
            return(binding);
        }
Beispiel #10
0
 /// <summary>
 /// Adds a binding to a collision, when the collusion occurs the call back will be invoked.
 /// </summary>
 /// <param name="t"></param>
 /// <param name="eventType"></param>
 /// <param name="action"></param>
 /// <returns></returns>
 public static IDisposable BindCollision(this ViewBase t, CollisionEventType eventType, Action <Collision> action)
 {
     return(t.AddBinding(OnCollisionObservable(t.gameObject, eventType).Subscribe(action)));
 }
Beispiel #11
0
 public static IDisposable BindViewCollisionWith <T>(this ViewBase t, CollisionEventType eventType, Action <T> collision) where T : ViewBase
 {
     return(t.AddBinding(OnViewCollisionWith <T>(t.gameObject, eventType).Subscribe(collision)));
 }
 public static IDisposable BindComponentTrigger2DWith <T>(this ViewBase t, CollisionEventType eventType,
                                                          Action <T> collision) where T : MonoBehaviour
 {
     return(t.AddBinding(OnComponentTriggerWith2D <T>(t.gameObject, eventType).Subscribe(collision)));
 }
Beispiel #13
0
 /// <summary>
 /// Binds to a commands execution and is diposed with the bindable
 /// </summary>
 /// <param name="bindable"></param>
 /// <param name="sourceCommand"></param>
 /// <param name="executed"></param>
 /// <returns></returns>
 public static IDisposable BindCommandExecuted <TCommandType>(this ViewBase bindable,
                                                              Signal <TCommandType> sourceCommand, Action <TCommandType> executed)
     where TCommandType : IViewModelCommand, new()
 {
     return(bindable.AddBinding(sourceCommand.ObserveOnMainThread().Subscribe(executed)));//uFrame_kbe:ObserveOnMainThread()
 }
Beispiel #14
0
 /// <summary>
 /// Binds to a commands execution and is diposed with the bindable
 /// </summary>
 /// <param name="bindable"></param>
 /// <param name="sourceCommand"></param>
 /// <param name="executed"></param>
 /// <returns></returns>
 public static IDisposable BindCommandExecuted <TCommandType>(this ViewBase bindable,
                                                              Signal <TCommandType> sourceCommand, Action <TCommandType> executed)
     where TCommandType : ViewModelCommand, new()
 {
     return(bindable.AddBinding(sourceCommand.Subscribe(executed)));
 }