Merge pull request #17417 from ShyRed/texturechangedupdate

Update Sprite when its Texture changes
This commit is contained in:
Juan Linietsky 2018-03-10 13:33:32 -03:00 committed by GitHub
commit eceba5aa6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -121,7 +121,15 @@ void Sprite::set_texture(const Ref<Texture> &p_texture) {
if (p_texture == texture) if (p_texture == texture)
return; return;
if (texture.is_valid())
texture->remove_change_receptor(this);
texture = p_texture; texture = p_texture;
if (texture.is_valid())
texture->add_change_receptor(this);
update(); update();
emit_signal("texture_changed"); emit_signal("texture_changed");
item_rect_changed(); item_rect_changed();
@ -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() { void Sprite::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite::set_texture); ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite::set_texture);
@ -436,3 +453,8 @@ Sprite::Sprite() {
vframes = 1; vframes = 1;
hframes = 1; hframes = 1;
} }
Sprite::~Sprite() {
if (texture.is_valid())
texture->remove_change_receptor(this);
}

View File

@ -64,6 +64,8 @@ protected:
virtual void _validate_property(PropertyInfo &property) const; virtual void _validate_property(PropertyInfo &property) const;
virtual void _changed_callback(Object *p_changed, const char *p_prop);
public: public:
virtual Dictionary _edit_get_state() const; virtual Dictionary _edit_get_state() const;
virtual void _edit_set_state(const Dictionary &p_state); virtual void _edit_set_state(const Dictionary &p_state);
@ -113,6 +115,7 @@ public:
Rect2 get_rect() const; Rect2 get_rect() const;
Sprite(); Sprite();
~Sprite();
}; };
#endif // SPRITE_H #endif // SPRITE_H