quick_open.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*************************************************************************/
  2. /* quick_open.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/keyboard.h"
  32. void EditorQuickOpen::popup(const StringName &p_base, bool p_enable_multi, bool p_add_dirs, bool p_dontclear) {
  33. add_directories = p_add_dirs;
  34. popup_centered_ratio(0.6);
  35. if (p_dontclear)
  36. search_box->select_all();
  37. else
  38. search_box->clear();
  39. if (p_enable_multi)
  40. search_options->set_select_mode(Tree::SELECT_MULTI);
  41. else
  42. search_options->set_select_mode(Tree::SELECT_SINGLE);
  43. search_box->grab_focus();
  44. base_type = p_base;
  45. _update_search();
  46. }
  47. String EditorQuickOpen::get_selected() const {
  48. TreeItem *ti = search_options->get_selected();
  49. if (!ti)
  50. return String();
  51. return "res://" + ti->get_text(0);
  52. }
  53. Vector<String> EditorQuickOpen::get_selected_files() const {
  54. Vector<String> files;
  55. TreeItem *item = search_options->get_next_selected(search_options->get_root());
  56. while (item) {
  57. files.push_back("res://" + item->get_text(0));
  58. item = search_options->get_next_selected(item);
  59. }
  60. return files;
  61. }
  62. void EditorQuickOpen::_text_changed(const String &p_newtext) {
  63. _update_search();
  64. }
  65. void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
  66. Ref<InputEventKey> k = p_ie;
  67. if (k.is_valid()) {
  68. switch (k->get_scancode()) {
  69. case KEY_UP:
  70. case KEY_DOWN:
  71. case KEY_PAGEUP:
  72. case KEY_PAGEDOWN: {
  73. search_options->call("_gui_input", k);
  74. search_box->accept_event();
  75. TreeItem *root = search_options->get_root();
  76. if (!root->get_children())
  77. break;
  78. TreeItem *current = search_options->get_selected();
  79. TreeItem *item = search_options->get_next_selected(root);
  80. while (item) {
  81. item->deselect(0);
  82. item = search_options->get_next_selected(item);
  83. }
  84. current->select(0);
  85. } break;
  86. }
  87. }
  88. }
  89. float EditorQuickOpen::_path_cmp(String search, String path) const {
  90. if (search == path) {
  91. return 1.2f;
  92. }
  93. if (path.findn(search) != -1) {
  94. return 1.1f;
  95. }
  96. return path.to_lower().similarity(search.to_lower());
  97. }
  98. void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture> > > &list) {
  99. if (!add_directories) {
  100. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  101. _parse_fs(efsd->get_subdir(i), list);
  102. }
  103. }
  104. String search_text = search_box->get_text();
  105. if (add_directories) {
  106. String path = efsd->get_path();
  107. if (!path.ends_with("/"))
  108. path += "/";
  109. if (path != "res://") {
  110. path = path.substr(6, path.length());
  111. if (search_text.is_subsequence_ofi(path)) {
  112. Pair<String, Ref<Texture> > pair;
  113. pair.first = path;
  114. pair.second = get_icon("folder", "FileDialog");
  115. if (search_text != String() && list.size() > 0) {
  116. float this_sim = _path_cmp(search_text, path);
  117. float other_sim = _path_cmp(list[0].first, path);
  118. int pos = 1;
  119. while (pos < list.size() && this_sim <= other_sim) {
  120. other_sim = _path_cmp(list[pos++].first, path);
  121. }
  122. pos = this_sim >= other_sim ? pos - 1 : pos;
  123. list.insert(pos, pair);
  124. } else {
  125. list.push_back(pair);
  126. }
  127. }
  128. }
  129. }
  130. for (int i = 0; i < efsd->get_file_count(); i++) {
  131. String file = efsd->get_file_path(i);
  132. file = file.substr(6, file.length());
  133. if (ClassDB::is_parent_class(efsd->get_file_type(i), base_type) && (search_text.is_subsequence_ofi(file))) {
  134. Pair<String, Ref<Texture> > pair;
  135. pair.first = file;
  136. pair.second = get_icon((has_icon(efsd->get_file_type(i), ei) ? efsd->get_file_type(i) : ot), ei);
  137. list.push_back(pair);
  138. }
  139. }
  140. if (add_directories) {
  141. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  142. _parse_fs(efsd->get_subdir(i), list);
  143. }
  144. }
  145. }
  146. Vector<Pair<String, Ref<Texture> > > EditorQuickOpen::_sort_fs(Vector<Pair<String, Ref<Texture> > > &list) {
  147. String search_text = search_box->get_text();
  148. Vector<Pair<String, Ref<Texture> > > sorted_list;
  149. if (search_text == String() || list.size() == 0)
  150. return list;
  151. Vector<float> scores;
  152. scores.resize(list.size());
  153. for (int i = 0; i < list.size(); i++)
  154. scores[i] = _path_cmp(search_text, list[i].first);
  155. while (list.size() > 0) {
  156. float best_score = 0.0f;
  157. int best_idx = 0;
  158. for (int i = 0; i < list.size(); i++) {
  159. float current_score = scores[i];
  160. if (current_score > best_score) {
  161. best_score = current_score;
  162. best_idx = i;
  163. }
  164. }
  165. sorted_list.push_back(list[best_idx]);
  166. list.remove(best_idx);
  167. scores.remove(best_idx);
  168. }
  169. return sorted_list;
  170. }
  171. void EditorQuickOpen::_update_search() {
  172. search_options->clear();
  173. TreeItem *root = search_options->create_item();
  174. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
  175. Vector<Pair<String, Ref<Texture> > > list;
  176. _parse_fs(efsd, list);
  177. list = _sort_fs(list);
  178. for (int i = 0; i < list.size(); i++) {
  179. TreeItem *ti = search_options->create_item(root);
  180. ti->set_text(0, list[i].first);
  181. ti->set_icon(0, list[i].second);
  182. }
  183. if (root->get_children()) {
  184. TreeItem *ti = root->get_children();
  185. ti->select(0);
  186. ti->set_as_cursor(0);
  187. }
  188. get_ok()->set_disabled(root->get_children() == NULL);
  189. }
  190. void EditorQuickOpen::_confirmed() {
  191. TreeItem *ti = search_options->get_selected();
  192. if (!ti)
  193. return;
  194. emit_signal("quick_open");
  195. hide();
  196. }
  197. void EditorQuickOpen::_notification(int p_what) {
  198. if (p_what == NOTIFICATION_ENTER_TREE) {
  199. connect("confirmed", this, "_confirmed");
  200. }
  201. }
  202. StringName EditorQuickOpen::get_base_type() const {
  203. return base_type;
  204. }
  205. void EditorQuickOpen::_bind_methods() {
  206. ClassDB::bind_method(D_METHOD("_text_changed"), &EditorQuickOpen::_text_changed);
  207. ClassDB::bind_method(D_METHOD("_confirmed"), &EditorQuickOpen::_confirmed);
  208. ClassDB::bind_method(D_METHOD("_sbox_input"), &EditorQuickOpen::_sbox_input);
  209. ADD_SIGNAL(MethodInfo("quick_open"));
  210. }
  211. EditorQuickOpen::EditorQuickOpen() {
  212. VBoxContainer *vbc = memnew(VBoxContainer);
  213. add_child(vbc);
  214. //set_child_rect(vbc);
  215. search_box = memnew(LineEdit);
  216. vbc->add_margin_child(TTR("Search:"), search_box);
  217. search_box->connect("text_changed", this, "_text_changed");
  218. search_box->connect("gui_input", this, "_sbox_input");
  219. search_options = memnew(Tree);
  220. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  221. get_ok()->set_text(TTR("Open"));
  222. get_ok()->set_disabled(true);
  223. register_text_enter(search_box);
  224. set_hide_on_ok(false);
  225. search_options->connect("item_activated", this, "_confirmed");
  226. search_options->set_hide_root(true);
  227. ei = "EditorIcons";
  228. ot = "Object";
  229. add_directories = false;
  230. }