Skip to content

Commit

Permalink
Merge pull request #17417 from ShyRed/texturechangedupdate
Browse files Browse the repository at this point in the history
Update Sprite when its Texture changes
  • Loading branch information
reduz authored Mar 10, 2018
2 parents aeb1c67 + a23c087 commit eceba5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions scene/2d/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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();
Expand Down Expand Up @@ -362,6 +370,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);
Expand Down Expand Up @@ -436,3 +453,8 @@ Sprite::Sprite() {
vframes = 1;
hframes = 1;
}

Sprite::~Sprite() {
if (texture.is_valid())
texture->remove_change_receptor(this);
}
3 changes: 3 additions & 0 deletions scene/2d/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Sprite : public Node2D {

virtual void _validate_property(PropertyInfo &property) const;

virtual void _changed_callback(Object *p_changed, const char *p_prop);

public:
virtual Dictionary _edit_get_state() const;
virtual void _edit_set_state(const Dictionary &p_state);
Expand Down Expand Up @@ -113,6 +115,7 @@ class Sprite : public Node2D {
Rect2 get_rect() const;

Sprite();
~Sprite();
};

#endif // SPRITE_H

0 comments on commit eceba5a

Please sign in to comment.