animation_blend_space_1d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. void AnimationNodeBlendSpace1D::get_parameter_list(List<PropertyInfo> *r_list) const {
  32. r_list->push_back(PropertyInfo(Variant::REAL, blend_position));
  33. }
  34. Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName &p_parameter) const {
  35. return 0;
  36. }
  37. Ref<AnimationNode> AnimationNodeBlendSpace1D::get_child_by_name(const StringName &p_name) {
  38. return get_blend_point_node(p_name.operator String().to_int());
  39. }
  40. void AnimationNodeBlendSpace1D::_validate_property(PropertyInfo &property) const {
  41. if (property.name.begins_with("blend_point_")) {
  42. String left = property.name.get_slicec('/', 0);
  43. int idx = left.get_slicec('_', 2).to_int();
  44. if (idx >= blend_points_used) {
  45. property.usage = 0;
  46. }
  47. }
  48. AnimationRootNode::_validate_property(property);
  49. }
  50. void AnimationNodeBlendSpace1D::_tree_changed() {
  51. emit_signal("tree_changed");
  52. }
  53. void AnimationNodeBlendSpace1D::_bind_methods() {
  54. ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace1D::add_blend_point, DEFVAL(-1));
  55. ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace1D::set_blend_point_position);
  56. ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace1D::get_blend_point_position);
  57. ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace1D::set_blend_point_node);
  58. ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace1D::get_blend_point_node);
  59. ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace1D::remove_blend_point);
  60. ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace1D::get_blend_point_count);
  61. ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace1D::set_min_space);
  62. ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace1D::get_min_space);
  63. ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace1D::set_max_space);
  64. ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace1D::get_max_space);
  65. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace1D::set_snap);
  66. ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace1D::get_snap);
  67. ClassDB::bind_method(D_METHOD("set_value_label", "text"), &AnimationNodeBlendSpace1D::set_value_label);
  68. ClassDB::bind_method(D_METHOD("get_value_label"), &AnimationNodeBlendSpace1D::get_value_label);
  69. ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace1D::_add_blend_point);
  70. ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeBlendSpace1D::_tree_changed);
  71. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  72. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point", "get_blend_point_node", i);
  73. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
  74. }
  75. ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_min_space", "get_min_space");
  76. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_max_space", "get_max_space");
  77. ADD_PROPERTY(PropertyInfo(Variant::REAL, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_snap", "get_snap");
  78. ADD_PROPERTY(PropertyInfo(Variant::STRING, "value_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_value_label", "get_value_label");
  79. }
  80. void AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {
  81. for (int i = 0; i < blend_points_used; i++) {
  82. ChildNode cn;
  83. cn.name = itos(i);
  84. cn.node = blend_points[i].node;
  85. r_child_nodes->push_back(cn);
  86. }
  87. }
  88. void AnimationNodeBlendSpace1D::add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index) {
  89. ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
  90. ERR_FAIL_COND(p_node.is_null());
  91. ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);
  92. if (p_at_index == -1 || p_at_index == blend_points_used) {
  93. p_at_index = blend_points_used;
  94. } else {
  95. for (int i = blend_points_used - 1; i > p_at_index; i--) {
  96. blend_points[i] = blend_points[i - 1];
  97. }
  98. }
  99. blend_points[p_at_index].node = p_node;
  100. blend_points[p_at_index].position = p_position;
  101. blend_points[p_at_index].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
  102. blend_points_used++;
  103. emit_signal("tree_changed");
  104. }
  105. void AnimationNodeBlendSpace1D::set_blend_point_position(int p_point, float p_position) {
  106. ERR_FAIL_INDEX(p_point, blend_points_used);
  107. blend_points[p_point].position = p_position;
  108. }
  109. void AnimationNodeBlendSpace1D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
  110. ERR_FAIL_INDEX(p_point, blend_points_used);
  111. ERR_FAIL_COND(p_node.is_null());
  112. if (blend_points[p_point].node.is_valid()) {
  113. blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
  114. }
  115. blend_points[p_point].node = p_node;
  116. blend_points[p_point].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
  117. emit_signal("tree_changed");
  118. }
  119. float AnimationNodeBlendSpace1D::get_blend_point_position(int p_point) const {
  120. ERR_FAIL_INDEX_V(p_point, blend_points_used, 0);
  121. return blend_points[p_point].position;
  122. }
  123. Ref<AnimationRootNode> AnimationNodeBlendSpace1D::get_blend_point_node(int p_point) const {
  124. ERR_FAIL_INDEX_V(p_point, blend_points_used, Ref<AnimationRootNode>());
  125. return blend_points[p_point].node;
  126. }
  127. void AnimationNodeBlendSpace1D::remove_blend_point(int p_point) {
  128. ERR_FAIL_INDEX(p_point, blend_points_used);
  129. ERR_FAIL_COND(blend_points[p_point].node.is_null());
  130. blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
  131. for (int i = p_point; i < blend_points_used - 1; i++) {
  132. blend_points[i] = blend_points[i + 1];
  133. }
  134. blend_points_used--;
  135. emit_signal("tree_changed");
  136. }
  137. int AnimationNodeBlendSpace1D::get_blend_point_count() const {
  138. return blend_points_used;
  139. }
  140. void AnimationNodeBlendSpace1D::set_min_space(float p_min) {
  141. min_space = p_min;
  142. if (min_space >= max_space) {
  143. min_space = max_space - 1;
  144. }
  145. }
  146. float AnimationNodeBlendSpace1D::get_min_space() const {
  147. return min_space;
  148. }
  149. void AnimationNodeBlendSpace1D::set_max_space(float p_max) {
  150. max_space = p_max;
  151. if (max_space <= min_space) {
  152. max_space = min_space + 1;
  153. }
  154. }
  155. float AnimationNodeBlendSpace1D::get_max_space() const {
  156. return max_space;
  157. }
  158. void AnimationNodeBlendSpace1D::set_snap(float p_snap) {
  159. snap = p_snap;
  160. }
  161. float AnimationNodeBlendSpace1D::get_snap() const {
  162. return snap;
  163. }
  164. void AnimationNodeBlendSpace1D::set_value_label(const String &p_label) {
  165. value_label = p_label;
  166. }
  167. String AnimationNodeBlendSpace1D::get_value_label() const {
  168. return value_label;
  169. }
  170. void AnimationNodeBlendSpace1D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
  171. if (p_index == blend_points_used) {
  172. add_blend_point(p_node, 0);
  173. } else {
  174. set_blend_point_node(p_index, p_node);
  175. }
  176. }
  177. float AnimationNodeBlendSpace1D::process(float p_time, bool p_seek) {
  178. if (blend_points_used == 0) {
  179. return 0.0;
  180. }
  181. if (blend_points_used == 1) {
  182. // only one point available, just play that animation
  183. return blend_node(blend_points[0].name, blend_points[0].node, p_time, p_seek, 1.0, FILTER_IGNORE, false);
  184. }
  185. float blend_pos = get_parameter(blend_position);
  186. float weights[MAX_BLEND_POINTS] = {};
  187. int point_lower = -1;
  188. float pos_lower = 0.0;
  189. int point_higher = -1;
  190. float pos_higher = 0.0;
  191. // find the closest two points to blend between
  192. for (int i = 0; i < blend_points_used; i++) {
  193. float pos = blend_points[i].position;
  194. if (pos <= blend_pos) {
  195. if (point_lower == -1) {
  196. point_lower = i;
  197. pos_lower = pos;
  198. } else if ((blend_pos - pos) < (blend_pos - pos_lower)) {
  199. point_lower = i;
  200. pos_lower = pos;
  201. }
  202. } else {
  203. if (point_higher == -1) {
  204. point_higher = i;
  205. pos_higher = pos;
  206. } else if ((pos - blend_pos) < (pos_higher - blend_pos)) {
  207. point_higher = i;
  208. pos_higher = pos;
  209. }
  210. }
  211. }
  212. // fill in weights
  213. if (point_lower == -1 && point_higher != -1) {
  214. // we are on the left side, no other point to the left
  215. // we just play the next point.
  216. weights[point_higher] = 1.0;
  217. } else if (point_higher == -1) {
  218. // we are on the right side, no other point to the right
  219. // we just play the previous point
  220. weights[point_lower] = 1.0;
  221. } else {
  222. // we are between two points.
  223. // figure out weights, then blend the animations
  224. float distance_between_points = pos_higher - pos_lower;
  225. float current_pos_inbetween = blend_pos - pos_lower;
  226. float blend_percentage = current_pos_inbetween / distance_between_points;
  227. float blend_lower = 1.0 - blend_percentage;
  228. float blend_higher = blend_percentage;
  229. weights[point_lower] = blend_lower;
  230. weights[point_higher] = blend_higher;
  231. }
  232. // actually blend the animations now
  233. float max_time_remaining = 0.0;
  234. for (int i = 0; i < blend_points_used; i++) {
  235. float remaining = blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, weights[i], FILTER_IGNORE, false);
  236. max_time_remaining = MAX(max_time_remaining, remaining);
  237. }
  238. return max_time_remaining;
  239. }
  240. String AnimationNodeBlendSpace1D::get_caption() const {
  241. return "BlendSpace1D";
  242. }
  243. AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {
  244. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  245. blend_points[i].name = itos(i);
  246. }
  247. blend_points_used = 0;
  248. max_space = 1;
  249. min_space = -1;
  250. snap = 0.1;
  251. value_label = "value";
  252. blend_position = "blend_position";
  253. }
  254. AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {
  255. }