editor_quick_open.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. void EditorQuickOpen::popup_dialog(const StringName &p_base, bool p_enable_multi, bool p_dontclear) {
  33. base_type = p_base;
  34. search_options->set_select_mode(p_enable_multi ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  35. popup_centered_ratio(0.4);
  36. if (p_dontclear) {
  37. search_box->select_all();
  38. } else {
  39. search_box->clear();
  40. }
  41. search_box->grab_focus();
  42. _update_search();
  43. }
  44. String EditorQuickOpen::get_selected() const {
  45. TreeItem *ti = search_options->get_selected();
  46. if (!ti) {
  47. return String();
  48. }
  49. return "res://" + ti->get_text(0);
  50. }
  51. Vector<String> EditorQuickOpen::get_selected_files() const {
  52. Vector<String> files;
  53. TreeItem *item = search_options->get_next_selected(search_options->get_root());
  54. while (item) {
  55. files.push_back("res://" + item->get_text(0));
  56. item = search_options->get_next_selected(item);
  57. }
  58. return files;
  59. }
  60. void EditorQuickOpen::_text_changed(const String &p_newtext) {
  61. _update_search();
  62. }
  63. void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
  64. Ref<InputEventKey> k = p_ie;
  65. if (k.is_valid()) {
  66. switch (k->get_scancode()) {
  67. case KEY_UP:
  68. case KEY_DOWN:
  69. case KEY_PAGEUP:
  70. case KEY_PAGEDOWN: {
  71. search_options->call("_gui_input", k);
  72. search_box->accept_event();
  73. TreeItem *root = search_options->get_root();
  74. if (!root->get_children()) {
  75. break;
  76. }
  77. TreeItem *current = search_options->get_selected();
  78. TreeItem *item = search_options->get_next_selected(root);
  79. while (item) {
  80. item->deselect(0);
  81. item = search_options->get_next_selected(item);
  82. }
  83. current->select(0);
  84. } break;
  85. }
  86. }
  87. }
  88. float EditorQuickOpen::_path_cmp(String search, String path) const {
  89. // Exact match.
  90. if (search == path) {
  91. return 1.2f;
  92. }
  93. // Substring match, with positive bias for matches close to the end of the path.
  94. int pos = path.rfindn(search);
  95. if (pos != -1) {
  96. return 1.1f + 0.09 / (path.length() - pos + 1);
  97. }
  98. // Similarity.
  99. return path.to_lower().similarity(search.to_lower());
  100. }
  101. void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture>>> &list) {
  102. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  103. _parse_fs(efsd->get_subdir(i), list);
  104. }
  105. String search_text = search_box->get_text();
  106. Vector<String> base_types = String(base_type).split(String(","));
  107. for (int i = 0; i < efsd->get_file_count(); i++) {
  108. String file = efsd->get_file_path(i);
  109. file = file.substr(6, file.length());
  110. StringName file_type = efsd->get_file_type(i);
  111. // Iterate all possible base types.
  112. for (int j = 0; j < base_types.size(); j++) {
  113. if (ClassDB::is_parent_class(file_type, base_types[j]) && search_text.is_subsequence_ofi(file)) {
  114. Pair<String, Ref<Texture>> pair;
  115. pair.first = file;
  116. StringName icon_name = search_options->has_icon(file_type, ei) ? file_type : ot;
  117. pair.second = search_options->get_icon(icon_name, ei);
  118. list.push_back(pair);
  119. // Stop testing base types as soon as we got a match.
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. Vector<Pair<String, Ref<Texture>>> EditorQuickOpen::_sort_fs(Vector<Pair<String, Ref<Texture>>> &list) {
  126. String search_text = search_box->get_text();
  127. Vector<Pair<String, Ref<Texture>>> sorted_list;
  128. if (search_text == String() || list.size() == 0) {
  129. return list;
  130. }
  131. Vector<float> scores;
  132. scores.resize(list.size());
  133. for (int i = 0; i < list.size(); i++) {
  134. scores.write[i] = _path_cmp(search_text, list[i].first);
  135. }
  136. while (list.size() > 0) {
  137. float best_score = 0.0f;
  138. int best_idx = 0;
  139. for (int i = 0; i < list.size(); i++) {
  140. float current_score = scores[i];
  141. if (current_score > best_score) {
  142. best_score = current_score;
  143. best_idx = i;
  144. }
  145. }
  146. sorted_list.push_back(list[best_idx]);
  147. list.remove(best_idx);
  148. scores.remove(best_idx);
  149. }
  150. return sorted_list;
  151. }
  152. void EditorQuickOpen::_update_search() {
  153. search_options->clear();
  154. TreeItem *root = search_options->create_item();
  155. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
  156. Vector<Pair<String, Ref<Texture>>> list;
  157. _parse_fs(efsd, list);
  158. list = _sort_fs(list);
  159. for (int i = 0; i < list.size(); i++) {
  160. TreeItem *ti = search_options->create_item(root);
  161. ti->set_text(0, list[i].first);
  162. ti->set_icon(0, list[i].second);
  163. }
  164. if (root->get_children()) {
  165. TreeItem *ti = root->get_children();
  166. ti->select(0);
  167. ti->set_as_cursor(0);
  168. }
  169. get_ok()->set_disabled(root->get_children() == nullptr);
  170. }
  171. void EditorQuickOpen::_confirmed() {
  172. TreeItem *ti = search_options->get_selected();
  173. if (!ti) {
  174. return;
  175. }
  176. emit_signal("quick_open");
  177. hide();
  178. }
  179. void EditorQuickOpen::_notification(int p_what) {
  180. switch (p_what) {
  181. case NOTIFICATION_ENTER_TREE: {
  182. connect("confirmed", this, "_confirmed");
  183. search_box->set_clear_button_enabled(true);
  184. FALLTHROUGH;
  185. }
  186. case NOTIFICATION_THEME_CHANGED: {
  187. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  188. } break;
  189. case NOTIFICATION_EXIT_TREE: {
  190. disconnect("confirmed", this, "_confirmed");
  191. } break;
  192. }
  193. }
  194. StringName EditorQuickOpen::get_base_type() const {
  195. return base_type;
  196. }
  197. void EditorQuickOpen::_bind_methods() {
  198. ClassDB::bind_method(D_METHOD("_text_changed"), &EditorQuickOpen::_text_changed);
  199. ClassDB::bind_method(D_METHOD("_confirmed"), &EditorQuickOpen::_confirmed);
  200. ClassDB::bind_method(D_METHOD("_sbox_input"), &EditorQuickOpen::_sbox_input);
  201. ADD_SIGNAL(MethodInfo("quick_open"));
  202. }
  203. EditorQuickOpen::EditorQuickOpen() {
  204. VBoxContainer *vbc = memnew(VBoxContainer);
  205. add_child(vbc);
  206. search_box = memnew(LineEdit);
  207. search_box->connect("text_changed", this, "_text_changed");
  208. search_box->connect("gui_input", this, "_sbox_input");
  209. vbc->add_margin_child(TTR("Search:"), search_box);
  210. search_options = memnew(Tree);
  211. search_options->connect("item_activated", this, "_confirmed");
  212. search_options->set_hide_root(true);
  213. search_options->set_hide_folding(true);
  214. search_options->add_constant_override("draw_guides", 1);
  215. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  216. get_ok()->set_text(TTR("Open"));
  217. get_ok()->set_disabled(true);
  218. register_text_enter(search_box);
  219. set_hide_on_ok(false);
  220. ei = "EditorIcons";
  221. ot = "Object";
  222. }