| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/expression.h" |
| 34 |
#include "scene/animation/animation_tree.h" |
| 35 |
#include "scene/resources/curve.h" |
| 36 |
|
| 37 |
class AnimationNodeStateMachineTransition : public Resource { |
| 38 |
GDCLASS(AnimationNodeStateMachineTransition, Resource); |
| 39 |
|
| 40 |
public: |
| 41 |
enum SwitchMode { |
| 42 |
SWITCH_MODE_IMMEDIATE, |
| 43 |
SWITCH_MODE_SYNC, |
| 44 |
SWITCH_MODE_AT_END, |
| 45 |
}; |
| 46 |
|
| 47 |
enum AdvanceMode { |
| 48 |
ADVANCE_MODE_DISABLED, |
| 49 |
ADVANCE_MODE_ENABLED, |
| 50 |
ADVANCE_MODE_AUTO, |
| 51 |
}; |
| 52 |
|
| 53 |
private: |
| 54 |
SwitchMode switch_mode = SWITCH_MODE_IMMEDIATE; |
| 55 |
AdvanceMode advance_mode = ADVANCE_MODE_ENABLED; |
| 56 |
StringName advance_condition; |
| 57 |
StringName advance_condition_name; |
| 58 |
float xfade_time = 0.0; |
| 59 |
Ref<Curve> xfade_curve; |
| 60 |
bool break_loop_at_end = false; |
| 61 |
bool reset = true; |
| 62 |
int priority = 1; |
| 63 |
String advance_expression; |
| 64 |
|
| 65 |
friend class AnimationNodeStateMachinePlayback; |
| 66 |
Ref<Expression> expression; |
| 67 |
|
| 68 |
protected: |
| 69 |
static void _bind_methods(); |
| 70 |
|
| 71 |
public: |
| 72 |
void set_switch_mode(SwitchMode p_mode); |
| 73 |
SwitchMode get_switch_mode() const; |
| 74 |
|
| 75 |
void set_advance_mode(AdvanceMode p_mode); |
| 76 |
AdvanceMode get_advance_mode() const; |
| 77 |
|
| 78 |
void set_advance_condition(const StringName &p_condition); |
| 79 |
StringName get_advance_condition() const; |
| 80 |
|
| 81 |
StringName get_advance_condition_name() const; |
| 82 |
|
| 83 |
void set_advance_expression(const String &p_expression); |
| 84 |
String get_advance_expression() const; |
| 85 |
|
| 86 |
void set_xfade_time(float p_xfade); |
| 87 |
float get_xfade_time() const; |
| 88 |
|
| 89 |
void set_break_loop_at_end(bool p_enable); |
| 90 |
bool is_loop_broken_at_end() const; |
| 91 |
|
| 92 |
void set_reset(bool p_reset); |
| 93 |
bool is_reset() const; |
| 94 |
|
| 95 |
void set_xfade_curve(const Ref<Curve> &p_curve); |
| 96 |
Ref<Curve> get_xfade_curve() const; |
| 97 |
|
| 98 |
void set_priority(int p_priority); |
| 99 |
int get_priority() const; |
| 100 |
|
| 101 |
AnimationNodeStateMachineTransition(); |
| 102 |
}; |
| 103 |
|
| 104 |
VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::SwitchMode) |
| 105 |
VARIANT_ENUM_CAST(AnimationNodeStateMachineTransition::AdvanceMode) |
| 106 |
|
| 107 |
class AnimationNodeStateMachinePlayback; |
| 108 |
|
| 109 |
class AnimationNodeStateMachine : public AnimationRootNode { |
| 110 |
GDCLASS(AnimationNodeStateMachine, AnimationRootNode); |
| 111 |
|
| 112 |
public: |
| 113 |
enum StateMachineType { |
| 114 |
STATE_MACHINE_TYPE_ROOT, |
| 115 |
STATE_MACHINE_TYPE_NESTED, |
| 116 |
STATE_MACHINE_TYPE_GROUPED, |
| 117 |
}; |
| 118 |
|
| 119 |
private: |
| 120 |
friend class AnimationNodeStateMachinePlayback; |
| 121 |
|
| 122 |
StateMachineType state_machine_type = STATE_MACHINE_TYPE_ROOT; |
| 123 |
|
| 124 |
struct State { |
| 125 |
Ref<AnimationRootNode> node; |
| 126 |
Vector2 position; |
| 127 |
}; |
| 128 |
|
| 129 |
HashMap<StringName, State> states; |
| 130 |
bool allow_transition_to_self = false; |
| 131 |
bool reset_ends = false; |
| 132 |
|
| 133 |
struct Transition { |
| 134 |
StringName from; |
| 135 |
StringName to; |
| 136 |
Ref<AnimationNodeStateMachineTransition> transition; |
| 137 |
}; |
| 138 |
|
| 139 |
Vector<Transition> transitions; |
| 140 |
|
| 141 |
StringName playback = "playback"; |
| 142 |
bool updating_transitions = false; |
| 143 |
|
| 144 |
Vector2 graph_offset; |
| 145 |
|
| 146 |
void _remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition); |
| 147 |
void _rename_transitions(const StringName &p_name, const StringName &p_new_name); |
| 148 |
bool _can_connect(const StringName &p_name); |
| 149 |
|
| 150 |
protected: |
| 151 |
static void _bind_methods(); |
| 152 |
|
| 153 |
bool _set(const StringName &p_name, const Variant &p_value); |
| 154 |
bool _get(const StringName &p_name, Variant &r_ret) const; |
| 155 |
void _get_property_list(List<PropertyInfo> *p_list) const; |
| 156 |
void _validate_property(PropertyInfo &p_property) const; |
| 157 |
|
| 158 |
bool _check_advance_condition(const Ref<AnimationNodeStateMachine> p_state_machine, const Ref<AnimationNodeStateMachineTransition> p_transition) const; |
| 159 |
|
| 160 |
virtual void _tree_changed() override; |
| 161 |
virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) override; |
| 162 |
virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node) override; |
| 163 |
|
| 164 |
virtual void reset_state() override; |
| 165 |
|
| 166 |
public: |
| 167 |
virtual void get_parameter_list(List<PropertyInfo> *r_list) const override; |
| 168 |
virtual Variant get_parameter_default_value(const StringName &p_parameter) const override; |
| 169 |
virtual bool is_parameter_read_only(const StringName &p_parameter) const override; |
| 170 |
|
| 171 |
void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2()); |
| 172 |
void replace_node(const StringName &p_name, Ref<AnimationNode> p_node); |
| 173 |
Ref<AnimationNode> get_node(const StringName &p_name) const; |
| 174 |
void remove_node(const StringName &p_name); |
| 175 |
void rename_node(const StringName &p_name, const StringName &p_new_name); |
| 176 |
bool has_node(const StringName &p_name) const; |
| 177 |
StringName get_node_name(const Ref<AnimationNode> &p_node) const; |
| 178 |
LocalVector<StringName> get_node_list() const; |
| 179 |
TypedArray<StringName> get_node_list_as_typed_array() const; |
| 180 |
|
| 181 |
void set_node_position(const StringName &p_name, const Vector2 &p_position); |
| 182 |
Vector2 get_node_position(const StringName &p_name) const; |
| 183 |
|
| 184 |
virtual void get_child_nodes(List<ChildNode> *r_child_nodes) override; |
| 185 |
|
| 186 |
bool has_transition(const StringName &p_from, const StringName &p_to) const; |
| 187 |
bool has_transition_from(const StringName &p_from) const; |
| 188 |
bool has_transition_to(const StringName &p_to) const; |
| 189 |
int find_transition(const StringName &p_from, const StringName &p_to) const; |
| 190 |
Vector<int> find_transition_from(const StringName &p_from) const; |
| 191 |
Vector<int> find_transition_to(const StringName &p_to) const; |
| 192 |
void add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition); |
| 193 |
Ref<AnimationNodeStateMachineTransition> get_transition(int p_transition) const; |
| 194 |
StringName get_transition_from(int p_transition) const; |
| 195 |
StringName get_transition_to(int p_transition) const; |
| 196 |
int get_transition_count() const; |
| 197 |
bool is_transition_across_group(int p_transition) const; |
| 198 |
void remove_transition_by_index(const int p_transition); |
| 199 |
void remove_transition(const StringName &p_from, const StringName &p_to); |
| 200 |
|
| 201 |
void set_state_machine_type(StateMachineType p_state_machine_type); |
| 202 |
StateMachineType get_state_machine_type() const; |
| 203 |
|
| 204 |
void set_allow_transition_to_self(bool p_enable); |
| 205 |
bool is_allow_transition_to_self() const; |
| 206 |
|
| 207 |
void set_reset_ends(bool p_enable); |
| 208 |
bool are_ends_reset() const; |
| 209 |
|
| 210 |
bool can_edit_node(const StringName &p_name) const; |
| 211 |
|
| 212 |
void set_graph_offset(const Vector2 &p_offset); |
| 213 |
Vector2 get_graph_offset() const; |
| 214 |
|
| 215 |
virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override; |
| 216 |
virtual String get_caption() const override; |
| 217 |
|
| 218 |
virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) const override; |
| 219 |
|
| 220 |
#ifdef TOOLS_ENABLED |
| 221 |
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override; |
| 222 |
#endif |
| 223 |
|
| 224 |
Vector<StringName> get_nodes_with_transitions_from(const StringName &p_node) const; |
| 225 |
Vector<StringName> get_nodes_with_transitions_to(const StringName &p_node) const; |
| 226 |
|
| 227 |
AnimationNodeStateMachine(); |
| 228 |
}; |
| 229 |
|
| 230 |
VARIANT_ENUM_CAST(AnimationNodeStateMachine::StateMachineType); |
| 231 |
|
| 232 |
class AnimationNodeStateMachinePlayback : public Resource { |
| 233 |
GDCLASS(AnimationNodeStateMachinePlayback, Resource); |
| 234 |
|
| 235 |
friend class AnimationNodeStateMachine; |
| 236 |
|
| 237 |
struct AStarCost { |
| 238 |
float distance = 0.0; |
| 239 |
StringName prev; |
| 240 |
}; |
| 241 |
|
| 242 |
struct TransitionInfo { |
| 243 |
StringName from; |
| 244 |
StringName to; |
| 245 |
StringName next; |
| 246 |
}; |
| 247 |
|
| 248 |
struct NextInfo { |
| 249 |
StringName node; |
| 250 |
double xfade; |
| 251 |
Ref<Curve> curve; |
| 252 |
AnimationNodeStateMachineTransition::SwitchMode switch_mode; |
| 253 |
bool is_reset; |
| 254 |
bool break_loop_at_end; |
| 255 |
}; |
| 256 |
|
| 257 |
struct ChildStateMachineInfo { |
| 258 |
Ref<AnimationNodeStateMachinePlayback> playback; |
| 259 |
Vector<StringName> path; |
| 260 |
bool is_reset = false; |
| 261 |
}; |
| 262 |
|
| 263 |
Ref<AnimationNodeStateMachineTransition> default_transition; |
| 264 |
String base_path; |
| 265 |
|
| 266 |
AnimationNode::NodeTimeInfo current_nti; |
| 267 |
StringName current = SceneStringName(Start); |
| 268 |
Ref<Curve> current_curve; |
| 269 |
|
| 270 |
Ref<AnimationNodeStateMachineTransition> group_start_transition; |
| 271 |
Ref<AnimationNodeStateMachineTransition> group_end_transition; |
| 272 |
|
| 273 |
AnimationNode::NodeTimeInfo fadeing_from_nti; |
| 274 |
StringName fading_from; |
| 275 |
float fading_time = 0.0; |
| 276 |
float fading_pos = 0.0; |
| 277 |
|
| 278 |
Vector<StringName> path; |
| 279 |
bool playing = false; |
| 280 |
|
| 281 |
StringName start_request; |
| 282 |
StringName travel_request; |
| 283 |
bool reset_request = false; |
| 284 |
bool reset_request_on_teleport = false; |
| 285 |
bool _reset_request_for_fading_from = false; |
| 286 |
bool next_request = false; |
| 287 |
bool stop_request = false; |
| 288 |
bool teleport_request = false; |
| 289 |
|
| 290 |
bool is_grouped = false; |
| 291 |
|
| 292 |
void _clear_fading(AnimationNodeStateMachine *p_state_machine, const StringName &p_state); |
| 293 |
void _signal_state_change(AnimationTree *p_animation_tree, const StringName &p_state, bool p_started); |
| 294 |
void _travel_main(const StringName &p_state, bool p_reset_on_teleport = true); |
| 295 |
void _start_main(const StringName &p_state, bool p_reset = true); |
| 296 |
void _next_main(); |
| 297 |
void _stop_main(); |
| 298 |
|
| 299 |
bool _make_travel_path(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, Vector<StringName> &r_path, bool p_test_only); |
| 300 |
String _validate_path(AnimationNodeStateMachine *p_state_machine, const String &p_path); |
| 301 |
bool _travel(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, bool p_test_only); |
| 302 |
void _start(AnimationNodeStateMachine *p_state_machine); |
| 303 |
|
| 304 |
void _clear_path_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_test_only); |
| 305 |
bool _travel_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_is_allow_transition_to_self, bool p_is_parent_same_state, bool p_test_only); |
| 306 |
void _start_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_test_only); |
| 307 |
|
| 308 |
AnimationNode::NodeTimeInfo process(AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only); |
| 309 |
AnimationNode::NodeTimeInfo _process(AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only); |
| 310 |
|
| 311 |
bool _check_advance_condition(const Ref<AnimationNodeStateMachine> p_state_machine, const Ref<AnimationNodeStateMachineTransition> p_transition) const; |
| 312 |
bool _transition_to_next_recursive(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, double p_delta, bool p_test_only); |
| 313 |
NextInfo _find_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine) const; |
| 314 |
Ref<AnimationNodeStateMachineTransition> _check_group_transition(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const AnimationNodeStateMachine::Transition &p_transition, Ref<AnimationNodeStateMachine> &r_state_machine, bool &r_bypass) const; |
| 315 |
bool _can_transition_to_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, NextInfo p_next, bool p_test_only); |
| 316 |
|
| 317 |
void _set_current(AnimationNodeStateMachine *p_state_machine, const StringName &p_state); |
| 318 |
void _set_grouped(bool p_is_grouped); |
| 319 |
void _set_base_path(const String &p_base_path); |
| 320 |
Ref<AnimationNodeStateMachinePlayback> _get_parent_playback(AnimationTree *p_tree) const; |
| 321 |
Ref<AnimationNodeStateMachine> _get_parent_state_machine(AnimationTree *p_tree) const; |
| 322 |
Ref<AnimationNodeStateMachineTransition> _get_group_start_transition() const; |
| 323 |
Ref<AnimationNodeStateMachineTransition> _get_group_end_transition() const; |
| 324 |
|
| 325 |
TypedArray<StringName> _get_travel_path() const; |
| 326 |
|
| 327 |
protected: |
| 328 |
static void _bind_methods(); |
| 329 |
|
| 330 |
public: |
| 331 |
void travel(const StringName &p_state, bool p_reset_on_teleport = true); |
| 332 |
void start(const StringName &p_state, bool p_reset = true); |
| 333 |
void next(); |
| 334 |
void stop(); |
| 335 |
bool is_playing() const; |
| 336 |
bool is_end() const; |
| 337 |
StringName get_current_node() const; |
| 338 |
StringName get_fading_from_node() const; |
| 339 |
Vector<StringName> get_travel_path() const; |
| 340 |
float get_current_play_pos() const; |
| 341 |
float get_current_length() const; |
| 342 |
|
| 343 |
float get_fading_from_play_pos() const; |
| 344 |
float get_fading_from_length() const; |
| 345 |
|
| 346 |
float get_fading_time() const; |
| 347 |
float get_fading_pos() const; |
| 348 |
|
| 349 |
void clear_path(); |
| 350 |
void push_path(const StringName &p_state); |
| 351 |
|
| 352 |
AnimationNodeStateMachinePlayback(); |
| 353 |
}; |