Ejemplo n.º 1
0
    public DataBindingConnection(BaseViewModel viewModel, DataBindInfo dataBindInfo)
    {
        this.dataBindInfo = dataBindInfo;
        this.viewModel    = viewModel;


        _getProperty = ReflectionTool.GetVmPropertyByName(viewModel.GetType(), dataBindInfo.propertyName);
        if (_getProperty == null)
        {
            Debug.LogErrorFormat("get property null {0}:{1}", viewModel.GetType(), dataBindInfo.propertyName);
        }

        ReflectionMethodItem item = ReflectionTool.GetComponentMethod(dataBindInfo.component.GetType(), dataBindInfo.invokeFunctionName, _getProperty.PropertyType);

        if (item == null)
        {
            Debug.LogErrorFormat("get invokeMethod null {0}", dataBindInfo.invokeFunctionName);
        }

        _invokeMethod = item.methodInfo;

        foreach (var bindingParameter in dataBindInfo.parameters)
        {
            _lstParameter.Add(new BindingParameterInfo(bindingParameter, viewModel));
        }

        ps    = new object[dataBindInfo.parameters.Length + 2];
        ps[0] = dataBindInfo.component;
    }
Ejemplo n.º 2
0
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        if (property.isExpanded)
        {
            ReflectionMethodItem item = GetMethod(property);
            if (item == null)
            {
                return(60);
            }

            return(item.parameters.Length * 20 + 40);
        }

        return(20);
    }
Ejemplo n.º 3
0
    static void InitViewModelPropertys(Type vmType)
    {
        if (!vmType.IsSubclassOf(typeof(BaseViewModel)))
        {
            Debug.LogError("vmType not BaseViewModel");
            return;
        }

        if (!_dicVMMethods.TryGetValue(vmType, out List <ReflectionMethodItem> lstMetheds))
        {
            lstMetheds = new List <ReflectionMethodItem>();
            _dicVMMethods.Add(vmType, lstMetheds);
        }

        if (!_dicVMProperty.TryGetValue(vmType, out List <ReflectionPropertyItem> lstPropertys))
        {
            lstPropertys = new List <ReflectionPropertyItem>();
            _dicVMProperty.Add(vmType, lstPropertys);
        }

        MethodInfo[] members = vmType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(m => !m.IsSpecialName).ToArray();
        foreach (var member in members)
        {
            ReflectionMethodItem item = new ReflectionMethodItem()
            {
                methodInfo = member,
                Name       = member.Name,
                parameters = member.GetParameters().Select(m => m.ParameterType).ToArray()
            };

            lstMetheds.Add(item);
        }

        PropertyInfo[] propertys = vmType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(m => !m.IsSpecialName).ToArray();
        foreach (var property in propertys)
        {
            ReflectionPropertyItem item = new ReflectionPropertyItem()
            {
                propertyInfo = property,
                Name         = property.Name,
                propertyType = property.PropertyType
            };

            lstPropertys.Add(item);
        }
    }
Ejemplo n.º 4
0
    ReflectionMethodItem GetMethod(SerializedProperty property)
    {
        DataBindInfo bindingInfo = property.GetSerializedValue <DataBindInfo>();

        if (bindingInfo == null)
        {
            return(null);
        }

        Type         tO = property.serializedObject.targetObject.GetType();
        FieldInfo    viewModelProperty = tO.GetField("ViewModel", BindingFlags.NonPublic | BindingFlags.Instance);
        Type         vmType            = viewModelProperty.FieldType;
        PropertyInfo propertyInfo      = vmType.GetProperty(bindingInfo.propertyName);

        if (propertyInfo == null)
        {
            return(null);
        }

        ReflectionMethodItem item = ReflectionTool.GetComponentMethod(bindingInfo.component.GetType(), bindingInfo.invokeFunctionName, propertyInfo.PropertyType);

        return(item);
    }
Ejemplo n.º 5
0
    static List <ReflectionMethodItem> InitComponentFuncs(Type componentType)
    {
        if (_dicComponentOpts.ContainsKey(componentType))
        {
            return(null);
        }

        MethodInfo[] infos = typeof(UIBindingFunctions).GetMethods(BindingFlags.Public | BindingFlags.Static);
        foreach (var info in infos)
        {
            ParameterInfo[] ps = info.GetParameters();
            if (ps.Length < 2)
            {
                continue;
            }

            Type t = ps[0].ParameterType;
            if (!_dicComponentOpts.TryGetValue(t, out List <ReflectionMethodItem> list))
            {
                list = new List <ReflectionMethodItem>();
                _dicComponentOpts.Add(t, list);
            }

            ReflectionMethodItem item = new ReflectionMethodItem()
            {
                methodInfo = info,
                Name       = info.Name,
                parameters = info.GetParameters().Select(m => m.ParameterType).ToArray()
            };

            list.Add(item);
        }

        _dicComponentOpts.TryGetValue(componentType, out List <ReflectionMethodItem> lst);
        return(lst);
    }
