diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-07-22 23:33:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-22 23:33:11 +0200 |
commit | 90aab0402bcaa65c8f0dc2b15a0d74b7508bbfe7 (patch) | |
tree | 2cadebf01e5b573e762ce2a2e3adde97efd977c8 | |
parent | c2c82de49de93d58f8be1255ca787e2aa8c7f129 (diff) | |
parent | 8ad00087ce67771f4cdb072b87a13c9a22c84ecc (diff) |
Merge pull request #50746 from LightningAA/fix-texture-preview-crash-4.0
Fix `TexturePreview` crashing
-rw-r--r-- | editor/plugins/texture_editor_plugin.cpp | 35 | ||||
-rw-r--r-- | editor/plugins/texture_editor_plugin.h | 6 |
2 files changed, 32 insertions, 9 deletions
diff --git a/editor/plugins/texture_editor_plugin.cpp b/editor/plugins/texture_editor_plugin.cpp index 4029d6785c..2eee9b8de3 100644 --- a/editor/plugins/texture_editor_plugin.cpp +++ b/editor/plugins/texture_editor_plugin.cpp @@ -36,9 +36,28 @@ TextureRect *TexturePreview::get_texture_display() { return texture_display; } +void TexturePreview::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: + case NOTIFICATION_THEME_CHANGED: { + if (!is_inside_tree()) { + // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED` + // is getting called for some reason when the `TexturePreview` is + // getting destroyed, which causes `get_theme_font()` to return `nullptr`. + // See https://github.com/godotengine/godot/issues/50743. + break; + } + + Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), SNAME("EditorFonts")); + metadata_label->add_theme_font_override("font", metadata_label_font); + + checkerboard->set_texture(get_theme_icon(SNAME("Checkerboard"), SNAME("EditorIcons"))); + } break; + } +} + TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) { - TextureRect *checkerboard = memnew(TextureRect); - checkerboard->set_texture(get_theme_icon("Checkerboard", "EditorIcons")); + checkerboard = memnew(TextureRect); checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE); checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED); checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); @@ -52,7 +71,7 @@ TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) { add_child(texture_display); if (p_show_metadata) { - Label *metadata_label = memnew(Label); + metadata_label = memnew(Label); String format; if (Object::cast_to<ImageTexture>(*p_texture)) { @@ -65,16 +84,14 @@ TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) { metadata_label->set_text(itos(p_texture->get_width()) + "x" + itos(p_texture->get_height()) + " " + format); - metadata_label->add_theme_font_size_override("font_size", 16 * EDSCALE); - metadata_label->add_theme_color_override("font_outline_color", Color::named("black")); - metadata_label->add_theme_constant_override("outline_size", 2 * EDSCALE); - Ref<Font> metadata_label_font = get_theme_font("expression", "EditorFonts"); - metadata_label->add_theme_font_override("font", metadata_label_font); - // It's okay that these colors are static since the grid color is static too. metadata_label->add_theme_color_override("font_color", Color::named("white")); metadata_label->add_theme_color_override("font_color_shadow", Color::named("black")); + metadata_label->add_theme_font_size_override("font_size", 16 * EDSCALE); + metadata_label->add_theme_color_override("font_outline_color", Color::named("black")); + metadata_label->add_theme_constant_override("outline_size", 2 * EDSCALE); + metadata_label->add_theme_constant_override("shadow_as_outline", 1); metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END); metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END); diff --git a/editor/plugins/texture_editor_plugin.h b/editor/plugins/texture_editor_plugin.h index 165090be93..c7272c83bc 100644 --- a/editor/plugins/texture_editor_plugin.h +++ b/editor/plugins/texture_editor_plugin.h @@ -41,6 +41,12 @@ class TexturePreview : public MarginContainer { private: TextureRect *texture_display; + TextureRect *checkerboard; + Label *metadata_label; + +protected: + void _notification(int p_what); + public: TextureRect *get_texture_display(); TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata); |