editor_dir_dialog.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**************************************************************************/
  2. /* editor_dir_dialog.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_dir_dialog.h"
  31. #include "editor/directory_create_dialog.h"
  32. #include "editor/editor_file_system.h"
  33. #include "editor/filesystem_dock.h"
  34. #include "editor/themes/editor_theme_manager.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/tree.h"
  37. #include "servers/display_server.h"
  38. void EditorDirDialog::_update_dir(const Color &p_default_folder_color, const Dictionary &p_assigned_folder_colors, const HashMap<String, Color> &p_folder_colors, bool p_is_dark_theme, TreeItem *p_item, EditorFileSystemDirectory *p_dir, const String &p_select_path) {
  39. updating = true;
  40. const String path = p_dir->get_path();
  41. p_item->set_metadata(0, path);
  42. p_item->set_icon(0, tree->get_editor_theme_icon(SNAME("Folder")));
  43. if (!p_item->get_parent()) {
  44. p_item->set_text(0, "res://");
  45. p_item->set_icon_modulate(0, p_default_folder_color);
  46. } else {
  47. if (!opened_paths.has(path) && (p_select_path.is_empty() || !p_select_path.begins_with(path))) {
  48. p_item->set_collapsed(true);
  49. }
  50. p_item->set_text(0, p_dir->get_name());
  51. if (p_assigned_folder_colors.has(path)) {
  52. const Color &folder_color = p_folder_colors[p_assigned_folder_colors[path]];
  53. p_item->set_icon_modulate(0, p_is_dark_theme ? folder_color : folder_color * FileSystemDock::ITEM_COLOR_SCALE);
  54. p_item->set_custom_bg_color(0, Color(folder_color, p_is_dark_theme ? FileSystemDock::ITEM_ALPHA_MIN : FileSystemDock::ITEM_ALPHA_MAX));
  55. } else {
  56. TreeItem *parent_item = p_item->get_parent();
  57. Color parent_bg_color = parent_item->get_custom_bg_color(0);
  58. if (parent_bg_color != Color()) {
  59. p_item->set_custom_bg_color(0, p_assigned_folder_colors.has(parent_item->get_metadata(0)) ? parent_bg_color.darkened(FileSystemDock::ITEM_BG_DARK_SCALE) : parent_bg_color);
  60. p_item->set_icon_modulate(0, parent_item->get_icon_modulate(0));
  61. } else {
  62. p_item->set_icon_modulate(0, p_default_folder_color);
  63. }
  64. }
  65. }
  66. if (path == new_dir_path || !p_item->get_parent()) {
  67. p_item->select(0);
  68. }
  69. updating = false;
  70. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  71. TreeItem *ti = tree->create_item(p_item);
  72. _update_dir(p_default_folder_color, p_assigned_folder_colors, p_folder_colors, p_is_dark_theme, ti, p_dir->get_subdir(i));
  73. }
  74. }
  75. void EditorDirDialog::config(const Vector<String> &p_paths) {
  76. ERR_FAIL_COND(p_paths.is_empty());
  77. if (p_paths.size() == 1) {
  78. String path = p_paths[0];
  79. if (path.ends_with("/")) {
  80. path = path.substr(0, path.length() - 1);
  81. }
  82. // TRANSLATORS: %s is the file name that will be moved or duplicated.
  83. set_title(vformat(TTR("Move/Duplicate: %s"), path.get_file()));
  84. } else {
  85. // TRANSLATORS: %d is the number of files that will be moved or duplicated.
  86. set_title(vformat(TTRN("Move/Duplicate %d Item", "Move/Duplicate %d Items", p_paths.size()), p_paths.size()));
  87. }
  88. }
  89. void EditorDirDialog::reload(const String &p_path) {
  90. if (!is_visible()) {
  91. must_reload = true;
  92. return;
  93. }
  94. tree->clear();
  95. TreeItem *root = tree->create_item();
  96. _update_dir(tree->get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog")), FileSystemDock::get_singleton()->get_assigned_folder_colors(), FileSystemDock::get_singleton()->get_folder_colors(), EditorThemeManager::is_dark_theme(), root, EditorFileSystem::get_singleton()->get_filesystem(), p_path);
  97. _item_collapsed(root);
  98. new_dir_path.clear();
  99. must_reload = false;
  100. }
  101. void EditorDirDialog::_notification(int p_what) {
  102. switch (p_what) {
  103. case NOTIFICATION_ENTER_TREE: {
  104. FileSystemDock::get_singleton()->connect("folder_color_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
  105. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload).bind(""));
  106. reload();
  107. } break;
  108. case NOTIFICATION_EXIT_TREE: {
  109. EditorFileSystem::get_singleton()->disconnect("filesystem_changed", callable_mp(this, &EditorDirDialog::reload));
  110. FileSystemDock::get_singleton()->disconnect("folder_color_changed", callable_mp(this, &EditorDirDialog::reload));
  111. } break;
  112. case NOTIFICATION_VISIBILITY_CHANGED: {
  113. if (must_reload && is_visible()) {
  114. reload();
  115. }
  116. } break;
  117. }
  118. }
  119. void EditorDirDialog::_item_collapsed(Object *p_item) {
  120. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  121. if (updating) {
  122. return;
  123. }
  124. if (item->is_collapsed()) {
  125. opened_paths.erase(item->get_metadata(0));
  126. } else {
  127. opened_paths.insert(item->get_metadata(0));
  128. }
  129. }
  130. void EditorDirDialog::_item_activated() {
  131. TreeItem *ti = tree->get_selected();
  132. ERR_FAIL_NULL(ti);
  133. if (ti->get_child_count() > 0) {
  134. ti->set_collapsed(!ti->is_collapsed());
  135. }
  136. }
  137. void EditorDirDialog::_copy_pressed() {
  138. TreeItem *ti = tree->get_selected();
  139. ERR_FAIL_NULL(ti);
  140. hide();
  141. emit_signal(SNAME("copy_pressed"), ti->get_metadata(0));
  142. }
  143. void EditorDirDialog::ok_pressed() {
  144. TreeItem *ti = tree->get_selected();
  145. ERR_FAIL_NULL(ti);
  146. hide();
  147. emit_signal(SNAME("move_pressed"), ti->get_metadata(0));
  148. }
  149. void EditorDirDialog::_make_dir() {
  150. TreeItem *ti = tree->get_selected();
  151. ERR_FAIL_NULL(ti);
  152. const String &directory = ti->get_metadata(0);
  153. makedialog->config(directory, callable_mp(this, &EditorDirDialog::_make_dir_confirm).bind(directory), DirectoryCreateDialog::MODE_DIRECTORY, "new folder");
  154. makedialog->popup_centered();
  155. }
  156. void EditorDirDialog::_make_dir_confirm(const String &p_path, const String &p_base_dir) {
  157. FileSystemDock::get_singleton()->create_directory(p_path, p_base_dir);
  158. // Multiple level of directories can be created at once.
  159. String base_dir = p_path.get_base_dir();
  160. while (true) {
  161. opened_paths.insert(base_dir + "/");
  162. if (base_dir == "res://") {
  163. break;
  164. }
  165. base_dir = base_dir.get_base_dir();
  166. }
  167. new_dir_path = p_path + "/";
  168. }
  169. void EditorDirDialog::_bind_methods() {
  170. ADD_SIGNAL(MethodInfo("copy_pressed", PropertyInfo(Variant::STRING, "dir")));
  171. ADD_SIGNAL(MethodInfo("move_pressed", PropertyInfo(Variant::STRING, "dir")));
  172. }
  173. EditorDirDialog::EditorDirDialog() {
  174. set_hide_on_ok(false);
  175. VBoxContainer *vb = memnew(VBoxContainer);
  176. add_child(vb);
  177. HBoxContainer *hb = memnew(HBoxContainer);
  178. vb->add_child(hb);
  179. hb->add_child(memnew(Label(TTR("Choose target directory:"))));
  180. hb->add_spacer();
  181. makedir = memnew(Button(TTR("Create Folder")));
  182. hb->add_child(makedir);
  183. makedir->connect(SceneStringName(pressed), callable_mp(this, &EditorDirDialog::_make_dir));
  184. tree = memnew(Tree);
  185. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  186. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  187. vb->add_child(tree);
  188. tree->connect("item_activated", callable_mp(this, &EditorDirDialog::_item_activated));
  189. tree->connect("item_collapsed", callable_mp(this, &EditorDirDialog::_item_collapsed), CONNECT_DEFERRED);
  190. set_ok_button_text(TTR("Move"));
  191. copy = add_button(TTR("Copy"), !DisplayServer::get_singleton()->get_swap_cancel_ok());
  192. copy->connect(SceneStringName(pressed), callable_mp(this, &EditorDirDialog::_copy_pressed));
  193. makedialog = memnew(DirectoryCreateDialog);
  194. add_child(makedialog);
  195. }