editor_command_palette.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**************************************************************************/
  2. /* editor_command_palette.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef EDITOR_COMMAND_PALETTE_H
  31. #define EDITOR_COMMAND_PALETTE_H
  32. #include "core/input/shortcut.h"
  33. #include "core/os/thread_safe.h"
  34. #include "scene/gui/dialogs.h"
  35. #include "scene/gui/tree.h"
  36. class EditorCommandPalette : public ConfirmationDialog {
  37. GDCLASS(EditorCommandPalette, ConfirmationDialog);
  38. static EditorCommandPalette *singleton;
  39. LineEdit *command_search_box = nullptr;
  40. Tree *search_options = nullptr;
  41. struct Command {
  42. Callable callable;
  43. String name;
  44. String shortcut;
  45. int last_used = 0; // Store time as int, because doubles have problems with text serialization.
  46. };
  47. struct CommandEntry {
  48. String key_name;
  49. String display_name;
  50. String shortcut_text;
  51. int last_used = 0;
  52. float score = 0;
  53. };
  54. struct CommandEntryComparator {
  55. _FORCE_INLINE_ bool operator()(const CommandEntry &A, const CommandEntry &B) const {
  56. return A.score > B.score;
  57. }
  58. };
  59. struct CommandHistoryComparator {
  60. _FORCE_INLINE_ bool operator()(const CommandEntry &A, const CommandEntry &B) const {
  61. if (A.last_used == B.last_used) {
  62. return A.display_name < B.display_name;
  63. } else {
  64. return A.last_used > B.last_used;
  65. }
  66. }
  67. };
  68. HashMap<String, Command> commands;
  69. HashMap<String, Pair<String, Ref<Shortcut>>> unregistered_shortcuts;
  70. List<String> command_keys;
  71. void _update_command_search(const String &search_text);
  72. float _score_path(const String &p_search, const String &p_path);
  73. void _sbox_input(const Ref<InputEvent> &p_ie);
  74. void _confirmed();
  75. void _update_command_keys();
  76. void _add_command(String p_command_name, String p_key_name, Callable p_binded_action, String p_shortcut_text = "None");
  77. void _theme_changed();
  78. void _save_history() const;
  79. EditorCommandPalette();
  80. protected:
  81. static void _bind_methods();
  82. void _notification(int p_what);
  83. public:
  84. void open_popup();
  85. void get_actions_list(List<String> *p_list) const;
  86. void add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, String p_shortcut_text = "None");
  87. void execute_command(String &p_command_name);
  88. void register_shortcuts_as_command();
  89. Ref<Shortcut> add_shortcut_command(const String &p_command, const String &p_key, Ref<Shortcut> p_shortcut);
  90. void remove_command(String p_key_name);
  91. static EditorCommandPalette *get_singleton();
  92. };
  93. Ref<Shortcut> ED_SHORTCUT_AND_COMMAND(const String &p_path, const String &p_name, Key p_keycode = Key::NONE, String p_command = "");
  94. #endif // EDITOR_COMMAND_PALETTE_H