Path: blob/master/scene/resources/2d/skeleton/skeleton_modification_stack_2d.cpp
9903 views
/**************************************************************************/1/* skeleton_modification_stack_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "skeleton_modification_stack_2d.h"31#include "scene/2d/skeleton_2d.h"3233void SkeletonModificationStack2D::_get_property_list(List<PropertyInfo> *p_list) const {34for (int i = 0; i < modifications.size(); i++) {35p_list->push_back(36PropertyInfo(Variant::OBJECT, "modifications/" + itos(i),37PROPERTY_HINT_RESOURCE_TYPE,38"SkeletonModification2D",39PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE));40}41}4243bool SkeletonModificationStack2D::_set(const StringName &p_path, const Variant &p_value) {44String path = p_path;4546if (path.begins_with("modifications/")) {47int mod_idx = path.get_slicec('/', 1).to_int();48set_modification(mod_idx, p_value);49return true;50}51return false;52}5354bool SkeletonModificationStack2D::_get(const StringName &p_path, Variant &r_ret) const {55String path = p_path;5657if (path.begins_with("modifications/")) {58int mod_idx = path.get_slicec('/', 1).to_int();59r_ret = get_modification(mod_idx);60return true;61}62return false;63}6465void SkeletonModificationStack2D::setup() {66if (is_setup) {67return;68}6970if (skeleton != nullptr) {71is_setup = true;72for (int i = 0; i < modifications.size(); i++) {73if (modifications[i].is_null()) {74continue;75}76modifications.get(i)->_setup_modification(this);77}7879#ifdef TOOLS_ENABLED80set_editor_gizmos_dirty(true);81#endif // TOOLS_ENABLED8283} else {84WARN_PRINT("Cannot setup SkeletonModificationStack2D: no Skeleton2D set!");85}86}8788void SkeletonModificationStack2D::execute(float p_delta, int p_execution_mode) {89ERR_FAIL_COND_MSG(!is_setup || skeleton == nullptr || is_queued_for_deletion(),90"Modification stack is not properly setup and therefore cannot execute!");9192if (!skeleton->is_inside_tree()) {93ERR_PRINT_ONCE("Skeleton is not inside SceneTree! Cannot execute modification!");94return;95}9697if (!enabled) {98return;99}100101for (int i = 0; i < modifications.size(); i++) {102if (modifications[i].is_null()) {103continue;104}105106if (modifications[i]->get_execution_mode() == p_execution_mode) {107modifications.get(i)->_execute(p_delta);108}109}110}111112void SkeletonModificationStack2D::draw_editor_gizmos() {113if (!is_setup) {114return;115}116117if (editor_gizmo_dirty) {118for (int i = 0; i < modifications.size(); i++) {119if (modifications[i].is_null()) {120continue;121}122123if (modifications[i]->editor_draw_gizmo) {124modifications.get(i)->_draw_editor_gizmo();125}126}127skeleton->draw_set_transform(Vector2(0, 0));128editor_gizmo_dirty = false;129}130}131132void SkeletonModificationStack2D::set_editor_gizmos_dirty(bool p_dirty) {133if (!is_setup) {134return;135}136137if (!editor_gizmo_dirty && p_dirty) {138editor_gizmo_dirty = p_dirty;139if (skeleton) {140skeleton->queue_redraw();141}142} else {143editor_gizmo_dirty = p_dirty;144}145}146147void SkeletonModificationStack2D::enable_all_modifications(bool p_enabled) {148for (int i = 0; i < modifications.size(); i++) {149if (modifications[i].is_null()) {150continue;151}152modifications.get(i)->set_enabled(p_enabled);153}154}155156Ref<SkeletonModification2D> SkeletonModificationStack2D::get_modification(int p_mod_idx) const {157ERR_FAIL_INDEX_V(p_mod_idx, modifications.size(), nullptr);158return modifications[p_mod_idx];159}160161void SkeletonModificationStack2D::add_modification(Ref<SkeletonModification2D> p_mod) {162ERR_FAIL_COND(p_mod.is_null());163164p_mod->_setup_modification(this);165modifications.push_back(p_mod);166167#ifdef TOOLS_ENABLED168set_editor_gizmos_dirty(true);169#endif // TOOLS_ENABLED170}171172void SkeletonModificationStack2D::delete_modification(int p_mod_idx) {173ERR_FAIL_INDEX(p_mod_idx, modifications.size());174modifications.remove_at(p_mod_idx);175176#ifdef TOOLS_ENABLED177set_editor_gizmos_dirty(true);178#endif // TOOLS_ENABLED179}180181void SkeletonModificationStack2D::set_modification(int p_mod_idx, Ref<SkeletonModification2D> p_mod) {182ERR_FAIL_INDEX(p_mod_idx, modifications.size());183184if (p_mod.is_null()) {185modifications.write[p_mod_idx] = Ref<SkeletonModification2D>();186} else {187modifications.write[p_mod_idx] = p_mod;188p_mod->_setup_modification(this);189}190191#ifdef TOOLS_ENABLED192set_editor_gizmos_dirty(true);193#endif // TOOLS_ENABLED194}195196void SkeletonModificationStack2D::set_modification_count(int p_count) {197ERR_FAIL_COND_MSG(p_count < 0, "Modification count cannot be less than zero.");198modifications.resize(p_count);199notify_property_list_changed();200201#ifdef TOOLS_ENABLED202set_editor_gizmos_dirty(true);203#endif // TOOLS_ENABLED204}205206int SkeletonModificationStack2D::get_modification_count() const {207return modifications.size();208}209210void SkeletonModificationStack2D::set_skeleton(Skeleton2D *p_skeleton) {211skeleton = p_skeleton;212}213214Skeleton2D *SkeletonModificationStack2D::get_skeleton() const {215return skeleton;216}217218bool SkeletonModificationStack2D::get_is_setup() const {219return is_setup;220}221222void SkeletonModificationStack2D::set_enabled(bool p_enabled) {223enabled = p_enabled;224}225226bool SkeletonModificationStack2D::get_enabled() const {227return enabled;228}229230void SkeletonModificationStack2D::set_strength(float p_strength) {231ERR_FAIL_COND_MSG(p_strength < 0, "Strength cannot be less than zero!");232ERR_FAIL_COND_MSG(p_strength > 1, "Strength cannot be more than one!");233strength = p_strength;234}235236float SkeletonModificationStack2D::get_strength() const {237return strength;238}239240void SkeletonModificationStack2D::_bind_methods() {241ClassDB::bind_method(D_METHOD("setup"), &SkeletonModificationStack2D::setup);242ClassDB::bind_method(D_METHOD("execute", "delta", "execution_mode"), &SkeletonModificationStack2D::execute);243244ClassDB::bind_method(D_METHOD("enable_all_modifications", "enabled"), &SkeletonModificationStack2D::enable_all_modifications);245ClassDB::bind_method(D_METHOD("get_modification", "mod_idx"), &SkeletonModificationStack2D::get_modification);246ClassDB::bind_method(D_METHOD("add_modification", "modification"), &SkeletonModificationStack2D::add_modification);247ClassDB::bind_method(D_METHOD("delete_modification", "mod_idx"), &SkeletonModificationStack2D::delete_modification);248ClassDB::bind_method(D_METHOD("set_modification", "mod_idx", "modification"), &SkeletonModificationStack2D::set_modification);249250ClassDB::bind_method(D_METHOD("set_modification_count", "count"), &SkeletonModificationStack2D::set_modification_count);251ClassDB::bind_method(D_METHOD("get_modification_count"), &SkeletonModificationStack2D::get_modification_count);252253ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModificationStack2D::get_is_setup);254255ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModificationStack2D::set_enabled);256ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModificationStack2D::get_enabled);257258ClassDB::bind_method(D_METHOD("set_strength", "strength"), &SkeletonModificationStack2D::set_strength);259ClassDB::bind_method(D_METHOD("get_strength"), &SkeletonModificationStack2D::get_strength);260261ClassDB::bind_method(D_METHOD("get_skeleton"), &SkeletonModificationStack2D::get_skeleton);262263ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");264ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "strength", PROPERTY_HINT_RANGE, "0, 1, 0.001"), "set_strength", "get_strength");265ADD_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");266}267268SkeletonModificationStack2D::SkeletonModificationStack2D() {269}270271272