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