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

        private static void AddEventClipboardItems(GenericMenu menu, SerializedProperty property,
                                                   Serialization.PropertyAccessor accessor)
        {
            // Copy Event.
            menu.AddItem(new GUIContent("Copy Event"), false, () =>
            {
                Clipboard.CopyEvent(property);
            });

            // Paste Event.
            AddMenuItem(menu, "Paste Event (Overwrite)", Clipboard.HasEvent, () =>
            {
                property.ModifyValues <UltEventBase>((e) =>
                {
                    Clipboard.Paste(e);
                }, "Paste Event");
            });

            // Paste Listener.
            AddMenuItem(menu, "Paste Listener (New) %#V", Clipboard.HasCall, () =>
            {
                property.ModifyValues <UltEventBase>((e) =>
                {
                    var call = new PersistentCall();
                    Clipboard.PasteCall(call);

                    if (e._PersistentCalls == null)
                    {
                        e._PersistentCalls = new List <PersistentCall>();
                    }
                    e._PersistentCalls.Add(call);
                }, "Paste PersistentCall");
            });
        }
Ejemplo n.º 2
0
        /************************************************************************************************************************/

        /// <summary>Stores the details of the specified call.</summary>
        public static void CopyCall(PersistentCall call)
        {
            if (_Call == null)
            {
                _Call = new PersistentCall();
            }

            _Call.CopyFrom(call);
        }
Ejemplo n.º 3
0
        /// <summary>Cancels out a call to <see cref="BeginCall"/>.</summary>
        public void EndCall()
        {
            if (CachePreviousCalls)
            {
                PreviousCalls.Add(call);
            }

            call = null;
        }
Ejemplo n.º 4
0
        /************************************************************************************************************************/

        /// <summary>Caches the call from the specified property.</summary>
        public void BeginCall(SerializedProperty callProperty)
        {
            CallProperty = callProperty;

            TargetProperty              = GetTargetProperty(callProperty);
            MethodNameProperty          = GetMethodNameProperty(callProperty);
            PersistentArgumentsProperty = GetPersistentArgumentsProperty(callProperty);

            call = GetCall(callProperty);
        }
Ejemplo n.º 5
0
        private void OnTargetChanged(Object target)
        {
            if (target == null)
            {
                var persistentCall = Property.ValueEntry.WeakSmartValue as PersistentCallBase;

                var call = new PersistentCall();
                call.Set(null, string.Empty, persistentCall.Info);
                Property.BaseValueEntry.WeakSmartValue = call;

                return;
            }

            bool IsValidMethod(MethodInfo info)
            {
                if (info.IsGenericMethod)
                {
                    return(false);
                }
                if (info.ReturnType != typeof(void))
                {
                    return(false);
                }
                if (info.GetParameters().Length > 4)
                {
                    return(false);
                }

                return(true);
            }

            var methods = target.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(IsValidMethod).ToArray();

            string GetMethodName(MethodInfo info)
            {
                if (info.Name.Contains("set_"))
                {
                    return($"Properties/{info.GetParameters().First().ParameterType.Name} {info.Name.Remove(0,4)}");
                }
                else if (methods.Count(otherInfo => info.Name == otherInfo.Name) > 1)
                {
                    return($"Methods/{info.Name}/{info.GetNiceName()}");
                }
                else
                {
                    return($"Methods/{info.GetNiceName()}");
                }
            }

            dropdown = new GenericSelector <MethodInfo>(string.Empty, false, GetMethodName, methods);
            dropdown.SelectionTree.DefaultMenuStyle.Height = (int)EditorGUIUtility.singleLineHeight + 4;

            dropdown.SelectionChanged   += SaveMethod;
            dropdown.SelectionConfirmed += SaveMethod;
        }
Ejemplo n.º 6
0
        private void SaveMethod(IEnumerable <MethodInfo> infos)
        {
            var persistentCall = Property.ValueEntry.WeakSmartValue as PersistentCallBase;

            var info = infos.FirstOrDefault();

            if (info == null)
            {
                var current = persistentCall.GetMethod();
                if (current != null)
                {
                    dropdown.SetSelection(current);
                }

                return;
            }

            var parameters = info.GetParameters();
            var types      = new Type[parameters.Length];

            var method = info.Name;

            for (var i = 0; i < parameters.Length; i++)
            {
                types[i] = parameters[i].ParameterType;
                method  += $"/{types[i].AssemblyQualifiedName}";
            }


            PersistentCallBase call;

            if (parameters.Length == 0)
            {
                call = new PersistentCall();
            }
            else
            {
                var callDefinitions = new Type[]
                {
                    typeof(PersistentCall <>),
                    typeof(PersistentCall <,>),
                    typeof(PersistentCall <, ,>),
                    typeof(PersistentCall <, , ,>),
                };

                var callType = callDefinitions[parameters.Length - 1].MakeGenericType(types);
                call = Activator.CreateInstance(callType) as PersistentCallBase;
            }

            call.Set(persistentCall.Target, method, persistentCall.Info);
            Property.BaseValueEntry.WeakSmartValue = call;
        }
        /************************************************************************************************************************/

        /// <summary>
        /// Returns true if the specified `type` can be represented by a <see cref="PersistentArgument"/>.
        /// </summary>
        public static bool IsSupported(Type type)
        {
            if (PersistentCall.IsSupportedNative(type))
            {
                return(true);
            }
            else
            {
                int linkIndex;
                PersistentArgumentType linkType;
                return(DrawerState.Current.TryGetLinkable(type, out linkIndex, out linkType));
            }
        }
