animation_blend_space_2d.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /**************************************************************************/
  2. /* animation_blend_space_2d.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_2d.h"
  31. #include "animation_blend_tree.h"
  32. #include "core/math/geometry_2d.h"
  33. void AnimationNodeBlendSpace2D::get_parameter_list(List<PropertyInfo> *r_list) const {
  34. AnimationNode::get_parameter_list(r_list);
  35. r_list->push_back(PropertyInfo(Variant::VECTOR2, blend_position));
  36. r_list->push_back(PropertyInfo(Variant::INT, closest, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  37. }
  38. Variant AnimationNodeBlendSpace2D::get_parameter_default_value(const StringName &p_parameter) const {
  39. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  40. if (ret != Variant()) {
  41. return ret;
  42. }
  43. if (p_parameter == closest) {
  44. return -1;
  45. } else {
  46. return Vector2();
  47. }
  48. }
  49. void AnimationNodeBlendSpace2D::get_child_nodes(List<ChildNode> *r_child_nodes) {
  50. for (int i = 0; i < blend_points_used; i++) {
  51. ChildNode cn;
  52. cn.name = itos(i);
  53. cn.node = blend_points[i].node;
  54. r_child_nodes->push_back(cn);
  55. }
  56. }
  57. void AnimationNodeBlendSpace2D::add_blend_point(const Ref<AnimationRootNode> &p_node, const Vector2 &p_position, int p_at_index) {
  58. ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
  59. ERR_FAIL_COND(p_node.is_null());
  60. ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);
  61. if (p_at_index == -1 || p_at_index == blend_points_used) {
  62. p_at_index = blend_points_used;
  63. } else {
  64. for (int i = blend_points_used - 1; i > p_at_index; i--) {
  65. blend_points[i] = blend_points[i - 1];
  66. }
  67. for (int i = 0; i < triangles.size(); i++) {
  68. for (int j = 0; j < 3; j++) {
  69. if (triangles[i].points[j] >= p_at_index) {
  70. triangles.write[i].points[j]++;
  71. }
  72. }
  73. }
  74. }
  75. blend_points[p_at_index].node = p_node;
  76. blend_points[p_at_index].position = p_position;
  77. blend_points[p_at_index].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace2D::_tree_changed), CONNECT_REFERENCE_COUNTED);
  78. blend_points[p_at_index].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  79. blend_points[p_at_index].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  80. blend_points_used++;
  81. _queue_auto_triangles();
  82. emit_signal(SNAME("tree_changed"));
  83. }
  84. void AnimationNodeBlendSpace2D::set_blend_point_position(int p_point, const Vector2 &p_position) {
  85. ERR_FAIL_INDEX(p_point, blend_points_used);
  86. blend_points[p_point].position = p_position;
  87. _queue_auto_triangles();
  88. }
  89. void AnimationNodeBlendSpace2D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
  90. ERR_FAIL_INDEX(p_point, blend_points_used);
  91. ERR_FAIL_COND(p_node.is_null());
  92. if (blend_points[p_point].node.is_valid()) {
  93. blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace2D::_tree_changed));
  94. blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_renamed));
  95. blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_removed));
  96. }
  97. blend_points[p_point].node = p_node;
  98. blend_points[p_point].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace2D::_tree_changed), CONNECT_REFERENCE_COUNTED);
  99. blend_points[p_point].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  100. blend_points[p_point].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  101. emit_signal(SNAME("tree_changed"));
  102. }
  103. Vector2 AnimationNodeBlendSpace2D::get_blend_point_position(int p_point) const {
  104. ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, Vector2());
  105. return blend_points[p_point].position;
  106. }
  107. Ref<AnimationRootNode> AnimationNodeBlendSpace2D::get_blend_point_node(int p_point) const {
  108. ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, Ref<AnimationRootNode>());
  109. return blend_points[p_point].node;
  110. }
  111. void AnimationNodeBlendSpace2D::remove_blend_point(int p_point) {
  112. ERR_FAIL_INDEX(p_point, blend_points_used);
  113. ERR_FAIL_COND(blend_points[p_point].node.is_null());
  114. blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace2D::_tree_changed));
  115. blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_renamed));
  116. blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_removed));
  117. for (int i = 0; i < triangles.size(); i++) {
  118. bool erase = false;
  119. for (int j = 0; j < 3; j++) {
  120. if (triangles[i].points[j] == p_point) {
  121. erase = true;
  122. break;
  123. } else if (triangles[i].points[j] > p_point) {
  124. triangles.write[i].points[j]--;
  125. }
  126. }
  127. if (erase) {
  128. triangles.remove_at(i);
  129. i--;
  130. }
  131. }
  132. for (int i = p_point; i < blend_points_used - 1; i++) {
  133. blend_points[i] = blend_points[i + 1];
  134. }
  135. blend_points_used--;
  136. emit_signal(SNAME("animation_node_removed"), get_instance_id(), itos(p_point));
  137. emit_signal(SNAME("tree_changed"));
  138. }
  139. int AnimationNodeBlendSpace2D::get_blend_point_count() const {
  140. return blend_points_used;
  141. }
  142. bool AnimationNodeBlendSpace2D::has_triangle(int p_x, int p_y, int p_z) const {
  143. ERR_FAIL_INDEX_V(p_x, blend_points_used, false);
  144. ERR_FAIL_INDEX_V(p_y, blend_points_used, false);
  145. ERR_FAIL_INDEX_V(p_z, blend_points_used, false);
  146. BlendTriangle t;
  147. t.points[0] = p_x;
  148. t.points[1] = p_y;
  149. t.points[2] = p_z;
  150. SortArray<int> sort;
  151. sort.sort(t.points, 3);
  152. for (int i = 0; i < triangles.size(); i++) {
  153. bool all_equal = true;
  154. for (int j = 0; j < 3; j++) {
  155. if (triangles[i].points[j] != t.points[j]) {
  156. all_equal = false;
  157. break;
  158. }
  159. }
  160. if (all_equal) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. void AnimationNodeBlendSpace2D::add_triangle(int p_x, int p_y, int p_z, int p_at_index) {
  167. ERR_FAIL_INDEX(p_x, blend_points_used);
  168. ERR_FAIL_INDEX(p_y, blend_points_used);
  169. ERR_FAIL_INDEX(p_z, blend_points_used);
  170. _update_triangles();
  171. BlendTriangle t;
  172. t.points[0] = p_x;
  173. t.points[1] = p_y;
  174. t.points[2] = p_z;
  175. SortArray<int> sort;
  176. sort.sort(t.points, 3);
  177. for (int i = 0; i < triangles.size(); i++) {
  178. bool all_equal = true;
  179. for (int j = 0; j < 3; j++) {
  180. if (triangles[i].points[j] != t.points[j]) {
  181. all_equal = false;
  182. break;
  183. }
  184. }
  185. ERR_FAIL_COND(all_equal);
  186. }
  187. if (p_at_index == -1 || p_at_index == triangles.size()) {
  188. triangles.push_back(t);
  189. } else {
  190. triangles.insert(p_at_index, t);
  191. }
  192. }
  193. int AnimationNodeBlendSpace2D::get_triangle_point(int p_triangle, int p_point) {
  194. _update_triangles();
  195. ERR_FAIL_INDEX_V(p_point, 3, -1);
  196. ERR_FAIL_INDEX_V(p_triangle, triangles.size(), -1);
  197. return triangles[p_triangle].points[p_point];
  198. }
  199. void AnimationNodeBlendSpace2D::remove_triangle(int p_triangle) {
  200. ERR_FAIL_INDEX(p_triangle, triangles.size());
  201. triangles.remove_at(p_triangle);
  202. }
  203. int AnimationNodeBlendSpace2D::get_triangle_count() const {
  204. return triangles.size();
  205. }
  206. void AnimationNodeBlendSpace2D::set_min_space(const Vector2 &p_min) {
  207. min_space = p_min;
  208. if (min_space.x >= max_space.x) {
  209. min_space.x = max_space.x - 1;
  210. }
  211. if (min_space.y >= max_space.y) {
  212. min_space.y = max_space.y - 1;
  213. }
  214. }
  215. Vector2 AnimationNodeBlendSpace2D::get_min_space() const {
  216. return min_space;
  217. }
  218. void AnimationNodeBlendSpace2D::set_max_space(const Vector2 &p_max) {
  219. max_space = p_max;
  220. if (max_space.x <= min_space.x) {
  221. max_space.x = min_space.x + 1;
  222. }
  223. if (max_space.y <= min_space.y) {
  224. max_space.y = min_space.y + 1;
  225. }
  226. }
  227. Vector2 AnimationNodeBlendSpace2D::get_max_space() const {
  228. return max_space;
  229. }
  230. void AnimationNodeBlendSpace2D::set_snap(const Vector2 &p_snap) {
  231. snap = p_snap;
  232. }
  233. Vector2 AnimationNodeBlendSpace2D::get_snap() const {
  234. return snap;
  235. }
  236. void AnimationNodeBlendSpace2D::set_x_label(const String &p_label) {
  237. x_label = p_label;
  238. }
  239. String AnimationNodeBlendSpace2D::get_x_label() const {
  240. return x_label;
  241. }
  242. void AnimationNodeBlendSpace2D::set_y_label(const String &p_label) {
  243. y_label = p_label;
  244. }
  245. String AnimationNodeBlendSpace2D::get_y_label() const {
  246. return y_label;
  247. }
  248. void AnimationNodeBlendSpace2D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
  249. if (p_index == blend_points_used) {
  250. add_blend_point(p_node, Vector2());
  251. } else {
  252. set_blend_point_node(p_index, p_node);
  253. }
  254. }
  255. void AnimationNodeBlendSpace2D::_set_triangles(const Vector<int> &p_triangles) {
  256. if (auto_triangles) {
  257. return;
  258. }
  259. ERR_FAIL_COND(p_triangles.size() % 3 != 0);
  260. for (int i = 0; i < p_triangles.size(); i += 3) {
  261. add_triangle(p_triangles[i + 0], p_triangles[i + 1], p_triangles[i + 2]);
  262. }
  263. }
  264. Vector<int> AnimationNodeBlendSpace2D::_get_triangles() const {
  265. Vector<int> t;
  266. if (auto_triangles && trianges_dirty) {
  267. return t;
  268. }
  269. t.resize(triangles.size() * 3);
  270. for (int i = 0; i < triangles.size(); i++) {
  271. t.write[i * 3 + 0] = triangles[i].points[0];
  272. t.write[i * 3 + 1] = triangles[i].points[1];
  273. t.write[i * 3 + 2] = triangles[i].points[2];
  274. }
  275. return t;
  276. }
  277. void AnimationNodeBlendSpace2D::_queue_auto_triangles() {
  278. if (!auto_triangles || trianges_dirty) {
  279. return;
  280. }
  281. trianges_dirty = true;
  282. callable_mp(this, &AnimationNodeBlendSpace2D::_update_triangles).call_deferred();
  283. }
  284. void AnimationNodeBlendSpace2D::_update_triangles() {
  285. if (!auto_triangles || !trianges_dirty) {
  286. return;
  287. }
  288. trianges_dirty = false;
  289. triangles.clear();
  290. if (blend_points_used < 3) {
  291. emit_signal(SNAME("triangles_updated"));
  292. return;
  293. }
  294. Vector<Vector2> points;
  295. points.resize(blend_points_used);
  296. for (int i = 0; i < blend_points_used; i++) {
  297. points.write[i] = blend_points[i].position;
  298. }
  299. Vector<Delaunay2D::Triangle> tr = Delaunay2D::triangulate(points);
  300. for (int i = 0; i < tr.size(); i++) {
  301. add_triangle(tr[i].points[0], tr[i].points[1], tr[i].points[2]);
  302. }
  303. emit_signal(SNAME("triangles_updated"));
  304. }
  305. Vector2 AnimationNodeBlendSpace2D::get_closest_point(const Vector2 &p_point) {
  306. _update_triangles();
  307. if (triangles.size() == 0) {
  308. return Vector2();
  309. }
  310. Vector2 best_point;
  311. bool first = true;
  312. for (int i = 0; i < triangles.size(); i++) {
  313. Vector2 points[3];
  314. for (int j = 0; j < 3; j++) {
  315. points[j] = get_blend_point_position(get_triangle_point(i, j));
  316. }
  317. if (Geometry2D::is_point_in_triangle(p_point, points[0], points[1], points[2])) {
  318. return p_point;
  319. }
  320. for (int j = 0; j < 3; j++) {
  321. Vector2 s[2] = {
  322. points[j],
  323. points[(j + 1) % 3]
  324. };
  325. Vector2 closest_point = Geometry2D::get_closest_point_to_segment(p_point, s);
  326. if (first || closest_point.distance_to(p_point) < best_point.distance_to(p_point)) {
  327. best_point = closest_point;
  328. first = false;
  329. }
  330. }
  331. }
  332. return best_point;
  333. }
  334. void AnimationNodeBlendSpace2D::_blend_triangle(const Vector2 &p_pos, const Vector2 *p_points, float *r_weights) {
  335. if (p_pos.is_equal_approx(p_points[0])) {
  336. r_weights[0] = 1;
  337. r_weights[1] = 0;
  338. r_weights[2] = 0;
  339. return;
  340. }
  341. if (p_pos.is_equal_approx(p_points[1])) {
  342. r_weights[0] = 0;
  343. r_weights[1] = 1;
  344. r_weights[2] = 0;
  345. return;
  346. }
  347. if (p_pos.is_equal_approx(p_points[2])) {
  348. r_weights[0] = 0;
  349. r_weights[1] = 0;
  350. r_weights[2] = 1;
  351. return;
  352. }
  353. Vector2 v0 = p_points[1] - p_points[0];
  354. Vector2 v1 = p_points[2] - p_points[0];
  355. Vector2 v2 = p_pos - p_points[0];
  356. float d00 = v0.dot(v0);
  357. float d01 = v0.dot(v1);
  358. float d11 = v1.dot(v1);
  359. float d20 = v2.dot(v0);
  360. float d21 = v2.dot(v1);
  361. float denom = (d00 * d11 - d01 * d01);
  362. if (denom == 0) {
  363. r_weights[0] = 1;
  364. r_weights[1] = 0;
  365. r_weights[2] = 0;
  366. return;
  367. }
  368. float v = (d11 * d20 - d01 * d21) / denom;
  369. float w = (d00 * d21 - d01 * d20) / denom;
  370. float u = 1.0f - v - w;
  371. r_weights[0] = u;
  372. r_weights[1] = v;
  373. r_weights[2] = w;
  374. }
  375. AnimationNode::NodeTimeInfo AnimationNodeBlendSpace2D::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  376. _update_triangles();
  377. if (!blend_points_used) {
  378. return NodeTimeInfo();
  379. }
  380. Vector2 blend_pos = get_parameter(blend_position);
  381. int cur_closest = get_parameter(closest);
  382. NodeTimeInfo mind; //time of min distance point
  383. AnimationMixer::PlaybackInfo pi = p_playback_info;
  384. if (blend_mode == BLEND_MODE_INTERPOLATED) {
  385. if (triangles.is_empty()) {
  386. return NodeTimeInfo();
  387. }
  388. Vector2 best_point;
  389. bool first = true;
  390. int blend_triangle = -1;
  391. float blend_weights[3] = { 0, 0, 0 };
  392. for (int i = 0; i < triangles.size(); i++) {
  393. Vector2 points[3];
  394. for (int j = 0; j < 3; j++) {
  395. points[j] = get_blend_point_position(get_triangle_point(i, j));
  396. }
  397. if (Geometry2D::is_point_in_triangle(blend_pos, points[0], points[1], points[2])) {
  398. blend_triangle = i;
  399. _blend_triangle(blend_pos, points, blend_weights);
  400. break;
  401. }
  402. for (int j = 0; j < 3; j++) {
  403. Vector2 s[2] = {
  404. points[j],
  405. points[(j + 1) % 3]
  406. };
  407. Vector2 closest2 = Geometry2D::get_closest_point_to_segment(blend_pos, s);
  408. if (first || closest2.distance_to(blend_pos) < best_point.distance_to(blend_pos)) {
  409. best_point = closest2;
  410. blend_triangle = i;
  411. first = false;
  412. float d = s[0].distance_to(s[1]);
  413. if (d == 0.0) {
  414. blend_weights[j] = 1.0;
  415. blend_weights[(j + 1) % 3] = 0.0;
  416. blend_weights[(j + 2) % 3] = 0.0;
  417. } else {
  418. float c = s[0].distance_to(closest2) / d;
  419. blend_weights[j] = 1.0 - c;
  420. blend_weights[(j + 1) % 3] = c;
  421. blend_weights[(j + 2) % 3] = 0.0;
  422. }
  423. }
  424. }
  425. }
  426. ERR_FAIL_COND_V(blend_triangle == -1, NodeTimeInfo()); //should never reach here
  427. int triangle_points[3];
  428. for (int j = 0; j < 3; j++) {
  429. triangle_points[j] = get_triangle_point(blend_triangle, j);
  430. }
  431. first = true;
  432. double max_weight = 0.0;
  433. for (int i = 0; i < blend_points_used; i++) {
  434. bool found = false;
  435. for (int j = 0; j < 3; j++) {
  436. if (i == triangle_points[j]) {
  437. //blend with the given weight
  438. pi.weight = blend_weights[j];
  439. NodeTimeInfo t = blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  440. if (first || pi.weight > max_weight) {
  441. mind = t;
  442. max_weight = pi.weight;
  443. first = false;
  444. }
  445. found = true;
  446. break;
  447. }
  448. }
  449. if (sync && !found) {
  450. pi.weight = 0;
  451. blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  452. }
  453. }
  454. } else {
  455. int new_closest = -1;
  456. float new_closest_dist = 1e20;
  457. for (int i = 0; i < blend_points_used; i++) {
  458. float d = blend_points[i].position.distance_squared_to(blend_pos);
  459. if (d < new_closest_dist) {
  460. new_closest = i;
  461. new_closest_dist = d;
  462. }
  463. }
  464. if (new_closest != cur_closest && new_closest != -1) {
  465. NodeTimeInfo from;
  466. if (blend_mode == BLEND_MODE_DISCRETE_CARRY && cur_closest != -1) {
  467. //for ping-pong loop
  468. Ref<AnimationNodeAnimation> na_c = static_cast<Ref<AnimationNodeAnimation>>(blend_points[cur_closest].node);
  469. Ref<AnimationNodeAnimation> na_n = static_cast<Ref<AnimationNodeAnimation>>(blend_points[new_closest].node);
  470. if (!na_c.is_null() && !na_n.is_null()) {
  471. na_n->set_backward(na_c->is_backward());
  472. }
  473. //see how much animation remains
  474. pi.seeked = false;
  475. pi.weight = 0;
  476. from = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, p_test_only);
  477. }
  478. pi.time = from.position;
  479. pi.seeked = true;
  480. pi.weight = 1.0;
  481. mind = blend_node(blend_points[new_closest].node, blend_points[new_closest].name, pi, FILTER_IGNORE, true, p_test_only);
  482. cur_closest = new_closest;
  483. } else {
  484. pi.weight = 1.0;
  485. mind = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, p_test_only);
  486. }
  487. if (sync) {
  488. pi = p_playback_info;
  489. pi.weight = 0;
  490. for (int i = 0; i < blend_points_used; i++) {
  491. if (i != cur_closest) {
  492. blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  493. }
  494. }
  495. }
  496. }
  497. set_parameter(closest, cur_closest);
  498. return mind;
  499. }
  500. String AnimationNodeBlendSpace2D::get_caption() const {
  501. return "BlendSpace2D";
  502. }
  503. void AnimationNodeBlendSpace2D::_validate_property(PropertyInfo &p_property) const {
  504. if (auto_triangles && p_property.name == "triangles") {
  505. p_property.usage = PROPERTY_USAGE_NONE;
  506. }
  507. if (p_property.name.begins_with("blend_point_")) {
  508. String left = p_property.name.get_slicec('/', 0);
  509. int idx = left.get_slicec('_', 2).to_int();
  510. if (idx >= blend_points_used) {
  511. p_property.usage = PROPERTY_USAGE_NONE;
  512. }
  513. }
  514. }
  515. void AnimationNodeBlendSpace2D::set_auto_triangles(bool p_enable) {
  516. if (auto_triangles == p_enable) {
  517. return;
  518. }
  519. auto_triangles = p_enable;
  520. _queue_auto_triangles();
  521. }
  522. bool AnimationNodeBlendSpace2D::get_auto_triangles() const {
  523. return auto_triangles;
  524. }
  525. Ref<AnimationNode> AnimationNodeBlendSpace2D::get_child_by_name(const StringName &p_name) const {
  526. return get_blend_point_node(p_name.operator String().to_int());
  527. }
  528. void AnimationNodeBlendSpace2D::set_blend_mode(BlendMode p_blend_mode) {
  529. blend_mode = p_blend_mode;
  530. }
  531. AnimationNodeBlendSpace2D::BlendMode AnimationNodeBlendSpace2D::get_blend_mode() const {
  532. return blend_mode;
  533. }
  534. void AnimationNodeBlendSpace2D::set_use_sync(bool p_sync) {
  535. sync = p_sync;
  536. }
  537. bool AnimationNodeBlendSpace2D::is_using_sync() const {
  538. return sync;
  539. }
  540. void AnimationNodeBlendSpace2D::_tree_changed() {
  541. AnimationRootNode::_tree_changed();
  542. }
  543. void AnimationNodeBlendSpace2D::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  544. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  545. }
  546. void AnimationNodeBlendSpace2D::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  547. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  548. }
  549. void AnimationNodeBlendSpace2D::_bind_methods() {
  550. ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace2D::add_blend_point, DEFVAL(-1));
  551. ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace2D::set_blend_point_position);
  552. ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace2D::get_blend_point_position);
  553. ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace2D::set_blend_point_node);
  554. ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace2D::get_blend_point_node);
  555. ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace2D::remove_blend_point);
  556. ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace2D::get_blend_point_count);
  557. ClassDB::bind_method(D_METHOD("add_triangle", "x", "y", "z", "at_index"), &AnimationNodeBlendSpace2D::add_triangle, DEFVAL(-1));
  558. ClassDB::bind_method(D_METHOD("get_triangle_point", "triangle", "point"), &AnimationNodeBlendSpace2D::get_triangle_point);
  559. ClassDB::bind_method(D_METHOD("remove_triangle", "triangle"), &AnimationNodeBlendSpace2D::remove_triangle);
  560. ClassDB::bind_method(D_METHOD("get_triangle_count"), &AnimationNodeBlendSpace2D::get_triangle_count);
  561. ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace2D::set_min_space);
  562. ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace2D::get_min_space);
  563. ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace2D::set_max_space);
  564. ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace2D::get_max_space);
  565. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace2D::set_snap);
  566. ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace2D::get_snap);
  567. ClassDB::bind_method(D_METHOD("set_x_label", "text"), &AnimationNodeBlendSpace2D::set_x_label);
  568. ClassDB::bind_method(D_METHOD("get_x_label"), &AnimationNodeBlendSpace2D::get_x_label);
  569. ClassDB::bind_method(D_METHOD("set_y_label", "text"), &AnimationNodeBlendSpace2D::set_y_label);
  570. ClassDB::bind_method(D_METHOD("get_y_label"), &AnimationNodeBlendSpace2D::get_y_label);
  571. ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace2D::_add_blend_point);
  572. ClassDB::bind_method(D_METHOD("_set_triangles", "triangles"), &AnimationNodeBlendSpace2D::_set_triangles);
  573. ClassDB::bind_method(D_METHOD("_get_triangles"), &AnimationNodeBlendSpace2D::_get_triangles);
  574. ClassDB::bind_method(D_METHOD("set_auto_triangles", "enable"), &AnimationNodeBlendSpace2D::set_auto_triangles);
  575. ClassDB::bind_method(D_METHOD("get_auto_triangles"), &AnimationNodeBlendSpace2D::get_auto_triangles);
  576. ClassDB::bind_method(D_METHOD("set_blend_mode", "mode"), &AnimationNodeBlendSpace2D::set_blend_mode);
  577. ClassDB::bind_method(D_METHOD("get_blend_mode"), &AnimationNodeBlendSpace2D::get_blend_mode);
  578. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeBlendSpace2D::set_use_sync);
  579. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeBlendSpace2D::is_using_sync);
  580. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_triangles", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_auto_triangles", "get_auto_triangles");
  581. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  582. 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);
  583. ADD_PROPERTYI(PropertyInfo(Variant::VECTOR2, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
  584. }
  585. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "triangles", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_triangles", "_get_triangles");
  586. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_min_space", "get_min_space");
  587. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_space", "get_max_space");
  588. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_snap", "get_snap");
  589. ADD_PROPERTY(PropertyInfo(Variant::STRING, "x_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_x_label", "get_x_label");
  590. ADD_PROPERTY(PropertyInfo(Variant::STRING, "y_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_y_label", "get_y_label");
  591. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Interpolated,Discrete,Carry", PROPERTY_USAGE_NO_EDITOR), "set_blend_mode", "get_blend_mode");
  592. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_sync", "is_using_sync");
  593. ADD_SIGNAL(MethodInfo("triangles_updated"));
  594. BIND_ENUM_CONSTANT(BLEND_MODE_INTERPOLATED);
  595. BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE);
  596. BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE_CARRY);
  597. }
  598. AnimationNodeBlendSpace2D::AnimationNodeBlendSpace2D() {
  599. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  600. blend_points[i].name = itos(i);
  601. }
  602. }
  603. AnimationNodeBlendSpace2D::~AnimationNodeBlendSpace2D() {
  604. }