renderer_canvas_render.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**************************************************************************/
  2. /* renderer_canvas_render.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. #include "renderer_canvas_render.h"
  31. #include "servers/rendering/rendering_server_globals.h"
  32. const Rect2 &RendererCanvasRender::Item::get_rect() const {
  33. if (custom_rect || (!rect_dirty && !update_when_visible && skeleton == RID())) {
  34. return rect;
  35. }
  36. //must update rect
  37. if (commands == nullptr) {
  38. rect = Rect2();
  39. rect_dirty = false;
  40. return rect;
  41. }
  42. Transform2D xf;
  43. bool found_xform = false;
  44. bool first = true;
  45. const Item::Command *c = commands;
  46. while (c) {
  47. Rect2 r;
  48. switch (c->type) {
  49. case Item::Command::TYPE_RECT: {
  50. const Item::CommandRect *crect = static_cast<const Item::CommandRect *>(c);
  51. r = crect->rect;
  52. } break;
  53. case Item::Command::TYPE_NINEPATCH: {
  54. const Item::CommandNinePatch *style = static_cast<const Item::CommandNinePatch *>(c);
  55. r = style->rect;
  56. } break;
  57. case Item::Command::TYPE_POLYGON: {
  58. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
  59. r = polygon->polygon.rect_cache;
  60. } break;
  61. case Item::Command::TYPE_PRIMITIVE: {
  62. const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
  63. for (uint32_t j = 0; j < primitive->point_count; j++) {
  64. if (j == 0) {
  65. r.position = primitive->points[0];
  66. } else {
  67. r.expand_to(primitive->points[j]);
  68. }
  69. }
  70. } break;
  71. case Item::Command::TYPE_MESH: {
  72. const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);
  73. AABB aabb = RSG::mesh_storage->mesh_get_aabb(mesh->mesh, skeleton);
  74. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  75. } break;
  76. case Item::Command::TYPE_MULTIMESH: {
  77. const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c);
  78. AABB aabb = RSG::mesh_storage->multimesh_get_aabb(multimesh->multimesh);
  79. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  80. } break;
  81. case Item::Command::TYPE_PARTICLES: {
  82. const Item::CommandParticles *particles_cmd = static_cast<const Item::CommandParticles *>(c);
  83. if (particles_cmd->particles.is_valid()) {
  84. AABB aabb = RSG::particles_storage->particles_get_aabb(particles_cmd->particles);
  85. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  86. }
  87. } break;
  88. case Item::Command::TYPE_TRANSFORM: {
  89. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
  90. xf = transform->xform;
  91. found_xform = true;
  92. [[fallthrough]];
  93. }
  94. default: {
  95. c = c->next;
  96. continue;
  97. }
  98. }
  99. if (found_xform) {
  100. r = xf.xform(r);
  101. }
  102. if (first) {
  103. rect = r;
  104. first = false;
  105. } else {
  106. rect = rect.merge(r);
  107. }
  108. c = c->next;
  109. }
  110. rect_dirty = false;
  111. return rect;
  112. }
  113. RendererCanvasRender::Item::CommandMesh::~CommandMesh() {
  114. if (mesh_instance.is_valid()) {
  115. RSG::mesh_storage->mesh_instance_free(mesh_instance);
  116. }
  117. }