/*************************************************************************/ /* editor_node.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "editor_node.h" #include "core/bind/core_bind.h" #include "core/class_db.h" #include "core/io/config_file.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/io/stream_peer_ssl.h" #include "core/io/zip_io.h" #include "core/message_queue.h" #include "core/os/file_access.h" #include "core/os/input.h" #include "core/os/keyboard.h" #include "core/os/os.h" #include "core/path_remap.h" #include "core/print_string.h" #include "core/project_settings.h" #include "core/translation.h" #include "core/version.h" #include "main/input_default.h" #include "scene/resources/packed_scene.h" #include "servers/physics_2d_server.h" #include "editor/animation_editor.h" #include "editor/editor_audio_buses.h" #include "editor/editor_file_system.h" #include "editor/editor_help.h" #include "editor/editor_initialize_ssl.h" #include "editor/editor_settings.h" #include "editor/editor_themes.h" #include "editor/import/editor_import_collada.h" #include "editor/import/editor_scene_importer_gltf.h" #include "editor/import/resource_importer_bitmask.h" #include "editor/import/resource_importer_csv_translation.h" #include "editor/import/resource_importer_obj.h" #include "editor/import/resource_importer_scene.h" #include "editor/import/resource_importer_texture.h" #include "editor/import/resource_importer_wav.h" #include "editor/plugins/animation_player_editor_plugin.h" #include "editor/plugins/animation_tree_editor_plugin.h" #include "editor/plugins/asset_library_editor_plugin.h" #include "editor/plugins/baked_lightmap_editor_plugin.h" #include "editor/plugins/camera_editor_plugin.h" #include "editor/plugins/canvas_item_editor_plugin.h" #include "editor/plugins/collision_polygon_2d_editor_plugin.h" #include "editor/plugins/collision_polygon_editor_plugin.h" #include "editor/plugins/collision_shape_2d_editor_plugin.h" #include "editor/plugins/cube_grid_theme_editor_plugin.h" #include "editor/plugins/curve_editor_plugin.h" #include "editor/plugins/editor_preview_plugins.h" #include "editor/plugins/gi_probe_editor_plugin.h" #include "editor/plugins/gradient_editor_plugin.h" #include "editor/plugins/item_list_editor_plugin.h" #include "editor/plugins/light_occluder_2d_editor_plugin.h" #include "editor/plugins/line_2d_editor_plugin.h" #include "editor/plugins/material_editor_plugin.h" #include "editor/plugins/mesh_editor_plugin.h" #include "editor/plugins/mesh_instance_editor_plugin.h" #include "editor/plugins/multimesh_editor_plugin.h" #include "editor/plugins/navigation_mesh_editor_plugin.h" #include "editor/plugins/navigation_polygon_editor_plugin.h" #include "editor/plugins/particles_2d_editor_plugin.h" #include "editor/plugins/particles_editor_plugin.h" #include "editor/plugins/path_2d_editor_plugin.h" #include "editor/plugins/path_editor_plugin.h" #include "editor/plugins/polygon_2d_editor_plugin.h" #include "editor/plugins/resource_preloader_editor_plugin.h" #include "editor/plugins/script_editor_plugin.h" #include "editor/plugins/script_text_editor.h" #include "editor/plugins/shader_editor_plugin.h" #include "editor/plugins/shader_graph_editor_plugin.h" #include "editor/plugins/spatial_editor_plugin.h" #include "editor/plugins/sprite_frames_editor_plugin.h" #include "editor/plugins/style_box_editor_plugin.h" #include "editor/plugins/texture_editor_plugin.h" #include "editor/plugins/texture_region_editor_plugin.h" #include "editor/plugins/theme_editor_plugin.h" #include "editor/plugins/tile_map_editor_plugin.h" #include "editor/plugins/tile_set_editor_plugin.h" #include "editor/pvrtc_compress.h" #include "editor/register_exporters.h" #include "editor/script_editor_debugger.h" #include EditorNode *EditorNode::singleton = NULL; void EditorNode::_update_scene_tabs() { bool show_rb = EditorSettings::get_singleton()->get("interface/scene_tabs/show_script_button"); scene_tabs->clear_tabs(); Ref script_icon = gui_base->get_icon("Script", "EditorIcons"); for (int i = 0; i < editor_data.get_edited_scene_count(); i++) { String type = editor_data.get_scene_type(i); Ref icon; if (type != String()) { if (!gui_base->has_icon(type, "EditorIcons")) { type = "Node"; } icon = gui_base->get_icon(type, "EditorIcons"); } int current = editor_data.get_edited_scene(); bool unsaved = (i == current) ? saved_version != editor_data.get_undo_redo().get_version() : editor_data.get_scene_version(i) != 0; scene_tabs->add_tab(editor_data.get_scene_title(i) + (unsaved ? "(*)" : ""), icon); if (show_rb && editor_data.get_scene_root_script(i).is_valid()) { scene_tabs->set_tab_right_button(i, script_icon); } } scene_tabs->set_current_tab(editor_data.get_edited_scene()); int current = editor_data.get_edited_scene(); if (scene_tabs->get_offset_buttons_visible()) { // move add button to fixed position on the tabbar if (scene_tab_add->get_parent() == scene_tabs) { scene_tab_add->set_position(Point2(0, 0)); scene_tabs->remove_child(scene_tab_add); tabbar_container->add_child(scene_tab_add); tabbar_container->move_child(scene_tab_add, 1); } } else { // move add button to after last tab if (scene_tab_add->get_parent() == tabbar_container) { tabbar_container->remove_child(scene_tab_add); scene_tabs->add_child(scene_tab_add); } Rect2 last_tab = Rect2(); if (scene_tabs->get_tab_count() != 0) last_tab = scene_tabs->get_tab_rect(scene_tabs->get_tab_count() - 1); scene_tab_add->set_position(Point2(last_tab.get_position().x + last_tab.get_size().x + 3, last_tab.get_position().y)); } } void EditorNode::_update_title() { String appname = ProjectSettings::get_singleton()->get("application/config/name"); String title = appname.empty() ? String(VERSION_FULL_NAME) : String(VERSION_NAME + String(" - ") + appname); String edited = editor_data.get_edited_scene_root() ? editor_data.get_edited_scene_root()->get_filename() : String(); if (!edited.empty()) title += " - " + String(edited.get_file()); if (unsaved_cache) title += " (*)"; OS::get_singleton()->set_window_title(title); } void EditorNode::_unhandled_input(const Ref &p_event) { if (Node::get_viewport()->get_modal_stack_top()) return; //ignore because of modal window Ref k = p_event; if (k.is_valid() && k->is_pressed() && !k->is_echo() && !gui_base->get_viewport()->gui_has_modal_stack()) { EditorPlugin *old_editor = editor_plugin_screen; if (ED_IS_SHORTCUT("editor/next_tab", p_event)) { int next_tab = editor_data.get_edited_scene() + 1; next_tab %= editor_data.get_edited_scene_count(); _scene_tab_changed(next_tab); } if (ED_IS_SHORTCUT("editor/prev_tab", p_event)) { int next_tab = editor_data.get_edited_scene() - 1; next_tab = next_tab >= 0 ? next_tab : editor_data.get_edited_scene_count() - 1; _scene_tab_changed(next_tab); } if (ED_IS_SHORTCUT("editor/filter_files", p_event)) { filesystem_dock->focus_on_filter(); } if (ED_IS_SHORTCUT("editor/editor_2d", p_event)) { _editor_select(EDITOR_2D); } else if (ED_IS_SHORTCUT("editor/editor_3d", p_event)) { _editor_select(EDITOR_3D); } else if (ED_IS_SHORTCUT("editor/editor_script", p_event)) { _editor_select(EDITOR_SCRIPT); } else if (ED_IS_SHORTCUT("editor/editor_help", p_event)) { emit_signal("request_help_search", ""); } else if (ED_IS_SHORTCUT("editor/editor_assetlib", p_event)) { _editor_select(EDITOR_ASSETLIB); } else if (ED_IS_SHORTCUT("editor/editor_next", p_event)) { _editor_select_next(); } else if (ED_IS_SHORTCUT("editor/editor_prev", p_event)) { _editor_select_prev(); } if (old_editor != editor_plugin_screen) { get_tree()->set_input_as_handled(); } } } void EditorNode::_notification(int p_what) { if (p_what == NOTIFICATION_EXIT_TREE) { editor_data.save_editor_external_data(); FileAccess::set_file_close_fail_notify_callback(NULL); log->deinit(); // do not get messages anymore } if (p_what == NOTIFICATION_PROCESS) { if (opening_prev && !confirmation->is_visible()) opening_prev = false; if (unsaved_cache != (saved_version != editor_data.get_undo_redo().get_version())) { unsaved_cache = (saved_version != editor_data.get_undo_redo().get_version()); _update_title(); } if (last_checked_version != editor_data.get_undo_redo().get_version()) { _update_scene_tabs(); last_checked_version = editor_data.get_undo_redo().get_version(); } //update the circle uint64_t frame = Engine::get_singleton()->get_frames_drawn(); uint32_t tick = OS::get_singleton()->get_ticks_msec(); if (frame != circle_step_frame && (tick - circle_step_msec) > (1000 / 8)) { circle_step++; if (circle_step >= 8) circle_step = 0; circle_step_msec = tick; circle_step_frame = frame + 1; // update the circle itself only when its enabled if (!update_menu->get_popup()->is_item_checked(3)) { update_menu->set_icon(gui_base->get_icon("Progress" + itos(circle_step + 1), "EditorIcons")); } } editor_selection->update(); scene_root->set_size_override(true, Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height"))); ResourceImporterTexture::get_singleton()->update_imports(); } if (p_what == NOTIFICATION_ENTER_TREE) { Engine::get_singleton()->set_editor_hint(true); get_tree()->get_root()->set_usage(Viewport::USAGE_2D_NO_SAMPLING); //reduce memory usage get_tree()->get_root()->set_disable_3d(true); get_tree()->get_root()->set_as_audio_listener(false); get_tree()->get_root()->set_as_audio_listener_2d(false); get_tree()->set_auto_accept_quit(false); get_tree()->connect("files_dropped", this, "_dropped_files"); property_editable_warning->set_icon(gui_base->get_icon("NodeWarning", "EditorIcons")); } if (p_what == NOTIFICATION_EXIT_TREE) { editor_data.clear_edited_scenes(); } if (p_what == NOTIFICATION_READY) { VisualServer::get_singleton()->viewport_set_hide_scenario(get_scene_root()->get_viewport_rid(), true); VisualServer::get_singleton()->viewport_set_hide_canvas(get_scene_root()->get_viewport_rid(), true); VisualServer::get_singleton()->viewport_set_disable_environment(get_viewport()->get_viewport_rid(), true); _editor_select(EDITOR_3D); _update_debug_options(); _load_docks(); } if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN) { EditorFileSystem::get_singleton()->scan_changes(); } if (p_what == MainLoop::NOTIFICATION_WM_QUIT_REQUEST) { _menu_option_confirm(FILE_QUIT, false); } if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) { scene_tabs->set_tab_close_display_policy((bool(EDITOR_DEF("interface/scene_tabs/always_show_close_button", false)) ? Tabs::CLOSE_BUTTON_SHOW_ALWAYS : Tabs::CLOSE_BUTTON_SHOW_ACTIVE_ONLY)); Ref theme = create_editor_theme(theme_base->get_theme()); theme_base->set_theme(theme); gui_base->set_theme(theme); gui_base->add_style_override("panel", gui_base->get_stylebox("Background", "EditorStyles")); play_button_panel->add_style_override("panel", gui_base->get_stylebox("PlayButtonPanel", "EditorStyles")); scene_root_parent->add_style_override("panel", gui_base->get_stylebox("Content", "EditorStyles")); bottom_panel->add_style_override("panel", gui_base->get_stylebox("panel", "TabContainer")); scene_tabs->add_style_override("tab_fg", gui_base->get_stylebox("SceneTabFG", "EditorStyles")); scene_tabs->add_style_override("tab_bg", gui_base->get_stylebox("SceneTabBG", "EditorStyles")); file_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles")); project_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles")); debug_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles")); settings_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles")); help_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles")); if (bool(EDITOR_DEF("interface/scene_tabs/resize_if_many_tabs", true))) { scene_tabs->set_min_width(int(EDITOR_DEF("interface/scene_tabs/minimum_width", 50)) * EDSCALE); } else { scene_tabs->set_min_width(0); } _update_scene_tabs(); recent_scenes->set_as_minsize(); // debugger area if (ScriptEditor::get_singleton()->get_debugger()->is_visible()) bottom_panel->add_style_override("panel", gui_base->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")); //_update_icons for (int i = 0; i < singleton->main_editor_buttons.size(); i++) { Ref icon = singleton->main_editor_buttons[i]->get_icon(); if (icon.is_valid()) { main_editor_buttons[i]->set_icon(icon); } else if (singleton->gui_base->has_icon(singleton->main_editor_buttons[i]->get_name(), "EditorIcons")) { main_editor_buttons[i]->set_icon(gui_base->get_icon(singleton->main_editor_buttons[i]->get_name(), "EditorIcons")); } } play_button->set_icon(gui_base->get_icon("MainPlay", "EditorIcons")); play_scene_button->set_icon(gui_base->get_icon("PlayScene", "EditorIcons")); play_custom_scene_button->set_icon(gui_base->get_icon("PlayCustom", "EditorIcons")); pause_button->set_icon(gui_base->get_icon("Pause", "EditorIcons")); stop_button->set_icon(gui_base->get_icon("Stop", "EditorIcons")); prev_scene->set_icon(gui_base->get_icon("PrevScene", "EditorIcons")); distraction_free->set_icon(gui_base->get_icon("DistractionFree", "EditorIcons")); scene_tab_add->set_icon(gui_base->get_icon("Add", "EditorIcons")); resource_new_button->set_icon(gui_base->get_icon("New", "EditorIcons")); resource_load_button->set_icon(gui_base->get_icon("Load", "EditorIcons")); resource_save_button->set_icon(gui_base->get_icon("Save", "EditorIcons")); property_back->set_icon(gui_base->get_icon("Back", "EditorIcons")); property_forward->set_icon(gui_base->get_icon("Forward", "EditorIcons")); editor_history_menu->set_icon(gui_base->get_icon("History", "EditorIcons")); search_button->set_icon(gui_base->get_icon("Search", "EditorIcons")); object_menu->set_icon(gui_base->get_icon("Tools", "EditorIcons")); // clear_button->set_icon(gui_base->get_icon("Close", "EditorIcons")); don't have access to that node. needs to become a class property update_menu->set_icon(gui_base->get_icon("Collapse", "EditorIcons")); dock_tab_move_left->set_icon(theme->get_icon("Back", "EditorIcons")); dock_tab_move_right->set_icon(theme->get_icon("Forward", "EditorIcons")); update_menu->set_icon(gui_base->get_icon("Progress1", "EditorIcons")); } if (p_what == Control::NOTIFICATION_RESIZED) { _update_scene_tabs(); } } void EditorNode::_fs_changed() { for (Set::Element *E = file_dialogs.front(); E; E = E->next()) { E->get()->invalidate(); } for (Set::Element *E = editor_file_dialogs.front(); E; E = E->next()) { E->get()->invalidate(); } { //reload changed resources List > changed; List > cached; ResourceCache::get_cached_resources(&cached); // FIXME: This should be done in a thread. for (List >::Element *E = cached.front(); E; E = E->next()) { if (!E->get()->editor_can_reload_from_file()) continue; if (!E->get()->get_path().is_resource_file() && !E->get()->get_path().is_abs_path()) continue; if (!FileAccess::exists(E->get()->get_path())) continue; if (E->get()->get_import_path() != String()) { //this is an imported resource, will be reloaded if reimported via the _resources_reimported() callback continue; } uint64_t mt = FileAccess::get_modified_time(E->get()->get_path()); if (mt != E->get()->get_last_modified_time()) { changed.push_back(E->get()); } } if (changed.size()) { for (List >::Element *E = changed.front(); E; E = E->next()) { E->get()->reload_from_file(); } } } _mark_unsaved_scenes(); if (export_defer.preset != "" && !EditorFileSystem::get_singleton()->is_scanning()) { Ref preset; for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); ++i) { preset = EditorExport::get_singleton()->get_export_preset(i); if (preset->get_name() == export_defer.preset) { break; } preset.unref(); } if (preset.is_null()) { String err = "Unknown export preset: " + export_defer.preset; ERR_PRINT(err.utf8().get_data()); } else { Ref platform = preset->get_platform(); if (platform.is_null()) { String err = "Preset \"" + export_defer.preset + "\" doesn't have a platform."; ERR_PRINT(err.utf8().get_data()); } else { // ensures export_project does not loop infinitely, because notifications may // come during the export export_defer.preset = ""; if (!preset->is_runnable() && (export_defer.path.ends_with(".pck") || export_defer.path.ends_with(".zip"))) { if (export_defer.path.ends_with(".zip")) { platform->save_zip(preset, export_defer.path); } else if (export_defer.path.ends_with(".pck")) { platform->save_pack(preset, export_defer.path); } } else { platform->export_project(preset, export_defer.debug, export_defer.path, /*p_flags*/ 0); } } } get_tree()->quit(); } } void EditorNode::_resources_reimported(const Vector &p_resources) { List scenes; //will load later for (int i = 0; i < p_resources.size(); i++) { String file_type = ResourceLoader::get_resource_type(p_resources[i]); if (file_type == "PackedScene") { scenes.push_back(p_resources[i]); //reload later if needed, first go with normal resources continue; } if (!ResourceCache::has(p_resources[i])) { continue; //not loaded, no need to reload } //reload normally Resource *resource = ResourceCache::get(p_resources[i]); if (resource) { resource->reload_from_file(); } } for (List::Element *E = scenes.front(); E; E = E->next()) { reload_scene(E->get()); } } void EditorNode::_sources_changed(bool p_exist) { if (waiting_for_first_scan) { if (defer_load_scene != "") { load_scene(defer_load_scene); defer_load_scene = ""; } waiting_for_first_scan = false; } } void EditorNode::_vp_resized() { } void EditorNode::_node_renamed() { if (property_editor) property_editor->update_tree(); } void EditorNode::_editor_select_next() { int editor = _get_current_main_editor(); if (editor == editor_table.size() - 1) { editor = 0; } else { editor++; } _editor_select(editor); } void EditorNode::_editor_select_prev() { int editor = _get_current_main_editor(); if (editor == 0) { editor = editor_table.size() - 1; } else { editor--; } _editor_select(editor); } Error EditorNode::load_resource(const String &p_scene) { RES res = ResourceLoader::load(p_scene); ERR_FAIL_COND_V(!res.is_valid(), ERR_CANT_OPEN); edit_resource(res); return OK; } void EditorNode::edit_resource(const Ref &p_resource) { _resource_selected(p_resource, ""); } void EditorNode::edit_node(Node *p_node) { push_item(p_node); } void EditorNode::open_resource(const String &p_type) { file->set_mode(EditorFileDialog::MODE_OPEN_FILE); List extensions; ResourceLoader::get_recognized_extensions_for_type(p_type, &extensions); file->clear_filters(); for (int i = 0; i < extensions.size(); i++) { file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper()); } file->popup_centered_ratio(); current_option = RESOURCE_LOAD; } void EditorNode::save_resource_in_path(const Ref &p_resource, const String &p_path) { editor_data.apply_changes_in_editors(); int flg = 0; if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) flg |= ResourceSaver::FLAG_COMPRESS; String path = ProjectSettings::get_singleton()->localize_path(p_path); Error err = ResourceSaver::save(path, p_resource, flg | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS); if (err != OK) { current_option = -1; accept->set_text(TTR("Error saving resource!")); accept->popup_centered_minsize(); return; } ((Resource *)p_resource.ptr())->set_path(path); emit_signal("resource_saved", p_resource); } void EditorNode::save_resource(const Ref &p_resource) { if (p_resource->get_path().is_resource_file()) { save_resource_in_path(p_resource, p_resource->get_path()); } else { save_resource_as(p_resource); } } void EditorNode::save_resource_as(const Ref &p_resource, const String &p_at_path) { file->set_mode(EditorFileDialog::MODE_SAVE_FILE); current_option = RESOURCE_SAVE_AS; List extensions; Ref sd = memnew(PackedScene); ResourceSaver::get_recognized_extensions(p_resource, &extensions); file->clear_filters(); List preferred; for (int i = 0; i < extensions.size(); i++) { if (p_resource->is_class("Script") && (extensions[i] == "tres" || extensions[i] == "res" || extensions[i] == "xml")) { //this serves no purpose and confused people continue; } file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper()); preferred.push_back(extensions[i]); } if (p_at_path != String()) { file->set_current_dir(p_at_path); if (p_resource->get_path().is_resource_file()) { file->set_current_file(p_resource->get_path().get_file()); } else { if (extensions.size()) { file->set_current_file("new_" + p_resource->get_class().to_lower() + "." + preferred.front()->get().to_lower()); } else { file->set_current_file(String()); } } } else if (p_resource->get_path() != "") { file->set_current_path(p_resource->get_path()); if (extensions.size()) { String ext = p_resource->get_path().get_extension().to_lower(); if (extensions.find(ext) == NULL) { file->set_current_path(p_resource->get_path().replacen("." + ext, "." + extensions.front()->get())); } } } else if (preferred.size()) { String existing; if (extensions.size()) { existing = "new_" + p_resource->get_class().to_lower() + "." + preferred.front()->get().to_lower(); } file->set_current_path(existing); } file->popup_centered_ratio(); file->set_title(TTR("Save Resource As..")); } void EditorNode::_menu_option(int p_option) { _menu_option_confirm(p_option, false); } void EditorNode::_menu_confirm_current() { _menu_option_confirm(current_option, true); } void EditorNode::_dialog_display_save_error(String p_file, Error p_error) { if (p_error) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); switch (p_error) { case ERR_FILE_CANT_WRITE: { accept->set_text(TTR("Can't open file for writing:") + " " + p_file.get_extension()); } break; case ERR_FILE_UNRECOGNIZED: { accept->set_text(TTR("Requested file format unknown:") + " " + p_file.get_extension()); } break; default: { accept->set_text(TTR("Error while saving.")); } break; } accept->popup_centered_minsize(); } } void EditorNode::_dialog_display_load_error(String p_file, Error p_error) { if (p_error) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); switch (p_error) { case ERR_CANT_OPEN: { accept->set_text(vformat(TTR("Can't open '%s'."), p_file.get_file())); } break; case ERR_PARSE_ERROR: { accept->set_text(vformat(TTR("Error while parsing '%s'."), p_file.get_file())); } break; case ERR_FILE_CORRUPT: { accept->set_text(vformat(TTR("Unexpected end of file '%s'."), p_file.get_file())); } break; case ERR_FILE_NOT_FOUND: { accept->set_text(vformat(TTR("Missing '%s' or its dependencies."), p_file.get_file())); } break; default: { accept->set_text(vformat(TTR("Error while loading '%s'."), p_file.get_file())); } break; } accept->popup_centered_minsize(); } } void EditorNode::_get_scene_metadata(const String &p_file) { Node *scene = editor_data.get_edited_scene_root(); if (!scene) return; String path = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(p_file.get_file() + "-editstate-" + p_file.md5_text() + ".cfg"); Ref cf; cf.instance(); Error err = cf->load(path); if (err != OK || !cf->has_section("editor_states")) return; //must not exist List esl; cf->get_section_keys("editor_states", &esl); Dictionary md; for (List::Element *E = esl.front(); E; E = E->next()) { Variant st = cf->get_value("editor_states", E->get()); if (st.get_type()) { md[E->get()] = st; } } editor_data.set_editor_states(md); } void EditorNode::_set_scene_metadata(const String &p_file, int p_idx) { Node *scene = editor_data.get_edited_scene_root(p_idx); if (!scene) return; scene->set_meta("__editor_run_settings__", Variant()); //clear it (no point in keeping it) scene->set_meta("__editor_plugin_states__", Variant()); String path = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(p_file.get_file() + "-editstate-" + p_file.md5_text() + ".cfg"); Ref cf; cf.instance(); Dictionary md; if (p_idx < 0 || editor_data.get_edited_scene() == p_idx) { md = editor_data.get_editor_states(); } else { md = editor_data.get_scene_editor_states(p_idx); } List keys; md.get_key_list(&keys); for (List::Element *E = keys.front(); E; E = E->next()) { cf->set_value("editor_states", E->get(), md[E->get()]); } Error err = cf->save(path); ERR_FAIL_COND(err != OK); } bool EditorNode::_find_and_save_resource(RES p_res, Map &processed, int32_t flags) { if (p_res.is_null()) return false; if (processed.has(p_res)) { return processed[p_res]; } bool changed = p_res->is_edited(); p_res->set_edited(false); bool subchanged = _find_and_save_edited_subresources(p_res.ptr(), processed, flags); if (p_res->get_path().is_resource_file()) { if (changed || subchanged) { //save ResourceSaver::save(p_res->get_path(), p_res, flags); } processed[p_res] = false; //because it's a file return false; } else { processed[p_res] = changed; return changed; } } bool EditorNode::_find_and_save_edited_subresources(Object *obj, Map &processed, int32_t flags) { bool ret_changed = false; List pi; obj->get_property_list(&pi); for (List::Element *E = pi.front(); E; E = E->next()) { if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) continue; switch (E->get().type) { case Variant::OBJECT: { RES res = obj->get(E->get().name); if (_find_and_save_resource(res, processed, flags)) ret_changed = true; } break; case Variant::ARRAY: { Array varray = obj->get(E->get().name); int len = varray.size(); for (int i = 0; i < len; i++) { Variant v = varray.get(i); RES res = v; if (_find_and_save_resource(res, processed, flags)) ret_changed = true; } } break; case Variant::DICTIONARY: { Dictionary d = obj->get(E->get().name); List keys; d.get_key_list(&keys); for (List::Element *E = keys.front(); E; E = E->next()) { Variant v = d[E->get()]; RES res = v; if (_find_and_save_resource(res, processed, flags)) ret_changed = true; } } break; default: {} } } return ret_changed; } void EditorNode::_save_edited_subresources(Node *scene, Map &processed, int32_t flags) { _find_and_save_edited_subresources(scene, processed, flags); for (int i = 0; i < scene->get_child_count(); i++) { Node *n = scene->get_child(i); if (n->get_owner() != editor_data.get_edited_scene_root()) continue; _save_edited_subresources(n, processed, flags); } } void EditorNode::_find_node_types(Node *p_node, int &count_2d, int &count_3d) { if (p_node->is_class("Viewport") || (p_node != editor_data.get_edited_scene_root() && p_node->get_owner() != editor_data.get_edited_scene_root())) return; if (p_node->is_class("CanvasItem")) count_2d++; else if (p_node->is_class("Spatial")) count_3d++; for (int i = 0; i < p_node->get_child_count(); i++) _find_node_types(p_node->get_child(i), count_2d, count_3d); } void EditorNode::_save_scene_with_preview(String p_file, int p_idx) { EditorProgress save("save", TTR("Saving Scene"), 4); save.step(TTR("Analyzing"), 0); int c2d = 0; int c3d = 0; _find_node_types(editor_data.get_edited_scene_root(), c2d, c3d); RID viewport; bool is2d; if (c3d < c2d) { viewport = scene_root->get_viewport_rid(); is2d = true; } else { viewport = SpatialEditor::get_singleton()->get_editor_viewport(0)->get_viewport_node()->get_viewport_rid(); is2d = false; } save.step(TTR("Creating Thumbnail"), 1); //current view? Ref img; if (is2d) { img = scene_root->get_texture()->get_data(); } else { img = SpatialEditor::get_singleton()->get_editor_viewport(0)->get_viewport_node()->get_texture()->get_data(); } if (img.is_valid()) { save.step(TTR("Creating Thumbnail"), 2); save.step(TTR("Creating Thumbnail"), 3); int preview_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); preview_size *= EDSCALE; // consider a square region int vp_size = MIN(img->get_width(), img->get_height()); int x = (img->get_width() - vp_size) / 2; int y = (img->get_height() - vp_size) / 2; img->convert(Image::FORMAT_RGB8); if (vp_size < preview_size) { // just square it. img->crop_from_point(x, y, vp_size, vp_size); } else { int ratio = vp_size / preview_size; int size = preview_size * (ratio / 2); x = (img->get_width() - size) / 2; y = (img->get_height() - size) / 2; img->crop_from_point(x, y, size, size); // We could get better pictures with better filters img->resize(preview_size, preview_size, Image::INTERPOLATE_CUBIC); } img->flip_y(); //save thumbnail directly, as thumbnailer may not update due to actual scene not changing md5 String temp_path = EditorSettings::get_singleton()->get_cache_dir(); String cache_base = ProjectSettings::get_singleton()->globalize_path(p_file).md5_text(); cache_base = temp_path.plus_file("resthumb-" + cache_base); //does not have it, try to load a cached thumbnail String file = cache_base + ".png"; post_process_preview(img); img->save_png(file); } save.step(TTR("Saving Scene"), 4); _save_scene(p_file, p_idx); EditorResourcePreview::get_singleton()->check_for_invalidation(p_file); } void EditorNode::_save_scene(String p_file, int idx) { Node *scene = editor_data.get_edited_scene_root(idx); if (!scene) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); accept->set_text(TTR("This operation can't be done without a tree root.")); accept->popup_centered_minsize(); return; } editor_data.apply_changes_in_editors(); _save_default_environment(); _set_scene_metadata(p_file, idx); Ref sdata; if (ResourceCache::has(p_file)) { // something may be referencing this resource and we are good with that. // we must update it, but also let the previous scene state go, as // old version still work for referencing changes in instanced or inherited scenes sdata = Ref(Object::cast_to(ResourceCache::get(p_file))); if (sdata.is_valid()) sdata->recreate_state(); else sdata.instance(); } else { sdata.instance(); } Error err = sdata->pack(scene); if (err != OK) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); accept->set_text(TTR("Couldn't save scene. Likely dependencies (instances or inheritance) couldn't be satisfied.")); accept->popup_centered_minsize(); return; } // force creation of node path cache // (hacky but needed for the tree to update properly) Node *dummy_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE); if (!dummy_scene) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); accept->set_text(TTR("Couldn't save scene. Likely dependencies (instances or inheritance) couldn't be satisfied.")); accept->popup_centered_minsize(); return; } memdelete(dummy_scene); int flg = 0; if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) flg |= ResourceSaver::FLAG_COMPRESS; flg |= ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS; err = ResourceSaver::save(p_file, sdata, flg); Map processed; _save_edited_subresources(scene, processed, flg); editor_data.save_editor_external_data(); if (err == OK) { scene->set_filename(ProjectSettings::get_singleton()->localize_path(p_file)); if (idx < 0 || idx == editor_data.get_edited_scene()) set_current_version(editor_data.get_undo_redo().get_version()); else editor_data.set_edited_scene_version(0, idx); _update_title(); _update_scene_tabs(); } else { _dialog_display_save_error(p_file, err); } } void EditorNode::_save_all_scenes() { int i = _next_unsaved_scene(true, 0); while (i != -1) { Node *scene = editor_data.get_edited_scene_root(i); if (scene && scene->get_filename() != "") { if (i != editor_data.get_edited_scene()) _save_scene(scene->get_filename(), i); else _save_scene_with_preview(scene->get_filename()); } // else: ignore new scenes i = _next_unsaved_scene(true, ++i); } _save_default_environment(); } void EditorNode::_mark_unsaved_scenes() { for (int i = 0; i < editor_data.get_edited_scene_count(); i++) { Node *node = editor_data.get_edited_scene_root(i); if (!node) continue; String path = node->get_filename(); if (!(path == String() || FileAccess::exists(path))) { node->set_filename(""); if (i == editor_data.get_edited_scene()) set_current_version(-1); else editor_data.set_edited_scene_version(-1, i); } } _update_title(); _update_scene_tabs(); } void EditorNode::_dialog_action(String p_file) { switch (current_option) { case RESOURCE_LOAD: { RES res = ResourceLoader::load(p_file); if (res.is_null()) { current_option = -1; accept->get_ok()->set_text("ok :("); accept->set_text(TTR("Failed to load resource.")); return; }; push_item(res.operator->()); } break; case FILE_NEW_INHERITED_SCENE: { load_scene(p_file, false, true); } break; case FILE_OPEN_SCENE: { load_scene(p_file); } break; case SETTINGS_PICK_MAIN_SCENE: { ProjectSettings::get_singleton()->set("application/run/main_scene", p_file); ProjectSettings::get_singleton()->save(); //would be nice to show the project manager opened with the highlighted field.. _run(false, ""); // automatically run the project } break; case FILE_CLOSE: case FILE_CLOSE_ALL_AND_QUIT: case FILE_CLOSE_ALL_AND_RUN_PROJECT_MANAGER: case SCENE_TAB_CLOSE: case FILE_SAVE_SCENE: case FILE_SAVE_AS_SCENE: { int scene_idx = (current_option == FILE_SAVE_SCENE || current_option == FILE_SAVE_AS_SCENE) ? -1 : tab_closing; if (file->get_mode() == EditorFileDialog::MODE_SAVE_FILE) { _save_default_environment(); _save_scene_with_preview(p_file, scene_idx); if (scene_idx != -1) _discard_changes(); } } break; case FILE_SAVE_AND_RUN: { if (file->get_mode() == EditorFileDialog::MODE_SAVE_FILE) { _save_default_environment(); _save_scene_with_preview(p_file); _run(false, p_file); } } break; case FILE_EXPORT_MESH_LIBRARY: { Ref ml; if (file_export_lib_merge->is_pressed() && FileAccess::exists(p_file)) { ml = ResourceLoader::load(p_file, "MeshLibrary"); if (ml.is_null()) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); accept->set_text(TTR("Can't load MeshLibrary for merging!")); accept->popup_centered_minsize(); return; } } if (ml.is_null()) { ml = Ref(memnew(MeshLibrary)); } MeshLibraryEditor::update_library_file(editor_data.get_edited_scene_root(), ml, true); Error err = ResourceSaver::save(p_file, ml); if (err) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); accept->set_text(TTR("Error saving MeshLibrary!")); accept->popup_centered_minsize(); return; } } break; case FILE_EXPORT_TILESET: { Ref ml; if (FileAccess::exists(p_file) && file_export_lib_merge->is_pressed()) { ml = ResourceLoader::load(p_file, "TileSet"); if (ml.is_null()) { if (file_export_lib_merge->is_pressed()) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); accept->set_text(TTR("Can't load TileSet for merging!")); accept->popup_centered_minsize(); return; } } else if (!file_export_lib_merge->is_pressed()) { ml->clear(); } } else { ml = Ref(memnew(TileSet)); } TileSetEditor::update_library_file(editor_data.get_edited_scene_root(), ml, true); Error err = ResourceSaver::save(p_file, ml); if (err) { current_option = -1; accept->get_ok()->set_text(TTR("I see..")); accept->set_text(TTR("Error saving TileSet!")); accept->popup_centered_minsize(); return; } } break; case RESOURCE_SAVE: case RESOURCE_SAVE_AS: { uint32_t current = editor_history.get_current(); Object *current_obj = current > 0 ? ObjectDB::get_instance(current) : NULL; ERR_FAIL_COND(!Object::cast_to(current_obj)) RES current_res = RES(Object::cast_to(current_obj)); save_resource_in_path(current_res, p_file); } break; case SETTINGS_LAYOUT_SAVE: { if (p_file.empty()) return; Ref config; config.instance(); Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config()); if (err == ERR_CANT_OPEN) { config.instance(); // new config } else if (err != OK) { show_warning(TTR("Error trying to save layout!")); return; } _save_docks_to_config(config, p_file); config->save(EditorSettings::get_singleton()->get_editor_layouts_config()); layout_dialog->hide(); _update_layouts_menu(); if (p_file == "Default") { show_warning(TTR("Default editor layout overridden.")); } } break; case SETTINGS_LAYOUT_DELETE: { if (p_file.empty()) return; Ref config; config.instance(); Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config()); if (err != OK || !config->has_section(p_file)) { show_warning(TTR("Layout name not found!")); return; } // erase List keys; config->get_section_keys(p_file, &keys); for (List::Element *E = keys.front(); E; E = E->next()) { config->set_value(p_file, E->get(), Variant()); } config->save(EditorSettings::get_singleton()->get_editor_layouts_config()); layout_dialog->hide(); _update_layouts_menu(); if (p_file == "Default") { show_warning(TTR("Restored default layout to base settings.")); } } break; default: { //save scene? if (file->get_mode() == EditorFileDialog::MODE_SAVE_FILE) { _save_scene_with_preview(p_file); } } break; } } void EditorNode::push_item(Object *p_object, const String &p_property) { if (!p_object) { property_editor->edit(NULL); node_dock->set_node(NULL); scene_tree_dock->set_selected(NULL); return; } uint32_t id = p_object->get_instance_id(); if (id != editor_history.get_current()) { if (p_property == "") editor_history.add_object(id); else editor_history.add_object(id, p_property); } _edit_current(); } void EditorNode::_select_history(int p_idx) { //push it to the top, it is not correct, but it's more useful ObjectID id = editor_history.get_history_obj(p_idx); Object *obj = ObjectDB::get_instance(id); if (!obj) return; push_item(obj); } void EditorNode::_prepare_history() { int history_to = MAX(0, editor_history.get_history_len() - 25); editor_history_menu->get_popup()->clear(); Ref base_icon = gui_base->get_icon("Object", "EditorIcons"); Set already; for (int i = editor_history.get_history_len() - 1; i >= history_to; i--) { ObjectID id = editor_history.get_history_obj(i); Object *obj = ObjectDB::get_instance(id); if (!obj || already.has(id)) { if (history_to > 0) { history_to--; } continue; } already.insert(id); Ref icon = gui_base->get_icon("Object", "EditorIcons"); if (gui_base->has_icon(obj->get_class(), "EditorIcons")) icon = gui_base->get_icon(obj->get_class(), "EditorIcons"); else icon = base_icon; String text; if (Object::cast_to(obj)) { Resource *r = Object::cast_to(obj); if (r->get_path().is_resource_file()) text = r->get_path().get_file(); else if (r->get_name() != String()) { text = r->get_name(); } else { text = r->get_class(); } } else if (Object::cast_to(obj)) { text = Object::cast_to(obj)->get_name(); } else if (obj->is_class("ScriptEditorDebuggerInspectedObject")) { text = obj->call("get_title"); } else { text = obj->get_class(); } if (i == editor_history.get_history_pos()) { text = "[" + text + "]"; } editor_history_menu->get_popup()->add_icon_item(icon, text, i); } } void EditorNode::_property_editor_forward() { if (editor_history.next()) _edit_current(); } void EditorNode::_property_editor_back() { if (editor_history.previous() || editor_history.get_path_size() == 1) _edit_current(); } void EditorNode::_menu_collapseall() { property_editor->collapse_all_folding(); } void EditorNode::_menu_expandall() { property_editor->expand_all_folding(); } void EditorNode::_save_default_environment() { Ref fallback = get_tree()->get_root()->get_world()->get_fallback_environment(); if (fallback.is_valid() && fallback->get_path().is_resource_file()) { Map processed; _find_and_save_edited_subresources(fallback.ptr(), processed, 0); if (fallback->get_last_modified_time() != fallback->get_import_last_modified_time()) save_resource_in_path(fallback, fallback->get_path()); } } void EditorNode::_hide_top_editors() { _display_top_editors(false); editor_plugins_over->clear(); } void EditorNode::_display_top_editors(bool p_display) { editor_plugins_over->make_visible(p_display); } void EditorNode::_set_top_editors(Vector p_editor_plugins_over) { editor_plugins_over->set_plugins_list(p_editor_plugins_over); } void EditorNode::_set_editing_top_editors(Object *p_current_object) { editor_plugins_over->edit(p_current_object); } static bool overrides_external_editor(Object *p_object) { Script *script = Object::cast_to