summaryrefslogtreecommitdiff
path: root/scene/gui
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-01-12 20:06:26 +0100
committerRémi Verschelde <rverschelde@gmail.com>2023-01-12 20:06:26 +0100
commit797fb296f08b3d8f77f318ece2f203df225f5cdd (patch)
tree039f59154a29adf3d5f808df70d8e61e2f1a3d9a /scene/gui
parent73ab51465748a15ed4d80d66a44db83bfe574bb6 (diff)
parentdfc4367a478f1d6c8f13aeb9d8e0be01156f5afb (diff)
Merge pull request #58517 from KoBeWi/size_matters
Add expand modes to TextureRect
Diffstat (limited to 'scene/gui')
-rw-r--r--scene/gui/texture_rect.cpp54
-rw-r--r--scene/gui/texture_rect.h16
2 files changed, 55 insertions, 15 deletions
diff --git a/scene/gui/texture_rect.cpp b/scene/gui/texture_rect.cpp
index ac6d0cd453..5a3f4af106 100644
--- a/scene/gui/texture_rect.cpp
+++ b/scene/gui/texture_rect.cpp
@@ -110,22 +110,45 @@ void TextureRect::_notification(int p_what) {
draw_texture_rect(texture, Rect2(offset, size), tile);
}
} break;
+ case NOTIFICATION_RESIZED: {
+ update_minimum_size();
+ } break;
}
}
Size2 TextureRect::get_minimum_size() const {
- if (!ignore_texture_size && !texture.is_null()) {
- return texture->get_size();
- } else {
- return Size2();
+ if (!texture.is_null()) {
+ switch (expand_mode) {
+ case EXPAND_KEEP_SIZE: {
+ return texture->get_size();
+ } break;
+ case EXPAND_IGNORE_SIZE: {
+ return Size2();
+ } break;
+ case EXPAND_FIT_WIDTH: {
+ return Size2(get_size().y, 0);
+ } break;
+ case EXPAND_FIT_WIDTH_PROPORTIONAL: {
+ real_t ratio = real_t(texture->get_width()) / texture->get_height();
+ return Size2(get_size().y * ratio, 0);
+ } break;
+ case EXPAND_FIT_HEIGHT: {
+ return Size2(0, get_size().x);
+ } break;
+ case EXPAND_FIT_HEIGHT_PROPORTIONAL: {
+ real_t ratio = real_t(texture->get_height()) / texture->get_width();
+ return Size2(0, get_size().x * ratio);
+ } break;
+ }
}
+ return Size2();
}
void TextureRect::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
- ClassDB::bind_method(D_METHOD("set_ignore_texture_size", "ignore"), &TextureRect::set_ignore_texture_size);
- ClassDB::bind_method(D_METHOD("get_ignore_texture_size"), &TextureRect::get_ignore_texture_size);
+ ClassDB::bind_method(D_METHOD("set_expand_mode", "expand_mode"), &TextureRect::set_expand_mode);
+ ClassDB::bind_method(D_METHOD("get_expand_mode"), &TextureRect::get_expand_mode);
ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureRect::set_flip_h);
ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureRect::is_flipped_h);
ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureRect::set_flip_v);
@@ -134,11 +157,18 @@ void TextureRect::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_texture_size"), "set_ignore_texture_size", "get_ignore_texture_size");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "expand_mode", PROPERTY_HINT_ENUM, "Keep Size,Ignore Size,Fit Width,Fit Width Proportional,Fit Height,Fit Height Proportional"), "set_expand_mode", "get_expand_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
+ BIND_ENUM_CONSTANT(EXPAND_KEEP_SIZE);
+ BIND_ENUM_CONSTANT(EXPAND_IGNORE_SIZE);
+ BIND_ENUM_CONSTANT(EXPAND_FIT_WIDTH);
+ BIND_ENUM_CONSTANT(EXPAND_FIT_WIDTH_PROPORTIONAL);
+ BIND_ENUM_CONSTANT(EXPAND_FIT_HEIGHT);
+ BIND_ENUM_CONSTANT(EXPAND_FIT_HEIGHT_PROPORTIONAL);
+
BIND_ENUM_CONSTANT(STRETCH_SCALE);
BIND_ENUM_CONSTANT(STRETCH_TILE);
BIND_ENUM_CONSTANT(STRETCH_KEEP);
@@ -178,18 +208,18 @@ Ref<Texture2D> TextureRect::get_texture() const {
return texture;
}
-void TextureRect::set_ignore_texture_size(bool p_ignore) {
- if (ignore_texture_size == p_ignore) {
+void TextureRect::set_expand_mode(ExpandMode p_mode) {
+ if (expand_mode == p_mode) {
return;
}
- ignore_texture_size = p_ignore;
+ expand_mode = p_mode;
queue_redraw();
update_minimum_size();
}
-bool TextureRect::get_ignore_texture_size() const {
- return ignore_texture_size;
+TextureRect::ExpandMode TextureRect::get_expand_mode() const {
+ return expand_mode;
}
void TextureRect::set_stretch_mode(StretchMode p_mode) {
diff --git a/scene/gui/texture_rect.h b/scene/gui/texture_rect.h
index c56fee91e1..6f17ebd87f 100644
--- a/scene/gui/texture_rect.h
+++ b/scene/gui/texture_rect.h
@@ -37,6 +37,15 @@ class TextureRect : public Control {
GDCLASS(TextureRect, Control);
public:
+ enum ExpandMode {
+ EXPAND_KEEP_SIZE,
+ EXPAND_IGNORE_SIZE,
+ EXPAND_FIT_WIDTH,
+ EXPAND_FIT_WIDTH_PROPORTIONAL,
+ EXPAND_FIT_HEIGHT,
+ EXPAND_FIT_HEIGHT_PROPORTIONAL,
+ };
+
enum StretchMode {
STRETCH_SCALE,
STRETCH_TILE,
@@ -48,10 +57,10 @@ public:
};
private:
- bool ignore_texture_size = false;
bool hflip = false;
bool vflip = false;
Ref<Texture2D> texture;
+ ExpandMode expand_mode = EXPAND_KEEP_SIZE;
StretchMode stretch_mode = STRETCH_SCALE;
void _texture_changed();
@@ -65,8 +74,8 @@ public:
void set_texture(const Ref<Texture2D> &p_tex);
Ref<Texture2D> get_texture() const;
- void set_ignore_texture_size(bool p_ignore);
- bool get_ignore_texture_size() const;
+ void set_expand_mode(ExpandMode p_mode);
+ ExpandMode get_expand_mode() const;
void set_stretch_mode(StretchMode p_mode);
StretchMode get_stretch_mode() const;
@@ -81,6 +90,7 @@ public:
~TextureRect();
};
+VARIANT_ENUM_CAST(TextureRect::ExpandMode);
VARIANT_ENUM_CAST(TextureRect::StretchMode);
#endif // TEXTURE_RECT_H