2
0

animation_blend_space_2d.cpp 22 KB

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