/*************************************************************************/ /* editor_node.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2017 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 "animation_editor.h" #include "authors.gen.h" #include "bind/core_bind.h" #include "class_db.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "editor_file_system.h" #include "editor_help.h" #include "editor_settings.h" #include "editor_themes.h" #include "global_config.h" #include "io/config_file.h" #include "io/stream_peer_ssl.h" #include "io/zip_io.h" #include "main/input_default.h" #include "message_queue.h" #include "os/file_access.h" #include "os/input.h" #include "os/keyboard.h" #include "os/os.h" #include "path_remap.h" #include "print_string.h" #include "pvrtc_compress.h" #include "register_exporters.h" #include "scene/resources/packed_scene.h" #include "servers/physics_2d_server.h" #include "translation.h" #include "version.h" #include // plugins #include "asset_library_editor_plugin.h" #include "import/resource_importer_csv_translation.h" #include "import/resource_importer_obj.h" #include "import/resource_importer_scene.h" #include "import/resource_importer_texture.h" #include "import/resource_importer_wav.h" #include "plugins/animation_player_editor_plugin.h" #include "plugins/animation_tree_editor_plugin.h" #include "plugins/baked_light_editor_plugin.h" #include "plugins/camera_editor_plugin.h" #include "plugins/canvas_item_editor_plugin.h" #include "plugins/collision_polygon_2d_editor_plugin.h" #include "plugins/collision_polygon_editor_plugin.h" #include "plugins/collision_shape_2d_editor_plugin.h" #include "plugins/cube_grid_theme_editor_plugin.h" #include "plugins/curve_editor_plugin.h" #include "plugins/gi_probe_editor_plugin.h" #include "plugins/gradient_editor_plugin.h" #include "plugins/item_list_editor_plugin.h" #include "plugins/light_occluder_2d_editor_plugin.h" #include "plugins/line_2d_editor_plugin.h" #include "plugins/material_editor_plugin.h" #include "plugins/mesh_editor_plugin.h" #include "plugins/mesh_instance_editor_plugin.h" #include "plugins/multimesh_editor_plugin.h" #include "plugins/navigation_polygon_editor_plugin.h" #include "plugins/particles_2d_editor_plugin.h" #include "plugins/particles_editor_plugin.h" #include "plugins/path_2d_editor_plugin.h" #include "plugins/path_editor_plugin.h" #include "plugins/polygon_2d_editor_plugin.h" #include "plugins/resource_preloader_editor_plugin.h" #include "plugins/rich_text_editor_plugin.h" #include "plugins/sample_editor_plugin.h" #include "plugins/sample_library_editor_plugin.h" #include "plugins/sample_player_editor_plugin.h" #include "plugins/script_editor_plugin.h" #include "plugins/script_text_editor.h" #include "plugins/shader_editor_plugin.h" #include "plugins/shader_graph_editor_plugin.h" #include "plugins/spatial_editor_plugin.h" #include "plugins/sprite_frames_editor_plugin.h" #include "plugins/stream_editor_plugin.h" #include "plugins/style_box_editor_plugin.h" #include "plugins/texture_editor_plugin.h" #include "plugins/texture_region_editor_plugin.h" #include "plugins/theme_editor_plugin.h" #include "plugins/tile_map_editor_plugin.h" #include "plugins/tile_set_editor_plugin.h" // end #include "editor_settings.h" #include "import/editor_import_collada.h" #include "io_plugins/editor_bitmask_import_plugin.h" #include "io_plugins/editor_export_scene.h" #include "io_plugins/editor_font_import_plugin.h" #include "io_plugins/editor_mesh_import_plugin.h" #include "io_plugins/editor_sample_import_plugin.h" #include "io_plugins/editor_scene_import_plugin.h" #include "io_plugins/editor_scene_importer_fbxconv.h" #include "io_plugins/editor_texture_import_plugin.h" #include "io_plugins/editor_translation_import_plugin.h" #include "editor_audio_buses.h" #include "editor_initialize_ssl.h" #include "plugins/editor_preview_plugins.h" #include "script_editor_debugger.h" EditorNode *EditorNode::singleton = NULL; void EditorNode::_update_scene_tabs() { bool show_rb = EditorSettings::get_singleton()->get("interface/show_script_in_scene_tabs"); 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()); scene_tabs->ensure_tab_visible(editor_data.get_edited_scene()); } void EditorNode::_update_title() { String appname = GlobalConfig::get_singleton()->get("application/name"); String title = appname.empty() ? String(VERSION_FULL_NAME) : String(_MKSTR(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()) { 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(); } } } 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) { //force the whole tree viewport #if 0 { Rect2 grect = scene_root_base->get_global_rect(); Rect2 grectsrp = scene_root_parent->get_global_rect(); if (grect!=grectsrp) { scene_root_parent->set_position(grect.pos); scene_root_parent->set_size(grect.size); } } #endif 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(); } //get_root_node()->set_rect(viewport->get_global_rect()); //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(GlobalConfig::get_singleton()->get("display/window/width"), GlobalConfig::get_singleton()->get("display/window/height"))); ResourceImporterTexture::get_singleton()->update_imports(); } if (p_what == NOTIFICATION_ENTER_TREE) { get_tree()->get_root()->set_disable_3d(true); //MessageQueue::get_singleton()->push_call(this,"_get_scene_metadata"); get_tree()->set_editor_hint(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"); //VisualServer::get_singleton()->viewport_set_hide_canvas(editor->get_scene_root()->get_viewport(),false); //import_monitor->scan_changes(); } 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(); /* if (defer_optimize!="") { Error ok = save_optimized_copy(defer_optimize,defer_optimize_preset); defer_optimize_preset=""; if (ok!=OK) OS::get_singleton()->set_exit_code(255); get_scene()->quit(); } */ /* // moved to "_sources_changed" if (export_defer.platform!="") { project_export_settings->export_platform(export_defer.platform,export_defer.path,export_defer.debug,export_defer.password,true); export_defer.platform=""; } */ } 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/always_show_close_button_in_scene_tabs", false)) ? Tabs::CLOSE_BUTTON_SHOW_ALWAYS : Tabs::CLOSE_BUTTON_SHOW_ACTIVE_ONLY)); property_editor->set_enable_capitalize_paths(bool(EDITOR_DEF("interface/capitalize_properties", true))); Ref theme = create_editor_theme(); theme_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("Content", "EditorStyles")); 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")); 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(); } } 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(); } if (export_defer.platform != "") { //project_export_settings->export_platform(export_defer.platform,export_defer.path,export_defer.debug,export_defer.password,true); export_defer.platform = ""; } { //reload changed resources List > changed; List > cached; ResourceCache::get_cached_resources(&cached); //this should probably 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()) { //imported resource uint64_t mt = FileAccess::get_modified_time(E->get()->get_import_path()); print_line("testing modified: " + E->get()->get_import_path() + " " + itos(mt) + " vs " + itos(E->get()->get_import_last_modified_time())); if (mt != E->get()->get_import_last_modified_time()) { print_line("success"); changed.push_back(E->get()); } } else { 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()) { //EditorProgress ep("reload_res","Reload Modified Resources",changed.size()); int idx = 0; for (List >::Element *E = changed.front(); E; E = E->next()) { //ep.step(E->get()->get_path(),idx++); E->get()->reload_from_file(); } } } } void EditorNode::_sources_changed(bool p_exist) { if (waiting_for_first_scan) { if (defer_load_scene != "") { print_line("loading scene DEFERRED"); 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->set_current_path(current_path); 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; /* if (EditorSettings::get_singleton()->get("filesystem/on_save/save_paths_as_relative")) flg|=ResourceSaver::FLAG_RELATIVE_PATHS; */ String path = GlobalConfig::get_singleton()->localize_path(p_path); Error err = ResourceSaver::save(path, p_resource, flg | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS); if (err != OK) { accept->set_text(TTR("Error saving resource!")); accept->popup_centered_minsize(); return; } //EditorFileSystem::get_singleton()->update_file(path,p_resource->get_type()); ((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]); } //file->set_current_path(current_path); 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_file_error(String p_file, Error p_error) { if (p_error) { current_option = -1; //accept->"()->hide(); 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::_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_path().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_path().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 res, Map &processed, int32_t flags) { if (res.is_null()) return false; if (processed.has(res)) { return processed[res]; } bool changed = res->is_edited(); res->set_edited(false); bool subchanged = _find_and_save_edited_subresources(res.ptr(), processed, flags); //print_line("checking if edited: "+res->get_type()+" :: "+res->get_name()+" :: "+res->get_path()+" :: "+itos(changed)+" :: SR "+itos(subchanged)); if (res->get_path().is_resource_file()) { if (changed || subchanged) { //save print_line("Also saving modified external resource: " + res->get_path()); ResourceSaver::save(res->get_path(), res, flags); } processed[res] = false; //because it's a file return false; } else { processed[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; //_find_resources(v); } } 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 c2d = 0; int c3d = 0; EditorProgress save("save", TTR("Saving Scene"), 4); save.step(TTR("Analyzing"), 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; int width, height; if (img->get_width() > preview_size && img->get_width() >= img->get_height()) { width = preview_size; height = img->get_height() * preview_size / img->get_width(); } else if (img->get_height() > preview_size && img->get_height() >= img->get_width()) { height = preview_size; width = img->get_width() * preview_size / img->get_height(); } else { width = img->get_width(); height = img->get_height(); } img->convert(Image::FORMAT_RGB8); img->resize(width, height); 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_settings_path().plus_file("tmp"); String cache_base = GlobalConfig::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"; img->save_png(file); } save.step(TTR("Saving Scene"), 4); _save_scene(p_file); 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_cancel()->hide(); accept->get_ok()->set_text(TTR("I see..")); accept->set_text("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(ResourceCache::get(p_file)->cast_to()); 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_cancel()->hide(); accept->get_ok()->set_text(TTR("I see..")); accept->set_text(TTR("Couldn't save scene. Likely dependencies (instances) 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); memdelete(dummy_scene); int flg = 0; if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) flg |= ResourceSaver::FLAG_COMPRESS; /* if (EditorSettings::get_singleton()->get("filesystem/on_save/save_paths_as_relative")) flg|=ResourceSaver::FLAG_RELATIVE_PATHS; */ 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(GlobalConfig::get_singleton()->localize_path(p_file)); //EditorFileSystem::get_singleton()->update_file(p_file,sdata->get_type()); 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_file_error(p_file, err); } } void EditorNode::_save_all_scenes() { for (int i = 0; i < editor_data.get_edited_scene_count(); i++) { Node *scene = editor_data.get_edited_scene_root(i); if (scene && scene->get_filename() != "") { // save in background if in the script editor if (i != editor_data.get_edited_scene() || _get_current_main_editor() == EDITOR_SCRIPT) { _save_scene(scene->get_filename(), i); } else { _save_scene_with_preview(scene->get_filename()); } } // else: ignore new scenes } _save_default_environment(); } void EditorNode::_import_action(const String &p_action) { #if 0 import_confirmation->hide(); if (p_action=="re-import") { _import(_tmp_import_path); } if (p_action=="update") { Node *src = EditorImport::import_scene(_tmp_import_path); if (!src) { current_option=-1; //accept->get_cancel()->hide(); accept->get_ok()->set_text("Ugh"); accept->set_text("Error importing scene."); accept->popup_centered(Size2(300,70)); return; } //as soon as the scene is imported, version hashes must be generated for comparison against saved scene EditorImport::generate_version_hashes(src); Node *dst = SceneLoader::load(editor_data.get_imported_scene(GlobalConfig::get_singleton()->localize_path(_tmp_import_path))); if (!dst) { memdelete(src); //accept->get_cancel()->hide(); accept->get_ok()->set_text("Ugh"); accept->set_text("Error load scene to update."); accept->popup_centered(Size2(300,70)); return; } List conflicts; EditorImport::check_conflicts(src,dst,&conflicts); bool conflicted=false; for (List::Element *E=conflicts.front();E;E=E->next()) { if (E->get().status==EditorImport::Conflict::STATUS_CONFLICT) { conflicted=true; break; } } if (conflicted) { import_conflicts_dialog->popup(src,dst,conflicts); return; } _import_with_conflicts(src,dst,conflicts); //not conflicted, just reimport! } #endif } void EditorNode::_import(const String &p_file) { #if 0 Node *new_scene = EditorImport::import_scene(p_file); if (!new_scene) { current_option=-1; //accept->get_cancel()->hide(); accept->get_ok()->set_text("Ugh"); accept->set_text("Error importing scene."); accept->popup_centered(Size2(300,70)); return; } //as soon as the scene is imported, version hashes must be generated for comparison against saved scene EditorImport::generate_version_hashes(new_scene); Node *old_scene = edited_scene; _hide_top_editors(); set_edited_scene(NULL); editor_data.clear_editor_states(); if (old_scene) { memdelete(old_scene); } set_edited_scene(new_scene); scene_tree_dock->set_selected(new_scene); //_get_scene_metadata(); editor_data.get_undo_redo().clear_history(); saved_version=editor_data.get_undo_redo().get_version(); _update_title(); #endif } 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_cancel()->hide(); 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: { GlobalConfig::get_singleton()->set("application/main_scene", p_file); GlobalConfig::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_SAVE_OPTIMIZED: { } break; case FILE_RUN_SCRIPT: { Ref