animation_blend_space_1d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
  130. for (int i = p_point; i < blend_points_used - 1; i++) {
  131. blend_points[i] = blend_points[i + 1];
  132. }
  133. blend_points_used--;
  134. emit_signal("tree_changed");
  135. }
  136. int AnimationNodeBlendSpace1D::get_blend_point_count() const {
  137. return blend_points_used;
  138. }
  139. void AnimationNodeBlendSpace1D::set_min_space(float p_min) {
  140. min_space = p_min;
  141. if (min_space >= max_space) {
  142. min_space = max_space - 1;
  143. }
  144. }
  145. float AnimationNodeBlendSpace1D::get_min_space() const {
  146. return min_space;
  147. }
  148. void AnimationNodeBlendSpace1D::set_max_space(float p_max) {
  149. max_space = p_max;
  150. if (max_space <= min_space) {
  151. max_space = min_space + 1;
  152. }
  153. }
  154. float AnimationNodeBlendSpace1D::get_max_space() const {
  155. return max_space;
  156. }
  157. void AnimationNodeBlendSpace1D::set_snap(float p_snap) {
  158. snap = p_snap;
  159. }
  160. float AnimationNodeBlendSpace1D::get_snap() const {
  161. return snap;
  162. }
  163. void AnimationNodeBlendSpace1D::set_value_label(const String &p_label) {
  164. value_label = p_label;
  165. }
  166. String AnimationNodeBlendSpace1D::get_value_label() const {
  167. return value_label;
  168. }
  169. void AnimationNodeBlendSpace1D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
  170. if (p_index == blend_points_used) {
  171. add_blend_point(p_node, 0);
  172. } else {
  173. set_blend_point_node(p_index, p_node);
  174. }
  175. }
  176. float AnimationNodeBlendSpace1D::process(float p_time, bool p_seek) {
  177. if (blend_points_used == 0) {
  178. return 0.0;
  179. }
  180. if (blend_points_used == 1) {
  181. // only one point available, just play that animation
  182. return blend_node(blend_points[0].name, blend_points[0].node, p_time, p_seek, 1.0, FILTER_IGNORE, false);
  183. }
  184. float blend_pos = get_parameter(blend_position);
  185. float weights[MAX_BLEND_POINTS] = {};
  186. int point_lower = -1;
  187. float pos_lower = 0.0;
  188. int point_higher = -1;
  189. float pos_higher = 0.0;
  190. // find the closest two points to blend between
  191. for (int i = 0; i < blend_points_used; i++) {
  192. float pos = blend_points[i].position;
  193. if (pos <= blend_pos) {
  194. if (point_lower == -1) {
  195. point_lower = i;
  196. pos_lower = pos;
  197. } else if ((blend_pos - pos) < (blend_pos - pos_lower)) {
  198. point_lower = i;
  199. pos_lower = pos;
  200. }
  201. } else {
  202. if (point_higher == -1) {
  203. point_higher = i;
  204. pos_higher = pos;
  205. } else if ((pos - blend_pos) < (pos_higher - blend_pos)) {
  206. point_higher = i;
  207. pos_higher = pos;
  208. }
  209. }
  210. }
  211. // fill in weights
  212. if (point_lower == -1) {
  213. // we are on the left side, no other point to the left
  214. // we just play the next point.
  215. weights[point_higher] = 1.0;
  216. } else if (point_higher == -1) {
  217. // we are on the right side, no other point to the right
  218. // we just play the previous point
  219. weights[point_lower] = 1.0;
  220. } else {
  221. // we are between two points.
  222. // figure out weights, then blend the animations
  223. float distance_between_points = pos_higher - pos_lower;
  224. float current_pos_inbetween = blend_pos - pos_lower;
  225. float blend_percentage = current_pos_inbetween / distance_between_points;
  226. float blend_lower = 1.0 - blend_percentage;
  227. float blend_higher = blend_percentage;
  228. weights[point_lower] = blend_lower;
  229. weights[point_higher] = blend_higher;
  230. }
  231. // actually blend the animations now
  232. float max_time_remaining = 0.0;
  233. for (int i = 0; i < blend_points_used; i++) {
  234. float remaining = blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, weights[i], FILTER_IGNORE, false);
  235. max_time_remaining = MAX(max_time_remaining, remaining);
  236. }
  237. return max_time_remaining;
  238. }
  239. String AnimationNodeBlendSpace1D::get_caption() const {
  240. return "BlendSpace1D";
  241. }
  242. AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {
  243. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  244. blend_points[i].name = itos(i);
  245. }
  246. blend_points_used = 0;
  247. max_space = 1;
  248. min_space = -1;
  249. snap = 0.1;
  250. value_label = "value";
  251. blend_position = "blend_position";
  252. }
  253. AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {
  254. }