2018-08-30 04:38:13 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* animation_blend_space_1d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-04 04:27:34 +08:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-08-30 04:38:13 +08:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2018-06-23 03:52:13 +08:00
|
|
|
#include "animation_blend_space_1d.h"
|
2018-06-22 23:13:43 +08:00
|
|
|
|
2018-08-21 00:38:18 +08:00
|
|
|
void AnimationNodeBlendSpace1D::get_parameter_list(List<PropertyInfo> *r_list) const {
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-25 02:20:53 +08:00
|
|
|
r_list->push_back(PropertyInfo(Variant::FLOAT, blend_position));
|
2018-08-21 00:38:18 +08:00
|
|
|
}
|
2020-05-14 20:29:06 +08:00
|
|
|
|
2018-08-21 00:38:18 +08:00
|
|
|
Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName &p_parameter) const {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-28 07:50:25 +08:00
|
|
|
|
2018-08-21 00:38:18 +08:00
|
|
|
Ref<AnimationNode> AnimationNodeBlendSpace1D::get_child_by_name(const StringName &p_name) {
|
|
|
|
return get_blend_point_node(p_name.operator String().to_int());
|
2018-06-28 07:50:25 +08:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:13:43 +08:00
|
|
|
void AnimationNodeBlendSpace1D::_validate_property(PropertyInfo &property) const {
|
|
|
|
if (property.name.begins_with("blend_point_")) {
|
|
|
|
String left = property.name.get_slicec('/', 0);
|
2018-06-23 03:52:13 +08:00
|
|
|
int idx = left.get_slicec('_', 2).to_int();
|
2018-06-22 23:13:43 +08:00
|
|
|
if (idx >= blend_points_used) {
|
2021-07-01 09:24:34 +08:00
|
|
|
property.usage = PROPERTY_USAGE_NONE;
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AnimationRootNode::_validate_property(property);
|
|
|
|
}
|
|
|
|
|
2018-08-21 00:38:18 +08:00
|
|
|
void AnimationNodeBlendSpace1D::_tree_changed() {
|
2021-07-18 05:22:52 +08:00
|
|
|
emit_signal(SNAME("tree_changed"));
|
2018-08-21 00:38:18 +08:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:13:43 +08:00
|
|
|
void AnimationNodeBlendSpace1D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace1D::add_blend_point, DEFVAL(-1));
|
|
|
|
ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace1D::set_blend_point_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace1D::get_blend_point_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace1D::set_blend_point_node);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace1D::get_blend_point_node);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace1D::remove_blend_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace1D::get_blend_point_count);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace1D::set_min_space);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace1D::get_min_space);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace1D::set_max_space);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace1D::get_max_space);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace1D::set_snap);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace1D::get_snap);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_value_label", "text"), &AnimationNodeBlendSpace1D::set_value_label);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_value_label"), &AnimationNodeBlendSpace1D::get_value_label);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace1D::_add_blend_point);
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
|
2021-11-04 06:06:17 +08:00
|
|
|
ADD_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);
|
|
|
|
ADD_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);
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
2021-11-04 06:06:17 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_min_space", "get_min_space");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_space", "get_max_space");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_snap", "get_snap");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "value_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_value_label", "get_value_label");
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
2018-08-21 00:38:18 +08:00
|
|
|
void AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {
|
2018-08-22 03:28:06 +08:00
|
|
|
for (int i = 0; i < blend_points_used; i++) {
|
2018-08-21 00:38:18 +08:00
|
|
|
ChildNode cn;
|
2018-08-22 03:28:06 +08:00
|
|
|
cn.name = itos(i);
|
|
|
|
cn.node = blend_points[i].node;
|
2018-08-21 00:38:18 +08:00
|
|
|
r_child_nodes->push_back(cn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:13:43 +08:00
|
|
|
void AnimationNodeBlendSpace1D::add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index) {
|
|
|
|
ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
|
|
|
|
ERR_FAIL_COND(p_node.is_null());
|
2018-08-21 00:38:18 +08:00
|
|
|
|
2018-06-22 23:13:43 +08:00
|
|
|
ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);
|
|
|
|
|
|
|
|
if (p_at_index == -1 || p_at_index == blend_points_used) {
|
|
|
|
p_at_index = blend_points_used;
|
|
|
|
} else {
|
2019-01-16 17:59:45 +08:00
|
|
|
for (int i = blend_points_used - 1; i > p_at_index; i--) {
|
2018-06-22 23:13:43 +08:00
|
|
|
blend_points[i] = blend_points[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blend_points[p_at_index].node = p_node;
|
|
|
|
blend_points[p_at_index].position = p_position;
|
|
|
|
|
2020-02-22 01:28:45 +08:00
|
|
|
blend_points[p_at_index].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED);
|
2018-06-22 23:13:43 +08:00
|
|
|
|
|
|
|
blend_points_used++;
|
2021-07-18 05:22:52 +08:00
|
|
|
emit_signal(SNAME("tree_changed"));
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationNodeBlendSpace1D::set_blend_point_position(int p_point, float p_position) {
|
|
|
|
ERR_FAIL_INDEX(p_point, blend_points_used);
|
|
|
|
|
|
|
|
blend_points[p_point].position = p_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationNodeBlendSpace1D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
|
|
|
|
ERR_FAIL_INDEX(p_point, blend_points_used);
|
|
|
|
ERR_FAIL_COND(p_node.is_null());
|
|
|
|
|
|
|
|
if (blend_points[p_point].node.is_valid()) {
|
2020-02-22 01:28:45 +08:00
|
|
|
blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
blend_points[p_point].node = p_node;
|
2020-02-22 01:28:45 +08:00
|
|
|
blend_points[p_point].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED);
|
2018-08-21 00:38:18 +08:00
|
|
|
|
2021-07-18 05:22:52 +08:00
|
|
|
emit_signal(SNAME("tree_changed"));
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationNodeBlendSpace1D::get_blend_point_position(int p_point) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_point, blend_points_used, 0);
|
|
|
|
return blend_points[p_point].position;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<AnimationRootNode> AnimationNodeBlendSpace1D::get_blend_point_node(int p_point) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_point, blend_points_used, Ref<AnimationRootNode>());
|
|
|
|
return blend_points[p_point].node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationNodeBlendSpace1D::remove_blend_point(int p_point) {
|
|
|
|
ERR_FAIL_INDEX(p_point, blend_points_used);
|
|
|
|
|
2019-11-01 23:16:31 +08:00
|
|
|
ERR_FAIL_COND(blend_points[p_point].node.is_null());
|
2020-02-22 01:28:45 +08:00
|
|
|
blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));
|
2018-06-22 23:13:43 +08:00
|
|
|
|
|
|
|
for (int i = p_point; i < blend_points_used - 1; i++) {
|
|
|
|
blend_points[i] = blend_points[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
blend_points_used--;
|
2021-07-18 05:22:52 +08:00
|
|
|
emit_signal(SNAME("tree_changed"));
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int AnimationNodeBlendSpace1D::get_blend_point_count() const {
|
|
|
|
return blend_points_used;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationNodeBlendSpace1D::set_min_space(float p_min) {
|
|
|
|
min_space = p_min;
|
|
|
|
|
|
|
|
if (min_space >= max_space) {
|
|
|
|
min_space = max_space - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationNodeBlendSpace1D::get_min_space() const {
|
|
|
|
return min_space;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationNodeBlendSpace1D::set_max_space(float p_max) {
|
|
|
|
max_space = p_max;
|
|
|
|
|
|
|
|
if (max_space <= min_space) {
|
|
|
|
max_space = min_space + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationNodeBlendSpace1D::get_max_space() const {
|
|
|
|
return max_space;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationNodeBlendSpace1D::set_snap(float p_snap) {
|
|
|
|
snap = p_snap;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationNodeBlendSpace1D::get_snap() const {
|
|
|
|
return snap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationNodeBlendSpace1D::set_value_label(const String &p_label) {
|
|
|
|
value_label = p_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
String AnimationNodeBlendSpace1D::get_value_label() const {
|
|
|
|
return value_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationNodeBlendSpace1D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
|
|
|
|
if (p_index == blend_points_used) {
|
|
|
|
add_blend_point(p_node, 0);
|
|
|
|
} else {
|
|
|
|
set_blend_point_node(p_index, p_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:42:37 +08:00
|
|
|
double AnimationNodeBlendSpace1D::process(double p_time, bool p_seek) {
|
2018-06-22 23:13:43 +08:00
|
|
|
if (blend_points_used == 0) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blend_points_used == 1) {
|
|
|
|
// only one point available, just play that animation
|
2018-08-22 03:28:06 +08:00
|
|
|
return blend_node(blend_points[0].name, blend_points[0].node, p_time, p_seek, 1.0, FILTER_IGNORE, false);
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:42:37 +08:00
|
|
|
double blend_pos = get_parameter(blend_position);
|
2018-08-21 00:38:18 +08:00
|
|
|
|
2018-06-26 08:41:36 +08:00
|
|
|
float weights[MAX_BLEND_POINTS] = {};
|
|
|
|
|
2018-06-22 23:13:43 +08:00
|
|
|
int point_lower = -1;
|
|
|
|
float pos_lower = 0.0;
|
|
|
|
int point_higher = -1;
|
|
|
|
float pos_higher = 0.0;
|
|
|
|
|
|
|
|
// find the closest two points to blend between
|
|
|
|
for (int i = 0; i < blend_points_used; i++) {
|
|
|
|
float pos = blend_points[i].position;
|
|
|
|
|
|
|
|
if (pos <= blend_pos) {
|
|
|
|
if (point_lower == -1) {
|
|
|
|
point_lower = i;
|
|
|
|
pos_lower = pos;
|
|
|
|
} else if ((blend_pos - pos) < (blend_pos - pos_lower)) {
|
|
|
|
point_lower = i;
|
|
|
|
pos_lower = pos;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (point_higher == -1) {
|
|
|
|
point_higher = i;
|
|
|
|
pos_higher = pos;
|
|
|
|
} else if ((pos - blend_pos) < (pos_higher - blend_pos)) {
|
|
|
|
point_higher = i;
|
|
|
|
pos_higher = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 08:41:36 +08:00
|
|
|
// fill in weights
|
|
|
|
|
2019-08-07 18:54:30 +08:00
|
|
|
if (point_lower == -1 && point_higher != -1) {
|
2018-06-22 23:13:43 +08:00
|
|
|
// we are on the left side, no other point to the left
|
|
|
|
// we just play the next point.
|
|
|
|
|
2018-06-26 08:41:36 +08:00
|
|
|
weights[point_higher] = 1.0;
|
2018-06-22 23:13:43 +08:00
|
|
|
} else if (point_higher == -1) {
|
|
|
|
// we are on the right side, no other point to the right
|
|
|
|
// we just play the previous point
|
2018-06-26 08:41:36 +08:00
|
|
|
|
|
|
|
weights[point_lower] = 1.0;
|
2018-06-22 23:13:43 +08:00
|
|
|
} else {
|
2018-06-26 08:41:36 +08:00
|
|
|
// we are between two points.
|
2018-06-22 23:13:43 +08:00
|
|
|
// figure out weights, then blend the animations
|
|
|
|
|
|
|
|
float distance_between_points = pos_higher - pos_lower;
|
|
|
|
|
|
|
|
float current_pos_inbetween = blend_pos - pos_lower;
|
|
|
|
|
|
|
|
float blend_percentage = current_pos_inbetween / distance_between_points;
|
|
|
|
|
|
|
|
float blend_lower = 1.0 - blend_percentage;
|
|
|
|
float blend_higher = blend_percentage;
|
|
|
|
|
2018-06-26 08:41:36 +08:00
|
|
|
weights[point_lower] = blend_lower;
|
|
|
|
weights[point_higher] = blend_higher;
|
|
|
|
}
|
|
|
|
|
|
|
|
// actually blend the animations now
|
|
|
|
|
2022-01-27 10:52:39 +08:00
|
|
|
double max_time_remaining = 0.0;
|
2018-06-22 23:13:43 +08:00
|
|
|
|
2018-06-26 08:41:36 +08:00
|
|
|
for (int i = 0; i < blend_points_used; i++) {
|
2022-01-27 10:52:39 +08:00
|
|
|
double remaining = blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, weights[i], FILTER_IGNORE, false);
|
2018-06-22 23:13:43 +08:00
|
|
|
|
2018-06-26 08:41:36 +08:00
|
|
|
max_time_remaining = MAX(max_time_remaining, remaining);
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
2018-06-26 08:41:36 +08:00
|
|
|
|
|
|
|
return max_time_remaining;
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String AnimationNodeBlendSpace1D::get_caption() const {
|
|
|
|
return "BlendSpace1D";
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {
|
2018-08-22 03:28:06 +08:00
|
|
|
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
|
|
|
|
blend_points[i].name = itos(i);
|
2018-08-21 00:38:18 +08:00
|
|
|
}
|
2018-06-22 23:13:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {
|
|
|
|
}
|