Merge pull request #32351 from nekomatata/texture-rect-size-update

Update TextureRect when its Texture is modified directly
This commit is contained in:
Rémi Verschelde 2019-10-07 08:57:17 +02:00 committed by GitHub
commit f84bf7e8a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 11 deletions

View File

@ -135,12 +135,12 @@ void Sprite::set_texture(const Ref<Texture> &p_texture) {
return; return;
if (texture.is_valid()) if (texture.is_valid())
texture->remove_change_receptor(this); texture->disconnect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
texture = p_texture; texture = p_texture;
if (texture.is_valid()) if (texture.is_valid())
texture->add_change_receptor(this); texture->connect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
update(); update();
emit_signal("texture_changed"); emit_signal("texture_changed");
@ -389,11 +389,11 @@ void Sprite::_validate_property(PropertyInfo &property) const {
} }
} }
void Sprite::_changed_callback(Object *p_changed, const char *p_prop) { void Sprite::_texture_changed() {
// Changes to the texture need to trigger an update to make // Changes to the texture need to trigger an update to make
// the editor redraw the sprite with the updated texture. // the editor redraw the sprite with the updated texture.
if (texture.is_valid() && texture.ptr() == p_changed) { if (texture.is_valid()) {
update(); update();
} }
} }
@ -443,6 +443,8 @@ void Sprite::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_rect"), &Sprite::get_rect); ClassDB::bind_method(D_METHOD("get_rect"), &Sprite::get_rect);
ClassDB::bind_method(D_METHOD("_texture_changed"), &Sprite::_texture_changed);
ADD_SIGNAL(MethodInfo("frame_changed")); ADD_SIGNAL(MethodInfo("frame_changed"));
ADD_SIGNAL(MethodInfo("texture_changed")); ADD_SIGNAL(MethodInfo("texture_changed"));
@ -480,6 +482,4 @@ Sprite::Sprite() {
} }
Sprite::~Sprite() { Sprite::~Sprite() {
if (texture.is_valid())
texture->remove_change_receptor(this);
} }

View File

@ -57,6 +57,8 @@ class Sprite : public Node2D {
void _get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_clip) const; void _get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_clip) const;
void _texture_changed();
protected: protected:
void _notification(int p_what); void _notification(int p_what);
@ -64,8 +66,6 @@ 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);

View File

@ -29,6 +29,7 @@
/*************************************************************************/ /*************************************************************************/
#include "texture_rect.h" #include "texture_rect.h"
#include "core/core_string_names.h"
#include "servers/visual_server.h" #include "servers/visual_server.h"
void TextureRect::_notification(int p_what) { void TextureRect::_notification(int p_what) {
@ -123,6 +124,7 @@ void TextureRect::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v); ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v);
ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode); ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode); ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
ClassDB::bind_method(D_METHOD("_texture_changed"), &TextureRect::_texture_changed);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
@ -140,9 +142,27 @@ void TextureRect::_bind_methods() {
BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED); BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
} }
void TextureRect::_texture_changed() {
if (texture.is_valid()) {
update();
minimum_size_changed();
}
}
void TextureRect::set_texture(const Ref<Texture> &p_tex) { void TextureRect::set_texture(const Ref<Texture> &p_tex) {
if (p_tex == texture)
return;
if (texture.is_valid())
texture->disconnect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
texture = p_tex; texture = p_tex;
if (texture.is_valid())
texture->connect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
update(); update();
/* /*
if (texture.is_valid()) if (texture.is_valid())

View File

@ -56,6 +56,8 @@ private:
Ref<Texture> texture; Ref<Texture> texture;
StretchMode stretch_mode; StretchMode stretch_mode;
void _texture_changed();
protected: protected:
void _notification(int p_what); void _notification(int p_what);
virtual Size2 get_minimum_size() const; virtual Size2 get_minimum_size() const;

View File

@ -117,6 +117,7 @@ void ImageTexture::reload_from_file() {
} else { } else {
Resource::reload_from_file(); Resource::reload_from_file();
_change_notify(); _change_notify();
emit_changed();
} }
} }
@ -180,6 +181,7 @@ void ImageTexture::_reload_hook(const RID &p_hook) {
VisualServer::get_singleton()->texture_set_data(texture, img); VisualServer::get_singleton()->texture_set_data(texture, img);
_change_notify(); _change_notify();
emit_changed();
} }
void ImageTexture::create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags) { void ImageTexture::create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags) {
@ -190,6 +192,7 @@ void ImageTexture::create(int p_width, int p_height, Image::Format p_format, uin
w = p_width; w = p_width;
h = p_height; h = p_height;
_change_notify(); _change_notify();
emit_changed();
} }
void ImageTexture::create_from_image(const Ref<Image> &p_image, uint32_t p_flags) { void ImageTexture::create_from_image(const Ref<Image> &p_image, uint32_t p_flags) {
@ -202,23 +205,23 @@ void ImageTexture::create_from_image(const Ref<Image> &p_image, uint32_t p_flags
VisualServer::get_singleton()->texture_allocate(texture, p_image->get_width(), p_image->get_height(), 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, p_flags); VisualServer::get_singleton()->texture_allocate(texture, p_image->get_width(), p_image->get_height(), 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, p_flags);
VisualServer::get_singleton()->texture_set_data(texture, p_image); VisualServer::get_singleton()->texture_set_data(texture, p_image);
_change_notify(); _change_notify();
emit_changed();
image_stored = true; image_stored = true;
} }
void ImageTexture::set_flags(uint32_t p_flags) { void ImageTexture::set_flags(uint32_t p_flags) {
/* uint32_t cube = flags & FLAG_CUBEMAP; if (flags == p_flags)
if (flags == p_flags&cube)
return; return;
flags=p_flags|cube; */
flags = p_flags; flags = p_flags;
if (w == 0 || h == 0) { if (w == 0 || h == 0) {
return; //uninitialized, do not set to texture return; //uninitialized, do not set to texture
} }
VisualServer::get_singleton()->texture_set_flags(texture, p_flags); VisualServer::get_singleton()->texture_set_flags(texture, p_flags);
_change_notify("flags"); _change_notify("flags");
emit_changed();
} }
uint32_t ImageTexture::get_flags() const { uint32_t ImageTexture::get_flags() const {
@ -250,6 +253,8 @@ void ImageTexture::set_data(const Ref<Image> &p_image) {
VisualServer::get_singleton()->texture_set_data(texture, p_image); VisualServer::get_singleton()->texture_set_data(texture, p_image);
_change_notify(); _change_notify();
emit_changed();
alpha_cache.unref(); alpha_cache.unref();
image_stored = true; image_stored = true;
} }
@ -736,6 +741,7 @@ Error StreamTexture::load(const String &p_path) {
format = image->get_format(); format = image->get_format();
_change_notify(); _change_notify();
emit_changed();
return OK; return OK;
} }
String StreamTexture::get_load_path() const { String StreamTexture::get_load_path() const {
@ -827,6 +833,7 @@ void StreamTexture::set_flags(uint32_t p_flags) {
flags = p_flags; flags = p_flags;
VS::get_singleton()->texture_set_flags(texture, flags); VS::get_singleton()->texture_set_flags(texture, flags);
_change_notify("flags"); _change_notify("flags");
emit_changed();
} }
void StreamTexture::reload_from_file() { void StreamTexture::reload_from_file() {