editor_quick_open.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**************************************************************************/
  2. /* editor_quick_open.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_quick_open.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/themes/editor_scale.h"
  35. Rect2i EditorQuickOpen::prev_rect = Rect2i();
  36. bool EditorQuickOpen::was_showed = false;
  37. void EditorQuickOpen::popup_dialog(const String &p_base, bool p_enable_multi, bool p_dont_clear) {
  38. base_type = p_base;
  39. allow_multi_select = p_enable_multi;
  40. search_options->set_select_mode(allow_multi_select ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  41. if (was_showed) {
  42. popup(prev_rect);
  43. } else {
  44. popup_centered_clamped(Size2(600, 440) * EDSCALE, 0.8f);
  45. }
  46. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
  47. _build_search_cache(efsd);
  48. if (p_dont_clear) {
  49. search_box->select_all();
  50. _update_search();
  51. } else {
  52. search_box->clear(); // This will emit text_changed.
  53. }
  54. search_box->grab_focus();
  55. }
  56. void EditorQuickOpen::_build_search_cache(EditorFileSystemDirectory *p_efsd) {
  57. for (int i = 0; i < p_efsd->get_subdir_count(); i++) {
  58. _build_search_cache(p_efsd->get_subdir(i));
  59. }
  60. Vector<String> base_types = base_type.split(",");
  61. for (int i = 0; i < p_efsd->get_file_count(); i++) {
  62. String file = p_efsd->get_file_path(i);
  63. String engine_type = p_efsd->get_file_type(i);
  64. String script_type = p_efsd->get_file_resource_script_class(i);
  65. String actual_type = script_type.is_empty() ? engine_type : script_type;
  66. // Iterate all possible base types.
  67. for (String &parent_type : base_types) {
  68. if (ClassDB::is_parent_class(engine_type, parent_type) || EditorNode::get_editor_data().script_class_is_parent(script_type, parent_type)) {
  69. files.push_back(file.substr(6, file.length()));
  70. // Store refs to used icons.
  71. String ext = file.get_extension();
  72. if (!icons.has(ext)) {
  73. icons.insert(ext, EditorNode::get_singleton()->get_class_icon(actual_type, "Object"));
  74. }
  75. // Stop testing base types as soon as we got a match.
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. void EditorQuickOpen::_update_search() {
  82. const PackedStringArray search_tokens = search_box->get_text().to_lower().replace("/", " ").split(" ", false);
  83. const bool empty_search = search_tokens.is_empty();
  84. // Filter possible candidates.
  85. Vector<Entry> entries;
  86. for (int i = 0; i < files.size(); i++) {
  87. Entry r;
  88. r.path = files[i];
  89. if (empty_search) {
  90. entries.push_back(r);
  91. } else {
  92. r.score = _score_search_result(search_tokens, r.path.to_lower());
  93. if (r.score > 0) {
  94. entries.push_back(r);
  95. }
  96. }
  97. }
  98. // Display results
  99. TreeItem *root = search_options->get_root();
  100. root->clear_children();
  101. if (entries.size() > 0) {
  102. if (!empty_search) {
  103. SortArray<Entry, EntryComparator> sorter;
  104. sorter.sort(entries.ptrw(), entries.size());
  105. }
  106. const int class_icon_size = search_options->get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  107. const int entry_limit = MIN(entries.size(), 300);
  108. for (int i = 0; i < entry_limit; i++) {
  109. TreeItem *ti = search_options->create_item(root);
  110. ti->set_text(0, entries[i].path);
  111. ti->set_icon_max_width(0, class_icon_size);
  112. ti->set_icon(0, *icons.lookup_ptr(entries[i].path.get_extension()));
  113. }
  114. TreeItem *to_select = root->get_first_child();
  115. to_select->select(0);
  116. to_select->set_as_cursor(0);
  117. search_options->scroll_to_item(to_select);
  118. get_ok_button()->set_disabled(false);
  119. } else {
  120. search_options->deselect_all();
  121. get_ok_button()->set_disabled(true);
  122. }
  123. }
  124. float EditorQuickOpen::_score_search_result(const PackedStringArray &p_search_tokens, const String &p_path) {
  125. float score = 0.0f;
  126. int prev_min_match_idx = -1;
  127. for (const String &s : p_search_tokens) {
  128. int min_match_idx = p_path.find(s);
  129. if (min_match_idx == -1) {
  130. return 0.0f;
  131. }
  132. float token_score = s.length();
  133. int max_match_idx = p_path.rfind(s);
  134. // Prioritize the actual file name over folder.
  135. if (max_match_idx > p_path.rfind("/")) {
  136. token_score *= 2.0f;
  137. }
  138. // Prioritize matches at the front of the path token.
  139. if (min_match_idx == 0 || p_path.contains("/" + s)) {
  140. token_score += 1.0f;
  141. }
  142. score += token_score;
  143. // Prioritize tokens which appear in order.
  144. if (prev_min_match_idx != -1 && max_match_idx > prev_min_match_idx) {
  145. score += 1.0f;
  146. }
  147. prev_min_match_idx = min_match_idx;
  148. }
  149. return score;
  150. }
  151. void EditorQuickOpen::_confirmed() {
  152. if (!search_options->get_selected()) {
  153. return;
  154. }
  155. _cleanup();
  156. hide();
  157. emit_signal(SNAME("quick_open"));
  158. }
  159. void EditorQuickOpen::cancel_pressed() {
  160. _cleanup();
  161. }
  162. void EditorQuickOpen::_cleanup() {
  163. files.clear();
  164. icons.clear();
  165. }
  166. void EditorQuickOpen::_text_changed(const String &p_newtext) {
  167. _update_search();
  168. }
  169. void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
  170. Ref<InputEventKey> k = p_ie;
  171. if (k.is_valid()) {
  172. switch (k->get_keycode()) {
  173. case Key::UP:
  174. case Key::DOWN:
  175. case Key::PAGEUP:
  176. case Key::PAGEDOWN: {
  177. search_options->gui_input(k);
  178. search_box->accept_event();
  179. if (allow_multi_select) {
  180. TreeItem *root = search_options->get_root();
  181. if (!root->get_first_child()) {
  182. break;
  183. }
  184. TreeItem *current = search_options->get_selected();
  185. TreeItem *item = search_options->get_next_selected(root);
  186. while (item) {
  187. item->deselect(0);
  188. item = search_options->get_next_selected(item);
  189. }
  190. current->select(0);
  191. current->set_as_cursor(0);
  192. }
  193. } break;
  194. default:
  195. break;
  196. }
  197. }
  198. }
  199. String EditorQuickOpen::get_selected() const {
  200. TreeItem *ti = search_options->get_selected();
  201. if (!ti) {
  202. return String();
  203. }
  204. return "res://" + ti->get_text(0);
  205. }
  206. Vector<String> EditorQuickOpen::get_selected_files() const {
  207. Vector<String> selected_files;
  208. TreeItem *item = search_options->get_next_selected(search_options->get_root());
  209. while (item) {
  210. selected_files.push_back("res://" + item->get_text(0));
  211. item = search_options->get_next_selected(item);
  212. }
  213. return selected_files;
  214. }
  215. String EditorQuickOpen::get_base_type() const {
  216. return base_type;
  217. }
  218. void EditorQuickOpen::_notification(int p_what) {
  219. switch (p_what) {
  220. case NOTIFICATION_ENTER_TREE: {
  221. connect(SceneStringName(confirmed), callable_mp(this, &EditorQuickOpen::_confirmed));
  222. search_box->set_clear_button_enabled(true);
  223. } break;
  224. case NOTIFICATION_VISIBILITY_CHANGED: {
  225. if (!is_visible()) {
  226. prev_rect = Rect2i(get_position(), get_size());
  227. was_showed = true;
  228. }
  229. } break;
  230. case NOTIFICATION_THEME_CHANGED: {
  231. search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  232. } break;
  233. case NOTIFICATION_EXIT_TREE: {
  234. disconnect(SceneStringName(confirmed), callable_mp(this, &EditorQuickOpen::_confirmed));
  235. } break;
  236. }
  237. }
  238. void EditorQuickOpen::_bind_methods() {
  239. ADD_SIGNAL(MethodInfo("quick_open"));
  240. }
  241. EditorQuickOpen::EditorQuickOpen() {
  242. VBoxContainer *vbc = memnew(VBoxContainer);
  243. add_child(vbc);
  244. search_box = memnew(LineEdit);
  245. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpen::_text_changed));
  246. search_box->connect(SceneStringName(gui_input), callable_mp(this, &EditorQuickOpen::_sbox_input));
  247. vbc->add_margin_child(TTR("Search:"), search_box);
  248. register_text_enter(search_box);
  249. search_options = memnew(Tree);
  250. search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  251. search_options->connect("item_activated", callable_mp(this, &EditorQuickOpen::_confirmed));
  252. search_options->create_item();
  253. search_options->set_hide_root(true);
  254. search_options->set_hide_folding(true);
  255. search_options->add_theme_constant_override("draw_guides", 1);
  256. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  257. set_ok_button_text(TTR("Open"));
  258. set_hide_on_ok(false);
  259. }