Exemplo n.º 1
0
        /// <summary>
        /// Set up and bind a given UnityEvent with a list of types matching its generic type arguments
        /// and an adapter.
        /// </summary>
        public UnityEventBinderBase Create(UnityEventBase unityEvent,
                                           IViewModelBinding viewModel,
                                           string methodName,
                                           IAdapter adapter)
        {
            // TODO Rory (16/09/2016): Add argument checking

            // Note that to find the paramaters of events on the UI, we need to see what
            // generic arguments were passed to the UnityEvent they inherit from.
            var eventArgumentTypes = unityEvent.GetType().BaseType.GetGenericArguments();

            if (eventArgumentTypes.Count() != 1)
            {
                throw new ApplicationException("Adapters can only be used on events with a single argument");
            }

            var adapterType = adapter.GetType().GetCustomAttributes(false)
                              .Where(attribute => attribute.GetType() == typeof(AdapterAttribute))
                              .Select(attribute => (AdapterAttribute)attribute)
                              .Single()
                              .OutputType;

            var genericType = typeof(UnityEventBinder <>).MakeGenericType(eventArgumentTypes);

            return((UnityEventBinderBase)Activator
                   .CreateInstance(genericType, unityEvent, viewModel, methodName, adapter));
        }
Exemplo n.º 2
0
 public UnityEventBinder(UnityEventBase unityEvent, IViewModelBinding viewModel, string methodName)
 {
     this.unityEvent = (UnityEvent)unityEvent;
     this.viewModel  = viewModel;
     this.methodName = methodName;
     this.unityEvent.AddListener(EventHandler);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Connect to the attached view model.
        /// </summary>
        public override void Connect()
        {
            Disconnect();

            // Cache available templates.
            var templatesInScene = templates.GetComponentsInChildren <TemplateBinding>();

            foreach (var template in templatesInScene)
            {
                availableTemplates.Add(template.ViewModelTypeName, template);
            }

            this.viewModelBinding = GetViewModelBinding();

            // Subscribe to property changed events.
            var notifyPropertyChanged = viewModelBinding.BoundViewModel as INotifyPropertyChanged;

            if (notifyPropertyChanged != null)
            {
                notifyPropertyChanged.PropertyChanged += NotifyPropertyChanged_PropertyChanged;
            }

            // Get property from view model.
            viewModelProperty = viewModelBinding
                                .BoundViewModel.GetType()
                                .GetProperty(viewModelPropertyName);
            if (viewModelProperty == null)
            {
                throw new ApplicationException("Expected property " + viewModelPropertyName + " not found on type " + viewModelName);
            }

            InitalizeTemplate();
        }
Exemplo n.º 4
0
        public override void Connect()
        {
            this.viewModelBinding = GetViewModelBinding();

            var notifyPropertyChanged = viewModelBinding.BoundViewModel as INotifyPropertyChanged;

            if (notifyPropertyChanged != null)
            {
                notifyPropertyChanged.PropertyChanged += NotifyPropertyChanged_PropertyChanged;
            }

            BindCollection();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Return the type of a view model bound by an IViewModelBinding
        /// </summary>
        private static Type GetBoundViewType(IViewModelBinding binding)
        {
            var type = TypesWithBindingAttribute
                       .Where(t => t.Name == binding.ViewModelTypeName)
                       .FirstOrDefault();

            if (type == null)
            {
                throw new ApplicationException("Could not find the specified view model \"" + binding.ViewModelTypeName + "\"");
            }

            return(type);
        }
Exemplo n.º 6
0
        public override void Disconnect()
        {
            UnbindCollection();

            if (this.viewModelBinding != null)
            {
                var notifyPropertyChanged = viewModelBinding.BoundViewModel as INotifyPropertyChanged;
                if (notifyPropertyChanged != null)
                {
                    notifyPropertyChanged.PropertyChanged -= NotifyPropertyChanged_PropertyChanged;
                }

                viewModelBinding = null;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Disconnect from the attached view model.
        /// </summary>
        public override void Disconnect()
        {
            DestroyTemplate();
            availableTemplates.Clear();

            if (viewModelBinding != null)
            {
                // Unsubscribe from property changed events.
                var notifyPropertyChanged = viewModelBinding.BoundViewModel as INotifyPropertyChanged;
                if (notifyPropertyChanged != null)
                {
                    notifyPropertyChanged.PropertyChanged -= NotifyPropertyChanged_PropertyChanged;
                }
            }
            viewModelBinding = null;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Set up and bind a given UnityEvent with a list of types matching its generic type arguments.
        /// </summary>
        public UnityEventBinderBase Create(UnityEventBase unityEvent, IViewModelBinding viewModel, string methodName)
        {
            // Note that to find the paramaters of events on the UI, we need to see what
            // generic arguments were passed to the UnityEvent they inherit from.
            var eventArgumentTypes = unityEvent.GetType().BaseType.GetGenericArguments();

            if (!eventArgumentTypes.Any())
            {
                return(new UnityEventBinder(unityEvent, viewModel, methodName));
            }

            try
            {
                var genericType = typeof(UnityEventBinder <>).MakeGenericType(eventArgumentTypes);
                return((UnityEventBinderBase)Activator.CreateInstance(genericType, unityEvent, viewModel, methodName));
            }
            catch (ArgumentException ex)
            {
                // There are only UnityEvents and UnityActions that support up to 5 arguments.
                // MakeGenericType will throw an ArgumentException if it is used to try and create a type with arguments that don't match any generic type.
                throw new ApplicationException("Cannot bind event with more than 5 arguments", ex);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Create a new EventBinder
        /// </summary>
        public EventBinder(GameObject gameObject, string methodName, string eventName, string boundComponentType, IAdapter adapter, IViewModelBinding viewModel)
        {
            this.gameObject         = gameObject;
            this.boundEventName     = eventName;
            this.boundComponentType = boundComponentType;

            var boundEvent = GetBoundEvent();

            if (adapter == null)
            {
                unityEventBinder = new UnityEventBinderFactory().Create(boundEvent.UnityEvent, viewModel, methodName);
            }
            else
            {
                unityEventBinder = new UnityEventBinderFactory().Create(boundEvent.UnityEvent, viewModel, methodName, adapter);
            }
        }