Path: blob/master/scene/animation/animation_blend_space_1d.cpp
9896 views
/**************************************************************************/1/* animation_blend_space_1d.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 "animation_blend_space_1d.h"3132#include "animation_blend_tree.h"3334void AnimationNodeBlendSpace1D::get_parameter_list(List<PropertyInfo> *r_list) const {35AnimationNode::get_parameter_list(r_list);36r_list->push_back(PropertyInfo(Variant::FLOAT, blend_position));37r_list->push_back(PropertyInfo(Variant::INT, closest, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));38}3940Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName &p_parameter) const {41Variant ret = AnimationNode::get_parameter_default_value(p_parameter);42if (ret != Variant()) {43return ret;44}4546if (p_parameter == closest) {47return (int)-1;48} else {49return 0.0;50}51}5253Ref<AnimationNode> AnimationNodeBlendSpace1D::get_child_by_name(const StringName &p_name) const {54return get_blend_point_node(p_name.operator String().to_int());55}5657void AnimationNodeBlendSpace1D::_validate_property(PropertyInfo &p_property) const {58if (p_property.name.begins_with("blend_point_")) {59String left = p_property.name.get_slicec('/', 0);60int idx = left.get_slicec('_', 2).to_int();61if (idx >= blend_points_used) {62p_property.usage = PROPERTY_USAGE_NONE;63}64}65}6667void AnimationNodeBlendSpace1D::_tree_changed() {68AnimationRootNode::_tree_changed();69}7071void AnimationNodeBlendSpace1D::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {72AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);73}7475void AnimationNodeBlendSpace1D::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {76AnimationRootNode::_animation_node_removed(p_oid, p_node);77}7879void AnimationNodeBlendSpace1D::_bind_methods() {80ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace1D::add_blend_point, DEFVAL(-1));81ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace1D::set_blend_point_position);82ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace1D::get_blend_point_position);83ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace1D::set_blend_point_node);84ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace1D::get_blend_point_node);85ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace1D::remove_blend_point);86ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace1D::get_blend_point_count);8788ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace1D::set_min_space);89ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace1D::get_min_space);9091ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace1D::set_max_space);92ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace1D::get_max_space);9394ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace1D::set_snap);95ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace1D::get_snap);9697ClassDB::bind_method(D_METHOD("set_value_label", "text"), &AnimationNodeBlendSpace1D::set_value_label);98ClassDB::bind_method(D_METHOD("get_value_label"), &AnimationNodeBlendSpace1D::get_value_label);99100ClassDB::bind_method(D_METHOD("set_blend_mode", "mode"), &AnimationNodeBlendSpace1D::set_blend_mode);101ClassDB::bind_method(D_METHOD("get_blend_mode"), &AnimationNodeBlendSpace1D::get_blend_mode);102103ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeBlendSpace1D::set_use_sync);104ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeBlendSpace1D::is_using_sync);105106ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace1D::_add_blend_point);107108for (int i = 0; i < MAX_BLEND_POINTS; i++) {109ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point", "get_blend_point_node", i);110ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);111}112113ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_min_space", "get_min_space");114ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_space", "get_max_space");115ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_snap", "get_snap");116ADD_PROPERTY(PropertyInfo(Variant::STRING, "value_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_value_label", "get_value_label");117ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Interpolated,Discrete,Carry", PROPERTY_USAGE_NO_EDITOR), "set_blend_mode", "get_blend_mode");118ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_sync", "is_using_sync");119120BIND_ENUM_CONSTANT(BLEND_MODE_INTERPOLATED);121BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE);122BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE_CARRY);123}124125void AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {126for (int i = 0; i < blend_points_used; i++) {127ChildNode cn;128cn.name = itos(i);129cn.node = blend_points[i].node;130r_child_nodes->push_back(cn);131}132}133134void AnimationNodeBlendSpace1D::add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index) {135ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);136ERR_FAIL_COND(p_node.is_null());137138ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);139140if (p_at_index == -1 || p_at_index == blend_points_used) {141p_at_index = blend_points_used;142} else {143for (int i = blend_points_used - 1; i > p_at_index; i--) {144blend_points[i] = blend_points[i - 1];145}146}147148blend_points[p_at_index].node = p_node;149blend_points[p_at_index].position = p_position;150151blend_points[p_at_index].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), CONNECT_REFERENCE_COUNTED);152blend_points[p_at_index].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);153blend_points[p_at_index].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);154155blend_points_used++;156emit_signal(SNAME("tree_changed"));157}158159void AnimationNodeBlendSpace1D::set_blend_point_position(int p_point, float p_position) {160ERR_FAIL_INDEX(p_point, blend_points_used);161162blend_points[p_point].position = p_position;163}164165void AnimationNodeBlendSpace1D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {166ERR_FAIL_INDEX(p_point, blend_points_used);167ERR_FAIL_COND(p_node.is_null());168169if (blend_points[p_point].node.is_valid()) {170blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));171blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed));172blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed));173}174175blend_points[p_point].node = p_node;176blend_points[p_point].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), CONNECT_REFERENCE_COUNTED);177blend_points[p_point].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);178blend_points[p_point].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);179180emit_signal(SNAME("tree_changed"));181}182183float AnimationNodeBlendSpace1D::get_blend_point_position(int p_point) const {184ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, 0);185return blend_points[p_point].position;186}187188Ref<AnimationRootNode> AnimationNodeBlendSpace1D::get_blend_point_node(int p_point) const {189ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, Ref<AnimationRootNode>());190return blend_points[p_point].node;191}192193void AnimationNodeBlendSpace1D::remove_blend_point(int p_point) {194ERR_FAIL_INDEX(p_point, blend_points_used);195196ERR_FAIL_COND(blend_points[p_point].node.is_null());197blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));198blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed));199blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed));200201for (int i = p_point; i < blend_points_used - 1; i++) {202blend_points[i] = blend_points[i + 1];203}204205blend_points_used--;206207emit_signal(SNAME("animation_node_removed"), get_instance_id(), itos(p_point));208emit_signal(SNAME("tree_changed"));209}210211int AnimationNodeBlendSpace1D::get_blend_point_count() const {212return blend_points_used;213}214215void AnimationNodeBlendSpace1D::set_min_space(float p_min) {216min_space = p_min;217218if (min_space >= max_space) {219min_space = max_space - 1;220}221}222223float AnimationNodeBlendSpace1D::get_min_space() const {224return min_space;225}226227void AnimationNodeBlendSpace1D::set_max_space(float p_max) {228max_space = p_max;229230if (max_space <= min_space) {231max_space = min_space + 1;232}233}234235float AnimationNodeBlendSpace1D::get_max_space() const {236return max_space;237}238239void AnimationNodeBlendSpace1D::set_snap(float p_snap) {240snap = p_snap;241}242243float AnimationNodeBlendSpace1D::get_snap() const {244return snap;245}246247void AnimationNodeBlendSpace1D::set_value_label(const String &p_label) {248value_label = p_label;249}250251String AnimationNodeBlendSpace1D::get_value_label() const {252return value_label;253}254255void AnimationNodeBlendSpace1D::set_blend_mode(BlendMode p_blend_mode) {256blend_mode = p_blend_mode;257}258259AnimationNodeBlendSpace1D::BlendMode AnimationNodeBlendSpace1D::get_blend_mode() const {260return blend_mode;261}262263void AnimationNodeBlendSpace1D::set_use_sync(bool p_sync) {264sync = p_sync;265}266267bool AnimationNodeBlendSpace1D::is_using_sync() const {268return sync;269}270271void AnimationNodeBlendSpace1D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {272if (p_index == blend_points_used) {273add_blend_point(p_node, 0);274} else {275set_blend_point_node(p_index, p_node);276}277}278279AnimationNode::NodeTimeInfo AnimationNodeBlendSpace1D::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {280if (!blend_points_used) {281return NodeTimeInfo();282}283284AnimationMixer::PlaybackInfo pi = p_playback_info;285286if (blend_points_used == 1) {287// only one point available, just play that animation288pi.weight = 1.0;289return blend_node(blend_points[0].node, blend_points[0].name, pi, FILTER_IGNORE, true, p_test_only);290}291292double blend_pos = get_parameter(blend_position);293int cur_closest = get_parameter(closest);294NodeTimeInfo mind;295296if (blend_mode == BLEND_MODE_INTERPOLATED) {297int point_lower = -1;298float pos_lower = 0.0;299int point_higher = -1;300float pos_higher = 0.0;301302// find the closest two points to blend between303for (int i = 0; i < blend_points_used; i++) {304float pos = blend_points[i].position;305306if (pos <= blend_pos) {307if (point_lower == -1 || pos > pos_lower) {308point_lower = i;309pos_lower = pos;310}311} else if (point_higher == -1 || pos < pos_higher) {312point_higher = i;313pos_higher = pos;314}315}316317// fill in weights318float weights[MAX_BLEND_POINTS] = {};319if (point_lower == -1 && point_higher != -1) {320// we are on the left side, no other point to the left321// we just play the next point.322323weights[point_higher] = 1.0;324} else if (point_higher == -1) {325// we are on the right side, no other point to the right326// we just play the previous point327328weights[point_lower] = 1.0;329} else {330// we are between two points.331// figure out weights, then blend the animations332333float distance_between_points = pos_higher - pos_lower;334335float current_pos_inbetween = blend_pos - pos_lower;336337float blend_percentage = current_pos_inbetween / distance_between_points;338339float blend_lower = 1.0 - blend_percentage;340float blend_higher = blend_percentage;341342weights[point_lower] = blend_lower;343weights[point_higher] = blend_higher;344}345346// actually blend the animations now347bool first = true;348double max_weight = 0.0;349for (int i = 0; i < blend_points_used; i++) {350if (i == point_lower || i == point_higher) {351pi.weight = weights[i];352NodeTimeInfo t = blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);353if (first || pi.weight > max_weight) {354max_weight = pi.weight;355mind = t;356first = false;357}358} else if (sync) {359pi.weight = 0;360blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);361}362}363} else {364int new_closest = -1;365double new_closest_dist = 1e20;366367for (int i = 0; i < blend_points_used; i++) {368double d = std::abs(blend_points[i].position - blend_pos);369if (d < new_closest_dist) {370new_closest = i;371new_closest_dist = d;372}373}374375if (new_closest != cur_closest && new_closest != -1) {376if (blend_mode == BLEND_MODE_DISCRETE_CARRY && cur_closest != -1) {377NodeTimeInfo from;378// For ping-pong loop.379Ref<AnimationNodeAnimation> na_c = static_cast<Ref<AnimationNodeAnimation>>(blend_points[cur_closest].node);380Ref<AnimationNodeAnimation> na_n = static_cast<Ref<AnimationNodeAnimation>>(blend_points[new_closest].node);381if (na_c.is_valid() && na_n.is_valid()) {382na_n->process_state = process_state;383na_c->process_state = process_state;384385na_n->set_backward(na_c->is_backward());386387na_n = nullptr;388na_c = nullptr;389}390// See how much animation remains.391pi.seeked = false;392pi.weight = 0;393from = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, true);394pi.time = from.position;395}396pi.seeked = true;397pi.weight = 1.0;398mind = blend_node(blend_points[new_closest].node, blend_points[new_closest].name, pi, FILTER_IGNORE, true, p_test_only);399cur_closest = new_closest;400} else {401pi.weight = 1.0;402mind = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, p_test_only);403}404405if (sync) {406pi = p_playback_info;407pi.weight = 0;408for (int i = 0; i < blend_points_used; i++) {409if (i != cur_closest) {410blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);411}412}413}414}415416set_parameter(closest, cur_closest);417return mind;418}419420String AnimationNodeBlendSpace1D::get_caption() const {421return "BlendSpace1D";422}423424AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {425for (int i = 0; i < MAX_BLEND_POINTS; i++) {426blend_points[i].name = itos(i);427}428}429430AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {431}432433434