editor_command_palette.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**************************************************************************/
  2. /* editor_command_palette.cpp */
  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. #include "editor/editor_command_palette.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "scene/gui/control.h"
  36. #include "scene/gui/tree.h"
  37. EditorCommandPalette *EditorCommandPalette::singleton = nullptr;
  38. static Rect2i prev_rect = Rect2i();
  39. static bool was_showed = false;
  40. float EditorCommandPalette::_score_path(const String &p_search, const String &p_path) {
  41. float score = 0.9f + .1f * (p_search.length() / (float)p_path.length());
  42. // Positive bias for matches close to the beginning of the file name.
  43. int pos = p_path.findn(p_search);
  44. if (pos != -1) {
  45. return score * (1.0f - 0.1f * (float(pos) / p_path.length()));
  46. }
  47. // Positive bias for matches close to the end of the path.
  48. pos = p_path.rfindn(p_search);
  49. if (pos != -1) {
  50. return score * (0.8f - 0.1f * (float(p_path.length() - pos) / p_path.length()));
  51. }
  52. // Remaining results belong to the same class of results.
  53. return score * 0.69f;
  54. }
  55. void EditorCommandPalette::_update_command_search(const String &search_text) {
  56. ERR_FAIL_COND(commands.size() == 0);
  57. HashMap<String, TreeItem *> sections;
  58. TreeItem *first_section = nullptr;
  59. // Filter possible candidates.
  60. Vector<CommandEntry> entries;
  61. for (const KeyValue<String, Command> &E : commands) {
  62. CommandEntry r;
  63. r.key_name = E.key;
  64. r.display_name = E.value.name;
  65. r.shortcut_text = E.value.shortcut;
  66. r.last_used = E.value.last_used;
  67. bool is_subsequence_of_key_name = search_text.is_subsequence_ofn(r.key_name);
  68. bool is_subsequence_of_display_name = search_text.is_subsequence_ofn(r.display_name);
  69. if (is_subsequence_of_key_name || is_subsequence_of_display_name) {
  70. if (!search_text.is_empty()) {
  71. float key_name_score = is_subsequence_of_key_name ? _score_path(search_text, r.key_name.to_lower()) : .0f;
  72. float display_name_score = is_subsequence_of_display_name ? _score_path(search_text, r.display_name.to_lower()) : .0f;
  73. r.score = MAX(key_name_score, display_name_score);
  74. }
  75. entries.push_back(r);
  76. }
  77. }
  78. command_keys.clear();
  79. TreeItem *root = search_options->get_root();
  80. root->clear_children();
  81. if (entries.is_empty()) {
  82. get_ok_button()->set_disabled(true);
  83. return;
  84. }
  85. if (!search_text.is_empty()) {
  86. SortArray<CommandEntry, CommandEntryComparator> sorter;
  87. sorter.sort(entries.ptrw(), entries.size());
  88. } else {
  89. SortArray<CommandEntry, CommandHistoryComparator> sorter;
  90. sorter.sort(entries.ptrw(), entries.size());
  91. }
  92. const int entry_limit = MIN(entries.size(), 300);
  93. for (int i = 0; i < entry_limit; i++) {
  94. String section_name = entries[i].key_name.get_slice("/", 0);
  95. TreeItem *section;
  96. if (sections.has(section_name)) {
  97. section = sections[section_name];
  98. } else {
  99. section = search_options->create_item(root);
  100. if (!first_section) {
  101. first_section = section;
  102. }
  103. String item_name = section_name.capitalize();
  104. section->set_text(0, item_name);
  105. section->set_selectable(0, false);
  106. section->set_selectable(1, false);
  107. section->set_custom_bg_color(0, search_options->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  108. section->set_custom_bg_color(1, search_options->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  109. sections[section_name] = section;
  110. }
  111. TreeItem *ti = search_options->create_item(section);
  112. String shortcut_text = entries[i].shortcut_text == "None" ? "" : entries[i].shortcut_text;
  113. ti->set_text(0, entries[i].display_name);
  114. ti->set_metadata(0, entries[i].key_name);
  115. ti->set_text_alignment(1, HORIZONTAL_ALIGNMENT_RIGHT);
  116. ti->set_text(1, shortcut_text);
  117. Color c = get_theme_color(SNAME("font_color"), SNAME("Editor")) * Color(1, 1, 1, 0.5);
  118. ti->set_custom_color(1, c);
  119. }
  120. TreeItem *to_select = first_section->get_first_child();
  121. to_select->select(0);
  122. to_select->set_as_cursor(0);
  123. search_options->ensure_cursor_is_visible();
  124. }
  125. void EditorCommandPalette::_bind_methods() {
  126. ClassDB::bind_method(D_METHOD("add_command", "command_name", "key_name", "binded_callable", "shortcut_text"), &EditorCommandPalette::_add_command, DEFVAL("None"));
  127. ClassDB::bind_method(D_METHOD("remove_command", "key_name"), &EditorCommandPalette::remove_command);
  128. }
  129. void EditorCommandPalette::_notification(int p_what) {
  130. switch (p_what) {
  131. case NOTIFICATION_VISIBILITY_CHANGED: {
  132. if (!is_visible()) {
  133. prev_rect = Rect2i(get_position(), get_size());
  134. was_showed = true;
  135. }
  136. } break;
  137. }
  138. }
  139. void EditorCommandPalette::_sbox_input(const Ref<InputEvent> &p_ie) {
  140. Ref<InputEventKey> k = p_ie;
  141. if (k.is_valid()) {
  142. switch (k->get_keycode()) {
  143. case Key::UP:
  144. case Key::DOWN:
  145. case Key::PAGEUP:
  146. case Key::PAGEDOWN: {
  147. search_options->gui_input(k);
  148. } break;
  149. default:
  150. break;
  151. }
  152. }
  153. }
  154. void EditorCommandPalette::_confirmed() {
  155. TreeItem *selected_option = search_options->get_selected();
  156. String command_key = selected_option != nullptr ? selected_option->get_metadata(0) : "";
  157. if (!command_key.is_empty()) {
  158. hide();
  159. execute_command(command_key);
  160. }
  161. }
  162. void EditorCommandPalette::open_popup() {
  163. if (was_showed) {
  164. popup(prev_rect);
  165. } else {
  166. popup_centered_clamped(Size2(600, 440) * EDSCALE, 0.8f);
  167. }
  168. command_search_box->clear();
  169. command_search_box->grab_focus();
  170. search_options->scroll_to_item(search_options->get_root());
  171. }
  172. void EditorCommandPalette::get_actions_list(List<String> *p_list) const {
  173. for (const KeyValue<String, Command> &E : commands) {
  174. p_list->push_back(E.key);
  175. }
  176. }
  177. void EditorCommandPalette::remove_command(String p_key_name) {
  178. ERR_FAIL_COND_MSG(!commands.has(p_key_name), "The Command '" + String(p_key_name) + "' doesn't exists. Unable to remove it.");
  179. commands.erase(p_key_name);
  180. }
  181. void EditorCommandPalette::add_command(String p_command_name, String p_key_name, Callable p_action, Vector<Variant> arguments, String p_shortcut_text) {
  182. ERR_FAIL_COND_MSG(commands.has(p_key_name), "The Command '" + String(p_command_name) + "' already exists. Unable to add it.");
  183. const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * arguments.size());
  184. for (int i = 0; i < arguments.size(); i++) {
  185. argptrs[i] = &arguments[i];
  186. }
  187. Command command;
  188. command.name = p_command_name;
  189. command.callable = p_action.bindp(argptrs, arguments.size());
  190. command.shortcut = p_shortcut_text;
  191. commands[p_key_name] = command;
  192. }
  193. void EditorCommandPalette::_add_command(String p_command_name, String p_key_name, Callable p_binded_action, String p_shortcut_text) {
  194. ERR_FAIL_COND_MSG(commands.has(p_key_name), "The Command '" + String(p_command_name) + "' already exists. Unable to add it.");
  195. Command command;
  196. command.name = p_command_name;
  197. command.callable = p_binded_action;
  198. command.shortcut = p_shortcut_text;
  199. // Commands added from plugins don't exist yet when the history is loaded, so we assign the last use time here if it was recorded.
  200. Dictionary command_history = EditorSettings::get_singleton()->get_project_metadata("command_palette", "command_history", Dictionary());
  201. if (command_history.has(p_key_name)) {
  202. command.last_used = command_history[p_key_name];
  203. }
  204. commands[p_key_name] = command;
  205. }
  206. void EditorCommandPalette::execute_command(String &p_command_key) {
  207. ERR_FAIL_COND_MSG(!commands.has(p_command_key), p_command_key + " not found.");
  208. commands[p_command_key].last_used = OS::get_singleton()->get_unix_time();
  209. commands[p_command_key].callable.call_deferred();
  210. _save_history();
  211. }
  212. void EditorCommandPalette::register_shortcuts_as_command() {
  213. for (const KeyValue<String, Pair<String, Ref<Shortcut>>> &E : unregistered_shortcuts) {
  214. String command_name = E.value.first;
  215. Ref<Shortcut> shortcut = E.value.second;
  216. Ref<InputEventShortcut> ev;
  217. ev.instantiate();
  218. ev->set_shortcut(shortcut);
  219. String shortcut_text = String(shortcut->get_as_text());
  220. add_command(command_name, E.key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_input), varray(ev, false), shortcut_text);
  221. }
  222. unregistered_shortcuts.clear();
  223. // Load command use history.
  224. Dictionary command_history = EditorSettings::get_singleton()->get_project_metadata("command_palette", "command_history", Dictionary());
  225. Array history_entries = command_history.keys();
  226. for (int i = 0; i < history_entries.size(); i++) {
  227. const String &history_key = history_entries[i];
  228. if (commands.has(history_key)) {
  229. commands[history_key].last_used = command_history[history_key];
  230. }
  231. }
  232. }
  233. Ref<Shortcut> EditorCommandPalette::add_shortcut_command(const String &p_command, const String &p_key, Ref<Shortcut> p_shortcut) {
  234. if (is_inside_tree()) {
  235. Ref<InputEventShortcut> ev;
  236. ev.instantiate();
  237. ev->set_shortcut(p_shortcut);
  238. String shortcut_text = String(p_shortcut->get_as_text());
  239. add_command(p_command, p_key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_input), varray(ev, false), shortcut_text);
  240. } else {
  241. const String key_name = String(p_key);
  242. const String command_name = String(p_command);
  243. Pair pair = Pair(command_name, p_shortcut);
  244. unregistered_shortcuts[key_name] = pair;
  245. }
  246. return p_shortcut;
  247. }
  248. void EditorCommandPalette::_theme_changed() {
  249. command_search_box->set_right_icon(search_options->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  250. }
  251. void EditorCommandPalette::_save_history() const {
  252. Dictionary command_history;
  253. for (const KeyValue<String, Command> &E : commands) {
  254. if (E.value.last_used > 0) {
  255. command_history[E.key] = E.value.last_used;
  256. }
  257. }
  258. EditorSettings::get_singleton()->set_project_metadata("command_palette", "command_history", command_history);
  259. }
  260. EditorCommandPalette *EditorCommandPalette::get_singleton() {
  261. if (singleton == nullptr) {
  262. singleton = memnew(EditorCommandPalette);
  263. }
  264. return singleton;
  265. }
  266. EditorCommandPalette::EditorCommandPalette() {
  267. set_hide_on_ok(false);
  268. connect("confirmed", callable_mp(this, &EditorCommandPalette::_confirmed));
  269. VBoxContainer *vbc = memnew(VBoxContainer);
  270. vbc->connect("theme_changed", callable_mp(this, &EditorCommandPalette::_theme_changed));
  271. add_child(vbc);
  272. command_search_box = memnew(LineEdit);
  273. command_search_box->set_placeholder(TTR("Filter Commands"));
  274. command_search_box->connect("gui_input", callable_mp(this, &EditorCommandPalette::_sbox_input));
  275. command_search_box->connect("text_changed", callable_mp(this, &EditorCommandPalette::_update_command_search));
  276. command_search_box->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  277. command_search_box->set_clear_button_enabled(true);
  278. MarginContainer *margin_container_csb = memnew(MarginContainer);
  279. margin_container_csb->add_child(command_search_box);
  280. vbc->add_child(margin_container_csb);
  281. register_text_enter(command_search_box);
  282. search_options = memnew(Tree);
  283. search_options->connect("item_activated", callable_mp(this, &EditorCommandPalette::_confirmed));
  284. search_options->connect("item_selected", callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(false));
  285. search_options->connect("nothing_selected", callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(true));
  286. search_options->create_item();
  287. search_options->set_hide_root(true);
  288. search_options->set_columns(2);
  289. search_options->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  290. search_options->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  291. search_options->set_column_custom_minimum_width(0, int(8 * EDSCALE));
  292. vbc->add_child(search_options, true);
  293. }
  294. Ref<Shortcut> ED_SHORTCUT_AND_COMMAND(const String &p_path, const String &p_name, Key p_keycode, String p_command_name) {
  295. if (p_command_name.is_empty()) {
  296. p_command_name = p_name;
  297. }
  298. Ref<Shortcut> shortcut = ED_SHORTCUT(p_path, p_name, p_keycode);
  299. EditorCommandPalette::get_singleton()->add_shortcut_command(p_command_name, p_path, shortcut);
  300. return shortcut;
  301. }