editor_quick_open.cpp 9.1 KB

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