directory_create_dialog.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**************************************************************************/
  2. /* directory_create_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 "directory_create_dialog.h"
  31. #include "core/io/dir_access.h"
  32. #include "editor/editor_file_system.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/gui/editor_validation_panel.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/line_edit.h"
  39. String DirectoryCreateDialog::_sanitize_input(const String &p_path) const {
  40. String path = p_path.strip_edges();
  41. if (mode == MODE_DIRECTORY) {
  42. path = path.trim_suffix("/");
  43. }
  44. return path;
  45. }
  46. String DirectoryCreateDialog::_validate_path(const String &p_path) const {
  47. if (p_path.is_empty()) {
  48. return TTR("Name cannot be empty.");
  49. }
  50. if (mode == MODE_FILE && p_path.ends_with("/")) {
  51. return TTR("File name can't end with /.");
  52. }
  53. const PackedStringArray splits = p_path.split("/");
  54. for (int i = 0; i < splits.size(); i++) {
  55. const String &part = splits[i];
  56. bool is_file = mode == MODE_FILE && i == splits.size() - 1;
  57. if (part.is_empty()) {
  58. if (is_file) {
  59. return TTR("File name cannot be empty.");
  60. } else {
  61. return TTR("Folder name cannot be empty.");
  62. }
  63. }
  64. if (part.contains("\\") || part.contains(":") || part.contains("*") ||
  65. part.contains("|") || part.contains(">") || part.ends_with(".") || part.ends_with(" ")) {
  66. if (is_file) {
  67. return TTR("File name contains invalid characters.");
  68. } else {
  69. return TTR("Folder name contains invalid characters.");
  70. }
  71. }
  72. if (part[0] == '.') {
  73. if (is_file) {
  74. return TTR("File name begins with a dot.");
  75. } else {
  76. return TTR("Folder name begins with a dot.");
  77. }
  78. }
  79. }
  80. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  81. da->change_dir(base_dir);
  82. if (da->file_exists(p_path)) {
  83. return TTR("File with that name already exists.");
  84. }
  85. if (da->dir_exists(p_path)) {
  86. return TTR("Folder with that name already exists.");
  87. }
  88. return String();
  89. }
  90. void DirectoryCreateDialog::_on_dir_path_changed() {
  91. const String path = _sanitize_input(dir_path->get_text());
  92. const String error = _validate_path(path);
  93. if (error.is_empty()) {
  94. if (path.contains("/")) {
  95. if (mode == MODE_DIRECTORY) {
  96. validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Using slashes in folder names will create subfolders recursively."), EditorValidationPanel::MSG_OK);
  97. } else {
  98. validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Using slashes in path will create the file in subfolder, creating new subfolders if necessary."), EditorValidationPanel::MSG_OK);
  99. }
  100. } else if (mode == MODE_FILE) {
  101. validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("File name is valid."), EditorValidationPanel::MSG_OK);
  102. }
  103. } else {
  104. validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, error, EditorValidationPanel::MSG_ERROR);
  105. }
  106. }
  107. void DirectoryCreateDialog::ok_pressed() {
  108. const String path = _sanitize_input(dir_path->get_text());
  109. // The OK button should be disabled if the path is invalid, but just in case.
  110. const String error = _validate_path(path);
  111. ERR_FAIL_COND_MSG(!error.is_empty(), error);
  112. accept_callback.call(base_dir.path_join(path));
  113. hide();
  114. }
  115. void DirectoryCreateDialog::_post_popup() {
  116. ConfirmationDialog::_post_popup();
  117. dir_path->grab_focus();
  118. }
  119. void DirectoryCreateDialog::config(const String &p_base_dir, const Callable &p_accept_callback, int p_mode, const String &p_title, const String &p_default_name) {
  120. set_title(p_title);
  121. base_dir = p_base_dir;
  122. base_path_label->set_text(vformat(TTR("Base path: %s"), base_dir));
  123. accept_callback = p_accept_callback;
  124. mode = p_mode;
  125. dir_path->set_text(p_default_name);
  126. validation_panel->update();
  127. if (p_mode == MODE_FILE) {
  128. int extension_pos = p_default_name.rfind_char('.');
  129. if (extension_pos > -1) {
  130. dir_path->select(0, extension_pos);
  131. return;
  132. }
  133. }
  134. dir_path->select_all();
  135. }
  136. DirectoryCreateDialog::DirectoryCreateDialog() {
  137. set_min_size(Size2i(480, 0) * EDSCALE);
  138. VBoxContainer *vb = memnew(VBoxContainer);
  139. add_child(vb);
  140. base_path_label = memnew(Label);
  141. base_path_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS);
  142. vb->add_child(base_path_label);
  143. Label *name_label = memnew(Label);
  144. name_label->set_text(TTR("Name:"));
  145. name_label->set_theme_type_variation("HeaderSmall");
  146. vb->add_child(name_label);
  147. dir_path = memnew(LineEdit);
  148. vb->add_child(dir_path);
  149. register_text_enter(dir_path);
  150. Control *spacing = memnew(Control);
  151. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  152. vb->add_child(spacing);
  153. validation_panel = memnew(EditorValidationPanel);
  154. vb->add_child(validation_panel);
  155. validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Folder name is valid."));
  156. validation_panel->set_update_callback(callable_mp(this, &DirectoryCreateDialog::_on_dir_path_changed));
  157. validation_panel->set_accept_button(get_ok_button());
  158. dir_path->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  159. }