2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* texture_button.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "texture_button.h"
|
2021-02-04 13:38:51 +00:00
|
|
|
|
2018-01-18 23:01:23 +00:00
|
|
|
#include "core/typedefs.h"
|
2021-02-04 13:38:51 +00:00
|
|
|
|
2018-01-18 23:01:23 +00:00
|
|
|
#include <stdlib.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Size2 TextureButton::get_minimum_size() const {
|
2017-02-09 14:46:35 +00:00
|
|
|
Size2 rscale = Control::get_minimum_size();
|
|
|
|
|
2022-02-05 01:11:32 +00:00
|
|
|
if (!ignore_texture_size) {
|
2017-02-09 14:46:35 +00:00
|
|
|
if (normal.is_null()) {
|
|
|
|
if (pressed.is_null()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (hover.is_null()) {
|
|
|
|
if (click_mask.is_null()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
rscale = Size2();
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
rscale = click_mask->get_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
rscale = hover->get_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
rscale = pressed->get_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
rscale = normal->get_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-02-09 14:46:35 +00:00
|
|
|
}
|
2015-01-03 03:23:14 +00:00
|
|
|
|
2017-02-09 14:46:35 +00:00
|
|
|
return rscale.abs();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool TextureButton::has_point(const Point2 &p_point) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (click_mask.is_valid()) {
|
2018-01-18 23:01:23 +00:00
|
|
|
Point2 point = p_point;
|
2022-11-14 17:21:06 +00:00
|
|
|
Rect2 rect;
|
2018-01-18 23:01:23 +00:00
|
|
|
Size2 mask_size = click_mask->get_size();
|
|
|
|
|
2022-08-15 02:51:45 +00:00
|
|
|
if (!_position_rect.has_area()) {
|
2019-02-20 23:47:17 +00:00
|
|
|
rect.size = mask_size;
|
|
|
|
} else if (_tile) {
|
2018-01-18 23:01:23 +00:00
|
|
|
// if the stretch mode is tile we offset the point to keep it inside the mask size
|
|
|
|
rect.size = mask_size;
|
|
|
|
if (_position_rect.has_point(point)) {
|
|
|
|
int cols = (int)Math::ceil(_position_rect.size.x / mask_size.x);
|
|
|
|
int rows = (int)Math::ceil(_position_rect.size.y / mask_size.y);
|
|
|
|
int col = (int)(point.x / mask_size.x) % cols;
|
|
|
|
int row = (int)(point.y / mask_size.y) % rows;
|
|
|
|
point.x -= mask_size.x * col;
|
|
|
|
point.y -= mask_size.y * row;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we need to transform the point from our scaled / translated image back to our mask image
|
|
|
|
Point2 ofs = _position_rect.position;
|
|
|
|
Size2 scale = mask_size / _position_rect.size;
|
|
|
|
|
|
|
|
switch (stretch_mode) {
|
|
|
|
case STRETCH_KEEP_ASPECT_COVERED: {
|
|
|
|
// if the stretch mode is aspect covered the image uses a texture region so we need to take that into account
|
|
|
|
float min = MIN(scale.x, scale.y);
|
|
|
|
scale.x = min;
|
|
|
|
scale.y = min;
|
|
|
|
ofs -= _texture_region.position / min;
|
|
|
|
} break;
|
2018-09-26 11:13:56 +00:00
|
|
|
default: {
|
|
|
|
// FIXME: Why a switch if we only handle one enum value?
|
|
|
|
}
|
2018-01-18 23:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// offset and scale the new point position to adjust it to the bitmask size
|
|
|
|
point -= ofs;
|
|
|
|
point *= scale;
|
|
|
|
|
|
|
|
// finally, we need to check if the point is inside a rectangle with a position >= 0,0 and a size <= mask_size
|
2024-03-03 13:37:52 +00:00
|
|
|
rect.position = _texture_region.position.maxf(0);
|
2024-03-03 11:49:08 +00:00
|
|
|
rect.size = mask_size.min(_texture_region.size);
|
2018-01-18 23:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rect.has_point(point)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2018-01-18 23:01:23 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-01-18 23:01:23 +00:00
|
|
|
Point2i p = point;
|
2022-09-01 16:39:17 +00:00
|
|
|
return click_mask->get_bitv(p);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Control::has_point(p_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureButton::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
DrawMode draw_mode = get_draw_mode();
|
2015-01-03 03:23:14 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> texdraw;
|
2015-01-03 03:23:14 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
switch (draw_mode) {
|
|
|
|
case DRAW_NORMAL: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (normal.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = normal;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
2018-10-05 23:06:32 +00:00
|
|
|
case DRAW_HOVER_PRESSED:
|
2014-02-10 01:10:30 +00:00
|
|
|
case DRAW_PRESSED: {
|
|
|
|
if (pressed.is_null()) {
|
|
|
|
if (hover.is_null()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (normal.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = normal;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = hover;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = pressed;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case DRAW_HOVER: {
|
|
|
|
if (hover.is_null()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (pressed.is_valid() && is_pressed()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = pressed;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else if (normal.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = normal;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = hover;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case DRAW_DISABLED: {
|
|
|
|
if (disabled.is_null()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (normal.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = normal;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
texdraw = disabled;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
2015-01-03 03:23:14 +00:00
|
|
|
|
2019-10-03 09:02:51 +00:00
|
|
|
Point2 ofs;
|
|
|
|
Size2 size;
|
2022-01-03 23:45:24 +00:00
|
|
|
bool draw_focus = (has_focus() && focused.is_valid());
|
|
|
|
|
|
|
|
// If no other texture is valid, try using focused texture.
|
2022-01-29 15:00:30 +00:00
|
|
|
bool draw_focus_only = draw_focus && !texdraw.is_valid();
|
|
|
|
if (draw_focus_only) {
|
2022-01-03 23:45:24 +00:00
|
|
|
texdraw = focused;
|
|
|
|
}
|
2019-10-03 09:02:51 +00:00
|
|
|
|
2024-05-21 20:45:12 +00:00
|
|
|
if (texdraw.is_valid() || click_mask.is_valid()) {
|
|
|
|
const Size2 texdraw_size = texdraw.is_valid() ? texdraw->get_size() : Size2(click_mask->get_size());
|
|
|
|
|
|
|
|
size = texdraw_size;
|
|
|
|
_texture_region = Rect2(Point2(), texdraw_size);
|
2018-01-18 23:01:23 +00:00
|
|
|
_tile = false;
|
2022-02-05 01:11:32 +00:00
|
|
|
switch (stretch_mode) {
|
|
|
|
case STRETCH_KEEP:
|
|
|
|
break;
|
|
|
|
case STRETCH_SCALE:
|
|
|
|
size = get_size();
|
|
|
|
break;
|
|
|
|
case STRETCH_TILE:
|
|
|
|
size = get_size();
|
|
|
|
_tile = true;
|
|
|
|
break;
|
|
|
|
case STRETCH_KEEP_CENTERED:
|
2024-05-21 20:45:12 +00:00
|
|
|
ofs = (get_size() - texdraw_size) / 2;
|
2022-02-05 01:11:32 +00:00
|
|
|
break;
|
|
|
|
case STRETCH_KEEP_ASPECT_CENTERED:
|
|
|
|
case STRETCH_KEEP_ASPECT: {
|
|
|
|
Size2 _size = get_size();
|
2024-05-21 20:45:12 +00:00
|
|
|
float tex_width = texdraw_size.width * _size.height / texdraw_size.height;
|
2022-02-05 01:11:32 +00:00
|
|
|
float tex_height = _size.height;
|
|
|
|
|
|
|
|
if (tex_width > _size.width) {
|
|
|
|
tex_width = _size.width;
|
2024-05-21 20:45:12 +00:00
|
|
|
tex_height = texdraw_size.height * tex_width / texdraw_size.width;
|
2022-02-05 01:11:32 +00:00
|
|
|
}
|
2017-02-09 14:46:35 +00:00
|
|
|
|
2022-02-05 01:11:32 +00:00
|
|
|
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 STRETCH_KEEP_ASPECT_COVERED: {
|
|
|
|
size = get_size();
|
2024-05-21 20:45:12 +00:00
|
|
|
Size2 scale_size = size / texdraw_size;
|
2022-02-05 01:11:32 +00:00
|
|
|
float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
|
2024-05-21 20:45:12 +00:00
|
|
|
Size2 scaled_tex_size = texdraw_size * scale;
|
2022-02-05 01:11:32 +00:00
|
|
|
Point2 ofs2 = ((scaled_tex_size - size) / scale).abs() / 2.0f;
|
|
|
|
_texture_region = Rect2(ofs2, size / scale);
|
|
|
|
} break;
|
2017-02-09 14:46:35 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
|
2018-01-18 23:01:23 +00:00
|
|
|
_position_rect = Rect2(ofs, size);
|
2019-10-03 09:02:51 +00:00
|
|
|
|
|
|
|
size.width *= hflip ? -1.0f : 1.0f;
|
|
|
|
size.height *= vflip ? -1.0f : 1.0f;
|
|
|
|
|
2022-01-29 15:00:30 +00:00
|
|
|
if (draw_focus_only) {
|
2022-01-03 23:45:24 +00:00
|
|
|
// Do nothing, we only needed to calculate the rectangle.
|
2024-05-21 20:45:12 +00:00
|
|
|
} else if (texdraw.is_valid()) {
|
|
|
|
if (_tile) {
|
|
|
|
draw_texture_rect(texdraw, Rect2(ofs, size), _tile);
|
|
|
|
} else {
|
|
|
|
draw_texture_rect_region(texdraw, Rect2(ofs, size), _texture_region);
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
2019-02-20 23:47:17 +00:00
|
|
|
} else {
|
|
|
|
_position_rect = Rect2();
|
2015-01-03 03:23:14 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-01-03 23:45:24 +00:00
|
|
|
if (draw_focus) {
|
2019-10-03 09:02:51 +00:00
|
|
|
draw_texture_rect(focused, Rect2(ofs, size), false);
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureButton::_bind_methods() {
|
2022-11-07 08:09:13 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TextureButton::set_texture_normal);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TextureButton::set_texture_pressed);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_texture_hover", "texture"), &TextureButton::set_texture_hover);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_texture_disabled", "texture"), &TextureButton::set_texture_disabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_texture_focused", "texture"), &TextureButton::set_texture_focused);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_click_mask", "mask"), &TextureButton::set_click_mask);
|
2022-02-05 01:11:32 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_ignore_texture_size", "ignore"), &TextureButton::set_ignore_texture_size);
|
2021-02-04 13:38:51 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_stretch_mode", "mode"), &TextureButton::set_stretch_mode);
|
2019-10-03 09:02:51 +00:00
|
|
|
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);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_texture_normal"), &TextureButton::get_texture_normal);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TextureButton::get_texture_pressed);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_texture_hover"), &TextureButton::get_texture_hover);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_texture_disabled"), &TextureButton::get_texture_disabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_texture_focused"), &TextureButton::get_texture_focused);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_click_mask"), &TextureButton::get_click_mask);
|
2022-02-05 01:11:32 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_ignore_texture_size"), &TextureButton::get_ignore_texture_size);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode);
|
|
|
|
|
|
|
|
ADD_GROUP("Textures", "texture_");
|
2022-11-07 08:09:13 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_hover", "get_texture_hover");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_disabled", "get_texture_disabled");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_focused", "get_texture_focused");
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask");
|
2022-02-05 01:11:32 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_texture_size", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_ignore_texture_size", "get_ignore_texture_size");
|
2018-11-08 14:30:02 +00:00
|
|
|
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");
|
2019-10-03 09:02:51 +00:00
|
|
|
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");
|
2017-02-13 11:41:12 +00:00
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(STRETCH_SCALE);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_TILE);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
void TextureButton::set_texture_normal(const Ref<Texture2D> &p_normal) {
|
2023-05-17 12:49:59 +00:00
|
|
|
_set_texture(&normal, p_normal);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
void TextureButton::set_texture_pressed(const Ref<Texture2D> &p_pressed) {
|
2023-05-17 12:49:59 +00:00
|
|
|
_set_texture(&pressed, p_pressed);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
void TextureButton::set_texture_hover(const Ref<Texture2D> &p_hover) {
|
2023-05-17 12:49:59 +00:00
|
|
|
_set_texture(&hover, p_hover);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
void TextureButton::set_texture_disabled(const Ref<Texture2D> &p_disabled) {
|
2023-05-17 12:49:59 +00:00
|
|
|
_set_texture(&disabled, p_disabled);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
|
2022-03-16 07:50:48 +00:00
|
|
|
if (click_mask == p_click_mask) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
click_mask = p_click_mask;
|
2023-05-17 12:49:59 +00:00
|
|
|
_texture_changed();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
Ref<Texture2D> TextureButton::get_texture_normal() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return normal;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
Ref<Texture2D> TextureButton::get_texture_pressed() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return pressed;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
Ref<Texture2D> TextureButton::get_texture_hover() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return hover;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
Ref<Texture2D> TextureButton::get_texture_disabled() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return disabled;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<BitMap> TextureButton::get_click_mask() const {
|
|
|
|
return click_mask;
|
|
|
|
}
|
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
Ref<Texture2D> TextureButton::get_texture_focused() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return focused;
|
|
|
|
};
|
|
|
|
|
2022-11-07 08:09:13 +00:00
|
|
|
void TextureButton::set_texture_focused(const Ref<Texture2D> &p_focused) {
|
2014-02-10 01:10:30 +00:00
|
|
|
focused = p_focused;
|
|
|
|
};
|
|
|
|
|
2023-05-17 12:49:59 +00:00
|
|
|
void TextureButton::_set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture) {
|
|
|
|
DEV_ASSERT(p_destination);
|
|
|
|
Ref<Texture2D> &destination = *p_destination;
|
|
|
|
if (destination == p_texture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (destination.is_valid()) {
|
2023-07-03 19:29:37 +00:00
|
|
|
destination->disconnect_changed(callable_mp(this, &TextureButton::_texture_changed));
|
2023-05-17 12:49:59 +00:00
|
|
|
}
|
|
|
|
destination = p_texture;
|
|
|
|
if (destination.is_valid()) {
|
|
|
|
// Pass `CONNECT_REFERENCE_COUNTED` to avoid early disconnect in case the same texture is assigned to different "slots".
|
2023-07-03 19:29:37 +00:00
|
|
|
destination->connect_changed(callable_mp(this, &TextureButton::_texture_changed), CONNECT_REFERENCE_COUNTED);
|
2023-05-17 12:49:59 +00:00
|
|
|
}
|
|
|
|
_texture_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureButton::_texture_changed() {
|
|
|
|
queue_redraw();
|
|
|
|
update_minimum_size();
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:11:32 +00:00
|
|
|
bool TextureButton::get_ignore_texture_size() const {
|
|
|
|
return ignore_texture_size;
|
2017-02-09 14:46:35 +00:00
|
|
|
}
|
2015-01-03 03:23:14 +00:00
|
|
|
|
2022-02-05 01:11:32 +00:00
|
|
|
void TextureButton::set_ignore_texture_size(bool p_ignore) {
|
2022-03-16 07:50:48 +00:00
|
|
|
if (ignore_texture_size == p_ignore) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:11:32 +00:00
|
|
|
ignore_texture_size = p_ignore;
|
2021-12-06 13:02:34 +00:00
|
|
|
update_minimum_size();
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2015-01-03 03:23:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
void TextureButton::set_stretch_mode(StretchMode p_stretch_mode) {
|
2022-03-16 07:50:48 +00:00
|
|
|
if (stretch_mode == p_stretch_mode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
stretch_mode = p_stretch_mode;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2017-02-09 14:46:35 +00:00
|
|
|
}
|
2015-01-03 03:23:14 +00:00
|
|
|
|
2017-02-09 14:46:35 +00:00
|
|
|
TextureButton::StretchMode TextureButton::get_stretch_mode() const {
|
|
|
|
return stretch_mode;
|
2015-01-03 03:23:14 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 09:02:51 +00:00
|
|
|
void TextureButton::set_flip_h(bool p_flip) {
|
2022-03-16 07:50:48 +00:00
|
|
|
if (hflip == p_flip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-03 09:02:51 +00:00
|
|
|
hflip = p_flip;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2019-10-03 09:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureButton::is_flipped_h() const {
|
|
|
|
return hflip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureButton::set_flip_v(bool p_flip) {
|
2022-03-16 07:50:48 +00:00
|
|
|
if (vflip == p_flip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-03 09:02:51 +00:00
|
|
|
vflip = p_flip;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2019-10-03 09:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureButton::is_flipped_v() const {
|
|
|
|
return vflip;
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:24:36 +00:00
|
|
|
TextureButton::TextureButton() {}
|