Removed AnimationTree.

This commit is contained in:
Relintai 2023-12-16 01:09:49 +01:00
parent b9e6090c65
commit 14272c1eaf
11 changed files with 1 additions and 6119 deletions

View File

@ -1,326 +0,0 @@
/*************************************************************************/
/* animation_blend_space_1d.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "animation_blend_space_1d.h"
void AnimationNodeBlendSpace1D::get_parameter_list(List<PropertyInfo> *r_list) const {
r_list->push_back(PropertyInfo(Variant::REAL, blend_position));
}
Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName &p_parameter) const {
return 0;
}
Ref<AnimationNode> AnimationNodeBlendSpace1D::get_child_by_name(const StringName &p_name) {
return get_blend_point_node(p_name.operator String().to_int());
}
void AnimationNodeBlendSpace1D::_validate_property(PropertyInfo &property) const {
if (property.name.begins_with("blend_point_")) {
String left = property.name.get_slicec('/', 0);
int idx = left.get_slicec('_', 2).to_int();
if (idx >= blend_points_used) {
property.usage = 0;
}
}
AnimationRootNode::_validate_property(property);
}
void AnimationNodeBlendSpace1D::_tree_changed() {
emit_signal("tree_changed");
}
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);
ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeBlendSpace1D::_tree_changed);
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point", "get_blend_point_node", i);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
}
ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_min_space", "get_min_space");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_max_space", "get_max_space");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_snap", "get_snap");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "value_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_value_label", "get_value_label");
}
void AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {
for (int i = 0; i < blend_points_used; i++) {
ChildNode cn;
cn.name = itos(i);
cn.node = blend_points[i].node;
r_child_nodes->push_back(cn);
}
}
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());
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 {
for (int i = blend_points_used - 1; i > p_at_index; i--) {
blend_points[i] = blend_points[i - 1];
}
}
blend_points[p_at_index].node = p_node;
blend_points[p_at_index].position = p_position;
blend_points[p_at_index].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
blend_points_used++;
emit_signal("tree_changed");
}
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()) {
blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
}
blend_points[p_point].node = p_node;
blend_points[p_point].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
emit_signal("tree_changed");
}
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);
ERR_FAIL_COND(blend_points[p_point].node.is_null());
blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
for (int i = p_point; i < blend_points_used - 1; i++) {
blend_points[i] = blend_points[i + 1];
}
blend_points_used--;
emit_signal("tree_changed");
}
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);
}
}
float AnimationNodeBlendSpace1D::process(float p_time, bool p_seek) {
if (blend_points_used == 0) {
return 0.0;
}
if (blend_points_used == 1) {
// only one point available, just play that animation
return blend_node(blend_points[0].name, blend_points[0].node, p_time, p_seek, 1.0, FILTER_IGNORE, false);
}
float blend_pos = get_parameter(blend_position);
float weights[MAX_BLEND_POINTS] = {};
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;
}
}
}
// fill in weights
if (point_lower == -1 && point_higher != -1) {
// we are on the left side, no other point to the left
// we just play the next point.
weights[point_higher] = 1.0;
} else if (point_higher == -1) {
// we are on the right side, no other point to the right
// we just play the previous point
weights[point_lower] = 1.0;
} else {
// we are between two points.
// 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;
weights[point_lower] = blend_lower;
weights[point_higher] = blend_higher;
}
// actually blend the animations now
float max_time_remaining = 0.0;
for (int i = 0; i < blend_points_used; i++) {
float remaining = blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, weights[i], FILTER_IGNORE, false);
max_time_remaining = MAX(max_time_remaining, remaining);
}
return max_time_remaining;
}
String AnimationNodeBlendSpace1D::get_caption() const {
return "BlendSpace1D";
}
AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
blend_points[i].name = itos(i);
}
blend_points_used = 0;
max_space = 1;
min_space = -1;
snap = 0.1;
value_label = "value";
blend_position = "blend_position";
}
AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {
}

View File

@ -1,104 +0,0 @@
#ifndef ANIMATION_BLEND_SPACE_1D_H
#define ANIMATION_BLEND_SPACE_1D_H
/*************************************************************************/
/* animation_blend_space_1d.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "scene/animation/animation_tree.h"
class AnimationNodeBlendSpace1D : public AnimationRootNode {
GDCLASS(AnimationNodeBlendSpace1D, AnimationRootNode);
enum {
MAX_BLEND_POINTS = 64
};
struct BlendPoint {
StringName name;
Ref<AnimationRootNode> node;
float position;
};
BlendPoint blend_points[MAX_BLEND_POINTS];
int blend_points_used;
float max_space;
float min_space;
float snap;
String value_label;
void _add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node);
void _tree_changed();
StringName blend_position;
protected:
virtual void _validate_property(PropertyInfo &property) const;
static void _bind_methods();
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
void add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index = -1);
void set_blend_point_position(int p_point, float p_position);
void set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node);
float get_blend_point_position(int p_point) const;
Ref<AnimationRootNode> get_blend_point_node(int p_point) const;
void remove_blend_point(int p_point);
int get_blend_point_count() const;
void set_min_space(float p_min);
float get_min_space() const;
void set_max_space(float p_max);
float get_max_space() const;
void set_snap(float p_snap);
float get_snap() const;
void set_value_label(const String &p_label);
String get_value_label() const;
float process(float p_time, bool p_seek);
String get_caption() const;
Ref<AnimationNode> get_child_by_name(const StringName &p_name);
AnimationNodeBlendSpace1D();
~AnimationNodeBlendSpace1D();
};
#endif // ANIMATION_BLEND_SPACE_1D_H

View File

@ -1,672 +0,0 @@
/*************************************************************************/
/* animation_blend_space_2d.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "animation_blend_space_2d.h"
#include "core/math/delaunay.h"
#include "core/math/geometry.h"
void AnimationNodeBlendSpace2D::get_parameter_list(List<PropertyInfo> *r_list) const {
r_list->push_back(PropertyInfo(Variant::VECTOR2, blend_position));
r_list->push_back(PropertyInfo(Variant::INT, closest, PROPERTY_HINT_NONE, "", 0));
r_list->push_back(PropertyInfo(Variant::REAL, length_internal, PROPERTY_HINT_NONE, "", 0));
}
Variant AnimationNodeBlendSpace2D::get_parameter_default_value(const StringName &p_parameter) const {
if (p_parameter == closest) {
return -1;
} else if (p_parameter == length_internal) {
return 0;
} else {
return Vector2();
}
}
void AnimationNodeBlendSpace2D::get_child_nodes(List<ChildNode> *r_child_nodes) {
for (int i = 0; i < blend_points_used; i++) {
ChildNode cn;
cn.name = itos(i);
cn.node = blend_points[i].node;
r_child_nodes->push_back(cn);
}
}
void AnimationNodeBlendSpace2D::add_blend_point(const Ref<AnimationRootNode> &p_node, const Vector2 &p_position, int p_at_index) {
ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
ERR_FAIL_COND(p_node.is_null());
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 {
for (int i = blend_points_used - 1; i > p_at_index; i--) {
blend_points[i] = blend_points[i - 1];
}
for (int i = 0; i < triangles.size(); i++) {
for (int j = 0; j < 3; j++) {
if (triangles[i].points[j] >= p_at_index) {
triangles.write[i].points[j]++;
}
}
}
}
blend_points[p_at_index].node = p_node;
blend_points[p_at_index].position = p_position;
blend_points[p_at_index].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
blend_points_used++;
_queue_auto_triangles();
emit_signal("tree_changed");
}
void AnimationNodeBlendSpace2D::set_blend_point_position(int p_point, const Vector2 &p_position) {
ERR_FAIL_INDEX(p_point, blend_points_used);
blend_points[p_point].position = p_position;
_queue_auto_triangles();
}
void AnimationNodeBlendSpace2D::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()) {
blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
}
blend_points[p_point].node = p_node;
blend_points[p_point].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
emit_signal("tree_changed");
}
Vector2 AnimationNodeBlendSpace2D::get_blend_point_position(int p_point) const {
ERR_FAIL_INDEX_V(p_point, blend_points_used, Vector2());
return blend_points[p_point].position;
}
Ref<AnimationRootNode> AnimationNodeBlendSpace2D::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 AnimationNodeBlendSpace2D::remove_blend_point(int p_point) {
ERR_FAIL_INDEX(p_point, blend_points_used);
ERR_FAIL_COND(blend_points[p_point].node.is_null());
blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
for (int i = 0; i < triangles.size(); i++) {
bool erase = false;
for (int j = 0; j < 3; j++) {
if (triangles[i].points[j] == p_point) {
erase = true;
break;
} else if (triangles[i].points[j] > p_point) {
triangles.write[i].points[j]--;
}
}
if (erase) {
triangles.remove(i);
i--;
}
}
for (int i = p_point; i < blend_points_used - 1; i++) {
blend_points[i] = blend_points[i + 1];
}
blend_points_used--;
emit_signal("tree_changed");
}
int AnimationNodeBlendSpace2D::get_blend_point_count() const {
return blend_points_used;
}
bool AnimationNodeBlendSpace2D::has_triangle(int p_x, int p_y, int p_z) const {
ERR_FAIL_INDEX_V(p_x, blend_points_used, false);
ERR_FAIL_INDEX_V(p_y, blend_points_used, false);
ERR_FAIL_INDEX_V(p_z, blend_points_used, false);
BlendTriangle t;
t.points[0] = p_x;
t.points[1] = p_y;
t.points[2] = p_z;
SortArray<int> sort;
sort.sort(t.points, 3);
for (int i = 0; i < triangles.size(); i++) {
bool all_equal = true;
for (int j = 0; j < 3; j++) {
if (triangles[i].points[j] != t.points[j]) {
all_equal = false;
break;
}
}
if (all_equal) {
return true;
}
}
return false;
}
void AnimationNodeBlendSpace2D::add_triangle(int p_x, int p_y, int p_z, int p_at_index) {
ERR_FAIL_INDEX(p_x, blend_points_used);
ERR_FAIL_INDEX(p_y, blend_points_used);
ERR_FAIL_INDEX(p_z, blend_points_used);
_update_triangles();
BlendTriangle t;
t.points[0] = p_x;
t.points[1] = p_y;
t.points[2] = p_z;
SortArray<int> sort;
sort.sort(t.points, 3);
for (int i = 0; i < triangles.size(); i++) {
bool all_equal = true;
for (int j = 0; j < 3; j++) {
if (triangles[i].points[j] != t.points[j]) {
all_equal = false;
break;
}
}
ERR_FAIL_COND(all_equal);
}
if (p_at_index == -1 || p_at_index == triangles.size()) {
triangles.push_back(t);
} else {
triangles.insert(p_at_index, t);
}
}
int AnimationNodeBlendSpace2D::get_triangle_point(int p_triangle, int p_point) {
_update_triangles();
ERR_FAIL_INDEX_V(p_point, 3, -1);
ERR_FAIL_INDEX_V(p_triangle, triangles.size(), -1);
return triangles[p_triangle].points[p_point];
}
void AnimationNodeBlendSpace2D::remove_triangle(int p_triangle) {
ERR_FAIL_INDEX(p_triangle, triangles.size());
triangles.remove(p_triangle);
}
int AnimationNodeBlendSpace2D::get_triangle_count() const {
return triangles.size();
}
void AnimationNodeBlendSpace2D::set_min_space(const Vector2 &p_min) {
min_space = p_min;
if (min_space.x >= max_space.x) {
min_space.x = max_space.x - 1;
}
if (min_space.y >= max_space.y) {
min_space.y = max_space.y - 1;
}
}
Vector2 AnimationNodeBlendSpace2D::get_min_space() const {
return min_space;
}
void AnimationNodeBlendSpace2D::set_max_space(const Vector2 &p_max) {
max_space = p_max;
if (max_space.x <= min_space.x) {
max_space.x = min_space.x + 1;
}
if (max_space.y <= min_space.y) {
max_space.y = min_space.y + 1;
}
}
Vector2 AnimationNodeBlendSpace2D::get_max_space() const {
return max_space;
}
void AnimationNodeBlendSpace2D::set_snap(const Vector2 &p_snap) {
snap = p_snap;
}
Vector2 AnimationNodeBlendSpace2D::get_snap() const {
return snap;
}
void AnimationNodeBlendSpace2D::set_x_label(const String &p_label) {
x_label = p_label;
}
String AnimationNodeBlendSpace2D::get_x_label() const {
return x_label;
}
void AnimationNodeBlendSpace2D::set_y_label(const String &p_label) {
y_label = p_label;
}
String AnimationNodeBlendSpace2D::get_y_label() const {
return y_label;
}
void AnimationNodeBlendSpace2D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
if (p_index == blend_points_used) {
add_blend_point(p_node, Vector2());
} else {
set_blend_point_node(p_index, p_node);
}
}
void AnimationNodeBlendSpace2D::_set_triangles(const Vector<int> &p_triangles) {
if (auto_triangles) {
return;
}
ERR_FAIL_COND(p_triangles.size() % 3 != 0);
for (int i = 0; i < p_triangles.size(); i += 3) {
add_triangle(p_triangles[i + 0], p_triangles[i + 1], p_triangles[i + 2]);
}
}
Vector<int> AnimationNodeBlendSpace2D::_get_triangles() const {
Vector<int> t;
if (auto_triangles && trianges_dirty) {
return t;
}
t.resize(triangles.size() * 3);
for (int i = 0; i < triangles.size(); i++) {
t.write[i * 3 + 0] = triangles[i].points[0];
t.write[i * 3 + 1] = triangles[i].points[1];
t.write[i * 3 + 2] = triangles[i].points[2];
}
return t;
}
void AnimationNodeBlendSpace2D::_queue_auto_triangles() {
if (!auto_triangles || trianges_dirty) {
return;
}
trianges_dirty = true;
call_deferred("_update_triangles");
}
void AnimationNodeBlendSpace2D::_update_triangles() {
if (!auto_triangles || !trianges_dirty) {
return;
}
trianges_dirty = false;
triangles.clear();
if (blend_points_used < 3) {
emit_signal("triangles_updated");
return;
}
Vector<Vector2> points;
points.resize(blend_points_used);
for (int i = 0; i < blend_points_used; i++) {
points.write[i] = blend_points[i].position;
}
Vector<Delaunay2D::Triangle> triangles = Delaunay2D::triangulate(points);
for (int i = 0; i < triangles.size(); i++) {
add_triangle(triangles[i].points[0], triangles[i].points[1], triangles[i].points[2]);
}
emit_signal("triangles_updated");
}
Vector2 AnimationNodeBlendSpace2D::get_closest_point(const Vector2 &p_point) {
_update_triangles();
if (triangles.size() == 0) {
return Vector2();
}
Vector2 best_point;
bool first = true;
for (int i = 0; i < triangles.size(); i++) {
Vector2 points[3];
for (int j = 0; j < 3; j++) {
points[j] = get_blend_point_position(get_triangle_point(i, j));
}
if (Geometry::is_point_in_triangle(p_point, points[0], points[1], points[2])) {
return p_point;
}
for (int j = 0; j < 3; j++) {
Vector2 s[2] = {
points[j],
points[(j + 1) % 3]
};
Vector2 closest = Geometry::get_closest_point_to_segment_2d(p_point, s);
if (first || closest.distance_to(p_point) < best_point.distance_to(p_point)) {
best_point = closest;
first = false;
}
}
}
return best_point;
}
void AnimationNodeBlendSpace2D::_blend_triangle(const Vector2 &p_pos, const Vector2 *p_points, float *r_weights) {
if (p_pos.distance_squared_to(p_points[0]) < CMP_EPSILON2) {
r_weights[0] = 1;
r_weights[1] = 0;
r_weights[2] = 0;
return;
}
if (p_pos.distance_squared_to(p_points[1]) < CMP_EPSILON2) {
r_weights[0] = 0;
r_weights[1] = 1;
r_weights[2] = 0;
return;
}
if (p_pos.distance_squared_to(p_points[2]) < CMP_EPSILON2) {
r_weights[0] = 0;
r_weights[1] = 0;
r_weights[2] = 1;
return;
}
Vector2 v0 = p_points[1] - p_points[0];
Vector2 v1 = p_points[2] - p_points[0];
Vector2 v2 = p_pos - p_points[0];
float d00 = v0.dot(v0);
float d01 = v0.dot(v1);
float d11 = v1.dot(v1);
float d20 = v2.dot(v0);
float d21 = v2.dot(v1);
float denom = (d00 * d11 - d01 * d01);
if (denom == 0) {
r_weights[0] = 1;
r_weights[1] = 0;
r_weights[2] = 0;
return;
}
float v = (d11 * d20 - d01 * d21) / denom;
float w = (d00 * d21 - d01 * d20) / denom;
float u = 1.0f - v - w;
r_weights[0] = u;
r_weights[1] = v;
r_weights[2] = w;
}
float AnimationNodeBlendSpace2D::process(float p_time, bool p_seek) {
_update_triangles();
Vector2 blend_pos = get_parameter(blend_position);
int closest = get_parameter(this->closest);
float length_internal = get_parameter(this->length_internal);
float mind = 0; //time of min distance point
if (blend_mode == BLEND_MODE_INTERPOLATED) {
if (triangles.size() == 0) {
return 0;
}
Vector2 best_point;
bool first = true;
int blend_triangle = -1;
float blend_weights[3] = { 0, 0, 0 };
for (int i = 0; i < triangles.size(); i++) {
Vector2 points[3];
for (int j = 0; j < 3; j++) {
points[j] = get_blend_point_position(get_triangle_point(i, j));
}
if (Geometry::is_point_in_triangle(blend_pos, points[0], points[1], points[2])) {
blend_triangle = i;
_blend_triangle(blend_pos, points, blend_weights);
break;
}
for (int j = 0; j < 3; j++) {
Vector2 s[2] = {
points[j],
points[(j + 1) % 3]
};
Vector2 closest2 = Geometry::get_closest_point_to_segment_2d(blend_pos, s);
if (first || closest2.distance_to(blend_pos) < best_point.distance_to(blend_pos)) {
best_point = closest2;
blend_triangle = i;
first = false;
float d = s[0].distance_to(s[1]);
if (d == 0.0) {
blend_weights[j] = 1.0;
blend_weights[(j + 1) % 3] = 0.0;
blend_weights[(j + 2) % 3] = 0.0;
} else {
float c = s[0].distance_to(closest2) / d;
blend_weights[j] = 1.0 - c;
blend_weights[(j + 1) % 3] = c;
blend_weights[(j + 2) % 3] = 0.0;
}
}
}
}
ERR_FAIL_COND_V(blend_triangle == -1, 0); //should never reach here
int triangle_points[3];
for (int j = 0; j < 3; j++) {
triangle_points[j] = get_triangle_point(blend_triangle, j);
}
first = true;
for (int i = 0; i < blend_points_used; i++) {
bool found = false;
for (int j = 0; j < 3; j++) {
if (i == triangle_points[j]) {
//blend with the given weight
float t = blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, blend_weights[j], FILTER_IGNORE, false);
if (first || t < mind) {
mind = t;
first = false;
}
found = true;
break;
}
}
if (!found) {
//ignore
blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, 0, FILTER_IGNORE, false);
}
}
} else {
int new_closest = -1;
float new_closest_dist = 1e20;
for (int i = 0; i < blend_points_used; i++) {
float d = blend_points[i].position.distance_squared_to(blend_pos);
if (d < new_closest_dist) {
new_closest = i;
new_closest_dist = d;
}
}
if (new_closest != closest && new_closest != -1) {
float from = 0;
if (blend_mode == BLEND_MODE_DISCRETE_CARRY && closest != -1) {
//see how much animation remains
from = length_internal - blend_node(blend_points[closest].name, blend_points[closest].node, p_time, false, 0.0, FILTER_IGNORE, false);
}
mind = blend_node(blend_points[new_closest].name, blend_points[new_closest].node, from, true, 1.0, FILTER_IGNORE, false);
length_internal = from + mind;
closest = new_closest;
} else {
mind = blend_node(blend_points[closest].name, blend_points[closest].node, p_time, p_seek, 1.0, FILTER_IGNORE, false);
}
}
set_parameter(this->closest, closest);
set_parameter(this->length_internal, length_internal);
return mind;
}
String AnimationNodeBlendSpace2D::get_caption() const {
return "BlendSpace2D";
}
void AnimationNodeBlendSpace2D::_validate_property(PropertyInfo &property) const {
if (auto_triangles && property.name == "triangles") {
property.usage = 0;
}
if (property.name.begins_with("blend_point_")) {
String left = property.name.get_slicec('/', 0);
int idx = left.get_slicec('_', 2).to_int();
if (idx >= blend_points_used) {
property.usage = 0;
}
}
AnimationRootNode::_validate_property(property);
}
void AnimationNodeBlendSpace2D::set_auto_triangles(bool p_enable) {
if (auto_triangles == p_enable) {
return;
}
auto_triangles = p_enable;
_queue_auto_triangles();
}
bool AnimationNodeBlendSpace2D::get_auto_triangles() const {
return auto_triangles;
}
Ref<AnimationNode> AnimationNodeBlendSpace2D::get_child_by_name(const StringName &p_name) {
return get_blend_point_node(p_name.operator String().to_int());
}
void AnimationNodeBlendSpace2D::_tree_changed() {
emit_signal("tree_changed");
}
void AnimationNodeBlendSpace2D::set_blend_mode(BlendMode p_blend_mode) {
blend_mode = p_blend_mode;
}
AnimationNodeBlendSpace2D::BlendMode AnimationNodeBlendSpace2D::get_blend_mode() const {
return blend_mode;
}
void AnimationNodeBlendSpace2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace2D::add_blend_point, DEFVAL(-1));
ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace2D::set_blend_point_position);
ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace2D::get_blend_point_position);
ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace2D::set_blend_point_node);
ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace2D::get_blend_point_node);
ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace2D::remove_blend_point);
ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace2D::get_blend_point_count);
ClassDB::bind_method(D_METHOD("add_triangle", "x", "y", "z", "at_index"), &AnimationNodeBlendSpace2D::add_triangle, DEFVAL(-1));
ClassDB::bind_method(D_METHOD("get_triangle_point", "triangle", "point"), &AnimationNodeBlendSpace2D::get_triangle_point);
ClassDB::bind_method(D_METHOD("remove_triangle", "triangle"), &AnimationNodeBlendSpace2D::remove_triangle);
ClassDB::bind_method(D_METHOD("get_triangle_count"), &AnimationNodeBlendSpace2D::get_triangle_count);
ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace2D::set_min_space);
ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace2D::get_min_space);
ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace2D::set_max_space);
ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace2D::get_max_space);
ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace2D::set_snap);
ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace2D::get_snap);
ClassDB::bind_method(D_METHOD("set_x_label", "text"), &AnimationNodeBlendSpace2D::set_x_label);
ClassDB::bind_method(D_METHOD("get_x_label"), &AnimationNodeBlendSpace2D::get_x_label);
ClassDB::bind_method(D_METHOD("set_y_label", "text"), &AnimationNodeBlendSpace2D::set_y_label);
ClassDB::bind_method(D_METHOD("get_y_label"), &AnimationNodeBlendSpace2D::get_y_label);
ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace2D::_add_blend_point);
ClassDB::bind_method(D_METHOD("_set_triangles", "triangles"), &AnimationNodeBlendSpace2D::_set_triangles);
ClassDB::bind_method(D_METHOD("_get_triangles"), &AnimationNodeBlendSpace2D::_get_triangles);
ClassDB::bind_method(D_METHOD("set_auto_triangles", "enable"), &AnimationNodeBlendSpace2D::set_auto_triangles);
ClassDB::bind_method(D_METHOD("get_auto_triangles"), &AnimationNodeBlendSpace2D::get_auto_triangles);
ClassDB::bind_method(D_METHOD("set_blend_mode", "mode"), &AnimationNodeBlendSpace2D::set_blend_mode);
ClassDB::bind_method(D_METHOD("get_blend_mode"), &AnimationNodeBlendSpace2D::get_blend_mode);
ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeBlendSpace2D::_tree_changed);
ClassDB::bind_method(D_METHOD("_update_triangles"), &AnimationNodeBlendSpace2D::_update_triangles);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_triangles", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_auto_triangles", "get_auto_triangles");
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point", "get_blend_point_node", i);
ADD_PROPERTYI(PropertyInfo(Variant::VECTOR2, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
}
ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "triangles", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_triangles", "_get_triangles");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_min_space", "get_min_space");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_max_space", "get_max_space");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_snap", "get_snap");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "x_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_x_label", "get_x_label");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "y_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_y_label", "get_y_label");
ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Interpolated,Discrete,Carry", PROPERTY_USAGE_NOEDITOR), "set_blend_mode", "get_blend_mode");
ADD_SIGNAL(MethodInfo("triangles_updated"));
BIND_ENUM_CONSTANT(BLEND_MODE_INTERPOLATED);
BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE);
BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE_CARRY);
}
AnimationNodeBlendSpace2D::AnimationNodeBlendSpace2D() {
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
blend_points[i].name = itos(i);
}
auto_triangles = true;
blend_points_used = 0;
max_space = Vector2(1, 1);
min_space = Vector2(-1, -1);
snap = Vector2(0.1, 0.1);
x_label = "x";
y_label = "y";
trianges_dirty = false;
blend_position = "blend_position";
closest = "closest";
length_internal = "length_internal";
blend_mode = BLEND_MODE_INTERPOLATED;
}
AnimationNodeBlendSpace2D::~AnimationNodeBlendSpace2D() {
}

View File

@ -1,147 +0,0 @@
#ifndef ANIMATION_BLEND_SPACE_2D_H
#define ANIMATION_BLEND_SPACE_2D_H
/*************************************************************************/
/* animation_blend_space_2d.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "scene/animation/animation_tree.h"
class AnimationNodeBlendSpace2D : public AnimationRootNode {
GDCLASS(AnimationNodeBlendSpace2D, AnimationRootNode);
public:
enum BlendMode {
BLEND_MODE_INTERPOLATED,
BLEND_MODE_DISCRETE,
BLEND_MODE_DISCRETE_CARRY,
};
protected:
enum {
MAX_BLEND_POINTS = 64
};
struct BlendPoint {
StringName name;
Ref<AnimationRootNode> node;
Vector2 position;
};
BlendPoint blend_points[MAX_BLEND_POINTS];
int blend_points_used;
struct BlendTriangle {
int points[3];
};
Vector<BlendTriangle> triangles;
StringName blend_position;
StringName closest;
StringName length_internal;
Vector2 max_space;
Vector2 min_space;
Vector2 snap;
String x_label;
String y_label;
BlendMode blend_mode;
void _add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node);
void _set_triangles(const Vector<int> &p_triangles);
Vector<int> _get_triangles() const;
void _blend_triangle(const Vector2 &p_pos, const Vector2 *p_points, float *r_weights);
bool auto_triangles;
bool trianges_dirty;
void _update_triangles();
void _queue_auto_triangles();
void _tree_changed();
protected:
virtual void _validate_property(PropertyInfo &property) const;
static void _bind_methods();
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
void add_blend_point(const Ref<AnimationRootNode> &p_node, const Vector2 &p_position, int p_at_index = -1);
void set_blend_point_position(int p_point, const Vector2 &p_position);
void set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node);
Vector2 get_blend_point_position(int p_point) const;
Ref<AnimationRootNode> get_blend_point_node(int p_point) const;
void remove_blend_point(int p_point);
int get_blend_point_count() const;
bool has_triangle(int p_x, int p_y, int p_z) const;
void add_triangle(int p_x, int p_y, int p_z, int p_at_index = -1);
int get_triangle_point(int p_triangle, int p_point);
void remove_triangle(int p_triangle);
int get_triangle_count() const;
void set_min_space(const Vector2 &p_min);
Vector2 get_min_space() const;
void set_max_space(const Vector2 &p_max);
Vector2 get_max_space() const;
void set_snap(const Vector2 &p_snap);
Vector2 get_snap() const;
void set_x_label(const String &p_label);
String get_x_label() const;
void set_y_label(const String &p_label);
String get_y_label() const;
virtual float process(float p_time, bool p_seek);
virtual String get_caption() const;
Vector2 get_closest_point(const Vector2 &p_point);
void set_auto_triangles(bool p_enable);
bool get_auto_triangles() const;
void set_blend_mode(BlendMode p_blend_mode);
BlendMode get_blend_mode() const;
virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);
AnimationNodeBlendSpace2D();
~AnimationNodeBlendSpace2D();
};
VARIANT_ENUM_CAST(AnimationNodeBlendSpace2D::BlendMode)
#endif // ANIMATION_BLEND_SPACE_2D_H

File diff suppressed because it is too large Load Diff

View File

@ -1,407 +0,0 @@
#ifndef ANIMATION_BLEND_TREE_H
#define ANIMATION_BLEND_TREE_H
/*************************************************************************/
/* animation_blend_tree.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "scene/animation/animation_tree.h"
class AnimationNodeAnimation : public AnimationRootNode {
GDCLASS(AnimationNodeAnimation, AnimationRootNode);
StringName animation;
StringName time;
uint64_t last_version;
bool skip;
protected:
void _validate_property(PropertyInfo &property) const;
static void _bind_methods();
public:
void get_parameter_list(List<PropertyInfo> *r_list) const;
static Vector<String> (*get_editable_animation_list)();
virtual String get_caption() const;
virtual float process(float p_time, bool p_seek);
void set_animation(const StringName &p_name);
StringName get_animation() const;
AnimationNodeAnimation();
};
class AnimationNodeOneShot : public AnimationNode {
GDCLASS(AnimationNodeOneShot, AnimationNode);
public:
enum MixMode {
MIX_MODE_BLEND,
MIX_MODE_ADD
};
private:
float fade_in;
float fade_out;
bool autorestart;
float autorestart_delay;
float autorestart_random_delay;
MixMode mix;
bool sync;
/* bool active;
bool do_start;
float time;
float remaining;*/
StringName active;
StringName prev_active;
StringName time;
StringName remaining;
StringName time_to_restart;
protected:
static void _bind_methods();
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual String get_caption() const;
void set_fadein_time(float p_time);
void set_fadeout_time(float p_time);
float get_fadein_time() const;
float get_fadeout_time() const;
void set_autorestart(bool p_active);
void set_autorestart_delay(float p_time);
void set_autorestart_random_delay(float p_time);
bool has_autorestart() const;
float get_autorestart_delay() const;
float get_autorestart_random_delay() const;
void set_mix_mode(MixMode p_mix);
MixMode get_mix_mode() const;
void set_use_sync(bool p_sync);
bool is_using_sync() const;
virtual bool has_filter() const;
virtual float process(float p_time, bool p_seek);
AnimationNodeOneShot();
};
VARIANT_ENUM_CAST(AnimationNodeOneShot::MixMode)
class AnimationNodeAdd2 : public AnimationNode {
GDCLASS(AnimationNodeAdd2, AnimationNode);
StringName add_amount;
bool sync;
protected:
static void _bind_methods();
public:
void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual String get_caption() const;
void set_use_sync(bool p_sync);
bool is_using_sync() const;
virtual bool has_filter() const;
virtual float process(float p_time, bool p_seek);
AnimationNodeAdd2();
};
class AnimationNodeAdd3 : public AnimationNode {
GDCLASS(AnimationNodeAdd3, AnimationNode);
StringName add_amount;
bool sync;
protected:
static void _bind_methods();
public:
void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual String get_caption() const;
void set_use_sync(bool p_sync);
bool is_using_sync() const;
virtual bool has_filter() const;
virtual float process(float p_time, bool p_seek);
AnimationNodeAdd3();
};
class AnimationNodeBlend2 : public AnimationNode {
GDCLASS(AnimationNodeBlend2, AnimationNode);
StringName blend_amount;
bool sync;
protected:
static void _bind_methods();
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual String get_caption() const;
virtual float process(float p_time, bool p_seek);
void set_use_sync(bool p_sync);
bool is_using_sync() const;
virtual bool has_filter() const;
AnimationNodeBlend2();
};
class AnimationNodeBlend3 : public AnimationNode {
GDCLASS(AnimationNodeBlend3, AnimationNode);
StringName blend_amount;
bool sync;
protected:
static void _bind_methods();
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual String get_caption() const;
void set_use_sync(bool p_sync);
bool is_using_sync() const;
float process(float p_time, bool p_seek);
AnimationNodeBlend3();
};
class AnimationNodeTimeScale : public AnimationNode {
GDCLASS(AnimationNodeTimeScale, AnimationNode);
StringName scale;
protected:
static void _bind_methods();
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual String get_caption() const;
float process(float p_time, bool p_seek);
AnimationNodeTimeScale();
};
class AnimationNodeTimeSeek : public AnimationNode {
GDCLASS(AnimationNodeTimeSeek, AnimationNode);
StringName seek_pos;
protected:
static void _bind_methods();
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual String get_caption() const;
float process(float p_time, bool p_seek);
AnimationNodeTimeSeek();
};
class AnimationNodeTransition : public AnimationNode {
GDCLASS(AnimationNodeTransition, AnimationNode);
enum {
MAX_INPUTS = 32
};
struct InputData {
String name;
bool auto_advance;
InputData() { auto_advance = false; }
};
InputData inputs[MAX_INPUTS];
int enabled_inputs;
/*
float prev_xfading;
int prev;
float time;
int current;
int prev_current; */
StringName prev_xfading;
StringName prev;
StringName time;
StringName current;
StringName prev_current;
float xfade;
void _update_inputs();
protected:
static void _bind_methods();
void _validate_property(PropertyInfo &property) const;
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
virtual String get_caption() const;
void set_enabled_inputs(int p_inputs);
int get_enabled_inputs();
void set_input_as_auto_advance(int p_input, bool p_enable);
bool is_input_set_as_auto_advance(int p_input) const;
void set_input_caption(int p_input, const String &p_name);
String get_input_caption(int p_input) const;
void set_cross_fade_time(float p_fade);
float get_cross_fade_time() const;
float process(float p_time, bool p_seek);
AnimationNodeTransition();
};
class AnimationNodeOutput : public AnimationNode {
GDCLASS(AnimationNodeOutput, AnimationNode);
public:
virtual String get_caption() const;
virtual float process(float p_time, bool p_seek);
AnimationNodeOutput();
};
/////
class AnimationNodeBlendTree : public AnimationRootNode {
GDCLASS(AnimationNodeBlendTree, AnimationRootNode);
struct Node {
Ref<AnimationNode> node;
Vector2 position;
Vector<StringName> connections;
};
typedef RBMap<StringName, Node, StringName::AlphCompare> NodeMap;
NodeMap nodes;
Vector2 graph_offset;
void _tree_changed();
void _node_changed(const StringName &p_node);
protected:
static void _bind_methods();
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
public:
enum ConnectionError {
CONNECTION_OK,
CONNECTION_ERROR_NO_INPUT,
CONNECTION_ERROR_NO_INPUT_INDEX,
CONNECTION_ERROR_NO_OUTPUT,
CONNECTION_ERROR_SAME_NODE,
CONNECTION_ERROR_CONNECTION_EXISTS,
//no need to check for cycles due to tree topology
};
void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2());
Ref<AnimationNode> get_node(const StringName &p_name) const;
void remove_node(const StringName &p_name);
void rename_node(const StringName &p_name, const StringName &p_new_name);
bool has_node(const StringName &p_name) const;
StringName get_node_name(const Ref<AnimationNode> &p_node) const;
Vector<StringName> get_node_connection_array(const StringName &p_name) const;
void set_node_position(const StringName &p_node, const Vector2 &p_position);
Vector2 get_node_position(const StringName &p_node) const;
virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
void connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node);
void disconnect_node(const StringName &p_node, int p_input_index);
struct NodeConnection {
StringName input_node;
int input_index;
StringName output_node;
};
ConnectionError can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const;
void get_node_connections(List<NodeConnection> *r_connections) const;
virtual String get_caption() const;
virtual float process(float p_time, bool p_seek);
void get_node_list(List<StringName> *r_list);
void set_graph_offset(const Vector2 &p_graph_offset);
Vector2 get_graph_offset() const;
virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);
AnimationNodeBlendTree();
~AnimationNodeBlendTree();
};
VARIANT_ENUM_CAST(AnimationNodeBlendTree::ConnectionError)
#endif // ANIMATION_BLEND_TREE_H

