///Shows the Generic Menu as a browser with CompleteContextMenu.
 public static void ShowAsBrowser(this GenericMenu menu, string title, System.Type keyType)
 {
     if (menu != null)
     {
         CompleteContextMenu.Show(menu, Event.current.mousePosition, title, keyType);
     }
 }
 ///Shows the Generic Menu as a browser with CompleteContextMenu.
 public static void ShowAsBrowser(this GenericMenu menu, Vector2 pos, string title, System.Type keyType)
 {
     if (menu != null)
     {
         CompleteContextMenu.Show(menu, pos, title, keyType);
     }
 }
        //Shows a button that when clicked, pops a context menu with a list of tasks deriving the base type specified. When something is selected the callback is called
        //On top of that it also shows a search field for Tasks
        public static void TaskSelectionButton(ITaskSystem ownerSystem, Type baseType, Action <Task> callback)
        {
            Action <Type> TaskTypeSelected = (t) => {
                var newTask = Task.Create(t, ownerSystem);
                Undo.RecordObject(ownerSystem.baseObject, "New Task");
                callback(newTask);
            };

            Func <GenericMenu> GetMenu = () => {
                var menu = GetTypeSelectionMenu(baseType, TaskTypeSelected);
                if (Task.copiedTask != null && baseType.IsAssignableFrom(Task.copiedTask.GetType()))
                {
                    menu.AddItem(new GUIContent(string.Format("Paste ({0})", Task.copiedTask.name)), false, () => { callback(Task.copiedTask.Duplicate(ownerSystem)); });
                }
                return(menu);
            };


            GUI.backgroundColor = lightBlue;
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Add " + baseType.Name.SplitCamelCase()))
            {
                GetMenu().ShowAsContext();
                Event.current.Use();
            }
            if (EditorGUIUtility.isProSkin && GUILayout.Button("...", GUILayout.Width(22)))
            {
                CompleteContextMenu.Show(GetMenu(), Event.current.mousePosition, "Add Task");
                Event.current.Use();
            }
            GUILayout.EndHorizontal();


            GUI.backgroundColor = Color.white;
            GUILayout.BeginHorizontal();
            var search = EditorGUILayout.TextField(lastSearch, (GUIStyle)"ToolbarSeachTextField");

            if (GUILayout.Button("", (GUIStyle)"ToolbarSeachCancelButton"))
            {
                search = string.Empty;
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.EndHorizontal();

            if (!string.IsNullOrEmpty(search))
            {
                if (search != lastSearch)
                {
                    searchResults = GetScriptInfosOfType(baseType);
                }

                GUILayout.BeginVertical("TextField");
                foreach (var taskInfo in searchResults)
                {
                    if (taskInfo.name.ToLower().Trim().Contains(search.ToLower().Trim()))
                    {
                        if (GUILayout.Button(taskInfo.name))
                        {
                            search = string.Empty;
                            GUIUtility.keyboardControl = 0;
                            TaskTypeSelected(taskInfo.type);
                        }
                    }
                }
                GUILayout.EndVertical();
            }

            lastSearch = search;
        }
        public static void TaskSelectionButton(ITaskSystem ownerSystem, Type baseType, Action <Task> callback)
        {
            Action <Type> TaskTypeSelected = (t) => {
                var newTask = Task.Create(t, ownerSystem);
                Undo.RecordObject(ownerSystem.contextObject, "New Task");
                callback(newTask);
            };

            Func <GenericMenu> GetMenu = () => {
                var menu = GetTypeSelectionMenu(baseType, TaskTypeSelected);
                if (Task.copiedTask != null && baseType.IsAssignableFrom(Task.copiedTask.GetType()))
                {
                    menu.AddSeparator("/");
                    menu.AddItem(new GUIContent(string.Format("Paste ({0})", Task.copiedTask.name)), false, () => { callback(Task.copiedTask.Duplicate(ownerSystem)); });
                }
                return(menu);
            };

            GUI.backgroundColor = lightBlue;
            var label = "Assign " + baseType.Name.SplitCamelCase();

            if (GUILayout.Button(label))
            {
                var menu = GetMenu();
                if (NodeCanvas.Editor.NCPrefs.useBrowser)
                {
                    CompleteContextMenu.Show(menu, Event.current.mousePosition, label, typeof(Task));
                }
                else
                {
                    menu.ShowAsContext();
                }
                Event.current.Use();
            }


            GUI.backgroundColor = Color.white;
            GUILayout.BeginHorizontal();
            search = EditorGUILayout.TextField(search, (GUIStyle)"ToolbarSeachTextField");
            if (GUILayout.Button("", (GUIStyle)"ToolbarSeachCancelButton"))
            {
                search = string.Empty;
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.EndHorizontal();

            if (!string.IsNullOrEmpty(search))
            {
                GUILayout.BeginVertical("TextField");
                foreach (var taskInfo in GetScriptInfosOfType(baseType))
                {
                    if (taskInfo.name.Replace(" ", "").ToUpper().Contains(search.Replace(" ", "").ToUpper()))
                    {
                        if (GUILayout.Button(taskInfo.name))
                        {
                            search = string.Empty;
                            GUIUtility.keyboardControl = 0;
                            TaskTypeSelected(taskInfo.type);
                        }
                    }
                }
                GUILayout.EndVertical();
            }
        }