| 1 | /**************************************************************************/ |
| 2 | /* texture_progress_bar.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_progress_bar.h" |
| 32 | |
| 33 | void TextureProgressBar::set_under_texture(const Ref<Texture2D> &p_texture) { |
| 34 | _set_texture(&under, p_texture); |
| 35 | } |
| 36 | |
| 37 | Ref<Texture2D> TextureProgressBar::get_under_texture() const { |
| 38 | return under; |
| 39 | } |
| 40 | |
| 41 | void TextureProgressBar::set_over_texture(const Ref<Texture2D> &p_texture) { |
| 42 | _set_texture(&over, p_texture); |
| 43 | } |
| 44 | |
| 45 | Ref<Texture2D> TextureProgressBar::get_over_texture() const { |
| 46 | return over; |
| 47 | } |
| 48 | |
| 49 | void TextureProgressBar::set_stretch_margin(Side p_side, int p_size) { |
| 50 | ERR_FAIL_INDEX((int)p_side, 4); |
| 51 | |
| 52 | if (stretch_margin[p_side] == p_size) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | stretch_margin[p_side] = p_size; |
| 57 | queue_redraw(); |
| 58 | update_minimum_size(); |
| 59 | } |
| 60 | |
| 61 | int TextureProgressBar::get_stretch_margin(Side p_side) const { |
| 62 | ERR_FAIL_INDEX_V((int)p_side, 4, 0); |
| 63 | return stretch_margin[p_side]; |
| 64 | } |
| 65 | |
| 66 | void TextureProgressBar::set_nine_patch_stretch(bool p_stretch) { |
| 67 | if (nine_patch_stretch == p_stretch) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | nine_patch_stretch = p_stretch; |
| 72 | queue_redraw(); |
| 73 | update_minimum_size(); |
| 74 | notify_property_list_changed(); |
| 75 | } |
| 76 | |
| 77 | bool TextureProgressBar::get_nine_patch_stretch() const { |
| 78 | return nine_patch_stretch; |
| 79 | } |
| 80 | |
| 81 | Size2 TextureProgressBar::get_minimum_size() const { |
| 82 | if (nine_patch_stretch) { |
| 83 | return Size2(stretch_margin[SIDE_LEFT] + stretch_margin[SIDE_RIGHT], stretch_margin[SIDE_TOP] + stretch_margin[SIDE_BOTTOM]); |
| 84 | } |
| 85 | |
| 86 | Size2 size = Size2(1, 1); |
| 87 | if (under.is_valid()) { |
| 88 | size = size.max(under->get_size()); |
| 89 | } |
| 90 | if (progress.is_valid()) { |
| 91 | size = size.max(progress->get_size()); |
| 92 | } |
| 93 | if (over.is_valid()) { |
| 94 | size = size.max(over->get_size()); |
| 95 | } |
| 96 | return size; |
| 97 | } |
| 98 | |
| 99 | void TextureProgressBar::set_progress_texture(const Ref<Texture2D> &p_texture) { |
| 100 | _set_texture(&progress, p_texture); |
| 101 | } |
| 102 | |
| 103 | Ref<Texture2D> TextureProgressBar::get_progress_texture() const { |
| 104 | return progress; |
| 105 | } |
| 106 | |
| 107 | void TextureProgressBar::set_progress_offset(Point2 p_offset) { |
| 108 | if (progress_offset == p_offset) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | progress_offset = p_offset; |
| 113 | queue_redraw(); |
| 114 | } |
| 115 | |
| 116 | Point2 TextureProgressBar::get_progress_offset() const { |
| 117 | return progress_offset; |
| 118 | } |
| 119 | |
| 120 | void TextureProgressBar::set_tint_under(const Color &p_tint) { |
| 121 | if (tint_under == p_tint) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | tint_under = p_tint; |
| 126 | queue_redraw(); |
| 127 | } |
| 128 | |
| 129 | Color TextureProgressBar::get_tint_under() const { |
| 130 | return tint_under; |
| 131 | } |
| 132 | |
| 133 | void TextureProgressBar::set_tint_progress(const Color &p_tint) { |
| 134 | if (tint_progress == p_tint) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | tint_progress = p_tint; |
| 139 | queue_redraw(); |
| 140 | } |
| 141 | |
| 142 | Color TextureProgressBar::get_tint_progress() const { |
| 143 | return tint_progress; |
| 144 | } |
| 145 | |
| 146 | void TextureProgressBar::set_tint_over(const Color &p_tint) { |
| 147 | if (tint_over == p_tint) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | tint_over = p_tint; |
| 152 | queue_redraw(); |
| 153 | } |
| 154 | |
| 155 | Color TextureProgressBar::get_tint_over() const { |
| 156 | return tint_over; |
| 157 | } |
| 158 | |
| 159 | void TextureProgressBar::_set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture) { |
| 160 | DEV_ASSERT(p_destination); |
| 161 | Ref<Texture2D> &destination = *p_destination; |
| 162 | if (destination == p_texture) { |
| 163 | return; |
| 164 | } |
| 165 | if (destination.is_valid()) { |
| 166 | destination->disconnect_changed(callable_mp(this, &TextureProgressBar::_texture_changed)); |
| 167 | } |
| 168 | destination = p_texture; |
| 169 | if (destination.is_valid()) { |
| 170 | // Pass `CONNECT_REFERENCE_COUNTED` to avoid early disconnect in case the same texture is assigned to different "slots". |
| 171 | destination->connect_changed(callable_mp(this, &TextureProgressBar::_texture_changed), CONNECT_REFERENCE_COUNTED); |
| 172 | } |
| 173 | _texture_changed(); |
| 174 | } |
| 175 | |
| 176 | void TextureProgressBar::_texture_changed() { |
| 177 | update_minimum_size(); |
| 178 | queue_redraw(); |
| 179 | } |
| 180 | |
| 181 | Point2 TextureProgressBar::unit_val_to_uv(float val) { |
| 182 | if (progress.is_null()) { |
| 183 | return Point2(); |
| 184 | } |
| 185 | |
| 186 | if (val < 0) { |
| 187 | val += 1; |
| 188 | } |
| 189 | if (val > 1) { |
| 190 | val -= 1; |
| 191 | } |
| 192 | |
| 193 | Point2 p = get_relative_center(); |
| 194 | |
| 195 | // Minimal version of Liang-Barsky clipping algorithm |
| 196 | float angle = (val * Math::TAU) - Math::PI * 0.5; |
| 197 | Point2 dir = Vector2(Math::cos(angle), Math::sin(angle)); |
| 198 | float t1 = 1.0; |
| 199 | float cp = 0.0; |
| 200 | float cq = 0.0; |
| 201 | float cr = 0.0; |
| 202 | float edgeLeft = 0.0; |
| 203 | float edgeRight = 1.0; |
| 204 | float edgeBottom = 0.0; |
| 205 | float edgeTop = 1.0; |
| 206 | |
| 207 | for (int edge = 0; edge < 4; edge++) { |
| 208 | if (edge == 0) { |
| 209 | if (dir.x > 0) { |
| 210 | continue; |
| 211 | } |
| 212 | cq = -(edgeLeft - p.x); |
| 213 | dir.x *= 2.0 * cq; |
| 214 | cp = -dir.x; |
| 215 | } else if (edge == 1) { |
| 216 | if (dir.x < 0) { |
| 217 | continue; |
| 218 | } |
| 219 | cq = (edgeRight - p.x); |
| 220 | dir.x *= 2.0 * cq; |
| 221 | cp = dir.x; |
| 222 | } else if (edge == 2) { |
| 223 | if (dir.y > 0) { |
| 224 | continue; |
| 225 | } |
| 226 | cq = -(edgeBottom - p.y); |
| 227 | dir.y *= 2.0 * cq; |
| 228 | cp = -dir.y; |
| 229 | } else if (edge == 3) { |
| 230 | if (dir.y < 0) { |
| 231 | continue; |
| 232 | } |
| 233 | cq = (edgeTop - p.y); |
| 234 | dir.y *= 2.0 * cq; |
| 235 | cp = dir.y; |
| 236 | } |
| 237 | cr = cq / cp; |
| 238 | if (cr >= 0 && cr < t1) { |
| 239 | t1 = cr; |
| 240 | } |
| 241 | } |
| 242 | return (p + t1 * dir); |
| 243 | } |
| 244 | |
| 245 | Point2 TextureProgressBar::get_relative_center() { |
| 246 | if (progress.is_null()) { |
| 247 | return Point2(); |
| 248 | } |
| 249 | Point2 p = progress->get_size() / 2; |
| 250 | p += rad_center_off; |
| 251 | p.x /= progress->get_width(); |
| 252 | p.y /= progress->get_height(); |
| 253 | p = p.clampf(0, 1); |
| 254 | return p; |
| 255 | } |
| 256 | |
| 257 | void TextureProgressBar::draw_nine_patch_stretched(const Ref<Texture2D> &p_texture, FillMode p_mode, double p_ratio, const Color &p_modulate) { |
| 258 | Vector2 texture_size = p_texture->get_size(); |
| 259 | Vector2 topleft = Vector2(stretch_margin[SIDE_LEFT], stretch_margin[SIDE_TOP]); |
| 260 | Vector2 bottomright = Vector2(stretch_margin[SIDE_RIGHT], stretch_margin[SIDE_BOTTOM]); |
| 261 | |
| 262 | Rect2 src_rect = Rect2(Point2(), texture_size); |
| 263 | Rect2 dst_rect = Rect2(Point2(), get_size()); |
| 264 | |
| 265 | if (p_ratio < 1.0) { |
| 266 | // Drawing a partially-filled 9-patch is a little tricky - |
| 267 | // texture is divided by 3 sections toward fill direction, |
| 268 | // then middle section is stretching while the other two aren't. |
| 269 | |
| 270 | double width_total = 0.0; |
| 271 | double width_texture = 0.0; |
| 272 | double first_section_size = 0.0; |
| 273 | double last_section_size = 0.0; |
| 274 | switch (p_mode) { |
| 275 | case FILL_LEFT_TO_RIGHT: { |
| 276 | width_total = dst_rect.size.x; |
| 277 | width_texture = texture_size.x; |
| 278 | first_section_size = topleft.x; |
| 279 | last_section_size = bottomright.x; |
| 280 | } break; |
| 281 | case FILL_RIGHT_TO_LEFT: { |
| 282 | width_total = dst_rect.size.x; |
| 283 | width_texture = texture_size.x; |
| 284 | // In contrast to `FILL_LEFT_TO_RIGHT`, `first_section_size` and `last_section_size` should switch value. |
| 285 | first_section_size = bottomright.x; |
| 286 | last_section_size = topleft.x; |
| 287 | } break; |
| 288 | case FILL_TOP_TO_BOTTOM: { |
| 289 | width_total = dst_rect.size.y; |
| 290 | width_texture = texture_size.y; |
| 291 | first_section_size = topleft.y; |
| 292 | last_section_size = bottomright.y; |
| 293 | } break; |
| 294 | case FILL_BOTTOM_TO_TOP: { |
| 295 | width_total = dst_rect.size.y; |
| 296 | width_texture = texture_size.y; |
| 297 | // Similar to `FILL_RIGHT_TO_LEFT`. |
| 298 | first_section_size = bottomright.y; |
| 299 | last_section_size = topleft.y; |
| 300 | } break; |
| 301 | case FILL_BILINEAR_LEFT_AND_RIGHT: { |
| 302 | width_total = dst_rect.size.x; |
| 303 | width_texture = texture_size.x; |
| 304 | first_section_size = topleft.x; |
| 305 | last_section_size = bottomright.x; |
| 306 | } break; |
| 307 | case FILL_BILINEAR_TOP_AND_BOTTOM: { |
| 308 | width_total = dst_rect.size.y; |
| 309 | width_texture = texture_size.y; |
| 310 | first_section_size = topleft.y; |
| 311 | last_section_size = bottomright.y; |
| 312 | } break; |
| 313 | case FILL_CLOCKWISE: |
| 314 | case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: |
| 315 | case FILL_COUNTER_CLOCKWISE: { |
| 316 | // Those modes are circular, not relevant for nine patch. |
| 317 | } break; |
| 318 | case FILL_MODE_MAX: |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | double width_filled = width_total * p_ratio; |
| 323 | double middle_section_size = MAX(0.0, width_texture - first_section_size - last_section_size); |
| 324 | |
| 325 | // Maximum middle texture size. |
| 326 | double max_middle_texture_size = middle_section_size; |
| 327 | |
| 328 | // Maximum real middle texture size. |
| 329 | double max_middle_real_size = MAX(0.0, width_total - (first_section_size + last_section_size)); |
| 330 | |
| 331 | switch (p_mode) { |
| 332 | case FILL_BILINEAR_LEFT_AND_RIGHT: |
| 333 | case FILL_BILINEAR_TOP_AND_BOTTOM: { |
| 334 | last_section_size = MAX(0.0, last_section_size - (width_total - width_filled) * 0.5); |
| 335 | first_section_size = MAX(0.0, first_section_size - (width_total - width_filled) * 0.5); |
| 336 | |
| 337 | // When `width_filled` increases, `middle_section_size` only increases when either of `first_section_size` and `last_section_size` is zero. |
| 338 | // Also, it should always be smaller than or equal to `(width_total - (first_section_size + last_section_size))`. |
| 339 | double real_middle_size = width_filled - first_section_size - last_section_size; |
| 340 | middle_section_size *= MIN(max_middle_real_size, real_middle_size) / max_middle_real_size; |
| 341 | |
| 342 | width_texture = MIN(width_texture, first_section_size + middle_section_size + last_section_size); |
| 343 | } break; |
| 344 | case FILL_MODE_MAX: |
| 345 | break; |
| 346 | default: { |
| 347 | middle_section_size *= MIN(1.0, (MAX(0.0, width_filled - first_section_size) / MAX(1.0, width_total - first_section_size - last_section_size))); |
| 348 | last_section_size = MAX(0.0, last_section_size - (width_total - width_filled)); |
| 349 | first_section_size = MIN(first_section_size, width_filled); |
| 350 | width_texture = MIN(width_texture, first_section_size + middle_section_size + last_section_size); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | switch (p_mode) { |
| 355 | case FILL_LEFT_TO_RIGHT: { |
| 356 | src_rect.size.x = width_texture; |
| 357 | dst_rect.size.x = width_filled; |
| 358 | topleft.x = first_section_size; |
| 359 | bottomright.x = last_section_size; |
| 360 | } break; |
| 361 | case FILL_RIGHT_TO_LEFT: { |
| 362 | src_rect.position.x += src_rect.size.x - width_texture; |
| 363 | src_rect.size.x = width_texture; |
| 364 | dst_rect.position.x += width_total - width_filled; |
| 365 | dst_rect.size.x = width_filled; |
| 366 | topleft.x = last_section_size; |
| 367 | bottomright.x = first_section_size; |
| 368 | } break; |
| 369 | case FILL_TOP_TO_BOTTOM: { |
| 370 | src_rect.size.y = width_texture; |
| 371 | dst_rect.size.y = width_filled; |
| 372 | bottomright.y = last_section_size; |
| 373 | topleft.y = first_section_size; |
| 374 | } break; |
| 375 | case FILL_BOTTOM_TO_TOP: { |
| 376 | src_rect.position.y += src_rect.size.y - width_texture; |
| 377 | src_rect.size.y = width_texture; |
| 378 | dst_rect.position.y += width_total - width_filled; |
| 379 | dst_rect.size.y = width_filled; |
| 380 | topleft.y = last_section_size; |
| 381 | bottomright.y = first_section_size; |
| 382 | } break; |
| 383 | case FILL_BILINEAR_LEFT_AND_RIGHT: { |
| 384 | double center_mapped_from_real_width = (width_total * 0.5 - topleft.x) / max_middle_real_size * max_middle_texture_size + topleft.x; |
| 385 | double drift_from_unscaled_center = 0; |
| 386 | if (bottomright.y != topleft.y) { // To avoid division by zero. |
| 387 | drift_from_unscaled_center = (src_rect.size.x * 0.5 - center_mapped_from_real_width) * (last_section_size - first_section_size) / (bottomright.x - topleft.x); |
| 388 | } |
| 389 | |
| 390 | src_rect.position.x += center_mapped_from_real_width + drift_from_unscaled_center - width_texture * 0.5; |
| 391 | src_rect.size.x = width_texture; |
| 392 | dst_rect.position.x += (width_total - width_filled) * 0.5; |
| 393 | dst_rect.size.x = width_filled; |
| 394 | topleft.x = first_section_size; |
| 395 | bottomright.x = last_section_size; |
| 396 | } break; |
| 397 | case FILL_BILINEAR_TOP_AND_BOTTOM: { |
| 398 | double center_mapped_from_real_width = (width_total * 0.5 - topleft.y) / max_middle_real_size * max_middle_texture_size + topleft.y; |
| 399 | double drift_from_unscaled_center = 0; |
| 400 | if (bottomright.y != topleft.y) { // To avoid division by zero. |
| 401 | drift_from_unscaled_center = (src_rect.size.y * 0.5 - center_mapped_from_real_width) * (last_section_size - first_section_size) / (bottomright.y - topleft.y); |
| 402 | } |
| 403 | |
| 404 | src_rect.position.y += center_mapped_from_real_width + drift_from_unscaled_center - width_texture * 0.5; |
| 405 | src_rect.size.y = width_texture; |
| 406 | dst_rect.position.y += (width_total - width_filled) * 0.5; |
| 407 | dst_rect.size.y = width_filled; |
| 408 | topleft.y = first_section_size; |
| 409 | bottomright.y = last_section_size; |
| 410 | } break; |
| 411 | case FILL_CLOCKWISE: |
| 412 | case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: |
| 413 | case FILL_COUNTER_CLOCKWISE: { |
| 414 | // Those modes are circular, not relevant for nine patch. |
| 415 | } break; |
| 416 | case FILL_MODE_MAX: |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (p_texture == progress) { |
| 422 | dst_rect.position += progress_offset; |
| 423 | } |
| 424 | p_texture->get_rect_region(dst_rect, src_rect, dst_rect, src_rect); |
| 425 | |
| 426 | RID ci = get_canvas_item(); |
| 427 | RS::get_singleton()->canvas_item_add_nine_patch(ci, dst_rect, src_rect, p_texture->get_scaled_rid(), topleft, bottomright, RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, true, p_modulate); |
| 428 | } |
| 429 | |
| 430 | void TextureProgressBar::_notification(int p_what) { |
| 431 | switch (p_what) { |
| 432 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 433 | RID ae = get_accessibility_element(); |
| 434 | ERR_FAIL_COND(ae.is_null()); |
| 435 | |
| 436 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_PROGRESS_INDICATOR); |
| 437 | } break; |
| 438 | |
| 439 | case NOTIFICATION_DRAW: { |
| 440 | if (under.is_valid()) { |
| 441 | if (nine_patch_stretch) { |
| 442 | draw_nine_patch_stretched(under, mode, 1.0, tint_under); |
| 443 | } else { |
| 444 | draw_texture(under, Point2(), tint_under); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | if (progress.is_valid()) { |
| 449 | const bool is_radial_mode = (mode == FILL_CLOCKWISE || mode == FILL_COUNTER_CLOCKWISE || mode == FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE); |
| 450 | |
| 451 | if (nine_patch_stretch && !is_radial_mode) { |
| 452 | draw_nine_patch_stretched(progress, mode, get_as_ratio(), tint_progress); |
| 453 | } else { |
| 454 | Size2 s = progress->get_size(); |
| 455 | switch (mode) { |
| 456 | case FILL_LEFT_TO_RIGHT: { |
| 457 | Rect2 region = Rect2(progress_offset, Size2(s.x * get_as_ratio(), s.y)); |
| 458 | Rect2 source = Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y)); |
| 459 | draw_texture_rect_region(progress, region, source, tint_progress); |
| 460 | } break; |
| 461 | case FILL_RIGHT_TO_LEFT: { |
| 462 | Rect2 region = Rect2(progress_offset + Point2(s.x - s.x * get_as_ratio(), 0), Size2(s.x * get_as_ratio(), s.y)); |
| 463 | Rect2 source = Rect2(Point2(s.x - s.x * get_as_ratio(), 0), Size2(s.x * get_as_ratio(), s.y)); |
| 464 | draw_texture_rect_region(progress, region, source, tint_progress); |
| 465 | } break; |
| 466 | case FILL_TOP_TO_BOTTOM: { |
| 467 | Rect2 region = Rect2(progress_offset + Point2(), Size2(s.x, s.y * get_as_ratio())); |
| 468 | Rect2 source = Rect2(Point2(), Size2(s.x, s.y * get_as_ratio())); |
| 469 | draw_texture_rect_region(progress, region, source, tint_progress); |
| 470 | } break; |
| 471 | case FILL_BOTTOM_TO_TOP: { |
| 472 | Rect2 region = Rect2(progress_offset + Point2(0, s.y - s.y * get_as_ratio()), Size2(s.x, s.y * get_as_ratio())); |
| 473 | Rect2 source = Rect2(Point2(0, s.y - s.y * get_as_ratio()), Size2(s.x, s.y * get_as_ratio())); |
| 474 | draw_texture_rect_region(progress, region, source, tint_progress); |
| 475 | } break; |
| 476 | case FILL_CLOCKWISE: |
| 477 | case FILL_COUNTER_CLOCKWISE: |
| 478 | case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: { |
| 479 | if (nine_patch_stretch) { |
| 480 | s = get_size(); |
| 481 | } |
| 482 | |
| 483 | float val = get_as_ratio() * rad_max_degrees / 360; |
| 484 | if (val == 1) { |
| 485 | Rect2 region = Rect2(progress_offset, s); |
| 486 | Rect2 source = Rect2(Point2(), progress->get_size()); |
| 487 | draw_texture_rect_region(progress, region, source, tint_progress); |
| 488 | } else if (val != 0) { |
| 489 | LocalVector<float> pts; |
| 490 | float direction = mode == FILL_COUNTER_CLOCKWISE ? -1 : 1; |
| 491 | float start; |
| 492 | |
| 493 | if (mode == FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE) { |
| 494 | start = rad_init_angle / 360 - val / 2; |
| 495 | } else { |
| 496 | start = rad_init_angle / 360; |
| 497 | } |
| 498 | |
| 499 | float end = start + direction * val; |
| 500 | float from = MIN(start, end); |
| 501 | float to = MAX(start, end); |
| 502 | pts.push_back(from); |
| 503 | for (float corner = Math::floor(from * 4 + 0.5) * 0.25 + 0.125; corner < to; corner += 0.25) { |
| 504 | pts.push_back(corner); |
| 505 | } |
| 506 | pts.push_back(to); |
| 507 | |
| 508 | Vector<Point2> uvs; |
| 509 | Vector<Point2> points; |
| 510 | for (const float &f : pts) { |
| 511 | Point2 uv = unit_val_to_uv(f); |
| 512 | if (uvs.has(uv)) { |
| 513 | continue; |
| 514 | } |
| 515 | points.push_back(progress_offset + Point2(uv.x * s.x, uv.y * s.y)); |
| 516 | uvs.push_back(uv); |
| 517 | } |
| 518 | |
| 519 | // Filter out an edge case where almost equal `from`, `to` were mapped to the same UV. |
| 520 | if (points.size() >= 2) { |
| 521 | Point2 center_point = get_relative_center(); |
| 522 | points.push_back(progress_offset + s * center_point); |
| 523 | uvs.push_back(center_point); |
| 524 | |
| 525 | Vector<Color> colors; |
| 526 | colors.push_back(tint_progress); |
| 527 | draw_polygon(points, colors, uvs, progress); |
| 528 | } |
| 529 | } |
| 530 | } break; |
| 531 | case FILL_BILINEAR_LEFT_AND_RIGHT: { |
| 532 | Rect2 region = Rect2(progress_offset + Point2(s.x / 2 - s.x * get_as_ratio() / 2, 0), Size2(s.x * get_as_ratio(), s.y)); |
| 533 | Rect2 source = Rect2(Point2(s.x / 2 - s.x * get_as_ratio() / 2, 0), Size2(s.x * get_as_ratio(), s.y)); |
| 534 | draw_texture_rect_region(progress, region, source, tint_progress); |
| 535 | } break; |
| 536 | case FILL_BILINEAR_TOP_AND_BOTTOM: { |
| 537 | Rect2 region = Rect2(progress_offset + Point2(0, s.y / 2 - s.y * get_as_ratio() / 2), Size2(s.x, s.y * get_as_ratio())); |
| 538 | Rect2 source = Rect2(Point2(0, s.y / 2 - s.y * get_as_ratio() / 2), Size2(s.x, s.y * get_as_ratio())); |
| 539 | draw_texture_rect_region(progress, region, source, tint_progress); |
| 540 | } break; |
| 541 | case FILL_MODE_MAX: |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | #ifdef TOOLS_ENABLED |
| 546 | // Draw a reference cross for radial modes. |
| 547 | if (is_radial_mode && is_part_of_edited_scene()) { |
| 548 | Point2 p; |
| 549 | |
| 550 | if (nine_patch_stretch) { |
| 551 | p = get_size(); |
| 552 | } else { |
| 553 | p = progress->get_size(); |
| 554 | } |
| 555 | |
| 556 | p *= get_relative_center(); |
| 557 | p += progress_offset; |
| 558 | draw_line(p - Point2(8, 0), p + Point2(8, 0), Color(0.9, 0.5, 0.5), 2); |
| 559 | draw_line(p - Point2(0, 8), p + Point2(0, 8), Color(0.9, 0.5, 0.5), 2); |
| 560 | } |
| 561 | #endif |
| 562 | } |
| 563 | |
| 564 | if (over.is_valid()) { |
| 565 | if (nine_patch_stretch) { |
| 566 | draw_nine_patch_stretched(over, mode, 1.0, tint_over); |
| 567 | } else { |
| 568 | draw_texture(over, Point2(), tint_over); |
| 569 | } |
| 570 | } |
| 571 | } break; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | void TextureProgressBar::set_fill_mode(int p_fill) { |
| 576 | ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX); |
| 577 | |
| 578 | if (mode == (FillMode)p_fill) { |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | mode = (FillMode)p_fill; |
| 583 | queue_redraw(); |
| 584 | notify_property_list_changed(); |
| 585 | } |
| 586 | |
| 587 | int TextureProgressBar::get_fill_mode() { |
| 588 | return mode; |
| 589 | } |
| 590 | |
| 591 | void TextureProgressBar::set_radial_initial_angle(float p_angle) { |
| 592 | ERR_FAIL_COND_MSG(!Math::is_finite(p_angle), "Angle is non-finite."); |
| 593 | |
| 594 | if (p_angle < 0.0 || p_angle > 360.0) { |
| 595 | p_angle = Math::fposmodp(p_angle, 360.0f); |
| 596 | } |
| 597 | |
| 598 | if (rad_init_angle == p_angle) { |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | rad_init_angle = p_angle; |
| 603 | queue_redraw(); |
| 604 | } |
| 605 | |
| 606 | float TextureProgressBar::get_radial_initial_angle() { |
| 607 | return rad_init_angle; |
| 608 | } |
| 609 | |
| 610 | void TextureProgressBar::set_fill_degrees(float p_angle) { |
| 611 | float angle_clamped = CLAMP(p_angle, 0, 360); |
| 612 | |
| 613 | if (rad_max_degrees == angle_clamped) { |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | rad_max_degrees = angle_clamped; |
| 618 | queue_redraw(); |
| 619 | } |
| 620 | |
| 621 | float TextureProgressBar::get_fill_degrees() { |
| 622 | return rad_max_degrees; |
| 623 | } |
| 624 | |
| 625 | void TextureProgressBar::set_radial_center_offset(const Point2 &p_off) { |
| 626 | if (rad_center_off == p_off) { |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | rad_center_off = p_off; |
| 631 | queue_redraw(); |
| 632 | } |
| 633 | |
| 634 | Point2 TextureProgressBar::get_radial_center_offset() { |
| 635 | return rad_center_off; |
| 636 | } |
| 637 | |
| 638 | void TextureProgressBar::_validate_property(PropertyInfo &p_property) const { |
| 639 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 640 | return; |
| 641 | } |
| 642 | if (p_property.name.begins_with("stretch_margin_") && !nine_patch_stretch) { |
| 643 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 644 | } |
| 645 | |
| 646 | if (p_property.name.begins_with("radial_") && (mode != FillMode::FILL_CLOCKWISE && mode != FillMode::FILL_COUNTER_CLOCKWISE && mode != FillMode::FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE)) { |
| 647 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | void TextureProgressBar::_bind_methods() { |
| 652 | ClassDB::bind_method(D_METHOD("set_under_texture", "tex"), &TextureProgressBar::set_under_texture); |
| 653 | ClassDB::bind_method(D_METHOD("get_under_texture"), &TextureProgressBar::get_under_texture); |
| 654 | |
| 655 | ClassDB::bind_method(D_METHOD("set_progress_texture", "tex"), &TextureProgressBar::set_progress_texture); |
| 656 | ClassDB::bind_method(D_METHOD("get_progress_texture"), &TextureProgressBar::get_progress_texture); |
| 657 | |
| 658 | ClassDB::bind_method(D_METHOD("set_over_texture", "tex"), &TextureProgressBar::set_over_texture); |
| 659 | ClassDB::bind_method(D_METHOD("get_over_texture"), &TextureProgressBar::get_over_texture); |
| 660 | |
| 661 | ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &TextureProgressBar::set_fill_mode); |
| 662 | ClassDB::bind_method(D_METHOD("get_fill_mode"), &TextureProgressBar::get_fill_mode); |
| 663 | |
| 664 | ClassDB::bind_method(D_METHOD("set_tint_under", "tint"), &TextureProgressBar::set_tint_under); |
| 665 | ClassDB::bind_method(D_METHOD("get_tint_under"), &TextureProgressBar::get_tint_under); |
| 666 | |
| 667 | ClassDB::bind_method(D_METHOD("set_tint_progress", "tint"), &TextureProgressBar::set_tint_progress); |
| 668 | ClassDB::bind_method(D_METHOD("get_tint_progress"), &TextureProgressBar::get_tint_progress); |
| 669 | |
| 670 | ClassDB::bind_method(D_METHOD("set_tint_over", "tint"), &TextureProgressBar::set_tint_over); |
| 671 | ClassDB::bind_method(D_METHOD("get_tint_over"), &TextureProgressBar::get_tint_over); |
| 672 | |
| 673 | ClassDB::bind_method(D_METHOD("set_texture_progress_offset", "offset"), &TextureProgressBar::set_progress_offset); |
| 674 | ClassDB::bind_method(D_METHOD("get_texture_progress_offset"), &TextureProgressBar::get_progress_offset); |
| 675 | |
| 676 | ClassDB::bind_method(D_METHOD("set_radial_initial_angle", "mode"), &TextureProgressBar::set_radial_initial_angle); |
| 677 | ClassDB::bind_method(D_METHOD("get_radial_initial_angle"), &TextureProgressBar::get_radial_initial_angle); |
| 678 | |
| 679 | ClassDB::bind_method(D_METHOD("set_radial_center_offset", "mode"), &TextureProgressBar::set_radial_center_offset); |
| 680 | ClassDB::bind_method(D_METHOD("get_radial_center_offset"), &TextureProgressBar::get_radial_center_offset); |
| 681 | |
| 682 | ClassDB::bind_method(D_METHOD("set_fill_degrees", "mode"), &TextureProgressBar::set_fill_degrees); |
| 683 | ClassDB::bind_method(D_METHOD("get_fill_degrees"), &TextureProgressBar::get_fill_degrees); |
| 684 | |
| 685 | ClassDB::bind_method(D_METHOD("set_stretch_margin", "margin", "value"), &TextureProgressBar::set_stretch_margin); |
| 686 | ClassDB::bind_method(D_METHOD("get_stretch_margin", "margin"), &TextureProgressBar::get_stretch_margin); |
| 687 | |
| 688 | ClassDB::bind_method(D_METHOD("set_nine_patch_stretch", "stretch"), &TextureProgressBar::set_nine_patch_stretch); |
| 689 | ClassDB::bind_method(D_METHOD("get_nine_patch_stretch"), &TextureProgressBar::get_nine_patch_stretch); |
| 690 | |
| 691 | ADD_PROPERTY(PropertyInfo(Variant::INT, "fill_mode", PROPERTY_HINT_ENUM, "Left to Right,Right to Left,Top to Bottom,Bottom to Top,Clockwise,Counter Clockwise,Bilinear (Left and Right),Bilinear (Top and Bottom),Clockwise and Counter Clockwise"), "set_fill_mode", "get_fill_mode"); |
| 692 | ADD_GROUP("Radial Fill", "radial_"); |
| 693 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radial_initial_angle", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,degrees"), "set_radial_initial_angle", "get_radial_initial_angle"); |
| 694 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radial_fill_degrees", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,degrees"), "set_fill_degrees", "get_fill_degrees"); |
| 695 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "radial_center_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_radial_center_offset", "get_radial_center_offset"); |
| 696 | |
| 697 | ADD_GROUP("", ""); |
| 698 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "nine_patch_stretch"), "set_nine_patch_stretch", "get_nine_patch_stretch"); |
| 699 | ADD_GROUP("Stretch Margin", "stretch_margin_"); |
| 700 | ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_stretch_margin", "get_stretch_margin", SIDE_LEFT); |
| 701 | ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_stretch_margin", "get_stretch_margin", SIDE_TOP); |
| 702 | ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_stretch_margin", "get_stretch_margin", SIDE_RIGHT); |
| 703 | ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_stretch_margin", "get_stretch_margin", SIDE_BOTTOM); |
| 704 | |
| 705 | ADD_GROUP("Textures", "texture_"); |
| 706 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_under", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_under_texture", "get_under_texture"); |
| 707 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_over", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_over_texture", "get_over_texture"); |
| 708 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_progress", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_progress_texture", "get_progress_texture"); |
| 709 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_progress_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_progress_offset", "get_texture_progress_offset"); |
| 710 | |
| 711 | ADD_GROUP("Tint", "tint_"); |
| 712 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_under"), "set_tint_under", "get_tint_under"); |
| 713 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_over"), "set_tint_over", "get_tint_over"); |
| 714 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_progress"), "set_tint_progress", "get_tint_progress"); |
| 715 | |
| 716 | BIND_ENUM_CONSTANT(FILL_LEFT_TO_RIGHT); |
| 717 | BIND_ENUM_CONSTANT(FILL_RIGHT_TO_LEFT); |
| 718 | BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM); |
| 719 | BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP); |
| 720 | BIND_ENUM_CONSTANT(FILL_CLOCKWISE); |
| 721 | BIND_ENUM_CONSTANT(FILL_COUNTER_CLOCKWISE); |
| 722 | BIND_ENUM_CONSTANT(FILL_BILINEAR_LEFT_AND_RIGHT); |
| 723 | BIND_ENUM_CONSTANT(FILL_BILINEAR_TOP_AND_BOTTOM); |
| 724 | BIND_ENUM_CONSTANT(FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE); |
| 725 | } |
| 726 | |
| 727 | TextureProgressBar::TextureProgressBar() { |
| 728 | set_mouse_filter(MOUSE_FILTER_PASS); |
| 729 | } |