quick_open.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "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 InputEvent &p_ie) {
  66. if (p_ie.type == InputEvent::KEY) {
  67. switch (p_ie.key.scancode) {
  68. case KEY_UP:
  69. case KEY_DOWN:
  70. case KEY_PAGEUP:
  71. case KEY_PAGEDOWN: {
  72. search_options->call("_input_event", p_ie);
  73. search_box->accept_event();
  74. TreeItem *root = search_options->get_root();
  75. if (!root->get_children())
  76. break;
  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. if (search == path) {
  90. return 1.2f;
  91. }
  92. if (path.findn(search) != -1) {
  93. return 1.1f;
  94. }
  95. return path.to_lower().similarity(search.to_lower());
  96. }
  97. void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture> > > &list) {
  98. if (!add_directories) {
  99. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  100. _parse_fs(efsd->get_subdir(i), list);
  101. }
  102. }
  103. String search_text = search_box->get_text();
  104. if (add_directories) {
  105. String path = efsd->get_path();
  106. if (!path.ends_with("/"))
  107. path += "/";
  108. if (path != "res://") {
  109. path = path.substr(6, path.length());
  110. if (search_text.is_subsequence_ofi(path)) {
  111. Pair<String, Ref<Texture> > pair;
  112. pair.first = path;
  113. pair.second = get_icon("folder", "FileDialog");
  114. if (search_text != String() && list.size() > 0) {
  115. float this_sim = _path_cmp(search_text, path);
  116. float other_sim = _path_cmp(list[0].first, path);
  117. int pos = 1;
  118. while (pos < list.size() && this_sim <= other_sim) {
  119. other_sim = _path_cmp(list[pos++].first, path);
  120. }
  121. pos = this_sim >= other_sim ? pos - 1 : pos;
  122. list.insert(pos, pair);
  123. } else {
  124. list.push_back(pair);
  125. }
  126. }
  127. }
  128. }
  129. for (int i = 0; i < efsd->get_file_count(); i++) {
  130. String file = efsd->get_file_path(i);
  131. file = file.substr(6, file.length());
  132. if (ObjectTypeDB::is_type(efsd->get_file_type(i), base_type) && (search_text.is_subsequence_ofi(file))) {
  133. Pair<String, Ref<Texture> > pair;
  134. pair.first = file;
  135. pair.second = get_icon((has_icon(efsd->get_file_type(i), ei) ? efsd->get_file_type(i) : ot), ei);
  136. if (search_text != String() && list.size() > 0) {
  137. float this_sim = _path_cmp(search_text, file);
  138. float other_sim = _path_cmp(list[0].first, file);
  139. int pos = 1;
  140. while (pos < list.size() && this_sim <= other_sim) {
  141. other_sim = _path_cmp(list[pos++].first, file);
  142. }
  143. pos = this_sim >= other_sim ? pos - 1 : pos;
  144. list.insert(pos, pair);
  145. } else {
  146. list.push_back(pair);
  147. }
  148. }
  149. }
  150. if (add_directories) {
  151. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  152. _parse_fs(efsd->get_subdir(i), list);
  153. }
  154. }
  155. }
  156. void EditorQuickOpen::_update_search() {
  157. search_options->clear();
  158. TreeItem *root = search_options->create_item();
  159. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
  160. Vector<Pair<String, Ref<Texture> > > list;
  161. _parse_fs(efsd, list);
  162. for (int i = 0; i < list.size(); i++) {
  163. TreeItem *ti = search_options->create_item(root);
  164. ti->set_text(0, list[i].first);
  165. ti->set_icon(0, list[i].second);
  166. }
  167. if (root->get_children()) {
  168. TreeItem *ti = root->get_children();
  169. ti->select(0);
  170. ti->set_as_cursor(0);
  171. }
  172. get_ok()->set_disabled(root->get_children() == NULL);
  173. }
  174. void EditorQuickOpen::_confirmed() {
  175. TreeItem *ti = search_options->get_selected();
  176. if (!ti)
  177. return;
  178. emit_signal("quick_open");
  179. hide();
  180. }
  181. void EditorQuickOpen::_notification(int p_what) {
  182. if (p_what == NOTIFICATION_ENTER_TREE) {
  183. connect("confirmed", this, "_confirmed");
  184. }
  185. }
  186. StringName EditorQuickOpen::get_base_type() const {
  187. return base_type;
  188. }
  189. void EditorQuickOpen::_bind_methods() {
  190. ObjectTypeDB::bind_method(_MD("_text_changed"), &EditorQuickOpen::_text_changed);
  191. ObjectTypeDB::bind_method(_MD("_confirmed"), &EditorQuickOpen::_confirmed);
  192. ObjectTypeDB::bind_method(_MD("_sbox_input"), &EditorQuickOpen::_sbox_input);
  193. ADD_SIGNAL(MethodInfo("quick_open"));
  194. }
  195. EditorQuickOpen::EditorQuickOpen() {
  196. VBoxContainer *vbc = memnew(VBoxContainer);
  197. add_child(vbc);
  198. set_child_rect(vbc);
  199. search_box = memnew(LineEdit);
  200. vbc->add_margin_child(TTR("Search:"), search_box);
  201. search_box->connect("text_changed", this, "_text_changed");
  202. search_box->connect("input_event", this, "_sbox_input");
  203. search_options = memnew(Tree);
  204. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  205. get_ok()->set_text(TTR("Open"));
  206. get_ok()->set_disabled(true);
  207. register_text_enter(search_box);
  208. set_hide_on_ok(false);
  209. search_options->connect("item_activated", this, "_confirmed");
  210. search_options->set_hide_root(true);
  211. ei = "EditorIcons";
  212. ot = "Object";
  213. add_directories = false;
  214. }