Exemple #1
0
        public void UpdateBindings()
        {
            ViewModels = ViewModelProvider.Instance.ViewModels;

            if (_srcView != null)
            {
                var props = _srcView.GetType().GetProperties(/*BindingFlags.DeclaredOnly |*/ BindingFlags.Instance | BindingFlags.Public);

                SrcEvents = props
                            .Where(p => p.PropertyType.IsSubclassOf(typeof(UnityEventBase)) &&
                                   !p.GetCustomAttributes(typeof(ObsoleteAttribute), true).Any())
                            .Select(p => p.Name).ToList();

                if (!string.IsNullOrEmpty(SrcEventName))
                {
                    _srcEventProp = _srcView.GetType().GetProperty(SrcEventName);
                }
            }

            if (!string.IsNullOrEmpty(ViewModelName))
            {
                var methods = ViewModelProvider.GetViewModelType(ViewModelName)
                              .GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);

                DstMethods = methods.Where(m => !m.IsSpecialName && !m.GetCustomAttributes(typeof(ObsoleteAttribute), true).Any()).Select(e => e.Name).ToList();;
            }


            if (!string.IsNullOrEmpty(DstMethodName))
            {
                _method = ViewModelProvider.GetViewModelType(ViewModelName).GetMethod(DstMethodName);
            }
        }
Exemple #2
0
        protected virtual void BindEvent()
        {
            _dstViewModel = ViewModelProvider.Instance.GetViewModelBehaviour(ViewModelName);

            //TODO: Wrap PropertyInfo & MethodInfo in Serializable classes so we don't need reflection here

            if (_srcEventProp == null)
            {
                _srcEventProp = _srcView.GetType().GetProperty(SrcEventName);
            }

            if (_method == null)
            {
                _method = ViewModelProvider.GetViewModelType(ViewModelName).GetMethod(DstMethodName);
            }

            if (_method == null)
            {
                Debug.LogErrorFormat("EventBinding error in {0}. No method found in {1} with name {2}", gameObject.name, ViewModelName, DstMethodName);

                return;
            }

            var method = UnityEventBinder.GetAddListener(_srcEventProp.GetValue(_srcView));

            var arg = method.GetParameters()[0];

            d = Delegate.CreateDelegate(arg.ParameterType, _dstViewModel, _method);

            var p = new object[] { d };

            method.Invoke(_srcEventProp.GetValue(_srcView), p);
        }
        public override bool ExecuteTest()
        {
            if (_binder is null)
            {
                LogNotMineError();
                return(false);
            }

            bool ValidateSrcCollection(Type ownerType, BindablePropertyInfo property)
            {
                var propInfo = ownerType.GetProperty(property.PropertyName);

                return(!(propInfo is null) &&
                       propInfo.PropertyType.IsAssignableToGenericType(typeof(RxCollection <>)) &&
                       !propInfo.GetCustomAttributes(typeof(ObsoleteAttribute), true).Any());
            }

            var validateSrcCollection = ValidateSrcCollection(ViewModelProvider.GetViewModelType(_binder.ViewModelName), _binder.SrcCollectionName);

            if (!validateSrcCollection)
            {
                Errors.Push("Source collection is not valid");
            }
            return(validateSrcCollection);
        }
        public override bool ExecuteTest()
        {
            if (!base.ExecuteTest())
            {
                return(false);
            }

            if (_binder is null)
            {
                LogNotMineError();
                return(false);
            }

            bool ValidateSrcProperty(Type ownerType, BindablePropertyInfo property)
            {
                var propInfo = ownerType.GetProperty(property.PropertyName);

                if (propInfo == null)
                {
                    return(false);
                }

                var genericType = (property.IsStatic
                    ? propInfo.PropertyType
                    : propInfo.PropertyType.GetGenericTypeRecursive()?.GetGenericArguments()[0])?.Name;

                return(!propInfo.GetCustomAttributes(typeof(ObsoleteAttribute), true).Any() &&
                       property.PropertyType == genericType);
            }

            bool ValidateDstProperty(Type ownerType, BindablePropertyInfo property)
            {
                var propInfo = ownerType.GetProperty(property.PropertyName);

                return(!(propInfo is null) && propInfo.PropertyType.Name == property.PropertyType);
            }

            var validateSrcProperty = ValidateSrcProperty(ViewModelProvider.GetViewModelType(_binder.ViewModelName), _binder.SrcPropertyName);

            if (!validateSrcProperty)
            {
                Errors.Push("Source property is not valid");
            }
            var validateDstView = !(_binder._dstView is null);

            if (!validateDstView)
            {
                Errors.Push("Destination view is not valid");
            }
            var validateDstProperty = validateDstView && ValidateDstProperty(_binder._dstView.GetType(), _binder.DstPropertyName);

            if (!validateDstProperty)
            {
                Errors.Push("Destination property is not valid");
            }
            return(validateSrcProperty &&
                   validateDstView &&
                   validateDstProperty);
        }
        protected override void CollectPropertyLists()
        {
            var bindingMode = (BindingMode)_bindingModeProp.enumValueIndex;

            base.CollectPropertyLists();

            if (_viewModelChanged)
            {
                _srcNames.Value = null;
                _srcPaths.Value = null;
            }

            if (string.IsNullOrEmpty(_viewModelProp.Value))
            {
                return;
            }

            var view = _dstViewProp.objectReferenceValue as Component;

            _srcNames.Clear();
            _srcPaths.Clear();
            _dstNames.Clear();
            _dstPaths.Clear();

            _dstChangeEvents.Clear();


            if (view)
            {
                var dstNeedsGetter = bindingMode != BindingMode.OneWay;
                var dstNeedsSetter = bindingMode != BindingMode.OneWayToSource;

                _dstNames.Values        = view.GetBindablePropertyList(needsGetter: dstNeedsGetter, needsSetter: dstNeedsSetter);
                _dstPaths.Values        = view.GetPropertiesAndFieldsList(_dstNames.Value);
                _dstChangeEvents.Values = view.GetBindableEventsList();
            }
            else
            {
                _dstNames.Value        = null;
                _dstPaths.Value        = null;
                _dstChangeEvents.Value = null;
            }

            var vmType = ViewModelProvider.GetViewModelType(ViewModelName);

            _srcNames.Values = ViewModelProvider.GetViewModelPropertyList(ViewModelName);

            var propType = vmType.GetProperty(_srcNames.Value)?.PropertyType;

            if (propType != null)
            {
                _srcPaths.Values = propType.GetNestedFields();
            }
        }
        public override void UpdateBindings()
        {
            base.UpdateBindings();

            if (ViewModelProvider.IsViewModelTypeNameValid(ViewModelName))
            {
                var props = ViewModelProvider.GetViewModelType(ViewModelName).GetProperties();

                SrcCollections = props.Where(prop =>
                                             prop.PropertyType.IsAssignableToGenericType(typeof(RxCollection <>)) &&
                                             !prop.GetCustomAttributes(typeof(ObsoleteAttribute), true).Any()
                                             ).Select(e => new BindablePropertyInfo(e.Name, e.PropertyType.GenericTypeArguments[0].Name)).ToList();
            }
        }
