editor_quick_open.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*************************************************************************/
  2. /* editor_quick_open.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_quick_open.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/editor_node.h"
  33. void EditorQuickOpen::popup_dialog(const StringName &p_base, bool p_enable_multi, bool p_dontclear) {
  34. base_type = p_base;
  35. allow_multi_select = p_enable_multi;
  36. search_options->set_select_mode(allow_multi_select ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  37. popup_centered_clamped(Size2i(600, 440), 0.8f);
  38. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
  39. _build_search_cache(efsd);
  40. if (p_dontclear) {
  41. search_box->select_all();
  42. _update_search();
  43. } else {
  44. search_box->clear(); // This will emit text_changed.
  45. }
  46. search_box->grab_focus();
  47. }
  48. void EditorQuickOpen::_build_search_cache(EditorFileSystemDirectory *p_efsd) {
  49. for (int i = 0; i < p_efsd->get_subdir_count(); i++) {
  50. _build_search_cache(p_efsd->get_subdir(i));
  51. }
  52. Vector<String> base_types = String(base_type).split(String(","));
  53. for (int i = 0; i < p_efsd->get_file_count(); i++) {
  54. String file = p_efsd->get_file_path(i);
  55. String engine_type = p_efsd->get_file_type(i);
  56. // TODO: Fix lack of caching for resource's script's global class name (if applicable).
  57. String script_type;
  58. if (_load_resources) {
  59. Ref<Resource> res = ResourceLoader::load(file);
  60. if (res.is_valid()) {
  61. Ref<Script> scr = res->get_script();
  62. if (scr.is_valid()) {
  63. script_type = scr->get_language()->get_global_class_name(file);
  64. }
  65. }
  66. }
  67. String actual_type = script_type.is_empty() ? engine_type : script_type;
  68. // Iterate all possible base types.
  69. for (String &parent_type : base_types) {
  70. if (ClassDB::is_parent_class(engine_type, parent_type) || EditorNode::get_editor_data().script_class_is_parent(script_type, parent_type)) {
  71. files.push_back(file.substr(6, file.length()));
  72. // Store refs to used icons.
  73. String ext = file.get_extension();
  74. if (!icons.has(ext)) {
  75. icons.insert(ext, get_theme_icon((has_theme_icon(actual_type, SNAME("EditorIcons")) ? actual_type : String("Object")), SNAME("EditorIcons")));
  76. }
  77. // Stop testing base types as soon as we got a match.
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. void EditorQuickOpen::_update_search() {
  84. const String search_text = search_box->get_text();
  85. const bool empty_search = search_text.is_empty();
  86. // Filter possible candidates.
  87. Vector<Entry> entries;
  88. for (int i = 0; i < files.size(); i++) {
  89. if (empty_search || search_text.is_subsequence_ofn(files[i])) {
  90. Entry r;
  91. r.path = files[i];
  92. r.score = empty_search ? 0 : _score_path(search_text, files[i].to_lower());
  93. entries.push_back(r);
  94. }
  95. }
  96. // Display results
  97. TreeItem *root = search_options->get_root();
  98. root->clear_children();
  99. if (entries.size() > 0) {
  100. if (!empty_search) {
  101. SortArray<Entry, EntryComparator> sorter;
  102. sorter.sort(entries.ptrw(), entries.size());
  103. }
  104. const int entry_limit = MIN(entries.size(), 300);
  105. for (int i = 0; i < entry_limit; i++) {
  106. TreeItem *ti = search_options->create_item(root);
  107. ti->set_text(0, entries[i].path);
  108. ti->set_icon(0, *icons.lookup_ptr(entries[i].path.get_extension()));
  109. }
  110. TreeItem *to_select = root->get_first_child();
  111. to_select->select(0);
  112. to_select->set_as_cursor(0);
  113. search_options->scroll_to_item(to_select);
  114. get_ok_button()->set_disabled(false);
  115. } else {
  116. search_options->deselect_all();
  117. get_ok_button()->set_disabled(true);
  118. }
  119. }
  120. float EditorQuickOpen::_score_path(const String &p_search, const String &p_path) {
  121. float score = 0.9f + .1f * (p_search.length() / (float)p_path.length());
  122. // Exact match.
  123. if (p_search == p_path) {
  124. return 1.2f;
  125. }
  126. // Positive bias for matches close to the beginning of the file name.
  127. String file = p_path.get_file();
  128. int pos = file.findn(p_search);
  129. if (pos != -1) {
  130. return score * (1.0f - 0.1f * (float(pos) / file.length()));
  131. }
  132. // Similarity
  133. return p_path.to_lower().similarity(p_search.to_lower());
  134. }
  135. void EditorQuickOpen::_confirmed() {
  136. if (!search_options->get_selected()) {
  137. return;
  138. }
  139. _cleanup();
  140. hide();
  141. emit_signal(SNAME("quick_open"));
  142. }
  143. void EditorQuickOpen::cancel_pressed() {
  144. _cleanup();
  145. }
  146. void EditorQuickOpen::_cleanup() {
  147. files.clear();
  148. icons.clear();
  149. }
  150. void EditorQuickOpen::_text_changed(const String &p_newtext) {
  151. _update_search();
  152. }
  153. void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
  154. Ref<InputEventKey> k = p_ie;
  155. if (k.is_valid()) {
  156. switch (k->get_keycode()) {
  157. case Key::UP:
  158. case Key::DOWN:
  159. case Key::PAGEUP:
  160. case Key::PAGEDOWN: {
  161. search_options->gui_input(k);
  162. search_box->accept_event();
  163. if (allow_multi_select) {
  164. TreeItem *root = search_options->get_root();
  165. if (!root->get_first_child()) {
  166. break;
  167. }
  168. TreeItem *current = search_options->get_selected();
  169. TreeItem *item = search_options->get_next_selected(root);
  170. while (item) {
  171. item->deselect(0);
  172. item = search_options->get_next_selected(item);
  173. }
  174. current->select(0);
  175. current->set_as_cursor(0);
  176. }
  177. } break;
  178. default:
  179. break;
  180. }
  181. }
  182. }
  183. String EditorQuickOpen::get_selected() const {
  184. TreeItem *ti = search_options->get_selected();
  185. if (!ti) {
  186. return String();
  187. }
  188. return "res://" + ti->get_text(0);
  189. }
  190. Vector<String> EditorQuickOpen::get_selected_files() const {
  191. Vector<String> selected_files;
  192. TreeItem *item = search_options->get_next_selected(search_options->get_root());
  193. while (item) {
  194. selected_files.push_back("res://" + item->get_text(0));
  195. item = search_options->get_next_selected(item);
  196. }
  197. return selected_files;
  198. }
  199. StringName EditorQuickOpen::get_base_type() const {
  200. return base_type;
  201. }
  202. void EditorQuickOpen::_notification(int p_what) {
  203. switch (p_what) {
  204. case NOTIFICATION_ENTER_TREE: {
  205. connect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
  206. search_box->set_clear_button_enabled(true);
  207. } break;
  208. case NOTIFICATION_EXIT_TREE: {
  209. disconnect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
  210. } break;
  211. }
  212. }
  213. void EditorQuickOpen::_theme_changed() {
  214. search_box->set_right_icon(search_options->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  215. }
  216. void EditorQuickOpen::_bind_methods() {
  217. ADD_SIGNAL(MethodInfo("quick_open"));
  218. }
  219. EditorQuickOpen::EditorQuickOpen() {
  220. VBoxContainer *vbc = memnew(VBoxContainer);
  221. vbc->connect("theme_changed", callable_mp(this, &EditorQuickOpen::_theme_changed));
  222. add_child(vbc);
  223. search_box = memnew(LineEdit);
  224. search_box->connect("text_changed", callable_mp(this, &EditorQuickOpen::_text_changed));
  225. search_box->connect("gui_input", callable_mp(this, &EditorQuickOpen::_sbox_input));
  226. vbc->add_margin_child(TTR("Search:"), search_box);
  227. register_text_enter(search_box);
  228. search_options = memnew(Tree);
  229. search_options->connect("item_activated", callable_mp(this, &EditorQuickOpen::_confirmed));
  230. search_options->create_item();
  231. search_options->set_hide_root(true);
  232. search_options->set_hide_folding(true);
  233. search_options->add_theme_constant_override("draw_guides", 1);
  234. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  235. set_ok_button_text(TTR("Open"));
  236. set_hide_on_ok(false);
  237. }