Ejemplo n.º 6
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        InitErrorStyle();

        EditorGUI.BeginProperty(position, label, property);

        DataBindInfo bindingInfo = property.GetSerializedValue <DataBindInfo>();

        if (bindingInfo == null)
        {
            return;
        }
        string     title        = bindingInfo.component == null ? "NULL" : bindingInfo.component.ToString();
        GUIContent titleContent = new GUIContent(title);

        bool error = bindingInfo.component == null ||
                     string.IsNullOrEmpty(bindingInfo.invokeFunctionName) ||
                     string.IsNullOrEmpty(bindingInfo.propertyName);

        GUIStyle style = error ? errorStyle : EditorStyles.foldout;

        property.isExpanded = EditorGUI.Foldout(new Rect(position.position, new Vector2(position.width, 20)),
                                                property.isExpanded, titleContent, true, style);


        if (property.isExpanded)
        {
            var amountRect = new Rect(position.x, position.y + 20, position.width, 20);
            var unitRect   = new Rect(position.x, position.y + 40, position.width, 20);
            var nameRect   = new Rect(position.x, position.y + 60, position.width, 20);

            EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("component"), GUIContent.none);

            SerializedObject   o            = property.serializedObject;
            SerializedProperty methodMethod = property.FindPropertyRelative("invokeFunctionName");

            lstTemp.Clear();
            ReflectionTool.GetComponentMethods(bindingInfo.component.GetType(), lstTemp);

            int index;
            if (lstTemp.Count > 0)
            {
                index = lstTemp.IndexOf(methodMethod.stringValue);
                index = Mathf.Max(0, index);
                index = EditorGUI.Popup(unitRect, index, lstTemp.ToArray());
                methodMethod.stringValue = lstTemp[index];
            }
            else
            {
                methodMethod.stringValue = "";
            }

            Type      tO = o.targetObject.GetType();
            FieldInfo viewModelProperty = tO.GetField("ViewModel", BindingFlags.NonPublic | BindingFlags.Instance);
            Type      vmType            = viewModelProperty.FieldType;

            lstTemp.Clear();
            ReflectionTool.GetVmPropertysByMethod(vmType, bindingInfo.component.GetType(), methodMethod.stringValue, lstTemp);

            SerializedProperty propertyProperty = property.FindPropertyRelative("propertyName");
            if (lstTemp.Count > 0)
            {
                index = lstTemp.IndexOf(propertyProperty.stringValue);
                index = Mathf.Max(0, index);
                index = EditorGUI.Popup(nameRect, index, lstTemp.ToArray());
                propertyProperty.stringValue = lstTemp[index];
            }
            else
            {
                propertyProperty.stringValue = "";
            }

            ReflectionMethodItem item = GetMethod(property);
            if (item != null && item.parameters.Length > 2)
            {
                SerializedProperty parameterProperty = property.FindPropertyRelative("parameters");
                parameterProperty.arraySize = item.parameters.Length - 2;
                for (int i = 0; i < parameterProperty.arraySize; i++)
                {
                    var  rect          = new Rect(position.x, position.y + 80 + i * 20, 50, 20);
                    var  rectDetail    = new Rect(position.x + 50, position.y + 80 + i * 20, position.width - 50, 20);
                    Type parameterType = item.parameters[i + 2];

                    SerializedProperty parameter           = parameterProperty.GetArrayElementAtIndex(i);
                    SerializedProperty dataFromProperty    = parameter.FindPropertyRelative("dataFrom");
                    SerializedProperty paramTypeProperty   = parameter.FindPropertyRelative("paramType");
                    SerializedProperty stringParamProperty = parameter.FindPropertyRelative("paramStr");
                    paramTypeProperty.stringValue = parameterType.ToString();

                    dataFromProperty.intValue = EditorGUI.Popup(rect, dataFromProperty.intValue, dataFrom.ToArray());
                    if (dataFromProperty.intValue == (int)DataFrom.VM)
                    {
                        lstTemp.Clear();
                        ReflectionTool.GetVmPropertyByType(vmType, parameterType, lstTemp);

                        if (lstTemp.Count > 0)
                        {
                            index = lstTemp.IndexOf(stringParamProperty.stringValue);
                            index = Mathf.Max(0, index);
                            index = EditorGUI.Popup(rectDetail, index, lstTemp.ToArray());
                            stringParamProperty.stringValue = lstTemp[index];
                        }
                    }
                    else if (dataFromProperty.intValue == (int)DataFrom.Custom)
                    {
                        if (parameterType == typeof(string))
                        {
                            stringParamProperty.stringValue = EditorGUI.TextField(rectDetail, stringParamProperty.stringValue);
                        }
                        else
                        {
                            object x = null;
                            try
                            {
                                x = Convert.ChangeType(stringParamProperty.stringValue, parameterType);;
                            }
                            catch (System.Exception)
                            {
                                x = GetDefault(parameterType);
                            }

                            if (parameterType == typeof(int))
                            {
                                x = EditorGUI.IntField(rectDetail, (int)x);
                            }
                            else if (parameterType == typeof(bool))
                            {
                                x = EditorGUI.Toggle(rectDetail, (bool)x);
                            }
                            else if (parameterType == typeof(float))
                            {
                                x = EditorGUI.FloatField(rectDetail, (float)x);
                            }
                            stringParamProperty.stringValue = (string)Convert.ChangeType(x, typeof(string));
                        }
                    }

                    EditorGUI.PropertyField(rect, dataFromProperty, GUIContent.none);
                }
            }


            o.ApplyModifiedProperties();
        }

        EditorGUI.EndProperty();
    }