quick_open.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* quick_open.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 "quick_open.h"
  31. #include "core/os/keyboard.h"
  32. void EditorQuickOpen::popup_dialog(const StringName &p_base, bool p_enable_multi, bool p_dontclear) {
  33. base_type = p_base;
  34. allow_multi_select = p_enable_multi;
  35. search_options->set_select_mode(allow_multi_select ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  36. popup_centered_clamped(Size2i(600, 440), 0.8f);
  37. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
  38. _build_search_cache(efsd);
  39. if (p_dontclear) {
  40. search_box->select_all();
  41. _update_search();
  42. } else {
  43. search_box->clear(); // This will emit text_changed.
  44. }
  45. search_box->grab_focus();
  46. }
  47. void EditorQuickOpen::_build_search_cache(EditorFileSystemDirectory *p_efsd) {
  48. for (int i = 0; i < p_efsd->get_subdir_count(); i++) {
  49. _build_search_cache(p_efsd->get_subdir(i));
  50. }
  51. for (int i = 0; i < p_efsd->get_file_count(); i++) {
  52. String file_type = p_efsd->get_file_type(i);
  53. if (ClassDB::is_parent_class(file_type, base_type)) {
  54. String file = p_efsd->get_file_path(i);
  55. files.push_back(file.substr(6, file.length()));
  56. // Store refs to used icons.
  57. String ext = file.get_extension();
  58. if (!icons.has(ext)) {
  59. icons.insert(ext, get_theme_icon((has_theme_icon(file_type, "EditorIcons") ? file_type : "Object"), "EditorIcons"));
  60. }
  61. }
  62. }
  63. }
  64. void EditorQuickOpen::_update_search() {
  65. const String search_text = search_box->get_text();
  66. const bool empty_search = search_text == "";
  67. // Filter possible candidates.
  68. Vector<Entry> entries;
  69. for (int i = 0; i < files.size(); i++) {
  70. if (empty_search || search_text.is_subsequence_ofi(files[i])) {
  71. Entry r;
  72. r.path = files[i];
  73. r.score = empty_search ? 0 : _score_path(search_text, files[i].to_lower());
  74. entries.push_back(r);
  75. }
  76. }
  77. // Display results
  78. TreeItem *root = search_options->get_root();
  79. root->clear_children();
  80. if (entries.size() > 0) {
  81. if (!empty_search) {
  82. SortArray<Entry, EntryComparator> sorter;
  83. sorter.sort(entries.ptrw(), entries.size());
  84. }
  85. const int entry_limit = MIN(entries.size(), 300);
  86. for (int i = 0; i < entry_limit; i++) {
  87. TreeItem *ti = search_options->create_item(root);
  88. ti->set_text(0, entries[i].path);
  89. ti->set_icon(0, *icons.lookup_ptr(entries[i].path.get_extension()));
  90. }
  91. TreeItem *to_select = root->get_children();
  92. to_select->select(0);
  93. to_select->set_as_cursor(0);
  94. search_options->scroll_to_item(to_select);
  95. get_ok_button()->set_disabled(false);
  96. } else {
  97. search_options->deselect_all();
  98. get_ok_button()->set_disabled(true);
  99. }
  100. }
  101. float EditorQuickOpen::_score_path(const String &p_search, const String &p_path) {
  102. float score = 0.9f + .1f * (p_search.length() / (float)p_path.length());
  103. // Positive bias for matches close to the beginning of the file name.
  104. String file = p_path.get_file();
  105. int pos = file.findn(p_search);
  106. if (pos != -1) {
  107. return score * (1.0f - 0.1f * (float(pos) / file.length()));
  108. }
  109. // Positive bias for matches close to the end of the path.
  110. pos = p_path.rfindn(p_search);
  111. if (pos != -1) {
  112. return score * (0.8f - 0.1f * (float(p_path.length() - pos) / p_path.length()));
  113. }
  114. // Remaining results belong to the same class of results.
  115. return score * 0.69f;
  116. }
  117. void EditorQuickOpen::_confirmed() {
  118. if (!search_options->get_selected()) {
  119. return;
  120. }
  121. _cleanup();
  122. emit_signal("quick_open");
  123. hide();
  124. }
  125. void EditorQuickOpen::cancel_pressed() {
  126. _cleanup();
  127. }
  128. void EditorQuickOpen::_cleanup() {
  129. files.clear();
  130. icons.clear();
  131. }
  132. void EditorQuickOpen::_text_changed(const String &p_newtext) {
  133. _update_search();
  134. }
  135. void EditorQuickOpen::_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->call("_gui_input", k);
  144. search_box->accept_event();
  145. if (allow_multi_select) {
  146. TreeItem *root = search_options->get_root();
  147. if (!root->get_children()) {
  148. break;
  149. }
  150. TreeItem *current = search_options->get_selected();
  151. TreeItem *item = search_options->get_next_selected(root);
  152. while (item) {
  153. item->deselect(0);
  154. item = search_options->get_next_selected(item);
  155. }
  156. current->select(0);
  157. current->set_as_cursor(0);
  158. }
  159. } break;
  160. }
  161. }
  162. }
  163. String EditorQuickOpen::get_selected() const {
  164. TreeItem *ti = search_options->get_selected();
  165. if (!ti) {
  166. return String();
  167. }
  168. return "res://" + ti->get_text(0);
  169. }
  170. Vector<String> EditorQuickOpen::get_selected_files() const {
  171. Vector<String> selected_files;
  172. TreeItem *item = search_options->get_next_selected(search_options->get_root());
  173. while (item) {
  174. selected_files.push_back("res://" + item->get_text(0));
  175. item = search_options->get_next_selected(item);
  176. }
  177. return selected_files;
  178. }
  179. StringName EditorQuickOpen::get_base_type() const {
  180. return base_type;
  181. }
  182. void EditorQuickOpen::_notification(int p_what) {
  183. switch (p_what) {
  184. case NOTIFICATION_ENTER_TREE: {
  185. connect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
  186. search_box->set_clear_button_enabled(true);
  187. } break;
  188. case NOTIFICATION_EXIT_TREE: {
  189. disconnect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
  190. } break;
  191. }
  192. }
  193. void EditorQuickOpen::_theme_changed() {
  194. search_box->set_right_icon(search_options->get_theme_icon("Search", "EditorIcons"));
  195. }
  196. void EditorQuickOpen::_bind_methods() {
  197. ADD_SIGNAL(MethodInfo("quick_open"));
  198. }
  199. EditorQuickOpen::EditorQuickOpen() {
  200. allow_multi_select = false;
  201. VBoxContainer *vbc = memnew(VBoxContainer);
  202. vbc->connect("theme_changed", callable_mp(this, &EditorQuickOpen::_theme_changed));
  203. add_child(vbc);
  204. search_box = memnew(LineEdit);
  205. search_box->connect("text_changed", callable_mp(this, &EditorQuickOpen::_text_changed));
  206. search_box->connect("gui_input", callable_mp(this, &EditorQuickOpen::_sbox_input));
  207. vbc->add_margin_child(TTR("Search:"), search_box);
  208. register_text_enter(search_box);
  209. search_options = memnew(Tree);
  210. search_options->connect("item_activated", callable_mp(this, &EditorQuickOpen::_confirmed));
  211. search_options->create_item();
  212. search_options->set_hide_root(true);
  213. search_options->set_hide_folding(true);
  214. search_options->add_theme_constant_override("draw_guides", 1);
  215. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  216. get_ok_button()->set_text(TTR("Open"));
  217. set_hide_on_ok(false);
  218. }