SpineSlotNode.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "SpineSlotNode.h"
  30. #ifdef TOOLS_ENABLED
  31. #ifdef SPINE_GODOT_EXTENSION
  32. // FIXME
  33. #else
  34. #include "editor/editor_node.h"
  35. #endif
  36. #endif
  37. #ifdef SPINE_GODOT_EXTENSION
  38. #include <godot_cpp/classes/viewport.hpp>
  39. #else
  40. #include "scene/main/viewport.h"
  41. #endif
  42. void SpineSlotNode::_bind_methods() {
  43. ClassDB::bind_method(D_METHOD("_on_world_transforms_changed", "spine_sprite"), &SpineSlotNode::on_world_transforms_changed);
  44. ClassDB::bind_method(D_METHOD("set_normal_material", "material"), &SpineSlotNode::set_normal_material);
  45. ClassDB::bind_method(D_METHOD("get_normal_material"), &SpineSlotNode::get_normal_material);
  46. ClassDB::bind_method(D_METHOD("set_additive_material", "material"), &SpineSlotNode::set_additive_material);
  47. ClassDB::bind_method(D_METHOD("get_additive_material"), &SpineSlotNode::get_additive_material);
  48. ClassDB::bind_method(D_METHOD("set_multiply_material", "material"), &SpineSlotNode::set_multiply_material);
  49. ClassDB::bind_method(D_METHOD("get_multiply_material"), &SpineSlotNode::get_multiply_material);
  50. ClassDB::bind_method(D_METHOD("set_screen_material", "material"), &SpineSlotNode::set_screen_material);
  51. ClassDB::bind_method(D_METHOD("get_screen_material"), &SpineSlotNode::get_screen_material);
  52. ADD_GROUP("Materials", "");
  53. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_normal_material", "get_normal_material");
  54. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "additive_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_additive_material", "get_additive_material");
  55. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiply_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_multiply_material", "get_multiply_material");
  56. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "screen_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_screen_material", "get_screen_material");
  57. }
  58. SpineSlotNode::SpineSlotNode() : slot_index(-1) {
  59. }
  60. void SpineSlotNode::_notification(int what) {
  61. switch (what) {
  62. case NOTIFICATION_PARENTED: {
  63. SpineSprite *sprite = cast_to<SpineSprite>(get_parent());
  64. if (sprite) {
  65. #if VERSION_MAJOR > 3
  66. sprite->connect(SNAME("world_transforms_changed"), callable_mp(this, &SpineSlotNode::on_world_transforms_changed));
  67. #else
  68. sprite->connect(SNAME("world_transforms_changed"), this, SNAME("_on_world_transforms_changed"));
  69. #endif
  70. update_transform(sprite);
  71. #if VERSION_MAJOR == 3
  72. _change_notify("transform/translation");
  73. _change_notify("transform/rotation");
  74. _change_notify("transform/scale");
  75. _change_notify("translation");
  76. _change_notify("rotation");
  77. _change_notify("rotation_deg");
  78. _change_notify("scale");
  79. #endif
  80. } else {
  81. WARN_PRINT("SpineSlotNode parent is not a SpineSprite.");
  82. }
  83. NOTIFY_PROPERTY_LIST_CHANGED();
  84. break;
  85. }
  86. case NOTIFICATION_UNPARENTED: {
  87. SpineSprite *sprite = cast_to<SpineSprite>(get_parent());
  88. if (sprite) {
  89. #if VERSION_MAJOR > 3
  90. sprite->disconnect(SNAME("world_transforms_changed"), callable_mp(this, &SpineSlotNode::on_world_transforms_changed));
  91. #else
  92. sprite->disconnect(SNAME("world_transforms_changed"), this, SNAME("_on_world_transforms_changed"));
  93. #endif
  94. }
  95. break;
  96. }
  97. default:
  98. break;
  99. }
  100. }
  101. void SpineSlotNode::_get_property_list(List<PropertyInfo> *list) const {
  102. #ifdef SPINE_GODOT_EXTENSION
  103. PackedStringArray slot_names;
  104. #else
  105. Vector<String> slot_names;
  106. #endif
  107. SpineSprite *sprite = cast_to<SpineSprite>(get_parent());
  108. if (sprite && sprite->get_skeleton_data_res().is_valid()) sprite->get_skeleton_data_res()->get_slot_names(slot_names);
  109. else
  110. slot_names.push_back(slot_name);
  111. auto element = list->front();
  112. while (element) {
  113. auto property_info = element->get();
  114. if (property_info.name == StringName("SpineSlotNode")) break;
  115. element = element->next();
  116. }
  117. PropertyInfo slot_name_property;
  118. slot_name_property.name = "slot_name";
  119. slot_name_property.type = Variant::STRING;
  120. slot_name_property.hint_string = String(",").join(slot_names);
  121. slot_name_property.hint = PROPERTY_HINT_ENUM;
  122. slot_name_property.usage = PROPERTY_USAGE_DEFAULT;
  123. list->insert_after(element, slot_name_property);
  124. }
  125. bool SpineSlotNode::_get(const StringName &property, Variant &value) const {
  126. if (property == StringName("slot_name")) {
  127. value = slot_name;
  128. return true;
  129. }
  130. return false;
  131. }
  132. bool SpineSlotNode::_set(const StringName &property, const Variant &value) {
  133. if (property == StringName("slot_name")) {
  134. slot_name = value;
  135. SpineSprite *sprite = cast_to<SpineSprite>(get_parent());
  136. update_transform(sprite);
  137. #if VERSION_MAJOR == 3
  138. _change_notify("transform/translation");
  139. _change_notify("transform/rotation");
  140. _change_notify("transform/scale");
  141. _change_notify("translation");
  142. _change_notify("rotation");
  143. _change_notify("rotation_deg");
  144. _change_notify("scale");
  145. #endif
  146. return true;
  147. }
  148. return false;
  149. }
  150. void SpineSlotNode::on_world_transforms_changed(const Variant &_sprite) {
  151. SpineSprite *sprite = cast_to<SpineSprite>(_sprite.operator Object *());
  152. update_transform(sprite);
  153. }
  154. void SpineSlotNode::update_transform(SpineSprite *sprite) {
  155. if (!is_visible_in_tree()) return;
  156. if (!sprite) return;
  157. if (!sprite->get_skeleton().is_valid() || !sprite->get_skeleton()->get_spine_object()) return;
  158. auto slot = sprite->get_skeleton()->find_slot(slot_name);
  159. if (!slot.is_valid()) {
  160. slot_index = -1;
  161. return;
  162. } else {
  163. slot_index = slot->get_data()->get_index();
  164. }
  165. auto bone = slot->get_bone();
  166. if (!bone.is_valid()) return;
  167. this->set_global_transform(bone->get_global_transform());
  168. }
  169. void SpineSlotNode::set_slot_name(const String &_slot_name) {
  170. slot_name = _slot_name;
  171. }
  172. String SpineSlotNode::get_slot_name() {
  173. return slot_name;
  174. }
  175. Ref<Material> SpineSlotNode::get_normal_material() {
  176. return normal_material;
  177. }
  178. void SpineSlotNode::set_normal_material(Ref<Material> material) {
  179. normal_material = material;
  180. }
  181. Ref<Material> SpineSlotNode::get_additive_material() {
  182. return additive_material;
  183. }
  184. void SpineSlotNode::set_additive_material(Ref<Material> material) {
  185. additive_material = material;
  186. }
  187. Ref<Material> SpineSlotNode::get_multiply_material() {
  188. return multiply_material;
  189. }
  190. void SpineSlotNode::set_multiply_material(Ref<Material> material) {
  191. multiply_material = material;
  192. }
  193. Ref<Material> SpineSlotNode::get_screen_material() {
  194. return screen_material;
  195. }
  196. void SpineSlotNode::set_screen_material(Ref<Material> material) {
  197. screen_material = material;
  198. }