Path: blob/master/editor/gui/directory_create_dialog.cpp
9896 views
/**************************************************************************/1/* directory_create_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "directory_create_dialog.h"3132#include "core/io/dir_access.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"3940String DirectoryCreateDialog::_sanitize_input(const String &p_path) const {41String path = p_path.strip_edges();42if (mode == MODE_DIRECTORY) {43path = path.trim_suffix("/");44}45return path;46}4748String DirectoryCreateDialog::_validate_path(const String &p_path) const {49if (p_path.is_empty()) {50return TTR("Name cannot be empty.");51}52if (mode == MODE_FILE && p_path.ends_with("/")) {53return TTR("File name can't end with /.");54}5556const PackedStringArray splits = p_path.split("/");57for (int i = 0; i < splits.size(); i++) {58const String &part = splits[i];59bool is_file = mode == MODE_FILE && i == splits.size() - 1;6061if (part.is_empty()) {62if (is_file) {63return TTR("File name cannot be empty.");64} else {65return TTR("Folder name cannot be empty.");66}67}68if (part.contains_char('\\') || part.contains_char(':') || part.contains_char('*') ||69part.contains_char('|') || part.contains_char('>') || part.ends_with(".") || part.ends_with(" ")) {70if (is_file) {71return TTR("File name contains invalid characters.");72} else {73return TTR("Folder name contains invalid characters.");74}75}76if (part[0] == '.') {77if (is_file) {78return TTR("File name begins with a dot.");79} else {80return TTR("Folder name begins with a dot.");81}82}83}8485Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);86da->change_dir(base_dir);87if (da->file_exists(p_path)) {88return TTR("File with that name already exists.");89}90if (da->dir_exists(p_path)) {91return TTR("Folder with that name already exists.");92}9394return String();95}9697void DirectoryCreateDialog::_on_dir_path_changed() {98const String path = _sanitize_input(dir_path->get_text());99const String error = _validate_path(path);100101if (error.is_empty()) {102if (path.contains_char('/')) {103if (mode == MODE_DIRECTORY) {104validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Using slashes in folder names will create subfolders recursively."), EditorValidationPanel::MSG_OK);105} else {106validation_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);107}108} else if (mode == MODE_FILE) {109validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("File name is valid."), EditorValidationPanel::MSG_OK);110}111} else {112validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, error, EditorValidationPanel::MSG_ERROR);113}114}115116void DirectoryCreateDialog::ok_pressed() {117const String path = _sanitize_input(dir_path->get_text());118119// The OK button should be disabled if the path is invalid, but just in case.120const String error = _validate_path(path);121ERR_FAIL_COND_MSG(!error.is_empty(), error);122123accept_callback.call(base_dir.path_join(path));124hide();125}126127void DirectoryCreateDialog::_post_popup() {128ConfirmationDialog::_post_popup();129dir_path->grab_focus();130}131132void DirectoryCreateDialog::config(const String &p_base_dir, const Callable &p_accept_callback, int p_mode, const String &p_title, const String &p_default_name) {133set_title(p_title);134base_dir = p_base_dir;135base_path_label->set_text(vformat(TTR("Base path: %s"), base_dir));136accept_callback = p_accept_callback;137mode = p_mode;138139dir_path->set_text(p_default_name);140validation_panel->update();141142if (p_mode == MODE_FILE) {143int extension_pos = p_default_name.rfind_char('.');144if (extension_pos > -1) {145dir_path->select(0, extension_pos);146return;147}148}149dir_path->select_all();150}151152DirectoryCreateDialog::DirectoryCreateDialog() {153set_min_size(Size2i(480, 0) * EDSCALE);154155VBoxContainer *vb = memnew(VBoxContainer);156add_child(vb);157158base_path_label = memnew(Label);159base_path_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);160base_path_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS);161vb->add_child(base_path_label);162163Label *name_label = memnew(Label);164name_label->set_text(TTR("Name:"));165name_label->set_theme_type_variation("HeaderSmall");166vb->add_child(name_label);167168dir_path = memnew(LineEdit);169dir_path->set_accessibility_name(TTRC("Name:"));170vb->add_child(dir_path);171register_text_enter(dir_path);172173Control *spacing = memnew(Control);174spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));175vb->add_child(spacing);176177validation_panel = memnew(EditorValidationPanel);178vb->add_child(validation_panel);179validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Folder name is valid."));180validation_panel->set_update_callback(callable_mp(this, &DirectoryCreateDialog::_on_dir_path_changed));181validation_panel->set_accept_button(get_ok_button());182183dir_path->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));184}185186187