Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TextureButton and Button update on texture change #77159

Merged
merged 1 commit into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions scene/gui/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "button.h"

#include "core/core_string_names.h"
#include "core/string/translation.h"
#include "servers/rendering_server.h"

Expand Down Expand Up @@ -533,8 +534,26 @@ String Button::get_language() const {
}

void Button::set_icon(const Ref<Texture2D> &p_icon) {
if (icon != p_icon) {
icon = p_icon;
if (icon == p_icon) {
return;
}

if (icon.is_valid()) {
icon->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Button::_texture_changed));
}

icon = p_icon;

if (icon.is_valid()) {
icon->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Button::_texture_changed));
}

queue_redraw();
update_minimum_size();
}

void Button::_texture_changed() {
if (icon.is_valid()) {
queue_redraw();
update_minimum_size();
}
Expand Down
1 change: 1 addition & 0 deletions scene/gui/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Button : public BaseButton {
Size2 _fit_icon_size(const Size2 &p_size) const;

void _shape(Ref<TextParagraph> p_paragraph = Ref<TextParagraph>(), String p_text = "");
void _texture_changed();

protected:
void _set_internal_margin(Side p_side, float p_value);
Expand Down
57 changes: 28 additions & 29 deletions scene/gui/texture_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "texture_button.h"

#include "core/core_string_names.h"
#include "core/typedefs.h"

#include <stdlib.h>
Expand Down Expand Up @@ -294,51 +295,27 @@ void TextureButton::_bind_methods() {
}

void TextureButton::set_texture_normal(const Ref<Texture2D> &p_normal) {
if (normal == p_normal) {
return;
}

normal = p_normal;
queue_redraw();
update_minimum_size();
_set_texture(&normal, p_normal);
}

void TextureButton::set_texture_pressed(const Ref<Texture2D> &p_pressed) {
if (pressed == p_pressed) {
return;
}

pressed = p_pressed;
queue_redraw();
update_minimum_size();
_set_texture(&pressed, p_pressed);
}

void TextureButton::set_texture_hover(const Ref<Texture2D> &p_hover) {
if (hover == p_hover) {
return;
}

hover = p_hover;
queue_redraw();
update_minimum_size();
_set_texture(&hover, p_hover);
}

void TextureButton::set_texture_disabled(const Ref<Texture2D> &p_disabled) {
if (disabled == p_disabled) {
return;
}

disabled = p_disabled;
queue_redraw();
_set_texture(&disabled, p_disabled);
}

void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
if (click_mask == p_click_mask) {
return;
}
click_mask = p_click_mask;
queue_redraw();
update_minimum_size();
_texture_changed();
}

Ref<Texture2D> TextureButton::get_texture_normal() const {
Expand Down Expand Up @@ -369,6 +346,28 @@ void TextureButton::set_texture_focused(const Ref<Texture2D> &p_focused) {
focused = p_focused;
};

void TextureButton::_set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture) {
DEV_ASSERT(p_destination);
AThousandShips marked this conversation as resolved.
Show resolved Hide resolved
Ref<Texture2D> &destination = *p_destination;
if (destination == p_texture) {
return;
}
if (destination.is_valid()) {
destination->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TextureButton::_texture_changed));
}
destination = p_texture;
if (destination.is_valid()) {
// Pass `CONNECT_REFERENCE_COUNTED` to avoid early disconnect in case the same texture is assigned to different "slots".
destination->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TextureButton::_texture_changed), CONNECT_REFERENCE_COUNTED);
}
_texture_changed();
}

void TextureButton::_texture_changed() {
queue_redraw();
update_minimum_size();
}

bool TextureButton::get_ignore_texture_size() const {
return ignore_texture_size;
}
Expand Down
3 changes: 3 additions & 0 deletions scene/gui/texture_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class TextureButton : public BaseButton {
bool hflip = false;
bool vflip = false;

void _set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture);
void _texture_changed();

protected:
virtual Size2 get_minimum_size() const override;
virtual bool has_point(const Point2 &p_point) const override;
Expand Down