/************************************************************************************************************************/

        private static void DoGetSetToggleGUI(ref Rect area, MethodBase method)
        {
            // Check if the method name starts with "get_" or "set_".
            // Check the underscore first since it's hopefully the rarest so it can break out early.

            var name = method.Name;

            if (name.Length <= 4 || name[3] != '_' || name[2] != 't' || name[1] != 'e')
            {
                return;
            }

            var first = name[0];
            var isGet = first == 'g';
            var isSet = first == 's';

            if (!isGet && !isSet)
            {
                return;
            }

            var methodName             = (isGet ? "set_" : "get_") + name.Substring(4, name.Length - 4);
            var oppositePropertyMethod = method.DeclaringType.GetMethod(methodName, UltEventUtils.AnyAccessBindings);

            if (oppositePropertyMethod == null ||
                (isGet && !MethodSelectionMenu.IsSupported(method.GetReturnType())))
            {
                return;
            }

            area.width -= GetSetWidth + Padding;

            var buttonArea = new Rect(
                area.x + area.width + Padding,
                area.y,
                GetSetWidth,
                area.height);

            if (GUI.Button(buttonArea, isGet ? "Get" : "Set"))
            {
                var cachedState = new DrawerState();
                cachedState.CopyFrom(DrawerState.Current);

                EditorApplication.delayCall += () =>
                {
                    DrawerState.Current.CopyFrom(cachedState);

                    SetMethod(oppositePropertyMethod);

                    DrawerState.Current.Clear();

                    InternalEditorUtility.RepaintAllViews();
                };
            }
        }
Esempio n. 2
0
        /************************************************************************************************************************/
        #endregion
        /************************************************************************************************************************/

        /// <summary>Copies the contents of the 'other' state to overwrite this one.</summary>
        public void CopyFrom(DrawerState other)
        {
            EventProperty = other.EventProperty;
            Event         = other.Event;

            CallProperty                = other.CallProperty;
            TargetProperty              = other.TargetProperty;
            MethodNameProperty          = other.MethodNameProperty;
            PersistentArgumentsProperty = other.PersistentArgumentsProperty;

            callIndex      = other.callIndex;
            call           = other.call;
            callParameters = other.callParameters;
            parameterIndex = other.parameterIndex;

            PreviousCalls.Clear();
            PreviousCalls.AddRange(other.PreviousCalls);
        }
Esempio n. 3
0
        private void DelayedRemoveCall(int index)
        {
            var list  = _CurrentCallList;
            var state = new DrawerState();

            state.CopyFrom(DrawerState.Current);

            EditorApplication.delayCall += () =>
            {
                DrawerState.Current.CopyFrom(state);

                RemoveCall(list, index);

                DrawerState.Current.UpdateLinkedArguments();
                DrawerState.Current.Clear();

                InternalEditorUtility.RepaintAllViews();
            };
        }
        /************************************************************************************************************************/

        public override float GetPropertyHeight(SerializedProperty callProperty, GUIContent label)
        {
            if (callProperty.hasMultipleDifferentValues)
            {
                if (DrawerState.GetPersistentArgumentsProperty(callProperty).hasMultipleDifferentValues)
                {
                    return(EditorGUIUtility.singleLineHeight);
                }

                if (DrawerState.GetMethodNameProperty(callProperty).hasMultipleDifferentValues)
                {
                    return(EditorGUIUtility.singleLineHeight);
                }
            }

            if (DrawerState.GetCall(callProperty).GetMethodSafe() == null)
            {
                return(EditorGUIUtility.singleLineHeight);
            }

            callProperty = DrawerState.GetPersistentArgumentsProperty(callProperty);

            return((EditorGUIUtility.singleLineHeight + Padding) * (1 + callProperty.arraySize) - Padding);
        }
        /************************************************************************************************************************/

        private static void DoMethodNameSuggestionGUI(ref Rect area, Type declaringType, string methodName)
        {
            if (declaringType == null ||
                string.IsNullOrEmpty(methodName))
            {
                return;
            }

            var lastDot = methodName.LastIndexOf('.');

            if (lastDot >= 0)
            {
                lastDot++;
                if (lastDot >= methodName.Length)
                {
                    return;
                }

                methodName = methodName.Substring(lastDot);
            }

            var methods = declaringType.GetMethods(UltEventUtils.AnyAccessBindings);

            if (methods.Length == 0)
            {
                return;
            }

            area.width -= SuggestionButtonWidth + Padding;

            var buttonArea = new Rect(
                area.x + area.width + Padding,
                area.y,
                SuggestionButtonWidth,
                area.height);

            if (GUI.Button(buttonArea, MethodNameSuggestionLabel))
            {
                var cachedState = new DrawerState();
                cachedState.CopyFrom(DrawerState.Current);

                EditorApplication.delayCall += () =>
                {
                    DrawerState.Current.CopyFrom(cachedState);

                    var bestMethod   = methods[0];
                    var bestDistance = UltEventUtils.CalculateLevenshteinDistance(methodName, bestMethod.Name);

                    var i = 1;
                    for (; i < methods.Length; i++)
                    {
                        var method   = methods[i];
                        var distance = UltEventUtils.CalculateLevenshteinDistance(methodName, method.Name);

                        if (bestDistance > distance)
                        {
                            bestDistance = distance;
                            bestMethod   = method;
                        }
                    }

                    SetMethod(bestMethod);

                    DrawerState.Current.Clear();

                    InternalEditorUtility.RepaintAllViews();
                };
            }
        }