From 0c1c34ef22e50fd83747358de645ac021fba660d Mon Sep 17 00:00:00 2001 From: geequlim Date: Sat, 4 Feb 2017 14:26:50 +0800 Subject: [PATCH 1/3] Add Keep Covered texture resize mode --- scene/gui/texture_frame.cpp | 14 ++++++++++++-- scene/gui/texture_frame.h | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/scene/gui/texture_frame.cpp b/scene/gui/texture_frame.cpp index 571aafdbf7..bc30737cbf 100644 --- a/scene/gui/texture_frame.cpp +++ b/scene/gui/texture_frame.cpp @@ -79,7 +79,17 @@ void TextureFrame::_notification(int p_what) { draw_texture_rect(texture,Rect2(ofs_x,ofs_y,tex_width,tex_height)); } break; + case STRETCH_KEEP_ASPECT_COVERED: { + Size2 size = get_size(); + Size2 tex_size = texture->get_size(); + Size2 scaleSize(size.width/tex_size.width, size.height/tex_size.height); + float scale = scaleSize.width > scaleSize.height? scaleSize.width : scaleSize.height; + Size2 scaledTexSize = tex_size * scale; + Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f; + + draw_texture_rect_region(texture, Rect2(Point2(), size), Rect2(ofs, size/scale), modulate); + } break; } } @@ -107,7 +117,7 @@ void TextureFrame::_bind_methods() { ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"),_SCS("get_texture") ); ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate") ); ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "expand" ), _SCS("set_expand"),_SCS("has_expand") ); - ADD_PROPERTYNO( PropertyInfo( Variant::INT, "stretch_mode",PROPERTY_HINT_ENUM,"Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered"), _SCS("set_stretch_mode"),_SCS("get_stretch_mode") ); + ADD_PROPERTYNO( PropertyInfo( Variant::INT, "stretch_mode",PROPERTY_HINT_ENUM,"Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), _SCS("set_stretch_mode"),_SCS("get_stretch_mode") ); BIND_CONSTANT( STRETCH_SCALE_ON_EXPAND ); BIND_CONSTANT( STRETCH_SCALE ); @@ -116,7 +126,7 @@ void TextureFrame::_bind_methods() { BIND_CONSTANT( STRETCH_KEEP_CENTERED ); BIND_CONSTANT( STRETCH_KEEP_ASPECT ); BIND_CONSTANT( STRETCH_KEEP_ASPECT_CENTERED ); - + BIND_CONSTANT( STRETCH_KEEP_ASPECT_COVERED ); } diff --git a/scene/gui/texture_frame.h b/scene/gui/texture_frame.h index bfd0ff0c20..b3385612c5 100644 --- a/scene/gui/texture_frame.h +++ b/scene/gui/texture_frame.h @@ -45,7 +45,7 @@ public: STRETCH_KEEP_CENTERED, STRETCH_KEEP_ASPECT, STRETCH_KEEP_ASPECT_CENTERED, - + STRETCH_KEEP_ASPECT_COVERED, }; private: bool expand; From 58a700e43e69ee025e046caa9677fdf755df17b8 Mon Sep 17 00:00:00 2001 From: geequlim Date: Sat, 4 Feb 2017 12:01:12 +0800 Subject: [PATCH 2/3] Make same resize behavior for TextureButton with TextureFrame. Remove property 'scale' of TextureButton which is not required any more. --- scene/gui/texture_button.cpp | 144 ++++++++++++++++++++++++----------- scene/gui/texture_button.h | 14 ++-- 2 files changed, 108 insertions(+), 50 deletions(-) diff --git a/scene/gui/texture_button.cpp b/scene/gui/texture_button.cpp index 2c788ee34c..581875d403 100644 --- a/scene/gui/texture_button.cpp +++ b/scene/gui/texture_button.cpp @@ -28,40 +28,32 @@ /*************************************************************************/ #include "texture_button.h" - Size2 TextureButton::get_minimum_size() const { - Size2 rscale; - if (normal.is_null()) { - if (pressed.is_null()) { - if (hover.is_null()) - if (click_mask.is_null()) - rscale= Size2(); + Size2 size = Control::get_minimum_size(); + if(!expand) { + if (normal.is_null()) { + if (pressed.is_null()) { + if (hover.is_null()) + if (click_mask.is_null()) + size=Size2(); + else + size=click_mask->get_size(); else - rscale= click_mask->get_size(); - else - rscale= hover->get_size(); + size=hover->get_size(); + } else + size=pressed->get_size(); } else - rscale=pressed->get_size(); - - } else - rscale= normal->get_size(); - - return rscale*scale.abs(); + size=normal->get_size(); + } + return size; } - bool TextureButton::has_point(const Point2& p_point) const { - if (scale[0] == 0 || scale[1] == 0) { - return false; - } - - Point2 ppos = p_point/scale.abs(); - if (click_mask.is_valid()) { - Point2i p =ppos; + Point2i p = p_point; if (p.x<0 || p.x>=click_mask->get_size().width || p.y<0 || p.y>=click_mask->get_size().height) return false; @@ -71,6 +63,7 @@ bool TextureButton::has_point(const Point2& p_point) const { return Control::has_point(p_point); } + void TextureButton::_notification(int p_what) { switch( p_what ) { @@ -119,17 +112,67 @@ void TextureButton::_notification(int p_what) { } if (texdraw.is_valid()) { - Rect2 drect(Point2(),texdraw->get_size()*scale); - draw_texture_rect(texdraw,drect,false,modulate); + Point2 ofs; + Size2 size = texdraw->get_size(); + Rect2 tex_regin = Rect2(Point2(), texdraw->get_size()); + bool tile = false; + if(expand) { + switch (stretch_mode) { + case TextureFrame::STRETCH_KEEP: + size = texdraw->get_size(); + break; + case TextureFrame::STRETCH_SCALE_ON_EXPAND: + case TextureFrame::STRETCH_SCALE: + size = get_size(); + break; + case TextureFrame::STRETCH_TILE: + size = get_size(); + tile = true; + break; + case TextureFrame::STRETCH_KEEP_CENTERED: + ofs = (get_size() - texdraw->get_size())/2; + size = texdraw->get_size(); + break; + case TextureFrame::STRETCH_KEEP_ASPECT_CENTERED: + case TextureFrame::STRETCH_KEEP_ASPECT: { + Size2 _size=get_size(); + float tex_width = texdraw->get_width() * _size.height / texdraw->get_height(); + float tex_height = _size.height; + if (tex_width > _size.width) { + tex_width = _size.width; + tex_height = texdraw->get_height() * tex_width / texdraw->get_width(); + } + + if (stretch_mode==TextureFrame::STRETCH_KEEP_ASPECT_CENTERED) { + ofs.x = (_size.width - tex_width)/2; + ofs.y = (_size.height - tex_height)/2; + } + size.width = tex_width; + size.height = tex_height; + } break; + case TextureFrame::STRETCH_KEEP_ASPECT_COVERED:{ + size = get_size(); + Size2 tex_size = texdraw->get_size(); + Size2 scaleSize(size.width/tex_size.width, size.height/tex_size.height); + float scale = scaleSize.width > scaleSize.height? scaleSize.width : scaleSize.height; + Size2 scaledTexSize = tex_size * scale; + Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f; + tex_regin = Rect2(ofs, size/scale); + } break; + } + } + if (tile) + draw_texture_rect(texdraw,Rect2(ofs,size),tile,modulate); + else + draw_texture_rect_region(texdraw, Rect2(ofs, size), tex_regin, modulate); } if (has_focus() && focused.is_valid()) { - Rect2 drect(Point2(),focused->get_size()*scale); + Rect2 drect(Point2(), get_size()); draw_texture_rect(focused,drect,false,modulate); }; - } break; } } @@ -142,8 +185,9 @@ void TextureButton::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_disabled_texture","texture:Texture"),&TextureButton::set_disabled_texture); ObjectTypeDB::bind_method(_MD("set_focused_texture","texture:Texture"),&TextureButton::set_focused_texture); ObjectTypeDB::bind_method(_MD("set_click_mask","mask:BitMap"),&TextureButton::set_click_mask); - ObjectTypeDB::bind_method(_MD("set_texture_scale","scale"),&TextureButton::set_texture_scale); ObjectTypeDB::bind_method(_MD("set_modulate","color"),&TextureButton::set_modulate); + ObjectTypeDB::bind_method(_MD("set_expand","enable"), &TextureButton::set_expand); + ObjectTypeDB::bind_method(_MD("set_stretch_mode","stretch_mode"), & TextureButton::set_stretch_mode ); ObjectTypeDB::bind_method(_MD("get_normal_texture:Texture"),&TextureButton::get_normal_texture); ObjectTypeDB::bind_method(_MD("get_pressed_texture:Texture"),&TextureButton::get_pressed_texture); @@ -151,8 +195,10 @@ void TextureButton::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_disabled_texture:Texture"),&TextureButton::get_disabled_texture); ObjectTypeDB::bind_method(_MD("get_focused_texture:Texture"),&TextureButton::get_focused_texture); ObjectTypeDB::bind_method(_MD("get_click_mask:BitMap"),&TextureButton::get_click_mask); - ObjectTypeDB::bind_method(_MD("get_texture_scale"),&TextureButton::get_texture_scale); ObjectTypeDB::bind_method(_MD("get_modulate"),&TextureButton::get_modulate); + ObjectTypeDB::bind_method(_MD("has_expand"), & TextureButton::has_expand); + ObjectTypeDB::bind_method(_MD("get_stretch_mode"), &TextureButton::get_stretch_mode); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/normal",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_normal_texture"), _SCS("get_normal_texture")); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/pressed",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_pressed_texture"), _SCS("get_pressed_texture")); @@ -160,8 +206,9 @@ void TextureButton::_bind_methods() { ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/disabled",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_disabled_texture"), _SCS("get_disabled_texture")); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/focused",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_focused_texture"), _SCS("get_focused_texture")); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/click_mask",PROPERTY_HINT_RESOURCE_TYPE,"BitMap"), _SCS("set_click_mask"), _SCS("get_click_mask")) ; - ADD_PROPERTYNO(PropertyInfo(Variant::VECTOR2,"params/scale",PROPERTY_HINT_RANGE,"0.01,1024,0.01"), _SCS("set_texture_scale"), _SCS("get_texture_scale")); - ADD_PROPERTYNO(PropertyInfo(Variant::COLOR,"params/modulate"), _SCS("set_modulate"), _SCS("get_modulate")); + ADD_PROPERTYNO(PropertyInfo(Variant::COLOR,"modulate"), _SCS("set_modulate"), _SCS("get_modulate")); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand" ), _SCS("set_expand"),_SCS("has_expand")); + ADD_PROPERTYNO(PropertyInfo(Variant::INT, "stretch_mode",PROPERTY_HINT_ENUM,"Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), _SCS("set_stretch_mode"),_SCS("get_stretch_mode")); } @@ -229,18 +276,6 @@ void TextureButton::set_focused_texture(const Ref& p_focused) { focused = p_focused; }; -void TextureButton::set_texture_scale(Size2 p_scale) { - - scale=p_scale; - minimum_size_changed(); - update(); -} - -Size2 TextureButton::get_texture_scale() const{ - - return scale; -} - void TextureButton::set_modulate(const Color& p_modulate) { modulate=p_modulate; update(); @@ -250,8 +285,27 @@ Color TextureButton::get_modulate() const { return modulate; } +void TextureButton::set_expand(bool expand) { + this->expand = expand; + update(); +} + +bool TextureButton::has_expand() const { + return expand; +} + +void TextureButton::set_stretch_mode(TextureFrame::StretchMode stretch_mode) { + this->stretch_mode = stretch_mode; + update(); +} + +TextureFrame::StretchMode TextureButton::get_stretch_mode() const { + return stretch_mode; +} + TextureButton::TextureButton() { - scale=Size2(1.0, 1.0); modulate=Color(1,1,1); + expand=false; + stretch_mode=TextureFrame::STRETCH_KEEP; } diff --git a/scene/gui/texture_button.h b/scene/gui/texture_button.h index 047dbfadfa..1f971ce842 100644 --- a/scene/gui/texture_button.h +++ b/scene/gui/texture_button.h @@ -31,6 +31,7 @@ #include "scene/gui/base_button.h" #include "scene/resources/bit_mask.h" +#include "scene/gui/texture_frame.h" class TextureButton : public BaseButton { OBJ_TYPE( TextureButton, BaseButton ); @@ -41,9 +42,9 @@ class TextureButton : public BaseButton { Ref disabled; Ref focused; Ref click_mask; - Size2 scale; Color modulate; - + bool expand; + TextureFrame::StretchMode stretch_mode; protected: @@ -68,12 +69,15 @@ public: Ref get_focused_texture() const; Ref get_click_mask() const; - void set_texture_scale(Size2 p_scale); - Size2 get_texture_scale() const; - void set_modulate(const Color& p_modulate); Color get_modulate() const; + void set_expand(bool expand); + bool has_expand() const; + + void set_stretch_mode(TextureFrame::StretchMode stretch_mode); + TextureFrame::StretchMode get_stretch_mode() const; + TextureButton(); }; From a7ec7dcd1228137535601eefadd56a05c966ff5a Mon Sep 17 00:00:00 2001 From: geequlim Date: Tue, 7 Feb 2017 22:34:55 +0800 Subject: [PATCH 3/3] Add scale property back for backwards compatibility now we have a choice Fix textureframe modulate doesn't work with STRETCH_KEEP_ASPECT_CENTERED and STRETCH_KEEP_ASPECT --- scene/gui/texture_button.cpp | 91 ++++++++++++++++++++++++------------ scene/gui/texture_button.h | 37 ++++++++++++--- scene/gui/texture_frame.cpp | 4 +- 3 files changed, 91 insertions(+), 41 deletions(-) diff --git a/scene/gui/texture_button.cpp b/scene/gui/texture_button.cpp index 581875d403..18d3f9072e 100644 --- a/scene/gui/texture_button.cpp +++ b/scene/gui/texture_button.cpp @@ -31,7 +31,7 @@ Size2 TextureButton::get_minimum_size() const { Size2 size = Control::get_minimum_size(); - if(!expand) { + if(resize_mode == RESIZE_SCALE) { if (normal.is_null()) { if (pressed.is_null()) { if (hover.is_null()) @@ -45,15 +45,19 @@ Size2 TextureButton::get_minimum_size() const { size=pressed->get_size(); } else size=normal->get_size(); + size=size*scale.abs(); } return size; } bool TextureButton::has_point(const Point2& p_point) const { - + if ( resize_mode == RESIZE_SCALE && (scale[0] == 0 || scale[1] == 0)) { + return false; + } + Point2 ppos = (resize_mode == RESIZE_SCALE) ? (p_point/scale.abs()) : p_point; if (click_mask.is_valid()) { - Point2i p = p_point; + Point2i p = ppos; if (p.x<0 || p.x>=click_mask->get_size().width || p.y<0 || p.y>=click_mask->get_size().height) return false; @@ -116,25 +120,25 @@ void TextureButton::_notification(int p_what) { Size2 size = texdraw->get_size(); Rect2 tex_regin = Rect2(Point2(), texdraw->get_size()); bool tile = false; - if(expand) { + if(resize_mode == RESIZE_STRETCH) { switch (stretch_mode) { - case TextureFrame::STRETCH_KEEP: + case STRETCH_KEEP: size = texdraw->get_size(); break; - case TextureFrame::STRETCH_SCALE_ON_EXPAND: - case TextureFrame::STRETCH_SCALE: + case STRETCH_SCALE_ON_EXPAND: + case STRETCH_SCALE: size = get_size(); break; - case TextureFrame::STRETCH_TILE: + case STRETCH_TILE: size = get_size(); tile = true; break; - case TextureFrame::STRETCH_KEEP_CENTERED: + case STRETCH_KEEP_CENTERED: ofs = (get_size() - texdraw->get_size())/2; size = texdraw->get_size(); break; - case TextureFrame::STRETCH_KEEP_ASPECT_CENTERED: - case TextureFrame::STRETCH_KEEP_ASPECT: { + case STRETCH_KEEP_ASPECT_CENTERED: + case STRETCH_KEEP_ASPECT: { Size2 _size=get_size(); float tex_width = texdraw->get_width() * _size.height / texdraw->get_height(); float tex_height = _size.height; @@ -144,14 +148,14 @@ void TextureButton::_notification(int p_what) { tex_height = texdraw->get_height() * tex_width / texdraw->get_width(); } - if (stretch_mode==TextureFrame::STRETCH_KEEP_ASPECT_CENTERED) { + if (stretch_mode==STRETCH_KEEP_ASPECT_CENTERED) { ofs.x = (_size.width - tex_width)/2; ofs.y = (_size.height - tex_height)/2; } size.width = tex_width; size.height = tex_height; } break; - case TextureFrame::STRETCH_KEEP_ASPECT_COVERED:{ + case STRETCH_KEEP_ASPECT_COVERED:{ size = get_size(); Size2 tex_size = texdraw->get_size(); Size2 scaleSize(size.width/tex_size.width, size.height/tex_size.height); @@ -162,6 +166,9 @@ void TextureButton::_notification(int p_what) { } break; } } + else { + size = texdraw->get_size()*scale; + } if (tile) draw_texture_rect(texdraw,Rect2(ofs,size),tile,modulate); else @@ -185,9 +192,10 @@ void TextureButton::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_disabled_texture","texture:Texture"),&TextureButton::set_disabled_texture); ObjectTypeDB::bind_method(_MD("set_focused_texture","texture:Texture"),&TextureButton::set_focused_texture); ObjectTypeDB::bind_method(_MD("set_click_mask","mask:BitMap"),&TextureButton::set_click_mask); + ObjectTypeDB::bind_method(_MD("set_texture_scale","scale"),&TextureButton::set_texture_scale); ObjectTypeDB::bind_method(_MD("set_modulate","color"),&TextureButton::set_modulate); - ObjectTypeDB::bind_method(_MD("set_expand","enable"), &TextureButton::set_expand); - ObjectTypeDB::bind_method(_MD("set_stretch_mode","stretch_mode"), & TextureButton::set_stretch_mode ); + ObjectTypeDB::bind_method(_MD("set_resize_mode","p_mode"), &TextureButton::set_resize_mode); + ObjectTypeDB::bind_method(_MD("set_stretch_mode","p_mode"), & TextureButton::set_stretch_mode ); ObjectTypeDB::bind_method(_MD("get_normal_texture:Texture"),&TextureButton::get_normal_texture); ObjectTypeDB::bind_method(_MD("get_pressed_texture:Texture"),&TextureButton::get_pressed_texture); @@ -195,21 +203,30 @@ void TextureButton::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_disabled_texture:Texture"),&TextureButton::get_disabled_texture); ObjectTypeDB::bind_method(_MD("get_focused_texture:Texture"),&TextureButton::get_focused_texture); ObjectTypeDB::bind_method(_MD("get_click_mask:BitMap"),&TextureButton::get_click_mask); + ObjectTypeDB::bind_method(_MD("get_texture_scale"),&TextureButton::get_texture_scale); ObjectTypeDB::bind_method(_MD("get_modulate"),&TextureButton::get_modulate); - ObjectTypeDB::bind_method(_MD("has_expand"), & TextureButton::has_expand); + ObjectTypeDB::bind_method(_MD("get_resize_mode"), & TextureButton::get_resize_mode); ObjectTypeDB::bind_method(_MD("get_stretch_mode"), &TextureButton::get_stretch_mode); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/normal",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_normal_texture"), _SCS("get_normal_texture")); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/pressed",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_pressed_texture"), _SCS("get_pressed_texture")); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/hover",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_hover_texture"), _SCS("get_hover_texture")); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/disabled",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_disabled_texture"), _SCS("get_disabled_texture")); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/focused",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_focused_texture"), _SCS("get_focused_texture")); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/click_mask",PROPERTY_HINT_RESOURCE_TYPE,"BitMap"), _SCS("set_click_mask"), _SCS("get_click_mask")) ; - ADD_PROPERTYNO(PropertyInfo(Variant::COLOR,"modulate"), _SCS("set_modulate"), _SCS("get_modulate")); - ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand" ), _SCS("set_expand"),_SCS("has_expand")); - ADD_PROPERTYNO(PropertyInfo(Variant::INT, "stretch_mode",PROPERTY_HINT_ENUM,"Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), _SCS("set_stretch_mode"),_SCS("get_stretch_mode")); + ADD_PROPERTY(PropertyInfo(Variant::INT, "params/resize_mode",PROPERTY_HINT_ENUM,"Scale (Compat),Stretch"), _SCS("set_resize_mode"), _SCS("get_resize_mode")); + ADD_PROPERTYNO(PropertyInfo(Variant::VECTOR2,"params/scale",PROPERTY_HINT_RANGE,"0.01,1024,0.01"), _SCS("set_texture_scale"), _SCS("get_texture_scale")); + ADD_PROPERTY(PropertyInfo(Variant::INT, "params/stretch_mode",PROPERTY_HINT_ENUM,"Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), _SCS("set_stretch_mode"),_SCS("get_stretch_mode")); + ADD_PROPERTYNO(PropertyInfo(Variant::COLOR,"params/modulate"), _SCS("set_modulate"), _SCS("get_modulate")); + BIND_CONSTANT(STRETCH_SCALE_ON_EXPAND); + BIND_CONSTANT(STRETCH_SCALE); + BIND_CONSTANT(STRETCH_TILE); + BIND_CONSTANT(STRETCH_KEEP); + BIND_CONSTANT(STRETCH_KEEP_CENTERED); + BIND_CONSTANT(STRETCH_KEEP_ASPECT); + BIND_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED); + BIND_CONSTANT(STRETCH_KEEP_ASPECT_COVERED); } @@ -285,27 +302,39 @@ Color TextureButton::get_modulate() const { return modulate; } -void TextureButton::set_expand(bool expand) { - this->expand = expand; +TextureButton::ResizeMode TextureButton::get_resize_mode() const { + return resize_mode; +} + +void TextureButton::set_resize_mode(TextureButton::ResizeMode p_mode) { + resize_mode = p_mode; + minimum_size_changed(); update(); } -bool TextureButton::has_expand() const { - return expand; -} - -void TextureButton::set_stretch_mode(TextureFrame::StretchMode stretch_mode) { - this->stretch_mode = stretch_mode; +void TextureButton::set_texture_scale(Size2 p_scale) { + scale = p_scale; + minimum_size_changed(); update(); } -TextureFrame::StretchMode TextureButton::get_stretch_mode() const { +Size2 TextureButton::get_texture_scale() const { + return scale; +} + +void TextureButton::set_stretch_mode(TextureButton::StretchMode p_mode) { + stretch_mode = p_mode; + update(); +} + +TextureButton::StretchMode TextureButton::get_stretch_mode() const { return stretch_mode; } TextureButton::TextureButton() { modulate=Color(1,1,1); - expand=false; - stretch_mode=TextureFrame::STRETCH_KEEP; + resize_mode=RESIZE_SCALE; + scale=Size2(1.0, 1.0); + stretch_mode=STRETCH_SCALE_ON_EXPAND; } diff --git a/scene/gui/texture_button.h b/scene/gui/texture_button.h index 1f971ce842..84651880a7 100644 --- a/scene/gui/texture_button.h +++ b/scene/gui/texture_button.h @@ -31,11 +31,28 @@ #include "scene/gui/base_button.h" #include "scene/resources/bit_mask.h" -#include "scene/gui/texture_frame.h" + class TextureButton : public BaseButton { OBJ_TYPE( TextureButton, BaseButton ); +public: + enum ResizeMode { + RESIZE_SCALE, // for backwards compatibility + RESIZE_STRETCH, + }; + enum StretchMode { + STRETCH_SCALE_ON_EXPAND, //default, for backwards compatibility + STRETCH_SCALE, + STRETCH_TILE, + STRETCH_KEEP, + STRETCH_KEEP_CENTERED, + STRETCH_KEEP_ASPECT, + STRETCH_KEEP_ASPECT_CENTERED, + STRETCH_KEEP_ASPECT_COVERED, + }; + +private: Ref normal; Ref pressed; Ref hover; @@ -43,8 +60,9 @@ class TextureButton : public BaseButton { Ref focused; Ref click_mask; Color modulate; - bool expand; - TextureFrame::StretchMode stretch_mode; + ResizeMode resize_mode; + Size2 scale; + StretchMode stretch_mode; protected: @@ -72,13 +90,18 @@ public: void set_modulate(const Color& p_modulate); Color get_modulate() const; - void set_expand(bool expand); - bool has_expand() const; + ResizeMode get_resize_mode() const; + void set_resize_mode(ResizeMode p_mode); - void set_stretch_mode(TextureFrame::StretchMode stretch_mode); - TextureFrame::StretchMode get_stretch_mode() const; + void set_texture_scale(Size2 p_scale); + Size2 get_texture_scale() const; + + void set_stretch_mode(StretchMode stretch_mode); + StretchMode get_stretch_mode() const; TextureButton(); }; +VARIANT_ENUM_CAST( TextureButton::ResizeMode ); +VARIANT_ENUM_CAST( TextureButton::StretchMode ); #endif // TEXTURE_BUTTON_H diff --git a/scene/gui/texture_frame.cpp b/scene/gui/texture_frame.cpp index bc30737cbf..278fff9964 100644 --- a/scene/gui/texture_frame.cpp +++ b/scene/gui/texture_frame.cpp @@ -77,7 +77,7 @@ void TextureFrame::_notification(int p_what) { ofs_y+=(size.height - tex_height)/2; } - draw_texture_rect(texture,Rect2(ofs_x,ofs_y,tex_width,tex_height)); + draw_texture_rect(texture,Rect2(ofs_x,ofs_y,tex_width,tex_height),false,modulate); } break; case STRETCH_KEEP_ASPECT_COVERED: { @@ -191,5 +191,3 @@ TextureFrame::TextureFrame() { TextureFrame::~TextureFrame() { } - -