animation_tree.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /**************************************************************************/
  2. /* animation_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. #include "animation_tree.h"
  31. #include "animation_tree.compat.inc"
  32. #include "animation_blend_tree.h"
  33. #include "core/config/engine.h"
  34. #include "scene/animation/animation_player.h"
  35. void AnimationNode::get_parameter_list(List<PropertyInfo> *r_list) const {
  36. Array parameters;
  37. if (GDVIRTUAL_CALL(_get_parameter_list, parameters)) {
  38. for (int i = 0; i < parameters.size(); i++) {
  39. Dictionary d = parameters[i];
  40. ERR_CONTINUE(d.is_empty());
  41. r_list->push_back(PropertyInfo::from_dict(d));
  42. }
  43. }
  44. r_list->push_back(PropertyInfo(Variant::FLOAT, current_length, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY));
  45. r_list->push_back(PropertyInfo(Variant::FLOAT, current_position, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY));
  46. r_list->push_back(PropertyInfo(Variant::FLOAT, current_delta, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY));
  47. }
  48. Variant AnimationNode::get_parameter_default_value(const StringName &p_parameter) const {
  49. Variant ret;
  50. GDVIRTUAL_CALL(_get_parameter_default_value, p_parameter, ret);
  51. return ret;
  52. }
  53. bool AnimationNode::is_parameter_read_only(const StringName &p_parameter) const {
  54. bool ret = false;
  55. if (GDVIRTUAL_CALL(_is_parameter_read_only, p_parameter, ret) && ret) {
  56. return true;
  57. }
  58. if (p_parameter == current_length || p_parameter == current_position || p_parameter == current_delta) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. void AnimationNode::set_parameter(const StringName &p_name, const Variant &p_value) {
  64. ERR_FAIL_NULL(process_state);
  65. if (process_state->is_testing) {
  66. return;
  67. }
  68. const AHashMap<StringName, int>::Iterator it = property_cache.find(p_name);
  69. if (it) {
  70. process_state->tree->property_map.get_by_index(it->value).value.first = p_value;
  71. return;
  72. }
  73. ERR_FAIL_COND(!process_state->tree->property_parent_map.has(node_state.base_path));
  74. ERR_FAIL_COND(!process_state->tree->property_parent_map[node_state.base_path].has(p_name));
  75. StringName path = process_state->tree->property_parent_map[node_state.base_path][p_name];
  76. int idx = process_state->tree->property_map.get_index(path);
  77. property_cache.insert_new(p_name, idx);
  78. process_state->tree->property_map.get_by_index(idx).value.first = p_value;
  79. }
  80. Variant AnimationNode::get_parameter(const StringName &p_name) const {
  81. ERR_FAIL_NULL_V(process_state, Variant());
  82. const AHashMap<StringName, int>::ConstIterator it = property_cache.find(p_name);
  83. if (it) {
  84. return process_state->tree->property_map.get_by_index(it->value).value.first;
  85. }
  86. ERR_FAIL_COND_V(!process_state->tree->property_parent_map.has(node_state.base_path), Variant());
  87. ERR_FAIL_COND_V(!process_state->tree->property_parent_map[node_state.base_path].has(p_name), Variant());
  88. StringName path = process_state->tree->property_parent_map[node_state.base_path][p_name];
  89. int idx = process_state->tree->property_map.get_index(path);
  90. property_cache.insert_new(p_name, idx);
  91. return process_state->tree->property_map.get_by_index(idx).value.first;
  92. }
  93. void AnimationNode::set_node_time_info(const NodeTimeInfo &p_node_time_info) {
  94. set_parameter(current_length, p_node_time_info.length);
  95. set_parameter(current_position, p_node_time_info.position);
  96. set_parameter(current_delta, p_node_time_info.delta);
  97. }
  98. AnimationNode::NodeTimeInfo AnimationNode::get_node_time_info() const {
  99. NodeTimeInfo nti;
  100. nti.length = get_parameter(current_length);
  101. nti.position = get_parameter(current_position);
  102. nti.delta = get_parameter(current_delta);
  103. return nti;
  104. }
  105. void AnimationNode::get_child_nodes(List<ChildNode> *r_child_nodes) {
  106. Dictionary cn;
  107. if (GDVIRTUAL_CALL(_get_child_nodes, cn)) {
  108. List<Variant> keys;
  109. cn.get_key_list(&keys);
  110. for (const Variant &E : keys) {
  111. ChildNode child;
  112. child.name = E;
  113. child.node = cn[E];
  114. r_child_nodes->push_back(child);
  115. }
  116. }
  117. }
  118. void AnimationNode::blend_animation(const StringName &p_animation, AnimationMixer::PlaybackInfo p_playback_info) {
  119. ERR_FAIL_NULL(process_state);
  120. p_playback_info.track_weights = node_state.track_weights;
  121. process_state->tree->make_animation_instance(p_animation, p_playback_info);
  122. }
  123. AnimationNode::NodeTimeInfo AnimationNode::_pre_process(ProcessState *p_process_state, AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  124. process_state = p_process_state;
  125. NodeTimeInfo nti = process(p_playback_info, p_test_only);
  126. process_state = nullptr;
  127. return nti;
  128. }
  129. void AnimationNode::make_invalid(const String &p_reason) {
  130. ERR_FAIL_NULL(process_state);
  131. process_state->valid = false;
  132. if (!process_state->invalid_reasons.is_empty()) {
  133. process_state->invalid_reasons += "\n";
  134. }
  135. process_state->invalid_reasons += String::utf8("• ") + p_reason;
  136. }
  137. AnimationTree *AnimationNode::get_animation_tree() const {
  138. ERR_FAIL_NULL_V(process_state, nullptr);
  139. return process_state->tree;
  140. }
  141. AnimationNode::NodeTimeInfo AnimationNode::blend_input(int p_input, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter, bool p_sync, bool p_test_only) {
  142. ERR_FAIL_INDEX_V(p_input, inputs.size(), NodeTimeInfo());
  143. AnimationNodeBlendTree *blend_tree = Object::cast_to<AnimationNodeBlendTree>(node_state.parent);
  144. ERR_FAIL_NULL_V(blend_tree, NodeTimeInfo());
  145. // Update connections.
  146. StringName current_name = blend_tree->get_node_name(Ref<AnimationNode>(this));
  147. node_state.connections = blend_tree->get_node_connection_array(current_name);
  148. // Get node which is connected input port.
  149. StringName node_name = node_state.connections[p_input];
  150. if (!blend_tree->has_node(node_name)) {
  151. make_invalid(vformat(RTR("Nothing connected to input '%s' of node '%s'."), get_input_name(p_input), current_name));
  152. return NodeTimeInfo();
  153. }
  154. Ref<AnimationNode> node = blend_tree->get_node(node_name);
  155. ERR_FAIL_COND_V(node.is_null(), NodeTimeInfo());
  156. real_t activity = 0.0;
  157. Vector<AnimationTree::Activity> *activity_ptr = process_state->tree->input_activity_map.getptr(node_state.base_path);
  158. NodeTimeInfo nti = _blend_node(node, node_name, nullptr, p_playback_info, p_filter, p_sync, p_test_only, &activity);
  159. if (activity_ptr && p_input < activity_ptr->size()) {
  160. activity_ptr->write[p_input].last_pass = process_state->last_pass;
  161. activity_ptr->write[p_input].activity = activity;
  162. }
  163. return nti;
  164. }
  165. AnimationNode::NodeTimeInfo AnimationNode::blend_node(Ref<AnimationNode> p_node, const StringName &p_subpath, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter, bool p_sync, bool p_test_only) {
  166. ERR_FAIL_COND_V(p_node.is_null(), NodeTimeInfo());
  167. p_node->node_state.connections.clear();
  168. return _blend_node(p_node, p_subpath, this, p_playback_info, p_filter, p_sync, p_test_only, nullptr);
  169. }
  170. AnimationNode::NodeTimeInfo AnimationNode::_blend_node(Ref<AnimationNode> p_node, const StringName &p_subpath, AnimationNode *p_new_parent, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter, bool p_sync, bool p_test_only, real_t *r_activity) {
  171. ERR_FAIL_NULL_V(process_state, NodeTimeInfo());
  172. int blend_count = node_state.track_weights.size();
  173. if (p_node->node_state.track_weights.size() != blend_count) {
  174. p_node->node_state.track_weights.resize(blend_count);
  175. }
  176. real_t *blendw = p_node->node_state.track_weights.ptrw();
  177. const real_t *blendr = node_state.track_weights.ptr();
  178. bool any_valid = false;
  179. if (has_filter() && is_filter_enabled() && p_filter != FILTER_IGNORE) {
  180. for (int i = 0; i < blend_count; i++) {
  181. blendw[i] = 0.0; // All to zero by default.
  182. }
  183. for (const KeyValue<NodePath, bool> &E : filter) {
  184. const AHashMap<NodePath, int> &map = *process_state->track_map;
  185. if (!map.has(E.key)) {
  186. continue;
  187. }
  188. int idx = map[E.key];
  189. blendw[idx] = 1.0; // Filtered goes to one.
  190. }
  191. switch (p_filter) {
  192. case FILTER_IGNORE:
  193. break; // Will not happen anyway.
  194. case FILTER_PASS: {
  195. // Values filtered pass, the rest don't.
  196. for (int i = 0; i < blend_count; i++) {
  197. if (blendw[i] == 0) { // Not filtered, does not pass.
  198. continue;
  199. }
  200. blendw[i] = blendr[i] * p_playback_info.weight;
  201. if (!Math::is_zero_approx(blendw[i])) {
  202. any_valid = true;
  203. }
  204. }
  205. } break;
  206. case FILTER_STOP: {
  207. // Values filtered don't pass, the rest are blended.
  208. for (int i = 0; i < blend_count; i++) {
  209. if (blendw[i] > 0) { // Filtered, does not pass.
  210. continue;
  211. }
  212. blendw[i] = blendr[i] * p_playback_info.weight;
  213. if (!Math::is_zero_approx(blendw[i])) {
  214. any_valid = true;
  215. }
  216. }
  217. } break;
  218. case FILTER_BLEND: {
  219. // Filtered values are blended, the rest are passed without blending.
  220. for (int i = 0; i < blend_count; i++) {
  221. if (blendw[i] == 1.0) {
  222. blendw[i] = blendr[i] * p_playback_info.weight; // Filtered, blend.
  223. } else {
  224. blendw[i] = blendr[i]; // Not filtered, do not blend.
  225. }
  226. if (!Math::is_zero_approx(blendw[i])) {
  227. any_valid = true;
  228. }
  229. }
  230. } break;
  231. }
  232. } else {
  233. for (int i = 0; i < blend_count; i++) {
  234. // Regular blend.
  235. blendw[i] = blendr[i] * p_playback_info.weight;
  236. if (!Math::is_zero_approx(blendw[i])) {
  237. any_valid = true;
  238. }
  239. }
  240. }
  241. if (r_activity) {
  242. *r_activity = 0;
  243. for (int i = 0; i < blend_count; i++) {
  244. *r_activity = MAX(*r_activity, Math::abs(blendw[i]));
  245. }
  246. }
  247. String new_path;
  248. AnimationNode *new_parent;
  249. // This is the slowest part of processing, but as strings process in powers of 2, and the paths always exist, it will not result in that many allocations.
  250. if (p_new_parent) {
  251. new_parent = p_new_parent;
  252. new_path = String(node_state.base_path) + String(p_subpath) + "/";
  253. } else {
  254. ERR_FAIL_NULL_V(node_state.parent, NodeTimeInfo());
  255. new_parent = node_state.parent;
  256. new_path = String(new_parent->node_state.base_path) + String(p_subpath) + "/";
  257. }
  258. // This process, which depends on p_sync is needed to process sync correctly in the case of
  259. // that a synced AnimationNodeSync exists under the un-synced AnimationNodeSync.
  260. p_node->set_node_state_base_path(new_path);
  261. p_node->node_state.parent = new_parent;
  262. if (!p_playback_info.seeked && !p_sync && !any_valid) {
  263. p_playback_info.delta = 0.0;
  264. return p_node->_pre_process(process_state, p_playback_info, p_test_only);
  265. }
  266. return p_node->_pre_process(process_state, p_playback_info, p_test_only);
  267. }
  268. String AnimationNode::get_caption() const {
  269. String ret = "Node";
  270. GDVIRTUAL_CALL(_get_caption, ret);
  271. return ret;
  272. }
  273. bool AnimationNode::add_input(const String &p_name) {
  274. // Root nodes can't add inputs.
  275. ERR_FAIL_COND_V(Object::cast_to<AnimationRootNode>(this) != nullptr, false);
  276. Input input;
  277. ERR_FAIL_COND_V(p_name.contains(".") || p_name.contains("/"), false);
  278. input.name = p_name;
  279. inputs.push_back(input);
  280. emit_changed();
  281. return true;
  282. }
  283. void AnimationNode::remove_input(int p_index) {
  284. ERR_FAIL_INDEX(p_index, inputs.size());
  285. inputs.remove_at(p_index);
  286. emit_changed();
  287. }
  288. bool AnimationNode::set_input_name(int p_input, const String &p_name) {
  289. ERR_FAIL_INDEX_V(p_input, inputs.size(), false);
  290. ERR_FAIL_COND_V(p_name.contains(".") || p_name.contains("/"), false);
  291. inputs.write[p_input].name = p_name;
  292. emit_changed();
  293. return true;
  294. }
  295. String AnimationNode::get_input_name(int p_input) const {
  296. ERR_FAIL_INDEX_V(p_input, inputs.size(), String());
  297. return inputs[p_input].name;
  298. }
  299. int AnimationNode::get_input_count() const {
  300. return inputs.size();
  301. }
  302. int AnimationNode::find_input(const String &p_name) const {
  303. int idx = -1;
  304. for (int i = 0; i < inputs.size(); i++) {
  305. if (inputs[i].name == p_name) {
  306. idx = i;
  307. break;
  308. }
  309. }
  310. return idx;
  311. }
  312. AnimationNode::NodeTimeInfo AnimationNode::process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  313. process_state->is_testing = p_test_only;
  314. AnimationMixer::PlaybackInfo pi = p_playback_info;
  315. if (p_playback_info.seeked) {
  316. if (p_playback_info.is_external_seeking) {
  317. pi.delta = get_node_time_info().position - p_playback_info.time;
  318. }
  319. } else {
  320. pi.time = get_node_time_info().position + p_playback_info.delta;
  321. }
  322. NodeTimeInfo nti = _process(pi, p_test_only);
  323. if (!p_test_only) {
  324. set_node_time_info(nti);
  325. }
  326. return nti;
  327. }
  328. AnimationNode::NodeTimeInfo AnimationNode::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  329. double r_ret = 0.0;
  330. GDVIRTUAL_CALL(_process, p_playback_info.time, p_playback_info.seeked, p_playback_info.is_external_seeking, p_test_only, r_ret);
  331. NodeTimeInfo nti;
  332. nti.delta = r_ret;
  333. return nti;
  334. }
  335. void AnimationNode::set_filter_path(const NodePath &p_path, bool p_enable) {
  336. if (p_enable) {
  337. filter[p_path] = true;
  338. } else {
  339. filter.erase(p_path);
  340. }
  341. }
  342. void AnimationNode::set_filter_enabled(bool p_enable) {
  343. filter_enabled = p_enable;
  344. }
  345. bool AnimationNode::is_filter_enabled() const {
  346. return filter_enabled;
  347. }
  348. void AnimationNode::set_deletable(bool p_closable) {
  349. closable = p_closable;
  350. }
  351. bool AnimationNode::is_deletable() const {
  352. return closable;
  353. }
  354. bool AnimationNode::is_path_filtered(const NodePath &p_path) const {
  355. return filter.has(p_path);
  356. }
  357. bool AnimationNode::has_filter() const {
  358. bool ret = false;
  359. GDVIRTUAL_CALL(_has_filter, ret);
  360. return ret;
  361. }
  362. Array AnimationNode::_get_filters() const {
  363. Array paths;
  364. for (const KeyValue<NodePath, bool> &E : filter) {
  365. paths.push_back(String(E.key)); // Use strings, so sorting is possible.
  366. }
  367. paths.sort(); // Done so every time the scene is saved, it does not change.
  368. return paths;
  369. }
  370. void AnimationNode::_set_filters(const Array &p_filters) {
  371. filter.clear();
  372. for (int i = 0; i < p_filters.size(); i++) {
  373. set_filter_path(p_filters[i], true);
  374. }
  375. }
  376. void AnimationNode::_validate_property(PropertyInfo &p_property) const {
  377. if (!has_filter() && (p_property.name == "filter_enabled" || p_property.name == "filters")) {
  378. p_property.usage = PROPERTY_USAGE_NONE;
  379. }
  380. }
  381. Ref<AnimationNode> AnimationNode::get_child_by_name(const StringName &p_name) const {
  382. Ref<AnimationNode> ret;
  383. GDVIRTUAL_CALL(_get_child_by_name, p_name, ret);
  384. return ret;
  385. }
  386. Ref<AnimationNode> AnimationNode::find_node_by_path(const String &p_name) const {
  387. Vector<String> split = p_name.split("/");
  388. Ref<AnimationNode> ret = const_cast<AnimationNode *>(this);
  389. for (int i = 0; i < split.size(); i++) {
  390. ret = ret->get_child_by_name(split[i]);
  391. if (!ret.is_valid()) {
  392. break;
  393. }
  394. }
  395. return ret;
  396. }
  397. void AnimationNode::blend_animation_ex(const StringName &p_animation, double p_time, double p_delta, bool p_seeked, bool p_is_external_seeking, real_t p_blend, Animation::LoopedFlag p_looped_flag) {
  398. AnimationMixer::PlaybackInfo info;
  399. info.time = p_time;
  400. info.delta = p_delta;
  401. info.seeked = p_seeked;
  402. info.is_external_seeking = p_is_external_seeking;
  403. info.weight = p_blend;
  404. info.looped_flag = p_looped_flag;
  405. blend_animation(p_animation, info);
  406. }
  407. double AnimationNode::blend_node_ex(const StringName &p_sub_path, Ref<AnimationNode> p_node, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter, bool p_sync, bool p_test_only) {
  408. AnimationMixer::PlaybackInfo info;
  409. info.time = p_time;
  410. info.seeked = p_seek;
  411. info.is_external_seeking = p_is_external_seeking;
  412. info.weight = p_blend;
  413. NodeTimeInfo nti = blend_node(p_node, p_sub_path, info, p_filter, p_sync, p_test_only);
  414. return nti.length - nti.position;
  415. }
  416. double AnimationNode::blend_input_ex(int p_input, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter, bool p_sync, bool p_test_only) {
  417. AnimationMixer::PlaybackInfo info;
  418. info.time = p_time;
  419. info.seeked = p_seek;
  420. info.is_external_seeking = p_is_external_seeking;
  421. info.weight = p_blend;
  422. NodeTimeInfo nti = blend_input(p_input, info, p_filter, p_sync, p_test_only);
  423. return nti.length - nti.position;
  424. }
  425. #ifdef TOOLS_ENABLED
  426. void AnimationNode::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  427. const String pf = p_function;
  428. if (p_idx == 0) {
  429. if (pf == "find_input") {
  430. for (const AnimationNode::Input &E : inputs) {
  431. r_options->push_back(E.name.quote());
  432. }
  433. } else if (pf == "get_parameter" || pf == "set_parameter") {
  434. bool is_setter = pf == "set_parameter";
  435. List<PropertyInfo> parameters;
  436. get_parameter_list(&parameters);
  437. for (const PropertyInfo &E : parameters) {
  438. if (is_setter && is_parameter_read_only(E.name)) {
  439. continue;
  440. }
  441. r_options->push_back(E.name.quote());
  442. }
  443. } else if (pf == "set_filter_path" || pf == "is_path_filtered") {
  444. for (const KeyValue<NodePath, bool> &E : filter) {
  445. r_options->push_back(String(E.key).quote());
  446. }
  447. }
  448. }
  449. Resource::get_argument_options(p_function, p_idx, r_options);
  450. }
  451. #endif
  452. void AnimationNode::_bind_methods() {
  453. ClassDB::bind_method(D_METHOD("add_input", "name"), &AnimationNode::add_input);
  454. ClassDB::bind_method(D_METHOD("remove_input", "index"), &AnimationNode::remove_input);
  455. ClassDB::bind_method(D_METHOD("set_input_name", "input", "name"), &AnimationNode::set_input_name);
  456. ClassDB::bind_method(D_METHOD("get_input_name", "input"), &AnimationNode::get_input_name);
  457. ClassDB::bind_method(D_METHOD("get_input_count"), &AnimationNode::get_input_count);
  458. ClassDB::bind_method(D_METHOD("find_input", "name"), &AnimationNode::find_input);
  459. ClassDB::bind_method(D_METHOD("set_filter_path", "path", "enable"), &AnimationNode::set_filter_path);
  460. ClassDB::bind_method(D_METHOD("is_path_filtered", "path"), &AnimationNode::is_path_filtered);
  461. ClassDB::bind_method(D_METHOD("set_filter_enabled", "enable"), &AnimationNode::set_filter_enabled);
  462. ClassDB::bind_method(D_METHOD("is_filter_enabled"), &AnimationNode::is_filter_enabled);
  463. ClassDB::bind_method(D_METHOD("_set_filters", "filters"), &AnimationNode::_set_filters);
  464. ClassDB::bind_method(D_METHOD("_get_filters"), &AnimationNode::_get_filters);
  465. ClassDB::bind_method(D_METHOD("blend_animation", "animation", "time", "delta", "seeked", "is_external_seeking", "blend", "looped_flag"), &AnimationNode::blend_animation_ex, DEFVAL(Animation::LOOPED_FLAG_NONE));
  466. ClassDB::bind_method(D_METHOD("blend_node", "name", "node", "time", "seek", "is_external_seeking", "blend", "filter", "sync", "test_only"), &AnimationNode::blend_node_ex, DEFVAL(FILTER_IGNORE), DEFVAL(true), DEFVAL(false));
  467. ClassDB::bind_method(D_METHOD("blend_input", "input_index", "time", "seek", "is_external_seeking", "blend", "filter", "sync", "test_only"), &AnimationNode::blend_input_ex, DEFVAL(FILTER_IGNORE), DEFVAL(true), DEFVAL(false));
  468. ClassDB::bind_method(D_METHOD("set_parameter", "name", "value"), &AnimationNode::set_parameter);
  469. ClassDB::bind_method(D_METHOD("get_parameter", "name"), &AnimationNode::get_parameter);
  470. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_filter_enabled", "is_filter_enabled");
  471. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "filters", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_filters", "_get_filters");
  472. GDVIRTUAL_BIND(_get_child_nodes);
  473. GDVIRTUAL_BIND(_get_parameter_list);
  474. GDVIRTUAL_BIND(_get_child_by_name, "name");
  475. GDVIRTUAL_BIND(_get_parameter_default_value, "parameter");
  476. GDVIRTUAL_BIND(_is_parameter_read_only, "parameter");
  477. GDVIRTUAL_BIND(_process, "time", "seek", "is_external_seeking", "test_only");
  478. GDVIRTUAL_BIND(_get_caption);
  479. GDVIRTUAL_BIND(_has_filter);
  480. ADD_SIGNAL(MethodInfo("tree_changed"));
  481. ADD_SIGNAL(MethodInfo("animation_node_renamed", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "old_name"), PropertyInfo(Variant::STRING, "new_name")));
  482. ADD_SIGNAL(MethodInfo("animation_node_removed", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "name")));
  483. BIND_ENUM_CONSTANT(FILTER_IGNORE);
  484. BIND_ENUM_CONSTANT(FILTER_PASS);
  485. BIND_ENUM_CONSTANT(FILTER_STOP);
  486. BIND_ENUM_CONSTANT(FILTER_BLEND);
  487. }
  488. AnimationNode::AnimationNode() {
  489. }
  490. ////////////////////
  491. void AnimationRootNode::_tree_changed() {
  492. emit_signal(SNAME("tree_changed"));
  493. }
  494. void AnimationRootNode::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  495. emit_signal(SNAME("animation_node_renamed"), p_oid, p_old_name, p_new_name);
  496. }
  497. void AnimationRootNode::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  498. emit_signal(SNAME("animation_node_removed"), p_oid, p_node);
  499. }
  500. ////////////////////
  501. void AnimationTree::set_root_animation_node(const Ref<AnimationRootNode> &p_animation_node) {
  502. if (root_animation_node.is_valid()) {
  503. root_animation_node->disconnect(SNAME("tree_changed"), callable_mp(this, &AnimationTree::_tree_changed));
  504. root_animation_node->disconnect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationTree::_animation_node_renamed));
  505. root_animation_node->disconnect(SNAME("animation_node_removed"), callable_mp(this, &AnimationTree::_animation_node_removed));
  506. }
  507. root_animation_node = p_animation_node;
  508. if (root_animation_node.is_valid()) {
  509. root_animation_node->connect(SNAME("tree_changed"), callable_mp(this, &AnimationTree::_tree_changed));
  510. root_animation_node->connect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationTree::_animation_node_renamed));
  511. root_animation_node->connect(SNAME("animation_node_removed"), callable_mp(this, &AnimationTree::_animation_node_removed));
  512. }
  513. properties_dirty = true;
  514. update_configuration_warnings();
  515. }
  516. Ref<AnimationRootNode> AnimationTree::get_root_animation_node() const {
  517. return root_animation_node;
  518. }
  519. bool AnimationTree::_blend_pre_process(double p_delta, int p_track_count, const AHashMap<NodePath, int> &p_track_map) {
  520. _update_properties(); // If properties need updating, update them.
  521. if (!root_animation_node.is_valid()) {
  522. return false;
  523. }
  524. { // Setup.
  525. process_pass++;
  526. // Init process state.
  527. process_state = AnimationNode::ProcessState();
  528. process_state.tree = this;
  529. process_state.valid = true;
  530. process_state.invalid_reasons = "";
  531. process_state.last_pass = process_pass;
  532. process_state.track_map = &p_track_map;
  533. // Init node state for root AnimationNode.
  534. root_animation_node->node_state.track_weights.resize(p_track_count);
  535. real_t *src_blendsw = root_animation_node->node_state.track_weights.ptrw();
  536. for (int i = 0; i < p_track_count; i++) {
  537. src_blendsw[i] = 1.0; // By default all go to 1 for the root input.
  538. }
  539. root_animation_node->set_node_state_base_path(SNAME(Animation::PARAMETERS_BASE_PATH.ascii().get_data()));
  540. root_animation_node->node_state.parent = nullptr;
  541. }
  542. // Process.
  543. {
  544. PlaybackInfo pi;
  545. if (started) {
  546. // If started, seek.
  547. pi.seeked = true;
  548. pi.delta = p_delta;
  549. root_animation_node->_pre_process(&process_state, pi, false);
  550. started = false;
  551. } else {
  552. pi.seeked = false;
  553. pi.delta = p_delta;
  554. root_animation_node->_pre_process(&process_state, pi, false);
  555. }
  556. }
  557. if (!process_state.valid) {
  558. return false; // State is not valid, abort process.
  559. }
  560. return true;
  561. }
  562. void AnimationTree::_set_active(bool p_active) {
  563. _set_process(p_active);
  564. started = p_active;
  565. }
  566. void AnimationTree::set_advance_expression_base_node(const NodePath &p_path) {
  567. advance_expression_base_node = p_path;
  568. }
  569. NodePath AnimationTree::get_advance_expression_base_node() const {
  570. return advance_expression_base_node;
  571. }
  572. bool AnimationTree::is_state_invalid() const {
  573. return !process_state.valid;
  574. }
  575. String AnimationTree::get_invalid_state_reason() const {
  576. return process_state.invalid_reasons;
  577. }
  578. uint64_t AnimationTree::get_last_process_pass() const {
  579. return process_pass;
  580. }
  581. PackedStringArray AnimationTree::get_configuration_warnings() const {
  582. PackedStringArray warnings = AnimationMixer::get_configuration_warnings();
  583. if (!root_animation_node.is_valid()) {
  584. warnings.push_back(RTR("No root AnimationNode for the graph is set."));
  585. }
  586. return warnings;
  587. }
  588. void AnimationTree::_tree_changed() {
  589. if (properties_dirty) {
  590. return;
  591. }
  592. callable_mp(this, &AnimationTree::_update_properties).call_deferred();
  593. properties_dirty = true;
  594. }
  595. void AnimationTree::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  596. ERR_FAIL_COND(!property_reference_map.has(p_oid));
  597. String base_path = property_reference_map[p_oid];
  598. String old_base = base_path + p_old_name;
  599. String new_base = base_path + p_new_name;
  600. for (const PropertyInfo &E : properties) {
  601. if (E.name.begins_with(old_base)) {
  602. String new_name = E.name.replace_first(old_base, new_base);
  603. property_map[new_name] = property_map[E.name];
  604. property_map.erase(E.name);
  605. }
  606. }
  607. // Update tree second.
  608. properties_dirty = true;
  609. _update_properties();
  610. }
  611. void AnimationTree::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  612. ERR_FAIL_COND(!property_reference_map.has(p_oid));
  613. String base_path = String(property_reference_map[p_oid]) + String(p_node);
  614. for (const PropertyInfo &E : properties) {
  615. if (E.name.begins_with(base_path)) {
  616. property_map.erase(E.name);
  617. }
  618. }
  619. // Update tree second.
  620. properties_dirty = true;
  621. _update_properties();
  622. }
  623. void AnimationTree::_update_properties_for_node(const String &p_base_path, Ref<AnimationNode> p_node) {
  624. ERR_FAIL_COND(p_node.is_null());
  625. if (!property_parent_map.has(p_base_path)) {
  626. property_parent_map[p_base_path] = AHashMap<StringName, StringName>();
  627. }
  628. if (!property_reference_map.has(p_node->get_instance_id())) {
  629. property_reference_map[p_node->get_instance_id()] = p_base_path;
  630. }
  631. if (p_node->get_input_count() && !input_activity_map.has(p_base_path)) {
  632. Vector<Activity> activity;
  633. for (int i = 0; i < p_node->get_input_count(); i++) {
  634. Activity a;
  635. a.activity = 0;
  636. a.last_pass = 0;
  637. activity.push_back(a);
  638. }
  639. input_activity_map[p_base_path] = activity;
  640. input_activity_map_get[String(p_base_path).substr(0, String(p_base_path).length() - 1)] = &input_activity_map[p_base_path];
  641. }
  642. List<PropertyInfo> plist;
  643. p_node->get_parameter_list(&plist);
  644. for (PropertyInfo &pinfo : plist) {
  645. StringName key = pinfo.name;
  646. if (!property_map.has(p_base_path + key)) {
  647. Pair<Variant, bool> param;
  648. param.first = p_node->get_parameter_default_value(key);
  649. param.second = p_node->is_parameter_read_only(key);
  650. property_map[p_base_path + key] = param;
  651. }
  652. property_parent_map[p_base_path][key] = p_base_path + key;
  653. pinfo.name = p_base_path + key;
  654. properties.push_back(pinfo);
  655. }
  656. p_node->make_cache_dirty();
  657. List<AnimationNode::ChildNode> children;
  658. p_node->get_child_nodes(&children);
  659. for (const AnimationNode::ChildNode &E : children) {
  660. _update_properties_for_node(p_base_path + E.name + "/", E.node);
  661. }
  662. }
  663. void AnimationTree::_update_properties() {
  664. if (!properties_dirty) {
  665. return;
  666. }
  667. properties.clear();
  668. property_reference_map.clear();
  669. property_parent_map.clear();
  670. input_activity_map.clear();
  671. input_activity_map_get.clear();
  672. if (root_animation_node.is_valid()) {
  673. _update_properties_for_node(Animation::PARAMETERS_BASE_PATH, root_animation_node);
  674. }
  675. properties_dirty = false;
  676. notify_property_list_changed();
  677. }
  678. void AnimationTree::_notification(int p_what) {
  679. switch (p_what) {
  680. case NOTIFICATION_ENTER_TREE: {
  681. _setup_animation_player();
  682. if (active) {
  683. _set_process(true);
  684. }
  685. } break;
  686. }
  687. }
  688. void AnimationTree::set_animation_player(const NodePath &p_path) {
  689. animation_player = p_path;
  690. if (p_path.is_empty()) {
  691. set_root_node(SceneStringName(path_pp));
  692. while (animation_libraries.size()) {
  693. remove_animation_library(animation_libraries[0].name);
  694. }
  695. }
  696. emit_signal(SNAME("animation_player_changed")); // Needs to unpin AnimationPlayerEditor.
  697. _setup_animation_player();
  698. notify_property_list_changed();
  699. }
  700. NodePath AnimationTree::get_animation_player() const {
  701. return animation_player;
  702. }
  703. void AnimationTree::_setup_animation_player() {
  704. if (!is_inside_tree()) {
  705. return;
  706. }
  707. cache_valid = false;
  708. if (animation_player.is_empty()) {
  709. clear_caches();
  710. return;
  711. }
  712. // Using AnimationPlayer here is for compatibility. Changing to AnimationMixer needs extra work like error handling.
  713. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node_or_null(animation_player));
  714. if (player) {
  715. if (!player->is_connected(SNAME("caches_cleared"), callable_mp(this, &AnimationTree::_setup_animation_player))) {
  716. player->connect(SNAME("caches_cleared"), callable_mp(this, &AnimationTree::_setup_animation_player), CONNECT_DEFERRED);
  717. }
  718. if (!player->is_connected(SNAME("animation_list_changed"), callable_mp(this, &AnimationTree::_setup_animation_player))) {
  719. player->connect(SNAME("animation_list_changed"), callable_mp(this, &AnimationTree::_setup_animation_player), CONNECT_DEFERRED);
  720. }
  721. Node *root = player->get_node_or_null(player->get_root_node());
  722. if (root) {
  723. set_root_node(get_path_to(root, true));
  724. }
  725. while (animation_libraries.size()) {
  726. remove_animation_library(animation_libraries[0].name);
  727. }
  728. List<StringName> list;
  729. player->get_animation_library_list(&list);
  730. for (const StringName &E : list) {
  731. Ref<AnimationLibrary> lib = player->get_animation_library(E);
  732. if (lib.is_valid()) {
  733. add_animation_library(E, lib);
  734. }
  735. }
  736. }
  737. clear_caches();
  738. }
  739. void AnimationTree::_validate_property(PropertyInfo &p_property) const {
  740. AnimationMixer::_validate_property(p_property);
  741. if (!animation_player.is_empty()) {
  742. if (p_property.name == "root_node" || p_property.name.begins_with("libraries")) {
  743. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  744. }
  745. if (p_property.name.begins_with("libraries")) {
  746. p_property.usage &= ~PROPERTY_USAGE_STORAGE;
  747. }
  748. }
  749. }
  750. bool AnimationTree::_set(const StringName &p_name, const Variant &p_value) {
  751. #ifndef DISABLE_DEPRECATED
  752. String name = p_name;
  753. if (name == "process_callback") {
  754. set_callback_mode_process(static_cast<AnimationCallbackModeProcess>((int)p_value));
  755. return true;
  756. }
  757. #endif // DISABLE_DEPRECATED
  758. if (properties_dirty) {
  759. _update_properties();
  760. }
  761. if (property_map.has(p_name)) {
  762. if (is_inside_tree() && property_map[p_name].second) {
  763. return false; // Prevent to set property by user.
  764. }
  765. property_map[p_name].first = p_value;
  766. return true;
  767. }
  768. return false;
  769. }
  770. bool AnimationTree::_get(const StringName &p_name, Variant &r_ret) const {
  771. #ifndef DISABLE_DEPRECATED
  772. if (p_name == "process_callback") {
  773. r_ret = get_callback_mode_process();
  774. return true;
  775. }
  776. #endif // DISABLE_DEPRECATED
  777. if (properties_dirty) {
  778. const_cast<AnimationTree *>(this)->_update_properties();
  779. }
  780. if (property_map.has(p_name)) {
  781. r_ret = property_map[p_name].first;
  782. return true;
  783. }
  784. return false;
  785. }
  786. void AnimationTree::_get_property_list(List<PropertyInfo> *p_list) const {
  787. if (properties_dirty) {
  788. const_cast<AnimationTree *>(this)->_update_properties();
  789. }
  790. for (const PropertyInfo &E : properties) {
  791. p_list->push_back(E);
  792. }
  793. }
  794. real_t AnimationTree::get_connection_activity(const StringName &p_path, int p_connection) const {
  795. if (!input_activity_map_get.has(p_path)) {
  796. return 0;
  797. }
  798. const Vector<Activity> *activity = input_activity_map_get[p_path];
  799. if (!activity || p_connection < 0 || p_connection >= activity->size()) {
  800. return 0;
  801. }
  802. if ((*activity)[p_connection].last_pass != process_pass) {
  803. return 0;
  804. }
  805. return (*activity)[p_connection].activity;
  806. }
  807. void AnimationTree::_bind_methods() {
  808. ClassDB::bind_method(D_METHOD("set_tree_root", "animation_node"), &AnimationTree::set_root_animation_node);
  809. ClassDB::bind_method(D_METHOD("get_tree_root"), &AnimationTree::get_root_animation_node);
  810. ClassDB::bind_method(D_METHOD("set_advance_expression_base_node", "path"), &AnimationTree::set_advance_expression_base_node);
  811. ClassDB::bind_method(D_METHOD("get_advance_expression_base_node"), &AnimationTree::get_advance_expression_base_node);
  812. ClassDB::bind_method(D_METHOD("set_animation_player", "path"), &AnimationTree::set_animation_player);
  813. ClassDB::bind_method(D_METHOD("get_animation_player"), &AnimationTree::get_animation_player);
  814. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tree_root", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode"), "set_tree_root", "get_tree_root");
  815. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "advance_expression_base_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node"), "set_advance_expression_base_node", "get_advance_expression_base_node");
  816. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "anim_player", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationPlayer"), "set_animation_player", "get_animation_player");
  817. ADD_SIGNAL(MethodInfo(SNAME("animation_player_changed")));
  818. }
  819. AnimationTree::AnimationTree() {
  820. deterministic = true;
  821. callback_mode_discrete = ANIMATION_CALLBACK_MODE_DISCRETE_FORCE_CONTINUOUS;
  822. }
  823. AnimationTree::~AnimationTree() {
  824. }