animation_blend_space_2d.cpp 22 KB

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