quick_open.cpp 8.0 KB

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