diff options
| author | ShyRed | 2018-03-10 17:01:46 +0100 |
|---|---|---|
| committer | Hein-Pieter van Braam | 2018-04-28 16:13:28 +0200 |
| commit | c39e32ae60bd39a88b4a15b57c2b87845fc5d69d (patch) | |
| tree | d04508e16f6ad1ccd4d93ebaccf63c4e9201307d | |
| parent | 5eef26b64e4316197bf6e09e83e15fb698ee835f (diff) | |
| download | godot-c39e32ae60bd39a88b4a15b57c2b87845fc5d69d.tar.gz godot-c39e32ae60bd39a88b4a15b57c2b87845fc5d69d.tar.zst godot-c39e32ae60bd39a88b4a15b57c2b87845fc5d69d.zip | |
Update Sprite when Texture changes
Make Sprite monitor its Texture for changes and trigger an update when the sprite changes.
(cherry picked from commit a23c0877f1bb26edb75aa344f1049a7b7a91f079)
| -rw-r--r-- | scene/2d/sprite.cpp | 22 | ||||
| -rw-r--r-- | scene/2d/sprite.h | 3 |
2 files changed, 25 insertions, 0 deletions
diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp index 1fd78544f..8cde509c9 100644 --- a/scene/2d/sprite.cpp +++ b/scene/2d/sprite.cpp @@ -111,7 +111,15 @@ void Sprite::set_texture(const Ref<Texture> &p_texture) { if (p_texture == texture) return; + + if (texture.is_valid()) + texture->remove_change_receptor(this); + texture = p_texture; + + if (texture.is_valid()) + texture->add_change_receptor(this); + update(); emit_signal("texture_changed"); item_rect_changed(); @@ -346,6 +354,15 @@ void Sprite::_validate_property(PropertyInfo &property) const { } } +void Sprite::_changed_callback(Object *p_changed, const char *p_prop) { + + // Changes to the texture need to trigger an update to make + // the editor redraw the sprite with the updated texture. + if (texture.is_valid() && texture.ptr() == p_changed) { + update(); + } +} + void Sprite::_bind_methods() { ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite::set_texture); @@ -418,3 +435,8 @@ Sprite::Sprite() { vframes = 1; hframes = 1; } + +Sprite::~Sprite() { + if (texture.is_valid()) + texture->remove_change_receptor(this); +} diff --git a/scene/2d/sprite.h b/scene/2d/sprite.h index 261165bbf..a92b74dd4 100644 --- a/scene/2d/sprite.h +++ b/scene/2d/sprite.h @@ -64,6 +64,8 @@ protected: virtual void _validate_property(PropertyInfo &property) const; + virtual void _changed_callback(Object *p_changed, const char *p_prop); + public: virtual void _edit_set_pivot(const Point2 &p_pivot); virtual Point2 _edit_get_pivot() const; @@ -108,6 +110,7 @@ public: int get_hframes() const; Sprite(); + ~Sprite(); }; #endif // SPRITE_H |
