2
0
Эх сурвалжийг харах

Make `TextureButton` and `Button` update on texture change

(cherry picked from commit d0a98e1ddcb68a9b0057d863ec09317936c92b2e)
A Thousand Ships 2 жил өмнө
parent
commit
9f603c85e1

+ 14 - 0
scene/gui/button.cpp

@@ -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 {
@@ -301,7 +302,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();
@@ -311,6 +318,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();
@@ -376,6 +388,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);

+ 2 - 0
scene/gui/button.h

@@ -54,6 +54,8 @@ private:
 	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);

+ 33 - 11
scene/gui/texture_button.cpp

@@ -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 {
@@ -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");
@@ -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();
@@ -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);
+	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;
 }

+ 3 - 0
scene/gui/texture_button.h

@@ -64,6 +64,9 @@ private:
 	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;