animation_blend_space_2d.cpp 22 KB

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