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