Ejemplo n.º 8
0
        /************************************************************************************************************************/

        /// <summary>Clears all the details of this state.</summary>
        public void Clear()
        {
            EventProperty = null;
            Event         = null;

            CallProperty                = null;
            TargetProperty              = null;
            MethodNameProperty          = null;
            PersistentArgumentsProperty = null;

            callIndex      = -1;
            call           = null;
            callParameters = null;
            parameterIndex = 0;

            PreviousCalls.Clear();
        }
        /************************************************************************************************************************/
        #endregion
        /************************************************************************************************************************/

        private static void DoMethodFieldGUI(Rect area, MethodBase method, bool autoOpenMethodMenu)
        {
            EditorGUI.BeginProperty(area, null, DrawerState.Current.MethodNameProperty);
            {
                if (includeRemoveButton)
                {
                    area.width -= RemoveButtonWidth;
                }

                var color = GUI.color;

                string label;
                if (EditorGUI.showMixedValue)
                {
                    label = "Mixed Values";
                }
                else if (method != null)
                {
                    label = MethodSelectionMenu.GetMethodSignature(method, false);

                    DoGetSetToggleGUI(ref area, method);
                }
                else
                {
                    var  methodName = DrawerState.Current.MethodNameProperty.stringValue;
                    Type declaringType;
                    PersistentCall.GetMethodDetails(methodName,
                                                    DrawerState.Current.TargetProperty.objectReferenceValue,
                                                    out declaringType, out label);
                    DoMethodNameSuggestionGUI(ref area, declaringType, methodName);
                    GUI.color = ErrorFieldColor;
                }

                if (autoOpenMethodMenu || (GUI.Button(area, GUIContent.none, PopupButtonStyle) && Event.current.button == 0))
                {
                    MethodSelectionMenu.ShowMenu(area);
                }

                GUI.color = color;

                PopupLabelStyle.fontStyle = DrawerState.Current.MethodNameProperty.prefabOverride ? FontStyle.Bold : FontStyle.Normal;
                GUI.Label(area, label, PopupLabelStyle);
            }
            EditorGUI.EndProperty();
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
        /************************************************************************************************************************/

        /// <summary>Overwrites the specified call with the previously copied details.</summary>
        public static void PasteCall(PersistentCall call)
        {
            call.CopyFrom(_Call);
        }
        /************************************************************************************************************************/
        #endregion
        /************************************************************************************************************************/
        #region Entry Point
        /************************************************************************************************************************/

        /// <summary>Opens the menu near the specified `area`.</summary>
        public static void ShowMenu(Rect area)
        {
            CachedState.CopyFrom(DrawerState.Current);

            _CurrentMethod = CachedState.call.GetMethodSafe();
            _Bindings      = GetBindingFlags();
            _Menu          = new GenericMenu();

            BoolPref.AddDisplayOptions(_Menu);

            Object[] targetObjects;
            var      targets = GetObjectReferences(CachedState.TargetProperty, out targetObjects);

            AddCoreItems(targets);

            // Populate the main contents of the menu.
            {
                if (targets == null)
                {
                    var    serializedMethodName = CachedState.MethodNameProperty.stringValue;
                    Type   declaringType;
                    string methodName;
                    PersistentCall.GetMethodDetails(serializedMethodName, null, out declaringType, out methodName);

                    // If we have no target, but do have a type, populate the menu with that type's statics.
                    if (declaringType != null)
                    {
                        PopulateMenuWithStatics(targetObjects, declaringType);

                        goto ShowMenu;
                    }
                    else// If we have no type either, pretend the inspected objects are the targets.
                    {
                        targets = targetObjects;
                    }
                }

                // Ensure that all targets share the same type.
                var firstTarget = ValidateTargetsAndGetFirst(targets);
                if (firstTarget == null)
                {
                    targets     = targetObjects;
                    firstTarget = targets[0];
                }

                // Add menu items according to the type of the target.
                if (firstTarget is GameObject)
                {
                    PopulateMenuForGameObject("", false, targets);
                }
                else if (firstTarget is Component)
                {
                    PopulateMenuForComponent(targets);
                }
                else
                {
                    PopulateMenuForObject(targets);
                }
            }

ShowMenu:

            _Menu.DropDown(area);

            GC.Collect();
        }
Ejemplo n.º 13
0
 public static Option <MethodInfo> __findMethod(this UnityEventBase evt, PersistentCall pc) =>
 findMethod(evt, pc);