Наследование: UnityEditor.PropertyDrawer
Пример #1
0
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        var vprop  = serializedObject.FindProperty("_target");
        var vmprop = serializedObject.FindProperty("_source");
        var veprop = serializedObject.FindProperty("_targetEvent");
        var cprop  = serializedObject.FindProperty("_converter");
        var mprop  = serializedObject.FindProperty("_mode");

        EditorGUILayout.PropertyField(vprop, new GUIContent(vprop.displayName, "Typically, the Target would be a View"));

        var epropcount = DrawCrefEvents(vprop, veprop);

        EditorGUILayout.PropertyField(vmprop, new GUIContent(vmprop.displayName, "Typically, the Source would be a ViewModel"));

        EditorGUILayout.PropertyField(mprop, false);
        if (epropcount == 0)
        {
            if (mprop.enumValueIndex > 1)
            {
                EditorUtility.DisplayDialog("Error", string.Format("Cannot change {0} to {1}, as only {2} and {3} are valid for no event",
                                                                   mprop.displayName, (BindingMode)mprop.enumValueIndex, BindingMode.OneTime, BindingMode.OneWayToTarget), "Okay");
                mprop.enumValueIndex = 1;
            }
        }

        var position = EditorGUILayout.GetControlRect(true);

        ComponentReferenceDrawer.PropertyField(position, cprop);

        serializedObject.ApplyModifiedProperties();
    }
Пример #2
0
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        ComponentReferenceDrawer.PropertyField(EditorGUILayout.GetControlRect(), _vprop);

        if (_vprop.objectReferenceValue != null)
        {
            PropertyBindingEditor.DrawComponentEvents(_vprop, _veprop);
        }

        EditorGUILayout.PropertyField(_vmprop);

        var rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);

        using (var propertyScope = new EditorGUI.PropertyScope(rect, null, _parmprop))
        {
            GUI.Label(rect, propertyScope.content);

            //parameter type
            var typeProp = _parmprop.FindPropertyRelative("Type");
            var trect    = rect;
            trect.x     += EditorGUIUtility.labelWidth;
            trect.width -= EditorGUIUtility.labelWidth;
            EditorGUI.PropertyField(trect, typeProp);

            //value field
            var typeValue = (BindingParameterType)Enum.GetValues(typeof(BindingParameterType)).GetValue(typeProp.enumValueIndex);
            SerializedProperty valueProp = null;
            switch (typeValue)
            {
            case BindingParameterType.None:
                break;

            default:
                valueProp = _parmprop.FindPropertyRelative(typeValue.ToString());
                break;
            }
            if (valueProp != null)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(valueProp);
                EditorGUI.indentLevel--;
            }
        }

        serializedObject.ApplyModifiedProperties();
    }
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        var target = serializedObject.FindProperty("_target");
        var targetUpdateTrigger = serializedObject.FindProperty("_targetUpdateTrigger");
        var targetEvent         = serializedObject.FindProperty("_targetEvent");

        var source = serializedObject.FindProperty("_source");
        var sourceUpdateTrigger = serializedObject.FindProperty("_sourceUpdateTrigger");
        var sourceEvent         = serializedObject.FindProperty("_sourceEvent");

        var converter = serializedObject.FindProperty("_converter");
        var mode      = serializedObject.FindProperty("_mode");

        int targetTriggerCount = DrawBindingComponent(target, "Typically, the Target would be a View", targetUpdateTrigger, targetEvent, ((BindingMode)mode.intValue).IsSourceBoundToTarget(), false);

        int sourceTriggerCount = DrawBindingComponent(source, "Typically, the Source would be a ViewModel", sourceUpdateTrigger, sourceEvent, ((BindingMode)mode.intValue).IsTargetBoundToSource(), true);

        EditorGUILayout.PropertyField(mode, false);

        if (targetTriggerCount == 0 && ((BindingMode)mode.intValue).IsSourceBoundToTarget())
        {
            EditorUtility.DisplayDialog("Error", string.Format("Cannot change {0} to {1}, as only {2} and {3} are valid for no target updated event",
                                                               mode.displayName, mode.enumNames[mode.enumValueIndex], nameof(BindingMode.OneTime), nameof(BindingMode.OneWayToTarget)), "Okay");
            mode.intValue = (int)BindingMode.OneTime;
        }

        if (sourceTriggerCount == 0 && ((BindingMode)mode.intValue).IsTargetBoundToSource())
        {
            EditorUtility.DisplayDialog("Error", string.Format("Cannot change {0} to {1}, as only {2} and {3} are valid for no source updated event",
                                                               mode.displayName, mode.enumNames[mode.enumValueIndex], nameof(BindingMode.OneTime), nameof(BindingMode.OneWayToSource)), "Okay");
            mode.intValue = (int)BindingMode.OneTime;
        }

        var position = EditorGUILayout.GetControlRect(true);

        ComponentReferenceDrawer.PropertyField(position, converter);

        serializedObject.ApplyModifiedProperties();
    }