View File

@ -1,962 +0,0 @@
/*************************************************************************/
/* animation_node_state_machine.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "animation_node_state_machine.h"
/////////////////////////////////////////////////
void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) {
switch_mode = p_mode;
}
AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const {
return switch_mode;
}
void AnimationNodeStateMachineTransition::set_auto_advance(bool p_enable) {
auto_advance = p_enable;
}
bool AnimationNodeStateMachineTransition::has_auto_advance() const {
return auto_advance;
}
void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {
String cs = p_condition;
ERR_FAIL_COND(cs.find("/") != -1 || cs.find(":") != -1);
advance_condition = p_condition;
if (cs != String()) {
advance_condition_name = "conditions/" + cs;
} else {
advance_condition_name = StringName();
}
emit_signal("advance_condition_changed");
}
StringName AnimationNodeStateMachineTransition::get_advance_condition() const {
return advance_condition;
}
StringName AnimationNodeStateMachineTransition::get_advance_condition_name() const {
return advance_condition_name;
}
void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {
ERR_FAIL_COND(p_xfade < 0);
xfade = p_xfade;
emit_changed();
}
float AnimationNodeStateMachineTransition::get_xfade_time() const {
return xfade;
}
void AnimationNodeStateMachineTransition::set_disabled(bool p_disabled) {
disabled = p_disabled;
emit_changed();
}
bool AnimationNodeStateMachineTransition::is_disabled() const {
return disabled;
}
void AnimationNodeStateMachineTransition::set_priority(int p_priority) {
priority = p_priority;
emit_changed();
}
int AnimationNodeStateMachineTransition::get_priority() const {
return priority;
}
void AnimationNodeStateMachineTransition::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);
ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);
ClassDB::bind_method(D_METHOD("set_auto_advance", "auto_advance"), &AnimationNodeStateMachineTransition::set_auto_advance);
ClassDB::bind_method(D_METHOD("has_auto_advance"), &AnimationNodeStateMachineTransition::has_auto_advance);
ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);
ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);
ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);
ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);
ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &AnimationNodeStateMachineTransition::set_disabled);
ClassDB::bind_method(D_METHOD("is_disabled"), &AnimationNodeStateMachineTransition::is_disabled);
ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);
ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);
ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,AtEnd"), "set_switch_mode", "get_switch_mode");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "auto_advance"), "set_auto_advance", "has_auto_advance");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "advance_condition"), "set_advance_condition", "get_advance_condition");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01"), "set_xfade_time", "get_xfade_time");
ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);
BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);
BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);
ADD_SIGNAL(MethodInfo("advance_condition_changed"));
}
AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {
switch_mode = SWITCH_MODE_IMMEDIATE;
auto_advance = false;
xfade = 0;
disabled = false;
priority = 1;
}
////////////////////////////////////////////////////////
void AnimationNodeStateMachinePlayback::travel(const StringName &p_state) {
start_request_travel = true;
start_request = p_state;
stop_request = false;
}
void AnimationNodeStateMachinePlayback::start(const StringName &p_state) {
start_request_travel = false;
start_request = p_state;
stop_request = false;
}
void AnimationNodeStateMachinePlayback::stop() {
stop_request = true;
}
bool AnimationNodeStateMachinePlayback::is_playing() const {
return playing;
}
StringName AnimationNodeStateMachinePlayback::get_current_node() const {
return current;
}
StringName AnimationNodeStateMachinePlayback::get_blend_from_node() const {
return fading_from;
}
Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {
return path;
}
float AnimationNodeStateMachinePlayback::get_current_play_pos() const {
return pos_current;
}
float AnimationNodeStateMachinePlayback::get_current_length() const {
return len_current;
}
bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) {
ERR_FAIL_COND_V(!playing, false);
ERR_FAIL_COND_V(!p_state_machine->states.has(p_travel), false);
ERR_FAIL_COND_V(!p_state_machine->states.has(current), false);
path.clear(); //a new one will be needed
if (current == p_travel) {
return true; //nothing to do
}
Vector2 current_pos = p_state_machine->states[current].position;
Vector2 target_pos = p_state_machine->states[p_travel].position;
RBMap<StringName, AStarCost> cost_map;
List<int> open_list;
//build open list
for (int i = 0; i < p_state_machine->transitions.size(); i++) {
if (p_state_machine->transitions[i].from == current) {
open_list.push_back(i);
float cost = p_state_machine->states[p_state_machine->transitions[i].to].position.distance_to(current_pos);
cost *= p_state_machine->transitions[i].transition->get_priority();
AStarCost ap;
ap.prev = current;
ap.distance = cost;
cost_map[p_state_machine->transitions[i].to] = ap;
if (p_state_machine->transitions[i].to == p_travel) { //prematurely found it! :D
path.push_back(p_travel);
return true;
}
}
}
//begin astar
bool found_route = false;
while (!found_route) {
if (open_list.size() == 0) {
return false; //no path found
}
//find the last cost transition
List<int>::Element *least_cost_transition = nullptr;
float least_cost = 1e20;
for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
float cost = cost_map[p_state_machine->transitions[E->get()].to].distance;
cost += p_state_machine->states[p_state_machine->transitions[E->get()].to].position.distance_to(target_pos);
if (cost < least_cost) {
least_cost_transition = E;
least_cost = cost;
}
}
StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].from;
StringName transition = p_state_machine->transitions[least_cost_transition->get()].to;
for (int i = 0; i < p_state_machine->transitions.size(); i++) {
if (p_state_machine->transitions[i].from != transition || p_state_machine->transitions[i].to == transition_prev) {
continue; //not interested on those
}
float distance = p_state_machine->states[p_state_machine->transitions[i].from].position.distance_to(p_state_machine->states[p_state_machine->transitions[i].to].position);
distance *= p_state_machine->transitions[i].transition->get_priority();
distance += cost_map[p_state_machine->transitions[i].from].distance;
if (cost_map.has(p_state_machine->transitions[i].to)) {
//oh this was visited already, can we win the cost?
if (distance < cost_map[p_state_machine->transitions[i].to].distance) {
cost_map[p_state_machine->transitions[i].to].distance = distance;
cost_map[p_state_machine->transitions[i].to].prev = p_state_machine->transitions[i].from;
}
} else {
//add to open list
AStarCost ac;
ac.prev = p_state_machine->transitions[i].from;
ac.distance = distance;
cost_map[p_state_machine->transitions[i].to] = ac;
open_list.push_back(i);
if (p_state_machine->transitions[i].to == p_travel) {
found_route = true;
break;
}
}
}
if (found_route) {
break;
}
open_list.erase(least_cost_transition);
}
//make path
StringName at = p_travel;
while (at != current) {
path.push_back(at);
at = cost_map[at].prev;
}
path.invert();
return true;
}
float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, float p_time, bool p_seek) {
//if not playing and it can restart, then restart
if (!playing && start_request == StringName()) {
if (!stop_request && p_state_machine->start_node) {
start(p_state_machine->start_node);
} else {
return 0;
}
}
if (playing && stop_request) {
stop_request = false;
playing = false;
return 0;
}
bool play_start = false;
if (start_request != StringName()) {
if (start_request_travel) {
if (!playing) {
if (!stop_request && p_state_machine->start_node) {
// can restart, just postpone traveling
path.clear();
current = p_state_machine->start_node;
playing = true;
play_start = true;
} else {
// stopped, invalid state
String node_name = start_request;
start_request = StringName(); //clear start request
ERR_FAIL_V_MSG(0, "Can't travel to '" + node_name + "' if state machine is not playing. Maybe you need to enable Autoplay on Load for one of the nodes in your state machine or call .start() first?");
}
} else {
if (!_travel(p_state_machine, start_request)) {
// can't travel, then teleport
path.clear();
current = start_request;
}
start_request = StringName(); //clear start request
}
} else {
// teleport to start
if (p_state_machine->states.has(start_request)) {
path.clear();
current = start_request;
playing = true;
play_start = true;
start_request = StringName(); //clear start request
} else {
StringName node = start_request;
start_request = StringName(); //clear start request
ERR_FAIL_V_MSG(0, "No such node: '" + node + "'");
}
}
}
bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
if (do_start) {
if (p_state_machine->start_node != StringName() && p_seek && p_time == 0) {
current = p_state_machine->start_node;
}
len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false);
pos_current = 0;
}
if (!p_state_machine->states.has(current)) {
playing = false; //current does not exist
current = StringName();
return 0;
}
float fade_blend = 1.0;
if (fading_from != StringName()) {
if (!p_state_machine->states.has(fading_from)) {
fading_from = StringName();
} else {
if (!p_seek) {
fading_pos += p_time;
}
fade_blend = MIN(1.0, fading_pos / fading_time);
if (fade_blend >= 1.0) {
fading_from = StringName();
}
}
}
float rem = p_state_machine->blend_node(current, p_state_machine->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false);
if (fading_from != StringName()) {
p_state_machine->blend_node(fading_from, p_state_machine->states[fading_from].node, p_time, p_seek, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, false);
}
//guess playback position
if (rem > len_current) { // weird but ok
len_current = rem;
}
{ //advance and loop check
float next_pos = len_current - rem;
end_loop = next_pos < pos_current;
pos_current = next_pos; //looped
}
//find next
StringName next;
float next_xfade = 0;
AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
if (path.size()) {
for (int i = 0; i < p_state_machine->transitions.size(); i++) {
if (p_state_machine->transitions[i].from == current && p_state_machine->transitions[i].to == path[0]) {
next_xfade = p_state_machine->transitions[i].transition->get_xfade_time();
switch_mode = p_state_machine->transitions[i].transition->get_switch_mode();
next = path[0];
}
}
} else {
float priority_best = 1e20;
int auto_advance_to = -1;
for (int i = 0; i < p_state_machine->transitions.size(); i++) {
bool auto_advance = false;
if (p_state_machine->transitions[i].transition->has_auto_advance()) {
auto_advance = true;
}
StringName advance_condition_name = p_state_machine->transitions[i].transition->get_advance_condition_name();
if (advance_condition_name != StringName() && bool(p_state_machine->get_parameter(advance_condition_name))) {
auto_advance = true;
}
if (p_state_machine->transitions[i].from == current && auto_advance) {
if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) {
priority_best = p_state_machine->transitions[i].transition->get_priority();
auto_advance_to = i;
}
}
}
if (auto_advance_to != -1) {
next = p_state_machine->transitions[auto_advance_to].to;
next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time();
switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode();
}
}
//if next, see when to transition
if (next != StringName()) {
bool goto_next = false;
if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
goto_next = next_xfade >= (len_current - pos_current) || end_loop;
if (end_loop) {
next_xfade = 0;
}
} else {
goto_next = fading_from == StringName();
}
if (goto_next) { //end_loop should be used because fade time may be too small or zero and animation may have looped
if (next_xfade) {
//time to fade, baby
fading_from = current;
fading_time = next_xfade;
fading_pos = 0;
} else {
fading_from = StringName();
fading_pos = 0;
}
if (path.size()) { //if it came from path, remove path
path.remove(0);
}
current = next;
if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
pos_current = MIN(pos_current, len_current);
p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false);
} else {
len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
pos_current = 0;
}
rem = len_current; //so it does not show 0 on transition
}
}
//compute time left for transitions by using the end node
if (p_state_machine->end_node != StringName() && p_state_machine->end_node != current) {
rem = p_state_machine->blend_node(p_state_machine->end_node, p_state_machine->states[p_state_machine->end_node].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false);
}
return rem;
}
void AnimationNodeStateMachinePlayback::_bind_methods() {
ClassDB::bind_method(D_METHOD("travel", "to_node"), &AnimationNodeStateMachinePlayback::travel);
ClassDB::bind_method(D_METHOD("start", "node"), &AnimationNodeStateMachinePlayback::start);
ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
ClassDB::bind_method(D_METHOD("get_current_play_position"), &AnimationNodeStateMachinePlayback::get_current_play_pos);
ClassDB::bind_method(D_METHOD("get_current_length"), &AnimationNodeStateMachinePlayback::get_current_length);
ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
}
AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
set_local_to_scene(true); //only one per instanced scene
playing = false;
len_current = 0;
fading_time = 0;
stop_request = false;
}
///////////////////////////////////////////////////////
void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
List<StringName> advance_conditions;
for (int i = 0; i < transitions.size(); i++) {
StringName ac = transitions[i].transition->get_advance_condition_name();
if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
advance_conditions.push_back(ac);
}
}
advance_conditions.sort_custom<StringName::AlphCompare>();
for (List<StringName>::Element *E = advance_conditions.front(); E; E = E->next()) {
r_list->push_back(PropertyInfo(Variant::BOOL, E->get()));
}
}
Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
if (p_parameter == playback) {
Ref<AnimationNodeStateMachinePlayback> p;
p.instance();
return p;
} else {
return false; //advance condition
}
}
void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
ERR_FAIL_COND(states.has(p_name));
ERR_FAIL_COND(p_node.is_null());
ERR_FAIL_COND(String(p_name).find("/") != -1);
State state;
state.node = p_node;
state.position = p_position;
states[p_name] = state;
emit_changed();
emit_signal("tree_changed");
p_node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
}
void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
ERR_FAIL_COND(states.has(p_name) == false);
ERR_FAIL_COND(p_node.is_null());
ERR_FAIL_COND(String(p_name).find("/") != -1);
{
Ref<AnimationNode> node = states[p_name].node;
if (node.is_valid()) {
node->disconnect("tree_changed", this, "_tree_changed");
}
}
states[p_name].node = p_node;
emit_changed();
emit_signal("tree_changed");
p_node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
}
Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
return states[p_name].node;
}
StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
for (RBMap<StringName, State>::Element *E = states.front(); E; E = E->next()) {
if (E->get().node == p_node) {
return E->key();
}
}
ERR_FAIL_V(StringName());
}
void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
Vector<StringName> nodes;
for (RBMap<StringName, State>::Element *E = states.front(); E; E = E->next()) {
nodes.push_back(E->key());
}
nodes.sort_custom<StringName::AlphCompare>();
for (int i = 0; i < nodes.size(); i++) {
ChildNode cn;
cn.name = nodes[i];
cn.node = states[cn.name].node;
r_child_nodes->push_back(cn);
}
}
bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
return states.has(p_name);
}
void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
ERR_FAIL_COND(!states.has(p_name));
{
Ref<AnimationNode> node = states[p_name].node;
ERR_FAIL_COND(node.is_null());
node->disconnect("tree_changed", this, "_tree_changed");
}
states.erase(p_name);
//path.erase(p_name);
for (int i = 0; i < transitions.size(); i++) {
if (transitions[i].from == p_name || transitions[i].to == p_name) {
transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
transitions.remove(i);
i--;
}
}
if (start_node == p_name) {
start_node = StringName();
}
if (end_node == p_name) {
end_node = StringName();
}
/*if (playing && current == p_name) {
stop();
}*/
emit_changed();
emit_signal("tree_changed");
}
void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
ERR_FAIL_COND(!states.has(p_name));
ERR_FAIL_COND(states.has(p_new_name));
states[p_new_name] = states[p_name];
states.erase(p_name);
for (int i = 0; i < transitions.size(); i++) {
if (transitions[i].from == p_name) {
transitions.write[i].from = p_new_name;
}
if (transitions[i].to == p_name) {
transitions.write[i].to = p_new_name;
}
}
if (start_node == p_name) {
start_node = p_new_name;
}
if (end_node == p_name) {
end_node = p_new_name;
}
/*if (playing && current == p_name) {
current = p_new_name;
}*/
//path.clear(); //clear path
emit_signal("tree_changed");
}
void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
List<StringName> nodes;
for (RBMap<StringName, State>::Element *E = states.front(); E; E = E->next()) {
nodes.push_back(E->key());
}
nodes.sort_custom<StringName::AlphCompare>();
for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) {
r_nodes->push_back(E->get());
}
}
bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
for (int i = 0; i < transitions.size(); i++) {
if (transitions[i].from == p_from && transitions[i].to == p_to) {
return true;
}
}
return false;
}
int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
for (int i = 0; i < transitions.size(); i++) {
if (transitions[i].from == p_from && transitions[i].to == p_to) {
return i;
}
}
return -1;
}
void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
ERR_FAIL_COND(p_from == p_to);
ERR_FAIL_COND(!states.has(p_from));
ERR_FAIL_COND(!states.has(p_to));
ERR_FAIL_COND(p_transition.is_null());
for (int i = 0; i < transitions.size(); i++) {
ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);
}
Transition tr;
tr.from = p_from;
tr.to = p_to;
tr.transition = p_transition;
tr.transition->connect("advance_condition_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
transitions.push_back(tr);
}
Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
return transitions[p_transition].transition;
}
StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
return transitions[p_transition].from;
}
StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
return transitions[p_transition].to;
}
int AnimationNodeStateMachine::get_transition_count() const {
return transitions.size();
}
void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
for (int i = 0; i < transitions.size(); i++) {
if (transitions[i].from == p_from && transitions[i].to == p_to) {
transitions.write[i].transition->disconnect("advance_condition_changed", this, "_tree_changed");
transitions.remove(i);
return;
}
}
/*if (playing) {
path.clear();
}*/
}
void AnimationNodeStateMachine::remove_transition_by_index(int p_transition) {
ERR_FAIL_INDEX(p_transition, transitions.size());
transitions.write[p_transition].transition->disconnect("advance_condition_changed", this, "_tree_changed");
transitions.remove(p_transition);
/*if (playing) {
path.clear();
}*/
}
void AnimationNodeStateMachine::set_start_node(const StringName &p_node) {
ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
start_node = p_node;
}
String AnimationNodeStateMachine::get_start_node() const {
return start_node;
}
void AnimationNodeStateMachine::set_end_node(const StringName &p_node) {
ERR_FAIL_COND(p_node != StringName() && !states.has(p_node));
end_node = p_node;
}
String AnimationNodeStateMachine::get_end_node() const {
return end_node;
}
void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
graph_offset = p_offset;
}
Vector2 AnimationNodeStateMachine::get_graph_offset() const {
return graph_offset;
}
float AnimationNodeStateMachine::process(float p_time, bool p_seek) {
Ref<AnimationNodeStateMachinePlayback> playback = get_parameter(this->playback);
ERR_FAIL_COND_V(playback.is_null(), 0.0);
return playback->process(this, p_time, p_seek);
}
String AnimationNodeStateMachine::get_caption() const {
return "StateMachine";
}
void AnimationNodeStateMachine::_notification(int p_what) {
}
Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
return get_node(p_name);
}
bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name.begins_with("states/")) {
String node_name = name.get_slicec('/', 1);
String what = name.get_slicec('/', 2);
if (what == "node") {
Ref<AnimationNode> anode = p_value;
if (anode.is_valid()) {
add_node(node_name, p_value);
}
return true;
}
if (what == "position") {
if (states.has(node_name)) {
states[node_name].position = p_value;
}
return true;
}
} else if (name == "transitions") {
Array trans = p_value;
ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
for (int i = 0; i < trans.size(); i += 3) {
add_transition(trans[i], trans[i + 1], trans[i + 2]);
}
return true;
} else if (name == "start_node") {
set_start_node(p_value);
return true;
} else if (name == "end_node") {
set_end_node(p_value);
return true;
} else if (name == "graph_offset") {
set_graph_offset(p_value);
return true;
}
return false;
}
bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
String name = p_name;
if (name.begins_with("states/")) {
String node_name = name.get_slicec('/', 1);
String what = name.get_slicec('/', 2);
if (what == "node") {
if (states.has(node_name)) {
r_ret = states[node_name].node;
return true;
}
}
if (what == "position") {
if (states.has(node_name)) {
r_ret = states[node_name].position;
return true;
}
}
} else if (name == "transitions") {
Array trans;
trans.resize(transitions.size() * 3);
for (int i = 0; i < transitions.size(); i++) {
trans[i * 3 + 0] = transitions[i].from;
trans[i * 3 + 1] = transitions[i].to;
trans[i * 3 + 2] = transitions[i].transition;
}
r_ret = trans;
return true;
} else if (name == "start_node") {
r_ret = get_start_node();
return true;
} else if (name == "end_node") {
r_ret = get_end_node();
return true;
} else if (name == "graph_offset") {
r_ret = get_graph_offset();
return true;
}
return false;
}
void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
List<StringName> names;
for (RBMap<StringName, State>::Element *E = states.front(); E; E = E->next()) {
names.push_back(E->key());
}
names.sort_custom<StringName::AlphCompare>();
for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
String name = E->get();
p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NOEDITOR));
p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
}
p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
p_list->push_back(PropertyInfo(Variant::STRING_NAME, "start_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
p_list->push_back(PropertyInfo(Variant::STRING_NAME, "end_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
}
void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
ERR_FAIL_COND(!states.has(p_name));
states[p_name].position = p_position;
}
Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
ERR_FAIL_COND_V(!states.has(p_name), Vector2());
return states[p_name].position;
}
void AnimationNodeStateMachine::_tree_changed() {
emit_signal("tree_changed");
}
void AnimationNodeStateMachine::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
ClassDB::bind_method(D_METHOD("set_start_node", "name"), &AnimationNodeStateMachine::set_start_node);
ClassDB::bind_method(D_METHOD("get_start_node"), &AnimationNodeStateMachine::get_start_node);
ClassDB::bind_method(D_METHOD("set_end_node", "name"), &AnimationNodeStateMachine::set_end_node);
ClassDB::bind_method(D_METHOD("get_end_node"), &AnimationNodeStateMachine::get_end_node);
ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeStateMachine::_tree_changed);
}
AnimationNodeStateMachine::AnimationNodeStateMachine() {
playback = "playback";
}

