Path: blob/master/scene/resources/2d/skeleton/skeleton_modification_2d.cpp
9903 views
/**************************************************************************/1/* skeleton_modification_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_2d.h"31#include "scene/2d/skeleton_2d.h"3233#ifdef TOOLS_ENABLED34#include "editor/settings/editor_settings.h"35#endif // TOOLS_ENABLED3637///////////////////////////////////////38// Modification2D39///////////////////////////////////////4041void SkeletonModification2D::_execute(float p_delta) {42GDVIRTUAL_CALL(_execute, p_delta);4344if (!enabled) {45return;46}47}4849void SkeletonModification2D::_setup_modification(SkeletonModificationStack2D *p_stack) {50stack = p_stack;51if (stack) {52is_setup = true;53} else {54WARN_PRINT("Could not setup modification with name " + get_name());55}5657GDVIRTUAL_CALL(_setup_modification, Ref<SkeletonModificationStack2D>(p_stack));58}5960void SkeletonModification2D::_draw_editor_gizmo() {61GDVIRTUAL_CALL(_draw_editor_gizmo);62}6364void SkeletonModification2D::set_enabled(bool p_enabled) {65enabled = p_enabled;6667#ifdef TOOLS_ENABLED68if (editor_draw_gizmo) {69if (stack) {70stack->set_editor_gizmos_dirty(true);71}72}73#endif // TOOLS_ENABLED74}7576bool SkeletonModification2D::get_enabled() {77return enabled;78}7980float SkeletonModification2D::clamp_angle(float p_angle, float p_min_bound, float p_max_bound, bool p_invert) {81// Map to the 0 to 360 range (in radians though) instead of the -180 to 180 range.82if (p_angle < 0) {83p_angle = Math::TAU + p_angle;84}8586// Make min and max in the range of 0 to 360 (in radians), and make sure they are in the right order87if (p_min_bound < 0) {88p_min_bound = Math::TAU + p_min_bound;89}90if (p_max_bound < 0) {91p_max_bound = Math::TAU + p_max_bound;92}93if (p_min_bound > p_max_bound) {94SWAP(p_min_bound, p_max_bound);95}9697bool is_beyond_bounds = (p_angle < p_min_bound || p_angle > p_max_bound);98bool is_within_bounds = (p_angle > p_min_bound && p_angle < p_max_bound);99100// Note: May not be the most optimal way to clamp, but it always constraints to the nearest angle.101if ((!p_invert && is_beyond_bounds) || (p_invert && is_within_bounds)) {102Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));103Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));104Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));105106if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {107p_angle = p_min_bound;108} else {109p_angle = p_max_bound;110}111}112113return p_angle;114}115116void SkeletonModification2D::editor_draw_angle_constraints(Bone2D *p_operation_bone, float p_min_bound, float p_max_bound,117bool p_constraint_enabled, bool p_constraint_in_localspace, bool p_constraint_inverted) {118if (!p_operation_bone) {119return;120}121122Color bone_ik_color = Color(1.0, 0.65, 0.0, 0.4);123#ifdef TOOLS_ENABLED124if (Engine::get_singleton()->is_editor_hint()) {125bone_ik_color = EDITOR_GET("editors/2d/bone_ik_color");126}127#endif // TOOLS_ENABLED128129float arc_angle_min = p_min_bound;130float arc_angle_max = p_max_bound;131if (arc_angle_min < 0) {132arc_angle_min = (Math::PI * 2) + arc_angle_min;133}134if (arc_angle_max < 0) {135arc_angle_max = (Math::PI * 2) + arc_angle_max;136}137if (arc_angle_min > arc_angle_max) {138SWAP(arc_angle_min, arc_angle_max);139}140arc_angle_min += p_operation_bone->get_bone_angle();141arc_angle_max += p_operation_bone->get_bone_angle();142143if (p_constraint_enabled) {144if (p_constraint_in_localspace) {145Node *operation_bone_parent = p_operation_bone->get_parent();146Bone2D *operation_bone_parent_bone = Object::cast_to<Bone2D>(operation_bone_parent);147148if (operation_bone_parent_bone) {149stack->skeleton->draw_set_transform(150stack->skeleton->to_local(p_operation_bone->get_global_position()),151operation_bone_parent_bone->get_global_rotation() - stack->skeleton->get_global_rotation());152} else {153stack->skeleton->draw_set_transform(stack->skeleton->to_local(p_operation_bone->get_global_position()));154}155} else {156stack->skeleton->draw_set_transform(stack->skeleton->to_local(p_operation_bone->get_global_position()));157}158159if (p_constraint_inverted) {160stack->skeleton->draw_arc(Vector2(0, 0), p_operation_bone->get_length(),161arc_angle_min + (Math::PI * 2), arc_angle_max, 32, bone_ik_color, 1.0);162} else {163stack->skeleton->draw_arc(Vector2(0, 0), p_operation_bone->get_length(),164arc_angle_min, arc_angle_max, 32, bone_ik_color, 1.0);165}166stack->skeleton->draw_line(Vector2(0, 0), Vector2(Math::cos(arc_angle_min), Math::sin(arc_angle_min)) * p_operation_bone->get_length(), bone_ik_color, 1.0);167stack->skeleton->draw_line(Vector2(0, 0), Vector2(Math::cos(arc_angle_max), Math::sin(arc_angle_max)) * p_operation_bone->get_length(), bone_ik_color, 1.0);168169} else {170stack->skeleton->draw_set_transform(stack->skeleton->to_local(p_operation_bone->get_global_position()));171stack->skeleton->draw_arc(Vector2(0, 0), p_operation_bone->get_length(), 0, Math::PI * 2, 32, bone_ik_color, 1.0);172stack->skeleton->draw_line(Vector2(0, 0), Vector2(1, 0) * p_operation_bone->get_length(), bone_ik_color, 1.0);173}174}175176Ref<SkeletonModificationStack2D> SkeletonModification2D::get_modification_stack() {177return stack;178}179180void SkeletonModification2D::set_is_setup(bool p_setup) {181is_setup = p_setup;182}183184bool SkeletonModification2D::get_is_setup() const {185return is_setup;186}187188void SkeletonModification2D::set_execution_mode(int p_mode) {189execution_mode = p_mode;190}191192int SkeletonModification2D::get_execution_mode() const {193return execution_mode;194}195196void SkeletonModification2D::set_editor_draw_gizmo(bool p_draw_gizmo) {197editor_draw_gizmo = p_draw_gizmo;198#ifdef TOOLS_ENABLED199if (is_setup) {200if (stack) {201stack->set_editor_gizmos_dirty(true);202}203}204#endif // TOOLS_ENABLED205}206207bool SkeletonModification2D::get_editor_draw_gizmo() const {208return editor_draw_gizmo;209}210211void SkeletonModification2D::_bind_methods() {212GDVIRTUAL_BIND(_execute, "delta");213GDVIRTUAL_BIND(_setup_modification, "modification_stack")214GDVIRTUAL_BIND(_draw_editor_gizmo)215216ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModification2D::set_enabled);217ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModification2D::get_enabled);218ClassDB::bind_method(D_METHOD("get_modification_stack"), &SkeletonModification2D::get_modification_stack);219ClassDB::bind_method(D_METHOD("set_is_setup", "is_setup"), &SkeletonModification2D::set_is_setup);220ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModification2D::get_is_setup);221ClassDB::bind_method(D_METHOD("set_execution_mode", "execution_mode"), &SkeletonModification2D::set_execution_mode);222ClassDB::bind_method(D_METHOD("get_execution_mode"), &SkeletonModification2D::get_execution_mode);223ClassDB::bind_method(D_METHOD("clamp_angle", "angle", "min", "max", "invert"), &SkeletonModification2D::clamp_angle);224ClassDB::bind_method(D_METHOD("set_editor_draw_gizmo", "draw_gizmo"), &SkeletonModification2D::set_editor_draw_gizmo);225ClassDB::bind_method(D_METHOD("get_editor_draw_gizmo"), &SkeletonModification2D::get_editor_draw_gizmo);226227ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");228ADD_PROPERTY(PropertyInfo(Variant::INT, "execution_mode", PROPERTY_HINT_ENUM, "process,physics_process"), "set_execution_mode", "get_execution_mode");229}230231void SkeletonModification2D::reset_state() {232stack = nullptr;233is_setup = false;234}235236SkeletonModification2D::SkeletonModification2D() {237stack = nullptr;238is_setup = false;239}240241242