| 1 | /**************************************************************************/ |
| 2 | /* texture_button.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "texture_button.h" |
| 32 | |
| 33 | #include "core/typedefs.h" |
| 34 | |
| 35 | Size2 TextureButton::get_minimum_size() const { |
| 36 | Size2 rscale = Control::get_minimum_size(); |
| 37 | |
| 38 | if (!ignore_texture_size) { |
| 39 | if (normal.is_null()) { |
| 40 | if (pressed.is_null()) { |
| 41 | if (hover.is_null()) { |
| 42 | if (click_mask.is_null()) { |
| 43 | rscale = Size2(); |
| 44 | } else { |
| 45 | rscale = click_mask->get_size(); |
| 46 | } |
| 47 | } else { |
| 48 | rscale = hover->get_size(); |
| 49 | } |
| 50 | } else { |
| 51 | rscale = pressed->get_size(); |
| 52 | } |
| 53 | |
| 54 | } else { |
| 55 | rscale = normal->get_size(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return rscale.abs(); |
| 60 | } |
| 61 | |
| 62 | bool TextureButton::has_point(const Point2 &p_point) const { |
| 63 | if (click_mask.is_valid()) { |
| 64 | Point2 point = p_point; |
| 65 | Rect2 rect; |
| 66 | Size2 mask_size = click_mask->get_size(); |
| 67 | |
| 68 | if (!_position_rect.has_area()) { |
| 69 | rect.size = mask_size; |
| 70 | } else if (_tile) { |
| 71 | // if the stretch mode is tile we offset the point to keep it inside the mask size |
| 72 | rect.size = mask_size; |
| 73 | if (_position_rect.has_point(point)) { |
| 74 | int cols = (int)Math::ceil(_position_rect.size.x / mask_size.x); |
| 75 | int rows = (int)Math::ceil(_position_rect.size.y / mask_size.y); |
| 76 | int col = (int)(point.x / mask_size.x) % cols; |
| 77 | int row = (int)(point.y / mask_size.y) % rows; |
| 78 | point.x -= mask_size.x * col; |
| 79 | point.y -= mask_size.y * row; |
| 80 | } |
| 81 | } else { |
| 82 | // we need to transform the point from our scaled / translated image back to our mask image |
| 83 | Point2 ofs = _position_rect.position; |
| 84 | Size2 scale = mask_size / _position_rect.size; |
| 85 | |
| 86 | switch (stretch_mode) { |
| 87 | case STRETCH_KEEP_ASPECT_COVERED: { |
| 88 | // if the stretch mode is aspect covered the image uses a texture region so we need to take that into account |
| 89 | float min = MIN(scale.x, scale.y); |
| 90 | scale.x = min; |
| 91 | scale.y = min; |
| 92 | ofs -= _texture_region.position / min; |
| 93 | } break; |
| 94 | default: { |
| 95 | // FIXME: Why a switch if we only handle one enum value? |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // offset and scale the new point position to adjust it to the bitmask size |
| 100 | point -= ofs; |
| 101 | point *= scale; |
| 102 | |
| 103 | // finally, we need to check if the point is inside a rectangle with a position >= 0,0 and a size <= mask_size |
| 104 | rect.position = _texture_region.position.maxf(0); |
| 105 | rect.size = mask_size.min(_texture_region.size); |
| 106 | } |
| 107 | |
| 108 | if (!rect.has_point(point)) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | Point2i p = point; |
| 113 | return click_mask->get_bitv(p); |
| 114 | } |
| 115 | |
| 116 | return Control::has_point(p_point); |
| 117 | } |
| 118 | |
| 119 | void TextureButton::_notification(int p_what) { |
| 120 | switch (p_what) { |
| 121 | case NOTIFICATION_DRAW: { |
| 122 | DrawMode draw_mode = get_draw_mode(); |
| 123 | |
| 124 | Ref<Texture2D> texdraw; |
| 125 | |
| 126 | switch (draw_mode) { |
| 127 | case DRAW_NORMAL: { |
| 128 | if (normal.is_valid()) { |
| 129 | texdraw = normal; |
| 130 | } |
| 131 | } break; |
| 132 | case DRAW_HOVER_PRESSED: |
| 133 | case DRAW_PRESSED: { |
| 134 | if (pressed.is_null()) { |
| 135 | if (hover.is_null()) { |
| 136 | if (normal.is_valid()) { |
| 137 | texdraw = normal; |
| 138 | } |
| 139 | } else { |
| 140 | texdraw = hover; |
| 141 | } |
| 142 | |
| 143 | } else { |
| 144 | texdraw = pressed; |
| 145 | } |
| 146 | } break; |
| 147 | case DRAW_HOVER: { |
| 148 | if (hover.is_null()) { |
| 149 | if (pressed.is_valid() && is_pressed()) { |
| 150 | texdraw = pressed; |
| 151 | } else if (normal.is_valid()) { |
| 152 | texdraw = normal; |
| 153 | } |
| 154 | } else { |
| 155 | texdraw = hover; |
| 156 | } |
| 157 | } break; |
| 158 | case DRAW_DISABLED: { |
| 159 | if (disabled.is_null()) { |
| 160 | if (normal.is_valid()) { |
| 161 | texdraw = normal; |
| 162 | } |
| 163 | } else { |
| 164 | texdraw = disabled; |
| 165 | } |
| 166 | } break; |
| 167 | } |
| 168 | |
| 169 | Point2 ofs; |
| 170 | Size2 size; |
| 171 | bool draw_focus = (has_focus(true) && focused.is_valid()); |
| 172 | |
| 173 | // If no other texture is valid, try using focused texture. |
| 174 | bool draw_focus_only = draw_focus && texdraw.is_null(); |
| 175 | if (draw_focus_only) { |
| 176 | texdraw = focused; |
| 177 | } |
| 178 | |
| 179 | if (texdraw.is_valid() || click_mask.is_valid()) { |
| 180 | const Size2 texdraw_size = texdraw.is_valid() ? texdraw->get_size() : Size2(click_mask->get_size()); |
| 181 | |
| 182 | size = texdraw_size; |
| 183 | _texture_region = Rect2(Point2(), texdraw_size); |
| 184 | _tile = false; |
| 185 | switch (stretch_mode) { |
| 186 | case STRETCH_KEEP: |
| 187 | break; |
| 188 | case STRETCH_SCALE: |
| 189 | size = get_size(); |
| 190 | break; |
| 191 | case STRETCH_TILE: |
| 192 | size = get_size(); |
| 193 | _tile = true; |
| 194 | break; |
| 195 | case STRETCH_KEEP_CENTERED: |
| 196 | ofs = (get_size() - texdraw_size) / 2; |
| 197 | break; |
| 198 | case STRETCH_KEEP_ASPECT_CENTERED: |
| 199 | case STRETCH_KEEP_ASPECT: { |
| 200 | Size2 _size = get_size(); |
| 201 | float tex_width = texdraw_size.width * _size.height / texdraw_size.height; |
| 202 | float tex_height = _size.height; |
| 203 | |
| 204 | if (tex_width > _size.width) { |
| 205 | tex_width = _size.width; |
| 206 | tex_height = texdraw_size.height * tex_width / texdraw_size.width; |
| 207 | } |
| 208 | |
| 209 | if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) { |
| 210 | ofs.x = (_size.width - tex_width) / 2; |
| 211 | ofs.y = (_size.height - tex_height) / 2; |
| 212 | } |
| 213 | size.width = tex_width; |
| 214 | size.height = tex_height; |
| 215 | } break; |
| 216 | case STRETCH_KEEP_ASPECT_COVERED: { |
| 217 | size = get_size(); |
| 218 | Size2 scale_size = size / texdraw_size; |
| 219 | float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height; |
| 220 | Size2 scaled_tex_size = texdraw_size * scale; |
| 221 | Point2 ofs2 = ((scaled_tex_size - size) / scale).abs() / 2.0f; |
| 222 | _texture_region = Rect2(ofs2, size / scale); |
| 223 | } break; |
| 224 | } |
| 225 | |
| 226 | _position_rect = Rect2(ofs, size); |
| 227 | |
| 228 | size.width *= hflip ? -1.0f : 1.0f; |
| 229 | size.height *= vflip ? -1.0f : 1.0f; |
| 230 | |
| 231 | if (draw_focus_only) { |
| 232 | // Do nothing, we only needed to calculate the rectangle. |
| 233 | } else if (texdraw.is_valid()) { |
| 234 | if (_tile) { |
| 235 | draw_texture_rect(texdraw, Rect2(ofs, size), _tile); |
| 236 | } else { |
| 237 | draw_texture_rect_region(texdraw, Rect2(ofs, size), _texture_region); |
| 238 | } |
| 239 | } |
| 240 | } else { |
| 241 | _position_rect = Rect2(); |
| 242 | } |
| 243 | |
| 244 | if (draw_focus) { |
| 245 | draw_texture_rect(focused, Rect2(ofs, size), false); |
| 246 | }; |
| 247 | } break; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void TextureButton::_bind_methods() { |
| 252 | ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TextureButton::set_texture_normal); |
| 253 | ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TextureButton::set_texture_pressed); |
| 254 | ClassDB::bind_method(D_METHOD("set_texture_hover", "texture"), &TextureButton::set_texture_hover); |
| 255 | ClassDB::bind_method(D_METHOD("set_texture_disabled", "texture"), &TextureButton::set_texture_disabled); |
| 256 | ClassDB::bind_method(D_METHOD("set_texture_focused", "texture"), &TextureButton::set_texture_focused); |
| 257 | ClassDB::bind_method(D_METHOD("set_click_mask", "mask"), &TextureButton::set_click_mask); |
| 258 | ClassDB::bind_method(D_METHOD("set_ignore_texture_size", "ignore"), &TextureButton::set_ignore_texture_size); |
| 259 | ClassDB::bind_method(D_METHOD("set_stretch_mode", "mode"), &TextureButton::set_stretch_mode); |
| 260 | ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureButton::set_flip_h); |
| 261 | ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureButton::is_flipped_h); |
| 262 | ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureButton::set_flip_v); |
| 263 | ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureButton::is_flipped_v); |
| 264 | |
| 265 | ClassDB::bind_method(D_METHOD("get_texture_normal"), &TextureButton::get_texture_normal); |
| 266 | ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TextureButton::get_texture_pressed); |
| 267 | ClassDB::bind_method(D_METHOD("get_texture_hover"), &TextureButton::get_texture_hover); |
| 268 | ClassDB::bind_method(D_METHOD("get_texture_disabled"), &TextureButton::get_texture_disabled); |
| 269 | ClassDB::bind_method(D_METHOD("get_texture_focused"), &TextureButton::get_texture_focused); |
| 270 | ClassDB::bind_method(D_METHOD("get_click_mask"), &TextureButton::get_click_mask); |
| 271 | ClassDB::bind_method(D_METHOD("get_ignore_texture_size"), &TextureButton::get_ignore_texture_size); |
| 272 | ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode); |
| 273 | |
| 274 | ADD_GROUP("Textures", "texture_"); |
| 275 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal"); |
| 276 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed"); |
| 277 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_hover", "get_texture_hover"); |
| 278 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_disabled", "get_texture_disabled"); |
| 279 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_focused", "get_texture_focused"); |
| 280 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask"); |
| 281 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_texture_size", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_ignore_texture_size", "get_ignore_texture_size"); |
| 282 | 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"); |
| 283 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_h", "is_flipped_h"); |
| 284 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_v", "is_flipped_v"); |
| 285 | |
| 286 | BIND_ENUM_CONSTANT(STRETCH_SCALE); |
| 287 | BIND_ENUM_CONSTANT(STRETCH_TILE); |
| 288 | BIND_ENUM_CONSTANT(STRETCH_KEEP); |
| 289 | BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED); |
| 290 | BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT); |
| 291 | BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED); |
| 292 | BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED); |
| 293 | } |
| 294 | |
| 295 | void TextureButton::set_texture_normal(const Ref<Texture2D> &p_normal) { |
| 296 | _set_texture(&normal, p_normal); |
| 297 | } |
| 298 | |
| 299 | void TextureButton::set_texture_pressed(const Ref<Texture2D> &p_pressed) { |
| 300 | _set_texture(&pressed, p_pressed); |
| 301 | } |
| 302 | |
| 303 | void TextureButton::set_texture_hover(const Ref<Texture2D> &p_hover) { |
| 304 | _set_texture(&hover, p_hover); |
| 305 | } |
| 306 | |
| 307 | void TextureButton::set_texture_disabled(const Ref<Texture2D> &p_disabled) { |
| 308 | _set_texture(&disabled, p_disabled); |
| 309 | } |
| 310 | |
| 311 | void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) { |
| 312 | if (click_mask == p_click_mask) { |
| 313 | return; |
| 314 | } |
| 315 | click_mask = p_click_mask; |
| 316 | _texture_changed(); |
| 317 | } |
| 318 | |
| 319 | Ref<Texture2D> TextureButton::get_texture_normal() const { |
| 320 | return normal; |
| 321 | } |
| 322 | |
| 323 | Ref<Texture2D> TextureButton::get_texture_pressed() const { |
| 324 | return pressed; |
| 325 | } |
| 326 | |
| 327 | Ref<Texture2D> TextureButton::get_texture_hover() const { |
| 328 | return hover; |
| 329 | } |
| 330 | |
| 331 | Ref<Texture2D> TextureButton::get_texture_disabled() const { |
| 332 | return disabled; |
| 333 | } |
| 334 | |
| 335 | Ref<BitMap> TextureButton::get_click_mask() const { |
| 336 | return click_mask; |
| 337 | } |
| 338 | |
| 339 | Ref<Texture2D> TextureButton::get_texture_focused() const { |
| 340 | return focused; |
| 341 | } |
| 342 | |
| 343 | void TextureButton::set_texture_focused(const Ref<Texture2D> &p_focused) { |
| 344 | _set_texture(&focused, p_focused); |
| 345 | } |
| 346 | |
| 347 | void TextureButton::_set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture) { |
| 348 | DEV_ASSERT(p_destination); |
| 349 | Ref<Texture2D> &destination = *p_destination; |
| 350 | if (destination == p_texture) { |
| 351 | return; |
| 352 | } |
| 353 | if (destination.is_valid()) { |
| 354 | destination->disconnect_changed(callable_mp(this, &TextureButton::_texture_changed)); |
| 355 | } |
| 356 | destination = p_texture; |
| 357 | if (destination.is_valid()) { |
| 358 | // Pass `CONNECT_REFERENCE_COUNTED` to avoid early disconnect in case the same texture is assigned to different "slots". |
| 359 | destination->connect_changed(callable_mp(this, &TextureButton::_texture_changed), CONNECT_REFERENCE_COUNTED); |
| 360 | } |
| 361 | _texture_changed(); |
| 362 | } |
| 363 | |
| 364 | void TextureButton::_texture_changed() { |
| 365 | queue_redraw(); |
| 366 | update_minimum_size(); |
| 367 | } |
| 368 | |
| 369 | bool TextureButton::get_ignore_texture_size() const { |
| 370 | return ignore_texture_size; |
| 371 | } |
| 372 | |
| 373 | void TextureButton::set_ignore_texture_size(bool p_ignore) { |
| 374 | if (ignore_texture_size == p_ignore) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | ignore_texture_size = p_ignore; |
| 379 | update_minimum_size(); |
| 380 | queue_redraw(); |
| 381 | } |
| 382 | |
| 383 | void TextureButton::set_stretch_mode(StretchMode p_stretch_mode) { |
| 384 | if (stretch_mode == p_stretch_mode) { |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | stretch_mode = p_stretch_mode; |
| 389 | queue_redraw(); |
| 390 | } |
| 391 | |
| 392 | TextureButton::StretchMode TextureButton::get_stretch_mode() const { |
| 393 | return stretch_mode; |
| 394 | } |
| 395 | |
| 396 | void TextureButton::set_flip_h(bool p_flip) { |
| 397 | if (hflip == p_flip) { |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | hflip = p_flip; |
| 402 | queue_redraw(); |
| 403 | } |
| 404 | |
| 405 | bool TextureButton::is_flipped_h() const { |
| 406 | return hflip; |
| 407 | } |
| 408 | |
| 409 | void TextureButton::set_flip_v(bool p_flip) { |
| 410 | if (vflip == p_flip) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | vflip = p_flip; |
| 415 | queue_redraw(); |
| 416 | } |
| 417 | |
| 418 | bool TextureButton::is_flipped_v() const { |
| 419 | return vflip; |
| 420 | } |