diff options
Diffstat (limited to 'scene/gui/texture_button.cpp')
-rw-r--r-- | scene/gui/texture_button.cpp | 135 |
1 files changed, 82 insertions, 53 deletions
diff --git a/scene/gui/texture_button.cpp b/scene/gui/texture_button.cpp index 9ffb69037a..4187d77083 100644 --- a/scene/gui/texture_button.cpp +++ b/scene/gui/texture_button.cpp @@ -33,33 +33,34 @@ #include <stdlib.h> Size2 TextureButton::get_minimum_size() const { - Size2 rscale = Control::get_minimum_size(); if (!expand) { if (normal.is_null()) { if (pressed.is_null()) { - if (hover.is_null()) - if (click_mask.is_null()) + if (hover.is_null()) { + if (click_mask.is_null()) { rscale = Size2(); - else + } else { rscale = click_mask->get_size(); - else + } + } else { rscale = hover->get_size(); - } else + } + } else { rscale = pressed->get_size(); + } - } else + } else { rscale = normal->get_size(); + } } return rscale.abs(); } bool TextureButton::has_point(const Point2 &p_point) const { - if (click_mask.is_valid()) { - Point2 point = p_point; Rect2 rect = Rect2(); Size2 mask_size = click_mask->get_size(); @@ -116,56 +117,60 @@ bool TextureButton::has_point(const Point2 &p_point) const { } void TextureButton::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_DRAW: { DrawMode draw_mode = get_draw_mode(); - Ref<Texture> texdraw; + Ref<Texture2D> texdraw; switch (draw_mode) { case DRAW_NORMAL: { - - if (normal.is_valid()) + if (normal.is_valid()) { texdraw = normal; + } } break; case DRAW_HOVER_PRESSED: case DRAW_PRESSED: { - if (pressed.is_null()) { if (hover.is_null()) { - if (normal.is_valid()) + if (normal.is_valid()) { texdraw = normal; - } else + } + } else { texdraw = hover; + } - } else + } else { texdraw = pressed; + } } break; case DRAW_HOVER: { - if (hover.is_null()) { - if (pressed.is_valid() && is_pressed()) + if (pressed.is_valid() && is_pressed()) { texdraw = pressed; - else if (normal.is_valid()) + } else if (normal.is_valid()) { texdraw = normal; - } else + } + } else { texdraw = hover; + } } break; case DRAW_DISABLED: { - if (disabled.is_null()) { - if (normal.is_valid()) + if (normal.is_valid()) { texdraw = normal; - } else + } + } else { texdraw = disabled; + } } break; } + Point2 ofs; + Size2 size; + if (texdraw.is_valid()) { - Point2 ofs; - Size2 size = texdraw->get_size(); + size = texdraw->get_size(); _texture_region = Rect2(Point2(), texdraw->get_size()); _tile = false; if (expand) { @@ -215,24 +220,27 @@ void TextureButton::_notification(int p_what) { } _position_rect = Rect2(ofs, size); + + size.width *= hflip ? -1.0f : 1.0f; + size.height *= vflip ? -1.0f : 1.0f; + if (_tile) { - draw_texture_rect(texdraw, _position_rect, _tile); + draw_texture_rect(texdraw, Rect2(ofs, size), _tile); } else { - draw_texture_rect_region(texdraw, _position_rect, _texture_region); + draw_texture_rect_region(texdraw, Rect2(ofs, size), _texture_region); } } else { _position_rect = Rect2(); } if (has_focus() && focused.is_valid()) { - draw_texture_rect(focused, _position_rect, false); + draw_texture_rect(focused, Rect2(ofs, size), false); }; } break; } } void TextureButton::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_normal_texture", "texture"), &TextureButton::set_normal_texture); ClassDB::bind_method(D_METHOD("set_pressed_texture", "texture"), &TextureButton::set_pressed_texture); ClassDB::bind_method(D_METHOD("set_hover_texture", "texture"), &TextureButton::set_hover_texture); @@ -241,6 +249,10 @@ void TextureButton::_bind_methods() { ClassDB::bind_method(D_METHOD("set_click_mask", "mask"), &TextureButton::set_click_mask); ClassDB::bind_method(D_METHOD("set_expand", "p_expand"), &TextureButton::set_expand); ClassDB::bind_method(D_METHOD("set_stretch_mode", "p_mode"), &TextureButton::set_stretch_mode); + ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureButton::set_flip_h); + ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureButton::is_flipped_h); + ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureButton::set_flip_v); + ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureButton::is_flipped_v); ClassDB::bind_method(D_METHOD("get_normal_texture"), &TextureButton::get_normal_texture); ClassDB::bind_method(D_METHOD("get_pressed_texture"), &TextureButton::get_pressed_texture); @@ -252,14 +264,16 @@ void TextureButton::_bind_methods() { ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode); 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"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_hover_texture", "get_hover_texture"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_disabled_texture", "get_disabled_texture"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_focused_texture", "get_focused_texture"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_normal_texture", "get_normal_texture"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_pressed_texture", "get_pressed_texture"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_hover_texture", "get_hover_texture"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_disabled_texture", "get_disabled_texture"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_focused_texture", "get_focused_texture"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_expand", "get_expand"); 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", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_h", "is_flipped_h"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_v", "is_flipped_v"); BIND_ENUM_CONSTANT(STRETCH_SCALE); BIND_ENUM_CONSTANT(STRETCH_TILE); @@ -270,62 +284,57 @@ void TextureButton::_bind_methods() { BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED); } -void TextureButton::set_normal_texture(const Ref<Texture> &p_normal) { - +void TextureButton::set_normal_texture(const Ref<Texture2D> &p_normal) { normal = p_normal; update(); minimum_size_changed(); } -void TextureButton::set_pressed_texture(const Ref<Texture> &p_pressed) { - +void TextureButton::set_pressed_texture(const Ref<Texture2D> &p_pressed) { pressed = p_pressed; update(); } -void TextureButton::set_hover_texture(const Ref<Texture> &p_hover) { +void TextureButton::set_hover_texture(const Ref<Texture2D> &p_hover) { hover = p_hover; update(); } -void TextureButton::set_disabled_texture(const Ref<Texture> &p_disabled) { +void TextureButton::set_disabled_texture(const Ref<Texture2D> &p_disabled) { disabled = p_disabled; update(); } -void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) { +void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) { click_mask = p_click_mask; update(); } -Ref<Texture> TextureButton::get_normal_texture() const { - +Ref<Texture2D> TextureButton::get_normal_texture() const { return normal; } -Ref<Texture> TextureButton::get_pressed_texture() const { +Ref<Texture2D> TextureButton::get_pressed_texture() const { return pressed; } -Ref<Texture> TextureButton::get_hover_texture() const { +Ref<Texture2D> TextureButton::get_hover_texture() const { return hover; } -Ref<Texture> TextureButton::get_disabled_texture() const { +Ref<Texture2D> TextureButton::get_disabled_texture() const { return disabled; } -Ref<BitMap> TextureButton::get_click_mask() const { +Ref<BitMap> TextureButton::get_click_mask() const { return click_mask; } -Ref<Texture> TextureButton::get_focused_texture() const { - +Ref<Texture2D> TextureButton::get_focused_texture() const { return focused; }; -void TextureButton::set_focused_texture(const Ref<Texture> &p_focused) { - +void TextureButton::set_focused_texture(const Ref<Texture2D> &p_focused) { focused = p_focused; }; @@ -348,9 +357,29 @@ TextureButton::StretchMode TextureButton::get_stretch_mode() const { return stretch_mode; } +void TextureButton::set_flip_h(bool p_flip) { + hflip = p_flip; + update(); +} + +bool TextureButton::is_flipped_h() const { + return hflip; +} + +void TextureButton::set_flip_v(bool p_flip) { + vflip = p_flip; + update(); +} + +bool TextureButton::is_flipped_v() const { + return vflip; +} + TextureButton::TextureButton() { expand = false; stretch_mode = STRETCH_SCALE; + hflip = false; + vflip = false; _texture_region = Rect2(); _position_rect = Rect2(); |