Merge pull request #31398 from KoBeWi/1000_ways_to_make_scene

Allow to create scene from FileSystem dock
This commit is contained in:
Rémi Verschelde 2019-08-17 11:25:33 +02:00 committed by GitHub
commit c7a7163dff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 7 deletions

View file

@ -537,6 +537,7 @@ int EditorData::add_edited_scene(int p_at_pos) {
p_at_pos = edited_scene.size();
EditedScene es;
es.root = NULL;
es.path = String();
es.history_current = -1;
es.version = 0;
es.live_edit_root = NodePath(String("/root"));
@ -653,6 +654,8 @@ bool EditorData::check_and_update_scene(int p_idx) {
memdelete(edited_scene[p_idx].root);
edited_scene.write[p_idx].root = new_scene;
if (new_scene->get_filename() != "")
edited_scene.write[p_idx].path = new_scene->get_filename();
edited_scene.write[p_idx].selection = new_selection;
return true;
@ -684,6 +687,12 @@ void EditorData::set_edited_scene_root(Node *p_root) {
ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
edited_scene.write[current_edited_scene].root = p_root;
if (p_root) {
if (p_root->get_filename() != "")
edited_scene.write[current_edited_scene].path = p_root->get_filename();
else
p_root->set_filename(edited_scene[current_edited_scene].path);
}
}
int EditorData::get_edited_scene_count() const {
@ -773,6 +782,7 @@ String EditorData::get_scene_title(int p_idx) const {
void EditorData::set_scene_path(int p_idx, const String &p_path) {
ERR_FAIL_INDEX(p_idx, edited_scene.size());
edited_scene.write[p_idx].path = p_path;
if (!edited_scene[p_idx].root)
return;
@ -783,9 +793,14 @@ String EditorData::get_scene_path(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
if (!edited_scene[p_idx].root)
return "";
return edited_scene[p_idx].root->get_filename();
if (edited_scene[p_idx].root) {
if (edited_scene[p_idx].root->get_filename() == "")
edited_scene[p_idx].root->set_filename(edited_scene[p_idx].path);
else
return edited_scene[p_idx].root->get_filename();
}
return edited_scene[p_idx].path;
}
void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {

View file

@ -117,6 +117,7 @@ public:
struct EditedScene {
Node *root;
String path;
Dictionary editor_states;
List<Node *> selection;
Vector<EditorHistory::History> history_stored;

View file

@ -1926,10 +1926,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
switch (p_option) {
case FILE_NEW_SCENE: {
int idx = editor_data.add_edited_scene(-1);
_scene_tab_changed(idx);
editor_data.clear_editor_states();
_update_scene_tabs();
new_scene();
} break;
case FILE_NEW_INHERITED_SCENE:
@ -3162,6 +3159,14 @@ void EditorNode::fix_dependencies(const String &p_for_file) {
dependency_fixer->edit(p_for_file);
}
int EditorNode::new_scene() {
int idx = editor_data.add_edited_scene(-1);
_scene_tab_changed(idx);
editor_data.clear_editor_states();
_update_scene_tabs();
return idx;
}
Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, bool p_set_inherited, bool p_clear_errors, bool p_force_open_imported) {
if (!is_inside_tree()) {

View file

@ -745,6 +745,7 @@ public:
void fix_dependencies(const String &p_for_file);
void clear_scene() { _cleanup_scene(); }
int new_scene();
Error load_scene(const String &p_scene, bool p_ignore_broken_deps = false, bool p_set_inherited = false, bool p_clear_errors = true, bool p_force_open_imported = false);
Error load_resource(const String &p_resource, bool p_ignore_broken_deps = false);

View file

@ -39,6 +39,7 @@
#include "editor_node.h"
#include "editor_settings.h"
#include "scene/main/viewport.h"
#include "scene/resources/packed_scene.h"
Ref<Texture> FileSystemDock::_get_tree_item_icon(EditorFileSystemDirectory *p_dir, int p_idx) {
Ref<Texture> file_icon;
@ -1250,6 +1251,48 @@ void FileSystemDock::_make_dir_confirm() {
}
}
void FileSystemDock::_make_scene_confirm() {
String scene_name = make_scene_dialog_text->get_text().strip_edges();
if (scene_name.length() == 0) {
EditorNode::get_singleton()->show_warning(TTR("No name provided."));
return;
}
String directory = path;
if (!directory.ends_with("/")) {
directory = directory.get_base_dir();
}
String extension = scene_name.get_extension();
List<String> extensions;
Ref<PackedScene> sd = memnew(PackedScene);
ResourceSaver::get_recognized_extensions(sd, &extensions);
bool extension_correct = false;
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
if (E->get() == extension) {
extension_correct = true;
break;
}
}
if (!extension_correct)
scene_name = scene_name.get_basename() + ".tscn";
scene_name = directory.plus_file(scene_name);
DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
if (da->file_exists(scene_name)) {
EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
memdelete(da);
return;
}
memdelete(da);
int idx = editor->new_scene();
editor->get_editor_data().set_scene_path(idx, scene_name);
}
void FileSystemDock::_file_deleted(String p_file) {
emit_signal("file_deleted", p_file);
}
@ -1698,6 +1741,13 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected
make_dir_dialog_text->grab_focus();
} break;
case FILE_NEW_SCENE: {
make_scene_dialog_text->set_text("new scene");
make_scene_dialog_text->select_all();
make_scene_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
make_scene_dialog_text->grab_focus();
} break;
case FILE_NEW_SCRIPT: {
// Create a new script
String fpath = path;
@ -2157,6 +2207,7 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, Vector<Str
p_popup->add_separator();
if (p_display_path_dependent_options) {
p_popup->add_item(TTR("New Folder..."), FILE_NEW_FOLDER);
p_popup->add_item(TTR("New Scene..."), FILE_NEW_SCENE);
p_popup->add_item(TTR("New Script..."), FILE_NEW_SCRIPT);
p_popup->add_item(TTR("New Resource..."), FILE_NEW_RESOURCE);
}
@ -2195,6 +2246,7 @@ void FileSystemDock::_tree_rmb_empty(const Vector2 &p_pos) {
tree_popup->clear();
tree_popup->set_size(Size2(1, 1));
tree_popup->add_item(TTR("New Folder..."), FILE_NEW_FOLDER);
tree_popup->add_item(TTR("New Scene..."), FILE_NEW_SCENE);
tree_popup->add_item(TTR("New Script..."), FILE_NEW_SCRIPT);
tree_popup->add_item(TTR("New Resource..."), FILE_NEW_RESOURCE);
tree_popup->set_position(tree->get_global_position() + p_pos);
@ -2237,6 +2289,7 @@ void FileSystemDock::_file_list_rmb_pressed(const Vector2 &p_pos) {
file_list_popup->set_size(Size2(1, 1));
file_list_popup->add_item(TTR("New Folder..."), FILE_NEW_FOLDER);
file_list_popup->add_item(TTR("New Scene..."), FILE_NEW_SCENE);
file_list_popup->add_item(TTR("New Script..."), FILE_NEW_SCRIPT);
file_list_popup->add_item(TTR("New Resource..."), FILE_NEW_RESOURCE);
file_list_popup->add_item(TTR("Show in File Manager"), FILE_SHOW_IN_EXPLORER);
@ -2411,6 +2464,7 @@ void FileSystemDock::_bind_methods() {
ClassDB::bind_method(D_METHOD("_fs_changed"), &FileSystemDock::_fs_changed);
ClassDB::bind_method(D_METHOD("_tree_multi_selected"), &FileSystemDock::_tree_multi_selected);
ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileSystemDock::_make_dir_confirm);
ClassDB::bind_method(D_METHOD("_make_scene_confirm"), &FileSystemDock::_make_scene_confirm);
ClassDB::bind_method(D_METHOD("_resource_created"), &FileSystemDock::_resource_created);
ClassDB::bind_method(D_METHOD("_move_operation_confirm", "to_path", "overwrite"), &FileSystemDock::_move_operation_confirm, DEFVAL(false));
ClassDB::bind_method(D_METHOD("_move_with_overwrite"), &FileSystemDock::_move_with_overwrite);
@ -2626,6 +2680,17 @@ FileSystemDock::FileSystemDock(EditorNode *p_editor) {
make_dir_dialog->register_text_enter(make_dir_dialog_text);
make_dir_dialog->connect("confirmed", this, "_make_dir_confirm");
make_scene_dialog = memnew(ConfirmationDialog);
make_scene_dialog->set_title(TTR("Create Scene"));
VBoxContainer *make_scene_dialog_vb = memnew(VBoxContainer);
make_scene_dialog->add_child(make_scene_dialog_vb);
make_scene_dialog_text = memnew(LineEdit);
make_scene_dialog_vb->add_margin_child(TTR("Name:"), make_scene_dialog_text);
add_child(make_scene_dialog);
make_scene_dialog->register_text_enter(make_scene_dialog_text);
make_scene_dialog->connect("confirmed", this, "_make_scene_confirm");
make_script_dialog_text = memnew(ScriptCreateDialog);
make_script_dialog_text->set_title(TTR("Create Script"));
add_child(make_script_dialog_text);

View file

@ -87,6 +87,7 @@ private:
FILE_INFO,
FILE_NEW_FOLDER,
FILE_NEW_SCRIPT,
FILE_NEW_SCENE,
FILE_SHOW_IN_EXPLORER,
FILE_COPY_PATH,
FILE_NEW_RESOURCE,
@ -135,6 +136,8 @@ private:
LineEdit *duplicate_dialog_text;
ConfirmationDialog *make_dir_dialog;
LineEdit *make_dir_dialog_text;
ConfirmationDialog *make_scene_dialog;
LineEdit *make_scene_dialog_text;
ConfirmationDialog *overwrite_dialog;
ScriptCreateDialog *make_script_dialog_text;
CreateDialog *new_resource_dialog;
@ -213,6 +216,7 @@ private:
void _resource_created() const;
void _make_dir_confirm();
void _make_scene_confirm();
void _rename_operation_confirm();
void _duplicate_operation_confirm();
void _move_with_overwrite();