Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AnimationNode lifecycle callbacks #41771

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions doc/classes/AnimationNode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@
This function should return the time left for the current animation to finish (if unsure, pass the value from the main blend being called).
</description>
</method>
<method name="_on_play" qualifiers="virtual">
<return type="void">
</return>
<argument index="0" name="time" type="float">
</argument>
<description>
Called when a custom node begins processing. The [code]time[/code] parameter is the time remaining in the node's current animation, which is the same value returned by [method process].
When an animation starts, [code]time[/code] should be the same as the length of the animation.
</description>
</method>
<method name="_advance" qualifiers="virtual">
<return type="void">
</return>
<argument index="0" name="delta" type="float">
</argument>
<description>
Called when the custom node is processed by [AnimationTree]. The [code]delta[/code] parameter is the time (in seconds) since the last advance callback. It should be the same delta value provided to [method process] when [code]seek[/code] is [code]false[/code].
</description>
</method>
<method name="_on_stop" qualifiers="virtual">
<return type="void">
</return>
<argument index="0" name="time" type="float">
</argument>
<description>
Called when a custom node stops processing. The [code]time[/code] parameter is the time remaining in the node's current animation, which is the same value returned by [method process].
</description>
</method>
<method name="remove_input">
<return type="void">
</return>
Expand Down Expand Up @@ -206,6 +234,12 @@
<member name="filter_enabled" type="bool" setter="set_filter_enabled" getter="is_filter_enabled">
If [code]true[/code], filtering is enabled.
</member>
<member name="is_processing" type="bool">
Returns [code]true[/code] when [method process] is called on the current [AnimationTree] frame.
</member>
<member name="child_nodes" type="Dictionary">
Returns all child nodes as a [code]name: node[/code] dictionary. Custom nodes can change this behavior by overriding [method get_child_nodes].
</member>
</members>
<signals>
<signal name="removed_from_graph">
Expand Down
17 changes: 11 additions & 6 deletions scene/animation/animation_blend_space_1d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,18 @@ void AnimationNodeBlendSpace1D::_bind_methods() {
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);
int AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {
int child_count = AnimationNode::get_child_nodes(r_child_nodes);
if (child_count == 0) {
child_count = blend_points_used;
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);
}
}
return child_count;
}

void AnimationNodeBlendSpace1D::add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index) {
Expand Down
2 changes: 1 addition & 1 deletion scene/animation/animation_blend_space_1d.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class AnimationNodeBlendSpace1D : public AnimationRootNode {
virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;

virtual void get_child_nodes(List<ChildNode> *r_child_nodes) override;
virtual int get_child_nodes(List<ChildNode> *r_child_nodes) override;

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);
Expand Down
19 changes: 13 additions & 6 deletions scene/animation/animation_blend_space_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ Variant AnimationNodeBlendSpace2D::get_parameter_default_value(const StringName
}
}

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);
int AnimationNodeBlendSpace2D::get_child_nodes(List<ChildNode> *r_child_nodes) {
int child_count = AnimationNode::get_child_nodes(r_child_nodes);

if (child_count == 0) {
child_count = blend_points_used;
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);
}
}

return child_count;
}

void AnimationNodeBlendSpace2D::add_blend_point(const Ref<AnimationRootNode> &p_node, const Vector2 &p_position, int p_at_index) {
Expand Down
2 changes: 1 addition & 1 deletion scene/animation/animation_blend_space_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class AnimationNodeBlendSpace2D : public AnimationRootNode {
virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;

virtual void get_child_nodes(List<ChildNode> *r_child_nodes) override;
virtual int get_child_nodes(List<ChildNode> *r_child_nodes) override;

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);
Expand Down
29 changes: 17 additions & 12 deletions scene/animation/animation_blend_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,21 +885,26 @@ Vector2 AnimationNodeBlendTree::get_node_position(const StringName &p_node) cons
return nodes[p_node].position;
}

void AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) {
Vector<StringName> ns;

for (Map<StringName, Node>::Element *E = nodes.front(); E; E = E->next()) {
ns.push_back(E->key());
}
int AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) {
int child_count = AnimationNode::get_child_nodes(r_child_nodes);
if (child_count == 0) {
Vector<StringName> ns;
child_count = nodes.size();

for (Map<StringName, Node>::Element *E = nodes.front(); E; E = E->next()) {
ns.push_back(E->key());
}

ns.sort_custom<StringName::AlphCompare>();
ns.sort_custom<StringName::AlphCompare>();

for (int i = 0; i < ns.size(); i++) {
ChildNode cn;
cn.name = ns[i];
cn.node = nodes[cn.name].node;
r_child_nodes->push_back(cn);
for (int i = 0; i < ns.size(); i++) {
ChildNode cn;
cn.name = ns[i];
cn.node = nodes[cn.name].node;
r_child_nodes->push_back(cn);
}
}
return child_count;
}

bool AnimationNodeBlendTree::has_node(const StringName &p_name) const {
Expand Down
2 changes: 1 addition & 1 deletion scene/animation/animation_blend_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class AnimationNodeBlendTree : public AnimationRootNode {
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) override;
virtual int get_child_nodes(List<ChildNode> *r_child_nodes) override;

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);
Expand Down
28 changes: 17 additions & 11 deletions scene/animation/animation_node_state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,21 +593,27 @@ StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_
ERR_FAIL_V(StringName());
}

void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
Vector<StringName> nodes;
int AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
int child_count = AnimationNode::get_child_nodes(r_child_nodes);

for (Map<StringName, State>::Element *E = states.front(); E; E = E->next()) {
nodes.push_back(E->key());
}
if (child_count == 0) {
Vector<StringName> nodes;

nodes.sort_custom<StringName::AlphCompare>();
for (Map<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);
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);
}
child_count = nodes.size();
}
return child_count;
}

bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
Expand Down
2 changes: 1 addition & 1 deletion scene/animation/animation_node_state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class AnimationNodeStateMachine : public AnimationRootNode {
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) override;
virtual int get_child_nodes(List<ChildNode> *r_child_nodes) override;

bool has_transition(const StringName &p_from, const StringName &p_to) const;
int find_transition(const StringName &p_from, const StringName &p_to) const;
Expand Down
Loading