Exemple #7
0
 public static void ViewModelField(SerializedList viewModelList)
 {
     EditorGUILayout.BeginHorizontal();
     EditorGUILayout.LabelField("View Model", labelOptions);
     viewModelList.Index = EditorGUILayout.Popup(viewModelList.Index, viewModelList.Values.ToArray());
     if (UnityEngine.GUILayout.Button("Open"))
     {
         var type  = ViewModelProvider.GetViewModelType(viewModelList.Value).Name;
         var str   = AssetDatabase.FindAssets(type).FirstOrDefault();
         var path  = AssetDatabase.GUIDToAssetPath(str);
         var asset = EditorGUIUtility.Load(path);
         AssetDatabase.OpenAsset(asset);
     }
     EditorGUILayout.EndHorizontal();
 }
Exemple #8
0
        protected override void CollectPropertyLists()
        {
            base.CollectPropertyLists();

            if (_viewModelChanged)
            {
                _srcNames.Value = null;
                _srcPaths.Value = null;
            }

            if (string.IsNullOrEmpty(_viewModelProp.Value))
            {
                return;
            }

            var view = _dstViewProp.objectReferenceValue as Component;

            _srcNames.Clear();
            _srcPaths.Clear();
            _dstNames.Clear();
            _dstPaths.Clear();


            if (view)
            {
                _dstNames.Values = view.GetBindablePropertyList(needsGetter: false);
                _dstPaths.Values = view.GetPropertiesAndFieldsList(_dstNames.Value);
            }
            else
            {
                _dstNames.Value = null;
                _dstPaths.Value = null;
            }

            var vmType = ViewModelProvider.GetViewModelType(ViewModelName);

            _srcNames.Values = ViewModelProvider.GetViewModelPropertyList(ViewModelName);

            var propType = vmType.GetProperty(_srcNames.Value)?.PropertyType;

            if (propType != null)
            {
                _srcPaths.Values = propType.GetNestedFields();
            }
        }
        protected override void CollectPropertyLists()
        {
            var myClass = target as CollectionViewBase;

            _selectedItemNames.Clear();
            _selectedItemCollectionNames.Clear();

            var collectionName = myClass.SrcCollectionName;

            if (!string.IsNullOrEmpty(collectionName))
            {
                var list = new List <string>();

                var listType = ViewModelProvider
                               .GetViewModelType(myClass.ViewModelName)
                               .GetProperty(collectionName)
                               .PropertyType
                               .GenericTypeArguments
                               .FirstOrDefault();

                var listCollectionType = typeof(ObservableCollection <>)
                                         .MakeGenericType(listType);


                list.Add("--");
                list.AddRange(ViewModelProvider.GetViewModelPropertyList(myClass.ViewModelName,
                                                                         _canSelectMultipleProp.boolValue ? listCollectionType : listType));

                if (_canSelectMultipleProp.boolValue)
                {
                    _selectedItemCollectionNames.Values = list;
                }
                else
                {
                    _selectedItemNames.Values = list;
                }
            }
        }