View File

@ -1,218 +0,0 @@
#ifndef ANIMATION_NODE_STATE_MACHINE_H
#define ANIMATION_NODE_STATE_MACHINE_H
/*************************************************************************/
/* animation_node_state_machine.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "scene/animation/animation_tree.h"
class AnimationNodeStateMachineTransition : public Resource {
GDCLASS(AnimationNodeStateMachineTransition, Resource);
public:
enum SwitchMode {
SWITCH_MODE_IMMEDIATE,
SWITCH_MODE_SYNC,
SWITCH_MODE_AT_END,
};
private:
SwitchMode switch_mode;
bool auto_advance;
StringName advance_condition;
StringName advance_condition_name;
float xfade;
bool disabled;
int priority;
protected:
static void _bind_methods();
public:
void set_switch_mode(SwitchMode p_mode);
SwitchMode get_switch_mode() const;
void set_auto_advance(bool p_enable);
bool has_auto_advance() const;
void set_advance_condition(const StringName &p_condition);
StringName get_advance_condition() const;
StringName get_advance_condition_name() const;
void set_xfade_time(float p_xfade);
float get_xfade_time() const;
void set_disabled(bool p_disabled);
bool is_disabled() const;
void set_priority(int p_priority);
int get_priority() const;
AnimationNodeStateMachineTransition();
};
VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::SwitchMode)
class AnimationNodeStateMachine;
class AnimationNodeStateMachinePlayback : public Resource {
GDCLASS(AnimationNodeStateMachinePlayback, Resource);
friend class AnimationNodeStateMachine;
struct AStarCost {
float distance;
StringName prev;
};
float len_total;
float len_current = 0.0;
float pos_current = 0.0;
bool end_loop = false;
StringName current;
StringName fading_from;
float fading_time;
float fading_pos;
Vector<StringName> path;
bool playing;
StringName start_request;
bool start_request_travel;
bool stop_request;
bool _travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel);
float process(AnimationNodeStateMachine *p_state_machine, float p_time, bool p_seek);
protected:
static void _bind_methods();
public:
void travel(const StringName &p_state);
void start(const StringName &p_state);
void stop();
bool is_playing() const;
StringName get_current_node() const;
StringName get_blend_from_node() const;
Vector<StringName> get_travel_path() const;
float get_current_play_pos() const;
float get_current_length() const;
AnimationNodeStateMachinePlayback();
};
class AnimationNodeStateMachine : public AnimationRootNode {
GDCLASS(AnimationNodeStateMachine, AnimationRootNode);
private:
friend class AnimationNodeStateMachinePlayback;
struct State {
Ref<AnimationRootNode> node;
Vector2 position;
};
RBMap<StringName, State> states;
struct Transition {
StringName from;
StringName to;
Ref<AnimationNodeStateMachineTransition> transition;
};
Vector<Transition> transitions;
StringName playback;
StringName start_node;
StringName end_node;
Vector2 graph_offset;
void _tree_changed();
protected:
void _notification(int p_what);
static void _bind_methods();
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2());
void replace_node(const StringName &p_name, Ref<AnimationNode> p_node);
Ref<AnimationNode> get_node(const StringName &p_name) const;
void remove_node(const StringName &p_name);
void rename_node(const StringName &p_name, const StringName &p_new_name);
bool has_node(const StringName &p_name) const;
StringName get_node_name(const Ref<AnimationNode> &p_node) const;
void get_node_list(List<StringName> *r_nodes) const;
void set_node_position(const StringName &p_name, const Vector2 &p_position);
Vector2 get_node_position(const StringName &p_name) const;
virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
bool has_transition(const StringName &p_from, const StringName &p_to) const;
int find_transition(const StringName &p_from, const StringName &p_to) const;
void add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition);
Ref<AnimationNodeStateMachineTransition> get_transition(int p_transition) const;
StringName get_transition_from(int p_transition) const;
StringName get_transition_to(int p_transition) const;
int get_transition_count() const;
void remove_transition_by_index(int p_transition);
void remove_transition(const StringName &p_from, const StringName &p_to);
void set_start_node(const StringName &p_node);
String get_start_node() const;
void set_end_node(const StringName &p_node);
String get_end_node() const;
void set_graph_offset(const Vector2 &p_offset);
Vector2 get_graph_offset() const;
virtual float process(float p_time, bool p_seek);
virtual String get_caption() const;
virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);
AnimationNodeStateMachine();
};
#endif // ANIMATION_NODE_STATE_MACHINE_H

File diff suppressed because it is too large Load Diff

View File

@ -1,341 +0,0 @@
#ifndef ANIMATION_GRAPH_PLAYER_H
#define ANIMATION_GRAPH_PLAYER_H
/*************************************************************************/
/* animation_tree.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "core/object/resource.h"
#include "scene/main/node.h"
#include "scene/animation/animation.h"
class AnimationNodeBlendTree;
class AnimationPlayer;
class AnimationTree;
class AnimationNode : public Resource {
GDCLASS(AnimationNode, Resource);
public:
enum FilterAction {
FILTER_IGNORE,
FILTER_PASS,
FILTER_STOP,
FILTER_BLEND
};
struct Input {
String name;
};
Vector<Input> inputs;
friend class AnimationTree;
struct AnimationState {
Ref<Animation> animation;
float time;
float delta;
const Vector<float> *track_blends;
float blend;
bool seeked;
};
struct State {
int track_count;
HashMap<NodePath, int> track_map;
List<AnimationState> animation_states;
bool valid;
AnimationPlayer *player;
AnimationTree *tree;
String invalid_reasons;
uint64_t last_pass;
};
Vector<float> blends;
State *state;
float _pre_process(const StringName &p_base_path, AnimationNode *p_parent, State *p_state, float p_time, bool p_seek, const Vector<StringName> &p_connections);
//all this is temporary
StringName base_path;
Vector<StringName> connections;
AnimationNode *parent;
HashMap<NodePath, bool> filter;
bool filter_enabled;
Array _get_filters() const;
void _set_filters(const Array &p_filters);
friend class AnimationNodeBlendTree;
float _blend_node(const StringName &p_subpath, const Vector<StringName> &p_connections, AnimationNode *p_new_parent, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true, float *r_max = nullptr);
protected:
void blend_animation(const StringName &p_animation, float p_time, float p_delta, bool p_seeked, float p_blend);
float blend_node(const StringName &p_sub_path, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true);
float blend_input(int p_input, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true);
void make_invalid(const String &p_reason);
static void _bind_methods();
void _validate_property(PropertyInfo &property) const;
public:
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
void set_parameter(const StringName &p_name, const Variant &p_value);
Variant get_parameter(const StringName &p_name) const;
struct ChildNode {
StringName name;
Ref<AnimationNode> node;
};
virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
virtual float process(float p_time, bool p_seek);
virtual String get_caption() const;
int get_input_count() const;
String get_input_name(int p_input);
void add_input(const String &p_name);
void set_input_name(int p_input, const String &p_name);
void remove_input(int p_index);
void set_filter_path(const NodePath &p_path, bool p_enable);
bool is_path_filtered(const NodePath &p_path) const;
void set_filter_enabled(bool p_enable);
bool is_filter_enabled() const;
virtual bool has_filter() const;
virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);
AnimationNode();
};
VARIANT_ENUM_CAST(AnimationNode::FilterAction)
//root node does not allow inputs
class AnimationRootNode : public AnimationNode {
GDCLASS(AnimationRootNode, AnimationNode);
public:
AnimationRootNode() {}
};
class AnimationTree : public Node {
GDCLASS(AnimationTree, Node);
public:
enum AnimationProcessMode {
ANIMATION_PROCESS_PHYSICS,
ANIMATION_PROCESS_IDLE,
ANIMATION_PROCESS_MANUAL,
};
private:
struct TrackCache {
bool root_motion;
uint64_t setup_pass;
uint64_t process_pass;
Animation::TrackType type;
Object *object;
ObjectID object_id;
TrackCache() {
type = Animation::TYPE_ANIMATION;
root_motion = false;
setup_pass = 0;
process_pass = 0;
object = nullptr;
object_id = 0;
}
virtual ~TrackCache() {}
};
struct TrackCacheTransform : public TrackCache {
bool loc_used;
bool rot_used;
bool scale_used;
Vector3 init_loc;
Quaternion ref_rot;
Quaternion init_rot;
Vector3 init_scale;
Vector3 loc;
Quaternion rot;
Vector3 scale;
TrackCacheTransform() {
type = Animation::TYPE_POSITION_3D;
loc_used = false;
rot_used = false;
scale_used = false;
init_loc = Vector3(0, 0, 0);
ref_rot = Quaternion(0, 0, 0, 1);
init_rot = Quaternion(0, 0, 0, 0);
init_scale = Vector3(1, 1, 1);
}
};
struct TrackCacheValue : public TrackCache {
Variant init_value;
Variant value;
Vector<StringName> subpath;
TrackCacheValue() { type = Animation::TYPE_VALUE; }
};
struct TrackCacheMethod : public TrackCache {
TrackCacheMethod() { type = Animation::TYPE_METHOD; }
};
struct TrackCacheBezier : public TrackCache {
real_t init_value;
float value;
Vector<StringName> subpath;
TrackCacheBezier() {
type = Animation::TYPE_BEZIER;
value = 0;
init_value = 0.0;
}
};
struct TrackCacheAudio : public TrackCache {
bool playing;
float start;
float len;
TrackCacheAudio() {
type = Animation::TYPE_AUDIO;
playing = false;
start = 0;
len = 0;
}
};
struct TrackCacheAnimation : public TrackCache {
bool playing;
TrackCacheAnimation() {
type = Animation::TYPE_ANIMATION;
playing = false;
}
};
HashMap<NodePath, TrackCache *> track_cache;
RBSet<TrackCache *> playing_caches;
Ref<AnimationNode> root;
AnimationProcessMode process_mode;
bool active;
NodePath animation_player;
AnimationNode::State state;
bool cache_valid;
void _node_removed(Node *p_node);
void _clear_caches();
bool _update_caches(AnimationPlayer *player);
void _process_graph(float p_delta);
uint64_t setup_pass;
uint64_t process_pass;
bool started;
NodePath root_motion_track;
Transform root_motion_transform;
friend class AnimationNode;
bool properties_dirty;
void _tree_changed();
void _update_properties();
List<PropertyInfo> properties;
HashMap<StringName, HashMap<StringName, StringName>> property_parent_map;
HashMap<StringName, Variant> property_map;
struct Activity {
uint64_t last_pass;
float activity;
};
HashMap<StringName, Vector<Activity>> input_activity_map;
HashMap<StringName, Vector<Activity> *> input_activity_map_get;
void _update_properties_for_node(const String &p_base_path, Ref<AnimationNode> node);
ObjectID last_animation_player;
protected:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
void _notification(int p_what);
static void _bind_methods();
public:
void set_tree_root(const Ref<AnimationNode> &p_root);
Ref<AnimationNode> get_tree_root() const;
void set_active(bool p_active);
bool is_active() const;
void set_process_mode(AnimationProcessMode p_mode);
AnimationProcessMode get_process_mode() const;
void set_animation_player(const NodePath &p_player);
NodePath get_animation_player() const;
virtual String get_configuration_warning() const;
bool is_state_invalid() const;
String get_invalid_state_reason() const;
void set_root_motion_track(const NodePath &p_track);
NodePath get_root_motion_track() const;
Transform get_root_motion_transform() const;
float get_connection_activity(const StringName &p_path, int p_connection) const;
void advance(float p_time);
void rename_parameter(const String &p_base, const String &p_new_base);
uint64_t get_last_process_pass() const;
AnimationTree();
~AnimationTree();
};
VARIANT_ENUM_CAST(AnimationTree::AnimationProcessMode)
#endif // ANIMATION_GRAPH_PLAYER_H

View File

@ -62,14 +62,10 @@
#include "scene/2d/touch_screen_button.h"
#include "scene/2d/visibility_notifier_2d.h"
#include "scene/2d/y_sort.h"
#include "scene/animation/animation_blend_space_1d.h"
#include "scene/animation/animation_blend_space_2d.h"
#include "scene/animation/animation_blend_tree.h"
#include "scene/animation/animation_node_state_machine.h"
#include "scene/animation/animation_player.h"
#include "scene/animation/animation_tree.h"
#include "scene/animation/scene_tree_tween.h"
#include "scene/animation/tween.h"
#include "scene/animation/animation.h"
#include "scene/audio/audio_stream_player.h"
#include "scene/gui/aspect_ratio_container.h"
#include "scene/gui/box_container.h"
@ -333,27 +329,6 @@ void register_scene_types() {
ClassDB::register_class<CallbackTweener>();
ClassDB::register_class<MethodTweener>();
ClassDB::register_class<AnimationTree>();
ClassDB::register_class<AnimationNode>();
ClassDB::register_class<AnimationRootNode>();
ClassDB::register_class<AnimationNodeBlendTree>();
ClassDB::register_class<AnimationNodeBlendSpace1D>();
ClassDB::register_class<AnimationNodeBlendSpace2D>();
ClassDB::register_class<AnimationNodeStateMachine>();
ClassDB::register_class<AnimationNodeStateMachinePlayback>();
ClassDB::register_class<AnimationNodeStateMachineTransition>();
ClassDB::register_class<AnimationNodeOutput>();
ClassDB::register_class<AnimationNodeOneShot>();
ClassDB::register_class<AnimationNodeAnimation>();
ClassDB::register_class<AnimationNodeAdd2>();
ClassDB::register_class<AnimationNodeAdd3>();
ClassDB::register_class<AnimationNodeBlend2>();
ClassDB::register_class<AnimationNodeBlend3>();
ClassDB::register_class<AnimationNodeTimeScale>();
ClassDB::register_class<AnimationNodeTimeSeek>();
ClassDB::register_class<AnimationNodeTransition>();
OS::get_singleton()->yield(); //may take time to init
#ifndef _3D_DISABLED