editor_command_palette.cpp 13 KB

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