Пример #4
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.LabelField(position, label);
            EditorGUI.indentLevel++;

            SerializedProperty component, propertyPath;

            GetCPathProperties(property, out component, out propertyPath);

            using (var changeScope = new EditorGUI.ChangeCheckScope())
            {
                position = EditorGUILayout.GetControlRect(false);
                ComponentReferenceDrawer.PropertyField(position, component);

                if (changeScope.changed)
                {
                    // Notify the suggestion provider that any cached list of suggestions are now invalid.
                    _suggestionProvider.FireSuggestionsChangedEvent();
                }
            }

            if (_autoSuggestField == null)
            {
                _suggestionProvider = new PropertyPathSuggestionProvider(property);
                _autoSuggestField   = new AutoSuggestField(
                    _suggestionProvider,
                    new GUIContent(propertyPath.displayName),
                    new AutoSuggestField.Options
                {
                    DisplayMode = DisplayMode.Inline,
                });
            }

            propertyPath.stringValue = _autoSuggestField.OnGUI(propertyPath.stringValue);

            EditorGUI.indentLevel--;
        }
Пример #5
0
    /// <summary>
    /// draw an INPCBinding.ComponentPath property
    /// </summary>
    /// <param name="position"></param>
    /// <param name="property"></param>
    /// <param name="label"></param>
    /// <param name="filter"></param>
    public static void DrawCRefProp(SerializedProperty property, GUIContent label, Type filter, bool resolveDataContext = true)
    {
        EditorGUILayout.LabelField(property.displayName);

        SerializedProperty cprop, pprop;

        GetCPathProperties(property, out cprop, out pprop);

        EditorGUI.indentLevel++;
        var position = EditorGUILayout.GetControlRect(true);

        ComponentReferenceDrawer.PropertyField(position, cprop);

        if (cprop.objectReferenceValue != null)
        {
            var name = "propfield" + (GUIUtility.GetControlID(FocusType.Keyboard) + 1);
            GUI.SetNextControlName(name);
            EditorGUILayout.PropertyField(pprop);

            var  orv = cprop.objectReferenceValue;
            Type ortype;
            if (orv is DataContext && resolveDataContext)
            {
                ortype = (orv as DataContext).Type;
            }
            else
            {
                ortype = orv.GetType();
            }

            if (ortype == null)
            {
                pprop.stringValue = null;
            }
            else
            {
                var  path = new INPCBinding.PropertyPath(pprop.stringValue, ortype);
                Type rtype;
                var  idx = Array.FindIndex(path.PPath, p => p == null);
                if (path.IsValid)
                {
                    rtype = path.PPath.Length == 1 ? ortype : path.PPath[path.PPath.Length - 1].PropertyType;
                }
                else
                {
                    rtype = idx - 1 < 0 ? ortype : path.PPath[idx - 1].PropertyType;
                }


                var lrect = GUILayoutUtility.GetLastRect();
                EditorGUI.Toggle(new Rect(lrect.x + EditorGUIUtility.fieldWidth + 5, lrect.y, lrect.width, lrect.height), path.IsValid);

                var props = rtype.GetProperties(BindingFlags.Instance | BindingFlags.Public);


                if (GUI.GetNameOfFocusedControl() == name)
                {
                    var propNames = props.Select(p => p.Name)
                                    .OrderByDescending(
                        s => s.IndexOf(path.Parts.LastOrDefault() ?? "", StringComparison.OrdinalIgnoreCase) == 0)
                                    .ToArray();

                    var propstring = string.Join("\n", propNames);
                    EditorGUILayout.HelpBox(propstring, MessageType.None);
                }
            }
        }
        else
        {
            pprop.stringValue = null;
        }

        EditorGUI.indentLevel--;
    }
