skeleton_modification_2d_stackholder.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*************************************************************************/
  2. /* skeleton_modification_2d_stackholder.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_2d_stackholder.h"
  31. #include "scene/2d/skeleton_2d.h"
  32. bool SkeletonModification2DStackHolder::_set(const StringName &p_path, const Variant &p_value) {
  33. String path = p_path;
  34. if (path == "held_modification_stack") {
  35. set_held_modification_stack(p_value);
  36. }
  37. #ifdef TOOLS_ENABLED
  38. if (path == "editor/draw_gizmo") {
  39. set_editor_draw_gizmo(p_value);
  40. }
  41. #endif // TOOLS_ENABLED
  42. return true;
  43. }
  44. bool SkeletonModification2DStackHolder::_get(const StringName &p_path, Variant &r_ret) const {
  45. String path = p_path;
  46. if (path == "held_modification_stack") {
  47. r_ret = get_held_modification_stack();
  48. }
  49. #ifdef TOOLS_ENABLED
  50. if (path == "editor/draw_gizmo") {
  51. r_ret = get_editor_draw_gizmo();
  52. }
  53. #endif // TOOLS_ENABLED
  54. return true;
  55. }
  56. void SkeletonModification2DStackHolder::_get_property_list(List<PropertyInfo> *p_list) const {
  57. p_list->push_back(PropertyInfo(Variant::OBJECT, "held_modification_stack", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonModificationStack2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  58. #ifdef TOOLS_ENABLED
  59. if (Engine::get_singleton()->is_editor_hint()) {
  60. p_list->push_back(PropertyInfo(Variant::BOOL, "editor/draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  61. }
  62. #endif // TOOLS_ENABLED
  63. }
  64. void SkeletonModification2DStackHolder::_execute(float p_delta) {
  65. ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
  66. "Modification is not setup and therefore cannot execute!");
  67. if (held_modification_stack.is_valid()) {
  68. held_modification_stack->execute(p_delta, execution_mode);
  69. }
  70. }
  71. void SkeletonModification2DStackHolder::_setup_modification(SkeletonModificationStack2D *p_stack) {
  72. stack = p_stack;
  73. if (stack != nullptr) {
  74. is_setup = true;
  75. if (held_modification_stack.is_valid()) {
  76. held_modification_stack->set_skeleton(stack->get_skeleton());
  77. held_modification_stack->setup();
  78. }
  79. }
  80. }
  81. void SkeletonModification2DStackHolder::_draw_editor_gizmo() {
  82. if (stack) {
  83. if (held_modification_stack.is_valid()) {
  84. held_modification_stack->draw_editor_gizmos();
  85. }
  86. }
  87. }
  88. void SkeletonModification2DStackHolder::set_held_modification_stack(Ref<SkeletonModificationStack2D> p_held_stack) {
  89. held_modification_stack = p_held_stack;
  90. if (is_setup && held_modification_stack.is_valid()) {
  91. held_modification_stack->set_skeleton(stack->get_skeleton());
  92. held_modification_stack->setup();
  93. }
  94. }
  95. Ref<SkeletonModificationStack2D> SkeletonModification2DStackHolder::get_held_modification_stack() const {
  96. return held_modification_stack;
  97. }
  98. void SkeletonModification2DStackHolder::_bind_methods() {
  99. ClassDB::bind_method(D_METHOD("set_held_modification_stack", "held_modification_stack"), &SkeletonModification2DStackHolder::set_held_modification_stack);
  100. ClassDB::bind_method(D_METHOD("get_held_modification_stack"), &SkeletonModification2DStackHolder::get_held_modification_stack);
  101. }
  102. SkeletonModification2DStackHolder::SkeletonModification2DStackHolder() {
  103. stack = nullptr;
  104. is_setup = false;
  105. enabled = true;
  106. }
  107. SkeletonModification2DStackHolder::~SkeletonModification2DStackHolder() {
  108. }