Skip to content

Commit

Permalink
Merge pull request #84361 from YuriSizov/editor-fix-sprite2d-regional…
Browse files Browse the repository at this point in the history
…-issues

Prevent crash and error spam related to Sprite2D with a region
  • Loading branch information
YuriSizov committed Nov 6, 2023
2 parents 3c68ab6 + 111a5e9 commit fae8ace
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 43 deletions.
53 changes: 24 additions & 29 deletions editor/plugins/sprite_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,72 +127,67 @@ 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<Texture2D> 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<Texture2D> texture = node->get_texture();
ERR_FAIL_COND(texture.is_null());
Ref<Image> image = texture->get_image();
ERR_FAIL_COND(image.is_null());

if (image->is_compressed()) {
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<BitMap> bm;
bm.instantiate();
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/sprite_2d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
29 changes: 15 additions & 14 deletions editor/plugins/texture_region_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Transform2D TextureRegionEditor::_get_offset_transform() const {
}

void TextureRegionEditor::_texture_preview_draw() {
Ref<Texture2D> object_texture = _get_edited_object_texture();
const Ref<Texture2D> object_texture = _get_edited_object_texture();
if (object_texture.is_null()) {
return;
}
Expand All @@ -68,7 +68,7 @@ void TextureRegionEditor::_texture_preview_draw() {
}

void TextureRegionEditor::_texture_overlay_draw() {
Ref<Texture2D> object_texture = _get_edited_object_texture();
const Ref<Texture2D> object_texture = _get_edited_object_texture();
if (object_texture.is_null()) {
return;
}
Expand Down Expand Up @@ -746,7 +746,7 @@ void TextureRegionEditor::_update_autoslice() {
autoslice_is_dirty = false;
autoslice_cache.clear();

Ref<Texture2D> object_texture = _get_edited_object_texture();
const Ref<Texture2D> object_texture = _get_edited_object_texture();
if (object_texture.is_null()) {
return;
}
Expand Down Expand Up @@ -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<StyleBoxTexture>();
res_atlas_texture = Ref<AtlasTexture>();
}

void TextureRegionEditor::edit(Object *p_obj) {
if (node_sprite_2d) {
node_sprite_2d->disconnect("texture_changed", callable_mp(this, &TextureRegionEditor::_texture_changed));
}
Expand All @@ -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<StyleBoxTexture>();
res_atlas_texture = Ref<AtlasTexture>();
}

void TextureRegionEditor::edit(Object *p_obj) {
_clear_edited_object();

if (p_obj) {
Expand Down Expand Up @@ -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<Texture2D> object_texture = _get_edited_object_texture();
if (region == Rect2() && object_texture.is_valid()) {
region = Rect2(Vector2(), object_texture->get_size());
}

return region;
Expand All @@ -965,7 +966,7 @@ void TextureRegionEditor::_texture_changed() {
}

void TextureRegionEditor::_edit_region() {
Ref<Texture2D> object_texture = _get_edited_object_texture();
const Ref<Texture2D> object_texture = _get_edited_object_texture();
if (object_texture.is_null()) {
_zoom_reset();
hscroll->hide();
Expand Down

0 comments on commit fae8ace

Please sign in to comment.