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