diff options
Diffstat (limited to 'editor')
33 files changed, 533 insertions, 1898 deletions
diff --git a/editor/editor_help.cpp b/editor/editor_help.cpp index b17f105507..c049db8ef6 100644 --- a/editor/editor_help.cpp +++ b/editor/editor_help.cpp @@ -1922,7 +1922,7 @@ void FindBar::_update_results_count() { return; } - String full_text = rich_text_label->get_text(); + String full_text = rich_text_label->get_parsed_text(); int from_pos = 0; diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 44825d2536..a8fdca1b20 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -49,6 +49,7 @@ #include "core/version.h" #include "core/version_hash.gen.h" #include "main/main.h" +#include "scene/3d/importer_mesh_instance_3d.h" #include "scene/gui/center_container.h" #include "scene/gui/control.h" #include "scene/gui/dialogs.h" @@ -113,7 +114,6 @@ #include "editor/import/resource_importer_texture_atlas.h" #include "editor/import/resource_importer_wav.h" #include "editor/import/scene_import_settings.h" -#include "editor/import/scene_importer_mesh_node_3d.h" #include "editor/import_dock.h" #include "editor/multi_node_edit.h" #include "editor/node_dock.h" @@ -3852,8 +3852,6 @@ void EditorNode::register_editor_types() { GDREGISTER_CLASS(EditorSpinSlider); GDREGISTER_CLASS(EditorResourcePicker); GDREGISTER_CLASS(EditorScriptPicker); - GDREGISTER_CLASS(EditorSceneImporterMesh); - GDREGISTER_CLASS(EditorSceneImporterMeshNode3D); GDREGISTER_VIRTUAL_CLASS(FileSystemDock); @@ -6221,12 +6219,12 @@ EditorNode::EditorNode() { scene_tabs->set_min_width(int(EDITOR_DEF("interface/scene_tabs/minimum_width", 50)) * EDSCALE); scene_tabs->set_drag_to_rearrange_enabled(true); scene_tabs->connect("tab_changed", callable_mp(this, &EditorNode::_scene_tab_changed)); - scene_tabs->connect("right_button_pressed", callable_mp(this, &EditorNode::_scene_tab_script_edited)); + scene_tabs->connect("tab_rmb_clicked", callable_mp(this, &EditorNode::_scene_tab_script_edited)); scene_tabs->connect("tab_closed", callable_mp(this, &EditorNode::_scene_tab_closed), varray(SCENE_TAB_CLOSE)); scene_tabs->connect("tab_hovered", callable_mp(this, &EditorNode::_scene_tab_hovered)); scene_tabs->connect("mouse_exited", callable_mp(this, &EditorNode::_scene_tab_exit)); scene_tabs->connect("gui_input", callable_mp(this, &EditorNode::_scene_tab_input)); - scene_tabs->connect("reposition_active_tab_request", callable_mp(this, &EditorNode::_reposition_active_tab)); + scene_tabs->connect("active_tab_rearranged", callable_mp(this, &EditorNode::_reposition_active_tab)); scene_tabs->connect("resized", callable_mp(this, &EditorNode::_update_scene_tabs)); tabbar_container = memnew(HBoxContainer); diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index c0dadc4484..2d4a3ac788 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -3574,7 +3574,7 @@ EditorProperty *EditorInspectorDefaultPlugin::get_editor_for_property(Object *p_ case Variant::PACKED_COLOR_ARRAY: { EditorPropertyArray *editor = memnew(EditorPropertyArray); editor->setup(Variant::PACKED_COLOR_ARRAY); - + return editor; } break; default: { } diff --git a/editor/editor_properties_array_dict.cpp b/editor/editor_properties_array_dict.cpp index 9cecb62c66..9b5dc8851c 100644 --- a/editor/editor_properties_array_dict.cpp +++ b/editor/editor_properties_array_dict.cpp @@ -429,6 +429,12 @@ void EditorPropertyArray::_button_draw() { bool EditorPropertyArray::_is_drop_valid(const Dictionary &p_drag_data) const { String allowed_type = Variant::get_type_name(subtype); + // When the subtype is of type Object, an additional subtype may be specified in the hint string + // (e.g. Resource, Texture2D, ShaderMaterial, etc). We want the allowed type to be that, not just "Object". + if (subtype == Variant::OBJECT && subtype_hint_string != "") { + allowed_type = subtype_hint_string; + } + Dictionary drag_data = p_drag_data; if (drag_data.has("type") && String(drag_data["type"]) == "files") { diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 25406055f2..43d458c58e 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -73,14 +73,15 @@ bool EditorSettings::_set_only(const StringName &p_name, const Variant &p_value) if (p_name == "shortcuts") { Array arr = p_value; - ERR_FAIL_COND_V(arr.size() && arr.size() & 1, true); - for (int i = 0; i < arr.size(); i += 2) { - String name = arr[i]; - Ref<InputEvent> shortcut = arr[i + 1]; + for (int i = 0; i < arr.size(); i++) { + Dictionary dict = arr[i]; + String name = dict["name"]; + + Array shortcut_events = dict["shortcuts"]; Ref<Shortcut> sc; sc.instantiate(); - sc->set_event(shortcut); + sc->set_events(shortcut_events); add_shortcut(name, sc); } @@ -138,11 +139,11 @@ bool EditorSettings::_get(const StringName &p_name, Variant &r_ret) const { _THREAD_SAFE_METHOD_ if (p_name == "shortcuts") { - Array arr; - for (const KeyValue<String, Ref<Shortcut>> &E : shortcuts) { - Ref<Shortcut> sc = E.value; + Array save_array; + for (const KeyValue<String, Ref<Shortcut>> &shortcut_definition : shortcuts) { + Ref<Shortcut> sc = shortcut_definition.value; - if (builtin_action_overrides.has(E.key)) { + if (builtin_action_overrides.has(shortcut_definition.key)) { // This shortcut was auto-generated from built in actions: don't save. continue; } @@ -151,34 +152,57 @@ bool EditorSettings::_get(const StringName &p_name, Variant &r_ret) const { if (!sc->has_meta("original")) { continue; //this came from settings but is not any longer used } + } - Ref<InputEvent> original = sc->get_meta("original"); - if (sc->matches_event(original) || (original.is_null() && sc->get_event().is_null())) { - continue; //not changed from default, don't save - } + Array original_events = sc->get_meta("original"); + Array shortcut_events = sc->get_events(); + + bool is_same = Shortcut::is_event_array_equal(original_events, shortcut_events); + if (is_same) { + continue; // Not changed from default; don't save. } - arr.push_back(E.key); - arr.push_back(sc->get_event()); + Dictionary dict; + dict["name"] = shortcut_definition.key; + dict["shortcuts"] = shortcut_events; + + save_array.push_back(dict); } - r_ret = arr; + r_ret = save_array; return true; } else if (p_name == "builtin_action_overrides") { Array actions_arr; - for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtin_action_overrides) { - List<Ref<InputEvent>> events = E.value; + for (const KeyValue<String, List<Ref<InputEvent>>> &action_override : builtin_action_overrides) { + List<Ref<InputEvent>> events = action_override.value; - // TODO: skip actions which are the same as the builtin. Dictionary action_dict; - action_dict["name"] = E.key; + action_dict["name"] = action_override.key; + // Convert the list to an array, and only keep key events as this is for the editor. Array events_arr; - for (List<Ref<InputEvent>>::Element *I = events.front(); I; I = I->next()) { - events_arr.push_back(I->get()); + for (const Ref<InputEvent> &ie : events) { + Ref<InputEventKey> iek = ie; + if (iek.is_valid()) { + events_arr.append(iek); + } } - action_dict["events"] = events_arr; + Array defaults_arr; + List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins()[action_override.key]; + for (const Ref<InputEvent> &default_input_event : defaults) { + if (default_input_event.is_valid()) { + defaults_arr.append(default_input_event); + } + } + + bool same = Shortcut::is_event_array_equal(events_arr, defaults_arr); + // Don't save if same as default. + if (same) { + continue; + } + + action_dict["events"] = events_arr; actions_arr.push_back(action_dict); } @@ -456,6 +480,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { // SceneTree _initial_set("docks/scene_tree/start_create_dialog_fully_expanded", false); + _initial_set("docks/scene_tree/auto_expand_to_selected", true); // FileSystem EDITOR_SETTING(Variant::INT, PROPERTY_HINT_RANGE, "docks/filesystem/thumbnail_size", 64, "32,128,16") @@ -1405,7 +1430,7 @@ Ref<Shortcut> EditorSettings::get_shortcut(const String &p_name) const { const Map<String, List<Ref<InputEvent>>>::Element *builtin_override = builtin_action_overrides.find(p_name); if (builtin_override) { sc.instantiate(); - sc->set_event(builtin_override->get().front()->get()); + sc->set_events_list(&builtin_override->get()); sc->set_name(InputMap::get_singleton()->get_builtin_display_name(p_name)); } @@ -1414,7 +1439,7 @@ Ref<Shortcut> EditorSettings::get_shortcut(const String &p_name) const { const OrderedHashMap<String, List<Ref<InputEvent>>>::ConstElement builtin_default = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().find(p_name); if (builtin_default) { sc.instantiate(); - sc->set_event(builtin_default.get().front()->get()); + sc->set_events_list(&builtin_default.get()); sc->set_name(InputMap::get_singleton()->get_builtin_display_name(p_name)); } } @@ -1450,52 +1475,91 @@ void ED_SHORTCUT_OVERRIDE(const String &p_path, const String &p_feature, Key p_k Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path); ERR_FAIL_COND_MSG(!sc.is_valid(), "Used ED_SHORTCUT_OVERRIDE with invalid shortcut: " + p_path + "."); + PackedInt32Array arr; + arr.push_back(p_keycode); + + ED_SHORTCUT_OVERRIDE_ARRAY(p_path, p_feature, arr); +} + +void ED_SHORTCUT_OVERRIDE_ARRAY(const String &p_path, const String &p_feature, const PackedInt32Array &p_keycodes) { + Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path); + ERR_FAIL_COND_MSG(!sc.is_valid(), "Used ED_SHORTCUT_OVERRIDE_ARRAY with invalid shortcut: " + p_path + "."); + // Only add the override if the OS supports the provided feature. - if (OS::get_singleton()->has_feature(p_feature)) { - Ref<InputEventKey> ie; - if (p_keycode) { - ie = InputEventKey::create_reference(p_keycode); + if (!OS::get_singleton()->has_feature(p_feature)) { + return; + } + + Array events; + + for (int i = 0; i < p_keycodes.size(); i++) { + Key keycode = (Key)p_keycodes[i]; + +#ifdef OSX_ENABLED + // Use Cmd+Backspace as a general replacement for Delete shortcuts on macOS + if (keycode == KEY_DELETE) { + keycode = KEY_MASK_CMD | KEY_BACKSPACE; } +#endif - // Directly override the existing shortcut. - sc->set_event(ie); - sc->set_meta("original", ie); + Ref<InputEventKey> ie; + if (keycode) { + ie = InputEventKey::create_reference(keycode); + events.push_back(ie); + } } + + // Directly override the existing shortcut. + sc->set_events(events); + sc->set_meta("original", events); } Ref<Shortcut> ED_SHORTCUT(const String &p_path, const String &p_name, Key p_keycode) { + PackedInt32Array arr; + arr.push_back(p_keycode); + return ED_SHORTCUT_ARRAY(p_path, p_name, arr); +} + +Ref<Shortcut> ED_SHORTCUT_ARRAY(const String &p_path, const String &p_name, const PackedInt32Array &p_keycodes) { + Array events; + + for (int i = 0; i < p_keycodes.size(); i++) { + Key keycode = (Key)p_keycodes[i]; + #ifdef OSX_ENABLED - // Use Cmd+Backspace as a general replacement for Delete shortcuts on macOS - if (p_keycode == KEY_DELETE) { - p_keycode = KEY_MASK_CMD | KEY_BACKSPACE; - } + // Use Cmd+Backspace as a general replacement for Delete shortcuts on macOS + if (keycode == KEY_DELETE) { + keycode = KEY_MASK_CMD | KEY_BACKSPACE; + } #endif - Ref<InputEventKey> ie; - if (p_keycode) { - ie = InputEventKey::create_reference(p_keycode); + Ref<InputEventKey> ie; + if (keycode) { + ie = InputEventKey::create_reference(keycode); + events.push_back(ie); + } } if (!EditorSettings::get_singleton()) { Ref<Shortcut> sc; sc.instantiate(); sc->set_name(p_name); - sc->set_event(ie); - sc->set_meta("original", ie); + sc->set_events(events); + sc->set_meta("original", events); return sc; } Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path); if (sc.is_valid()) { sc->set_name(p_name); //keep name (the ones that come from disk have no name) - sc->set_meta("original", ie); //to compare against changes + sc->set_meta("original", events); //to compare against changes return sc; } sc.instantiate(); sc->set_name(p_name); - sc->set_event(ie); - sc->set_meta("original", ie); //to compare against changes + sc->set_events(events); + sc->set_meta("original", events); //to compare against changes EditorSettings::get_singleton()->add_shortcut(p_path, sc); return sc; @@ -1550,7 +1614,7 @@ void EditorSettings::set_builtin_action_override(const String &p_name, const Arr // Update the shortcut (if it is used somewhere in the editor) to be the first event of the new list. if (shortcuts.has(p_name)) { - shortcuts[p_name]->set_event(event_list.front()->get()); + shortcuts[p_name]->set_events_list(&event_list); } } diff --git a/editor/editor_settings.h b/editor/editor_settings.h index 9067539e29..04e227bc5c 100644 --- a/editor/editor_settings.h +++ b/editor/editor_settings.h @@ -201,7 +201,9 @@ Variant _EDITOR_GET(const String &p_setting); #define ED_IS_SHORTCUT(p_name, p_ev) (EditorSettings::get_singleton()->is_shortcut(p_name, p_ev)) Ref<Shortcut> ED_SHORTCUT(const String &p_path, const String &p_name, Key p_keycode = KEY_NONE); +Ref<Shortcut> ED_SHORTCUT_ARRAY(const String &p_path, const String &p_name, const PackedInt32Array &p_keycodes); void ED_SHORTCUT_OVERRIDE(const String &p_path, const String &p_feature, Key p_keycode = KEY_NONE); +void ED_SHORTCUT_OVERRIDE_ARRAY(const String &p_path, const String &p_feature, const PackedInt32Array &p_keycodes); Ref<Shortcut> ED_GET_SHORTCUT(const String &p_path); #endif // EDITOR_SETTINGS_H diff --git a/editor/editor_spin_slider.cpp b/editor/editor_spin_slider.cpp index 1890814da9..ec90af1bcc 100644 --- a/editor/editor_spin_slider.cpp +++ b/editor/editor_spin_slider.cpp @@ -330,9 +330,8 @@ void EditorSpinSlider::_draw_spin_slider() { float text_start = rtl ? Math::round(sb->get_offset().x) : Math::round(sb->get_offset().x + label_width + sep); Vector2 text_ofs = rtl ? Vector2(text_start + (number_width - TS->shaped_text_get_width(num_rid)), vofs) : Vector2(text_start, vofs); - const Vector<TextServer::Glyph> visual = TS->shaped_text_get_glyphs(num_rid); - int v_size = visual.size(); - const TextServer::Glyph *glyphs = visual.ptr(); + int v_size = TS->shaped_text_get_glyph_count(num_rid); + const Glyph *glyphs = TS->shaped_text_get_glyphs(num_rid); for (int i = 0; i < v_size; i++) { for (int j = 0; j < glyphs[i].repeat; j++) { if (text_ofs.x >= text_start && (text_ofs.x + glyphs[i].advance) <= (text_start + number_width)) { diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index de1d7b5706..6efbcbc61e 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -292,7 +292,8 @@ void editor_register_and_generate_icons(Ref<Theme> p_theme, bool p_dark_theme = Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) { Ref<Theme> theme = Ref<Theme>(memnew(Theme)); - const float default_contrast = 0.3; + // Controls may rely on the scale for their internal drawing logic. + theme->set_default_theme_base_scale(EDSCALE); // Theme settings Color accent_color = EDITOR_GET("interface/theme/accent_color"); @@ -310,6 +311,8 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) { Color preset_base_color; float preset_contrast = 0; + const float default_contrast = 0.3; + // Please use alphabetical order if you're adding a new theme here // (after "Custom") @@ -962,8 +965,8 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) { theme->set_stylebox("SceneTabFG", "EditorStyles", style_tab_selected); theme->set_stylebox("SceneTabBG", "EditorStyles", style_tab_unselected); theme->set_icon("close", "Tabs", theme->get_icon("GuiClose", "EditorIcons")); - theme->set_stylebox("button_pressed", "Tabs", style_menu); - theme->set_stylebox("button", "Tabs", style_menu); + theme->set_stylebox("close_bg_pressed", "Tabs", style_menu); + theme->set_stylebox("close_bg_highlight", "Tabs", style_menu); theme->set_icon("increment", "TabContainer", theme->get_icon("GuiScrollArrowRight", "EditorIcons")); theme->set_icon("decrement", "TabContainer", theme->get_icon("GuiScrollArrowLeft", "EditorIcons")); theme->set_icon("increment", "Tabs", theme->get_icon("GuiScrollArrowRight", "EditorIcons")); diff --git a/editor/import/dynamicfont_import_settings.cpp b/editor/import/dynamicfont_import_settings.cpp index 9a8abfa5c6..474c9d5296 100644 --- a/editor/import/dynamicfont_import_settings.cpp +++ b/editor/import/dynamicfont_import_settings.cpp @@ -1011,9 +1011,10 @@ void DynamicFontImportSettings::_glyph_text_selected() { if (text_rid.is_valid()) { TS->shaped_text_add_string(text_rid, text_edit->get_text(), font_main->get_rids(), 16, ftrs, text_edit->get_language()); TS->shaped_text_shape(text_rid); - const Vector<TextServer::Glyph> &gl = TS->shaped_text_get_glyphs(text_rid); + const Glyph *gl = TS->shaped_text_get_glyphs(text_rid); + const int gl_size = TS->shaped_text_get_glyph_count(text_rid); - for (int i = 0; i < gl.size(); i++) { + for (int i = 0; i < gl_size; i++) { if (gl[i].font_rid.is_valid() && gl[i].index != 0) { selected_glyphs.insert(gl[i].index); } diff --git a/editor/import/editor_import_collada.cpp b/editor/import/editor_import_collada.cpp index 3de7426302..4b01595028 100644 --- a/editor/import/editor_import_collada.cpp +++ b/editor/import/editor_import_collada.cpp @@ -33,8 +33,8 @@ #include "core/os/os.h" #include "editor/editor_node.h" #include "editor/import/collada.h" -#include "editor/import/scene_importer_mesh_node_3d.h" #include "scene/3d/camera_3d.h" +#include "scene/3d/importer_mesh_instance_3d.h" #include "scene/3d/light_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/node_3d.h" @@ -42,6 +42,7 @@ #include "scene/3d/skeleton_3d.h" #include "scene/animation/animation_player.h" #include "scene/resources/animation.h" +#include "scene/resources/importer_mesh.h" #include "scene/resources/packed_scene.h" #include "scene/resources/surface_tool.h" @@ -68,7 +69,7 @@ struct ColladaImport { Map<String, NodeMap> node_map; //map from collada node to engine node Map<String, String> node_name_map; //map from collada node to engine node - Map<String, Ref<EditorSceneImporterMesh>> mesh_cache; + Map<String, Ref<ImporterMesh>> mesh_cache; Map<String, Ref<Curve3D>> curve_cache; Map<String, Ref<Material>> material_cache; Map<Collada::Node *, Skeleton3D *> skeleton_map; @@ -87,7 +88,7 @@ struct ColladaImport { Error _create_scene(Collada::Node *p_node, Node3D *p_parent); Error _create_resources(Collada::Node *p_node, bool p_use_compression); Error _create_material(const String &p_target); - Error _create_mesh_surfaces(bool p_optimize, Ref<EditorSceneImporterMesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform3D &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<EditorSceneImporterMesh>> p_morph_meshes = Vector<Ref<EditorSceneImporterMesh>>(), bool p_use_compression = false, bool p_use_mesh_material = false); + Error _create_mesh_surfaces(bool p_optimize, Ref<ImporterMesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform3D &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<ImporterMesh>> p_morph_meshes = Vector<Ref<ImporterMesh>>(), bool p_use_compression = false, bool p_use_mesh_material = false); Error load(const String &p_path, int p_flags, bool p_force_make_tangents = false, bool p_use_compression = false); void _fix_param_animation_tracks(); void create_animation(int p_clip, bool p_make_tracks_in_all_bones, bool p_import_value_tracks); @@ -282,8 +283,8 @@ Error ColladaImport::_create_scene(Collada::Node *p_node, Node3D *p_parent) { node = memnew(Path3D); } else { //mesh since nothing else - node = memnew(EditorSceneImporterMeshNode3D); - //Object::cast_to<EditorSceneImporterMeshNode3D>(node)->set_flag(GeometryInstance3D::FLAG_USE_BAKED_LIGHT, true); + node = memnew(ImporterMeshInstance3D); + //Object::cast_to<ImporterMeshInstance3D>(node)->set_flag(GeometryInstance3D::FLAG_USE_BAKED_LIGHT, true); } } break; case Collada::Node::TYPE_SKELETON: { @@ -457,7 +458,7 @@ Error ColladaImport::_create_material(const String &p_target) { return OK; } -Error ColladaImport::_create_mesh_surfaces(bool p_optimize, Ref<EditorSceneImporterMesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform3D &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<EditorSceneImporterMesh>> p_morph_meshes, bool p_use_compression, bool p_use_mesh_material) { +Error ColladaImport::_create_mesh_surfaces(bool p_optimize, Ref<ImporterMesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform3D &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<ImporterMesh>> p_morph_meshes, bool p_use_compression, bool p_use_mesh_material) { bool local_xform_mirror = p_local_xform.basis.determinant() < 0; if (p_morph_data) { @@ -1087,10 +1088,10 @@ Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compres } } - if (Object::cast_to<EditorSceneImporterMeshNode3D>(node)) { + if (Object::cast_to<ImporterMeshInstance3D>(node)) { Collada::NodeGeometry *ng2 = static_cast<Collada::NodeGeometry *>(p_node); - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(node); + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(node); ERR_FAIL_COND_V(!mi, ERR_BUG); @@ -1099,7 +1100,7 @@ Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compres String meshid; Transform3D apply_xform; Vector<int> bone_remap; - Vector<Ref<EditorSceneImporterMesh>> morphs; + Vector<Ref<ImporterMesh>> morphs; if (ng2->controller) { String ngsource = ng2->source; @@ -1168,10 +1169,10 @@ Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compres for (int i = 0; i < names.size(); i++) { String meshid2 = names[i]; if (collada.state.mesh_data_map.has(meshid2)) { - Ref<EditorSceneImporterMesh> mesh = Ref<EditorSceneImporterMesh>(memnew(EditorSceneImporterMesh)); + Ref<ImporterMesh> mesh = Ref<ImporterMesh>(memnew(ImporterMesh)); const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid2]; mesh->set_name(meshdata.name); - Error err = _create_mesh_surfaces(false, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, nullptr, Vector<Ref<EditorSceneImporterMesh>>(), false); + Error err = _create_mesh_surfaces(false, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, nullptr, Vector<Ref<ImporterMesh>>(), false); ERR_FAIL_COND_V(err, err); morphs.push_back(mesh); @@ -1194,7 +1195,7 @@ Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compres meshid = ng2->source; } - Ref<EditorSceneImporterMesh> mesh; + Ref<ImporterMesh> mesh; if (mesh_cache.has(meshid)) { mesh = mesh_cache[meshid]; } else { @@ -1202,7 +1203,7 @@ Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compres //bleh, must ignore invalid ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(meshid), ERR_INVALID_DATA); - mesh = Ref<EditorSceneImporterMesh>(memnew(EditorSceneImporterMesh)); + mesh = Ref<ImporterMesh>(memnew(ImporterMesh)); const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid]; String name = meshdata.name; if (name == "") { diff --git a/editor/import/editor_importer_bake_reset.cpp b/editor/import/editor_importer_bake_reset.cpp index fb5de941ae..451a07351c 100644 --- a/editor/import/editor_importer_bake_reset.cpp +++ b/editor/import/editor_importer_bake_reset.cpp @@ -33,8 +33,8 @@ #include "core/error/error_list.h" #include "core/error/error_macros.h" #include "core/math/transform_3d.h" -#include "editor/import/scene_importer_mesh_node_3d.h" #include "resource_importer_scene.h" +#include "scene/3d/importer_mesh_instance_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/node_3d.h" #include "scene/3d/skeleton_3d.h" @@ -71,7 +71,7 @@ void BakeReset::_bake_animation_pose(Node *scene, const String &p_bake_anim) { while (!queue.is_empty()) { List<Node *>::Element *E = queue.front(); Node *node = E->get(); - EditorSceneImporterMeshNode3D *editor_mesh_3d = scene->cast_to<EditorSceneImporterMeshNode3D>(node); + ImporterMeshInstance3D *editor_mesh_3d = scene->cast_to<ImporterMeshInstance3D>(node); MeshInstance3D *mesh_3d = scene->cast_to<MeshInstance3D>(node); if (scene->cast_to<Skeleton3D>(node)) { Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node); diff --git a/editor/import/resource_importer_obj.cpp b/editor/import/resource_importer_obj.cpp index 34bc0a7d8d..4f75faedcb 100644 --- a/editor/import/resource_importer_obj.cpp +++ b/editor/import/resource_importer_obj.cpp @@ -32,10 +32,10 @@ #include "core/io/file_access.h" #include "core/io/resource_saver.h" -#include "editor/import/scene_importer_mesh.h" -#include "editor/import/scene_importer_mesh_node_3d.h" +#include "scene/3d/importer_mesh_instance_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/node_3d.h" +#include "scene/resources/importer_mesh.h" #include "scene/resources/mesh.h" #include "scene/resources/surface_tool.h" @@ -439,13 +439,13 @@ Node *EditorOBJImporter::import_scene(const String &p_path, uint32_t p_flags, in Node3D *scene = memnew(Node3D); for (const Ref<Mesh> &m : meshes) { - Ref<EditorSceneImporterMesh> mesh; + Ref<ImporterMesh> mesh; mesh.instantiate(); for (int i = 0; i < m->get_surface_count(); i++) { mesh->add_surface(m->surface_get_primitive_type(i), m->surface_get_arrays(i), Array(), Dictionary(), m->surface_get_material(i)); } - EditorSceneImporterMeshNode3D *mi = memnew(EditorSceneImporterMeshNode3D); + ImporterMeshInstance3D *mi = memnew(ImporterMeshInstance3D); mi->set_mesh(mesh); mi->set_name(m->get_name()); scene->add_child(mi); diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index 2c9bc7dadf..1e93113488 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -34,9 +34,9 @@ #include "editor/editor_node.h" #include "editor/import/editor_importer_bake_reset.h" #include "editor/import/scene_import_settings.h" -#include "editor/import/scene_importer_mesh_node_3d.h" #include "scene/3d/area_3d.h" #include "scene/3d/collision_shape_3d.h" +#include "scene/3d/importer_mesh_instance_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/navigation_region_3d.h" #include "scene/3d/physics_body_3d.h" @@ -44,6 +44,7 @@ #include "scene/animation/animation_player.h" #include "scene/resources/animation.h" #include "scene/resources/box_shape_3d.h" +#include "scene/resources/importer_mesh.h" #include "scene/resources/packed_scene.h" #include "scene/resources/resource_format_text.h" #include "scene/resources/separation_ray_shape_3d.h" @@ -233,7 +234,7 @@ static String _fixstr(const String &p_what, const String &p_str) { return what; } -static void _pre_gen_shape_list(Ref<EditorSceneImporterMesh> &mesh, Vector<Ref<Shape3D>> &r_shape_list, bool p_convex) { +static void _pre_gen_shape_list(Ref<ImporterMesh> &mesh, Vector<Ref<Shape3D>> &r_shape_list, bool p_convex) { ERR_FAIL_NULL_MSG(mesh, "Cannot generate shape list with null mesh value"); if (!p_convex) { Ref<Shape3D> shape = mesh->create_trimesh_shape(); @@ -249,7 +250,7 @@ static void _pre_gen_shape_list(Ref<EditorSceneImporterMesh> &mesh, Vector<Ref<S } } -Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<EditorSceneImporterMesh>, Vector<Ref<Shape3D>>> &collision_map) { +Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &collision_map) { // children first for (int i = 0; i < p_node->get_child_count(); i++) { Node *r = _pre_fix_node(p_node->get_child(i), p_root, collision_map); @@ -267,10 +268,10 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<E return nullptr; } - if (Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + if (Object::cast_to<ImporterMeshInstance3D>(p_node)) { + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node); - Ref<EditorSceneImporterMesh> m = mi->get_mesh(); + Ref<ImporterMesh> m = mi->get_mesh(); if (m.is_valid()) { for (int i = 0; i < m->get_surface_count(); i++) { @@ -331,9 +332,9 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<E if (isroot) { return p_node; } - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node); if (mi) { - Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); + Ref<ImporterMesh> mesh = mi->get_mesh(); if (mesh.is_valid()) { Vector<Ref<Shape3D>> shapes; @@ -398,13 +399,13 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<E colshape->set_owner(sb->get_owner()); } - } else if (_teststr(name, "rigid") && Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { + } else if (_teststr(name, "rigid") && Object::cast_to<ImporterMeshInstance3D>(p_node)) { if (isroot) { return p_node; } - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); - Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node); + Ref<ImporterMesh> mesh = mi->get_mesh(); if (mesh.is_valid()) { Vector<Ref<Shape3D>> shapes; @@ -426,10 +427,10 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<E _add_shapes(rigid_body, shapes); } - } else if ((_teststr(name, "col") || (_teststr(name, "convcol"))) && Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + } else if ((_teststr(name, "col") || (_teststr(name, "convcol"))) && Object::cast_to<ImporterMeshInstance3D>(p_node)) { + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node); - Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); + Ref<ImporterMesh> mesh = mi->get_mesh(); if (mesh.is_valid()) { Vector<Ref<Shape3D>> shapes; @@ -465,14 +466,14 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<E } } - } else if (_teststr(name, "navmesh") && Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { + } else if (_teststr(name, "navmesh") && Object::cast_to<ImporterMeshInstance3D>(p_node)) { if (isroot) { return p_node; } - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node); - Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); + Ref<ImporterMesh> mesh = mi->get_mesh(); ERR_FAIL_COND_V(mesh.is_null(), nullptr); NavigationRegion3D *nmi = memnew(NavigationRegion3D); @@ -484,12 +485,12 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<E memdelete(p_node); p_node = nmi; - } else if (Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { + } else if (Object::cast_to<ImporterMeshInstance3D>(p_node)) { //last attempt, maybe collision inside the mesh data - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node); - Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); + Ref<ImporterMesh> mesh = mi->get_mesh(); if (!mesh.is_null()) { Vector<Ref<Shape3D>> shapes; if (collision_map.has(mesh)) { @@ -517,7 +518,7 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<E return p_node; } -Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, Map<Ref<EditorSceneImporterMesh>, Vector<Ref<Shape3D>>> &collision_map, Set<Ref<EditorSceneImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps) { +Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, Map<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &collision_map, Set<Ref<ImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps) { // children first for (int i = 0; i < p_node->get_child_count(); i++) { Node *r = _post_fix_node(p_node->get_child(i), p_root, collision_map, r_scanned_meshes, p_node_data, p_material_data, p_animation_data, p_animation_fps); @@ -546,10 +547,10 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, Map<Ref< return nullptr; } - if (Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + if (Object::cast_to<ImporterMeshInstance3D>(p_node)) { + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node); - Ref<EditorSceneImporterMesh> m = mi->get_mesh(); + Ref<ImporterMesh> m = mi->get_mesh(); if (m.is_valid()) { if (!r_scanned_meshes.has(m)) { @@ -669,10 +670,10 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, Map<Ref< } //navmesh (node may have changed type above) - if (Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { - EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + if (Object::cast_to<ImporterMeshInstance3D>(p_node)) { + ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node); - Ref<EditorSceneImporterMesh> m = mi->get_mesh(); + Ref<ImporterMesh> m = mi->get_mesh(); if (m.is_valid()) { if (node_settings.has("generate/navmesh")) { @@ -1247,7 +1248,7 @@ Ref<Animation> ResourceImporterScene::import_animation_from_other_importer(Edito } void ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_mesh_data, bool p_generate_lods, bool p_create_shadow_meshes, LightBakeMode p_light_bake_mode, float p_lightmap_texel_size, const Vector<uint8_t> &p_src_lightmap_cache, Vector<Vector<uint8_t>> &r_lightmap_caches) { - EditorSceneImporterMeshNode3D *src_mesh_node = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + ImporterMeshInstance3D *src_mesh_node = Object::cast_to<ImporterMeshInstance3D>(p_node); if (src_mesh_node) { //is mesh MeshInstance3D *mesh_node = memnew(MeshInstance3D); @@ -1452,7 +1453,7 @@ Node *ResourceImporterScene::pre_import(const String &p_source_file) { return nullptr; } - Map<Ref<EditorSceneImporterMesh>, Vector<Ref<Shape3D>>> collision_map; + Map<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> collision_map; _pre_fix_node(scene, scene, collision_map); @@ -1527,8 +1528,8 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p animation_data = subresources["animations"]; } - Set<Ref<EditorSceneImporterMesh>> scanned_meshes; - Map<Ref<EditorSceneImporterMesh>, Vector<Ref<Shape3D>>> collision_map; + Set<Ref<ImporterMesh>> scanned_meshes; + Map<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> collision_map; _pre_fix_node(scene, scene, collision_map); _post_fix_node(scene, scene, collision_map, scanned_meshes, node_data, material_data, animation_data, fps); diff --git a/editor/import/resource_importer_scene.h b/editor/import/resource_importer_scene.h index e232b715be..2a67fa9aae 100644 --- a/editor/import/resource_importer_scene.h +++ b/editor/import/resource_importer_scene.h @@ -41,7 +41,7 @@ class Material; class AnimationPlayer; -class EditorSceneImporterMesh; +class ImporterMesh; class EditorSceneImporter : public RefCounted { GDCLASS(EditorSceneImporter, RefCounted); @@ -181,8 +181,8 @@ public: // Import scenes *after* everything else (such as textures). virtual int get_import_order() const override { return ResourceImporter::IMPORT_ORDER_SCENE; } - Node *_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<EditorSceneImporterMesh>, Vector<Ref<Shape3D>>> &collision_map); - Node *_post_fix_node(Node *p_node, Node *p_root, Map<Ref<EditorSceneImporterMesh>, Vector<Ref<Shape3D>>> &collision_map, Set<Ref<EditorSceneImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps); + Node *_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &collision_map); + Node *_post_fix_node(Node *p_node, Node *p_root, Map<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &collision_map, Set<Ref<ImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps); Ref<Animation> _save_animation_to_file(Ref<Animation> anim, bool p_save_to_file, String p_save_to_path, bool p_keep_custom_tracks); void _create_clips(AnimationPlayer *anim, const Array &p_clips, bool p_bake_all); diff --git a/editor/import/scene_import_settings.cpp b/editor/import/scene_import_settings.cpp index 7ab5308a47..95a96f9e26 100644 --- a/editor/import/scene_import_settings.cpp +++ b/editor/import/scene_import_settings.cpp @@ -31,7 +31,8 @@ #include "scene_import_settings.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" -#include "editor/import/scene_importer_mesh_node_3d.h" +#include "scene/3d/importer_mesh_instance_3d.h" +#include "scene/resources/importer_mesh.h" #include "scene/resources/surface_tool.h" class SceneImportSettingsData : public Object { @@ -240,7 +241,7 @@ void SceneImportSettings::_fill_scene(Node *p_node, TreeItem *p_parent_item) { p_node->set_meta("import_id", import_id); } - EditorSceneImporterMeshNode3D *src_mesh_node = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + ImporterMeshInstance3D *src_mesh_node = Object::cast_to<ImporterMeshInstance3D>(p_node); if (src_mesh_node) { MeshInstance3D *mesh_node = memnew(MeshInstance3D); @@ -249,7 +250,7 @@ void SceneImportSettings::_fill_scene(Node *p_node, TreeItem *p_parent_item) { mesh_node->set_skin(src_mesh_node->get_skin()); mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path()); if (src_mesh_node->get_mesh().is_valid()) { - Ref<EditorSceneImporterMesh> editor_mesh = src_mesh_node->get_mesh(); + Ref<ImporterMesh> editor_mesh = src_mesh_node->get_mesh(); mesh_node->set_mesh(editor_mesh->get_mesh()); } diff --git a/editor/import/scene_importer_mesh.cpp b/editor/import/scene_importer_mesh.cpp deleted file mode 100644 index 370394b475..0000000000 --- a/editor/import/scene_importer_mesh.cpp +++ /dev/null @@ -1,1238 +0,0 @@ -/*************************************************************************/ -/* scene_importer_mesh.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 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 "scene_importer_mesh.h" - -#include "core/math/random_pcg.h" -#include "core/math/static_raycaster.h" -#include "scene/resources/surface_tool.h" - -#include <cstdint> - -void EditorSceneImporterMesh::Surface::split_normals(const LocalVector<int> &p_indices, const LocalVector<Vector3> &p_normals) { - ERR_FAIL_COND(arrays.size() != RS::ARRAY_MAX); - - const PackedVector3Array &vertices = arrays[RS::ARRAY_VERTEX]; - int current_vertex_count = vertices.size(); - int new_vertex_count = p_indices.size(); - int final_vertex_count = current_vertex_count + new_vertex_count; - const int *indices_ptr = p_indices.ptr(); - - for (int i = 0; i < arrays.size(); i++) { - if (i == RS::ARRAY_INDEX) { - continue; - } - - if (arrays[i].get_type() == Variant::NIL) { - continue; - } - - switch (arrays[i].get_type()) { - case Variant::PACKED_VECTOR3_ARRAY: { - PackedVector3Array data = arrays[i]; - data.resize(final_vertex_count); - Vector3 *data_ptr = data.ptrw(); - if (i == RS::ARRAY_NORMAL) { - const Vector3 *normals_ptr = p_normals.ptr(); - memcpy(&data_ptr[current_vertex_count], normals_ptr, sizeof(Vector3) * new_vertex_count); - } else { - for (int j = 0; j < new_vertex_count; j++) { - data_ptr[current_vertex_count + j] = data_ptr[indices_ptr[j]]; - } - } - arrays[i] = data; - } break; - case Variant::PACKED_VECTOR2_ARRAY: { - PackedVector2Array data = arrays[i]; - data.resize(final_vertex_count); - Vector2 *data_ptr = data.ptrw(); - for (int j = 0; j < new_vertex_count; j++) { - data_ptr[current_vertex_count + j] = data_ptr[indices_ptr[j]]; - } - arrays[i] = data; - } break; - case Variant::PACKED_FLOAT32_ARRAY: { - PackedFloat32Array data = arrays[i]; - int elements = data.size() / current_vertex_count; - data.resize(final_vertex_count * elements); - float *data_ptr = data.ptrw(); - for (int j = 0; j < new_vertex_count; j++) { - memcpy(&data_ptr[(current_vertex_count + j) * elements], &data_ptr[indices_ptr[j] * elements], sizeof(float) * elements); - } - arrays[i] = data; - } break; - case Variant::PACKED_INT32_ARRAY: { - PackedInt32Array data = arrays[i]; - int elements = data.size() / current_vertex_count; - data.resize(final_vertex_count * elements); - int32_t *data_ptr = data.ptrw(); - for (int j = 0; j < new_vertex_count; j++) { - memcpy(&data_ptr[(current_vertex_count + j) * elements], &data_ptr[indices_ptr[j] * elements], sizeof(int32_t) * elements); - } - arrays[i] = data; - } break; - case Variant::PACKED_BYTE_ARRAY: { - PackedByteArray data = arrays[i]; - int elements = data.size() / current_vertex_count; - data.resize(final_vertex_count * elements); - uint8_t *data_ptr = data.ptrw(); - for (int j = 0; j < new_vertex_count; j++) { - memcpy(&data_ptr[(current_vertex_count + j) * elements], &data_ptr[indices_ptr[j] * elements], sizeof(uint8_t) * elements); - } - arrays[i] = data; - } break; - case Variant::PACKED_COLOR_ARRAY: { - PackedColorArray data = arrays[i]; - data.resize(final_vertex_count); - Color *data_ptr = data.ptrw(); - for (int j = 0; j < new_vertex_count; j++) { - data_ptr[current_vertex_count + j] = data_ptr[indices_ptr[j]]; - } - } break; - default: { - ERR_FAIL_MSG("Uhandled array type."); - } break; - } - } -} - -void EditorSceneImporterMesh::add_blend_shape(const String &p_name) { - ERR_FAIL_COND(surfaces.size() > 0); - blend_shapes.push_back(p_name); -} - -int EditorSceneImporterMesh::get_blend_shape_count() const { - return blend_shapes.size(); -} - -String EditorSceneImporterMesh::get_blend_shape_name(int p_blend_shape) const { - ERR_FAIL_INDEX_V(p_blend_shape, blend_shapes.size(), String()); - return blend_shapes[p_blend_shape]; -} - -void EditorSceneImporterMesh::set_blend_shape_mode(Mesh::BlendShapeMode p_blend_shape_mode) { - blend_shape_mode = p_blend_shape_mode; -} - -Mesh::BlendShapeMode EditorSceneImporterMesh::get_blend_shape_mode() const { - return blend_shape_mode; -} - -void EditorSceneImporterMesh::add_surface(Mesh::PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, const Dictionary &p_lods, const Ref<Material> &p_material, const String &p_name, const uint32_t p_flags) { - ERR_FAIL_COND(p_blend_shapes.size() != blend_shapes.size()); - ERR_FAIL_COND(p_arrays.size() != Mesh::ARRAY_MAX); - Surface s; - s.primitive = p_primitive; - s.arrays = p_arrays; - s.name = p_name; - s.flags = p_flags; - - Vector<Vector3> vertex_array = p_arrays[Mesh::ARRAY_VERTEX]; - int vertex_count = vertex_array.size(); - ERR_FAIL_COND(vertex_count == 0); - - for (int i = 0; i < blend_shapes.size(); i++) { - Array bsdata = p_blend_shapes[i]; - ERR_FAIL_COND(bsdata.size() != Mesh::ARRAY_MAX); - Vector<Vector3> vertex_data = bsdata[Mesh::ARRAY_VERTEX]; - ERR_FAIL_COND(vertex_data.size() != vertex_count); - Surface::BlendShape bs; - bs.arrays = bsdata; - s.blend_shape_data.push_back(bs); - } - - List<Variant> lods; - p_lods.get_key_list(&lods); - for (const Variant &E : lods) { - ERR_CONTINUE(!E.is_num()); - Surface::LOD lod; - lod.distance = E; - lod.indices = p_lods[E]; - ERR_CONTINUE(lod.indices.size() == 0); - s.lods.push_back(lod); - } - - s.material = p_material; - - surfaces.push_back(s); - mesh.unref(); -} - -int EditorSceneImporterMesh::get_surface_count() const { - return surfaces.size(); -} - -Mesh::PrimitiveType EditorSceneImporterMesh::get_surface_primitive_type(int p_surface) { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Mesh::PRIMITIVE_MAX); - return surfaces[p_surface].primitive; -} -Array EditorSceneImporterMesh::get_surface_arrays(int p_surface) const { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array()); - return surfaces[p_surface].arrays; -} -String EditorSceneImporterMesh::get_surface_name(int p_surface) const { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), String()); - return surfaces[p_surface].name; -} -void EditorSceneImporterMesh::set_surface_name(int p_surface, const String &p_name) { - ERR_FAIL_INDEX(p_surface, surfaces.size()); - surfaces.write[p_surface].name = p_name; - mesh.unref(); -} - -Array EditorSceneImporterMesh::get_surface_blend_shape_arrays(int p_surface, int p_blend_shape) const { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array()); - ERR_FAIL_INDEX_V(p_blend_shape, surfaces[p_surface].blend_shape_data.size(), Array()); - return surfaces[p_surface].blend_shape_data[p_blend_shape].arrays; -} -int EditorSceneImporterMesh::get_surface_lod_count(int p_surface) const { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), 0); - return surfaces[p_surface].lods.size(); -} -Vector<int> EditorSceneImporterMesh::get_surface_lod_indices(int p_surface, int p_lod) const { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Vector<int>()); - ERR_FAIL_INDEX_V(p_lod, surfaces[p_surface].lods.size(), Vector<int>()); - - return surfaces[p_surface].lods[p_lod].indices; -} - -float EditorSceneImporterMesh::get_surface_lod_size(int p_surface, int p_lod) const { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), 0); - ERR_FAIL_INDEX_V(p_lod, surfaces[p_surface].lods.size(), 0); - return surfaces[p_surface].lods[p_lod].distance; -} - -uint32_t EditorSceneImporterMesh::get_surface_format(int p_surface) const { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), 0); - return surfaces[p_surface].flags; -} - -Ref<Material> EditorSceneImporterMesh::get_surface_material(int p_surface) const { - ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Ref<Material>()); - return surfaces[p_surface].material; -} - -void EditorSceneImporterMesh::set_surface_material(int p_surface, const Ref<Material> &p_material) { - ERR_FAIL_INDEX(p_surface, surfaces.size()); - surfaces.write[p_surface].material = p_material; - mesh.unref(); -} - -void EditorSceneImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_split_angle) { - if (!SurfaceTool::simplify_scale_func) { - return; - } - if (!SurfaceTool::simplify_with_attrib_func) { - return; - } - if (!SurfaceTool::optimize_vertex_cache_func) { - return; - } - - for (int i = 0; i < surfaces.size(); i++) { - if (surfaces[i].primitive != Mesh::PRIMITIVE_TRIANGLES) { - continue; - } - - surfaces.write[i].lods.clear(); - Vector<Vector3> vertices = surfaces[i].arrays[RS::ARRAY_VERTEX]; - PackedInt32Array indices = surfaces[i].arrays[RS::ARRAY_INDEX]; - Vector<Vector3> normals = surfaces[i].arrays[RS::ARRAY_NORMAL]; - Vector<Vector2> uvs = surfaces[i].arrays[RS::ARRAY_TEX_UV]; - - unsigned int index_count = indices.size(); - unsigned int vertex_count = vertices.size(); - - if (index_count == 0) { - continue; //no lods if no indices - } - - const Vector3 *vertices_ptr = vertices.ptr(); - const int *indices_ptr = indices.ptr(); - - if (normals.is_empty()) { - normals.resize(vertices.size()); - Vector3 *n_ptr = normals.ptrw(); - for (unsigned int j = 0; j < index_count; j += 3) { - const Vector3 &v0 = vertices_ptr[indices_ptr[j + 0]]; - const Vector3 &v1 = vertices_ptr[indices_ptr[j + 1]]; - const Vector3 &v2 = vertices_ptr[indices_ptr[j + 2]]; - Vector3 n = vec3_cross(v0 - v2, v0 - v1).normalized(); - n_ptr[j + 0] = n; - n_ptr[j + 1] = n; - n_ptr[j + 2] = n; - } - } - - float normal_merge_threshold = Math::cos(Math::deg2rad(p_normal_merge_angle)); - float normal_pre_split_threshold = Math::cos(Math::deg2rad(MIN(180.0f, p_normal_split_angle * 2.0f))); - float normal_split_threshold = Math::cos(Math::deg2rad(p_normal_split_angle)); - const Vector3 *normals_ptr = normals.ptr(); - - Map<Vector3, LocalVector<Pair<int, int>>> unique_vertices; - - LocalVector<int> vertex_remap; - LocalVector<int> vertex_inverse_remap; - LocalVector<Vector3> merged_vertices; - LocalVector<Vector3> merged_normals; - LocalVector<int> merged_normals_counts; - const Vector2 *uvs_ptr = uvs.ptr(); - - for (unsigned int j = 0; j < vertex_count; j++) { - const Vector3 &v = vertices_ptr[j]; - const Vector3 &n = normals_ptr[j]; - - Map<Vector3, LocalVector<Pair<int, int>>>::Element *E = unique_vertices.find(v); - - if (E) { - const LocalVector<Pair<int, int>> &close_verts = E->get(); - - bool found = false; - for (unsigned int k = 0; k < close_verts.size(); k++) { - const Pair<int, int> &idx = close_verts[k]; - - // TODO check more attributes? - if ((!uvs_ptr || uvs_ptr[j].distance_squared_to(uvs_ptr[idx.second]) < CMP_EPSILON2) && normals[idx.second].dot(n) > normal_merge_threshold) { - vertex_remap.push_back(idx.first); - merged_normals[idx.first] += normals[idx.second]; - merged_normals_counts[idx.first]++; - found = true; - break; - } - } - - if (!found) { - int vcount = merged_vertices.size(); - unique_vertices[v].push_back(Pair<int, int>(vcount, j)); - vertex_inverse_remap.push_back(j); - merged_vertices.push_back(v); - vertex_remap.push_back(vcount); - merged_normals.push_back(normals_ptr[j]); - merged_normals_counts.push_back(1); - } - } else { - int vcount = merged_vertices.size(); - unique_vertices[v] = LocalVector<Pair<int, int>>(); - unique_vertices[v].push_back(Pair<int, int>(vcount, j)); - vertex_inverse_remap.push_back(j); - merged_vertices.push_back(v); - vertex_remap.push_back(vcount); - merged_normals.push_back(normals_ptr[j]); - merged_normals_counts.push_back(1); - } - } - - LocalVector<int> merged_indices; - merged_indices.resize(index_count); - for (unsigned int j = 0; j < index_count; j++) { - merged_indices[j] = vertex_remap[indices[j]]; - } - - unsigned int merged_vertex_count = merged_vertices.size(); - const Vector3 *merged_vertices_ptr = merged_vertices.ptr(); - const int32_t *merged_indices_ptr = merged_indices.ptr(); - - { - const int *counts_ptr = merged_normals_counts.ptr(); - Vector3 *merged_normals_ptrw = merged_normals.ptr(); - for (unsigned int j = 0; j < merged_vertex_count; j++) { - merged_normals_ptrw[j] /= counts_ptr[j]; - } - } - - LocalVector<float> normal_weights; - normal_weights.resize(merged_vertex_count); - for (unsigned int j = 0; j < merged_vertex_count; j++) { - normal_weights[j] = 2.0; // Give some weight to normal preservation, may be worth exposing as an import setting - } - - const float max_mesh_error = FLT_MAX; // We don't want to limit by error, just by index target - float scale = SurfaceTool::simplify_scale_func((const float *)merged_vertices_ptr, merged_vertex_count, sizeof(Vector3)); - float mesh_error = 0.0f; - - unsigned int index_target = 12; // Start with the smallest target, 4 triangles - unsigned int last_index_count = 0; - - int split_vertex_count = vertex_count; - LocalVector<Vector3> split_vertex_normals; - LocalVector<int> split_vertex_indices; - split_vertex_normals.reserve(index_count / 3); - split_vertex_indices.reserve(index_count / 3); - - RandomPCG pcg; - pcg.seed(123456789); // Keep seed constant across imports - - Ref<StaticRaycaster> raycaster = StaticRaycaster::create(); - if (raycaster.is_valid()) { - raycaster->add_mesh(vertices, indices, 0); - raycaster->commit(); - } - - while (index_target < index_count) { - PackedInt32Array new_indices; - new_indices.resize(index_count); - - size_t new_index_count = SurfaceTool::simplify_with_attrib_func((unsigned int *)new_indices.ptrw(), (const uint32_t *)merged_indices_ptr, index_count, (const float *)merged_vertices_ptr, merged_vertex_count, sizeof(Vector3), index_target, max_mesh_error, &mesh_error, (float *)merged_normals.ptr(), normal_weights.ptr(), 3); - - if (new_index_count < last_index_count * 1.5f) { - index_target = index_target * 1.5f; - continue; - } - - if (new_index_count <= 0 || (new_index_count >= (index_count * 0.75f))) { - break; - } - - new_indices.resize(new_index_count); - - LocalVector<LocalVector<int>> vertex_corners; - vertex_corners.resize(vertex_count); - { - int *ptrw = new_indices.ptrw(); - for (unsigned int j = 0; j < new_index_count; j++) { - const int &remapped = vertex_inverse_remap[ptrw[j]]; - vertex_corners[remapped].push_back(j); - ptrw[j] = remapped; - } - } - - if (raycaster.is_valid()) { - float error_factor = 1.0f / (scale * MAX(mesh_error, 0.15)); - const float ray_bias = 0.05; - float ray_length = ray_bias + mesh_error * scale * 3.0f; - - Vector<StaticRaycaster::Ray> rays; - LocalVector<Vector2> ray_uvs; - - int32_t *new_indices_ptr = new_indices.ptrw(); - - int current_ray_count = 0; - for (unsigned int j = 0; j < new_index_count; j += 3) { - const Vector3 &v0 = vertices_ptr[new_indices_ptr[j + 0]]; - const Vector3 &v1 = vertices_ptr[new_indices_ptr[j + 1]]; - const Vector3 &v2 = vertices_ptr[new_indices_ptr[j + 2]]; - Vector3 face_normal = vec3_cross(v0 - v2, v0 - v1); - float face_area = face_normal.length(); // Actually twice the face area, since it's the same error_factor on all faces, we don't care - - Vector3 dir = face_normal / face_area; - int ray_count = CLAMP(5.0 * face_area * error_factor, 16, 64); - - rays.resize(current_ray_count + ray_count); - StaticRaycaster::Ray *rays_ptr = rays.ptrw(); - - ray_uvs.resize(current_ray_count + ray_count); - Vector2 *ray_uvs_ptr = ray_uvs.ptr(); - - for (int k = 0; k < ray_count; k++) { - float u = pcg.randf(); - float v = pcg.randf(); - - if (u + v >= 1.0f) { - u = 1.0f - u; - v = 1.0f - v; - } - - u = 0.9f * u + 0.05f / 3.0f; // Give barycentric coordinates some padding, we don't want to sample right on the edge - v = 0.9f * v + 0.05f / 3.0f; // v = (v - one_third) * 0.95f + one_third; - float w = 1.0f - u - v; - - Vector3 org = v0 * w + v1 * u + v2 * v; - org -= dir * ray_bias; - rays_ptr[current_ray_count + k] = StaticRaycaster::Ray(org, dir, 0.0f, ray_length); - rays_ptr[current_ray_count + k].id = j / 3; - ray_uvs_ptr[current_ray_count + k] = Vector2(u, v); - } - - current_ray_count += ray_count; - } - - raycaster->intersect(rays); - - LocalVector<Vector3> ray_normals; - LocalVector<float> ray_normal_weights; - - ray_normals.resize(new_index_count); - ray_normal_weights.resize(new_index_count); - - for (unsigned int j = 0; j < new_index_count; j++) { - ray_normal_weights[j] = 0.0f; - } - - const StaticRaycaster::Ray *rp = rays.ptr(); - for (int j = 0; j < rays.size(); j++) { - if (rp[j].geomID != 0) { // Ray missed - continue; - } - - if (rp[j].normal.normalized().dot(rp[j].dir) > 0.0f) { // Hit a back face. - continue; - } - - const float &u = rp[j].u; - const float &v = rp[j].v; - const float w = 1.0f - u - v; - - const unsigned int &hit_tri_id = rp[j].primID; - const unsigned int &orig_tri_id = rp[j].id; - - const Vector3 &n0 = normals_ptr[indices_ptr[hit_tri_id * 3 + 0]]; - const Vector3 &n1 = normals_ptr[indices_ptr[hit_tri_id * 3 + 1]]; - const Vector3 &n2 = normals_ptr[indices_ptr[hit_tri_id * 3 + 2]]; - Vector3 normal = n0 * w + n1 * u + n2 * v; - - Vector2 orig_uv = ray_uvs[j]; - float orig_bary[3] = { 1.0f - orig_uv.x - orig_uv.y, orig_uv.x, orig_uv.y }; - for (int k = 0; k < 3; k++) { - int idx = orig_tri_id * 3 + k; - float weight = orig_bary[k]; - ray_normals[idx] += normal * weight; - ray_normal_weights[idx] += weight; - } - } - - for (unsigned int j = 0; j < new_index_count; j++) { - if (ray_normal_weights[j] < 1.0f) { // Not enough data, the new normal would be just a bad guess - ray_normals[j] = Vector3(); - } else { - ray_normals[j] /= ray_normal_weights[j]; - } - } - - LocalVector<LocalVector<int>> normal_group_indices; - LocalVector<Vector3> normal_group_averages; - normal_group_indices.reserve(24); - normal_group_averages.reserve(24); - - for (unsigned int j = 0; j < vertex_count; j++) { - const LocalVector<int> &corners = vertex_corners[j]; - const Vector3 &vertex_normal = normals_ptr[j]; - - for (unsigned int k = 0; k < corners.size(); k++) { - const int &corner_idx = corners[k]; - const Vector3 &ray_normal = ray_normals[corner_idx]; - - if (ray_normal.length_squared() < CMP_EPSILON2) { - continue; - } - - bool found = false; - for (unsigned int l = 0; l < normal_group_indices.size(); l++) { - LocalVector<int> &group_indices = normal_group_indices[l]; - Vector3 n = normal_group_averages[l] / group_indices.size(); - if (n.dot(ray_normal) > normal_pre_split_threshold) { - found = true; - group_indices.push_back(corner_idx); - normal_group_averages[l] += ray_normal; - break; - } - } - - if (!found) { - LocalVector<int> new_group; - new_group.push_back(corner_idx); - normal_group_indices.push_back(new_group); - normal_group_averages.push_back(ray_normal); - } - } - - for (unsigned int k = 0; k < normal_group_indices.size(); k++) { - LocalVector<int> &group_indices = normal_group_indices[k]; - Vector3 n = normal_group_averages[k] / group_indices.size(); - - if (vertex_normal.dot(n) < normal_split_threshold) { - split_vertex_indices.push_back(j); - split_vertex_normals.push_back(n); - int new_idx = split_vertex_count++; - for (unsigned int l = 0; l < group_indices.size(); l++) { - new_indices_ptr[group_indices[l]] = new_idx; - } - } - } - - normal_group_indices.clear(); - normal_group_averages.clear(); - } - } - - Surface::LOD lod; - lod.distance = MAX(mesh_error * scale, CMP_EPSILON2); - lod.indices = new_indices; - surfaces.write[i].lods.push_back(lod); - index_target = MAX(new_index_count, index_target) * 2; - last_index_count = new_index_count; - - if (mesh_error == 0.0f) { - break; - } - } - - surfaces.write[i].split_normals(split_vertex_indices, split_vertex_normals); - surfaces.write[i].lods.sort_custom<Surface::LODComparator>(); - - for (int j = 0; j < surfaces.write[i].lods.size(); j++) { - Surface::LOD &lod = surfaces.write[i].lods.write[j]; - unsigned int *lod_indices_ptr = (unsigned int *)lod.indices.ptrw(); - SurfaceTool::optimize_vertex_cache_func(lod_indices_ptr, lod_indices_ptr, lod.indices.size(), split_vertex_count); - } - } -} - -bool EditorSceneImporterMesh::has_mesh() const { - return mesh.is_valid(); -} - -Ref<ArrayMesh> EditorSceneImporterMesh::get_mesh(const Ref<ArrayMesh> &p_base) { - ERR_FAIL_COND_V(surfaces.size() == 0, Ref<ArrayMesh>()); - - if (mesh.is_null()) { - if (p_base.is_valid()) { - mesh = p_base; - } - if (mesh.is_null()) { - mesh.instantiate(); - } - mesh->set_name(get_name()); - if (has_meta("import_id")) { - mesh->set_meta("import_id", get_meta("import_id")); - } - for (int i = 0; i < blend_shapes.size(); i++) { - mesh->add_blend_shape(blend_shapes[i]); - } - mesh->set_blend_shape_mode(blend_shape_mode); - for (int i = 0; i < surfaces.size(); i++) { - Array bs_data; - if (surfaces[i].blend_shape_data.size()) { - for (int j = 0; j < surfaces[i].blend_shape_data.size(); j++) { - bs_data.push_back(surfaces[i].blend_shape_data[j].arrays); - } - } - Dictionary lods; - if (surfaces[i].lods.size()) { - for (int j = 0; j < surfaces[i].lods.size(); j++) { - lods[surfaces[i].lods[j].distance] = surfaces[i].lods[j].indices; - } - } - - mesh->add_surface_from_arrays(surfaces[i].primitive, surfaces[i].arrays, bs_data, lods, surfaces[i].flags); - if (surfaces[i].material.is_valid()) { - mesh->surface_set_material(mesh->get_surface_count() - 1, surfaces[i].material); - } - if (surfaces[i].name != String()) { - mesh->surface_set_name(mesh->get_surface_count() - 1, surfaces[i].name); - } - } - - mesh->set_lightmap_size_hint(lightmap_size_hint); - - if (shadow_mesh.is_valid()) { - Ref<ArrayMesh> shadow = shadow_mesh->get_mesh(); - mesh->set_shadow_mesh(shadow); - } - } - - return mesh; -} - -void EditorSceneImporterMesh::clear() { - surfaces.clear(); - blend_shapes.clear(); - mesh.unref(); -} - -void EditorSceneImporterMesh::create_shadow_mesh() { - if (shadow_mesh.is_valid()) { - shadow_mesh.unref(); - } - - //no shadow mesh for blendshapes - if (blend_shapes.size() > 0) { - return; - } - //no shadow mesh for skeletons - for (int i = 0; i < surfaces.size(); i++) { - if (surfaces[i].arrays[RS::ARRAY_BONES].get_type() != Variant::NIL) { - return; - } - if (surfaces[i].arrays[RS::ARRAY_WEIGHTS].get_type() != Variant::NIL) { - return; - } - } - - shadow_mesh.instantiate(); - - for (int i = 0; i < surfaces.size(); i++) { - LocalVector<int> vertex_remap; - Vector<Vector3> new_vertices; - Vector<Vector3> vertices = surfaces[i].arrays[RS::ARRAY_VERTEX]; - int vertex_count = vertices.size(); - { - Map<Vector3, int> unique_vertices; - const Vector3 *vptr = vertices.ptr(); - for (int j = 0; j < vertex_count; j++) { - const Vector3 &v = vptr[j]; - - Map<Vector3, int>::Element *E = unique_vertices.find(v); - - if (E) { - vertex_remap.push_back(E->get()); - } else { - int vcount = unique_vertices.size(); - unique_vertices[v] = vcount; - vertex_remap.push_back(vcount); - new_vertices.push_back(v); - } - } - } - - Array new_surface; - new_surface.resize(RS::ARRAY_MAX); - Dictionary lods; - - // print_line("original vertex count: " + itos(vertices.size()) + " new vertex count: " + itos(new_vertices.size())); - - new_surface[RS::ARRAY_VERTEX] = new_vertices; - - Vector<int> indices = surfaces[i].arrays[RS::ARRAY_INDEX]; - if (indices.size()) { - int index_count = indices.size(); - const int *index_rptr = indices.ptr(); - Vector<int> new_indices; - new_indices.resize(indices.size()); - int *index_wptr = new_indices.ptrw(); - - for (int j = 0; j < index_count; j++) { - int index = index_rptr[j]; - ERR_FAIL_INDEX(index, vertex_count); - index_wptr[j] = vertex_remap[index]; - } - - new_surface[RS::ARRAY_INDEX] = new_indices; - - // Make sure the same LODs as the full version are used. - // This makes it more coherent between rendered model and its shadows. - for (int j = 0; j < surfaces[i].lods.size(); j++) { - indices = surfaces[i].lods[j].indices; - - index_count = indices.size(); - index_rptr = indices.ptr(); - new_indices.resize(indices.size()); - index_wptr = new_indices.ptrw(); - - for (int k = 0; k < index_count; k++) { - int index = index_rptr[k]; - ERR_FAIL_INDEX(index, vertex_count); - index_wptr[k] = vertex_remap[index]; - } - - lods[surfaces[i].lods[j].distance] = new_indices; - } - } - - shadow_mesh->add_surface(surfaces[i].primitive, new_surface, Array(), lods, Ref<Material>(), surfaces[i].name, surfaces[i].flags); - } -} - -Ref<EditorSceneImporterMesh> EditorSceneImporterMesh::get_shadow_mesh() const { - return shadow_mesh; -} - -void EditorSceneImporterMesh::_set_data(const Dictionary &p_data) { - clear(); - if (p_data.has("blend_shape_names")) { - blend_shapes = p_data["blend_shape_names"]; - } - if (p_data.has("surfaces")) { - Array surface_arr = p_data["surfaces"]; - for (int i = 0; i < surface_arr.size(); i++) { - Dictionary s = surface_arr[i]; - ERR_CONTINUE(!s.has("primitive")); - ERR_CONTINUE(!s.has("arrays")); - Mesh::PrimitiveType prim = Mesh::PrimitiveType(int(s["primitive"])); - ERR_CONTINUE(prim >= Mesh::PRIMITIVE_MAX); - Array arr = s["arrays"]; - Dictionary lods; - String name; - if (s.has("name")) { - name = s["name"]; - } - if (s.has("lods")) { - lods = s["lods"]; - } - Array b_shapes; - if (s.has("b_shapes")) { - b_shapes = s["b_shapes"]; - } - Ref<Material> material; - if (s.has("material")) { - material = s["material"]; - } - uint32_t flags = 0; - if (s.has("flags")) { - flags = s["flags"]; - } - add_surface(prim, arr, b_shapes, lods, material, name, flags); - } - } -} -Dictionary EditorSceneImporterMesh::_get_data() const { - Dictionary data; - if (blend_shapes.size()) { - data["blend_shape_names"] = blend_shapes; - } - Array surface_arr; - for (int i = 0; i < surfaces.size(); i++) { - Dictionary d; - d["primitive"] = surfaces[i].primitive; - d["arrays"] = surfaces[i].arrays; - if (surfaces[i].blend_shape_data.size()) { - Array bs_data; - for (int j = 0; j < surfaces[i].blend_shape_data.size(); j++) { - bs_data.push_back(surfaces[i].blend_shape_data[j].arrays); - } - d["blend_shapes"] = bs_data; - } - if (surfaces[i].lods.size()) { - Dictionary lods; - for (int j = 0; j < surfaces[i].lods.size(); j++) { - lods[surfaces[i].lods[j].distance] = surfaces[i].lods[j].indices; - } - d["lods"] = lods; - } - - if (surfaces[i].material.is_valid()) { - d["material"] = surfaces[i].material; - } - - if (surfaces[i].name != String()) { - d["name"] = surfaces[i].name; - } - - if (surfaces[i].flags != 0) { - d["flags"] = surfaces[i].flags; - } - - surface_arr.push_back(d); - } - data["surfaces"] = surface_arr; - return data; -} - -Vector<Face3> EditorSceneImporterMesh::get_faces() const { - Vector<Face3> faces; - for (int i = 0; i < surfaces.size(); i++) { - if (surfaces[i].primitive == Mesh::PRIMITIVE_TRIANGLES) { - Vector<Vector3> vertices = surfaces[i].arrays[Mesh::ARRAY_VERTEX]; - Vector<int> indices = surfaces[i].arrays[Mesh::ARRAY_INDEX]; - if (indices.size()) { - for (int j = 0; j < indices.size(); j += 3) { - Face3 f; - f.vertex[0] = vertices[indices[j + 0]]; - f.vertex[1] = vertices[indices[j + 1]]; - f.vertex[2] = vertices[indices[j + 2]]; - faces.push_back(f); - } - } else { - for (int j = 0; j < vertices.size(); j += 3) { - Face3 f; - f.vertex[0] = vertices[j + 0]; - f.vertex[1] = vertices[j + 1]; - f.vertex[2] = vertices[j + 2]; - faces.push_back(f); - } - } - } - } - - return faces; -} - -Vector<Ref<Shape3D>> EditorSceneImporterMesh::convex_decompose(const Mesh::ConvexDecompositionSettings &p_settings) const { - ERR_FAIL_COND_V(!Mesh::convex_decomposition_function, Vector<Ref<Shape3D>>()); - - const Vector<Face3> faces = get_faces(); - int face_count = faces.size(); - - Vector<Vector3> vertices; - uint32_t vertex_count = 0; - vertices.resize(face_count * 3); - Vector<uint32_t> indices; - indices.resize(face_count * 3); - { - Map<Vector3, uint32_t> vertex_map; - Vector3 *vertex_w = vertices.ptrw(); - uint32_t *index_w = indices.ptrw(); - for (int i = 0; i < face_count; i++) { - for (int j = 0; j < 3; j++) { - const Vector3 &vertex = faces[i].vertex[j]; - Map<Vector3, uint32_t>::Element *found_vertex = vertex_map.find(vertex); - uint32_t index; - if (found_vertex) { - index = found_vertex->get(); - } else { - index = ++vertex_count; - vertex_map[vertex] = index; - vertex_w[index] = vertex; - } - index_w[i * 3 + j] = index; - } - } - } - vertices.resize(vertex_count); - - Vector<Vector<Vector3>> decomposed = Mesh::convex_decomposition_function((real_t *)vertices.ptr(), vertex_count, indices.ptr(), face_count, p_settings, nullptr); - - Vector<Ref<Shape3D>> ret; - - for (int i = 0; i < decomposed.size(); i++) { - Ref<ConvexPolygonShape3D> shape; - shape.instantiate(); - shape->set_points(decomposed[i]); - ret.push_back(shape); - } - - return ret; -} - -Ref<Shape3D> EditorSceneImporterMesh::create_trimesh_shape() const { - Vector<Face3> faces = get_faces(); - if (faces.size() == 0) { - return Ref<Shape3D>(); - } - - Vector<Vector3> face_points; - face_points.resize(faces.size() * 3); - - for (int i = 0; i < face_points.size(); i += 3) { - Face3 f = faces.get(i / 3); - face_points.set(i, f.vertex[0]); - face_points.set(i + 1, f.vertex[1]); - face_points.set(i + 2, f.vertex[2]); - } - - Ref<ConcavePolygonShape3D> shape = memnew(ConcavePolygonShape3D); - shape->set_faces(face_points); - return shape; -} - -Ref<NavigationMesh> EditorSceneImporterMesh::create_navigation_mesh() { - Vector<Face3> faces = get_faces(); - if (faces.size() == 0) { - return Ref<NavigationMesh>(); - } - - Map<Vector3, int> unique_vertices; - LocalVector<int> face_indices; - - for (int i = 0; i < faces.size(); i++) { - for (int j = 0; j < 3; j++) { - Vector3 v = faces[i].vertex[j]; - int idx; - if (unique_vertices.has(v)) { - idx = unique_vertices[v]; - } else { - idx = unique_vertices.size(); - unique_vertices[v] = idx; - } - face_indices.push_back(idx); - } - } - - Vector<Vector3> vertices; - vertices.resize(unique_vertices.size()); - for (const KeyValue<Vector3, int> &E : unique_vertices) { - vertices.write[E.value] = E.key; - } - - Ref<NavigationMesh> nm; - nm.instantiate(); - nm->set_vertices(vertices); - - Vector<int> v3; - v3.resize(3); - for (uint32_t i = 0; i < face_indices.size(); i += 3) { - v3.write[0] = face_indices[i + 0]; - v3.write[1] = face_indices[i + 1]; - v3.write[2] = face_indices[i + 2]; - nm->add_polygon(v3); - } - - return nm; -} - -extern bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y); - -struct EditorSceneImporterMeshLightmapSurface { - Ref<Material> material; - LocalVector<SurfaceTool::Vertex> vertices; - Mesh::PrimitiveType primitive = Mesh::PrimitiveType::PRIMITIVE_MAX; - uint32_t format = 0; - String name; -}; - -Error EditorSceneImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache) { - ERR_FAIL_COND_V(!array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED); - ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes."); - - LocalVector<float> vertices; - LocalVector<float> normals; - LocalVector<int> indices; - LocalVector<float> uv; - LocalVector<Pair<int, int>> uv_indices; - - Vector<EditorSceneImporterMeshLightmapSurface> lightmap_surfaces; - - // Keep only the scale - Basis basis = p_base_transform.get_basis(); - Vector3 scale = Vector3(basis.get_axis(0).length(), basis.get_axis(1).length(), basis.get_axis(2).length()); - - Transform3D transform; - transform.scale(scale); - - Basis normal_basis = transform.basis.inverse().transposed(); - - for (int i = 0; i < get_surface_count(); i++) { - EditorSceneImporterMeshLightmapSurface s; - s.primitive = get_surface_primitive_type(i); - - ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap."); - Array arrays = get_surface_arrays(i); - s.material = get_surface_material(i); - s.name = get_surface_name(i); - - SurfaceTool::create_vertex_array_from_triangle_arrays(arrays, s.vertices, &s.format); - - PackedVector3Array rvertices = arrays[Mesh::ARRAY_VERTEX]; - int vc = rvertices.size(); - - PackedVector3Array rnormals = arrays[Mesh::ARRAY_NORMAL]; - - int vertex_ofs = vertices.size() / 3; - - vertices.resize((vertex_ofs + vc) * 3); - normals.resize((vertex_ofs + vc) * 3); - uv_indices.resize(vertex_ofs + vc); - - for (int j = 0; j < vc; j++) { - Vector3 v = transform.xform(rvertices[j]); - Vector3 n = normal_basis.xform(rnormals[j]).normalized(); - - vertices[(j + vertex_ofs) * 3 + 0] = v.x; - vertices[(j + vertex_ofs) * 3 + 1] = v.y; - vertices[(j + vertex_ofs) * 3 + 2] = v.z; - normals[(j + vertex_ofs) * 3 + 0] = n.x; - normals[(j + vertex_ofs) * 3 + 1] = n.y; - normals[(j + vertex_ofs) * 3 + 2] = n.z; - uv_indices[j + vertex_ofs] = Pair<int, int>(i, j); - } - - PackedInt32Array rindices = arrays[Mesh::ARRAY_INDEX]; - int ic = rindices.size(); - - float eps = 1.19209290e-7F; // Taken from xatlas.h - if (ic == 0) { - for (int j = 0; j < vc / 3; j++) { - Vector3 p0 = transform.xform(rvertices[j * 3 + 0]); - Vector3 p1 = transform.xform(rvertices[j * 3 + 1]); - Vector3 p2 = transform.xform(rvertices[j * 3 + 2]); - - if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) { - continue; - } - - indices.push_back(vertex_ofs + j * 3 + 0); - indices.push_back(vertex_ofs + j * 3 + 1); - indices.push_back(vertex_ofs + j * 3 + 2); - } - - } else { - for (int j = 0; j < ic / 3; j++) { - Vector3 p0 = transform.xform(rvertices[rindices[j * 3 + 0]]); - Vector3 p1 = transform.xform(rvertices[rindices[j * 3 + 1]]); - Vector3 p2 = transform.xform(rvertices[rindices[j * 3 + 2]]); - - if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) { - continue; - } - - indices.push_back(vertex_ofs + rindices[j * 3 + 0]); - indices.push_back(vertex_ofs + rindices[j * 3 + 1]); - indices.push_back(vertex_ofs + rindices[j * 3 + 2]); - } - } - - lightmap_surfaces.push_back(s); - } - - //unwrap - - bool use_cache = true; // Used to request cache generation and to know if cache was used - uint8_t *gen_cache; - int gen_cache_size; - float *gen_uvs; - int *gen_vertices; - int *gen_indices; - int gen_vertex_count; - int gen_index_count; - int size_x; - int size_y; - - bool ok = array_mesh_lightmap_unwrap_callback(p_texel_size, vertices.ptr(), normals.ptr(), vertices.size() / 3, indices.ptr(), indices.size(), p_src_cache.ptr(), &use_cache, &gen_cache, &gen_cache_size, &gen_uvs, &gen_vertices, &gen_vertex_count, &gen_indices, &gen_index_count, &size_x, &size_y); - - if (!ok) { - return ERR_CANT_CREATE; - } - - //remove surfaces - clear(); - - //create surfacetools for each surface.. - LocalVector<Ref<SurfaceTool>> surfaces_tools; - - for (int i = 0; i < lightmap_surfaces.size(); i++) { - Ref<SurfaceTool> st; - st.instantiate(); - st->begin(Mesh::PRIMITIVE_TRIANGLES); - st->set_material(lightmap_surfaces[i].material); - st->set_meta("name", lightmap_surfaces[i].name); - surfaces_tools.push_back(st); //stay there - } - - print_verbose("Mesh: Gen indices: " + itos(gen_index_count)); - - //go through all indices - for (int i = 0; i < gen_index_count; i += 3) { - ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], (int)uv_indices.size(), ERR_BUG); - ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], (int)uv_indices.size(), ERR_BUG); - ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], (int)uv_indices.size(), ERR_BUG); - - ERR_FAIL_COND_V(uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 1]]].first || uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 2]]].first, ERR_BUG); - - int surface = uv_indices[gen_vertices[gen_indices[i + 0]]].first; - - for (int j = 0; j < 3; j++) { - SurfaceTool::Vertex v = lightmap_surfaces[surface].vertices[uv_indices[gen_vertices[gen_indices[i + j]]].second]; - - if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_COLOR) { - surfaces_tools[surface]->set_color(v.color); - } - if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_TEX_UV) { - surfaces_tools[surface]->set_uv(v.uv); - } - if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_NORMAL) { - surfaces_tools[surface]->set_normal(v.normal); - } - if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_TANGENT) { - Plane t; - t.normal = v.tangent; - t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1; - surfaces_tools[surface]->set_tangent(t); - } - if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_BONES) { - surfaces_tools[surface]->set_bones(v.bones); - } - if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_WEIGHTS) { - surfaces_tools[surface]->set_weights(v.weights); - } - - Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]); - surfaces_tools[surface]->set_uv2(uv2); - - surfaces_tools[surface]->add_vertex(v.vertex); - } - } - - //generate surfaces - for (unsigned int i = 0; i < surfaces_tools.size(); i++) { - surfaces_tools[i]->index(); - Array arrays = surfaces_tools[i]->commit_to_arrays(); - add_surface(surfaces_tools[i]->get_primitive(), arrays, Array(), Dictionary(), surfaces_tools[i]->get_material(), surfaces_tools[i]->get_meta("name")); - } - - set_lightmap_size_hint(Size2(size_x, size_y)); - - if (gen_cache_size > 0) { - r_dst_cache.resize(gen_cache_size); - memcpy(r_dst_cache.ptrw(), gen_cache, gen_cache_size); - memfree(gen_cache); - } - - if (!use_cache) { - // Cache was not used, free the buffers - memfree(gen_vertices); - memfree(gen_indices); - memfree(gen_uvs); - } - - return OK; -} - -void EditorSceneImporterMesh::set_lightmap_size_hint(const Size2i &p_size) { - lightmap_size_hint = p_size; -} - -Size2i EditorSceneImporterMesh::get_lightmap_size_hint() const { - return lightmap_size_hint; -} - -void EditorSceneImporterMesh::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &EditorSceneImporterMesh::add_blend_shape); - ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &EditorSceneImporterMesh::get_blend_shape_count); - ClassDB::bind_method(D_METHOD("get_blend_shape_name", "blend_shape_idx"), &EditorSceneImporterMesh::get_blend_shape_name); - - ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &EditorSceneImporterMesh::set_blend_shape_mode); - ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &EditorSceneImporterMesh::get_blend_shape_mode); - - ClassDB::bind_method(D_METHOD("add_surface", "primitive", "arrays", "blend_shapes", "lods", "material", "name", "flags"), &EditorSceneImporterMesh::add_surface, DEFVAL(Array()), DEFVAL(Dictionary()), DEFVAL(Ref<Material>()), DEFVAL(String()), DEFVAL(0)); - - ClassDB::bind_method(D_METHOD("get_surface_count"), &EditorSceneImporterMesh::get_surface_count); - ClassDB::bind_method(D_METHOD("get_surface_primitive_type", "surface_idx"), &EditorSceneImporterMesh::get_surface_primitive_type); - ClassDB::bind_method(D_METHOD("get_surface_name", "surface_idx"), &EditorSceneImporterMesh::get_surface_name); - ClassDB::bind_method(D_METHOD("get_surface_arrays", "surface_idx"), &EditorSceneImporterMesh::get_surface_arrays); - ClassDB::bind_method(D_METHOD("get_surface_blend_shape_arrays", "surface_idx", "blend_shape_idx"), &EditorSceneImporterMesh::get_surface_blend_shape_arrays); - ClassDB::bind_method(D_METHOD("get_surface_lod_count", "surface_idx"), &EditorSceneImporterMesh::get_surface_lod_count); - ClassDB::bind_method(D_METHOD("get_surface_lod_size", "surface_idx", "lod_idx"), &EditorSceneImporterMesh::get_surface_lod_size); - ClassDB::bind_method(D_METHOD("get_surface_lod_indices", "surface_idx", "lod_idx"), &EditorSceneImporterMesh::get_surface_lod_indices); - ClassDB::bind_method(D_METHOD("get_surface_material", "surface_idx"), &EditorSceneImporterMesh::get_surface_material); - ClassDB::bind_method(D_METHOD("get_surface_format", "surface_idx"), &EditorSceneImporterMesh::get_surface_format); - - ClassDB::bind_method(D_METHOD("set_surface_name", "surface_idx", "name"), &EditorSceneImporterMesh::set_surface_name); - ClassDB::bind_method(D_METHOD("set_surface_material", "surface_idx", "material"), &EditorSceneImporterMesh::set_surface_material); - - ClassDB::bind_method(D_METHOD("get_mesh", "base_mesh"), &EditorSceneImporterMesh::get_mesh, DEFVAL(Ref<ArrayMesh>())); - ClassDB::bind_method(D_METHOD("clear"), &EditorSceneImporterMesh::clear); - - ClassDB::bind_method(D_METHOD("_set_data", "data"), &EditorSceneImporterMesh::_set_data); - ClassDB::bind_method(D_METHOD("_get_data"), &EditorSceneImporterMesh::_get_data); - - ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &EditorSceneImporterMesh::set_lightmap_size_hint); - ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &EditorSceneImporterMesh::get_lightmap_size_hint); - - ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_data", "_get_data"); -} diff --git a/editor/import/scene_importer_mesh.h b/editor/import/scene_importer_mesh.h deleted file mode 100644 index 111b191cae..0000000000 --- a/editor/import/scene_importer_mesh.h +++ /dev/null @@ -1,132 +0,0 @@ -/*************************************************************************/ -/* scene_importer_mesh.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 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. */ -/*************************************************************************/ - -#ifndef EDITOR_SCENE_IMPORTER_MESH_H -#define EDITOR_SCENE_IMPORTER_MESH_H - -#include "core/io/resource.h" -#include "core/templates/local_vector.h" -#include "scene/resources/concave_polygon_shape_3d.h" -#include "scene/resources/convex_polygon_shape_3d.h" -#include "scene/resources/mesh.h" -#include "scene/resources/navigation_mesh.h" - -#include <cstdint> - -// The following classes are used by importers instead of ArrayMesh and MeshInstance3D -// so the data is not registered (hence, quality loss), importing happens faster and -// its easier to modify before saving - -class EditorSceneImporterMesh : public Resource { - GDCLASS(EditorSceneImporterMesh, Resource) - - struct Surface { - Mesh::PrimitiveType primitive; - Array arrays; - struct BlendShape { - Array arrays; - }; - Vector<BlendShape> blend_shape_data; - struct LOD { - Vector<int> indices; - float distance = 0.0f; - }; - Vector<LOD> lods; - Ref<Material> material; - String name; - uint32_t flags = 0; - - struct LODComparator { - _FORCE_INLINE_ bool operator()(const LOD &l, const LOD &r) const { - return l.distance < r.distance; - } - }; - - void split_normals(const LocalVector<int> &p_indices, const LocalVector<Vector3> &p_normals); - }; - Vector<Surface> surfaces; - Vector<String> blend_shapes; - Mesh::BlendShapeMode blend_shape_mode = Mesh::BLEND_SHAPE_MODE_NORMALIZED; - - Ref<ArrayMesh> mesh; - - Ref<EditorSceneImporterMesh> shadow_mesh; - - Size2i lightmap_size_hint; - -protected: - void _set_data(const Dictionary &p_data); - Dictionary _get_data() const; - - static void _bind_methods(); - -public: - void add_blend_shape(const String &p_name); - int get_blend_shape_count() const; - String get_blend_shape_name(int p_blend_shape) const; - - void add_surface(Mesh::PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes = Array(), const Dictionary &p_lods = Dictionary(), const Ref<Material> &p_material = Ref<Material>(), const String &p_name = String(), const uint32_t p_flags = 0); - int get_surface_count() const; - - void set_blend_shape_mode(Mesh::BlendShapeMode p_blend_shape_mode); - Mesh::BlendShapeMode get_blend_shape_mode() const; - - Mesh::PrimitiveType get_surface_primitive_type(int p_surface); - String get_surface_name(int p_surface) const; - void set_surface_name(int p_surface, const String &p_name); - Array get_surface_arrays(int p_surface) const; - Array get_surface_blend_shape_arrays(int p_surface, int p_blend_shape) const; - int get_surface_lod_count(int p_surface) const; - Vector<int> get_surface_lod_indices(int p_surface, int p_lod) const; - float get_surface_lod_size(int p_surface, int p_lod) const; - Ref<Material> get_surface_material(int p_surface) const; - uint32_t get_surface_format(int p_surface) const; - - void set_surface_material(int p_surface, const Ref<Material> &p_material); - - void generate_lods(float p_normal_merge_angle, float p_normal_split_angle); - - void create_shadow_mesh(); - Ref<EditorSceneImporterMesh> get_shadow_mesh() const; - - Vector<Face3> get_faces() const; - Vector<Ref<Shape3D>> convex_decompose(const Mesh::ConvexDecompositionSettings &p_settings) const; - Ref<Shape3D> create_trimesh_shape() const; - Ref<NavigationMesh> create_navigation_mesh(); - Error lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache); - - void set_lightmap_size_hint(const Size2i &p_size); - Size2i get_lightmap_size_hint() const; - - bool has_mesh() const; - Ref<ArrayMesh> get_mesh(const Ref<ArrayMesh> &p_base = Ref<ArrayMesh>()); - void clear(); -}; -#endif // EDITOR_SCENE_IMPORTER_MESH_H diff --git a/editor/import/scene_importer_mesh_node_3d.cpp b/editor/import/scene_importer_mesh_node_3d.cpp deleted file mode 100644 index 3c201cf674..0000000000 --- a/editor/import/scene_importer_mesh_node_3d.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/*************************************************************************/ -/* scene_importer_mesh_node_3d.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 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 "scene_importer_mesh_node_3d.h" - -void EditorSceneImporterMeshNode3D::set_mesh(const Ref<EditorSceneImporterMesh> &p_mesh) { - mesh = p_mesh; -} -Ref<EditorSceneImporterMesh> EditorSceneImporterMeshNode3D::get_mesh() const { - return mesh; -} - -void EditorSceneImporterMeshNode3D::set_skin(const Ref<Skin> &p_skin) { - skin = p_skin; -} -Ref<Skin> EditorSceneImporterMeshNode3D::get_skin() const { - return skin; -} - -void EditorSceneImporterMeshNode3D::set_surface_material(int p_idx, const Ref<Material> &p_material) { - ERR_FAIL_COND(p_idx < 0); - if (p_idx >= surface_materials.size()) { - surface_materials.resize(p_idx + 1); - } - - surface_materials.write[p_idx] = p_material; -} -Ref<Material> EditorSceneImporterMeshNode3D::get_surface_material(int p_idx) const { - ERR_FAIL_COND_V(p_idx < 0, Ref<Material>()); - if (p_idx >= surface_materials.size()) { - return Ref<Material>(); - } - return surface_materials[p_idx]; -} - -void EditorSceneImporterMeshNode3D::set_skeleton_path(const NodePath &p_path) { - skeleton_path = p_path; -} -NodePath EditorSceneImporterMeshNode3D::get_skeleton_path() const { - return skeleton_path; -} - -void EditorSceneImporterMeshNode3D::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &EditorSceneImporterMeshNode3D::set_mesh); - ClassDB::bind_method(D_METHOD("get_mesh"), &EditorSceneImporterMeshNode3D::get_mesh); - - ClassDB::bind_method(D_METHOD("set_skin", "skin"), &EditorSceneImporterMeshNode3D::set_skin); - ClassDB::bind_method(D_METHOD("get_skin"), &EditorSceneImporterMeshNode3D::get_skin); - - ClassDB::bind_method(D_METHOD("set_skeleton_path", "skeleton_path"), &EditorSceneImporterMeshNode3D::set_skeleton_path); - ClassDB::bind_method(D_METHOD("get_skeleton_path"), &EditorSceneImporterMeshNode3D::get_skeleton_path); - - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "EditorSceneImporterMesh"), "set_mesh", "get_mesh"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin"); - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton"), "set_skeleton_path", "get_skeleton_path"); -} diff --git a/editor/import/scene_importer_mesh_node_3d.h b/editor/import/scene_importer_mesh_node_3d.h deleted file mode 100644 index dec1717c99..0000000000 --- a/editor/import/scene_importer_mesh_node_3d.h +++ /dev/null @@ -1,64 +0,0 @@ -/*************************************************************************/ -/* scene_importer_mesh_node_3d.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 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. */ -/*************************************************************************/ - -#ifndef EDITOR_SCENE_IMPORTER_MESH_NODE_3D_H -#define EDITOR_SCENE_IMPORTER_MESH_NODE_3D_H - -#include "editor/import/scene_importer_mesh.h" -#include "scene/3d/node_3d.h" -#include "scene/resources/skin.h" - -class EditorSceneImporterMesh; - -class EditorSceneImporterMeshNode3D : public Node3D { - GDCLASS(EditorSceneImporterMeshNode3D, Node3D) - - Ref<EditorSceneImporterMesh> mesh; - Ref<Skin> skin; - NodePath skeleton_path; - Vector<Ref<Material>> surface_materials; - -protected: - static void _bind_methods(); - -public: - void set_mesh(const Ref<EditorSceneImporterMesh> &p_mesh); - Ref<EditorSceneImporterMesh> get_mesh() const; - - void set_skin(const Ref<Skin> &p_skin); - Ref<Skin> get_skin() const; - - void set_surface_material(int p_idx, const Ref<Material> &p_material); - Ref<Material> get_surface_material(int p_idx) const; - - void set_skeleton_path(const NodePath &p_path); - NodePath get_skeleton_path() const; -}; -#endif diff --git a/editor/plugins/animation_player_editor_plugin.cpp b/editor/plugins/animation_player_editor_plugin.cpp index 68b143358a..902b0aa9ec 100644 --- a/editor/plugins/animation_player_editor_plugin.cpp +++ b/editor/plugins/animation_player_editor_plugin.cpp @@ -1466,15 +1466,15 @@ void AnimationPlayerEditor::_prepare_onion_layers_2() { } void AnimationPlayerEditor::_start_onion_skinning() { - // FIXME: Using "idle_frame" makes onion layers update one frame behind the current. - if (!get_tree()->is_connected("idle_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) { - get_tree()->connect("idle_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred)); + // FIXME: Using "process_frame" makes onion layers update one frame behind the current. + if (!get_tree()->is_connected("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) { + get_tree()->connect("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred)); } } void AnimationPlayerEditor::_stop_onion_skinning() { - if (get_tree()->is_connected("idle_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) { - get_tree()->disconnect("idle_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred)); + if (get_tree()->is_connected("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) { + get_tree()->disconnect("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred)); _free_onion_layers(); diff --git a/editor/plugins/node_3d_editor_gizmos.cpp b/editor/plugins/node_3d_editor_gizmos.cpp index ac7bfd267b..7b0fc07fe7 100644 --- a/editor/plugins/node_3d_editor_gizmos.cpp +++ b/editor/plugins/node_3d_editor_gizmos.cpp @@ -43,13 +43,13 @@ #include "scene/3d/decal.h" #include "scene/3d/gpu_particles_3d.h" #include "scene/3d/gpu_particles_collision_3d.h" +#include "scene/3d/joint_3d.h" #include "scene/3d/light_3d.h" #include "scene/3d/lightmap_gi.h" #include "scene/3d/lightmap_probe.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/navigation_region_3d.h" #include "scene/3d/occluder_instance_3d.h" -#include "scene/3d/physics_joint_3d.h" #include "scene/3d/position_3d.h" #include "scene/3d/ray_cast_3d.h" #include "scene/3d/reflection_probe.h" @@ -1840,47 +1840,6 @@ void Camera3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { p_gizmo->add_lines(lines, material); p_gizmo->add_handles(handles, get_material("handles")); - - ClippedCamera3D *clipcam = Object::cast_to<ClippedCamera3D>(camera); - if (clipcam) { - Node3D *parent = Object::cast_to<Node3D>(camera->get_parent()); - if (!parent) { - return; - } - Vector3 cam_normal = -camera->get_global_transform().basis.get_axis(Vector3::AXIS_Z).normalized(); - Vector3 cam_x = camera->get_global_transform().basis.get_axis(Vector3::AXIS_X).normalized(); - Vector3 cam_y = camera->get_global_transform().basis.get_axis(Vector3::AXIS_Y).normalized(); - Vector3 cam_pos = camera->get_global_transform().origin; - Vector3 parent_pos = parent->get_global_transform().origin; - - Plane parent_plane(parent_pos, cam_normal); - Vector3 ray_from = parent_plane.project(cam_pos); - - lines.clear(); - lines.push_back(ray_from + cam_x * 0.5 + cam_y * 0.5); - lines.push_back(ray_from + cam_x * 0.5 + cam_y * -0.5); - - lines.push_back(ray_from + cam_x * 0.5 + cam_y * -0.5); - lines.push_back(ray_from + cam_x * -0.5 + cam_y * -0.5); - - lines.push_back(ray_from + cam_x * -0.5 + cam_y * -0.5); - lines.push_back(ray_from + cam_x * -0.5 + cam_y * 0.5); - - lines.push_back(ray_from + cam_x * -0.5 + cam_y * 0.5); - lines.push_back(ray_from + cam_x * 0.5 + cam_y * 0.5); - - if (parent_plane.distance_to(cam_pos) < 0) { - lines.push_back(ray_from); - lines.push_back(cam_pos); - } - - Transform3D local = camera->get_global_transform().affine_inverse(); - for (int i = 0; i < lines.size(); i++) { - lines.write[i] = local.xform(lines[i]); - } - - p_gizmo->add_lines(lines, material); - } } ////// diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index dae3b6aaec..f179c01f89 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -2483,8 +2483,14 @@ static bool is_shortcut_pressed(const String &p_path) { if (shortcut.is_null()) { return false; } - InputEventKey *k = Object::cast_to<InputEventKey>(shortcut->get_event().ptr()); - if (k == nullptr) { + + const Array shortcuts = shortcut->get_events(); + Ref<InputEventKey> k; + if (shortcuts.size() > 0) { + k = shortcuts.front(); + } + + if (k.is_null()) { return false; } const Input &input = *Input::get_singleton(); diff --git a/editor/plugins/physical_bone_3d_editor_plugin.cpp b/editor/plugins/physical_bone_3d_editor_plugin.cpp index f92f50f826..b1e104e680 100644 --- a/editor/plugins/physical_bone_3d_editor_plugin.cpp +++ b/editor/plugins/physical_bone_3d_editor_plugin.cpp @@ -43,6 +43,7 @@ void PhysicalBone3DEditor::_on_toggle_button_transform_joint(bool p_is_pressed) void PhysicalBone3DEditor::_set_move_joint() { if (selected) { selected->_set_gizmo_move_joint(button_transform_joint->is_pressed()); + Node3DEditor::get_singleton()->update_transform_gizmo(); } } diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 2b1ca068ee..701d75fb08 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -433,10 +433,12 @@ void ScriptTextEditor::_validate_script() { int warning_nb = warnings.size(); warnings_panel->clear(); + bool has_connections_table = false; // Add missing connections. if (GLOBAL_GET("debug/gdscript/warnings/enable").booleanize()) { Node *base = get_tree()->get_edited_scene_root(); if (base && missing_connections.size() > 0) { + has_connections_table = true; warnings_panel->push_table(1); for (const Connection &connection : missing_connections) { String base_path = base->get_name(); @@ -458,6 +460,10 @@ void ScriptTextEditor::_validate_script() { code_editor->set_error_count(errors.size()); code_editor->set_warning_count(warning_nb); + if (has_connections_table) { + warnings_panel->add_newline(); + } + // Add script warnings. warnings_panel->push_table(3); for (const ScriptLanguage::Warning &w : warnings) { diff --git a/editor/plugins/skeleton_3d_editor_plugin.cpp b/editor/plugins/skeleton_3d_editor_plugin.cpp index 309821b3dc..4e3ab5380b 100644 --- a/editor/plugins/skeleton_3d_editor_plugin.cpp +++ b/editor/plugins/skeleton_3d_editor_plugin.cpp @@ -37,9 +37,9 @@ #include "editor/plugins/animation_player_editor_plugin.h" #include "node_3d_editor_plugin.h" #include "scene/3d/collision_shape_3d.h" +#include "scene/3d/joint_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/physics_body_3d.h" -#include "scene/3d/physics_joint_3d.h" #include "scene/resources/capsule_shape_3d.h" #include "scene/resources/sphere_shape_3d.h" diff --git a/editor/plugins/theme_editor_plugin.cpp b/editor/plugins/theme_editor_plugin.cpp index a1591ce9d8..19e1b40a0d 100644 --- a/editor/plugins/theme_editor_plugin.cpp +++ b/editor/plugins/theme_editor_plugin.cpp @@ -3333,7 +3333,7 @@ ThemeEditor::ThemeEditor() { preview_tabs->set_h_size_flags(SIZE_EXPAND_FILL); preview_tabbar_hb->add_child(preview_tabs); preview_tabs->connect("tab_changed", callable_mp(this, &ThemeEditor::_change_preview_tab)); - preview_tabs->connect("right_button_pressed", callable_mp(this, &ThemeEditor::_remove_preview_tab)); + preview_tabs->connect("tab_rmb_clicked", callable_mp(this, &ThemeEditor::_remove_preview_tab)); HBoxContainer *add_preview_button_hb = memnew(HBoxContainer); preview_tabbar_hb->add_child(add_preview_button_hb); diff --git a/editor/plugins/tiles/tiles_editor_plugin.cpp b/editor/plugins/tiles/tiles_editor_plugin.cpp index f51f4625a9..d0d01a8d49 100644 --- a/editor/plugins/tiles/tiles_editor_plugin.cpp +++ b/editor/plugins/tiles/tiles_editor_plugin.cpp @@ -246,11 +246,9 @@ void TilesEditorPlugin::make_visible(bool p_visible) { if (p_visible) { tiles_editor_button->show(); editor_node->make_bottom_panel_item_visible(tiles_editor); - //get_tree()->connect_compat("idle_frame", tileset_editor, "_on_workspace_process"); } else { editor_node->hide_bottom_panel(); tiles_editor_button->hide(); - //get_tree()->disconnect_compat("idle_frame", tileset_editor, "_on_workspace_process"); } } diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index a100e9fc55..a5b607c0e8 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -411,10 +411,6 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { } } break; case TOOL_EXPAND_COLLAPSE: { - if (!scene_tree->get_selected()) { - break; - } - Tree *tree = scene_tree->get_scene_tree(); TreeItem *selected_item = tree->get_selected(); @@ -979,6 +975,9 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { } EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT); } break; + case TOOL_AUTO_EXPAND: { + scene_tree->set_auto_expand_selected(!EditorSettings::get_singleton()->get("docks/scene_tree/auto_expand_to_selected"), true); + } break; case TOOL_SCENE_EDITABLE_CHILDREN: { if (!profile_allow_editing) { break; @@ -1223,6 +1222,7 @@ void SceneTreeDock::_notification(int p_what) { button_instance->set_icon(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons"))); button_create_script->set_icon(get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons"))); button_detach_script->set_icon(get_theme_icon(SNAME("ScriptRemove"), SNAME("EditorIcons"))); + button_tree_menu->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons"))); filter->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons"))); filter->set_clear_button_enabled(true); @@ -1291,12 +1291,14 @@ void SceneTreeDock::_notification(int p_what) { case NOTIFICATION_ENTER_TREE: { clear_inherit_confirm->connect("confirmed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM, false)); + scene_tree->set_auto_expand_selected(EditorSettings::get_singleton()->get("docks/scene_tree/auto_expand_to_selected"), false); } break; case NOTIFICATION_EXIT_TREE: { clear_inherit_confirm->disconnect("confirmed", callable_mp(this, &SceneTreeDock::_tool_selected)); } break; case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { + scene_tree->set_auto_expand_selected(EditorSettings::get_singleton()->get("docks/scene_tree/auto_expand_to_selected"), false); button_add->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons"))); button_instance->set_icon(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons"))); button_create_script->set_icon(get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons"))); @@ -2694,7 +2696,6 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) { menu->add_icon_shortcut(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW); menu->add_icon_shortcut(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANTIATE); } - menu->add_icon_shortcut(get_theme_icon(SNAME("Collapse"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/expand_collapse_all"), TOOL_EXPAND_COLLAPSE); menu->add_separator(); existing_script = selected->get_script(); @@ -2832,6 +2833,18 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) { menu->popup(); } +void SceneTreeDock::_open_tree_menu() { + menu->clear(); + + menu->add_icon_shortcut(get_theme_icon(SNAME("Collapse"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/expand_collapse_all"), TOOL_EXPAND_COLLAPSE); + menu->add_check_item(TTR("Auto Expand to Selected"), TOOL_AUTO_EXPAND); + menu->set_item_checked(menu->get_item_idx_from_text(TTR("Auto Expand to Selected")), EditorSettings::get_singleton()->get("docks/scene_tree/auto_expand_to_selected")); + + menu->set_size(Size2(1, 1)); + menu->set_position(get_screen_position() + get_local_mouse_position()); + menu->popup(); +} + void SceneTreeDock::_filter_changed(const String &p_filter) { scene_tree->set_filter(p_filter); } @@ -3271,6 +3284,11 @@ SceneTreeDock::SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSel filter_hbc->add_child(button_detach_script); button_detach_script->hide(); + button_tree_menu = memnew(Button); + button_tree_menu->set_flat(true); + button_tree_menu->connect("pressed", callable_mp(this, &SceneTreeDock::_open_tree_menu)); + filter_hbc->add_child(button_tree_menu); + button_hb = memnew(HBoxContainer); vbc->add_child(button_hb); diff --git a/editor/scene_tree_dock.h b/editor/scene_tree_dock.h index ece0ca5ca4..255e026887 100644 --- a/editor/scene_tree_dock.h +++ b/editor/scene_tree_dock.h @@ -81,6 +81,7 @@ class SceneTreeDock : public VBoxContainer { TOOL_COPY_NODE_PATH, TOOL_BUTTON_MAX, TOOL_OPEN_DOCUMENTATION, + TOOL_AUTO_EXPAND, TOOL_SCENE_EDITABLE_CHILDREN, TOOL_SCENE_USE_PLACEHOLDER, TOOL_SCENE_MAKE_LOCAL, @@ -115,6 +116,7 @@ class SceneTreeDock : public VBoxContainer { Button *button_instance; Button *button_create_script; Button *button_detach_script; + Button *button_tree_menu; Button *button_2d; Button *button_3d; @@ -237,6 +239,7 @@ class SceneTreeDock : public VBoxContainer { void _quick_open(); void _tree_rmb(const Vector2 &p_menu_pos); + void _open_tree_menu(); void _filter_changed(const String &p_filter); diff --git a/editor/scene_tree_editor.cpp b/editor/scene_tree_editor.cpp index ccca01ed8b..8ffaf0829e 100644 --- a/editor/scene_tree_editor.cpp +++ b/editor/scene_tree_editor.cpp @@ -737,17 +737,18 @@ void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) { TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : nullptr; if (item) { - // make visible when it's collapsed - TreeItem *node = item->get_parent(); - while (node && node != tree->get_root()) { - node->set_collapsed(false); - node = node->get_parent(); + if (auto_expand_selected) { + // Make visible when it's collapsed. + TreeItem *node = item->get_parent(); + while (node && node != tree->get_root()) { + node->set_collapsed(false); + node = node->get_parent(); + } + item->select(0); + item->set_as_cursor(0); + selected = p_node; + tree->ensure_cursor_is_visible(); } - item->select(0); - item->set_as_cursor(0); - selected = p_node; - tree->ensure_cursor_is_visible(); - } else { if (!p_node) { selected = nullptr; @@ -1127,11 +1128,19 @@ void SceneTreeEditor::_rmb_select(const Vector2 &p_pos) { void SceneTreeEditor::update_warning() { _warning_changed(nullptr); } + void SceneTreeEditor::_warning_changed(Node *p_for_node) { //should use a timer update_timer->start(); } +void SceneTreeEditor::set_auto_expand_selected(bool p_auto, bool p_update_settings) { + if (p_update_settings) { + EditorSettings::get_singleton()->set("docks/scene_tree/auto_expand_to_selected", p_auto); + } + auto_expand_selected = p_auto; +} + void SceneTreeEditor::set_connect_to_script_mode(bool p_enable) { connect_to_script_mode = p_enable; update_tree(); diff --git a/editor/scene_tree_editor.h b/editor/scene_tree_editor.h index acd49e8d92..4acd5d8486 100644 --- a/editor/scene_tree_editor.h +++ b/editor/scene_tree_editor.h @@ -64,6 +64,7 @@ class SceneTreeEditor : public Control { AcceptDialog *error; AcceptDialog *warning; + bool auto_expand_selected = true; bool connect_to_script_mode; bool connecting_signal; @@ -152,6 +153,7 @@ public: void update_tree() { _update_tree(); } + void set_auto_expand_selected(bool p_auto, bool p_update_settings); void set_connect_to_script_mode(bool p_enable); void set_connecting_signal(bool p_enable); diff --git a/editor/settings_config_dialog.cpp b/editor/settings_config_dialog.cpp index 9c579f3e9f..9062169e06 100644 --- a/editor/settings_config_dialog.cpp +++ b/editor/settings_config_dialog.cpp @@ -193,35 +193,25 @@ void EditorSettingsDialog::_event_config_confirmed() { return; } - if (editing_action) { - if (current_action_event_index == -1) { - // Add new event - current_action_events.push_back(k); - } else { - // Edit existing event - current_action_events[current_action_event_index] = k; - } + if (current_event_index == -1) { + // Add new event + current_events.push_back(k); + } else { + // Edit existing event + current_events[current_event_index] = k; + } - _update_builtin_action(current_action, current_action_events); + if (is_editing_action) { + _update_builtin_action(current_edited_identifier, current_events); } else { - k = k->duplicate(); - Ref<Shortcut> current_sc = EditorSettings::get_singleton()->get_shortcut(shortcut_being_edited); - - undo_redo->create_action(TTR("Change Shortcut") + " '" + shortcut_being_edited + "'"); - undo_redo->add_do_method(current_sc.ptr(), "set_event", k); - undo_redo->add_undo_method(current_sc.ptr(), "set_event", current_sc->get_event()); - undo_redo->add_do_method(this, "_update_shortcuts"); - undo_redo->add_undo_method(this, "_update_shortcuts"); - undo_redo->add_do_method(this, "_settings_changed"); - undo_redo->add_undo_method(this, "_settings_changed"); - undo_redo->commit_action(); + _update_shortcut_events(current_edited_identifier, current_events); } } void EditorSettingsDialog::_update_builtin_action(const String &p_name, const Array &p_events) { - Array old_input_array = EditorSettings::get_singleton()->get_builtin_action_overrides(current_action); + Array old_input_array = EditorSettings::get_singleton()->get_builtin_action_overrides(p_name); - undo_redo->create_action(TTR("Edit Built-in Action")); + undo_redo->create_action(TTR("Edit Built-in Action") + " '" + p_name + "'"); undo_redo->add_do_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, p_events); undo_redo->add_undo_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, old_input_array); undo_redo->add_do_method(this, "_settings_changed"); @@ -231,15 +221,125 @@ void EditorSettingsDialog::_update_builtin_action(const String &p_name, const Ar _update_shortcuts(); } +void EditorSettingsDialog::_update_shortcut_events(const String &p_path, const Array &p_events) { + Ref<Shortcut> current_sc = EditorSettings::get_singleton()->get_shortcut(p_path); + + undo_redo->create_action(TTR("Edit Shortcut") + " '" + p_path + "'"); + undo_redo->add_do_method(current_sc.ptr(), "set_events", p_events); + undo_redo->add_undo_method(current_sc.ptr(), "set_events", current_sc->get_events()); + undo_redo->add_do_method(this, "_update_shortcuts"); + undo_redo->add_undo_method(this, "_update_shortcuts"); + undo_redo->add_do_method(this, "_settings_changed"); + undo_redo->add_undo_method(this, "_settings_changed"); + undo_redo->commit_action(); +} + +Array EditorSettingsDialog::_event_list_to_array_helper(List<Ref<InputEvent>> &p_events) { + Array events; + + // Convert the list to an array, and only keep key events as this is for the editor. + for (List<Ref<InputEvent>>::Element *E = p_events.front(); E; E = E->next()) { + Ref<InputEventKey> k = E->get(); + if (k.is_valid()) { + events.append(E->get()); + } + } + + return events; +} + +void EditorSettingsDialog::_create_shortcut_treeitem(TreeItem *p_parent, const String &p_shortcut_identifier, const String &p_display, Array &p_events, bool p_allow_revert, bool p_is_action, bool p_is_collapsed) { + TreeItem *shortcut_item = shortcuts->create_item(p_parent); + shortcut_item->set_collapsed(p_is_collapsed); + shortcut_item->set_text(0, p_display); + + Ref<InputEvent> primary = p_events.size() > 0 ? Ref<InputEvent>(p_events[0]) : Ref<InputEvent>(); + Ref<InputEvent> secondary = p_events.size() > 1 ? Ref<InputEvent>(p_events[1]) : Ref<InputEvent>(); + + String sc_text = "None"; + if (primary.is_valid()) { + sc_text = primary->as_text(); + + if (secondary.is_valid()) { + sc_text += ", " + secondary->as_text(); + + if (p_events.size() > 2) { + sc_text += " (+" + itos(p_events.size() - 2) + ")"; + } + } + } + + shortcut_item->set_text(1, sc_text); + if (sc_text == "None") { + // Fade out unassigned shortcut labels for easier visual grepping. + shortcut_item->set_custom_color(1, shortcuts->get_theme_color("font_color", "Label") * Color(1, 1, 1, 0.5)); + } + + if (p_allow_revert) { + shortcut_item->add_button(1, shortcuts->get_theme_icon("Reload", "EditorIcons"), SHORTCUT_REVERT); + } + + shortcut_item->add_button(1, shortcuts->get_theme_icon("Add", "EditorIcons"), SHORTCUT_ADD); + shortcut_item->add_button(1, shortcuts->get_theme_icon("Close", "EditorIcons"), SHORTCUT_ERASE); + + shortcut_item->set_meta("is_action", p_is_action); + shortcut_item->set_meta("type", "shortcut"); + shortcut_item->set_meta("shortcut_identifier", p_shortcut_identifier); + shortcut_item->set_meta("events", p_events); + + // Shortcut Input Events + for (int i = 0; i < p_events.size(); i++) { + Ref<InputEvent> ie = p_events[i]; + if (ie.is_null()) { + continue; + } + + TreeItem *event_item = shortcuts->create_item(shortcut_item); + + event_item->set_text(0, shortcut_item->get_child_count() == 1 ? "Primary" : ""); + event_item->set_text(1, ie->as_text()); + + event_item->add_button(1, shortcuts->get_theme_icon("Edit", "EditorIcons"), SHORTCUT_EDIT); + event_item->add_button(1, shortcuts->get_theme_icon("Close", "EditorIcons"), SHORTCUT_ERASE); + + event_item->set_custom_bg_color(0, shortcuts->get_theme_color("dark_color_3", "Editor")); + event_item->set_custom_bg_color(1, shortcuts->get_theme_color("dark_color_3", "Editor")); + + event_item->set_meta("is_action", p_is_action); + event_item->set_meta("type", "event"); + event_item->set_meta("event_index", i); + } +} + void EditorSettingsDialog::_update_shortcuts() { // Before clearing the tree, take note of which categories are collapsed so that this state can be maintained when the tree is repopulated. Map<String, bool> collapsed; if (shortcuts->get_root() && shortcuts->get_root()->get_first_child()) { - for (TreeItem *item = shortcuts->get_root()->get_first_child(); item; item = item->get_next()) { - collapsed[item->get_text(0)] = item->is_collapsed(); + TreeItem *ti = shortcuts->get_root()->get_first_child(); + while (ti) { + // Not all items have valid or unique text in the first column - so if it has an identifier, use that, as it should be unique. + if (ti->get_first_child() && ti->has_meta("shortcut_identifier")) { + collapsed[ti->get_meta("shortcut_identifier")] = ti->is_collapsed(); + } else { + collapsed[ti->get_text(0)] = ti->is_collapsed(); + } + + // Try go down tree + TreeItem *ti_next = ti->get_first_child(); + // Try go across tree + if (!ti_next) { + ti_next = ti->get_next(); + } + // Try go up tree, to next node + if (!ti_next) { + ti_next = ti->get_parent()->get_next(); + } + + ti = ti_next; } } + shortcuts->clear(); TreeItem *root = shortcuts->create_item(); @@ -247,7 +347,6 @@ void EditorSettingsDialog::_update_shortcuts() { // Set up section for Common/Built-in actions TreeItem *common_section = shortcuts->create_item(root); - sections["Common"] = common_section; common_section->set_text(0, TTR("Common")); common_section->set_selectable(0, false); @@ -262,7 +361,6 @@ void EditorSettingsDialog::_update_shortcuts() { OrderedHashMap<StringName, InputMap::Action> action_map = InputMap::get_singleton()->get_action_map(); for (OrderedHashMap<StringName, InputMap::Action>::Element E = action_map.front(); E; E = E.next()) { String action_name = E.key(); - InputMap::Action action = E.get(); Array events; // Need to get the list of events into an array so it can be set as metadata on the item. @@ -278,21 +376,6 @@ void EditorSettingsDialog::_update_shortcuts() { } } - bool same_as_defaults = key_default_events.size() == action.inputs.size(); // Initially this is set to just whether the arrays are equal. Later we check the events if needed. - - int count = 0; - for (List<Ref<InputEvent>>::Element *I = action.inputs.front(); I; I = I->next()) { - // Add event and event text to respective arrays. - events.push_back(I->get()); - event_strings.push_back(I->get()->as_text()); - - // Only check if the events have been the same so far - once one fails, we don't need to check any more. - if (same_as_defaults && !key_default_events[count]->is_match(I->get())) { - same_as_defaults = false; - } - count++; - } - // Join the text of the events with a delimiter so they can all be displayed in one cell. String events_display_string = event_strings.is_empty() ? "None" : String("; ").join(event_strings); @@ -300,25 +383,12 @@ void EditorSettingsDialog::_update_shortcuts() { continue; } - TreeItem *item = shortcuts->create_item(common_section); - item->set_text(0, action_name); - item->set_text(1, events_display_string); - - if (!same_as_defaults) { - item->add_button(1, shortcuts->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")), 2); - } - - if (events_display_string == "None") { - // Fade out unassigned shortcut labels for easier visual grepping. - item->set_custom_color(1, shortcuts->get_theme_color(SNAME("font_color"), SNAME("Label")) * Color(1, 1, 1, 0.5)); - } + Array action_events = _event_list_to_array_helper(action.inputs); + Array default_events = _event_list_to_array_helper(all_default_events); + bool same_as_defaults = Shortcut::is_event_array_equal(default_events, action_events); + bool collapse = !collapsed.has(action_name) || (collapsed.has(action_name) && collapsed[action_name]); - item->add_button(1, shortcuts->get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")), 0); - item->add_button(1, shortcuts->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")), 1); - item->set_tooltip(0, action_name); - item->set_tooltip(1, events_display_string); - item->set_metadata(0, "Common"); - item->set_metadata(1, events); + _create_shortcut_treeitem(common_section, action_name, action_name, action_events, !same_as_defaults, true, collapse); } // Editor Shortcuts @@ -332,11 +402,10 @@ void EditorSettingsDialog::_update_shortcuts() { continue; } - Ref<InputEvent> original = sc->get_meta("original"); - - String section_name = E.get_slice("/", 0); + // Shortcut Section TreeItem *section; + String section_name = E.get_slice("/", 0); if (sections.has(section_name)) { section = sections[section_name]; @@ -357,28 +426,18 @@ void EditorSettingsDialog::_update_shortcuts() { sections[section_name] = section; } - // Don't match unassigned shortcuts when searching for assigned keys in search results. - // This prevents all unassigned shortcuts from appearing when searching a string like "no". - if (shortcut_filter.is_subsequence_ofi(sc->get_name()) || (sc->get_as_text() != "None" && shortcut_filter.is_subsequence_ofi(sc->get_as_text()))) { - TreeItem *item = shortcuts->create_item(section); - - item->set_text(0, sc->get_name()); - item->set_text(1, sc->get_as_text()); + // Shortcut Item - if (!sc->matches_event(original) && !(sc->get_event().is_null() && original.is_null())) { - item->add_button(1, shortcuts->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")), 2); - } + if (!shortcut_filter.is_subsequence_ofi(sc->get_name())) { + continue; + } - if (sc->get_as_text() == "None") { - // Fade out unassigned shortcut labels for easier visual grepping. - item->set_custom_color(1, shortcuts->get_theme_color(SNAME("font_color"), SNAME("Label")) * Color(1, 1, 1, 0.5)); - } + Array original = sc->get_meta("original"); + Array shortcuts_array = sc->get_events(); + bool same_as_defaults = Shortcut::is_event_array_equal(original, shortcuts_array); + bool collapse = !collapsed.has(E) || (collapsed.has(E) && collapsed[E]); - item->add_button(1, shortcuts->get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")), 0); - item->add_button(1, shortcuts->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")), 1); - item->set_tooltip(0, E); - item->set_metadata(0, E); - } + _create_shortcut_treeitem(section, E, sc->get_name(), shortcuts_array, !same_as_defaults, false, collapse); } // remove sections with no shortcuts @@ -392,123 +451,130 @@ void EditorSettingsDialog::_update_shortcuts() { void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column, int p_idx) { TreeItem *ti = Object::cast_to<TreeItem>(p_item); - ERR_FAIL_COND(!ti); - - button_idx = p_idx; + ERR_FAIL_COND_MSG(!ti, "Object passed is not a TreeItem"); - if (ti->get_metadata(0) == "Common") { - // Editing a Built-in action, which can have multiple bindings. - editing_action = true; - current_action = ti->get_text(0); + ShortcutButton button_idx = (ShortcutButton)p_idx; - switch (button_idx) { - case SHORTCUT_REVERT: { - Array events; - List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied()[current_action]; + is_editing_action = ti->get_meta("is_action"); - // Convert the list to an array, and only keep key events as this is for the editor. - for (const Ref<InputEvent> &k : defaults) { - if (k.is_valid()) { - events.append(k); - } - } + String type = ti->get_meta("type"); - _update_builtin_action(current_action, events); - } break; - case SHORTCUT_EDIT: - case SHORTCUT_ERASE: { - // For Edit end Delete, we will show a popup which displays each event so the user can select which one to edit/delete. - current_action_events = ti->get_metadata(1); - action_popup->clear(); - - for (int i = 0; i < current_action_events.size(); i++) { - Ref<InputEvent> ie = current_action_events[i]; - action_popup->add_item(ie->as_text()); - action_popup->set_item_metadata(i, ie); - } - - if (button_idx == SHORTCUT_EDIT) { - // If editing, add a button which can be used to add an additional event. - action_popup->add_icon_item(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), TTR("Add")); - } + if (type == "event") { + current_edited_identifier = ti->get_parent()->get_meta("shortcut_identifier"); + current_events = ti->get_parent()->get_meta("events"); + current_event_index = ti->get_meta("event_index"); + } else { // Type is "shortcut" + current_edited_identifier = ti->get_meta("shortcut_identifier"); + current_events = ti->get_meta("events"); + current_event_index = -1; + } - action_popup->set_position(get_position() + get_mouse_position()); - action_popup->take_mouse_focus(); - action_popup->popup(); - action_popup->set_as_minsize(); - } break; - default: - break; - } - } else { - // Editing an Editor Shortcut, which can only have 1 binding. - String item = ti->get_metadata(0); - Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(item); - editing_action = false; - - switch (button_idx) { - case EditorSettingsDialog::SHORTCUT_EDIT: - shortcut_editor->popup_and_configure(sc->get_event()); - shortcut_being_edited = item; - break; - case EditorSettingsDialog::SHORTCUT_ERASE: { - if (!sc.is_valid()) { - return; //pointless, there is nothing + switch (button_idx) { + case EditorSettingsDialog::SHORTCUT_ADD: { + // Only for "shortcut" types + shortcut_editor->popup_and_configure(); + } break; + case EditorSettingsDialog::SHORTCUT_EDIT: { + // Only for "event" types + shortcut_editor->popup_and_configure(current_events[current_event_index]); + } break; + case EditorSettingsDialog::SHORTCUT_ERASE: { + if (type == "shortcut") { + if (is_editing_action) { + _update_builtin_action(current_edited_identifier, Array()); + } else { + _update_shortcut_events(current_edited_identifier, Array()); } + } else if (type == "event") { + current_events.remove(current_event_index); - undo_redo->create_action(TTR("Erase Shortcut")); - undo_redo->add_do_method(sc.ptr(), "set_event", Ref<InputEvent>()); - undo_redo->add_undo_method(sc.ptr(), "set_event", sc->get_event()); - undo_redo->add_do_method(this, "_update_shortcuts"); - undo_redo->add_undo_method(this, "_update_shortcuts"); - undo_redo->add_do_method(this, "_settings_changed"); - undo_redo->add_undo_method(this, "_settings_changed"); - undo_redo->commit_action(); - } break; - case EditorSettingsDialog::SHORTCUT_REVERT: { - if (!sc.is_valid()) { - return; //pointless, there is nothing + if (is_editing_action) { + _update_builtin_action(current_edited_identifier, current_events); + } else { + _update_shortcut_events(current_edited_identifier, current_events); } + } + } break; + case EditorSettingsDialog::SHORTCUT_REVERT: { + // Only for "shortcut" types + if (is_editing_action) { + List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied()[current_edited_identifier]; + Array events = _event_list_to_array_helper(defaults); - Ref<InputEvent> original = sc->get_meta("original"); - - undo_redo->create_action(TTR("Restore Shortcut")); - undo_redo->add_do_method(sc.ptr(), "set_event", original); - undo_redo->add_undo_method(sc.ptr(), "set_event", sc->get_event()); - undo_redo->add_do_method(this, "_update_shortcuts"); - undo_redo->add_undo_method(this, "_update_shortcuts"); - undo_redo->add_do_method(this, "_settings_changed"); - undo_redo->add_undo_method(this, "_settings_changed"); - undo_redo->commit_action(); - } break; - default: - break; - } - } -} - -void EditorSettingsDialog::_builtin_action_popup_index_pressed(int p_index) { - switch (button_idx) { - case SHORTCUT_EDIT: { - if (p_index == action_popup->get_item_count() - 1) { - // Selected last item in list (Add button), therefore add new - current_action_event_index = -1; - shortcut_editor->popup_and_configure(); + _update_builtin_action(current_edited_identifier, events); } else { - // Configure existing - current_action_event_index = p_index; - shortcut_editor->popup_and_configure(action_popup->get_item_metadata(p_index)); + Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(current_edited_identifier); + Array original = sc->get_meta("original"); + _update_shortcut_events(current_edited_identifier, original); } } break; - case SHORTCUT_ERASE: { - current_action_events.remove(p_index); - _update_builtin_action(current_action, current_action_events); - } break; default: break; } } +Variant EditorSettingsDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) { + TreeItem *selected = shortcuts->get_selected(); + + // Only allow drag for events + if (!selected || !selected->has_meta("type") || selected->get_meta("type") != "event") { + return Variant(); + } + + String label_text = "Event " + itos(selected->get_meta("event_index")); + Label *label = memnew(Label(label_text)); + label->set_modulate(Color(1, 1, 1, 1.0f)); + shortcuts->set_drag_preview(label); + + shortcuts->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN); + + return Dictionary(); // No data required +} + +bool EditorSettingsDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { + TreeItem *selected = shortcuts->get_selected(); + TreeItem *item = shortcuts->get_item_at_position(p_point); + if (!selected || !item || item == selected || !item->has_meta("type") || item->get_meta("type") != "event") { + return false; + } + + // Don't allow moving an events in-between shortcuts. + if (selected->get_parent()->get_meta("shortcut_identifier") != item->get_parent()->get_meta("shortcut_identifier")) { + return false; + } + + return true; +} + +void EditorSettingsDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { + if (!can_drop_data_fw(p_point, p_data, p_from)) { + return; + } + + TreeItem *selected = shortcuts->get_selected(); + TreeItem *target = shortcuts->get_item_at_position(p_point); + + if (!target) { + return; + } + + int target_event_index = target->get_meta("event_index"); + int index_moving_from = selected->get_meta("event_index"); + + Array events = selected->get_parent()->get_meta("events"); + + Variant event_moved = events[index_moving_from]; + events.remove(index_moving_from); + events.insert(target_event_index, event_moved); + + String ident = selected->get_parent()->get_meta("shortcut_identifier"); + if (selected->get_meta("is_action")) { + _update_builtin_action(ident, events); + } else { + _update_shortcut_events(ident, events); + } +} + void EditorSettingsDialog::_tabs_tab_changed(int p_tab) { _focus_current_search_box(); } @@ -544,13 +610,13 @@ void EditorSettingsDialog::_editor_restart_close() { void EditorSettingsDialog::_bind_methods() { ClassDB::bind_method(D_METHOD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts); ClassDB::bind_method(D_METHOD("_settings_changed"), &EditorSettingsDialog::_settings_changed); + + ClassDB::bind_method(D_METHOD("_get_drag_data_fw"), &EditorSettingsDialog::get_drag_data_fw); + ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &EditorSettingsDialog::can_drop_data_fw); + ClassDB::bind_method(D_METHOD("_drop_data_fw"), &EditorSettingsDialog::drop_data_fw); } EditorSettingsDialog::EditorSettingsDialog() { - action_popup = memnew(PopupMenu); - action_popup->connect("index_pressed", callable_mp(this, &EditorSettingsDialog::_builtin_action_popup_index_pressed)); - add_child(action_popup); - set_title(TTR("Editor Settings")); undo_redo = memnew(UndoRedo); @@ -628,6 +694,8 @@ EditorSettingsDialog::EditorSettingsDialog() { shortcuts->connect("button_pressed", callable_mp(this, &EditorSettingsDialog::_shortcut_button_pressed)); tab_shortcuts->add_child(shortcuts); + shortcuts->set_drag_forwarding(this); + // Adding event dialog shortcut_editor = memnew(InputEventConfigurationDialog); shortcut_editor->connect("confirmed", callable_mp(this, &EditorSettingsDialog::_event_config_confirmed)); diff --git a/editor/settings_config_dialog.h b/editor/settings_config_dialog.h index 2b6c9b3e1d..7317a014b2 100644 --- a/editor/settings_config_dialog.h +++ b/editor/settings_config_dialog.h @@ -53,29 +53,28 @@ class EditorSettingsDialog : public AcceptDialog { LineEdit *shortcut_search_box; SectionedInspector *inspector; + // Shortcuts enum ShortcutButton { + SHORTCUT_ADD, SHORTCUT_EDIT, SHORTCUT_ERASE, SHORTCUT_REVERT }; - int button_idx; - int current_action_event_index = -1; - bool editing_action = false; - String current_action; - Array current_action_events; - PopupMenu *action_popup; + Tree *shortcuts; + String shortcut_filter; + + InputEventConfigurationDialog *shortcut_editor; + + bool is_editing_action = false; + String current_edited_identifier; + Array current_events; + int current_event_index = -1; Timer *timer; UndoRedo *undo_redo; - // Shortcuts - String shortcut_filter; - Tree *shortcuts; - InputEventConfigurationDialog *shortcut_editor; - String shortcut_being_edited; - virtual void cancel_pressed() override; virtual void ok_pressed() override; @@ -89,7 +88,14 @@ class EditorSettingsDialog : public AcceptDialog { void _event_config_confirmed(); + void _create_shortcut_treeitem(TreeItem *p_parent, const String &p_shortcut_identifier, const String &p_display, Array &p_events, bool p_allow_revert, bool p_is_common, bool p_is_collapsed); + Array _event_list_to_array_helper(List<Ref<InputEvent>> &p_events); void _update_builtin_action(const String &p_name, const Array &p_events); + void _update_shortcut_events(const String &p_path, const Array &p_events); + + Variant get_drag_data_fw(const Point2 &p_point, Control *p_from); + bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const; + void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from); void _tabs_tab_changed(int p_tab); void _focus_current_search_box(); |