SpineAnimation.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated April 5, 2025. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2025, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #include "SpineAnimation.h"
  30. #include "SpineSkeleton.h"
  31. #include "SpineEvent.h"
  32. #include "SpineTimeline.h"
  33. #if VERSION_MAJOR == 3
  34. #include "core/method_bind_ext.gen.inc"
  35. #endif
  36. void SpineAnimation::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("get_name"), &SpineAnimation::get_name);
  38. ClassDB::bind_method(D_METHOD("get_duration"), &SpineAnimation::get_duration);
  39. ClassDB::bind_method(D_METHOD("set_duration", "duration"), &SpineAnimation::set_duration);
  40. ClassDB::bind_method(D_METHOD("apply", "skeleton", "last_time", "time", "loop", "events", "alpha", "blend", "direction"), &SpineAnimation::apply);
  41. ClassDB::bind_method(D_METHOD("get_timelines"), &SpineAnimation::get_timelines);
  42. ClassDB::bind_method(D_METHOD("has_timeline", "ids"), &SpineAnimation::has_timeline);
  43. }
  44. String SpineAnimation::get_name() {
  45. SPINE_CHECK(get_spine_object(), "")
  46. String name;
  47. name.parse_utf8(get_spine_object()->getName().buffer());
  48. return name;
  49. }
  50. float SpineAnimation::get_duration() {
  51. SPINE_CHECK(get_spine_object(), 0)
  52. return get_spine_object()->getDuration();
  53. }
  54. void SpineAnimation::set_duration(float duration) {
  55. SPINE_CHECK(get_spine_object(), )
  56. get_spine_object()->setDuration(duration);
  57. }
  58. void SpineAnimation::apply(Ref<SpineSkeleton> skeleton, float last_time, float time, bool loop,
  59. Array events, float alpha, SpineConstant::MixBlend blend,
  60. SpineConstant::MixDirection direction) {
  61. SPINE_CHECK(get_spine_object(), )
  62. spine::Vector<spine::Event *> spineEvents;
  63. get_spine_object()->apply(*(skeleton->get_spine_object()), last_time, time, loop, &spineEvents, alpha, (spine::MixBlend) blend, (spine::MixDirection) direction);
  64. for (int i = 0; i < (int) spineEvents.size(); ++i) {
  65. auto event_ref = memnew(SpineEvent);
  66. event_ref->set_spine_object(skeleton->get_spine_owner(), spineEvents[i]);
  67. events.append(event_ref);
  68. }
  69. }
  70. Array SpineAnimation::get_timelines() {
  71. Array result;
  72. SPINE_CHECK(get_spine_object(), result)
  73. auto &timelines = get_spine_object()->getTimelines();
  74. result.resize((int) timelines.size());
  75. for (int i = 0; i < (int) result.size(); ++i) {
  76. auto timeline_ref = Ref<SpineTimeline>(memnew(SpineTimeline));
  77. timeline_ref->set_spine_object(get_spine_owner(), timelines[i]);
  78. #ifdef SPINE_GODOT_EXTENSION
  79. result[i] = timeline_ref;
  80. #else
  81. result.set(i, timeline_ref);
  82. #endif
  83. }
  84. return result;
  85. }
  86. bool SpineAnimation::has_timeline(Array ids) {
  87. SPINE_CHECK(get_spine_object(), false)
  88. spine::Vector<spine::PropertyId> property_ids;
  89. property_ids.setSize(ids.size(), 0);
  90. for (int i = 0; i < (int) property_ids.size(); ++i) {
  91. property_ids[i] = (int64_t) ids[i];
  92. }
  93. return get_spine_object()->hasTimeline(property_ids);
  94. }