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

[3.x] Make TextureButton and Button update on texture change #81113

Merged
merged 1 commit into from
Sep 7, 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
14 changes: 14 additions & 0 deletions scene/gui/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "button.h"

#include "core/translation.h"
#include "scene/scene_string_names.h"
#include "servers/visual_server.h"

Size2 Button::get_minimum_size() const {
Expand Down Expand Up @@ -309,7 +310,13 @@ void Button::set_icon(const Ref<Texture> &p_icon) {
if (icon == p_icon) {
return;
}
if (icon.is_valid()) {
icon->disconnect(SceneStringNames::get_singleton()->changed, this, "_texture_changed");
}
icon = p_icon;
if (icon.is_valid()) {
icon->connect(SceneStringNames::get_singleton()->changed, this, "_texture_changed");
}
update();
_change_notify("icon");
minimum_size_changed();
Expand All @@ -319,6 +326,11 @@ Ref<Texture> Button::get_icon() const {
return icon;
}

void Button::_texture_changed() {
update();
minimum_size_changed();
}

void Button::set_expand_icon(bool p_expand_icon) {
expand_icon = p_expand_icon;
update();
Expand Down Expand Up @@ -384,6 +396,8 @@ void Button::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon);
ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon);

ClassDB::bind_method(D_METHOD("_texture_changed"), &Button::_texture_changed);

BIND_ENUM_CONSTANT(ALIGN_LEFT);
BIND_ENUM_CONSTANT(ALIGN_CENTER);
BIND_ENUM_CONSTANT(ALIGN_RIGHT);
Expand Down
2 changes: 2 additions & 0 deletions scene/gui/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Button : public BaseButton {
TextAlign icon_align;
float _internal_margin[4];

void _texture_changed();

protected:
void _set_internal_margin(Margin p_margin, float p_value);
void _notification(int p_what);
Expand Down
44 changes: 33 additions & 11 deletions scene/gui/texture_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
/**************************************************************************/

#include "texture_button.h"

#include "core/typedefs.h"
#include "scene/scene_string_names.h"

#include <stdlib.h>

Size2 TextureButton::get_minimum_size() const {
Expand Down Expand Up @@ -272,6 +275,8 @@ void TextureButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_expand"), &TextureButton::get_expand);
ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode);

ClassDB::bind_method(D_METHOD("_texture_changed"), &TextureButton::_texture_changed);

ADD_GROUP("Textures", "texture_");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_texture", "get_normal_texture");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_pressed_texture", "get_pressed_texture");
Expand All @@ -294,25 +299,21 @@ void TextureButton::_bind_methods() {
}

void TextureButton::set_normal_texture(const Ref<Texture> &p_normal) {
normal = p_normal;
update();
minimum_size_changed();
_set_texture(&normal, p_normal);
}

void TextureButton::set_pressed_texture(const Ref<Texture> &p_pressed) {
pressed = p_pressed;
update();
minimum_size_changed();
_set_texture(&pressed, p_pressed);
}

void TextureButton::set_hover_texture(const Ref<Texture> &p_hover) {
hover = p_hover;
update();
minimum_size_changed();
_set_texture(&hover, p_hover);
}

void TextureButton::set_disabled_texture(const Ref<Texture> &p_disabled) {
disabled = p_disabled;
update();
_set_texture(&disabled, p_disabled);
}

void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
click_mask = p_click_mask;
update();
Expand Down Expand Up @@ -343,6 +344,27 @@ void TextureButton::set_focused_texture(const Ref<Texture> &p_focused) {
focused = p_focused;
};

void TextureButton::_set_texture(Ref<Texture> *p_destination, const Ref<Texture> &p_texture) {
DEV_ASSERT(p_destination);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEV_ASSERT should only be used for bottleneck code. A regular check here won't matter.

But I think a better way is to pass by non-const reference, so we don't have to do any check.

Copy link
Member Author

@AThousandShips AThousandShips Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: #77159 (comment)

Also DEV_ASSERT is for crashing, not performance critical AFAIK

Keeping the code as proven in 4.x for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work, assigning to the reference won't update the texture itself

The referenced Ref object won't change. What will be updated is the content of that Ref, which is the texture.

Also DEV_ASSERT is for crashing, not performance critical AFAIK

A note was added recently:

* DEV_ASSERT should generally only be used when both of the following conditions are met:
* 1) Bottleneck code where a check in release would be too expensive.
* 2) Situations where the check would fail obviously and straight away during the maintenance of the code
* (i.e. strict conditions that should be true no matter what)
* and that can't fail for other contributors once the code is finished and merged.

But anyway, I think it's fine if the Godot 4 version uses this. I should have read the original PR, sorry :(

Copy link
Member Author

@AThousandShips AThousandShips Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, all the changes except to fit it to 3.x code are from the 4.x one, now minus the is_valid part

Checked in 4.x and passing by reference works there, but don't want any potential unseen side effects for a cheery pick, thank you for feedback

Ref<Texture> &destination = *p_destination;
if (destination == p_texture) {
return;
}
if (destination.is_valid()) {
destination->disconnect(SceneStringNames::get_singleton()->changed, this, "_texture_changed");
}
destination = p_texture;
if (destination.is_valid()) {
destination->connect(SceneStringNames::get_singleton()->changed, this, "_texture_changed", varray(), CONNECT_REFERENCE_COUNTED);
}
_texture_changed();
}

void TextureButton::_texture_changed() {
update();
minimum_size_changed();
}

bool TextureButton::get_expand() const {
return expand;
}
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;
bool vflip;

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

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