animation_blend_space_1d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /**************************************************************************/
  2. /* animation_blend_space_1d.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_blend_space_1d.h"
  31. #include "animation_blend_tree.h"
  32. void AnimationNodeBlendSpace1D::get_parameter_list(List<PropertyInfo> *r_list) const {
  33. AnimationNode::get_parameter_list(r_list);
  34. r_list->push_back(PropertyInfo(Variant::FLOAT, blend_position));
  35. r_list->push_back(PropertyInfo(Variant::INT, closest, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  36. }
  37. Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName &p_parameter) const {
  38. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  39. if (ret != Variant()) {
  40. return ret;
  41. }
  42. if (p_parameter == closest) {
  43. return -1;
  44. } else {
  45. return 0;
  46. }
  47. }
  48. Ref<AnimationNode> AnimationNodeBlendSpace1D::get_child_by_name(const StringName &p_name) const {
  49. return get_blend_point_node(p_name.operator String().to_int());
  50. }
  51. void AnimationNodeBlendSpace1D::_validate_property(PropertyInfo &p_property) const {
  52. if (p_property.name.begins_with("blend_point_")) {
  53. String left = p_property.name.get_slicec('/', 0);
  54. int idx = left.get_slicec('_', 2).to_int();
  55. if (idx >= blend_points_used) {
  56. p_property.usage = PROPERTY_USAGE_NONE;
  57. }
  58. }
  59. }
  60. void AnimationNodeBlendSpace1D::_tree_changed() {
  61. AnimationRootNode::_tree_changed();
  62. }
  63. void AnimationNodeBlendSpace1D::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  64. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  65. }
  66. void AnimationNodeBlendSpace1D::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  67. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  68. }
  69. void AnimationNodeBlendSpace1D::_bind_methods() {
  70. ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace1D::add_blend_point, DEFVAL(-1));
  71. ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace1D::set_blend_point_position);
  72. ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace1D::get_blend_point_position);
  73. ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace1D::set_blend_point_node);
  74. ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace1D::get_blend_point_node);
  75. ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace1D::remove_blend_point);
  76. ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace1D::get_blend_point_count);
  77. ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace1D::set_min_space);
  78. ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace1D::get_min_space);
  79. ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace1D::set_max_space);
  80. ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace1D::get_max_space);
  81. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace1D::set_snap);
  82. ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace1D::get_snap);
  83. ClassDB::bind_method(D_METHOD("set_value_label", "text"), &AnimationNodeBlendSpace1D::set_value_label);
  84. ClassDB::bind_method(D_METHOD("get_value_label"), &AnimationNodeBlendSpace1D::get_value_label);
  85. ClassDB::bind_method(D_METHOD("set_blend_mode", "mode"), &AnimationNodeBlendSpace1D::set_blend_mode);
  86. ClassDB::bind_method(D_METHOD("get_blend_mode"), &AnimationNodeBlendSpace1D::get_blend_mode);
  87. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeBlendSpace1D::set_use_sync);
  88. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeBlendSpace1D::is_using_sync);
  89. ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace1D::_add_blend_point);
  90. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  91. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point", "get_blend_point_node", i);
  92. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
  93. }
  94. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_min_space", "get_min_space");
  95. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_space", "get_max_space");
  96. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_snap", "get_snap");
  97. ADD_PROPERTY(PropertyInfo(Variant::STRING, "value_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_value_label", "get_value_label");
  98. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Interpolated,Discrete,Carry", PROPERTY_USAGE_NO_EDITOR), "set_blend_mode", "get_blend_mode");
  99. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_sync", "is_using_sync");
  100. BIND_ENUM_CONSTANT(BLEND_MODE_INTERPOLATED);
  101. BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE);
  102. BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE_CARRY);
  103. }
  104. void AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {
  105. for (int i = 0; i < blend_points_used; i++) {
  106. ChildNode cn;
  107. cn.name = itos(i);
  108. cn.node = blend_points[i].node;
  109. r_child_nodes->push_back(cn);
  110. }
  111. }
  112. void AnimationNodeBlendSpace1D::add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index) {
  113. ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
  114. ERR_FAIL_COND(p_node.is_null());
  115. ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);
  116. if (p_at_index == -1 || p_at_index == blend_points_used) {
  117. p_at_index = blend_points_used;
  118. } else {
  119. for (int i = blend_points_used - 1; i > p_at_index; i--) {
  120. blend_points[i] = blend_points[i - 1];
  121. }
  122. }
  123. blend_points[p_at_index].node = p_node;
  124. blend_points[p_at_index].position = p_position;
  125. blend_points[p_at_index].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), CONNECT_REFERENCE_COUNTED);
  126. blend_points[p_at_index].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  127. blend_points[p_at_index].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  128. blend_points_used++;
  129. emit_signal(SNAME("tree_changed"));
  130. }
  131. void AnimationNodeBlendSpace1D::set_blend_point_position(int p_point, float p_position) {
  132. ERR_FAIL_INDEX(p_point, blend_points_used);
  133. blend_points[p_point].position = p_position;
  134. }
  135. void AnimationNodeBlendSpace1D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
  136. ERR_FAIL_INDEX(p_point, blend_points_used);
  137. ERR_FAIL_COND(p_node.is_null());
  138. if (blend_points[p_point].node.is_valid()) {
  139. blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));
  140. blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed));
  141. blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed));
  142. }
  143. blend_points[p_point].node = p_node;
  144. blend_points[p_point].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), CONNECT_REFERENCE_COUNTED);
  145. blend_points[p_point].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  146. blend_points[p_point].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  147. emit_signal(SNAME("tree_changed"));
  148. }
  149. float AnimationNodeBlendSpace1D::get_blend_point_position(int p_point) const {
  150. ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, 0);
  151. return blend_points[p_point].position;
  152. }
  153. Ref<AnimationRootNode> AnimationNodeBlendSpace1D::get_blend_point_node(int p_point) const {
  154. ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, Ref<AnimationRootNode>());
  155. return blend_points[p_point].node;
  156. }
  157. void AnimationNodeBlendSpace1D::remove_blend_point(int p_point) {
  158. ERR_FAIL_INDEX(p_point, blend_points_used);
  159. ERR_FAIL_COND(blend_points[p_point].node.is_null());
  160. blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));
  161. blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed));
  162. blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed));
  163. for (int i = p_point; i < blend_points_used - 1; i++) {
  164. blend_points[i] = blend_points[i + 1];
  165. }
  166. blend_points_used--;
  167. emit_signal(SNAME("animation_node_removed"), get_instance_id(), itos(p_point));
  168. emit_signal(SNAME("tree_changed"));
  169. }
  170. int AnimationNodeBlendSpace1D::get_blend_point_count() const {
  171. return blend_points_used;
  172. }
  173. void AnimationNodeBlendSpace1D::set_min_space(float p_min) {
  174. min_space = p_min;
  175. if (min_space >= max_space) {
  176. min_space = max_space - 1;
  177. }
  178. }
  179. float AnimationNodeBlendSpace1D::get_min_space() const {
  180. return min_space;
  181. }
  182. void AnimationNodeBlendSpace1D::set_max_space(float p_max) {
  183. max_space = p_max;
  184. if (max_space <= min_space) {
  185. max_space = min_space + 1;
  186. }
  187. }
  188. float AnimationNodeBlendSpace1D::get_max_space() const {
  189. return max_space;
  190. }
  191. void AnimationNodeBlendSpace1D::set_snap(float p_snap) {
  192. snap = p_snap;
  193. }
  194. float AnimationNodeBlendSpace1D::get_snap() const {
  195. return snap;
  196. }
  197. void AnimationNodeBlendSpace1D::set_value_label(const String &p_label) {
  198. value_label = p_label;
  199. }
  200. String AnimationNodeBlendSpace1D::get_value_label() const {
  201. return value_label;
  202. }
  203. void AnimationNodeBlendSpace1D::set_blend_mode(BlendMode p_blend_mode) {
  204. blend_mode = p_blend_mode;
  205. }
  206. AnimationNodeBlendSpace1D::BlendMode AnimationNodeBlendSpace1D::get_blend_mode() const {
  207. return blend_mode;
  208. }
  209. void AnimationNodeBlendSpace1D::set_use_sync(bool p_sync) {
  210. sync = p_sync;
  211. }
  212. bool AnimationNodeBlendSpace1D::is_using_sync() const {
  213. return sync;
  214. }
  215. void AnimationNodeBlendSpace1D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
  216. if (p_index == blend_points_used) {
  217. add_blend_point(p_node, 0);
  218. } else {
  219. set_blend_point_node(p_index, p_node);
  220. }
  221. }
  222. AnimationNode::NodeTimeInfo AnimationNodeBlendSpace1D::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  223. if (!blend_points_used) {
  224. return NodeTimeInfo();
  225. }
  226. AnimationMixer::PlaybackInfo pi = p_playback_info;
  227. if (blend_points_used == 1) {
  228. // only one point available, just play that animation
  229. pi.weight = 1.0;
  230. return blend_node(blend_points[0].node, blend_points[0].name, pi, FILTER_IGNORE, true, p_test_only);
  231. }
  232. double blend_pos = get_parameter(blend_position);
  233. int cur_closest = get_parameter(closest);
  234. NodeTimeInfo mind;
  235. if (blend_mode == BLEND_MODE_INTERPOLATED) {
  236. int point_lower = -1;
  237. float pos_lower = 0.0;
  238. int point_higher = -1;
  239. float pos_higher = 0.0;
  240. // find the closest two points to blend between
  241. for (int i = 0; i < blend_points_used; i++) {
  242. float pos = blend_points[i].position;
  243. if (pos <= blend_pos) {
  244. if (point_lower == -1 || pos > pos_lower) {
  245. point_lower = i;
  246. pos_lower = pos;
  247. }
  248. } else if (point_higher == -1 || pos < pos_higher) {
  249. point_higher = i;
  250. pos_higher = pos;
  251. }
  252. }
  253. // fill in weights
  254. float weights[MAX_BLEND_POINTS] = {};
  255. if (point_lower == -1 && point_higher != -1) {
  256. // we are on the left side, no other point to the left
  257. // we just play the next point.
  258. weights[point_higher] = 1.0;
  259. } else if (point_higher == -1) {
  260. // we are on the right side, no other point to the right
  261. // we just play the previous point
  262. weights[point_lower] = 1.0;
  263. } else {
  264. // we are between two points.
  265. // figure out weights, then blend the animations
  266. float distance_between_points = pos_higher - pos_lower;
  267. float current_pos_inbetween = blend_pos - pos_lower;
  268. float blend_percentage = current_pos_inbetween / distance_between_points;
  269. float blend_lower = 1.0 - blend_percentage;
  270. float blend_higher = blend_percentage;
  271. weights[point_lower] = blend_lower;
  272. weights[point_higher] = blend_higher;
  273. }
  274. // actually blend the animations now
  275. bool first = true;
  276. double max_weight = 0.0;
  277. for (int i = 0; i < blend_points_used; i++) {
  278. if (i == point_lower || i == point_higher) {
  279. pi.weight = weights[i];
  280. NodeTimeInfo t = blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  281. if (first || pi.weight > max_weight) {
  282. max_weight = pi.weight;
  283. mind = t;
  284. first = false;
  285. }
  286. } else if (sync) {
  287. pi.weight = 0;
  288. blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  289. }
  290. }
  291. } else {
  292. int new_closest = -1;
  293. double new_closest_dist = 1e20;
  294. for (int i = 0; i < blend_points_used; i++) {
  295. double d = abs(blend_points[i].position - blend_pos);
  296. if (d < new_closest_dist) {
  297. new_closest = i;
  298. new_closest_dist = d;
  299. }
  300. }
  301. if (new_closest != cur_closest && new_closest != -1) {
  302. if (blend_mode == BLEND_MODE_DISCRETE_CARRY && cur_closest != -1) {
  303. NodeTimeInfo from;
  304. // For ping-pong loop.
  305. Ref<AnimationNodeAnimation> na_c = static_cast<Ref<AnimationNodeAnimation>>(blend_points[cur_closest].node);
  306. Ref<AnimationNodeAnimation> na_n = static_cast<Ref<AnimationNodeAnimation>>(blend_points[new_closest].node);
  307. if (na_c.is_valid() && na_n.is_valid()) {
  308. na_n->set_backward(na_c->is_backward());
  309. }
  310. // See how much animation remains.
  311. pi.seeked = false;
  312. pi.weight = 0;
  313. from = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, true);
  314. pi.time = from.position;
  315. }
  316. pi.seeked = true;
  317. pi.weight = 1.0;
  318. mind = blend_node(blend_points[new_closest].node, blend_points[new_closest].name, pi, FILTER_IGNORE, true, p_test_only);
  319. cur_closest = new_closest;
  320. } else {
  321. pi.weight = 1.0;
  322. mind = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, p_test_only);
  323. }
  324. if (sync) {
  325. pi = p_playback_info;
  326. pi.weight = 0;
  327. for (int i = 0; i < blend_points_used; i++) {
  328. if (i != cur_closest) {
  329. blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  330. }
  331. }
  332. }
  333. }
  334. set_parameter(closest, cur_closest);
  335. return mind;
  336. }
  337. String AnimationNodeBlendSpace1D::get_caption() const {
  338. return "BlendSpace1D";
  339. }
  340. AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {
  341. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  342. blend_points[i].name = itos(i);
  343. }
  344. }
  345. AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {
  346. }