Exemple #10
0
        private static IEnumerable <ValidMethodMap> CalculateMethodMap(
            string target,
            System.Type[] t,
            bool allowSubclasses)
        {
            var validMethodMapList = new List <ValidMethodMap>();

            if (!ViewModelProvider.IsViewModelTypeNameValid(target) || t == null)
            {
                return(validMethodMapList);
            }
            var type     = ViewModelProvider.GetViewModelType(target);
            var commands = type.GetProperties().Where(x => x.PropertyType.GetInterfaces().Any(i =>
                                                                                              i.IsGenericType &&
                                                                                              new []
            {
                typeof(IReactiveCommand <>), typeof(IAsyncReactiveCommand <>)
            }.Any(tp => i.GetGenericTypeDefinition() == tp)))
                           /* .Select(c => c.FieldType.GetMethod("Execute")) */.ToArray();

            foreach (var com in commands)
            {
                MethodInfo c;
                if (com.PropertyType == typeof(RxCommand) || com.PropertyType == typeof(RxAsyncCommand))
                {
                    c = com.PropertyType.GetMethod("Execute", new Type[0]);
                }
                else if (com.PropertyType.IsGenericType &&
                         new []
                {
                    typeof(RxCommand <>), typeof(RxAsyncCommand <>)
                }.Any(tp => com.PropertyType.GetGenericTypeDefinition() == tp))
                {
                    c = com.PropertyType.GetMethod("Execute");
                }
                else
                {
                    continue;
                }
                var parameters = c.GetParameters();
                if (parameters.Length == t.Length && c.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length <= 0)
                {
                    var flag = true;
                    for (int i = 0; i < t.Length; ++i)
                    {
                        if (!parameters[i].ParameterType.IsAssignableFrom(t[i]))
                        {
                            flag = false;
                        }
                        if (allowSubclasses && t[i].IsAssignableFrom(parameters[i].ParameterType))
                        {
                            flag = true;
                        }
                    }
                    if (flag)
                    {
                        validMethodMapList.Add(
                            new ValidMethodMap()
                        {
                            target     = target,
                            methodInfo = c,
                            fieldName  = com.Name
                        });
                    }
                }
            }
            return(validMethodMapList);
        }
Exemple #11
0
        public override void OnInspectorGUI()
        {
            if (!(target is CollectionViewSource myClass))
            {
                return;
            }

            _srcIndex = myClass.SrcCollectionName is null
                ? -1
                : DataBindingBase.GetIndexOfBindablePropertyInfo(myClass.SrcCollectionName, ViewModelProvider.GetViewModelType(myClass.ViewModelName), myClass.SrcCollections);

            base.OnInspectorGUI();
        }