| 1 | /**************************************************************************/ |
| 2 | /* animation_blend_tree.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "animation_blend_tree.h" |
| 32 | |
| 33 | #include "scene/resources/animation.h" |
| 34 | |
| 35 | void AnimationNodeAnimation::set_animation(const StringName &p_name) { |
| 36 | animation = p_name; |
| 37 | } |
| 38 | |
| 39 | StringName AnimationNodeAnimation::get_animation() const { |
| 40 | return animation; |
| 41 | } |
| 42 | |
| 43 | Vector<String> (*AnimationNodeAnimation::get_editable_animation_list)() = nullptr; |
| 44 | |
| 45 | void AnimationNodeAnimation::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 46 | AnimationNode::get_parameter_list(r_list); |
| 47 | r_list->push_back(PropertyInfo(Variant::BOOL, backward, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE)); |
| 48 | } |
| 49 | |
| 50 | Variant AnimationNodeAnimation::get_parameter_default_value(const StringName &p_parameter) const { |
| 51 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 52 | if (ret != Variant()) { |
| 53 | return ret; |
| 54 | } |
| 55 | if (p_parameter == backward) { |
| 56 | return false; |
| 57 | } |
| 58 | return 0.0; |
| 59 | } |
| 60 | |
| 61 | AnimationNode::NodeTimeInfo AnimationNodeAnimation::get_node_time_info() const { |
| 62 | NodeTimeInfo nti; |
| 63 | if (!process_state->tree->has_animation(animation)) { |
| 64 | return nti; |
| 65 | } |
| 66 | |
| 67 | if (use_custom_timeline) { |
| 68 | nti.length = timeline_length; |
| 69 | nti.loop_mode = loop_mode; |
| 70 | } else { |
| 71 | Ref<Animation> anim = process_state->tree->get_animation(animation); |
| 72 | nti.length = (double)anim->get_length(); |
| 73 | nti.loop_mode = anim->get_loop_mode(); |
| 74 | } |
| 75 | nti.position = get_parameter(current_position); |
| 76 | |
| 77 | return nti; |
| 78 | } |
| 79 | |
| 80 | void AnimationNodeAnimation::_validate_property(PropertyInfo &p_property) const { |
| 81 | if (Engine::get_singleton()->is_editor_hint() && p_property.name == "animation" && get_editable_animation_list) { |
| 82 | Vector<String> names = get_editable_animation_list(); |
| 83 | String anims; |
| 84 | for (int i = 0; i < names.size(); i++) { |
| 85 | if (i > 0) { |
| 86 | anims += ","; |
| 87 | } |
| 88 | anims += String(names[i]); |
| 89 | } |
| 90 | if (!anims.is_empty()) { |
| 91 | p_property.hint = PROPERTY_HINT_ENUM; |
| 92 | p_property.hint_string = anims; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (!use_custom_timeline) { |
| 97 | if (p_property.name == "timeline_length" || p_property.name == "start_offset" || p_property.name == "loop_mode" || p_property.name == "stretch_time_scale") { |
| 98 | p_property.usage = PROPERTY_USAGE_NONE; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | AnimationNode::NodeTimeInfo AnimationNodeAnimation::process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 104 | process_state->is_testing = p_test_only; |
| 105 | |
| 106 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 107 | if (p_playback_info.seeked) { |
| 108 | if (p_playback_info.is_external_seeking) { |
| 109 | pi.delta = get_node_time_info().position - p_playback_info.time; |
| 110 | } |
| 111 | } else { |
| 112 | pi.time = get_node_time_info().position + (get_parameter(backward) ? -p_playback_info.delta : p_playback_info.delta); |
| 113 | } |
| 114 | |
| 115 | NodeTimeInfo nti = _process(pi, p_test_only); |
| 116 | |
| 117 | if (!p_test_only) { |
| 118 | set_node_time_info(nti); |
| 119 | } |
| 120 | |
| 121 | return nti; |
| 122 | } |
| 123 | |
| 124 | AnimationNode::NodeTimeInfo AnimationNodeAnimation::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 125 | if (!process_state->tree->has_animation(animation)) { |
| 126 | AnimationNodeBlendTree *tree = Object::cast_to<AnimationNodeBlendTree>(node_state.parent); |
| 127 | if (tree) { |
| 128 | String node_name = tree->get_node_name(Ref<AnimationNodeAnimation>(this)); |
| 129 | make_invalid(vformat(RTR("On BlendTree node '%s', animation not found: '%s'"), node_name, animation)); |
| 130 | |
| 131 | } else { |
| 132 | make_invalid(vformat(RTR("Animation not found: '%s'"), animation)); |
| 133 | } |
| 134 | |
| 135 | return NodeTimeInfo(); |
| 136 | } |
| 137 | |
| 138 | Ref<Animation> anim = process_state->tree->get_animation(animation); |
| 139 | double anim_size = (double)anim->get_length(); |
| 140 | |
| 141 | NodeTimeInfo cur_nti = get_node_time_info(); |
| 142 | double cur_len = cur_nti.length; |
| 143 | double cur_time = p_playback_info.time; |
| 144 | double cur_delta = p_playback_info.delta; |
| 145 | bool cur_backward = get_parameter(backward); |
| 146 | |
| 147 | Animation::LoopMode cur_loop_mode = cur_nti.loop_mode; |
| 148 | double prev_time = cur_nti.position; |
| 149 | |
| 150 | Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE; |
| 151 | bool node_backward = play_mode == PLAY_MODE_BACKWARD; |
| 152 | |
| 153 | bool p_seek = p_playback_info.seeked; |
| 154 | bool p_is_external_seeking = p_playback_info.is_external_seeking; |
| 155 | |
| 156 | // 1. Progress for AnimationNode. |
| 157 | bool will_end = Animation::is_greater_or_equal_approx(cur_time + cur_delta, cur_len); |
| 158 | bool is_started = p_seek && !p_is_external_seeking && Math::is_zero_approx(cur_time); |
| 159 | bool immediately_after_start = is_started && advance_on_start; |
| 160 | |
| 161 | // 1. Progress for AnimationNode. |
| 162 | if (immediately_after_start) { |
| 163 | cur_time = cur_delta; |
| 164 | } |
| 165 | if (cur_loop_mode != Animation::LOOP_NONE) { |
| 166 | if (cur_loop_mode == Animation::LOOP_LINEAR) { |
| 167 | if (!Math::is_zero_approx(cur_len)) { |
| 168 | cur_time = Math::fposmod(cur_time, cur_len); |
| 169 | } |
| 170 | cur_backward = false; |
| 171 | } else { |
| 172 | if (!Math::is_zero_approx(cur_len)) { |
| 173 | if (Animation::is_greater_or_equal_approx(prev_time, 0) && Animation::is_less_approx(cur_time, 0)) { |
| 174 | cur_backward = !cur_backward; |
| 175 | } else if (Animation::is_less_or_equal_approx(prev_time, cur_len) && Animation::is_greater_approx(cur_time, cur_len)) { |
| 176 | cur_backward = !cur_backward; |
| 177 | } |
| 178 | cur_time = Math::pingpong(cur_time, cur_len); |
| 179 | } |
| 180 | } |
| 181 | } else { |
| 182 | if (Animation::is_less_approx(cur_time, 0)) { |
| 183 | cur_delta += cur_time; |
| 184 | cur_time = 0; |
| 185 | } else if (Animation::is_greater_approx(cur_time, cur_len)) { |
| 186 | cur_delta += cur_time - cur_len; |
| 187 | cur_time = cur_len; |
| 188 | } |
| 189 | cur_backward = false; |
| 190 | // If ended, don't progress AnimationNode. So set delta to 0. |
| 191 | if (!Math::is_zero_approx(cur_delta)) { |
| 192 | if (play_mode == PLAY_MODE_FORWARD) { |
| 193 | if (Animation::is_greater_or_equal_approx(prev_time, cur_len)) { |
| 194 | cur_delta = 0; |
| 195 | } |
| 196 | } else { |
| 197 | if (Animation::is_less_or_equal_approx(prev_time, 0)) { |
| 198 | cur_delta = 0; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // 2. For return, store "AnimationNode" time info here, not "Animation" time info as below. |
| 205 | NodeTimeInfo nti; |
| 206 | nti.length = cur_len; |
| 207 | nti.position = cur_time; |
| 208 | nti.delta = cur_delta; |
| 209 | nti.loop_mode = cur_loop_mode; |
| 210 | nti.will_end = will_end; |
| 211 | |
| 212 | // 3. Progress for Animation. |
| 213 | double prev_playback_time = prev_time + start_offset; |
| 214 | double cur_playback_time = cur_time + start_offset; |
| 215 | if (stretch_time_scale) { |
| 216 | double mlt = anim_size / cur_len; |
| 217 | prev_playback_time *= mlt; |
| 218 | cur_playback_time *= mlt; |
| 219 | cur_delta *= mlt; |
| 220 | } |
| 221 | if (cur_loop_mode == Animation::LOOP_LINEAR) { |
| 222 | if (!Math::is_zero_approx(anim_size)) { |
| 223 | prev_playback_time = Math::fposmod(prev_playback_time, anim_size); |
| 224 | cur_playback_time = Math::fposmod(cur_playback_time, anim_size); |
| 225 | if (Animation::is_greater_or_equal_approx(prev_playback_time, 0) && Animation::is_less_approx(cur_playback_time, 0)) { |
| 226 | looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START; |
| 227 | } |
| 228 | if (Animation::is_less_or_equal_approx(prev_playback_time, anim_size) && Animation::is_greater_approx(cur_playback_time, anim_size)) { |
| 229 | looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END; |
| 230 | } |
| 231 | } |
| 232 | } else if (cur_loop_mode == Animation::LOOP_PINGPONG) { |
| 233 | if (!Math::is_zero_approx(anim_size)) { |
| 234 | if (Animation::is_greater_or_equal_approx(Math::fposmod(cur_playback_time, anim_size * 2.0), anim_size)) { |
| 235 | cur_delta = -cur_delta; // Needed for retrieving discrete keys correctly. |
| 236 | } |
| 237 | prev_playback_time = Math::pingpong(prev_playback_time, anim_size); |
| 238 | cur_playback_time = Math::pingpong(cur_playback_time, anim_size); |
| 239 | if (Animation::is_greater_or_equal_approx(prev_playback_time, 0) && Animation::is_less_approx(cur_playback_time, 0)) { |
| 240 | looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START; |
| 241 | } |
| 242 | if (Animation::is_less_or_equal_approx(prev_playback_time, anim_size) && Animation::is_greater_approx(cur_playback_time, anim_size)) { |
| 243 | looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END; |
| 244 | } |
| 245 | } |
| 246 | } else { |
| 247 | if (Animation::is_less_approx(cur_playback_time, 0)) { |
| 248 | cur_playback_time = 0; |
| 249 | } else if (Animation::is_greater_approx(cur_playback_time, anim_size)) { |
| 250 | cur_playback_time = anim_size; |
| 251 | } |
| 252 | |
| 253 | // Emit start & finish signal. Internally, the detections are the same for backward. |
| 254 | // We should use call_deferred since the track keys are still being processed. |
| 255 | if (process_state->tree && !p_test_only) { |
| 256 | // AnimationTree uses seek to 0 "internally" to process the first key of the animation, which is used as the start detection. |
| 257 | if (is_started) { |
| 258 | process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_started), animation); |
| 259 | } |
| 260 | // Finished. |
| 261 | if (Animation::is_less_approx(prev_playback_time, anim_size) && Animation::is_greater_or_equal_approx(cur_playback_time, anim_size)) { |
| 262 | cur_playback_time = anim_size; |
| 263 | process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_finished), animation); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (!p_test_only) { |
| 269 | // Force process first key for Discrete/Method/Audio/AnimationPlayback. |
| 270 | if (immediately_after_start) { |
| 271 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 272 | pi.start = 0.0; |
| 273 | pi.end = cur_len; |
| 274 | if (play_mode == PLAY_MODE_FORWARD) { |
| 275 | pi.time = 0; |
| 276 | } else { |
| 277 | pi.time = anim_size; |
| 278 | } |
| 279 | pi.delta = 0; |
| 280 | pi.weight = CMP_EPSILON; |
| 281 | blend_animation(animation, pi); |
| 282 | } |
| 283 | |
| 284 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 285 | pi.start = 0.0; |
| 286 | pi.end = cur_len; |
| 287 | if (play_mode == PLAY_MODE_FORWARD) { |
| 288 | pi.time = cur_playback_time; |
| 289 | } else { |
| 290 | pi.time = anim_size - cur_playback_time; |
| 291 | } |
| 292 | if (node_backward ? cur_backward : !cur_backward) { |
| 293 | pi.delta = cur_delta; |
| 294 | } else { |
| 295 | pi.delta = -cur_delta; |
| 296 | } |
| 297 | pi.weight = 1.0; |
| 298 | pi.looped_flag = looped_flag; |
| 299 | blend_animation(animation, pi); |
| 300 | |
| 301 | set_parameter(backward, cur_backward); |
| 302 | } |
| 303 | |
| 304 | return nti; |
| 305 | } |
| 306 | |
| 307 | String AnimationNodeAnimation::get_caption() const { |
| 308 | return "Animation"; |
| 309 | } |
| 310 | |
| 311 | void AnimationNodeAnimation::set_play_mode(PlayMode p_play_mode) { |
| 312 | play_mode = p_play_mode; |
| 313 | } |
| 314 | |
| 315 | AnimationNodeAnimation::PlayMode AnimationNodeAnimation::get_play_mode() const { |
| 316 | return play_mode; |
| 317 | } |
| 318 | |
| 319 | void AnimationNodeAnimation::set_backward(bool p_backward) { |
| 320 | set_parameter(backward, p_backward); |
| 321 | } |
| 322 | |
| 323 | bool AnimationNodeAnimation::is_backward() const { |
| 324 | return get_parameter(backward); |
| 325 | } |
| 326 | |
| 327 | void AnimationNodeAnimation::set_advance_on_start(bool p_advance_on_start) { |
| 328 | advance_on_start = p_advance_on_start; |
| 329 | } |
| 330 | |
| 331 | bool AnimationNodeAnimation::is_advance_on_start() const { |
| 332 | return advance_on_start; |
| 333 | } |
| 334 | |
| 335 | void AnimationNodeAnimation::set_use_custom_timeline(bool p_use_custom_timeline) { |
| 336 | use_custom_timeline = p_use_custom_timeline; |
| 337 | notify_property_list_changed(); |
| 338 | } |
| 339 | |
| 340 | bool AnimationNodeAnimation::is_using_custom_timeline() const { |
| 341 | return use_custom_timeline; |
| 342 | } |
| 343 | |
| 344 | void AnimationNodeAnimation::set_timeline_length(double p_length) { |
| 345 | timeline_length = p_length; |
| 346 | } |
| 347 | |
| 348 | double AnimationNodeAnimation::get_timeline_length() const { |
| 349 | return timeline_length; |
| 350 | } |
| 351 | |
| 352 | void AnimationNodeAnimation::set_stretch_time_scale(bool p_stretch_time_scale) { |
| 353 | stretch_time_scale = p_stretch_time_scale; |
| 354 | notify_property_list_changed(); |
| 355 | } |
| 356 | |
| 357 | bool AnimationNodeAnimation::is_stretching_time_scale() const { |
| 358 | return stretch_time_scale; |
| 359 | } |
| 360 | |
| 361 | void AnimationNodeAnimation::set_start_offset(double p_offset) { |
| 362 | start_offset = p_offset; |
| 363 | } |
| 364 | |
| 365 | double AnimationNodeAnimation::get_start_offset() const { |
| 366 | return start_offset; |
| 367 | } |
| 368 | |
| 369 | void AnimationNodeAnimation::set_loop_mode(Animation::LoopMode p_loop_mode) { |
| 370 | loop_mode = p_loop_mode; |
| 371 | } |
| 372 | |
| 373 | Animation::LoopMode AnimationNodeAnimation::get_loop_mode() const { |
| 374 | return loop_mode; |
| 375 | } |
| 376 | |
| 377 | void AnimationNodeAnimation::_bind_methods() { |
| 378 | ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimationNodeAnimation::set_animation); |
| 379 | ClassDB::bind_method(D_METHOD("get_animation"), &AnimationNodeAnimation::get_animation); |
| 380 | |
| 381 | ClassDB::bind_method(D_METHOD("set_play_mode", "mode"), &AnimationNodeAnimation::set_play_mode); |
| 382 | ClassDB::bind_method(D_METHOD("get_play_mode"), &AnimationNodeAnimation::get_play_mode); |
| 383 | |
| 384 | ClassDB::bind_method(D_METHOD("set_advance_on_start", "advance_on_start"), &AnimationNodeAnimation::set_advance_on_start); |
| 385 | ClassDB::bind_method(D_METHOD("is_advance_on_start"), &AnimationNodeAnimation::is_advance_on_start); |
| 386 | |
| 387 | ClassDB::bind_method(D_METHOD("set_use_custom_timeline", "use_custom_timeline"), &AnimationNodeAnimation::set_use_custom_timeline); |
| 388 | ClassDB::bind_method(D_METHOD("is_using_custom_timeline"), &AnimationNodeAnimation::is_using_custom_timeline); |
| 389 | |
| 390 | ClassDB::bind_method(D_METHOD("set_timeline_length", "timeline_length"), &AnimationNodeAnimation::set_timeline_length); |
| 391 | ClassDB::bind_method(D_METHOD("get_timeline_length"), &AnimationNodeAnimation::get_timeline_length); |
| 392 | |
| 393 | ClassDB::bind_method(D_METHOD("set_stretch_time_scale", "stretch_time_scale"), &AnimationNodeAnimation::set_stretch_time_scale); |
| 394 | ClassDB::bind_method(D_METHOD("is_stretching_time_scale"), &AnimationNodeAnimation::is_stretching_time_scale); |
| 395 | |
| 396 | ClassDB::bind_method(D_METHOD("set_start_offset", "start_offset"), &AnimationNodeAnimation::set_start_offset); |
| 397 | ClassDB::bind_method(D_METHOD("get_start_offset"), &AnimationNodeAnimation::get_start_offset); |
| 398 | |
| 399 | ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AnimationNodeAnimation::set_loop_mode); |
| 400 | ClassDB::bind_method(D_METHOD("get_loop_mode"), &AnimationNodeAnimation::get_loop_mode); |
| 401 | |
| 402 | ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation"); |
| 403 | ADD_PROPERTY(PropertyInfo(Variant::INT, "play_mode", PROPERTY_HINT_ENUM, "Forward,Backward"), "set_play_mode", "get_play_mode"); |
| 404 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "advance_on_start"), "set_advance_on_start", "is_advance_on_start"); |
| 405 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_timeline"), "set_use_custom_timeline", "is_using_custom_timeline"); |
| 406 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "timeline_length", PROPERTY_HINT_RANGE, "0.001,60,0.001,or_greater,or_less,hide_control,suffix:s"), "set_timeline_length", "get_timeline_length"); |
| 407 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch_time_scale"), "set_stretch_time_scale", "is_stretching_time_scale"); |
| 408 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "start_offset", PROPERTY_HINT_RANGE, "-60,60,0.001,or_greater,or_less,hide_control,suffix:s"), "set_start_offset", "get_start_offset"); |
| 409 | ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Ping-Pong"), "set_loop_mode", "get_loop_mode"); |
| 410 | |
| 411 | BIND_ENUM_CONSTANT(PLAY_MODE_FORWARD); |
| 412 | BIND_ENUM_CONSTANT(PLAY_MODE_BACKWARD); |
| 413 | } |
| 414 | |
| 415 | AnimationNodeAnimation::AnimationNodeAnimation() { |
| 416 | } |
| 417 | |
| 418 | //////////////////////////////////////////////////////// |
| 419 | |
| 420 | void AnimationNodeSync::_bind_methods() { |
| 421 | ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeSync::set_use_sync); |
| 422 | ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeSync::is_using_sync); |
| 423 | |
| 424 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync"); |
| 425 | } |
| 426 | |
| 427 | void AnimationNodeSync::set_use_sync(bool p_sync) { |
| 428 | sync = p_sync; |
| 429 | } |
| 430 | |
| 431 | bool AnimationNodeSync::is_using_sync() const { |
| 432 | return sync; |
| 433 | } |
| 434 | |
| 435 | AnimationNodeSync::AnimationNodeSync() { |
| 436 | } |
| 437 | |
| 438 | //////////////////////////////////////////////////////// |
| 439 | void AnimationNodeOneShot::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 440 | AnimationNode::get_parameter_list(r_list); |
| 441 | r_list->push_back(PropertyInfo(Variant::BOOL, active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY)); |
| 442 | r_list->push_back(PropertyInfo(Variant::BOOL, internal_active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY)); |
| 443 | r_list->push_back(PropertyInfo(Variant::INT, request, PROPERTY_HINT_ENUM, ",Fire,Abort,Fade Out")); |
| 444 | r_list->push_back(PropertyInfo(Variant::FLOAT, fade_in_remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE)); |
| 445 | r_list->push_back(PropertyInfo(Variant::FLOAT, fade_out_remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE)); |
| 446 | r_list->push_back(PropertyInfo(Variant::FLOAT, time_to_restart, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE)); |
| 447 | } |
| 448 | |
| 449 | Variant AnimationNodeOneShot::get_parameter_default_value(const StringName &p_parameter) const { |
| 450 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 451 | if (ret != Variant()) { |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | if (p_parameter == request) { |
| 456 | return ONE_SHOT_REQUEST_NONE; |
| 457 | } else if (p_parameter == active || p_parameter == internal_active) { |
| 458 | return false; |
| 459 | } else if (p_parameter == time_to_restart) { |
| 460 | return -1.0; |
| 461 | } |
| 462 | |
| 463 | return 0.0; |
| 464 | } |
| 465 | |
| 466 | bool AnimationNodeOneShot::is_parameter_read_only(const StringName &p_parameter) const { |
| 467 | if (AnimationNode::is_parameter_read_only(p_parameter)) { |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | if (p_parameter == active || p_parameter == internal_active) { |
| 472 | return true; |
| 473 | } |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | void AnimationNodeOneShot::set_fade_in_time(double p_time) { |
| 478 | fade_in = p_time; |
| 479 | } |
| 480 | |
| 481 | double AnimationNodeOneShot::get_fade_in_time() const { |
| 482 | return fade_in; |
| 483 | } |
| 484 | |
| 485 | void AnimationNodeOneShot::set_fade_out_time(double p_time) { |
| 486 | fade_out = p_time; |
| 487 | } |
| 488 | |
| 489 | double AnimationNodeOneShot::get_fade_out_time() const { |
| 490 | return fade_out; |
| 491 | } |
| 492 | |
| 493 | void AnimationNodeOneShot::set_fade_in_curve(const Ref<Curve> &p_curve) { |
| 494 | fade_in_curve = p_curve; |
| 495 | } |
| 496 | |
| 497 | Ref<Curve> AnimationNodeOneShot::get_fade_in_curve() const { |
| 498 | return fade_in_curve; |
| 499 | } |
| 500 | |
| 501 | void AnimationNodeOneShot::set_fade_out_curve(const Ref<Curve> &p_curve) { |
| 502 | fade_out_curve = p_curve; |
| 503 | } |
| 504 | |
| 505 | Ref<Curve> AnimationNodeOneShot::get_fade_out_curve() const { |
| 506 | return fade_out_curve; |
| 507 | } |
| 508 | |
| 509 | void AnimationNodeOneShot::set_auto_restart_enabled(bool p_enabled) { |
| 510 | auto_restart = p_enabled; |
| 511 | } |
| 512 | |
| 513 | void AnimationNodeOneShot::set_auto_restart_delay(double p_time) { |
| 514 | auto_restart_delay = p_time; |
| 515 | } |
| 516 | |
| 517 | void AnimationNodeOneShot::set_auto_restart_random_delay(double p_time) { |
| 518 | auto_restart_random_delay = p_time; |
| 519 | } |
| 520 | |
| 521 | bool AnimationNodeOneShot::is_auto_restart_enabled() const { |
| 522 | return auto_restart; |
| 523 | } |
| 524 | |
| 525 | double AnimationNodeOneShot::get_auto_restart_delay() const { |
| 526 | return auto_restart_delay; |
| 527 | } |
| 528 | |
| 529 | double AnimationNodeOneShot::get_auto_restart_random_delay() const { |
| 530 | return auto_restart_random_delay; |
| 531 | } |
| 532 | |
| 533 | void AnimationNodeOneShot::set_mix_mode(MixMode p_mix) { |
| 534 | mix = p_mix; |
| 535 | } |
| 536 | |
| 537 | AnimationNodeOneShot::MixMode AnimationNodeOneShot::get_mix_mode() const { |
| 538 | return mix; |
| 539 | } |
| 540 | |
| 541 | void AnimationNodeOneShot::set_break_loop_at_end(bool p_enable) { |
| 542 | break_loop_at_end = p_enable; |
| 543 | } |
| 544 | |
| 545 | bool AnimationNodeOneShot::is_loop_broken_at_end() const { |
| 546 | return break_loop_at_end; |
| 547 | } |
| 548 | |
| 549 | void AnimationNodeOneShot::set_abort_on_reset(bool p_enable) { |
| 550 | abort_on_reset = p_enable; |
| 551 | } |
| 552 | |
| 553 | bool AnimationNodeOneShot::is_aborted_on_reset() const { |
| 554 | return abort_on_reset; |
| 555 | } |
| 556 | |
| 557 | String AnimationNodeOneShot::get_caption() const { |
| 558 | return "OneShot"; |
| 559 | } |
| 560 | |
| 561 | bool AnimationNodeOneShot::has_filter() const { |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | AnimationNode::NodeTimeInfo AnimationNodeOneShot::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 566 | OneShotRequest cur_request = static_cast<OneShotRequest>((int)get_parameter(request)); |
| 567 | bool cur_active = get_parameter(active); |
| 568 | bool cur_internal_active = get_parameter(internal_active); |
| 569 | NodeTimeInfo cur_nti = get_node_time_info(); |
| 570 | double cur_time_to_restart = get_parameter(time_to_restart); |
| 571 | double cur_fade_in_remaining = get_parameter(fade_in_remaining); |
| 572 | double cur_fade_out_remaining = get_parameter(fade_out_remaining); |
| 573 | |
| 574 | set_parameter(request, ONE_SHOT_REQUEST_NONE); |
| 575 | |
| 576 | bool is_shooting = true; |
| 577 | bool is_fading_out = cur_active == true && cur_internal_active == false; |
| 578 | |
| 579 | double p_time = p_playback_info.time; |
| 580 | double p_delta = p_playback_info.delta; |
| 581 | double abs_delta = Math::abs(p_delta); |
| 582 | bool p_seek = p_playback_info.seeked; |
| 583 | bool p_is_external_seeking = p_playback_info.is_external_seeking; |
| 584 | |
| 585 | bool do_start = cur_request == ONE_SHOT_REQUEST_FIRE; |
| 586 | |
| 587 | bool is_reset = Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking; |
| 588 | if (is_reset && cur_internal_active) { |
| 589 | do_start = true; |
| 590 | } |
| 591 | |
| 592 | bool is_abort = cur_request == ONE_SHOT_REQUEST_ABORT; |
| 593 | if (is_reset && !do_start && (is_fading_out || (abort_on_reset && cur_active))) { |
| 594 | is_abort = true; |
| 595 | } |
| 596 | |
| 597 | if (is_abort) { |
| 598 | set_parameter(internal_active, false); |
| 599 | set_parameter(active, false); |
| 600 | set_parameter(time_to_restart, -1); |
| 601 | set_parameter(fade_out_remaining, 0); |
| 602 | cur_fade_out_remaining = 0; |
| 603 | is_fading_out = false; |
| 604 | is_shooting = false; |
| 605 | } else if (cur_request == ONE_SHOT_REQUEST_FADE_OUT && !is_fading_out) { // If fading, keep current fade. |
| 606 | if (cur_active) { |
| 607 | // Request fading. |
| 608 | is_fading_out = true; |
| 609 | cur_fade_out_remaining = fade_out; |
| 610 | cur_fade_in_remaining = 0; |
| 611 | } else { |
| 612 | // Shot is ended, do nothing. |
| 613 | is_shooting = false; |
| 614 | } |
| 615 | set_parameter(internal_active, false); |
| 616 | set_parameter(time_to_restart, -1); |
| 617 | } else if (!do_start && !cur_active) { |
| 618 | if (Animation::is_greater_or_equal_approx(cur_time_to_restart, 0) && !p_seek) { |
| 619 | cur_time_to_restart -= abs_delta; |
| 620 | if (Animation::is_less_approx(cur_time_to_restart, 0)) { |
| 621 | do_start = true; // Restart. |
| 622 | } |
| 623 | set_parameter(time_to_restart, cur_time_to_restart); |
| 624 | } |
| 625 | if (!do_start) { |
| 626 | is_shooting = false; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | bool os_seek = p_seek; |
| 631 | |
| 632 | if (!is_shooting) { |
| 633 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 634 | pi.weight = 1.0; |
| 635 | return blend_input(0, pi, FILTER_IGNORE, sync, p_test_only); |
| 636 | } |
| 637 | |
| 638 | if (do_start) { |
| 639 | os_seek = true; |
| 640 | if (!cur_internal_active) { |
| 641 | cur_fade_in_remaining = fade_in; // If already active, don't fade-in again. |
| 642 | } |
| 643 | cur_internal_active = true; |
| 644 | set_parameter(request, ONE_SHOT_REQUEST_NONE); |
| 645 | set_parameter(internal_active, true); |
| 646 | set_parameter(active, true); |
| 647 | // Clear fade-out. |
| 648 | is_fading_out = false; |
| 649 | cur_fade_out_remaining = 0; |
| 650 | } |
| 651 | |
| 652 | real_t blend = 1.0; |
| 653 | bool use_blend = sync; |
| 654 | |
| 655 | if (Animation::is_greater_approx(cur_fade_in_remaining, 0)) { |
| 656 | if (Animation::is_greater_approx(fade_in, 0)) { |
| 657 | use_blend = true; |
| 658 | blend = (fade_in - cur_fade_in_remaining) / fade_in; |
| 659 | if (fade_in_curve.is_valid()) { |
| 660 | blend = fade_in_curve->sample(blend); |
| 661 | } |
| 662 | } else { |
| 663 | blend = 0; // Should not happen. |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if (is_fading_out) { |
| 668 | use_blend = true; |
| 669 | if (Animation::is_greater_approx(fade_out, 0)) { |
| 670 | blend = cur_fade_out_remaining / fade_out; |
| 671 | if (fade_out_curve.is_valid()) { |
| 672 | blend = 1.0 - fade_out_curve->sample(1.0 - blend); |
| 673 | } |
| 674 | } else { |
| 675 | blend = 0; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 680 | NodeTimeInfo main_nti; |
| 681 | if (mix == MIX_MODE_ADD) { |
| 682 | pi.weight = 1.0; |
| 683 | main_nti = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only); |
| 684 | } else { |
| 685 | pi.seeked &= use_blend; |
| 686 | pi.weight = 1.0 - blend; |
| 687 | main_nti = blend_input(0, pi, FILTER_BLEND, sync, p_test_only); // Unlike below, processing this edge is a corner case. |
| 688 | } |
| 689 | |
| 690 | pi = p_playback_info; |
| 691 | if (do_start) { |
| 692 | pi.time = 0; |
| 693 | } else if (os_seek) { |
| 694 | pi.time = cur_nti.position; |
| 695 | } |
| 696 | pi.seeked = os_seek; |
| 697 | pi.weight = Math::is_zero_approx(blend) ? CMP_EPSILON : blend; |
| 698 | |
| 699 | NodeTimeInfo os_nti = blend_input(1, pi, FILTER_PASS, true, p_test_only); // Blend values must be more than CMP_EPSILON to process discrete keys in edge. |
| 700 | |
| 701 | if (Animation::is_less_or_equal_approx(cur_fade_in_remaining, 0) && !do_start && !is_fading_out) { |
| 702 | // Predict time scale by difference of delta times to estimate input animation's remain time in self time scale. |
| 703 | // TODO: Time scale should be included into NodeTimeInfo for Godot 5.0. |
| 704 | double abs_os_delta = Math::abs(os_nti.delta); |
| 705 | double tscl = Math::is_zero_approx(abs_delta) || Math::is_zero_approx(abs_os_delta) || Math::is_equal_approx(abs_delta, abs_os_delta) ? 1.0 : (abs_delta / abs_os_delta); |
| 706 | double os_rem = os_nti.get_remain(break_loop_at_end) * tscl; |
| 707 | if (Animation::is_less_or_equal_approx(os_rem, fade_out)) { |
| 708 | is_fading_out = true; |
| 709 | cur_fade_out_remaining = os_rem + abs_delta; |
| 710 | cur_fade_in_remaining = 0; |
| 711 | set_parameter(internal_active, false); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (!p_seek) { |
| 716 | if (Animation::is_less_or_equal_approx(os_nti.get_remain(break_loop_at_end), 0) || (is_fading_out && Animation::is_less_or_equal_approx(cur_fade_out_remaining, 0))) { |
| 717 | set_parameter(internal_active, false); |
| 718 | set_parameter(active, false); |
| 719 | if (auto_restart) { |
| 720 | double restart_sec = auto_restart_delay + Math::randd() * auto_restart_random_delay; |
| 721 | set_parameter(time_to_restart, restart_sec); |
| 722 | } |
| 723 | } |
| 724 | if (!do_start) { |
| 725 | cur_fade_in_remaining = MAX(0, cur_fade_in_remaining - abs_delta); // Don't consider seeked delta by restart. |
| 726 | } |
| 727 | cur_fade_out_remaining = MAX(0, cur_fade_out_remaining - abs_delta); |
| 728 | } |
| 729 | |
| 730 | set_parameter(fade_in_remaining, cur_fade_in_remaining); |
| 731 | set_parameter(fade_out_remaining, cur_fade_out_remaining); |
| 732 | |
| 733 | return cur_internal_active ? os_nti : main_nti; |
| 734 | } |
| 735 | |
| 736 | void AnimationNodeOneShot::_bind_methods() { |
| 737 | ClassDB::bind_method(D_METHOD("set_fadein_time", "time"), &AnimationNodeOneShot::set_fade_in_time); |
| 738 | ClassDB::bind_method(D_METHOD("get_fadein_time"), &AnimationNodeOneShot::get_fade_in_time); |
| 739 | |
| 740 | ClassDB::bind_method(D_METHOD("set_fadein_curve", "curve"), &AnimationNodeOneShot::set_fade_in_curve); |
| 741 | ClassDB::bind_method(D_METHOD("get_fadein_curve"), &AnimationNodeOneShot::get_fade_in_curve); |
| 742 | |
| 743 | ClassDB::bind_method(D_METHOD("set_fadeout_time", "time"), &AnimationNodeOneShot::set_fade_out_time); |
| 744 | ClassDB::bind_method(D_METHOD("get_fadeout_time"), &AnimationNodeOneShot::get_fade_out_time); |
| 745 | |
| 746 | ClassDB::bind_method(D_METHOD("set_fadeout_curve", "curve"), &AnimationNodeOneShot::set_fade_out_curve); |
| 747 | ClassDB::bind_method(D_METHOD("get_fadeout_curve"), &AnimationNodeOneShot::get_fade_out_curve); |
| 748 | |
| 749 | ClassDB::bind_method(D_METHOD("set_break_loop_at_end", "enable"), &AnimationNodeOneShot::set_break_loop_at_end); |
| 750 | ClassDB::bind_method(D_METHOD("is_loop_broken_at_end"), &AnimationNodeOneShot::is_loop_broken_at_end); |
| 751 | |
| 752 | ClassDB::bind_method(D_METHOD("set_abort_on_reset", "enable"), &AnimationNodeOneShot::set_abort_on_reset); |
| 753 | ClassDB::bind_method(D_METHOD("is_aborted_on_reset"), &AnimationNodeOneShot::is_aborted_on_reset); |
| 754 | |
| 755 | ClassDB::bind_method(D_METHOD("set_autorestart", "active"), &AnimationNodeOneShot::set_auto_restart_enabled); |
| 756 | ClassDB::bind_method(D_METHOD("has_autorestart"), &AnimationNodeOneShot::is_auto_restart_enabled); |
| 757 | |
| 758 | ClassDB::bind_method(D_METHOD("set_autorestart_delay", "time"), &AnimationNodeOneShot::set_auto_restart_delay); |
| 759 | ClassDB::bind_method(D_METHOD("get_autorestart_delay"), &AnimationNodeOneShot::get_auto_restart_delay); |
| 760 | |
| 761 | ClassDB::bind_method(D_METHOD("set_autorestart_random_delay", "time"), &AnimationNodeOneShot::set_auto_restart_random_delay); |
| 762 | ClassDB::bind_method(D_METHOD("get_autorestart_random_delay"), &AnimationNodeOneShot::get_auto_restart_random_delay); |
| 763 | |
| 764 | ClassDB::bind_method(D_METHOD("set_mix_mode", "mode"), &AnimationNodeOneShot::set_mix_mode); |
| 765 | ClassDB::bind_method(D_METHOD("get_mix_mode"), &AnimationNodeOneShot::get_mix_mode); |
| 766 | |
| 767 | ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_mode", PROPERTY_HINT_ENUM, "Blend,Add"), "set_mix_mode", "get_mix_mode"); |
| 768 | |
| 769 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadein_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadein_time", "get_fadein_time"); |
| 770 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadein_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadein_curve", "get_fadein_curve"); |
| 771 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadeout_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadeout_time", "get_fadeout_time"); |
| 772 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadeout_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadeout_curve", "get_fadeout_curve"); |
| 773 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "break_loop_at_end"), "set_break_loop_at_end", "is_loop_broken_at_end"); |
| 774 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "abort_on_reset"), "set_abort_on_reset", "is_aborted_on_reset"); |
| 775 | |
| 776 | ADD_GROUP("Auto Restart", "autorestart_"); |
| 777 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autorestart"), "set_autorestart", "has_autorestart"); |
| 778 | |
| 779 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_autorestart_delay", "get_autorestart_delay"); |
| 780 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_random_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_autorestart_random_delay", "get_autorestart_random_delay"); |
| 781 | |
| 782 | BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_NONE); |
| 783 | BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FIRE); |
| 784 | BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_ABORT); |
| 785 | BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FADE_OUT); |
| 786 | |
| 787 | BIND_ENUM_CONSTANT(MIX_MODE_BLEND); |
| 788 | BIND_ENUM_CONSTANT(MIX_MODE_ADD); |
| 789 | } |
| 790 | |
| 791 | AnimationNodeOneShot::AnimationNodeOneShot() { |
| 792 | add_input("in"); |
| 793 | add_input("shot"); |
| 794 | } |
| 795 | |
| 796 | //////////////////////////////////////////////// |
| 797 | |
| 798 | void AnimationNodeAdd2::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 799 | AnimationNode::get_parameter_list(r_list); |
| 800 | r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater")); |
| 801 | } |
| 802 | |
| 803 | Variant AnimationNodeAdd2::get_parameter_default_value(const StringName &p_parameter) const { |
| 804 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 805 | if (ret != Variant()) { |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | return 0.0; |
| 810 | } |
| 811 | |
| 812 | String AnimationNodeAdd2::get_caption() const { |
| 813 | return "Add2"; |
| 814 | } |
| 815 | |
| 816 | bool AnimationNodeAdd2::has_filter() const { |
| 817 | return true; |
| 818 | } |
| 819 | |
| 820 | AnimationNode::NodeTimeInfo AnimationNodeAdd2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 821 | double amount = get_parameter(add_amount); |
| 822 | |
| 823 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 824 | pi.weight = 1.0; |
| 825 | NodeTimeInfo nti = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only); |
| 826 | pi.weight = amount; |
| 827 | blend_input(1, pi, FILTER_PASS, sync, p_test_only); |
| 828 | |
| 829 | return nti; |
| 830 | } |
| 831 | |
| 832 | AnimationNodeAdd2::AnimationNodeAdd2() { |
| 833 | add_input("in"); |
| 834 | add_input("add"); |
| 835 | } |
| 836 | |
| 837 | //////////////////////////////////////////////// |
| 838 | |
| 839 | void AnimationNodeAdd3::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 840 | AnimationNode::get_parameter_list(r_list); |
| 841 | r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater")); |
| 842 | } |
| 843 | |
| 844 | Variant AnimationNodeAdd3::get_parameter_default_value(const StringName &p_parameter) const { |
| 845 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 846 | if (ret != Variant()) { |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | return 0.0; |
| 851 | } |
| 852 | |
| 853 | String AnimationNodeAdd3::get_caption() const { |
| 854 | return "Add3"; |
| 855 | } |
| 856 | |
| 857 | bool AnimationNodeAdd3::has_filter() const { |
| 858 | return true; |
| 859 | } |
| 860 | |
| 861 | AnimationNode::NodeTimeInfo AnimationNodeAdd3::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 862 | double amount = get_parameter(add_amount); |
| 863 | |
| 864 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 865 | pi.weight = MAX(0, -amount); |
| 866 | blend_input(0, pi, FILTER_PASS, sync, p_test_only); |
| 867 | pi.weight = 1.0; |
| 868 | NodeTimeInfo nti = blend_input(1, pi, FILTER_IGNORE, sync, p_test_only); |
| 869 | pi.weight = MAX(0, amount); |
| 870 | blend_input(2, pi, FILTER_PASS, sync, p_test_only); |
| 871 | |
| 872 | return nti; |
| 873 | } |
| 874 | |
| 875 | AnimationNodeAdd3::AnimationNodeAdd3() { |
| 876 | add_input("-add"); |
| 877 | add_input("in"); |
| 878 | add_input("+add"); |
| 879 | } |
| 880 | |
| 881 | ///////////////////////////////////////////// |
| 882 | |
| 883 | void AnimationNodeBlend2::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 884 | AnimationNode::get_parameter_list(r_list); |
| 885 | r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater")); |
| 886 | } |
| 887 | |
| 888 | Variant AnimationNodeBlend2::get_parameter_default_value(const StringName &p_parameter) const { |
| 889 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 890 | if (ret != Variant()) { |
| 891 | return ret; |
| 892 | } |
| 893 | |
| 894 | return 0.0; // For blend amount. |
| 895 | } |
| 896 | |
| 897 | String AnimationNodeBlend2::get_caption() const { |
| 898 | return "Blend2"; |
| 899 | } |
| 900 | |
| 901 | AnimationNode::NodeTimeInfo AnimationNodeBlend2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 902 | double amount = get_parameter(blend_amount); |
| 903 | |
| 904 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 905 | pi.weight = 1.0 - amount; |
| 906 | NodeTimeInfo nti0 = blend_input(0, pi, FILTER_BLEND, sync, p_test_only); |
| 907 | pi.weight = amount; |
| 908 | NodeTimeInfo nti1 = blend_input(1, pi, FILTER_PASS, sync, p_test_only); |
| 909 | |
| 910 | return amount > 0.5 ? nti1 : nti0; // Hacky but good enough. |
| 911 | } |
| 912 | |
| 913 | bool AnimationNodeBlend2::has_filter() const { |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | AnimationNodeBlend2::AnimationNodeBlend2() { |
| 918 | add_input("in"); |
| 919 | add_input("blend"); |
| 920 | } |
| 921 | |
| 922 | ////////////////////////////////////// |
| 923 | |
| 924 | void AnimationNodeBlend3::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 925 | AnimationNode::get_parameter_list(r_list); |
| 926 | r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater")); |
| 927 | } |
| 928 | |
| 929 | Variant AnimationNodeBlend3::get_parameter_default_value(const StringName &p_parameter) const { |
| 930 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 931 | if (ret != Variant()) { |
| 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | return 0.0; // For blend amount. |
| 936 | } |
| 937 | |
| 938 | String AnimationNodeBlend3::get_caption() const { |
| 939 | return "Blend3"; |
| 940 | } |
| 941 | |
| 942 | AnimationNode::NodeTimeInfo AnimationNodeBlend3::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 943 | double amount = get_parameter(blend_amount); |
| 944 | |
| 945 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 946 | pi.weight = MAX(0, -amount); |
| 947 | NodeTimeInfo nti0 = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only); |
| 948 | pi.weight = 1.0 - Math::abs(amount); |
| 949 | NodeTimeInfo nti1 = blend_input(1, pi, FILTER_IGNORE, sync, p_test_only); |
| 950 | pi.weight = MAX(0, amount); |
| 951 | NodeTimeInfo nti2 = blend_input(2, pi, FILTER_IGNORE, sync, p_test_only); |
| 952 | |
| 953 | return amount > 0.5 ? nti2 : (amount < -0.5 ? nti0 : nti1); // Hacky but good enough. |
| 954 | } |
| 955 | |
| 956 | AnimationNodeBlend3::AnimationNodeBlend3() { |
| 957 | add_input("-blend"); |
| 958 | add_input("in"); |
| 959 | add_input("+blend"); |
| 960 | } |
| 961 | |
| 962 | //////////////////////////////////////////////// |
| 963 | |
| 964 | void AnimationNodeSub2::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 965 | AnimationNode::get_parameter_list(r_list); |
| 966 | r_list->push_back(PropertyInfo(Variant::FLOAT, sub_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater")); |
| 967 | } |
| 968 | |
| 969 | Variant AnimationNodeSub2::get_parameter_default_value(const StringName &p_parameter) const { |
| 970 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 971 | if (ret != Variant()) { |
| 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | return 0.0; |
| 976 | } |
| 977 | |
| 978 | String AnimationNodeSub2::get_caption() const { |
| 979 | return "Sub2"; |
| 980 | } |
| 981 | |
| 982 | bool AnimationNodeSub2::has_filter() const { |
| 983 | return true; |
| 984 | } |
| 985 | |
| 986 | AnimationNode::NodeTimeInfo AnimationNodeSub2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 987 | double amount = get_parameter(sub_amount); |
| 988 | |
| 989 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 990 | // Out = Sub.Transform3D^(-1) * In.Transform3D |
| 991 | pi.weight = -amount; |
| 992 | blend_input(1, pi, FILTER_PASS, sync, p_test_only); |
| 993 | pi.weight = 1.0; |
| 994 | |
| 995 | return blend_input(0, pi, FILTER_IGNORE, sync, p_test_only); |
| 996 | } |
| 997 | |
| 998 | AnimationNodeSub2::AnimationNodeSub2() { |
| 999 | add_input("in"); |
| 1000 | add_input("sub"); |
| 1001 | } |
| 1002 | |
| 1003 | ///////////////////////////////// |
| 1004 | |
| 1005 | void AnimationNodeTimeScale::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 1006 | AnimationNode::get_parameter_list(r_list); |
| 1007 | r_list->push_back(PropertyInfo(Variant::FLOAT, scale, PROPERTY_HINT_RANGE, "-32,32,0.01,or_less,or_greater")); |
| 1008 | } |
| 1009 | |
| 1010 | Variant AnimationNodeTimeScale::get_parameter_default_value(const StringName &p_parameter) const { |
| 1011 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 1012 | if (ret != Variant()) { |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
| 1016 | return 1.0; // Initial timescale. |
| 1017 | } |
| 1018 | |
| 1019 | String AnimationNodeTimeScale::get_caption() const { |
| 1020 | return "TimeScale"; |
| 1021 | } |
| 1022 | |
| 1023 | AnimationNode::NodeTimeInfo AnimationNodeTimeScale::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 1024 | double cur_scale = get_parameter(scale); |
| 1025 | |
| 1026 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 1027 | pi.weight = 1.0; |
| 1028 | if (!pi.seeked) { |
| 1029 | pi.delta *= cur_scale; |
| 1030 | } |
| 1031 | |
| 1032 | return blend_input(0, pi, FILTER_IGNORE, true, p_test_only); |
| 1033 | } |
| 1034 | |
| 1035 | AnimationNodeTimeScale::AnimationNodeTimeScale() { |
| 1036 | add_input("in"); |
| 1037 | } |
| 1038 | |
| 1039 | //////////////////////////////////// |
| 1040 | |
| 1041 | void AnimationNodeTimeSeek::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 1042 | AnimationNode::get_parameter_list(r_list); |
| 1043 | r_list->push_back(PropertyInfo(Variant::FLOAT, seek_pos_request, PROPERTY_HINT_RANGE, "-1,3600,0.01,or_greater")); // It will be reset to -1 after seeking the position immediately. |
| 1044 | } |
| 1045 | |
| 1046 | Variant AnimationNodeTimeSeek::get_parameter_default_value(const StringName &p_parameter) const { |
| 1047 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 1048 | if (ret != Variant()) { |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | return -1.0; // Initial seek request. |
| 1053 | } |
| 1054 | |
| 1055 | String AnimationNodeTimeSeek::get_caption() const { |
| 1056 | return "TimeSeek"; |
| 1057 | } |
| 1058 | |
| 1059 | void AnimationNodeTimeSeek::set_explicit_elapse(bool p_enable) { |
| 1060 | explicit_elapse = p_enable; |
| 1061 | } |
| 1062 | |
| 1063 | bool AnimationNodeTimeSeek::is_explicit_elapse() const { |
| 1064 | return explicit_elapse; |
| 1065 | } |
| 1066 | |
| 1067 | AnimationNode::NodeTimeInfo AnimationNodeTimeSeek::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 1068 | double cur_seek_pos = get_parameter(seek_pos_request); |
| 1069 | |
| 1070 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 1071 | pi.weight = 1.0; |
| 1072 | if (Animation::is_greater_or_equal_approx(cur_seek_pos, 0)) { |
| 1073 | pi.time = cur_seek_pos; |
| 1074 | pi.seeked = true; |
| 1075 | pi.is_external_seeking = explicit_elapse; |
| 1076 | set_parameter(seek_pos_request, -1.0); // Reset. |
| 1077 | } |
| 1078 | |
| 1079 | return blend_input(0, pi, FILTER_IGNORE, true, p_test_only); |
| 1080 | } |
| 1081 | |
| 1082 | AnimationNodeTimeSeek::AnimationNodeTimeSeek() { |
| 1083 | add_input("in"); |
| 1084 | } |
| 1085 | |
| 1086 | void AnimationNodeTimeSeek::_bind_methods() { |
| 1087 | ClassDB::bind_method(D_METHOD("set_explicit_elapse", "enable"), &AnimationNodeTimeSeek::set_explicit_elapse); |
| 1088 | ClassDB::bind_method(D_METHOD("is_explicit_elapse"), &AnimationNodeTimeSeek::is_explicit_elapse); |
| 1089 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "explicit_elapse"), "set_explicit_elapse", "is_explicit_elapse"); |
| 1090 | } |
| 1091 | |
| 1092 | ///////////////////////////////////////////////// |
| 1093 | |
| 1094 | bool AnimationNodeTransition::_set(const StringName &p_path, const Variant &p_value) { |
| 1095 | String path = p_path; |
| 1096 | |
| 1097 | if (!path.begins_with("input_")) { |
| 1098 | return false; |
| 1099 | } |
| 1100 | |
| 1101 | int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int(); |
| 1102 | String what = path.get_slicec('/', 1); |
| 1103 | |
| 1104 | if (which == get_input_count() && what == "name") { |
| 1105 | if (add_input(p_value)) { |
| 1106 | return true; |
| 1107 | } |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
| 1111 | ERR_FAIL_INDEX_V(which, get_input_count(), false); |
| 1112 | |
| 1113 | if (what == "name") { |
| 1114 | set_input_name(which, p_value); |
| 1115 | } else if (what == "auto_advance") { |
| 1116 | set_input_as_auto_advance(which, p_value); |
| 1117 | } else if (what == "break_loop_at_end") { |
| 1118 | set_input_break_loop_at_end(which, p_value); |
| 1119 | } else if (what == "reset") { |
| 1120 | set_input_reset(which, p_value); |
| 1121 | } else { |
| 1122 | return false; |
| 1123 | } |
| 1124 | |
| 1125 | return true; |
| 1126 | } |
| 1127 | |
| 1128 | bool AnimationNodeTransition::_get(const StringName &p_path, Variant &r_ret) const { |
| 1129 | String path = p_path; |
| 1130 | |
| 1131 | if (!path.begins_with("input_")) { |
| 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int(); |
| 1136 | String what = path.get_slicec('/', 1); |
| 1137 | |
| 1138 | ERR_FAIL_INDEX_V(which, get_input_count(), false); |
| 1139 | |
| 1140 | if (what == "name") { |
| 1141 | r_ret = get_input_name(which); |
| 1142 | } else if (what == "auto_advance") { |
| 1143 | r_ret = is_input_set_as_auto_advance(which); |
| 1144 | } else if (what == "break_loop_at_end") { |
| 1145 | r_ret = is_input_loop_broken_at_end(which); |
| 1146 | } else if (what == "reset") { |
| 1147 | r_ret = is_input_reset(which); |
| 1148 | } else { |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | return true; |
| 1153 | } |
| 1154 | |
| 1155 | void AnimationNodeTransition::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 1156 | AnimationNode::get_parameter_list(r_list); |
| 1157 | String anims; |
| 1158 | for (int i = 0; i < get_input_count(); i++) { |
| 1159 | if (i > 0) { |
| 1160 | anims += ","; |
| 1161 | } |
| 1162 | anims += inputs[i].name; |
| 1163 | } |
| 1164 | |
| 1165 | r_list->push_back(PropertyInfo(Variant::STRING, current_state, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY)); // For interface. |
| 1166 | r_list->push_back(PropertyInfo(Variant::STRING, transition_request, PROPERTY_HINT_ENUM, anims)); // For transition request. It will be cleared after setting the value immediately. |
| 1167 | r_list->push_back(PropertyInfo(Variant::INT, current_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY)); // To avoid finding the index every frame, use this internally. |
| 1168 | r_list->push_back(PropertyInfo(Variant::INT, prev_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE)); |
| 1169 | r_list->push_back(PropertyInfo(Variant::FLOAT, prev_xfading, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE)); |
| 1170 | } |
| 1171 | |
| 1172 | Variant AnimationNodeTransition::get_parameter_default_value(const StringName &p_parameter) const { |
| 1173 | Variant ret = AnimationNode::get_parameter_default_value(p_parameter); |
| 1174 | if (ret != Variant()) { |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
| 1178 | if (p_parameter == prev_xfading) { |
| 1179 | return 0.0; |
| 1180 | } else if (p_parameter == prev_index || p_parameter == current_index) { |
| 1181 | return (int)-1; |
| 1182 | } else { |
| 1183 | return String(); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | bool AnimationNodeTransition::is_parameter_read_only(const StringName &p_parameter) const { |
| 1188 | if (AnimationNode::is_parameter_read_only(p_parameter)) { |
| 1189 | return true; |
| 1190 | } |
| 1191 | |
| 1192 | if (p_parameter == current_state || p_parameter == current_index) { |
| 1193 | return true; |
| 1194 | } |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | String AnimationNodeTransition::get_caption() const { |
| 1199 | return "Transition"; |
| 1200 | } |
| 1201 | |
| 1202 | void AnimationNodeTransition::set_input_count(int p_inputs) { |
| 1203 | for (int i = get_input_count(); i < p_inputs; i++) { |
| 1204 | add_input("state_" + itos(i)); |
| 1205 | } |
| 1206 | while (get_input_count() > p_inputs) { |
| 1207 | remove_input(get_input_count() - 1); |
| 1208 | } |
| 1209 | |
| 1210 | pending_update = true; |
| 1211 | |
| 1212 | emit_signal(SNAME("tree_changed")); // For updating connect activity map. |
| 1213 | notify_property_list_changed(); |
| 1214 | } |
| 1215 | |
| 1216 | bool AnimationNodeTransition::add_input(const String &p_name) { |
| 1217 | if (AnimationNode::add_input(p_name)) { |
| 1218 | input_data.push_back(InputData()); |
| 1219 | return true; |
| 1220 | } |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | void AnimationNodeTransition::remove_input(int p_index) { |
| 1225 | input_data.remove_at(p_index); |
| 1226 | AnimationNode::remove_input(p_index); |
| 1227 | } |
| 1228 | |
| 1229 | bool AnimationNodeTransition::set_input_name(int p_input, const String &p_name) { |
| 1230 | pending_update = true; |
| 1231 | if (!AnimationNode::set_input_name(p_input, p_name)) { |
| 1232 | return false; |
| 1233 | } |
| 1234 | emit_signal(SNAME("tree_changed")); // For updating enum options. |
| 1235 | return true; |
| 1236 | } |
| 1237 | |
| 1238 | void AnimationNodeTransition::set_input_as_auto_advance(int p_input, bool p_enable) { |
| 1239 | ERR_FAIL_INDEX(p_input, get_input_count()); |
| 1240 | input_data[p_input].auto_advance = p_enable; |
| 1241 | } |
| 1242 | |
| 1243 | bool AnimationNodeTransition::is_input_set_as_auto_advance(int p_input) const { |
| 1244 | ERR_FAIL_INDEX_V(p_input, get_input_count(), false); |
| 1245 | return input_data[p_input].auto_advance; |
| 1246 | } |
| 1247 | |
| 1248 | void AnimationNodeTransition::set_input_break_loop_at_end(int p_input, bool p_enable) { |
| 1249 | ERR_FAIL_INDEX(p_input, get_input_count()); |
| 1250 | input_data[p_input].break_loop_at_end = p_enable; |
| 1251 | } |
| 1252 | |
| 1253 | bool AnimationNodeTransition::is_input_loop_broken_at_end(int p_input) const { |
| 1254 | ERR_FAIL_INDEX_V(p_input, get_input_count(), false); |
| 1255 | return input_data[p_input].break_loop_at_end; |
| 1256 | } |
| 1257 | |
| 1258 | void AnimationNodeTransition::set_input_reset(int p_input, bool p_enable) { |
| 1259 | ERR_FAIL_INDEX(p_input, get_input_count()); |
| 1260 | input_data[p_input].reset = p_enable; |
| 1261 | } |
| 1262 | |
| 1263 | bool AnimationNodeTransition::is_input_reset(int p_input) const { |
| 1264 | ERR_FAIL_INDEX_V(p_input, get_input_count(), true); |
| 1265 | return input_data[p_input].reset; |
| 1266 | } |
| 1267 | |
| 1268 | void AnimationNodeTransition::set_xfade_time(double p_fade) { |
| 1269 | xfade_time = p_fade; |
| 1270 | } |
| 1271 | |
| 1272 | double AnimationNodeTransition::get_xfade_time() const { |
| 1273 | return xfade_time; |
| 1274 | } |
| 1275 | |
| 1276 | void AnimationNodeTransition::set_xfade_curve(const Ref<Curve> &p_curve) { |
| 1277 | xfade_curve = p_curve; |
| 1278 | } |
| 1279 | |
| 1280 | Ref<Curve> AnimationNodeTransition::get_xfade_curve() const { |
| 1281 | return xfade_curve; |
| 1282 | } |
| 1283 | |
| 1284 | void AnimationNodeTransition::set_allow_transition_to_self(bool p_enable) { |
| 1285 | allow_transition_to_self = p_enable; |
| 1286 | } |
| 1287 | |
| 1288 | bool AnimationNodeTransition::is_allow_transition_to_self() const { |
| 1289 | return allow_transition_to_self; |
| 1290 | } |
| 1291 | |
| 1292 | AnimationNode::NodeTimeInfo AnimationNodeTransition::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 1293 | String cur_transition_request = get_parameter(transition_request); |
| 1294 | int cur_current_index = get_parameter(current_index); |
| 1295 | int cur_prev_index = get_parameter(prev_index); |
| 1296 | |
| 1297 | NodeTimeInfo cur_nti = get_node_time_info(); |
| 1298 | double cur_prev_xfading = get_parameter(prev_xfading); |
| 1299 | |
| 1300 | bool switched = false; |
| 1301 | bool restart = false; |
| 1302 | bool clear_remaining_fade = false; |
| 1303 | |
| 1304 | if (pending_update) { |
| 1305 | if (cur_current_index < 0 || cur_current_index >= get_input_count()) { |
| 1306 | set_parameter(prev_index, -1); |
| 1307 | if (get_input_count() > 0) { |
| 1308 | set_parameter(current_index, 0); |
| 1309 | set_parameter(current_state, get_input_name(0)); |
| 1310 | } else { |
| 1311 | set_parameter(current_index, -1); |
| 1312 | set_parameter(current_state, StringName()); |
| 1313 | } |
| 1314 | } else { |
| 1315 | set_parameter(current_state, get_input_name(cur_current_index)); |
| 1316 | } |
| 1317 | pending_update = false; |
| 1318 | } |
| 1319 | |
| 1320 | double p_time = p_playback_info.time; |
| 1321 | bool p_seek = p_playback_info.seeked; |
| 1322 | bool p_is_external_seeking = p_playback_info.is_external_seeking; |
| 1323 | |
| 1324 | if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) { |
| 1325 | clear_remaining_fade = true; // Reset occurs. |
| 1326 | } |
| 1327 | |
| 1328 | if (!cur_transition_request.is_empty()) { |
| 1329 | int new_idx = find_input(cur_transition_request); |
| 1330 | if (new_idx >= 0) { |
| 1331 | if (cur_current_index == new_idx) { |
| 1332 | if (allow_transition_to_self) { |
| 1333 | // Transition to same state. |
| 1334 | restart = input_data[cur_current_index].reset; |
| 1335 | clear_remaining_fade = true; |
| 1336 | } |
| 1337 | } else { |
| 1338 | switched = true; |
| 1339 | cur_prev_index = cur_current_index; |
| 1340 | set_parameter(prev_index, cur_current_index); |
| 1341 | cur_current_index = new_idx; |
| 1342 | set_parameter(current_index, cur_current_index); |
| 1343 | set_parameter(current_state, cur_transition_request); |
| 1344 | } |
| 1345 | } else { |
| 1346 | ERR_PRINT("No such input: '" + cur_transition_request + "'"); |
| 1347 | } |
| 1348 | cur_transition_request = String(); |
| 1349 | set_parameter(transition_request, cur_transition_request); |
| 1350 | } |
| 1351 | |
| 1352 | if (clear_remaining_fade) { |
| 1353 | cur_prev_xfading = 0; |
| 1354 | set_parameter(prev_xfading, 0); |
| 1355 | cur_prev_index = -1; |
| 1356 | set_parameter(prev_index, -1); |
| 1357 | } |
| 1358 | |
| 1359 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 1360 | |
| 1361 | // Special case for restart. |
| 1362 | if (restart) { |
| 1363 | pi.time = 0; |
| 1364 | pi.seeked = true; |
| 1365 | pi.weight = 1.0; |
| 1366 | return blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only); |
| 1367 | } |
| 1368 | |
| 1369 | if (switched) { |
| 1370 | cur_prev_xfading = xfade_time; |
| 1371 | } |
| 1372 | |
| 1373 | if (cur_current_index < 0 || cur_current_index >= get_input_count() || cur_prev_index >= get_input_count()) { |
| 1374 | return NodeTimeInfo(); |
| 1375 | } |
| 1376 | |
| 1377 | if (sync) { |
| 1378 | pi.weight = 0; |
| 1379 | for (int i = 0; i < get_input_count(); i++) { |
| 1380 | if (i != cur_current_index && i != cur_prev_index) { |
| 1381 | blend_input(i, pi, FILTER_IGNORE, true, p_test_only); |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | if (cur_prev_index < 0) { // Process current animation, check for transition. |
| 1387 | pi.weight = 1.0; |
| 1388 | cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only); |
| 1389 | if (input_data[cur_current_index].auto_advance && Animation::is_less_or_equal_approx(cur_nti.get_remain(input_data[cur_current_index].break_loop_at_end), xfade_time)) { |
| 1390 | set_parameter(transition_request, get_input_name((cur_current_index + 1) % get_input_count())); |
| 1391 | } |
| 1392 | } else { // Cross-fading from prev to current. |
| 1393 | |
| 1394 | real_t blend = 0.0; |
| 1395 | real_t blend_inv = 1.0; |
| 1396 | bool use_blend = sync; |
| 1397 | if (xfade_time > 0) { |
| 1398 | use_blend = true; |
| 1399 | blend = cur_prev_xfading / xfade_time; |
| 1400 | if (xfade_curve.is_valid()) { |
| 1401 | blend = xfade_curve->sample(blend); |
| 1402 | } |
| 1403 | blend_inv = 1.0 - blend; |
| 1404 | blend = Math::is_zero_approx(blend) ? CMP_EPSILON : blend; |
| 1405 | blend_inv = Math::is_zero_approx(blend_inv) ? CMP_EPSILON : blend_inv; |
| 1406 | } |
| 1407 | |
| 1408 | // Blend values must be more than CMP_EPSILON to process discrete keys in edge. |
| 1409 | pi.weight = blend_inv; |
| 1410 | if (input_data[cur_current_index].reset && !p_seek && switched) { // Just switched, seek to start of current. |
| 1411 | pi.time = 0; |
| 1412 | pi.seeked = true; |
| 1413 | } |
| 1414 | cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only); |
| 1415 | |
| 1416 | pi = p_playback_info; |
| 1417 | pi.seeked &= use_blend; |
| 1418 | pi.weight = blend; |
| 1419 | blend_input(cur_prev_index, pi, FILTER_IGNORE, true, p_test_only); |
| 1420 | if (!p_seek) { |
| 1421 | if (Animation::is_less_or_equal_approx(cur_prev_xfading, 0)) { |
| 1422 | set_parameter(prev_index, -1); |
| 1423 | } |
| 1424 | cur_prev_xfading -= Math::abs(p_playback_info.delta); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | set_parameter(prev_xfading, cur_prev_xfading); |
| 1429 | |
| 1430 | return cur_nti; |
| 1431 | } |
| 1432 | |
| 1433 | void AnimationNodeTransition::_get_property_list(List<PropertyInfo> *p_list) const { |
| 1434 | for (int i = 0; i < get_input_count(); i++) { |
| 1435 | p_list->push_back(PropertyInfo(Variant::STRING, "input_" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL)); |
| 1436 | p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/auto_advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL)); |
| 1437 | p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/break_loop_at_end", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL)); |
| 1438 | p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/reset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL)); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | void AnimationNodeTransition::_bind_methods() { |
| 1443 | ClassDB::bind_method(D_METHOD("set_input_count", "input_count"), &AnimationNodeTransition::set_input_count); |
| 1444 | |
| 1445 | ClassDB::bind_method(D_METHOD("set_input_as_auto_advance", "input", "enable"), &AnimationNodeTransition::set_input_as_auto_advance); |
| 1446 | ClassDB::bind_method(D_METHOD("is_input_set_as_auto_advance", "input"), &AnimationNodeTransition::is_input_set_as_auto_advance); |
| 1447 | |
| 1448 | ClassDB::bind_method(D_METHOD("set_input_break_loop_at_end", "input", "enable"), &AnimationNodeTransition::set_input_break_loop_at_end); |
| 1449 | ClassDB::bind_method(D_METHOD("is_input_loop_broken_at_end", "input"), &AnimationNodeTransition::is_input_loop_broken_at_end); |
| 1450 | |
| 1451 | ClassDB::bind_method(D_METHOD("set_input_reset", "input", "enable"), &AnimationNodeTransition::set_input_reset); |
| 1452 | ClassDB::bind_method(D_METHOD("is_input_reset", "input"), &AnimationNodeTransition::is_input_reset); |
| 1453 | |
| 1454 | ClassDB::bind_method(D_METHOD("set_xfade_time", "time"), &AnimationNodeTransition::set_xfade_time); |
| 1455 | ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeTransition::get_xfade_time); |
| 1456 | |
| 1457 | ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeTransition::set_xfade_curve); |
| 1458 | ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeTransition::get_xfade_curve); |
| 1459 | |
| 1460 | ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeTransition::set_allow_transition_to_self); |
| 1461 | ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeTransition::is_allow_transition_to_self); |
| 1462 | |
| 1463 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,120,0.01,suffix:s"), "set_xfade_time", "get_xfade_time"); |
| 1464 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve"); |
| 1465 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self"); |
| 1466 | ADD_PROPERTY(PropertyInfo(Variant::INT, "input_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED, "Inputs,input_"), "set_input_count", "get_input_count"); |
| 1467 | } |
| 1468 | |
| 1469 | AnimationNodeTransition::AnimationNodeTransition() { |
| 1470 | } |
| 1471 | |
| 1472 | ///////////////////// |
| 1473 | |
| 1474 | String AnimationNodeOutput::get_caption() const { |
| 1475 | return "Output"; |
| 1476 | } |
| 1477 | |
| 1478 | AnimationNode::NodeTimeInfo AnimationNodeOutput::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 1479 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 1480 | pi.weight = 1.0; |
| 1481 | return blend_input(0, pi, FILTER_IGNORE, true, p_test_only); |
| 1482 | } |
| 1483 | |
| 1484 | AnimationNodeOutput::AnimationNodeOutput() { |
| 1485 | add_input("output"); |
| 1486 | } |
| 1487 | |
| 1488 | /////////////////////////////////////////////////////// |
| 1489 | void AnimationNodeBlendTree::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) { |
| 1490 | ERR_FAIL_COND(nodes.has(p_name)); |
| 1491 | ERR_FAIL_COND(p_node.is_null()); |
| 1492 | ERR_FAIL_COND(p_name == SceneStringName(output)); |
| 1493 | ERR_FAIL_COND(String(p_name).contains_char('/')); |
| 1494 | |
| 1495 | Node n; |
| 1496 | n.node = p_node; |
| 1497 | n.position = p_position; |
| 1498 | n.connections.resize(n.node->get_input_count()); |
| 1499 | nodes[p_name] = n; |
| 1500 | |
| 1501 | emit_changed(); |
| 1502 | emit_signal(SNAME("tree_changed")); |
| 1503 | |
| 1504 | p_node->connect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed), CONNECT_REFERENCE_COUNTED); |
| 1505 | p_node->connect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed), CONNECT_REFERENCE_COUNTED); |
| 1506 | p_node->connect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed), CONNECT_REFERENCE_COUNTED); |
| 1507 | p_node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_name), CONNECT_REFERENCE_COUNTED); |
| 1508 | } |
| 1509 | |
| 1510 | Ref<AnimationNode> AnimationNodeBlendTree::get_node(const StringName &p_name) const { |
| 1511 | ERR_FAIL_COND_V(!nodes.has(p_name), Ref<AnimationNode>()); |
| 1512 | |
| 1513 | return nodes[p_name].node; |
| 1514 | } |
| 1515 | |
| 1516 | StringName AnimationNodeBlendTree::get_node_name(const Ref<AnimationNode> &p_node) const { |
| 1517 | for (const KeyValue<StringName, Node> &E : nodes) { |
| 1518 | if (E.value.node == p_node) { |
| 1519 | return E.key; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | ERR_FAIL_V(StringName()); |
| 1524 | } |
| 1525 | |
| 1526 | void AnimationNodeBlendTree::set_node_position(const StringName &p_node, const Vector2 &p_position) { |
| 1527 | ERR_FAIL_COND(!nodes.has(p_node)); |
| 1528 | nodes[p_node].position = p_position; |
| 1529 | } |
| 1530 | |
| 1531 | Vector2 AnimationNodeBlendTree::get_node_position(const StringName &p_node) const { |
| 1532 | ERR_FAIL_COND_V(!nodes.has(p_node), Vector2()); |
| 1533 | return nodes[p_node].position; |
| 1534 | } |
| 1535 | |
| 1536 | void AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) { |
| 1537 | Vector<StringName> ns; |
| 1538 | |
| 1539 | for (const KeyValue<StringName, Node> &E : nodes) { |
| 1540 | ns.push_back(E.key); |
| 1541 | } |
| 1542 | |
| 1543 | for (int i = 0; i < ns.size(); i++) { |
| 1544 | ChildNode cn; |
| 1545 | cn.name = ns[i]; |
| 1546 | cn.node = nodes[cn.name].node; |
| 1547 | r_child_nodes->push_back(cn); |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | bool AnimationNodeBlendTree::has_node(const StringName &p_name) const { |
| 1552 | return nodes.has(p_name); |
| 1553 | } |
| 1554 | |
| 1555 | Vector<StringName> AnimationNodeBlendTree::get_node_connection_array(const StringName &p_name) const { |
| 1556 | ERR_FAIL_COND_V(!nodes.has(p_name), Vector<StringName>()); |
| 1557 | return nodes[p_name].connections; |
| 1558 | } |
| 1559 | |
| 1560 | void AnimationNodeBlendTree::remove_node(const StringName &p_name) { |
| 1561 | ERR_FAIL_COND(!nodes.has(p_name)); |
| 1562 | ERR_FAIL_COND(p_name == SceneStringName(output)); //can't delete output |
| 1563 | |
| 1564 | { |
| 1565 | Ref<AnimationNode> node = nodes[p_name].node; |
| 1566 | node->disconnect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed)); |
| 1567 | node->disconnect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed)); |
| 1568 | node->disconnect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed)); |
| 1569 | node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed)); |
| 1570 | } |
| 1571 | |
| 1572 | nodes.erase(p_name); |
| 1573 | |
| 1574 | // Erase connections to name. |
| 1575 | for (KeyValue<StringName, Node> &E : nodes) { |
| 1576 | for (int i = 0; i < E.value.connections.size(); i++) { |
| 1577 | if (E.value.connections[i] == p_name) { |
| 1578 | E.value.connections.write[i] = StringName(); |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name); |
| 1584 | emit_changed(); |
| 1585 | emit_signal(SNAME("tree_changed")); |
| 1586 | } |
| 1587 | |
| 1588 | void AnimationNodeBlendTree::rename_node(const StringName &p_name, const StringName &p_new_name) { |
| 1589 | ERR_FAIL_COND(!nodes.has(p_name)); |
| 1590 | ERR_FAIL_COND(nodes.has(p_new_name)); |
| 1591 | ERR_FAIL_COND(p_name == SceneStringName(output)); |
| 1592 | ERR_FAIL_COND(p_new_name == SceneStringName(output)); |
| 1593 | |
| 1594 | nodes[p_name].node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed)); |
| 1595 | |
| 1596 | const Node temp_copy = nodes[p_name]; |
| 1597 | nodes[p_new_name] = temp_copy; // might realloc |
| 1598 | nodes.erase(p_name); |
| 1599 | |
| 1600 | // Rename connections. |
| 1601 | for (KeyValue<StringName, Node> &E : nodes) { |
| 1602 | for (int i = 0; i < E.value.connections.size(); i++) { |
| 1603 | if (E.value.connections[i] == p_name) { |
| 1604 | E.value.connections.write[i] = p_new_name; |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | // Connection must be done with new name. |
| 1609 | nodes[p_new_name].node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_new_name), CONNECT_REFERENCE_COUNTED); |
| 1610 | |
| 1611 | emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name); |
| 1612 | emit_signal(SNAME("tree_changed")); |
| 1613 | } |
| 1614 | |
| 1615 | void AnimationNodeBlendTree::connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) { |
| 1616 | ERR_FAIL_COND(!nodes.has(p_output_node)); |
| 1617 | ERR_FAIL_COND(!nodes.has(p_input_node)); |
| 1618 | ERR_FAIL_COND(p_output_node == SceneStringName(output)); |
| 1619 | ERR_FAIL_COND(p_input_node == p_output_node); |
| 1620 | |
| 1621 | Ref<AnimationNode> input = nodes[p_input_node].node; |
| 1622 | ERR_FAIL_INDEX(p_input_index, nodes[p_input_node].connections.size()); |
| 1623 | |
| 1624 | for (KeyValue<StringName, Node> &E : nodes) { |
| 1625 | for (int i = 0; i < E.value.connections.size(); i++) { |
| 1626 | StringName output = E.value.connections[i]; |
| 1627 | ERR_FAIL_COND(output == p_output_node); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | nodes[p_input_node].connections.write[p_input_index] = p_output_node; |
| 1632 | |
| 1633 | emit_changed(); |
| 1634 | } |
| 1635 | |
| 1636 | void AnimationNodeBlendTree::disconnect_node(const StringName &p_node, int p_input_index) { |
| 1637 | ERR_FAIL_COND(!nodes.has(p_node)); |
| 1638 | |
| 1639 | Ref<AnimationNode> input = nodes[p_node].node; |
| 1640 | ERR_FAIL_INDEX(p_input_index, nodes[p_node].connections.size()); |
| 1641 | |
| 1642 | nodes[p_node].connections.write[p_input_index] = StringName(); |
| 1643 | } |
| 1644 | |
| 1645 | AnimationNodeBlendTree::ConnectionError AnimationNodeBlendTree::can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const { |
| 1646 | if (!nodes.has(p_output_node) || p_output_node == SceneStringName(output)) { |
| 1647 | return CONNECTION_ERROR_NO_OUTPUT; |
| 1648 | } |
| 1649 | |
| 1650 | if (!nodes.has(p_input_node)) { |
| 1651 | return CONNECTION_ERROR_NO_INPUT; |
| 1652 | } |
| 1653 | |
| 1654 | if (p_input_node == p_output_node) { |
| 1655 | return CONNECTION_ERROR_SAME_NODE; |
| 1656 | } |
| 1657 | |
| 1658 | Ref<AnimationNode> input = nodes[p_input_node].node; |
| 1659 | |
| 1660 | if (p_input_index < 0 || p_input_index >= nodes[p_input_node].connections.size()) { |
| 1661 | return CONNECTION_ERROR_NO_INPUT_INDEX; |
| 1662 | } |
| 1663 | |
| 1664 | if (nodes[p_input_node].connections[p_input_index] != StringName()) { |
| 1665 | return CONNECTION_ERROR_CONNECTION_EXISTS; |
| 1666 | } |
| 1667 | |
| 1668 | for (const KeyValue<StringName, Node> &E : nodes) { |
| 1669 | for (int i = 0; i < E.value.connections.size(); i++) { |
| 1670 | const StringName output = E.value.connections[i]; |
| 1671 | if (output == p_output_node) { |
| 1672 | return CONNECTION_ERROR_CONNECTION_EXISTS; |
| 1673 | } |
| 1674 | } |
| 1675 | } |
| 1676 | return CONNECTION_OK; |
| 1677 | } |
| 1678 | |
| 1679 | void AnimationNodeBlendTree::get_node_connections(List<NodeConnection> *r_connections) const { |
| 1680 | for (const KeyValue<StringName, Node> &E : nodes) { |
| 1681 | for (int i = 0; i < E.value.connections.size(); i++) { |
| 1682 | const StringName output = E.value.connections[i]; |
| 1683 | if (output != StringName()) { |
| 1684 | NodeConnection nc; |
| 1685 | nc.input_node = E.key; |
| 1686 | nc.input_index = i; |
| 1687 | nc.output_node = output; |
| 1688 | r_connections->push_back(nc); |
| 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | String AnimationNodeBlendTree::get_caption() const { |
| 1695 | return "BlendTree"; |
| 1696 | } |
| 1697 | |
| 1698 | AnimationNode::NodeTimeInfo AnimationNodeBlendTree::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) { |
| 1699 | Ref<AnimationNodeOutput> output = nodes[SceneStringName(output)].node; |
| 1700 | node_state.connections = nodes[SceneStringName(output)].connections; |
| 1701 | ERR_FAIL_COND_V(output.is_null(), NodeTimeInfo()); |
| 1702 | |
| 1703 | AnimationMixer::PlaybackInfo pi = p_playback_info; |
| 1704 | pi.weight = 1.0; |
| 1705 | |
| 1706 | return _blend_node(output, "output", this, pi, FILTER_IGNORE, true, p_test_only, nullptr); |
| 1707 | } |
| 1708 | |
| 1709 | LocalVector<StringName> AnimationNodeBlendTree::get_node_list() const { |
| 1710 | LocalVector<StringName> list; |
| 1711 | list.reserve(nodes.size()); |
| 1712 | for (const KeyValue<StringName, Node> &E : nodes) { |
| 1713 | list.push_back(E.key); |
| 1714 | } |
| 1715 | list.sort_custom<StringName::AlphCompare>(); |
| 1716 | return list; |
| 1717 | } |
| 1718 | |
| 1719 | TypedArray<StringName> AnimationNodeBlendTree::get_node_list_as_typed_array() const { |
| 1720 | TypedArray<StringName> typed_arr; |
| 1721 | LocalVector<StringName> vec = get_node_list(); |
| 1722 | typed_arr.resize(vec.size()); |
| 1723 | for (uint32_t i = 0; i < vec.size(); i++) { |
| 1724 | typed_arr[i] = vec[i]; |
| 1725 | } |
| 1726 | return typed_arr; |
| 1727 | } |
| 1728 | |
| 1729 | void AnimationNodeBlendTree::set_graph_offset(const Vector2 &p_graph_offset) { |
| 1730 | graph_offset = p_graph_offset; |
| 1731 | } |
| 1732 | |
| 1733 | Vector2 AnimationNodeBlendTree::get_graph_offset() const { |
| 1734 | return graph_offset; |
| 1735 | } |
| 1736 | |
| 1737 | Ref<AnimationNode> AnimationNodeBlendTree::get_child_by_name(const StringName &p_name) const { |
| 1738 | return get_node(p_name); |
| 1739 | } |
| 1740 | |
| 1741 | bool AnimationNodeBlendTree::_set(const StringName &p_name, const Variant &p_value) { |
| 1742 | String prop_name = p_name; |
| 1743 | if (prop_name.begins_with("nodes/")) { |
| 1744 | String node_name = prop_name.get_slicec('/', 1); |
| 1745 | String what = prop_name.get_slicec('/', 2); |
| 1746 | |
| 1747 | if (what == "node") { |
| 1748 | Ref<AnimationNode> anode = p_value; |
| 1749 | if (anode.is_valid()) { |
| 1750 | add_node(node_name, p_value); |
| 1751 | } |
| 1752 | return true; |
| 1753 | } |
| 1754 | |
| 1755 | if (what == "position") { |
| 1756 | if (nodes.has(node_name)) { |
| 1757 | nodes[node_name].position = p_value; |
| 1758 | } |
| 1759 | return true; |
| 1760 | } |
| 1761 | } else if (prop_name == "node_connections") { |
| 1762 | Array conns = p_value; |
| 1763 | ERR_FAIL_COND_V(conns.size() % 3 != 0, false); |
| 1764 | |
| 1765 | for (int i = 0; i < conns.size(); i += 3) { |
| 1766 | connect_node(conns[i], conns[i + 1], conns[i + 2]); |
| 1767 | } |
| 1768 | return true; |
| 1769 | } |
| 1770 | |
| 1771 | return false; |
| 1772 | } |
| 1773 | |
| 1774 | bool AnimationNodeBlendTree::_get(const StringName &p_name, Variant &r_ret) const { |
| 1775 | String prop_name = p_name; |
| 1776 | if (prop_name.begins_with("nodes/")) { |
| 1777 | String node_name = prop_name.get_slicec('/', 1); |
| 1778 | String what = prop_name.get_slicec('/', 2); |
| 1779 | |
| 1780 | if (what == "node") { |
| 1781 | if (nodes.has(node_name)) { |
| 1782 | r_ret = nodes[node_name].node; |
| 1783 | return true; |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | if (what == "position") { |
| 1788 | if (nodes.has(node_name)) { |
| 1789 | r_ret = nodes[node_name].position; |
| 1790 | return true; |
| 1791 | } |
| 1792 | } |
| 1793 | } else if (prop_name == "node_connections") { |
| 1794 | List<NodeConnection> nc; |
| 1795 | get_node_connections(&nc); |
| 1796 | Array conns; |
| 1797 | conns.resize(nc.size() * 3); |
| 1798 | |
| 1799 | int idx = 0; |
| 1800 | for (const NodeConnection &E : nc) { |
| 1801 | conns[idx * 3 + 0] = E.input_node; |
| 1802 | conns[idx * 3 + 1] = E.input_index; |
| 1803 | conns[idx * 3 + 2] = E.output_node; |
| 1804 | idx++; |
| 1805 | } |
| 1806 | |
| 1807 | r_ret = conns; |
| 1808 | return true; |
| 1809 | } |
| 1810 | |
| 1811 | return false; |
| 1812 | } |
| 1813 | |
| 1814 | void AnimationNodeBlendTree::_get_property_list(List<PropertyInfo> *p_list) const { |
| 1815 | List<StringName> names; |
| 1816 | for (const KeyValue<StringName, Node> &E : nodes) { |
| 1817 | names.push_back(E.key); |
| 1818 | } |
| 1819 | |
| 1820 | for (const StringName &E : names) { |
| 1821 | String prop_name = E; |
| 1822 | if (prop_name != "output") { |
| 1823 | p_list->push_back(PropertyInfo(Variant::OBJECT, "nodes/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR)); |
| 1824 | } |
| 1825 | p_list->push_back(PropertyInfo(Variant::VECTOR2, "nodes/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); |
| 1826 | } |
| 1827 | |
| 1828 | p_list->push_back(PropertyInfo(Variant::ARRAY, "node_connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); |
| 1829 | } |
| 1830 | |
| 1831 | void AnimationNodeBlendTree::_tree_changed() { |
| 1832 | AnimationRootNode::_tree_changed(); |
| 1833 | } |
| 1834 | |
| 1835 | void AnimationNodeBlendTree::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) { |
| 1836 | AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name); |
| 1837 | } |
| 1838 | |
| 1839 | void AnimationNodeBlendTree::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) { |
| 1840 | AnimationRootNode::_animation_node_removed(p_oid, p_node); |
| 1841 | } |
| 1842 | |
| 1843 | void AnimationNodeBlendTree::reset_state() { |
| 1844 | graph_offset = Vector2(); |
| 1845 | nodes.clear(); |
| 1846 | _initialize_node_tree(); |
| 1847 | emit_changed(); |
| 1848 | emit_signal(SNAME("tree_changed")); |
| 1849 | } |
| 1850 | |
| 1851 | void AnimationNodeBlendTree::_node_changed(const StringName &p_node) { |
| 1852 | ERR_FAIL_COND(!nodes.has(p_node)); |
| 1853 | nodes[p_node].connections.resize(nodes[p_node].node->get_input_count()); |
| 1854 | emit_signal(SNAME("node_changed"), p_node); |
| 1855 | } |
| 1856 | |
| 1857 | #ifdef TOOLS_ENABLED |
| 1858 | void AnimationNodeBlendTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { |
| 1859 | const String pf = p_function; |
| 1860 | bool add_node_options = false; |
| 1861 | if (p_idx == 0) { |
| 1862 | add_node_options = (pf == "get_node" || pf == "has_node" || pf == "rename_node" || pf == "remove_node" || pf == "set_node_position" || pf == "get_node_position" || pf == "connect_node" || pf == "disconnect_node"); |
| 1863 | } else if (p_idx == 2) { |
| 1864 | add_node_options = (pf == "connect_node" || pf == "disconnect_node"); |
| 1865 | } |
| 1866 | if (add_node_options) { |
| 1867 | for (const KeyValue<StringName, Node> &E : nodes) { |
| 1868 | r_options->push_back(String(E.key).quote()); |
| 1869 | } |
| 1870 | } |
| 1871 | AnimationRootNode::get_argument_options(p_function, p_idx, r_options); |
| 1872 | } |
| 1873 | #endif |
| 1874 | |
| 1875 | void AnimationNodeBlendTree::_bind_methods() { |
| 1876 | ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeBlendTree::add_node, DEFVAL(Vector2())); |
| 1877 | ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeBlendTree::get_node); |
| 1878 | ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeBlendTree::remove_node); |
| 1879 | ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeBlendTree::rename_node); |
| 1880 | ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeBlendTree::has_node); |
| 1881 | ClassDB::bind_method(D_METHOD("connect_node", "input_node", "input_index", "output_node"), &AnimationNodeBlendTree::connect_node); |
| 1882 | ClassDB::bind_method(D_METHOD("disconnect_node", "input_node", "input_index"), &AnimationNodeBlendTree::disconnect_node); |
| 1883 | ClassDB::bind_method(D_METHOD("get_node_list"), &AnimationNodeBlendTree::get_node_list_as_typed_array); |
| 1884 | |
| 1885 | ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeBlendTree::set_node_position); |
| 1886 | ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeBlendTree::get_node_position); |
| 1887 | |
| 1888 | ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeBlendTree::set_graph_offset); |
| 1889 | ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeBlendTree::get_graph_offset); |
| 1890 | |
| 1891 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset"); |
| 1892 | |
| 1893 | BIND_CONSTANT(CONNECTION_OK); |
| 1894 | BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT); |
| 1895 | BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT_INDEX); |
| 1896 | BIND_CONSTANT(CONNECTION_ERROR_NO_OUTPUT); |
| 1897 | BIND_CONSTANT(CONNECTION_ERROR_SAME_NODE); |
| 1898 | BIND_CONSTANT(CONNECTION_ERROR_CONNECTION_EXISTS); |
| 1899 | |
| 1900 | ADD_SIGNAL(MethodInfo(SNAME("node_changed"), PropertyInfo(Variant::STRING_NAME, "node_name"))); |
| 1901 | } |
| 1902 | |
| 1903 | void AnimationNodeBlendTree::_initialize_node_tree() { |
| 1904 | Ref<AnimationNodeOutput> output; |
| 1905 | output.instantiate(); |
| 1906 | Node n; |
| 1907 | n.node = output; |
| 1908 | n.position = Vector2(300, 150); |
| 1909 | n.connections.resize(1); |
| 1910 | nodes["output"] = n; |
| 1911 | } |
| 1912 | |
| 1913 | AnimationNodeBlendTree::AnimationNodeBlendTree() { |
| 1914 | _initialize_node_tree(); |
| 1915 | } |
| 1916 | |
| 1917 | AnimationNodeBlendTree::~AnimationNodeBlendTree() { |
| 1918 | } |