quick_open.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*************************************************************************/
  2. /* quick_open.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. }
  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_keycode()) {
  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. current->set_as_cursor(0);
  85. } break;
  86. }
  87. }
  88. }
  89. float EditorQuickOpen::_score_path(String search, String path) const {
  90. // Positive bias for matches close to the _beginning of the file name_.
  91. String file = path.get_file();
  92. int pos = file.findn(search);
  93. if (pos != -1) {
  94. return 1.0f - 0.1f * (float(pos) / file.length());
  95. }
  96. // Positive bias for matches close to the _end of the path_.
  97. String base = path.get_base_dir();
  98. pos = base.rfindn(search);
  99. if (pos != -1) {
  100. return 0.9f - 0.1f * (float(base.length() - pos) / base.length());
  101. }
  102. // Results that contain all characters but not the string.
  103. return path.similarity(search) * 0.8f;
  104. }
  105. void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture2D>>> &list) {
  106. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  107. _parse_fs(efsd->get_subdir(i), list);
  108. }
  109. for (int i = 0; i < efsd->get_file_count(); i++) {
  110. StringName file_type = efsd->get_file_type(i);
  111. if (ClassDB::is_parent_class(file_type, base_type)) {
  112. String file = efsd->get_file_path(i);
  113. file = file.substr(6, file.length());
  114. if (search_box->get_text().is_subsequence_ofi(file)) {
  115. Pair<String, Ref<Texture2D>> pair;
  116. pair.first = file;
  117. pair.second = search_options->get_theme_icon(search_options->has_theme_icon(file_type, ei) ? file_type : ot, ei);
  118. list.push_back(pair);
  119. }
  120. }
  121. }
  122. }
  123. Vector<Pair<String, Ref<Texture2D>>> EditorQuickOpen::_sort_fs(Vector<Pair<String, Ref<Texture2D>>> &list) {
  124. String search_text = search_box->get_text().to_lower();
  125. Vector<Pair<String, Ref<Texture2D>>> sorted_list;
  126. if (search_text == String() || list.size() == 0) {
  127. return list;
  128. }
  129. Vector<float> scores;
  130. scores.resize(list.size());
  131. for (int i = 0; i < list.size(); i++) {
  132. scores.write[i] = _score_path(search_text, list[i].first.to_lower());
  133. }
  134. while (list.size() > 0) {
  135. float best_score = 0.0f;
  136. int best_idx = 0;
  137. for (int i = 0; i < list.size(); i++) {
  138. float current_score = scores[i];
  139. if (current_score > best_score) {
  140. best_score = current_score;
  141. best_idx = i;
  142. }
  143. }
  144. sorted_list.push_back(list[best_idx]);
  145. list.remove(best_idx);
  146. scores.remove(best_idx);
  147. }
  148. return sorted_list;
  149. }
  150. void EditorQuickOpen::_update_search() {
  151. search_options->clear();
  152. TreeItem *root = search_options->create_item();
  153. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
  154. Vector<Pair<String, Ref<Texture2D>>> list;
  155. _parse_fs(efsd, list);
  156. list = _sort_fs(list);
  157. for (int i = 0; i < list.size(); i++) {
  158. TreeItem *ti = search_options->create_item(root);
  159. ti->set_text(0, list[i].first);
  160. ti->set_icon(0, list[i].second);
  161. }
  162. TreeItem *result = root->get_children();
  163. if (result) {
  164. result->select(0);
  165. result->set_as_cursor(0);
  166. }
  167. get_ok()->set_disabled(!result);
  168. }
  169. void EditorQuickOpen::_confirmed() {
  170. if (!search_options->get_selected()) {
  171. return;
  172. }
  173. emit_signal("quick_open");
  174. hide();
  175. }
  176. void EditorQuickOpen::_theme_changed() {
  177. search_box->set_right_icon(search_options->get_theme_icon("Search", ei));
  178. }
  179. void EditorQuickOpen::_notification(int p_what) {
  180. switch (p_what) {
  181. case NOTIFICATION_ENTER_TREE: {
  182. connect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
  183. search_box->set_clear_button_enabled(true);
  184. } break;
  185. case NOTIFICATION_EXIT_TREE: {
  186. disconnect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
  187. } break;
  188. }
  189. }
  190. StringName EditorQuickOpen::get_base_type() const {
  191. return base_type;
  192. }
  193. void EditorQuickOpen::_bind_methods() {
  194. ADD_SIGNAL(MethodInfo("quick_open"));
  195. }
  196. EditorQuickOpen::EditorQuickOpen() {
  197. VBoxContainer *vbc = memnew(VBoxContainer);
  198. vbc->connect("theme_changed", callable_mp(this, &EditorQuickOpen::_theme_changed));
  199. add_child(vbc);
  200. search_box = memnew(LineEdit);
  201. search_box->connect("text_changed", callable_mp(this, &EditorQuickOpen::_text_changed));
  202. search_box->connect("gui_input", callable_mp(this, &EditorQuickOpen::_sbox_input));
  203. vbc->add_margin_child(TTR("Search:"), search_box);
  204. search_options = memnew(Tree);
  205. search_options->connect("item_activated", callable_mp(this, &EditorQuickOpen::_confirmed));
  206. search_options->set_hide_root(true);
  207. search_options->set_hide_folding(true);
  208. search_options->add_theme_constant_override("draw_guides", 1);
  209. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  210. get_ok()->set_text(TTR("Open"));
  211. register_text_enter(search_box);
  212. set_hide_on_ok(false);
  213. ei = "EditorIcons";
  214. ot = "Object";
  215. }