editor_command_palette.cpp 13 KB

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