1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171 |
- /******************************************************************************
- * Spine Runtimes License Agreement
- * Last updated January 1, 2020. Replaces all prior versions.
- *
- * Copyright (c) 2013-2020, Esoteric Software LLC
- *
- * Integration of the Spine Runtimes into software or otherwise creating
- * derivative works of the Spine Runtimes is permitted under the terms and
- * conditions of Section 2 of the Spine Editor License Agreement:
- * http://esotericsoftware.com/spine-editor-license
- *
- * Otherwise, it is permitted to integrate the Spine Runtimes into software
- * or otherwise create derivative works of the Spine Runtimes (collectively,
- * "Products"), provided that each user of the Products must obtain their own
- * Spine Editor license and redistribution of the Products in any form must
- * include this license and copyright notice.
- *
- * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
- * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *****************************************************************************/
- #include "SpineSprite.h"
- #include "SpineEvent.h"
- #include "SpineTrackEntry.h"
- #include "SpineSkeleton.h"
- #include "SpineRendererObject.h"
- #include "SpineSlotNode.h"
- #if VERSION_MAJOR > 3
- #include "core/config/engine.h"
- #include "core/math/geometry_2d.h"
- #include "core/math/transform_2d.h"
- #include "core/variant/array.h"
- #include "scene/resources/mesh.h"
- #include "servers/rendering_server.h"
- #else
- #include "core/engine.h"
- #endif
- #include "scene/gui/control.h"
- #include "scene/main/viewport.h"
- #if TOOLS_ENABLED
- #include "editor/editor_plugin.h"
- #endif
- Ref<CanvasItemMaterial> SpineSprite::default_materials[4] = {};
- static int sprite_count = 0;
- static spine::Vector<unsigned short> quad_indices;
- static spine::Vector<float> scratch_vertices;
- static Vector<Vector2> scratch_points;
- static void clear_triangles(SpineMesh2D *mesh_instance) {
- #if VERSION_MAJOR > 3
- RenderingServer::get_singleton()->canvas_item_clear(mesh_instance->get_canvas_item());
- #else
- VisualServer::get_singleton()->canvas_item_clear(mesh_instance->get_canvas_item());
- #endif
- }
- static void add_triangles(SpineMesh2D *mesh_instance,
- const Vector<Point2> &vertices,
- const Vector<Point2> &uvs,
- const Vector<Color> &colors,
- const Vector<int> &indices,
- SpineRendererObject *renderer_object) {
- #if VERSION_MAJOR > 3
- mesh_instance->update_mesh(vertices, uvs, colors, indices, renderer_object);
- #else
- #define USE_MESH 0
- #if USE_MESH
- mesh_instance->update_mesh(vertices, uvs, colors, indices, renderer_object);
- #else
- auto texture = renderer_object->texture;
- auto normal_map = renderer_object->normal_map;
- VisualServer::get_singleton()->canvas_item_add_triangle_array(mesh_instance->get_canvas_item(),
- indices,
- vertices,
- colors,
- uvs,
- Vector<int>(),
- Vector<float>(),
- texture.is_null() ? RID() : texture->get_rid(),
- -1,
- normal_map.is_null() ? RID() : normal_map->get_rid());
- #endif
- #endif
- }
- void SpineMesh2D::_notification(int what) {
- switch (what) {
- case NOTIFICATION_READY: {
- set_process_internal(true);
- break;
- }
- case NOTIFICATION_INTERNAL_PROCESS:
- #if VERSION_MAJOR > 3
- queue_redraw();
- #else
- update();
- #endif
- break;
- case NOTIFICATION_DRAW:
- clear_triangles(this);
- if (renderer_object)
- add_triangles(this, vertices, uvs, colors, indices, renderer_object);
- break;
- default:
- break;
- }
- }
- void SpineMesh2D::_bind_methods() {
- }
- void SpineMesh2D::update_mesh(const Vector<Point2> &vertices,
- const Vector<Point2> &uvs,
- const Vector<Color> &colors,
- const Vector<int> &indices,
- SpineRendererObject *renderer_object) {
- #if VERSION_MAJOR > 3
- if (!mesh.is_valid() || vertices.size() != num_vertices || indices.size() != num_indices || last_indices_id != indices_id) {
- if (mesh.is_valid()) {
- RS::get_singleton()->free(mesh);
- }
- mesh = RS::get_singleton()->mesh_create();
- Array arrays;
- arrays.resize(Mesh::ARRAY_MAX);
- arrays[Mesh::ARRAY_VERTEX] = vertices;
- arrays[Mesh::ARRAY_TEX_UV] = uvs;
- arrays[Mesh::ARRAY_COLOR] = colors;
- arrays[Mesh::ARRAY_INDEX] = indices;
- RS::SurfaceData surface;
- uint32_t skin_stride;
- RS::get_singleton()->mesh_create_surface_data_from_arrays(&surface, (RS::PrimitiveType) Mesh::PRIMITIVE_TRIANGLES, arrays, TypedArray<Array>(), Dictionary(), Mesh::ArrayFormat::ARRAY_FLAG_USE_DYNAMIC_UPDATE);
- RS::get_singleton()->mesh_add_surface(mesh, surface);
- RS::get_singleton()->mesh_surface_make_offsets_from_format(surface.format, surface.vertex_count, surface.index_count, surface_offsets, vertex_stride, attribute_stride, skin_stride);
- num_vertices = vertices.size();
- num_indices = indices.size();
- vertex_buffer = surface.vertex_data;
- attribute_buffer = surface.attribute_data;
- last_indices_id = indices_id;
- } else {
- AABB aabb_new;
- uint8_t *vertex_write_buffer = vertex_buffer.ptrw();
- uint8_t *attribute_write_buffer = attribute_buffer.ptrw();
- uint8_t color[4] = {
- uint8_t(CLAMP(colors[0].r * 255.0, 0.0, 255.0)),
- uint8_t(CLAMP(colors[0].g * 255.0, 0.0, 255.0)),
- uint8_t(CLAMP(colors[0].b * 255.0, 0.0, 255.0)),
- uint8_t(CLAMP(colors[0].a * 255.0, 0.0, 255.0))};
- for (int i = 0; i < vertices.size(); i++) {
- Vector2 vertex(vertices[i]);
- if (i == 0) {
- aabb_new.position = Vector3(vertex.x, vertex.y, 0);
- aabb_new.size = Vector3();
- } else {
- aabb_new.expand_to(Vector3(vertex.x, vertex.y, 0));
- }
- float uv[2] = {(float) uvs[i].x, (float) uvs[i].y};
- memcpy(&vertex_write_buffer[i * vertex_stride + surface_offsets[RS::ARRAY_VERTEX]], &vertex, sizeof(float) * 2);
- memcpy(&attribute_write_buffer[i * attribute_stride + surface_offsets[RS::ARRAY_COLOR]], color, 4);
- memcpy(&attribute_write_buffer[i * attribute_stride + surface_offsets[RS::ARRAY_TEX_UV]], uv, 8);
- }
- RS::get_singleton()->mesh_surface_update_vertex_region(mesh, 0, 0, vertex_buffer);
- RS::get_singleton()->mesh_surface_update_attribute_region(mesh, 0, 0, attribute_buffer);
- RS::get_singleton()->mesh_set_custom_aabb(mesh, aabb_new);
- }
- RenderingServer::get_singleton()->canvas_item_add_mesh(this->get_canvas_item(), mesh, Transform2D(), Color(1, 1, 1, 1), renderer_object->canvas_texture->get_rid());
- #else
- if (!mesh.is_valid() || vertices.size() != num_vertices || indices.size() != num_indices || last_indices_id != indices_id) {
- if (mesh.is_valid()) {
- VS::get_singleton()->free(mesh);
- }
- mesh = VS::get_singleton()->mesh_create();
- Array arrays;
- arrays.resize(Mesh::ARRAY_MAX);
- arrays[Mesh::ARRAY_VERTEX] = vertices;
- arrays[Mesh::ARRAY_TEX_UV] = uvs;
- arrays[Mesh::ARRAY_COLOR] = colors;
- arrays[Mesh::ARRAY_INDEX] = indices;
- uint32_t compress_format = (VS::ARRAY_COMPRESS_DEFAULT & ~VS::ARRAY_COMPRESS_TEX_UV);
- VS::get_singleton()->mesh_add_surface_from_arrays(mesh, (VS::PrimitiveType) Mesh::PRIMITIVE_TRIANGLES, arrays, Array(), compress_format);
- int surface_vertex_len = VS::get_singleton()->mesh_surface_get_array_len(mesh, 0);
- int surface_index_len = VS::get_singleton()->mesh_surface_get_array_index_len(mesh, 0);
- mesh_surface_format = VS::get_singleton()->mesh_surface_get_format(mesh, 0);
- mesh_buffer = VS::get_singleton()->mesh_surface_get_array(mesh, 0);
- VS::get_singleton()->mesh_surface_make_offsets_from_format(mesh_surface_format, surface_vertex_len, surface_index_len, mesh_surface_offsets, mesh_stride);
- num_vertices = vertices.size();
- num_indices = indices.size();
- last_indices_id = indices_id;
- } else {
- AABB aabb_new;
- PoolVector<uint8_t>::Write write_buffer = mesh_buffer.write();
- uint8_t color[4] = {
- uint8_t(CLAMP(colors[0].r * 255.0, 0.0, 255.0)),
- uint8_t(CLAMP(colors[0].g * 255.0, 0.0, 255.0)),
- uint8_t(CLAMP(colors[0].b * 255.0, 0.0, 255.0)),
- uint8_t(CLAMP(colors[0].a * 255.0, 0.0, 255.0))};
- for (int i = 0; i < vertices.size(); i++) {
- Vector2 vertex(vertices[i]);
- if (i == 0) {
- aabb_new.position = Vector3(vertex.x, vertex.y, 0);
- aabb_new.size = Vector3();
- } else {
- aabb_new.expand_to(Vector3(vertex.x, vertex.y, 0));
- }
- float uv[2] = {(float) uvs[i].x, (float) uvs[i].y};
- memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_VERTEX] + mesh_surface_offsets[VS::ARRAY_VERTEX]], &vertex, sizeof(float) * 2);
- memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_TEX_UV] + mesh_surface_offsets[VS::ARRAY_TEX_UV]], uv, 8);
- memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_COLOR] + mesh_surface_offsets[VS::ARRAY_COLOR]], color, 4);
- }
- write_buffer.release();
- VS::get_singleton()->mesh_surface_update_region(mesh, 0, 0, mesh_buffer);
- VS::get_singleton()->mesh_set_custom_aabb(mesh, aabb_new);
- }
- VS::get_singleton()->canvas_item_add_mesh(
- this->get_canvas_item(),
- mesh,
- Transform2D(),
- Color(1, 1, 1, 1),
- renderer_object->texture.is_null() ? RID() : renderer_object->texture->get_rid(),
- renderer_object->normal_map.is_null() ? RID() : renderer_object->normal_map->get_rid());
- #endif
- }
- void SpineSprite::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_skeleton_data_res", "skeleton_data_res"), &SpineSprite::set_skeleton_data_res);
- ClassDB::bind_method(D_METHOD("get_skeleton_data_res"), &SpineSprite::get_skeleton_data_res);
- ClassDB::bind_method(D_METHOD("get_skeleton"), &SpineSprite::get_skeleton);
- ClassDB::bind_method(D_METHOD("get_animation_state"), &SpineSprite::get_animation_state);
- ClassDB::bind_method(D_METHOD("on_skeleton_data_changed"), &SpineSprite::on_skeleton_data_changed);
- ClassDB::bind_method(D_METHOD("get_global_bone_transform", "bone_name"), &SpineSprite::get_global_bone_transform);
- ClassDB::bind_method(D_METHOD("set_global_bone_transform", "bone_name", "global_transform"), &SpineSprite::set_global_bone_transform);
- ClassDB::bind_method(D_METHOD("set_update_mode", "v"), &SpineSprite::set_update_mode);
- ClassDB::bind_method(D_METHOD("get_update_mode"), &SpineSprite::get_update_mode);
- ClassDB::bind_method(D_METHOD("set_normal_material", "material"), &SpineSprite::set_normal_material);
- ClassDB::bind_method(D_METHOD("get_normal_material"), &SpineSprite::get_normal_material);
- ClassDB::bind_method(D_METHOD("set_additive_material", "material"), &SpineSprite::set_additive_material);
- ClassDB::bind_method(D_METHOD("get_additive_material"), &SpineSprite::get_additive_material);
- ClassDB::bind_method(D_METHOD("set_multiply_material", "material"), &SpineSprite::set_multiply_material);
- ClassDB::bind_method(D_METHOD("get_multiply_material"), &SpineSprite::get_multiply_material);
- ClassDB::bind_method(D_METHOD("set_screen_material", "material"), &SpineSprite::set_screen_material);
- ClassDB::bind_method(D_METHOD("get_screen_material"), &SpineSprite::get_screen_material);
- ClassDB::bind_method(D_METHOD("set_debug_root", "v"), &SpineSprite::set_debug_root);
- ClassDB::bind_method(D_METHOD("get_debug_root"), &SpineSprite::get_debug_root);
- ClassDB::bind_method(D_METHOD("set_debug_root_color", "v"), &SpineSprite::set_debug_root_color);
- ClassDB::bind_method(D_METHOD("get_debug_root_color"), &SpineSprite::get_debug_root_color);
- ClassDB::bind_method(D_METHOD("set_debug_bones", "v"), &SpineSprite::set_debug_bones);
- ClassDB::bind_method(D_METHOD("get_debug_bones"), &SpineSprite::get_debug_bones);
- ClassDB::bind_method(D_METHOD("set_debug_bones_color", "v"), &SpineSprite::set_debug_bones_color);
- ClassDB::bind_method(D_METHOD("get_debug_bones_color"), &SpineSprite::get_debug_bones_color);
- ClassDB::bind_method(D_METHOD("set_debug_bones_thickness", "v"), &SpineSprite::set_debug_bones_thickness);
- ClassDB::bind_method(D_METHOD("get_debug_bones_thickness"), &SpineSprite::get_debug_bones_thickness);
- ClassDB::bind_method(D_METHOD("set_debug_regions", "v"), &SpineSprite::set_debug_regions);
- ClassDB::bind_method(D_METHOD("get_debug_regions"), &SpineSprite::get_debug_regions);
- ClassDB::bind_method(D_METHOD("set_debug_regions_color", "v"), &SpineSprite::set_debug_regions_color);
- ClassDB::bind_method(D_METHOD("get_debug_regions_color"), &SpineSprite::get_debug_regions_color);
- ClassDB::bind_method(D_METHOD("set_debug_meshes", "v"), &SpineSprite::set_debug_meshes);
- ClassDB::bind_method(D_METHOD("get_debug_meshes"), &SpineSprite::get_debug_meshes);
- ClassDB::bind_method(D_METHOD("set_debug_meshes_color", "v"), &SpineSprite::set_debug_meshes_color);
- ClassDB::bind_method(D_METHOD("get_debug_meshes_color"), &SpineSprite::get_debug_meshes_color);
- ClassDB::bind_method(D_METHOD("set_debug_bounding_boxes", "v"), &SpineSprite::set_debug_bounding_boxes);
- ClassDB::bind_method(D_METHOD("get_debug_bounding_boxes"), &SpineSprite::get_debug_bounding_boxes);
- ClassDB::bind_method(D_METHOD("set_debug_bounding_boxes_color", "v"), &SpineSprite::set_debug_bounding_boxes_color);
- ClassDB::bind_method(D_METHOD("get_debug_bounding_boxes_color"), &SpineSprite::get_debug_bounding_boxes_color);
- ClassDB::bind_method(D_METHOD("set_debug_paths", "v"), &SpineSprite::set_debug_paths);
- ClassDB::bind_method(D_METHOD("get_debug_paths"), &SpineSprite::get_debug_paths);
- ClassDB::bind_method(D_METHOD("set_debug_paths_color", "v"), &SpineSprite::set_debug_paths_color);
- ClassDB::bind_method(D_METHOD("get_debug_paths_color"), &SpineSprite::get_debug_paths_color);
- ClassDB::bind_method(D_METHOD("set_debug_clipping", "v"), &SpineSprite::set_debug_clipping);
- ClassDB::bind_method(D_METHOD("get_debug_clipping"), &SpineSprite::get_debug_clipping);
- ClassDB::bind_method(D_METHOD("set_debug_clipping_color", "v"), &SpineSprite::set_debug_clipping_color);
- ClassDB::bind_method(D_METHOD("get_debug_clipping_color"), &SpineSprite::get_debug_clipping_color);
- ClassDB::bind_method(D_METHOD("update_skeleton", "delta"), &SpineSprite::update_skeleton);
- ClassDB::bind_method(D_METHOD("new_skin", "name"), &SpineSprite::new_skin);
- ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite"), PropertyInfo(Variant::OBJECT, "animation_state", PROPERTY_HINT_TYPE_STRING, "SpineAnimationState"), PropertyInfo(Variant::OBJECT, "track_entry", PROPERTY_HINT_TYPE_STRING, "SpineTrackEntry")));
- ADD_SIGNAL(MethodInfo("animation_interrupted", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite"), PropertyInfo(Variant::OBJECT, "animation_state", PROPERTY_HINT_TYPE_STRING, "SpineAnimationState"), PropertyInfo(Variant::OBJECT, "track_entry", PROPERTY_HINT_TYPE_STRING, "SpineTrackEntry")));
- ADD_SIGNAL(MethodInfo("animation_ended", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite"), PropertyInfo(Variant::OBJECT, "animation_state", PROPERTY_HINT_TYPE_STRING, "SpineAnimationState"), PropertyInfo(Variant::OBJECT, "track_entry", PROPERTY_HINT_TYPE_STRING, "SpineTrackEntry")));
- ADD_SIGNAL(MethodInfo("animation_completed", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite"), PropertyInfo(Variant::OBJECT, "animation_state", PROPERTY_HINT_TYPE_STRING, "SpineAnimationState"), PropertyInfo(Variant::OBJECT, "track_entry", PROPERTY_HINT_TYPE_STRING, "SpineTrackEntry")));
- ADD_SIGNAL(MethodInfo("animation_disposed", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite"), PropertyInfo(Variant::OBJECT, "animation_state", PROPERTY_HINT_TYPE_STRING, "SpineAnimationState"), PropertyInfo(Variant::OBJECT, "track_entry", PROPERTY_HINT_TYPE_STRING, "SpineTrackEntry")));
- ADD_SIGNAL(MethodInfo("animation_event", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite"), PropertyInfo(Variant::OBJECT, "animation_state", PROPERTY_HINT_TYPE_STRING, "SpineAnimationState"), PropertyInfo(Variant::OBJECT, "track_entry", PROPERTY_HINT_TYPE_STRING, "SpineTrackEntry"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_TYPE_STRING, "SpineEvent")));
- ADD_SIGNAL(MethodInfo("before_animation_state_update", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite")));
- ADD_SIGNAL(MethodInfo("before_animation_state_apply", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite")));
- ADD_SIGNAL(MethodInfo("before_world_transforms_change", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite")));
- ADD_SIGNAL(MethodInfo("world_transforms_changed", PropertyInfo(Variant::OBJECT, "spine_sprite", PROPERTY_HINT_TYPE_STRING, "SpineSprite")));
- ADD_SIGNAL(MethodInfo("_internal_spine_objects_invalidated"));
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skeleton_data_res", PropertyHint::PROPERTY_HINT_RESOURCE_TYPE, "SpineSkeletonDataResource"), "set_skeleton_data_res", "get_skeleton_data_res");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Process,Physics,Manual"), "set_update_mode", "get_update_mode");
- ADD_GROUP("Materials", "");
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_normal_material", "get_normal_material");
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "additive_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_additive_material", "get_additive_material");
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiply_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_multiply_material", "get_multiply_material");
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "screen_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_screen_material", "get_screen_material");
- ADD_GROUP("Debug", "");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "root"), "set_debug_root", "get_debug_root");
- ADD_PROPERTY(PropertyInfo(Variant::COLOR, "root_color"), "set_debug_root_color", "get_debug_root_color");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bones"), "set_debug_bones", "get_debug_bones");
- ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bones_color"), "set_debug_bones_color", "get_debug_bones_color");
- ADD_PROPERTY(PropertyInfo(VARIANT_FLOAT, "bones_thickness"), "set_debug_bones_thickness", "get_debug_bones_thickness");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "regions"), "set_debug_regions", "get_debug_regions");
- ADD_PROPERTY(PropertyInfo(Variant::COLOR, "regions_color"), "set_debug_regions_color", "get_debug_regions_color");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meshes"), "set_debug_meshes", "get_debug_meshes");
- ADD_PROPERTY(PropertyInfo(Variant::COLOR, "meshes_color"), "set_debug_meshes_color", "get_debug_meshes_color");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bounding_boxes"), "set_debug_bounding_boxes", "get_debug_bounding_boxes");
- ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bounding_boxes_color"), "set_debug_bounding_boxes_color", "get_debug_bounding_boxes_color");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paths"), "set_debug_paths", "get_debug_paths");
- ADD_PROPERTY(PropertyInfo(Variant::COLOR, "paths_color"), "set_debug_paths_color", "get_debug_paths_color");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clipping"), "set_debug_clipping", "get_debug_clipping");
- ADD_PROPERTY(PropertyInfo(Variant::COLOR, "paths_clipping"), "set_debug_clipping_color", "get_debug_clipping_color");
- ADD_GROUP("Preview", "");
- // Filled in in _get_property_list()
- }
- SpineSprite::SpineSprite() : update_mode(SpineConstant::UpdateMode_Process), preview_skin("Default"), preview_animation("-- Empty --"), preview_frame(false), preview_time(0), skeleton_clipper(nullptr), modified_bones(false) {
- skeleton_clipper = new spine::SkeletonClipping();
- // One material per blend mode, shared across all sprites.
- if (!default_materials[0].is_valid()) {
- Ref<CanvasItemMaterial> material_normal(memnew(CanvasItemMaterial));
- material_normal->set_blend_mode(CanvasItemMaterial::BLEND_MODE_MIX);
- default_materials[spine::BlendMode_Normal] = material_normal;
- Ref<CanvasItemMaterial> material_additive(memnew(CanvasItemMaterial));
- material_additive->set_blend_mode(CanvasItemMaterial::BLEND_MODE_ADD);
- default_materials[spine::BlendMode_Additive] = material_additive;
- Ref<CanvasItemMaterial> material_multiply(memnew(CanvasItemMaterial));
- material_multiply->set_blend_mode(CanvasItemMaterial::BLEND_MODE_MUL);
- default_materials[spine::BlendMode_Multiply] = material_multiply;
- Ref<CanvasItemMaterial> material_screen(memnew(CanvasItemMaterial));
- material_screen->set_blend_mode(CanvasItemMaterial::BLEND_MODE_SUB);
- default_materials[spine::BlendMode_Screen] = material_screen;
- }
- // Setup static scratch buffers
- if (quad_indices.size() == 0) {
- quad_indices.setSize(6, 0);
- quad_indices[0] = 0;
- quad_indices[1] = 1;
- quad_indices[2] = 2;
- quad_indices[3] = 2;
- quad_indices[4] = 3;
- quad_indices[5] = 0;
- scratch_vertices.ensureCapacity(1200);
- }
- // Default debug settings
- debug_root = false;
- debug_root_color = Color(1, 1, 1, 0.5);
- debug_bones = false;
- debug_bones_color = Color(1, 1, 0, 0.5);
- debug_bones_thickness = 5;
- debug_regions = false;
- debug_regions_color = Color(0, 0, 1, 0.5);
- debug_meshes = false;
- debug_meshes_color = Color(0, 0, 1, 0.5);
- debug_bounding_boxes = false;
- debug_bounding_boxes_color = Color(0, 1, 0, 0.5);
- debug_paths = false;
- debug_paths_color = Color::hex(0xff7f0077);
- debug_clipping = false;
- debug_clipping_color = Color(0.8, 0, 0, 0.8);
- sprite_count++;
- }
- SpineSprite::~SpineSprite() {
- delete skeleton_clipper;
- sprite_count--;
- if (!sprite_count) {
- for (int i = 0; i < 4; i++)
- default_materials[i].unref();
- }
- }
- void SpineSprite::set_skeleton_data_res(const Ref<SpineSkeletonDataResource> &_skeleton_data) {
- skeleton_data_res = _skeleton_data;
- on_skeleton_data_changed();
- }
- Ref<SpineSkeletonDataResource> SpineSprite::get_skeleton_data_res() {
- return skeleton_data_res;
- }
- void SpineSprite::on_skeleton_data_changed() {
- remove_meshes();
- skeleton.unref();
- animation_state.unref();
- emit_signal("_internal_spine_objects_invalidated");
- if (skeleton_data_res.is_valid()) {
- #if VERSION_MAJOR > 3
- if (!skeleton_data_res->is_connected("skeleton_data_changed", callable_mp(this, &SpineSprite::on_skeleton_data_changed)))
- skeleton_data_res->connect("skeleton_data_changed", callable_mp(this, &SpineSprite::on_skeleton_data_changed));
- #else
- if (!skeleton_data_res->is_connected("skeleton_data_changed", this, "on_skeleton_data_changed"))
- skeleton_data_res->connect("skeleton_data_changed", this, "on_skeleton_data_changed");
- #endif
- }
- if (skeleton_data_res.is_valid() && skeleton_data_res->is_skeleton_data_loaded()) {
- skeleton = Ref<SpineSkeleton>(memnew(SpineSkeleton));
- skeleton->set_spine_sprite(this);
- animation_state = Ref<SpineAnimationState>(memnew(SpineAnimationState));
- animation_state->set_spine_sprite(this);
- animation_state->get_spine_object()->setListener(this);
- animation_state->update(0);
- animation_state->apply(skeleton);
- skeleton->update_world_transform();
- generate_meshes_for_slots(skeleton);
- if (update_mode == SpineConstant::UpdateMode_Process) {
- _notification(NOTIFICATION_INTERNAL_PROCESS);
- } else if (update_mode == SpineConstant::UpdateMode_Physics) {
- _notification(NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
- }
- }
- NOTIFY_PROPERTY_LIST_CHANGED();
- }
- void SpineSprite::generate_meshes_for_slots(Ref<SpineSkeleton> skeleton_ref) {
- auto skeleton = skeleton_ref->get_spine_object();
- for (int i = 0, n = (int) skeleton->getSlots().size(); i < n; i++) {
- auto mesh_instance = memnew(SpineMesh2D);
- mesh_instance->set_position(Vector2(0, 0));
- mesh_instance->set_material(default_materials[spine::BlendMode_Normal]);
- // Needed so that debug drawables are rendered in front of attachments
- mesh_instance->set_draw_behind_parent(true);
- add_child(mesh_instance);
- mesh_instances.push_back(mesh_instance);
- slot_nodes.add(spine::Vector<SpineSlotNode *>());
- }
- }
- void SpineSprite::remove_meshes() {
- for (int i = 0; i < mesh_instances.size(); ++i) {
- remove_child(mesh_instances[i]);
- memdelete(mesh_instances[i]);
- }
- mesh_instances.clear();
- slot_nodes.clear();
- }
- void SpineSprite::sort_slot_nodes() {
- for (int i = 0; i < (int) slot_nodes.size(); i++) {
- slot_nodes[i].setSize(0, nullptr);
- }
- auto draw_order = skeleton->get_spine_object()->getDrawOrder();
- for (int i = 0; i < get_child_count(); i++) {
- auto child = cast_to<Node2D>(get_child(i));
- if (!child) continue;
- // Needed so that debug drawables are rendered in front of attachments and other nodes under the sprite.
- child->set_draw_behind_parent(true);
- auto slot_node = Object::cast_to<SpineSlotNode>(get_child(i));
- if (!slot_node) continue;
- if (slot_node->get_slot_index() == -1 || slot_node->get_slot_index() >= (int) draw_order.size()) {
- continue;
- }
- slot_nodes[slot_node->get_slot_index()].add(slot_node);
- }
- for (int i = 0; i < (int) draw_order.size(); i++) {
- int slot_index = draw_order[i]->getData().getIndex();
- int mesh_index = mesh_instances[i]->get_index();
- spine::Vector<SpineSlotNode *> &nodes = slot_nodes[slot_index];
- for (int j = 0; j < (int) nodes.size(); j++) {
- auto node = nodes[j];
- move_child(node, mesh_index + 1);
- }
- }
- }
- Ref<SpineSkeleton> SpineSprite::get_skeleton() {
- return skeleton;
- }
- Ref<SpineAnimationState> SpineSprite::get_animation_state() {
- return animation_state;
- }
- void SpineSprite::_notification(int what) {
- switch (what) {
- case NOTIFICATION_READY: {
- set_process_internal(update_mode == SpineConstant::UpdateMode_Process);
- set_physics_process_internal(update_mode == SpineConstant::UpdateMode_Physics);
- break;
- }
- case NOTIFICATION_INTERNAL_PROCESS: {
- if (update_mode == SpineConstant::UpdateMode_Process)
- update_skeleton(get_process_delta_time());
- break;
- }
- case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
- if (update_mode == SpineConstant::UpdateMode_Physics)
- update_skeleton(get_physics_process_delta_time());
- break;
- }
- case NOTIFICATION_DRAW: {
- draw();
- break;
- }
- default:
- break;
- }
- }
- void SpineSprite::_get_property_list(List<PropertyInfo> *list) const {
- if (!skeleton_data_res.is_valid() || !skeleton_data_res->is_skeleton_data_loaded()) return;
- Vector<String> animation_names;
- Vector<String> skin_names;
- skeleton_data_res->get_animation_names(animation_names);
- skeleton_data_res->get_skin_names(skin_names);
- animation_names.insert(0, "-- Empty --");
- PropertyInfo preview_skin_property;
- preview_skin_property.name = "preview_skin";
- preview_skin_property.type = Variant::STRING;
- preview_skin_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
- preview_skin_property.hint_string = String(",").join(skin_names);
- preview_skin_property.hint = PROPERTY_HINT_ENUM;
- list->push_back(preview_skin_property);
- PropertyInfo preview_anim_property;
- preview_anim_property.name = "preview_animation";
- preview_anim_property.type = Variant::STRING;
- preview_anim_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
- preview_anim_property.hint_string = String(",").join(animation_names);
- preview_anim_property.hint = PROPERTY_HINT_ENUM;
- list->push_back(preview_anim_property);
- PropertyInfo preview_frame_property;
- preview_frame_property.name = "preview_frame";
- preview_frame_property.type = Variant::BOOL;
- preview_frame_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
- list->push_back(preview_frame_property);
- PropertyInfo preview_time_property;
- preview_time_property.name = "preview_time";
- preview_time_property.type = VARIANT_FLOAT;
- preview_time_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
- float animation_duration = 0;
- if (!EMPTY(preview_animation) && preview_animation != "-- Empty --") {
- auto animation = skeleton_data_res->find_animation(preview_animation);
- if (animation.is_valid()) animation_duration = animation->get_duration();
- }
- preview_time_property.hint_string = String("0.0,{0},0.01").format(varray(animation_duration));
- preview_time_property.hint = PROPERTY_HINT_RANGE;
- list->push_back(preview_time_property);
- }
- bool SpineSprite::_get(const StringName &property, Variant &value) const {
- if (property == "preview_skin") {
- value = preview_skin;
- return true;
- }
- if (property == "preview_animation") {
- value = preview_animation;
- return true;
- }
- if (property == "preview_frame") {
- value = preview_frame;
- return true;
- }
- if (property == "preview_time") {
- value = preview_time;
- return true;
- }
- return false;
- }
- static void update_preview_animation(SpineSprite *sprite, const String &skin, const String &animation, bool frame, float time) {
- if (!sprite->get_skeleton().is_valid()) return;
- if (EMPTY(skin) || skin == "Default") {
- sprite->get_skeleton()->set_skin(nullptr);
- } else {
- sprite->get_skeleton()->set_skin_by_name(skin);
- }
- sprite->get_skeleton()->set_to_setup_pose();
- if (EMPTY(animation) || animation == "-- Empty --") {
- sprite->get_animation_state()->set_empty_animation(0, 0);
- return;
- }
- auto track_entry = sprite->get_animation_state()->set_animation(animation, true, 0);
- track_entry->set_mix_duration(0);
- if (frame) {
- track_entry->set_time_scale(0);
- track_entry->set_track_time(time);
- }
- }
- bool SpineSprite::_set(const StringName &property, const Variant &value) {
- if (property == "preview_skin") {
- preview_skin = value;
- update_preview_animation(this, preview_skin, preview_animation, preview_frame, preview_time);
- NOTIFY_PROPERTY_LIST_CHANGED();
- return true;
- }
- if (property == "preview_animation") {
- preview_animation = value;
- update_preview_animation(this, preview_skin, preview_animation, preview_frame, preview_time);
- NOTIFY_PROPERTY_LIST_CHANGED();
- return true;
- }
- if (property == "preview_frame") {
- preview_frame = value;
- update_preview_animation(this, preview_skin, preview_animation, preview_frame, preview_time);
- return true;
- }
- if (property == "preview_time") {
- preview_time = value;
- update_preview_animation(this, preview_skin, preview_animation, preview_frame, preview_time);
- return true;
- }
- return false;
- }
- void SpineSprite::update_skeleton(float delta) {
- if (!skeleton_data_res.is_valid() ||
- !skeleton_data_res->is_skeleton_data_loaded() ||
- !skeleton.is_valid() ||
- !skeleton->get_spine_object() ||
- !animation_state.is_valid() ||
- !animation_state->get_spine_object())
- return;
- emit_signal("before_animation_state_update", this);
- animation_state->update(delta);
- if (!is_visible_in_tree()) return;
- emit_signal("before_animation_state_apply", this);
- animation_state->apply(skeleton);
- emit_signal("before_world_transforms_change", this);
- skeleton->update_world_transform();
- modified_bones = false;
- emit_signal("world_transforms_changed", this);
- if (modified_bones) skeleton->update_world_transform();
- sort_slot_nodes();
- update_meshes(skeleton);
- #if VERSION_MAJOR > 3
- queue_redraw();
- #else
- update();
- #endif
- }
- void SpineSprite::update_meshes(Ref<SpineSkeleton> skeleton_ref) {
- spine::Skeleton *skeleton = skeleton_ref->get_spine_object();
- for (int i = 0, n = (int) skeleton->getSlots().size(); i < n; ++i) {
- spine::Slot *slot = skeleton->getDrawOrder()[i];
- spine::Attachment *attachment = slot->getAttachment();
- SpineMesh2D *mesh_instance = mesh_instances[i];
- mesh_instance->renderer_object = nullptr;
- if (!attachment) {
- skeleton_clipper->clipEnd(*slot);
- continue;
- }
- if (!slot->getBone().isActive()) {
- skeleton_clipper->clipEnd(*slot);
- continue;
- }
- spine::Color skeleton_color = skeleton->getColor();
- spine::Color slot_color = slot->getColor();
- spine::Color tint(skeleton_color.r * slot_color.r, skeleton_color.g * slot_color.g, skeleton_color.b * slot_color.b, skeleton_color.a * slot_color.a);
- SpineRendererObject *renderer_object;
- spine::Vector<float> *vertices = &scratch_vertices;
- spine::Vector<float> *uvs;
- spine::Vector<unsigned short> *indices;
- if (attachment->getRTTI().isExactly(spine::RegionAttachment::rtti)) {
- auto *region = (spine::RegionAttachment *) attachment;
- vertices->setSize(8, 0);
- region->computeWorldVertices(*slot, *vertices, 0);
- renderer_object = (SpineRendererObject *) ((spine::AtlasRegion *) region->getRendererObject())->page->getRendererObject();
- uvs = ®ion->getUVs();
- indices = &quad_indices;
- auto attachment_color = region->getColor();
- tint.r *= attachment_color.r;
- tint.g *= attachment_color.g;
- tint.b *= attachment_color.b;
- tint.a *= attachment_color.a;
- } else if (attachment->getRTTI().isExactly(spine::MeshAttachment::rtti)) {
- auto *mesh = (spine::MeshAttachment *) attachment;
- vertices->setSize(mesh->getWorldVerticesLength(), 0);
- mesh->computeWorldVertices(*slot, *vertices);
- renderer_object = (SpineRendererObject *) ((spine::AtlasRegion *) mesh->getRendererObject())->page->getRendererObject();
- uvs = &mesh->getUVs();
- indices = &mesh->getTriangles();
- auto attachment_color = mesh->getColor();
- tint.r *= attachment_color.r;
- tint.g *= attachment_color.g;
- tint.b *= attachment_color.b;
- tint.a *= attachment_color.a;
- } else if (attachment->getRTTI().isExactly(spine::ClippingAttachment::rtti)) {
- auto clip = (spine::ClippingAttachment *) attachment;
- skeleton_clipper->clipStart(*slot, clip);
- continue;
- } else {
- skeleton_clipper->clipEnd(*slot);
- continue;
- }
- if (skeleton_clipper->isClipping()) {
- skeleton_clipper->clipTriangles(*vertices, *indices, *uvs, 2);
- if (skeleton_clipper->getClippedTriangles().size() == 0) {
- skeleton_clipper->clipEnd(*slot);
- continue;
- }
- vertices = &skeleton_clipper->getClippedVertices();
- uvs = &skeleton_clipper->getClippedUVs();
- indices = &skeleton_clipper->getClippedTriangles();
- }
- if (indices->size() > 0) {
- mesh_instance->set_light_mask(get_light_mask());
- size_t num_vertices = vertices->size() / 2;
- mesh_instance->vertices.resize((int) num_vertices);
- memcpy(mesh_instance->vertices.ptrw(), vertices->buffer(), num_vertices * 2 * sizeof(float));
- mesh_instance->uvs.resize((int) num_vertices);
- memcpy(mesh_instance->uvs.ptrw(), uvs->buffer(), num_vertices * 2 * sizeof(float));
- mesh_instance->colors.resize((int) num_vertices);
- for (int j = 0; j < (int) num_vertices; j++) {
- mesh_instance->colors.set(j, Color(tint.r, tint.g, tint.b, tint.a));
- }
- mesh_instance->indices.resize((int) indices->size());
- for (int j = 0; j < (int) indices->size(); ++j) {
- mesh_instance->indices.set(j, indices->buffer()[j]);
- }
- mesh_instance->renderer_object = renderer_object;
- #if VERSION_MAJOR > 3
- mesh_instance->indices_id = (uint64_t) indices;
- #endif
- spine::BlendMode blend_mode = slot->getData().getBlendMode();
- Ref<Material> custom_material;
- // See if we have a slot node for this slot with a custom material
- auto &nodes = slot_nodes[slot->getData().getIndex()];
- if (nodes.size() > 0) {
- auto slot_node = nodes[0];
- if (slot_node) {
- switch (blend_mode) {
- case spine::BlendMode_Normal:
- custom_material = slot_node->get_normal_material();
- break;
- case spine::BlendMode_Additive:
- custom_material = slot_node->get_additive_material();
- break;
- case spine::BlendMode_Multiply:
- custom_material = slot_node->get_multiply_material();
- break;
- case spine::BlendMode_Screen:
- custom_material = slot_node->get_screen_material();
- break;
- }
- }
- }
- // Else, check if we have a material on the sprite itself
- if (!custom_material.is_valid()) {
- switch (blend_mode) {
- case spine::BlendMode_Normal:
- custom_material = normal_material;
- break;
- case spine::BlendMode_Additive:
- custom_material = additive_material;
- break;
- case spine::BlendMode_Multiply:
- custom_material = multiply_material;
- break;
- case spine::BlendMode_Screen:
- custom_material = screen_material;
- break;
- }
- }
- // Set the custom material, or the default material
- if (custom_material.is_valid()) mesh_instance->set_material(custom_material);
- else
- mesh_instance->set_material(default_materials[slot->getData().getBlendMode()]);
- }
- skeleton_clipper->clipEnd(*slot);
- }
- skeleton_clipper->clipEnd();
- }
- void SpineSprite::draw() {
- if (!animation_state.is_valid() && !skeleton.is_valid()) return;
- if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) return;
- #if VERSION_MAJOR > 3
- RS::get_singleton()->canvas_item_clear(this->get_canvas_item());
- #else
- VisualServer::get_singleton()->canvas_item_clear(this->get_canvas_item());
- #endif
- auto mouse_position = get_local_mouse_position();
- spine::Slot *hovered_slot = nullptr;
- if (debug_regions) {
- draw_set_transform(Vector2(0, 0), 0, Vector2(1, 1));
- auto &draw_order = skeleton->get_spine_object()->getDrawOrder();
- for (int i = 0; i < (int) draw_order.size(); i++) {
- auto *slot = draw_order[i];
- if (!slot->getBone().isActive()) continue;
- auto *attachment = slot->getAttachment();
- if (!attachment) continue;
- if (!attachment->getRTTI().isExactly(spine::RegionAttachment::rtti)) continue;
- auto *region = (spine::RegionAttachment *) attachment;
- auto *vertices = &scratch_vertices;
- vertices->setSize(8, 0);
- region->computeWorldVertices(*slot, *vertices, 0);
- scratch_points.resize(0);
- for (int i = 0, j = 0; i < 4; i++, j += 2) {
- float x = vertices->buffer()[j];
- float y = vertices->buffer()[j + 1];
- scratch_points.push_back(Vector2(x, y));
- }
- scratch_points.push_back(Vector2(vertices->buffer()[0], vertices->buffer()[1]));
- Color color = debug_regions_color;
- if (GEOMETRY2D::is_point_in_polygon(mouse_position, scratch_points)) {
- hovered_slot = slot;
- color = Color(1, 1, 1, 1);
- }
- scratch_points.push_back(Vector2(vertices->buffer()[0], vertices->buffer()[1]));
- draw_polyline(scratch_points, color, 2);
- }
- }
- if (debug_meshes) {
- draw_set_transform(Vector2(0, 0), 0, Vector2(1, 1));
- auto &draw_order = skeleton->get_spine_object()->getDrawOrder();
- for (int i = 0; i < (int) draw_order.size(); i++) {
- auto *slot = draw_order[i];
- if (!slot->getBone().isActive()) continue;
- auto *attachment = slot->getAttachment();
- if (!attachment) continue;
- if (!attachment->getRTTI().isExactly(spine::MeshAttachment::rtti)) continue;
- auto *mesh = (spine::MeshAttachment *) attachment;
- auto *vertices = &scratch_vertices;
- vertices->setSize(mesh->getWorldVerticesLength(), 0);
- mesh->computeWorldVertices(*slot, *vertices);
- scratch_points.resize(0);
- for (int i = 0, j = 0; i < mesh->getHullLength(); i++, j += 2) {
- float x = vertices->buffer()[j];
- float y = vertices->buffer()[j + 1];
- scratch_points.push_back(Vector2(x, y));
- }
- Color color = debug_meshes_color;
- if (GEOMETRY2D::is_point_in_polygon(mouse_position, scratch_points)) {
- hovered_slot = slot;
- color = Color(1, 1, 1, 1);
- }
- scratch_points.push_back(Vector2(vertices->buffer()[0], vertices->buffer()[1]));
- draw_polyline(scratch_points, color, 2);
- }
- }
- if (debug_bounding_boxes) {
- draw_set_transform(Vector2(0, 0), 0, Vector2(1, 1));
- auto &draw_order = skeleton->get_spine_object()->getDrawOrder();
- for (int i = 0; i < (int) draw_order.size(); i++) {
- auto *slot = draw_order[i];
- if (!slot->getBone().isActive()) continue;
- auto *attachment = slot->getAttachment();
- if (!attachment) continue;
- if (!attachment->getRTTI().isExactly(spine::BoundingBoxAttachment::rtti)) continue;
- auto *bounding_box = (spine::BoundingBoxAttachment *) attachment;
- auto *vertices = &scratch_vertices;
- vertices->setSize(bounding_box->getWorldVerticesLength(), 0);
- bounding_box->computeWorldVertices(*slot, *vertices);
- size_t num_vertices = vertices->size() / 2;
- scratch_points.resize((int) num_vertices);
- memcpy(scratch_points.ptrw(), vertices->buffer(), num_vertices * 2 * sizeof(float));
- scratch_points.push_back(Vector2(vertices->buffer()[0], vertices->buffer()[1]));
- draw_polyline(scratch_points, debug_bounding_boxes_color, 2);
- }
- }
- if (debug_clipping) {
- draw_set_transform(Vector2(0, 0), 0, Vector2(1, 1));
- auto &draw_order = skeleton->get_spine_object()->getDrawOrder();
- for (int i = 0; i < (int) draw_order.size(); i++) {
- auto *slot = draw_order[i];
- if (!slot->getBone().isActive()) continue;
- auto *attachment = slot->getAttachment();
- if (!attachment) continue;
- if (!attachment->getRTTI().isExactly(spine::ClippingAttachment::rtti)) continue;
- auto *clipping = (spine::ClippingAttachment *) attachment;
- auto *vertices = &scratch_vertices;
- vertices->setSize(clipping->getWorldVerticesLength(), 0);
- clipping->computeWorldVertices(*slot, *vertices);
- size_t num_vertices = vertices->size() / 2;
- scratch_points.resize((int) num_vertices);
- memcpy(scratch_points.ptrw(), vertices->buffer(), num_vertices * 2 * sizeof(float));
- scratch_points.push_back(Vector2(vertices->buffer()[0], vertices->buffer()[1]));
- draw_polyline(scratch_points, debug_clipping_color, 2);
- }
- }
- spine::Bone *hovered_bone = nullptr;
- if (debug_root) {
- auto bone = skeleton->get_spine_object()->getRootBone();
- draw_bone(bone, debug_root_color);
- float bone_length = bone->getData().getLength();
- if (bone_length == 0) bone_length = debug_bones_thickness * 2;
- scratch_points.resize(5);
- scratch_points.set(0, Vector2(-debug_bones_thickness, 0));
- scratch_points.set(1, Vector2(0, debug_bones_thickness));
- scratch_points.set(2, Vector2(bone_length, 0));
- scratch_points.set(3, Vector2(0, -debug_bones_thickness));
- scratch_points.set(4, Vector2(-debug_bones_thickness, 0));
- Transform2D bone_transform(spine::MathUtil::Deg_Rad * bone->getWorldRotationX(), Vector2(bone->getWorldX(), bone->getWorldY()));
- bone_transform.scale_basis(Vector2(bone->getWorldScaleX(), bone->getWorldScaleY()));
- auto mouse_local_position = bone_transform.affine_inverse().xform(mouse_position);
- if (GEOMETRY2D::is_point_in_polygon(mouse_local_position, scratch_points)) {
- hovered_bone = bone;
- }
- }
- if (debug_bones) {
- auto &bones = skeleton->get_spine_object()->getBones();
- for (int i = 0; i < (int) bones.size(); i++) {
- auto *bone = bones[i];
- if (!bone->isActive()) continue;
- draw_bone(bone, debug_bones_color);
- float bone_length = bone->getData().getLength();
- if (bone_length == 0) bone_length = debug_bones_thickness * 2;
- scratch_points.resize(5);
- scratch_points.set(0, Vector2(-debug_bones_thickness, 0));
- scratch_points.set(1, Vector2(0, debug_bones_thickness));
- scratch_points.set(2, Vector2(bone_length, 0));
- scratch_points.set(3, Vector2(0, -debug_bones_thickness));
- scratch_points.set(4, Vector2(-debug_bones_thickness, 0));
- Transform2D bone_transform(spine::MathUtil::Deg_Rad * bone->getWorldRotationX(), Vector2(bone->getWorldX(), bone->getWorldY()));
- bone_transform.scale_basis(Vector2(bone->getWorldScaleX(), bone->getWorldScaleY()));
- auto mouse_local_position = bone_transform.affine_inverse().xform(mouse_position);
- if (GEOMETRY2D::is_point_in_polygon(mouse_local_position, scratch_points)) {
- hovered_bone = bone;
- }
- }
- }
- #if TOOLS_ENABLED
- Ref<Font> default_font;
- auto control = memnew(Control);
- #if VERSION_MAJOR > 3
- default_font = control->get_theme_default_font();
- #else
- default_font = control->get_font("font", "Label");
- #endif
- memfree(control);
- float editor_scale = EditorInterface::get_singleton()->get_editor_scale();
- float inverse_zoom = 1 / get_viewport()->get_global_canvas_transform().get_scale().x * editor_scale;
- Vector<String> hover_text_lines;
- if (hovered_slot) {
- hover_text_lines.push_back(String("Slot: ") + hovered_slot->getData().getName().buffer());
- }
- if (hovered_bone) {
- float thickness = debug_bones_thickness;
- debug_bones_thickness *= 1.1;
- draw_bone(hovered_bone, Color(debug_bones_color.r, debug_bones_color.g, debug_bones_color.b, 1));
- debug_bones_thickness = thickness;
- hover_text_lines.push_back(String("Bone: ") + hovered_bone->getData().getName().buffer());
- }
- auto global_scale = get_global_scale();
- draw_set_transform(mouse_position + Vector2(20, 0), -get_global_rotation(), Vector2(inverse_zoom * (1 / global_scale.x), inverse_zoom * (1 / global_scale.y)));
- #if VERSION_MAJOR > 3
- float line_height = default_font->get_height(Font::DEFAULT_FONT_SIZE) + default_font->get_descent(Font::DEFAULT_FONT_SIZE);
- #else
- float line_height = default_font->get_height() + default_font->get_descent();
- #endif
- float rect_width = 0;
- for (int i = 0; i < hover_text_lines.size(); i++) {
- rect_width = MAX(rect_width, default_font->get_string_size(hover_text_lines[i]).x);
- }
- #if VERSION_MAJOR > 3
- Rect2 background_rect(0, -default_font->get_height(Font::DEFAULT_FONT_SIZE) - 5, rect_width + 20, line_height * hover_text_lines.size() + 10);
- #else
- Rect2 background_rect(0, -default_font->get_height() - 5, rect_width + 20, line_height * hover_text_lines.size() + 10);
- #endif
- if (hover_text_lines.size() > 0) draw_rect(background_rect, Color(0, 0, 0, 0.8));
- for (int i = 0; i < hover_text_lines.size(); i++) {
- #if VERSION_MAJOR > 3
- draw_string(default_font, Vector2(10, 0 + i * default_font->get_height(Font::DEFAULT_FONT_SIZE)), hover_text_lines[i], HORIZONTAL_ALIGNMENT_LEFT, -1, Font::DEFAULT_FONT_SIZE, Color(1, 1, 1, 1));
- #else
- draw_string(default_font, Vector2(10, 0 + i * default_font->get_height()), hover_text_lines[i], Color(1, 1, 1, 1));
- #endif
- }
- #endif
- }
- void SpineSprite::draw_bone(spine::Bone *bone, const Color &color) {
- draw_set_transform(Vector2(bone->getWorldX(), bone->getWorldY()), spine::MathUtil::Deg_Rad * bone->getWorldRotationX(), Vector2(bone->getWorldScaleX(), bone->getWorldScaleY()));
- float bone_length = bone->getData().getLength();
- if (bone_length == 0) bone_length = debug_bones_thickness * 2;
- Vector<Vector2> points;
- points.push_back(Vector2(-debug_bones_thickness, 0));
- points.push_back(Vector2(0, debug_bones_thickness));
- points.push_back(Vector2(bone_length, 0));
- points.push_back(Vector2(0, -debug_bones_thickness));
- draw_colored_polygon(points, color);
- }
- void SpineSprite::callback(spine::AnimationState *state, spine::EventType type, spine::TrackEntry *entry, spine::Event *event) {
- Ref<SpineTrackEntry> entry_ref = Ref<SpineTrackEntry>(memnew(SpineTrackEntry));
- entry_ref->set_spine_object(this, entry);
- Ref<SpineEvent> event_ref(nullptr);
- if (event) {
- event_ref = Ref<SpineEvent>(memnew(SpineEvent));
- event_ref->set_spine_object(this, event);
- }
- switch (type) {
- case spine::EventType_Start:
- emit_signal("animation_started", this, animation_state, entry_ref);
- break;
- case spine::EventType_Interrupt:
- emit_signal("animation_interrupted", this, animation_state, entry_ref);
- break;
- case spine::EventType_End:
- emit_signal("animation_ended", this, animation_state, entry_ref);
- break;
- case spine::EventType_Complete:
- emit_signal("animation_completed", this, animation_state, entry_ref);
- break;
- case spine::EventType_Dispose:
- emit_signal("animation_disposed", this, animation_state, entry_ref);
- break;
- case spine::EventType_Event:
- emit_signal("animation_event", this, animation_state, entry_ref, event_ref);
- break;
- }
- }
- Transform2D SpineSprite::get_global_bone_transform(const String &bone_name) {
- if (!animation_state.is_valid() && !skeleton.is_valid()) return get_global_transform();
- auto bone = skeleton->find_bone(bone_name);
- if (!bone.is_valid()) {
- print_error(vformat("Bone: '%s' not found.", bone_name));
- return get_global_transform();
- }
- return bone->get_global_transform();
- }
- void SpineSprite::set_global_bone_transform(const String &bone_name, Transform2D transform) {
- if (!animation_state.is_valid() && !skeleton.is_valid()) return;
- auto bone = skeleton->find_bone(bone_name);
- if (!bone.is_valid()) return;
- bone->set_global_transform(transform);
- }
- SpineConstant::UpdateMode SpineSprite::get_update_mode() {
- return update_mode;
- }
- void SpineSprite::set_update_mode(SpineConstant::UpdateMode v) {
- update_mode = v;
- set_process_internal(update_mode == SpineConstant::UpdateMode_Process);
- set_physics_process_internal(update_mode == SpineConstant::UpdateMode_Physics);
- }
- Ref<SpineSkin> SpineSprite::new_skin(const String &name) {
- Ref<SpineSkin> skin = memnew(SpineSkin);
- skin->init(name, this);
- return skin;
- }
- Ref<Material> SpineSprite::get_normal_material() {
- return normal_material;
- }
- void SpineSprite::set_normal_material(Ref<Material> material) {
- normal_material = material;
- }
- Ref<Material> SpineSprite::get_additive_material() {
- return additive_material;
- }
- void SpineSprite::set_additive_material(Ref<Material> material) {
- additive_material = material;
- }
- Ref<Material> SpineSprite::get_multiply_material() {
- return multiply_material;
- }
- void SpineSprite::set_multiply_material(Ref<Material> material) {
- multiply_material = material;
- }
- Ref<Material> SpineSprite::get_screen_material() {
- return screen_material;
- }
- void SpineSprite::set_screen_material(Ref<Material> material) {
- screen_material = material;
- }
- #ifdef TOOLS_ENABLED
- Rect2 SpineSprite::_edit_get_rect() const {
- if (skeleton_data_res.is_valid() && skeleton_data_res->is_skeleton_data_loaded()) {
- auto data = skeleton_data_res->get_skeleton_data();
- return Rect2(data->getX(), -data->getY() - data->getHeight(), data->getWidth(), data->getHeight());
- }
- return Node2D::_edit_get_rect();
- }
- bool SpineSprite::_edit_use_rect() const {
- return skeleton_data_res.is_valid() && skeleton_data_res->is_skeleton_data_loaded();
- }
- #endif
|