diff --git a/editor/plugins/sprite_2d_editor_plugin.cpp b/editor/plugins/sprite_2d_editor_plugin.cpp index e90609cd2fe..56d74bba0ad 100644 --- a/editor/plugins/sprite_2d_editor_plugin.cpp +++ b/editor/plugins/sprite_2d_editor_plugin.cpp @@ -127,59 +127,57 @@ void Sprite2DEditor::_menu_option(int p_option) { debug_uv_dialog->set_ok_button_text(TTR("Create MeshInstance2D")); debug_uv_dialog->set_title(TTR("MeshInstance2D Preview")); - _update_mesh_data(); - debug_uv_dialog->popup_centered(); - debug_uv->queue_redraw(); - + _popup_debug_uv_dialog(); } break; case MENU_OPTION_CONVERT_TO_POLYGON_2D: { debug_uv_dialog->set_ok_button_text(TTR("Create Polygon2D")); debug_uv_dialog->set_title(TTR("Polygon2D Preview")); - _update_mesh_data(); - debug_uv_dialog->popup_centered(); - debug_uv->queue_redraw(); + _popup_debug_uv_dialog(); } break; case MENU_OPTION_CREATE_COLLISION_POLY_2D: { debug_uv_dialog->set_ok_button_text(TTR("Create CollisionPolygon2D")); debug_uv_dialog->set_title(TTR("CollisionPolygon2D Preview")); - _update_mesh_data(); - debug_uv_dialog->popup_centered(); - debug_uv->queue_redraw(); - + _popup_debug_uv_dialog(); } break; case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: { debug_uv_dialog->set_ok_button_text(TTR("Create LightOccluder2D")); debug_uv_dialog->set_title(TTR("LightOccluder2D Preview")); - _update_mesh_data(); - debug_uv_dialog->popup_centered(); - debug_uv->queue_redraw(); - + _popup_debug_uv_dialog(); } break; } } -void Sprite2DEditor::_update_mesh_data() { +void Sprite2DEditor::_popup_debug_uv_dialog() { + String error_message; if (node->get_owner() != get_tree()->get_edited_scene_root()) { - err_dialog->set_text(TTR("Can't convert a Sprite2D from a foreign scene.")); - err_dialog->popup_centered(); + error_message = TTR("Can't convert a sprite from a foreign scene."); } - Ref texture = node->get_texture(); if (texture.is_null()) { - err_dialog->set_text(TTR("Sprite2D is empty!")); - err_dialog->popup_centered(); - return; + error_message = TTR("Can't convert an empty sprite to mesh."); } - if (node->get_hframes() > 1 || node->get_vframes() > 1) { - err_dialog->set_text(TTR("Can't convert a sprite using animation frames to mesh.")); + error_message = TTR("Can't convert a sprite using animation frames to mesh."); + } + + if (!error_message.is_empty()) { + err_dialog->set_text(error_message); err_dialog->popup_centered(); return; } + _update_mesh_data(); + debug_uv_dialog->popup_centered(); + debug_uv->queue_redraw(); +} + +void Sprite2DEditor::_update_mesh_data() { + ERR_FAIL_NULL(node); + Ref texture = node->get_texture(); + ERR_FAIL_COND(texture.is_null()); Ref image = texture->get_image(); ERR_FAIL_COND(image.is_null()); @@ -187,12 +185,9 @@ void Sprite2DEditor::_update_mesh_data() { image->decompress(); } + // TODO: Add support for Sprite2D's region. Rect2 rect; - if (node->is_region_enabled()) { - rect = node->get_region_rect(); - } else { - rect.size = image->get_size(); - } + rect.size = image->get_size(); Ref bm; bm.instantiate(); diff --git a/editor/plugins/sprite_2d_editor_plugin.h b/editor/plugins/sprite_2d_editor_plugin.h index 4d351e21832..52e4b2b2643 100644 --- a/editor/plugins/sprite_2d_editor_plugin.h +++ b/editor/plugins/sprite_2d_editor_plugin.h @@ -79,6 +79,7 @@ class Sprite2DEditor : public Control { friend class Sprite2DEditorPlugin; void _debug_uv_draw(); + void _popup_debug_uv_dialog(); void _update_mesh_data(); void _create_node(); diff --git a/editor/plugins/texture_region_editor_plugin.cpp b/editor/plugins/texture_region_editor_plugin.cpp index 8df7be766b3..f5bff6f3df9 100644 --- a/editor/plugins/texture_region_editor_plugin.cpp +++ b/editor/plugins/texture_region_editor_plugin.cpp @@ -54,7 +54,7 @@ Transform2D TextureRegionEditor::_get_offset_transform() const { } void TextureRegionEditor::_texture_preview_draw() { - Ref object_texture = _get_edited_object_texture(); + const Ref object_texture = _get_edited_object_texture(); if (object_texture.is_null()) { return; } @@ -68,7 +68,7 @@ void TextureRegionEditor::_texture_preview_draw() { } void TextureRegionEditor::_texture_overlay_draw() { - Ref object_texture = _get_edited_object_texture(); + const Ref object_texture = _get_edited_object_texture(); if (object_texture.is_null()) { return; } @@ -746,7 +746,7 @@ void TextureRegionEditor::_update_autoslice() { autoslice_is_dirty = false; autoslice_cache.clear(); - Ref object_texture = _get_edited_object_texture(); + const Ref object_texture = _get_edited_object_texture(); if (object_texture.is_null()) { return; } @@ -860,14 +860,6 @@ void TextureRegionEditor::_node_removed(Node *p_node) { } void TextureRegionEditor::_clear_edited_object() { - node_sprite_2d = nullptr; - node_sprite_3d = nullptr; - node_ninepatch = nullptr; - res_stylebox = Ref(); - res_atlas_texture = Ref(); -} - -void TextureRegionEditor::edit(Object *p_obj) { if (node_sprite_2d) { node_sprite_2d->disconnect("texture_changed", callable_mp(this, &TextureRegionEditor::_texture_changed)); } @@ -884,6 +876,14 @@ void TextureRegionEditor::edit(Object *p_obj) { res_atlas_texture->disconnect_changed(callable_mp(this, &TextureRegionEditor::_texture_changed)); } + node_sprite_2d = nullptr; + node_sprite_3d = nullptr; + node_ninepatch = nullptr; + res_stylebox = Ref(); + res_atlas_texture = Ref(); +} + +void TextureRegionEditor::edit(Object *p_obj) { _clear_edited_object(); if (p_obj) { @@ -950,8 +950,9 @@ Rect2 TextureRegionEditor::_get_edited_object_region() const { region = res_atlas_texture->get_region(); } - if (region == Rect2()) { - region = Rect2(Vector2(), _get_edited_object_texture()->get_size()); + const Ref object_texture = _get_edited_object_texture(); + if (region == Rect2() && object_texture.is_valid()) { + region = Rect2(Vector2(), object_texture->get_size()); } return region; @@ -965,7 +966,7 @@ void TextureRegionEditor::_texture_changed() { } void TextureRegionEditor::_edit_region() { - Ref object_texture = _get_edited_object_texture(); + const Ref object_texture = _get_edited_object_texture(); if (object_texture.is_null()) { _zoom_reset(); hscroll->hide();