2
0

animation_blend_space_2d.cpp 22 KB

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