Пример #6
0
    /// <summary>
    /// draw an INPCBinding.ComponentPath property
    /// </summary>
    /// <param name="targetId"></param>
    /// <param name="property"></param>
    /// <param name="label"></param>
    /// <param name="filter"></param>
    /// <param name="resolveDataContext"></param>
    public static void DrawCRefProp(int targetId, string focusedControl, SerializedProperty property, GUIContent label, Type filter, bool resolveDataContext = true)
    {
        EditorGUILayout.LabelField(property.displayName);

        SerializedProperty cprop, pprop;

        GetCPathProperties(property, out cprop, out pprop);

        EditorGUI.indentLevel++;
        var position = EditorGUILayout.GetControlRect(true);

        ComponentReferenceDrawer.PropertyField(position, cprop);

        if (cprop.objectReferenceValue != null)
        {
            var name = "prop_" + property.propertyPath + "_" + targetId;
            GUI.SetNextControlName(name);
            EditorGUILayout.PropertyField(pprop);

            var  orv = cprop.objectReferenceValue;
            Type ortype;
            if (orv is DataContext && resolveDataContext)
            {
                ortype = (orv as DataContext).Type;
            }
            else
            {
                ortype = orv.GetType();
            }

            if (ortype == null)
            {
                // Handle invalid DataContext types
                if (!string.IsNullOrEmpty(pprop.stringValue))
                {
                    var style = new GUIStyle(EditorStyles.textField);
                    style.normal.textColor = Color.red;

                    EditorGUILayout.TextField(string.Format("Error: {0}/{1} is bound to property \"{2}\" of an invalid DataContext Type.",
                                                            property.displayName,
                                                            pprop.displayName,
                                                            pprop.stringValue),
                                              style);
                }
            }
            else
            {
                var  path = new INPCBinding.PropertyPath(pprop.stringValue, ortype);
                Type rtype;
                var  idx = Array.FindIndex(path.PPath, p => p == null);
                if (path.IsValid)
                {
                    rtype = path.PropertyType;
                }
                else
                {
                    rtype = idx - 1 < 0 ? ortype : path.PPath[idx - 1].PropertyType;
                    // Improve handling of invalid DataContext types
                    var style = new GUIStyle(EditorStyles.textField);
                    style.normal.textColor = Color.red;
                    EditorGUILayout.TextField(string.Format("Error: {0}/{1} invalid property \"{2}\" of a valid DataContext.",
                                                            property.displayName,
                                                            pprop.displayName,
                                                            pprop.stringValue),
                                              style);
                }

                var lrect = GUILayoutUtility.GetLastRect();
                var props = rtype.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                if (focusedControl == name)
                {
                    var propNames = props.Select(p => p.Name)
                                    .OrderByDescending(
                        s => s.IndexOf(path.Parts.LastOrDefault() ?? "", StringComparison.OrdinalIgnoreCase) == 0)
                                    .ToArray();

                    var propstring = string.Join("\n", propNames);
                    EditorGUILayout.HelpBox(propstring, MessageType.None);
                }
            }
        }
        else
        {
            // Improve handling of invalid DataContext types
            if (!string.IsNullOrEmpty(pprop.stringValue))
            {
                var style = new GUIStyle(EditorStyles.textField);
                style.normal.textColor = Color.red;

                EditorGUILayout.TextField(string.Format("Error: {0}/{1} is bound to property \"{2}\" of an invalid component object reference.",
                                                        property.displayName,
                                                        pprop.displayName,
                                                        pprop.stringValue),
                                          style);
            }
        }

        EditorGUI.indentLevel--;
    }