| 1 | /**************************************************************************/ |
| 2 | /* polygon_2d.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 "polygon_2d.h" |
| 32 | |
| 33 | #include "core/math/geometry_2d.h" |
| 34 | #include "skeleton_2d.h" |
| 35 | |
| 36 | #ifndef NAVIGATION_2D_DISABLED |
| 37 | #include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h" |
| 38 | #include "scene/resources/2d/navigation_polygon.h" |
| 39 | #include "servers/navigation_2d/navigation_server_2d.h" |
| 40 | |
| 41 | Callable Polygon2D::_navmesh_source_geometry_parsing_callback; |
| 42 | RID Polygon2D::_navmesh_source_geometry_parser; |
| 43 | #endif // NAVIGATION_2D_DISABLED |
| 44 | |
| 45 | #ifdef TOOLS_ENABLED |
| 46 | Dictionary Polygon2D::_edit_get_state() const { |
| 47 | Dictionary state = Node2D::_edit_get_state(); |
| 48 | state["offset"] = offset; |
| 49 | return state; |
| 50 | } |
| 51 | |
| 52 | void Polygon2D::_edit_set_state(const Dictionary &p_state) { |
| 53 | Node2D::_edit_set_state(p_state); |
| 54 | set_offset(p_state["offset"]); |
| 55 | } |
| 56 | |
| 57 | void Polygon2D::_edit_set_pivot(const Point2 &p_pivot) { |
| 58 | set_position(get_transform().xform(p_pivot)); |
| 59 | set_offset(get_offset() - p_pivot); |
| 60 | } |
| 61 | |
| 62 | Point2 Polygon2D::_edit_get_pivot() const { |
| 63 | return Vector2(); |
| 64 | } |
| 65 | |
| 66 | bool Polygon2D::_edit_use_pivot() const { |
| 67 | return true; |
| 68 | } |
| 69 | #endif // TOOLS_ENABLED |
| 70 | |
| 71 | #ifdef DEBUG_ENABLED |
| 72 | Rect2 Polygon2D::_edit_get_rect() const { |
| 73 | if (rect_cache_dirty) { |
| 74 | int l = polygon.size(); |
| 75 | const Vector2 *r = polygon.ptr(); |
| 76 | item_rect = Rect2(); |
| 77 | for (int i = 0; i < l; i++) { |
| 78 | Vector2 pos = r[i] + offset; |
| 79 | if (i == 0) { |
| 80 | item_rect.position = pos; |
| 81 | } else { |
| 82 | item_rect.expand_to(pos); |
| 83 | } |
| 84 | } |
| 85 | rect_cache_dirty = false; |
| 86 | } |
| 87 | |
| 88 | return item_rect; |
| 89 | } |
| 90 | |
| 91 | bool Polygon2D::_edit_use_rect() const { |
| 92 | return polygon.size() > 0; |
| 93 | } |
| 94 | |
| 95 | bool Polygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { |
| 96 | Vector<Vector2> polygon2d = Variant(polygon); |
| 97 | if (internal_vertices > 0) { |
| 98 | polygon2d.resize(MAX(polygon2d.size() - internal_vertices, 0)); |
| 99 | } |
| 100 | return Geometry2D::is_point_in_polygon(p_point - get_offset(), polygon2d); |
| 101 | } |
| 102 | #endif // DEBUG_ENABLED |
| 103 | |
| 104 | void Polygon2D::_skeleton_bone_setup_changed() { |
| 105 | queue_redraw(); |
| 106 | } |
| 107 | |
| 108 | void Polygon2D::_notification(int p_what) { |
| 109 | if (p_what == NOTIFICATION_TRANSFORM_CHANGED && !Engine::get_singleton()->is_editor_hint()) { |
| 110 | return; // Mesh recreation for NOTIFICATION_TRANSFORM_CHANGED is only needed in editor. |
| 111 | } |
| 112 | |
| 113 | switch (p_what) { |
| 114 | case NOTIFICATION_TRANSFORM_CHANGED: |
| 115 | case NOTIFICATION_DRAW: { |
| 116 | if (polygon.size() < 3) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | Skeleton2D *skeleton_node = nullptr; |
| 121 | if (has_node(skeleton)) { |
| 122 | skeleton_node = Object::cast_to<Skeleton2D>(get_node(skeleton)); |
| 123 | } |
| 124 | |
| 125 | ObjectID new_skeleton_id; |
| 126 | |
| 127 | if (skeleton_node && !invert && bone_weights.size()) { |
| 128 | RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), skeleton_node->get_skeleton()); |
| 129 | new_skeleton_id = skeleton_node->get_instance_id(); |
| 130 | } else { |
| 131 | RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID()); |
| 132 | } |
| 133 | |
| 134 | if (new_skeleton_id != current_skeleton_id) { |
| 135 | Object *old_skeleton = ObjectDB::get_instance(current_skeleton_id); |
| 136 | if (old_skeleton) { |
| 137 | old_skeleton->disconnect("bone_setup_changed", callable_mp(this, &Polygon2D::_skeleton_bone_setup_changed)); |
| 138 | } |
| 139 | |
| 140 | if (skeleton_node) { |
| 141 | skeleton_node->connect("bone_setup_changed", callable_mp(this, &Polygon2D::_skeleton_bone_setup_changed)); |
| 142 | } |
| 143 | |
| 144 | current_skeleton_id = new_skeleton_id; |
| 145 | } |
| 146 | |
| 147 | Vector<Vector2> points; |
| 148 | Vector<Vector2> uvs; |
| 149 | Vector<int> bones; |
| 150 | Vector<float> weights; |
| 151 | |
| 152 | int len = polygon.size(); |
| 153 | if ((invert || polygons.is_empty()) && internal_vertices > 0) { |
| 154 | //if no polygons are around, internal vertices must not be drawn, else let them be |
| 155 | len -= internal_vertices; |
| 156 | } |
| 157 | |
| 158 | if (len <= 0) { |
| 159 | return; |
| 160 | } |
| 161 | points.resize(len); |
| 162 | |
| 163 | { |
| 164 | const Vector2 *polyr = polygon.ptr(); |
| 165 | for (int i = 0; i < len; i++) { |
| 166 | points.write[i] = polyr[i] + offset; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (invert) { |
| 171 | Rect2 bounds; |
| 172 | int highest_idx = -1; |
| 173 | real_t highest_y = -1e20; |
| 174 | real_t sum = 0.0; |
| 175 | |
| 176 | for (int i = 0; i < len; i++) { |
| 177 | if (i == 0) { |
| 178 | bounds.position = points[i]; |
| 179 | } else { |
| 180 | bounds.expand_to(points[i]); |
| 181 | } |
| 182 | if (points[i].y > highest_y) { |
| 183 | highest_idx = i; |
| 184 | highest_y = points[i].y; |
| 185 | } |
| 186 | int ni = (i + 1) % len; |
| 187 | sum += (points[ni].x - points[i].x) * (points[ni].y + points[i].y); |
| 188 | } |
| 189 | |
| 190 | bounds = bounds.grow(invert_border); |
| 191 | |
| 192 | Vector2 ep[7] = { |
| 193 | Vector2(points[highest_idx].x, points[highest_idx].y + invert_border), |
| 194 | Vector2(bounds.position + bounds.size), |
| 195 | Vector2(bounds.position + Vector2(bounds.size.x, 0)), |
| 196 | Vector2(bounds.position), |
| 197 | Vector2(bounds.position + Vector2(0, bounds.size.y)), |
| 198 | Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y + invert_border), |
| 199 | Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y), |
| 200 | }; |
| 201 | |
| 202 | if (sum > 0) { |
| 203 | SWAP(ep[1], ep[4]); |
| 204 | SWAP(ep[2], ep[3]); |
| 205 | SWAP(ep[5], ep[0]); |
| 206 | SWAP(ep[6], points.write[highest_idx]); |
| 207 | } |
| 208 | |
| 209 | points.resize(points.size() + 7); |
| 210 | for (int i = points.size() - 1; i >= highest_idx + 7; i--) { |
| 211 | points.write[i] = points[i - 7]; |
| 212 | } |
| 213 | |
| 214 | for (int i = 0; i < 7; i++) { |
| 215 | points.write[highest_idx + i + 1] = ep[i]; |
| 216 | } |
| 217 | |
| 218 | len = points.size(); |
| 219 | } |
| 220 | |
| 221 | if (texture.is_valid()) { |
| 222 | Transform2D texmat(tex_rot, tex_ofs); |
| 223 | texmat.scale(tex_scale); |
| 224 | Size2 tex_size = texture->get_size(); |
| 225 | |
| 226 | uvs.resize(len); |
| 227 | |
| 228 | if (points.size() == uv.size()) { |
| 229 | const Vector2 *uvr = uv.ptr(); |
| 230 | |
| 231 | for (int i = 0; i < len; i++) { |
| 232 | uvs.write[i] = texmat.xform(uvr[i]) / tex_size; |
| 233 | } |
| 234 | |
| 235 | } else { |
| 236 | for (int i = 0; i < len; i++) { |
| 237 | uvs.write[i] = texmat.xform(points[i]) / tex_size; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (skeleton_node && !invert && bone_weights.size()) { |
| 243 | //a skeleton is set! fill indices and weights |
| 244 | int vc = len; |
| 245 | bones.resize(vc * 4); |
| 246 | weights.resize(vc * 4); |
| 247 | |
| 248 | int *bonesw = bones.ptrw(); |
| 249 | float *weightsw = weights.ptrw(); |
| 250 | |
| 251 | for (int i = 0; i < vc * 4; i++) { |
| 252 | bonesw[i] = 0; |
| 253 | weightsw[i] = 0; |
| 254 | } |
| 255 | |
| 256 | for (int i = 0; i < bone_weights.size(); i++) { |
| 257 | if (bone_weights[i].weights.size() != points.size()) { |
| 258 | continue; //different number of vertices, sorry not using. |
| 259 | } |
| 260 | if (!skeleton_node->has_node(bone_weights[i].path)) { |
| 261 | continue; //node does not exist |
| 262 | } |
| 263 | Bone2D *bone = Object::cast_to<Bone2D>(skeleton_node->get_node(bone_weights[i].path)); |
| 264 | if (!bone) { |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | int bone_index = bone->get_index_in_skeleton(); |
| 269 | const float *r = bone_weights[i].weights.ptr(); |
| 270 | for (int j = 0; j < vc; j++) { |
| 271 | if (r[j] == 0.0) { |
| 272 | continue; //weight is unpainted, skip |
| 273 | } |
| 274 | //find an index with a weight |
| 275 | for (int k = 0; k < 4; k++) { |
| 276 | if (weightsw[j * 4 + k] < r[j]) { |
| 277 | //this is less than this weight, insert weight! |
| 278 | for (int l = 3; l > k; l--) { |
| 279 | weightsw[j * 4 + l] = weightsw[j * 4 + l - 1]; |
| 280 | bonesw[j * 4 + l] = bonesw[j * 4 + l - 1]; |
| 281 | } |
| 282 | weightsw[j * 4 + k] = r[j]; |
| 283 | bonesw[j * 4 + k] = bone_index; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | //normalize the weights |
| 291 | for (int i = 0; i < vc; i++) { |
| 292 | real_t tw = 0.0; |
| 293 | for (int j = 0; j < 4; j++) { |
| 294 | tw += weightsw[i * 4 + j]; |
| 295 | } |
| 296 | if (tw == 0) { |
| 297 | continue; //unpainted, do nothing |
| 298 | } |
| 299 | |
| 300 | //normalize |
| 301 | for (int j = 0; j < 4; j++) { |
| 302 | weightsw[i * 4 + j] /= tw; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | Vector<Color> colors; |
| 308 | colors.resize(len); |
| 309 | |
| 310 | if (vertex_colors.size() == points.size()) { |
| 311 | const Color *color_r = vertex_colors.ptr(); |
| 312 | for (int i = 0; i < len; i++) { |
| 313 | colors.write[i] = color_r[i]; |
| 314 | } |
| 315 | } else { |
| 316 | for (int i = 0; i < len; i++) { |
| 317 | colors.write[i] = color; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | Vector<int> index_array; |
| 322 | |
| 323 | if (invert || polygons.is_empty()) { |
| 324 | index_array = Geometry2D::triangulate_polygon(points); |
| 325 | } else { |
| 326 | //draw individual polygons |
| 327 | for (int i = 0; i < polygons.size(); i++) { |
| 328 | Vector<int> src_indices = polygons[i]; |
| 329 | int ic = src_indices.size(); |
| 330 | if (ic < 3) { |
| 331 | continue; |
| 332 | } |
| 333 | const int *r = src_indices.ptr(); |
| 334 | |
| 335 | Vector<Vector2> tmp_points; |
| 336 | tmp_points.resize(ic); |
| 337 | |
| 338 | for (int j = 0; j < ic; j++) { |
| 339 | int idx = r[j]; |
| 340 | ERR_CONTINUE(idx < 0 || idx >= points.size()); |
| 341 | tmp_points.write[j] = points[r[j]]; |
| 342 | } |
| 343 | Vector<int> indices = Geometry2D::triangulate_polygon(tmp_points); |
| 344 | int ic2 = indices.size(); |
| 345 | const int *r2 = indices.ptr(); |
| 346 | |
| 347 | int bic = index_array.size(); |
| 348 | index_array.resize(bic + ic2); |
| 349 | int *w2 = index_array.ptrw(); |
| 350 | |
| 351 | for (int j = 0; j < ic2; j++) { |
| 352 | w2[j + bic] = r[r2[j]]; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | RS::get_singleton()->mesh_clear(mesh); |
| 358 | |
| 359 | if (index_array.size()) { |
| 360 | Array arr; |
| 361 | arr.resize(RS::ARRAY_MAX); |
| 362 | arr[RS::ARRAY_VERTEX] = points; |
| 363 | if (uvs.size() == points.size()) { |
| 364 | arr[RS::ARRAY_TEX_UV] = uvs; |
| 365 | } |
| 366 | if (colors.size() == points.size()) { |
| 367 | arr[RS::ARRAY_COLOR] = colors; |
| 368 | } |
| 369 | |
| 370 | if (bones.size() == points.size() * 4) { |
| 371 | arr[RS::ARRAY_BONES] = bones; |
| 372 | arr[RS::ARRAY_WEIGHTS] = weights; |
| 373 | } |
| 374 | |
| 375 | arr[RS::ARRAY_INDEX] = index_array; |
| 376 | |
| 377 | RS::SurfaceData sd; |
| 378 | |
| 379 | if (skeleton_node) { |
| 380 | // Compute transform between mesh and skeleton for runtime AABB compute. |
| 381 | const Transform2D mesh_transform = get_global_transform(); |
| 382 | const Transform2D skeleton_transform = skeleton_node->get_global_transform(); |
| 383 | const Transform2D mesh_to_sk2d = skeleton_transform.affine_inverse() * mesh_transform; |
| 384 | |
| 385 | // Convert 2d transform to 3d. |
| 386 | sd.mesh_to_skeleton_xform.basis.rows[0][0] = mesh_to_sk2d.columns[0][0]; |
| 387 | sd.mesh_to_skeleton_xform.basis.rows[1][0] = mesh_to_sk2d.columns[0][1]; |
| 388 | sd.mesh_to_skeleton_xform.origin.x = mesh_to_sk2d.get_origin().x; |
| 389 | |
| 390 | sd.mesh_to_skeleton_xform.basis.rows[0][1] = mesh_to_sk2d.columns[1][0]; |
| 391 | sd.mesh_to_skeleton_xform.basis.rows[1][1] = mesh_to_sk2d.columns[1][1]; |
| 392 | sd.mesh_to_skeleton_xform.origin.y = mesh_to_sk2d.get_origin().y; |
| 393 | } |
| 394 | |
| 395 | Error err = RS::get_singleton()->mesh_create_surface_data_from_arrays(&sd, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES); |
| 396 | if (err != OK) { |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | RS::get_singleton()->mesh_add_surface(mesh, sd); |
| 401 | RS::get_singleton()->canvas_item_add_mesh(get_canvas_item(), mesh, Transform2D(), Color(1, 1, 1), texture.is_valid() ? texture->get_scaled_rid() : RID()); |
| 402 | } |
| 403 | |
| 404 | } break; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void Polygon2D::set_polygon(const Vector<Vector2> &p_polygon) { |
| 409 | polygon = p_polygon; |
| 410 | rect_cache_dirty = true; |
| 411 | queue_redraw(); |
| 412 | } |
| 413 | |
| 414 | Vector<Vector2> Polygon2D::get_polygon() const { |
| 415 | return polygon; |
| 416 | } |
| 417 | |
| 418 | void Polygon2D::set_internal_vertex_count(int p_count) { |
| 419 | internal_vertices = p_count; |
| 420 | } |
| 421 | |
| 422 | int Polygon2D::get_internal_vertex_count() const { |
| 423 | return internal_vertices; |
| 424 | } |
| 425 | |
| 426 | void Polygon2D::set_uv(const Vector<Vector2> &p_uv) { |
| 427 | uv = p_uv; |
| 428 | queue_redraw(); |
| 429 | } |
| 430 | |
| 431 | Vector<Vector2> Polygon2D::get_uv() const { |
| 432 | return uv; |
| 433 | } |
| 434 | |
| 435 | void Polygon2D::set_polygons(const Array &p_polygons) { |
| 436 | polygons = p_polygons; |
| 437 | queue_redraw(); |
| 438 | } |
| 439 | |
| 440 | Array Polygon2D::get_polygons() const { |
| 441 | return polygons; |
| 442 | } |
| 443 | |
| 444 | void Polygon2D::set_color(const Color &p_color) { |
| 445 | color = p_color; |
| 446 | queue_redraw(); |
| 447 | } |
| 448 | |
| 449 | Color Polygon2D::get_color() const { |
| 450 | return color; |
| 451 | } |
| 452 | |
| 453 | void Polygon2D::set_vertex_colors(const Vector<Color> &p_colors) { |
| 454 | vertex_colors = p_colors; |
| 455 | queue_redraw(); |
| 456 | } |
| 457 | |
| 458 | Vector<Color> Polygon2D::get_vertex_colors() const { |
| 459 | return vertex_colors; |
| 460 | } |
| 461 | |
| 462 | void Polygon2D::set_texture(const Ref<Texture2D> &p_texture) { |
| 463 | texture = p_texture; |
| 464 | queue_redraw(); |
| 465 | } |
| 466 | |
| 467 | Ref<Texture2D> Polygon2D::get_texture() const { |
| 468 | return texture; |
| 469 | } |
| 470 | |
| 471 | void Polygon2D::set_texture_offset(const Vector2 &p_offset) { |
| 472 | tex_ofs = p_offset; |
| 473 | queue_redraw(); |
| 474 | } |
| 475 | |
| 476 | Vector2 Polygon2D::get_texture_offset() const { |
| 477 | return tex_ofs; |
| 478 | } |
| 479 | |
| 480 | void Polygon2D::set_texture_rotation(real_t p_rot) { |
| 481 | tex_rot = p_rot; |
| 482 | queue_redraw(); |
| 483 | } |
| 484 | |
| 485 | real_t Polygon2D::get_texture_rotation() const { |
| 486 | return tex_rot; |
| 487 | } |
| 488 | |
| 489 | void Polygon2D::set_texture_scale(const Size2 &p_scale) { |
| 490 | tex_scale = p_scale; |
| 491 | queue_redraw(); |
| 492 | } |
| 493 | |
| 494 | Size2 Polygon2D::get_texture_scale() const { |
| 495 | return tex_scale; |
| 496 | } |
| 497 | |
| 498 | void Polygon2D::set_invert(bool p_invert) { |
| 499 | invert = p_invert; |
| 500 | queue_redraw(); |
| 501 | } |
| 502 | |
| 503 | bool Polygon2D::get_invert() const { |
| 504 | return invert; |
| 505 | } |
| 506 | |
| 507 | void Polygon2D::set_antialiased(bool p_antialiased) { |
| 508 | antialiased = p_antialiased; |
| 509 | queue_redraw(); |
| 510 | } |
| 511 | |
| 512 | bool Polygon2D::get_antialiased() const { |
| 513 | return antialiased; |
| 514 | } |
| 515 | |
| 516 | void Polygon2D::set_invert_border(real_t p_invert_border) { |
| 517 | invert_border = p_invert_border; |
| 518 | queue_redraw(); |
| 519 | } |
| 520 | |
| 521 | real_t Polygon2D::get_invert_border() const { |
| 522 | return invert_border; |
| 523 | } |
| 524 | |
| 525 | void Polygon2D::set_offset(const Vector2 &p_offset) { |
| 526 | offset = p_offset; |
| 527 | rect_cache_dirty = true; |
| 528 | queue_redraw(); |
| 529 | } |
| 530 | |
| 531 | Vector2 Polygon2D::get_offset() const { |
| 532 | return offset; |
| 533 | } |
| 534 | |
| 535 | void Polygon2D::add_bone(const NodePath &p_path, const Vector<float> &p_weights) { |
| 536 | Bone bone; |
| 537 | bone.path = p_path; |
| 538 | bone.weights = p_weights; |
| 539 | bone_weights.push_back(bone); |
| 540 | } |
| 541 | |
| 542 | int Polygon2D::get_bone_count() const { |
| 543 | return bone_weights.size(); |
| 544 | } |
| 545 | |
| 546 | NodePath Polygon2D::get_bone_path(int p_index) const { |
| 547 | ERR_FAIL_INDEX_V(p_index, bone_weights.size(), NodePath()); |
| 548 | return bone_weights[p_index].path; |
| 549 | } |
| 550 | |
| 551 | Vector<float> Polygon2D::get_bone_weights(int p_index) const { |
| 552 | ERR_FAIL_INDEX_V(p_index, bone_weights.size(), Vector<float>()); |
| 553 | return bone_weights[p_index].weights; |
| 554 | } |
| 555 | |
| 556 | void Polygon2D::erase_bone(int p_idx) { |
| 557 | ERR_FAIL_INDEX(p_idx, bone_weights.size()); |
| 558 | bone_weights.remove_at(p_idx); |
| 559 | } |
| 560 | |
| 561 | void Polygon2D::clear_bones() { |
| 562 | bone_weights.clear(); |
| 563 | } |
| 564 | |
| 565 | void Polygon2D::set_bone_weights(int p_index, const Vector<float> &p_weights) { |
| 566 | ERR_FAIL_INDEX(p_index, bone_weights.size()); |
| 567 | bone_weights.write[p_index].weights = p_weights; |
| 568 | queue_redraw(); |
| 569 | } |
| 570 | |
| 571 | void Polygon2D::set_bone_path(int p_index, const NodePath &p_path) { |
| 572 | ERR_FAIL_INDEX(p_index, bone_weights.size()); |
| 573 | bone_weights.write[p_index].path = p_path; |
| 574 | queue_redraw(); |
| 575 | } |
| 576 | |
| 577 | Array Polygon2D::_get_bones() const { |
| 578 | Array bones; |
| 579 | for (int i = 0; i < get_bone_count(); i++) { |
| 580 | // Convert path property to String to avoid errors due to invalid node path in editor, |
| 581 | // because it's relative to the Skeleton2D node and not Polygon2D. |
| 582 | bones.push_back(String(get_bone_path(i))); |
| 583 | bones.push_back(get_bone_weights(i)); |
| 584 | } |
| 585 | return bones; |
| 586 | } |
| 587 | |
| 588 | void Polygon2D::_set_bones(const Array &p_bones) { |
| 589 | ERR_FAIL_COND(p_bones.size() & 1); |
| 590 | clear_bones(); |
| 591 | for (int i = 0; i < p_bones.size(); i += 2) { |
| 592 | // Convert back from String to NodePath. |
| 593 | add_bone(NodePath(p_bones[i]), p_bones[i + 1]); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | void Polygon2D::set_skeleton(const NodePath &p_skeleton) { |
| 598 | if (skeleton == p_skeleton) { |
| 599 | return; |
| 600 | } |
| 601 | skeleton = p_skeleton; |
| 602 | queue_redraw(); |
| 603 | } |
| 604 | |
| 605 | NodePath Polygon2D::get_skeleton() const { |
| 606 | return skeleton; |
| 607 | } |
| 608 | |
| 609 | #ifndef NAVIGATION_2D_DISABLED |
| 610 | void Polygon2D::navmesh_parse_init() { |
| 611 | ERR_FAIL_NULL(NavigationServer2D::get_singleton()); |
| 612 | if (!_navmesh_source_geometry_parser.is_valid()) { |
| 613 | _navmesh_source_geometry_parsing_callback = callable_mp_static(&Polygon2D::navmesh_parse_source_geometry); |
| 614 | _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create(); |
| 615 | NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | void Polygon2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) { |
| 620 | Polygon2D *polygon_2d = Object::cast_to<Polygon2D>(p_node); |
| 621 | |
| 622 | if (polygon_2d == nullptr) { |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type(); |
| 627 | |
| 628 | if (parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH) { |
| 629 | const Transform2D polygon_2d_xform = p_source_geometry_data->root_node_transform * polygon_2d->get_global_transform(); |
| 630 | |
| 631 | Vector<Vector2> shape_outline = polygon_2d->get_polygon(); |
| 632 | for (int i = 0; i < shape_outline.size(); i++) { |
| 633 | shape_outline.write[i] = polygon_2d_xform.xform(shape_outline[i]); |
| 634 | } |
| 635 | |
| 636 | p_source_geometry_data->add_obstruction_outline(shape_outline); |
| 637 | } |
| 638 | } |
| 639 | #endif // NAVIGATION_2D_DISABLED |
| 640 | |
| 641 | void Polygon2D::_bind_methods() { |
| 642 | ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &Polygon2D::set_polygon); |
| 643 | ClassDB::bind_method(D_METHOD("get_polygon"), &Polygon2D::get_polygon); |
| 644 | |
| 645 | ClassDB::bind_method(D_METHOD("set_uv", "uv"), &Polygon2D::set_uv); |
| 646 | ClassDB::bind_method(D_METHOD("get_uv"), &Polygon2D::get_uv); |
| 647 | |
| 648 | ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color); |
| 649 | ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color); |
| 650 | |
| 651 | ClassDB::bind_method(D_METHOD("set_polygons", "polygons"), &Polygon2D::set_polygons); |
| 652 | ClassDB::bind_method(D_METHOD("get_polygons"), &Polygon2D::get_polygons); |
| 653 | |
| 654 | ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors); |
| 655 | ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors); |
| 656 | |
| 657 | ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Polygon2D::set_texture); |
| 658 | ClassDB::bind_method(D_METHOD("get_texture"), &Polygon2D::get_texture); |
| 659 | |
| 660 | ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Polygon2D::set_texture_offset); |
| 661 | ClassDB::bind_method(D_METHOD("get_texture_offset"), &Polygon2D::get_texture_offset); |
| 662 | |
| 663 | ClassDB::bind_method(D_METHOD("set_texture_rotation", "texture_rotation"), &Polygon2D::set_texture_rotation); |
| 664 | ClassDB::bind_method(D_METHOD("get_texture_rotation"), &Polygon2D::get_texture_rotation); |
| 665 | |
| 666 | ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Polygon2D::set_texture_scale); |
| 667 | ClassDB::bind_method(D_METHOD("get_texture_scale"), &Polygon2D::get_texture_scale); |
| 668 | |
| 669 | ClassDB::bind_method(D_METHOD("set_invert_enabled", "invert"), &Polygon2D::set_invert); |
| 670 | ClassDB::bind_method(D_METHOD("get_invert_enabled"), &Polygon2D::get_invert); |
| 671 | |
| 672 | ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &Polygon2D::set_antialiased); |
| 673 | ClassDB::bind_method(D_METHOD("get_antialiased"), &Polygon2D::get_antialiased); |
| 674 | |
| 675 | ClassDB::bind_method(D_METHOD("set_invert_border", "invert_border"), &Polygon2D::set_invert_border); |
| 676 | ClassDB::bind_method(D_METHOD("get_invert_border"), &Polygon2D::get_invert_border); |
| 677 | |
| 678 | ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Polygon2D::set_offset); |
| 679 | ClassDB::bind_method(D_METHOD("get_offset"), &Polygon2D::get_offset); |
| 680 | |
| 681 | ClassDB::bind_method(D_METHOD("add_bone", "path", "weights"), &Polygon2D::add_bone); |
| 682 | ClassDB::bind_method(D_METHOD("get_bone_count"), &Polygon2D::get_bone_count); |
| 683 | ClassDB::bind_method(D_METHOD("get_bone_path", "index"), &Polygon2D::get_bone_path); |
| 684 | ClassDB::bind_method(D_METHOD("get_bone_weights", "index"), &Polygon2D::get_bone_weights); |
| 685 | ClassDB::bind_method(D_METHOD("erase_bone", "index"), &Polygon2D::erase_bone); |
| 686 | ClassDB::bind_method(D_METHOD("clear_bones"), &Polygon2D::clear_bones); |
| 687 | ClassDB::bind_method(D_METHOD("set_bone_path", "index", "path"), &Polygon2D::set_bone_path); |
| 688 | ClassDB::bind_method(D_METHOD("set_bone_weights", "index", "weights"), &Polygon2D::set_bone_weights); |
| 689 | |
| 690 | ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &Polygon2D::set_skeleton); |
| 691 | ClassDB::bind_method(D_METHOD("get_skeleton"), &Polygon2D::get_skeleton); |
| 692 | |
| 693 | ClassDB::bind_method(D_METHOD("set_internal_vertex_count", "internal_vertex_count"), &Polygon2D::set_internal_vertex_count); |
| 694 | ClassDB::bind_method(D_METHOD("get_internal_vertex_count"), &Polygon2D::get_internal_vertex_count); |
| 695 | |
| 696 | ClassDB::bind_method(D_METHOD("_set_bones", "bones"), &Polygon2D::_set_bones); |
| 697 | ClassDB::bind_method(D_METHOD("_get_bones"), &Polygon2D::_get_bones); |
| 698 | |
| 699 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); |
| 700 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset"); |
| 701 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased"); |
| 702 | |
| 703 | ADD_GROUP("Texture", "texture_"); |
| 704 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture"); |
| 705 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_offset", "get_texture_offset"); |
| 706 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_scale", PROPERTY_HINT_LINK), "set_texture_scale", "get_texture_scale"); |
| 707 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texture_rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_texture_rotation", "get_texture_rotation"); |
| 708 | |
| 709 | ADD_GROUP("Skeleton", ""); |
| 710 | ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton2D"), "set_skeleton", "get_skeleton"); |
| 711 | |
| 712 | ADD_GROUP("Invert", "invert_"); |
| 713 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_invert_enabled", "get_invert_enabled"); |
| 714 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "invert_border", PROPERTY_HINT_RANGE, "0.1,16384,0.1,suffix:px"), "set_invert_border", "get_invert_border"); |
| 715 | |
| 716 | ADD_GROUP("Data", ""); |
| 717 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon"); |
| 718 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv"); |
| 719 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors"); |
| 720 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_TYPE_STRING, "PackedInt32Array"), "set_polygons", "get_polygons"); |
| 721 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_bones", "_get_bones"); |
| 722 | ADD_PROPERTY(PropertyInfo(Variant::INT, "internal_vertex_count", PROPERTY_HINT_RANGE, "0,1000"), "set_internal_vertex_count", "get_internal_vertex_count"); |
| 723 | } |
| 724 | |
| 725 | Polygon2D::Polygon2D() { |
| 726 | mesh = RS::get_singleton()->mesh_create(); |
| 727 | } |
| 728 | |
| 729 | Polygon2D::~Polygon2D() { |
| 730 | // This will free the internally-allocated mesh instance, if allocated. |
| 731 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 732 | RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID()); |
| 733 | RS::get_singleton()->free_rid(mesh); |
| 734 | } |