skeleton_modification_stack_2d.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*************************************************************************/
  2. /* skeleton_modification_stack_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "skeleton_modification_stack_2d.h"
  31. #include "scene/2d/skeleton_2d.h"
  32. void SkeletonModificationStack2D::_get_property_list(List<PropertyInfo> *p_list) const {
  33. for (int i = 0; i < modifications.size(); i++) {
  34. p_list->push_back(
  35. PropertyInfo(Variant::OBJECT, "modifications/" + itos(i),
  36. PROPERTY_HINT_RESOURCE_TYPE,
  37. "SkeletonModification2D",
  38. PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  39. }
  40. }
  41. bool SkeletonModificationStack2D::_set(const StringName &p_path, const Variant &p_value) {
  42. String path = p_path;
  43. if (path.begins_with("modifications/")) {
  44. int mod_idx = path.get_slicec('/', 1).to_int();
  45. set_modification(mod_idx, p_value);
  46. return true;
  47. }
  48. return true;
  49. }
  50. bool SkeletonModificationStack2D::_get(const StringName &p_path, Variant &r_ret) const {
  51. String path = p_path;
  52. if (path.begins_with("modifications/")) {
  53. int mod_idx = path.get_slicec('/', 1).to_int();
  54. r_ret = get_modification(mod_idx);
  55. return true;
  56. }
  57. return true;
  58. }
  59. void SkeletonModificationStack2D::setup() {
  60. if (is_setup) {
  61. return;
  62. }
  63. if (skeleton != nullptr) {
  64. is_setup = true;
  65. for (int i = 0; i < modifications.size(); i++) {
  66. if (!modifications[i].is_valid()) {
  67. continue;
  68. }
  69. modifications.get(i)->_setup_modification(this);
  70. }
  71. #ifdef TOOLS_ENABLED
  72. set_editor_gizmos_dirty(true);
  73. #endif // TOOLS_ENABLED
  74. } else {
  75. WARN_PRINT("Cannot setup SkeletonModificationStack2D: no Skeleton2D set!");
  76. }
  77. }
  78. void SkeletonModificationStack2D::execute(float p_delta, int p_execution_mode) {
  79. ERR_FAIL_COND_MSG(!is_setup || skeleton == nullptr || is_queued_for_deletion(),
  80. "Modification stack is not properly setup and therefore cannot execute!");
  81. if (!skeleton->is_inside_tree()) {
  82. ERR_PRINT_ONCE("Skeleton is not inside SceneTree! Cannot execute modification!");
  83. return;
  84. }
  85. if (!enabled) {
  86. return;
  87. }
  88. for (int i = 0; i < modifications.size(); i++) {
  89. if (!modifications[i].is_valid()) {
  90. continue;
  91. }
  92. if (modifications[i]->get_execution_mode() == p_execution_mode) {
  93. modifications.get(i)->_execute(p_delta);
  94. }
  95. }
  96. }
  97. void SkeletonModificationStack2D::draw_editor_gizmos() {
  98. if (!is_setup) {
  99. return;
  100. }
  101. if (editor_gizmo_dirty) {
  102. for (int i = 0; i < modifications.size(); i++) {
  103. if (!modifications[i].is_valid()) {
  104. continue;
  105. }
  106. if (modifications[i]->editor_draw_gizmo) {
  107. modifications.get(i)->_draw_editor_gizmo();
  108. }
  109. }
  110. skeleton->draw_set_transform(Vector2(0, 0));
  111. editor_gizmo_dirty = false;
  112. }
  113. }
  114. void SkeletonModificationStack2D::set_editor_gizmos_dirty(bool p_dirty) {
  115. if (!is_setup) {
  116. return;
  117. }
  118. if (!editor_gizmo_dirty && p_dirty) {
  119. editor_gizmo_dirty = p_dirty;
  120. if (skeleton) {
  121. skeleton->update();
  122. }
  123. } else {
  124. editor_gizmo_dirty = p_dirty;
  125. }
  126. }
  127. void SkeletonModificationStack2D::enable_all_modifications(bool p_enabled) {
  128. for (int i = 0; i < modifications.size(); i++) {
  129. if (!modifications[i].is_valid()) {
  130. continue;
  131. }
  132. modifications.get(i)->set_enabled(p_enabled);
  133. }
  134. }
  135. Ref<SkeletonModification2D> SkeletonModificationStack2D::get_modification(int p_mod_idx) const {
  136. ERR_FAIL_INDEX_V(p_mod_idx, modifications.size(), nullptr);
  137. return modifications[p_mod_idx];
  138. }
  139. void SkeletonModificationStack2D::add_modification(Ref<SkeletonModification2D> p_mod) {
  140. ERR_FAIL_COND(!p_mod.is_valid());
  141. p_mod->_setup_modification(this);
  142. modifications.push_back(p_mod);
  143. #ifdef TOOLS_ENABLED
  144. set_editor_gizmos_dirty(true);
  145. #endif // TOOLS_ENABLED
  146. }
  147. void SkeletonModificationStack2D::delete_modification(int p_mod_idx) {
  148. ERR_FAIL_INDEX(p_mod_idx, modifications.size());
  149. modifications.remove_at(p_mod_idx);
  150. #ifdef TOOLS_ENABLED
  151. set_editor_gizmos_dirty(true);
  152. #endif // TOOLS_ENABLED
  153. }
  154. void SkeletonModificationStack2D::set_modification(int p_mod_idx, Ref<SkeletonModification2D> p_mod) {
  155. ERR_FAIL_INDEX(p_mod_idx, modifications.size());
  156. if (p_mod == nullptr) {
  157. modifications.insert(p_mod_idx, nullptr);
  158. } else {
  159. p_mod->_setup_modification(this);
  160. modifications.insert(p_mod_idx, p_mod);
  161. }
  162. #ifdef TOOLS_ENABLED
  163. set_editor_gizmos_dirty(true);
  164. #endif // TOOLS_ENABLED
  165. }
  166. void SkeletonModificationStack2D::set_modification_count(int p_count) {
  167. ERR_FAIL_COND_MSG(p_count < 0, "Modification count cannot be less than zero.");
  168. modifications.resize(p_count);
  169. notify_property_list_changed();
  170. #ifdef TOOLS_ENABLED
  171. set_editor_gizmos_dirty(true);
  172. #endif // TOOLS_ENABLED
  173. }
  174. int SkeletonModificationStack2D::get_modification_count() const {
  175. return modifications.size();
  176. }
  177. void SkeletonModificationStack2D::set_skeleton(Skeleton2D *p_skeleton) {
  178. skeleton = p_skeleton;
  179. }
  180. Skeleton2D *SkeletonModificationStack2D::get_skeleton() const {
  181. return skeleton;
  182. }
  183. bool SkeletonModificationStack2D::get_is_setup() const {
  184. return is_setup;
  185. }
  186. void SkeletonModificationStack2D::set_enabled(bool p_enabled) {
  187. enabled = p_enabled;
  188. }
  189. bool SkeletonModificationStack2D::get_enabled() const {
  190. return enabled;
  191. }
  192. void SkeletonModificationStack2D::set_strength(float p_strength) {
  193. ERR_FAIL_COND_MSG(p_strength < 0, "Strength cannot be less than zero!");
  194. ERR_FAIL_COND_MSG(p_strength > 1, "Strength cannot be more than one!");
  195. strength = p_strength;
  196. }
  197. float SkeletonModificationStack2D::get_strength() const {
  198. return strength;
  199. }
  200. void SkeletonModificationStack2D::_bind_methods() {
  201. ClassDB::bind_method(D_METHOD("setup"), &SkeletonModificationStack2D::setup);
  202. ClassDB::bind_method(D_METHOD("execute", "delta", "execution_mode"), &SkeletonModificationStack2D::execute);
  203. ClassDB::bind_method(D_METHOD("enable_all_modifications", "enabled"), &SkeletonModificationStack2D::enable_all_modifications);
  204. ClassDB::bind_method(D_METHOD("get_modification", "mod_idx"), &SkeletonModificationStack2D::get_modification);
  205. ClassDB::bind_method(D_METHOD("add_modification", "modification"), &SkeletonModificationStack2D::add_modification);
  206. ClassDB::bind_method(D_METHOD("delete_modification", "mod_idx"), &SkeletonModificationStack2D::delete_modification);
  207. ClassDB::bind_method(D_METHOD("set_modification", "mod_idx", "modification"), &SkeletonModificationStack2D::set_modification);
  208. ClassDB::bind_method(D_METHOD("set_modification_count", "count"), &SkeletonModificationStack2D::set_modification_count);
  209. ClassDB::bind_method(D_METHOD("get_modification_count"), &SkeletonModificationStack2D::get_modification_count);
  210. ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModificationStack2D::get_is_setup);
  211. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModificationStack2D::set_enabled);
  212. ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModificationStack2D::get_enabled);
  213. ClassDB::bind_method(D_METHOD("set_strength", "strength"), &SkeletonModificationStack2D::set_strength);
  214. ClassDB::bind_method(D_METHOD("get_strength"), &SkeletonModificationStack2D::get_strength);
  215. ClassDB::bind_method(D_METHOD("get_skeleton"), &SkeletonModificationStack2D::get_skeleton);
  216. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");
  217. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "strength", PROPERTY_HINT_RANGE, "0, 1, 0.001"), "set_strength", "get_strength");
  218. ADD_PROPERTY(PropertyInfo(Variant::INT, "modification_count", PROPERTY_HINT_RANGE, "0, 100, 1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY, "Modifications,modifications/"), "set_modification_count", "get_modification_count");
  219. }
  220. SkeletonModificationStack2D::SkeletonModificationStack2D() {
  221. }