2
0

polygon_2d.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*************************************************************************/
  2. /* polygon_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 "polygon_2d.h"
  31. #include "core/math/geometry.h"
  32. #include "skeleton_2d.h"
  33. Dictionary Polygon2D::_edit_get_state() const {
  34. Dictionary state = Node2D::_edit_get_state();
  35. state["offset"] = offset;
  36. return state;
  37. }
  38. void Polygon2D::_edit_set_state(const Dictionary &p_state) {
  39. Node2D::_edit_set_state(p_state);
  40. set_offset(p_state["offset"]);
  41. }
  42. void Polygon2D::_edit_set_pivot(const Point2 &p_pivot) {
  43. set_position(get_transform().xform(p_pivot));
  44. set_offset(get_offset() - p_pivot);
  45. }
  46. Point2 Polygon2D::_edit_get_pivot() const {
  47. return Vector2();
  48. }
  49. bool Polygon2D::_edit_use_pivot() const {
  50. return true;
  51. }
  52. Rect2 Polygon2D::_edit_get_rect() const {
  53. if (rect_cache_dirty) {
  54. int l = polygon.size();
  55. PoolVector<Vector2>::Read r = polygon.read();
  56. item_rect = Rect2();
  57. for (int i = 0; i < l; i++) {
  58. Vector2 pos = r[i] + offset;
  59. if (i == 0)
  60. item_rect.position = pos;
  61. else
  62. item_rect.expand_to(pos);
  63. }
  64. rect_cache_dirty = false;
  65. }
  66. return item_rect;
  67. }
  68. bool Polygon2D::_edit_use_rect() const {
  69. return true;
  70. }
  71. bool Polygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  72. Vector<Vector2> polygon2d = Variant(polygon);
  73. if (internal_vertices > 0) {
  74. polygon2d.resize(polygon2d.size() - internal_vertices);
  75. }
  76. return Geometry::is_point_in_polygon(p_point - get_offset(), polygon2d);
  77. }
  78. void Polygon2D::_skeleton_bone_setup_changed() {
  79. update();
  80. }
  81. void Polygon2D::_notification(int p_what) {
  82. switch (p_what) {
  83. case NOTIFICATION_DRAW: {
  84. if (polygon.size() < 3)
  85. return;
  86. Skeleton2D *skeleton_node = NULL;
  87. if (has_node(skeleton)) {
  88. skeleton_node = Object::cast_to<Skeleton2D>(get_node(skeleton));
  89. }
  90. ObjectID new_skeleton_id = 0;
  91. if (skeleton_node) {
  92. VS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), skeleton_node->get_skeleton());
  93. new_skeleton_id = skeleton_node->get_instance_id();
  94. } else {
  95. VS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID());
  96. }
  97. if (new_skeleton_id != current_skeleton_id) {
  98. Object *old_skeleton = ObjectDB::get_instance(current_skeleton_id);
  99. if (old_skeleton) {
  100. old_skeleton->disconnect("bone_setup_changed", this, "_skeleton_bone_setup_changed");
  101. }
  102. if (skeleton_node) {
  103. skeleton_node->connect("bone_setup_changed", this, "_skeleton_bone_setup_changed");
  104. }
  105. current_skeleton_id = new_skeleton_id;
  106. }
  107. Vector<Vector2> points;
  108. Vector<Vector2> uvs;
  109. Vector<int> bones;
  110. Vector<float> weights;
  111. int len = polygon.size();
  112. if ((invert || polygons.size() == 0) && internal_vertices > 0) {
  113. //if no polygons are around, internal vertices must not be drawn, else let them be
  114. len -= internal_vertices;
  115. }
  116. if (len <= 0) {
  117. return;
  118. }
  119. points.resize(len);
  120. {
  121. PoolVector<Vector2>::Read polyr = polygon.read();
  122. for (int i = 0; i < len; i++) {
  123. points.write[i] = polyr[i] + offset;
  124. }
  125. }
  126. if (invert) {
  127. Rect2 bounds;
  128. int highest_idx = -1;
  129. float highest_y = -1e20;
  130. float sum = 0;
  131. for (int i = 0; i < len; i++) {
  132. if (i == 0)
  133. bounds.position = points[i];
  134. else
  135. bounds.expand_to(points[i]);
  136. if (points[i].y > highest_y) {
  137. highest_idx = i;
  138. highest_y = points[i].y;
  139. }
  140. int ni = (i + 1) % len;
  141. sum += (points[ni].x - points[i].x) * (points[ni].y + points[i].y);
  142. }
  143. bounds = bounds.grow(invert_border);
  144. Vector2 ep[7] = {
  145. Vector2(points[highest_idx].x, points[highest_idx].y + invert_border),
  146. Vector2(bounds.position + bounds.size),
  147. Vector2(bounds.position + Vector2(bounds.size.x, 0)),
  148. Vector2(bounds.position),
  149. Vector2(bounds.position + Vector2(0, bounds.size.y)),
  150. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y + invert_border),
  151. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y),
  152. };
  153. if (sum > 0) {
  154. SWAP(ep[1], ep[4]);
  155. SWAP(ep[2], ep[3]);
  156. SWAP(ep[5], ep[0]);
  157. SWAP(ep[6], points.write[highest_idx]);
  158. }
  159. points.resize(points.size() + 7);
  160. for (int i = points.size() - 1; i >= highest_idx + 7; i--) {
  161. points.write[i] = points[i - 7];
  162. }
  163. for (int i = 0; i < 7; i++) {
  164. points.write[highest_idx + i + 1] = ep[i];
  165. }
  166. len = points.size();
  167. }
  168. if (texture.is_valid()) {
  169. Transform2D texmat(tex_rot, tex_ofs);
  170. texmat.scale(tex_scale);
  171. Size2 tex_size = texture->get_size();
  172. uvs.resize(len);
  173. if (points.size() == uv.size()) {
  174. PoolVector<Vector2>::Read uvr = uv.read();
  175. for (int i = 0; i < len; i++) {
  176. uvs.write[i] = texmat.xform(uvr[i]) / tex_size;
  177. }
  178. } else {
  179. for (int i = 0; i < len; i++) {
  180. uvs.write[i] = texmat.xform(points[i]) / tex_size;
  181. }
  182. }
  183. }
  184. if (skeleton_node && !invert && bone_weights.size()) {
  185. //a skeleton is set! fill indices and weights
  186. int vc = len;
  187. bones.resize(vc * 4);
  188. weights.resize(vc * 4);
  189. int *bonesw = bones.ptrw();
  190. float *weightsw = weights.ptrw();
  191. for (int i = 0; i < vc * 4; i++) {
  192. bonesw[i] = 0;
  193. weightsw[i] = 0;
  194. }
  195. for (int i = 0; i < bone_weights.size(); i++) {
  196. if (bone_weights[i].weights.size() != points.size()) {
  197. continue; //different number of vertices, sorry not using.
  198. }
  199. if (!skeleton_node->has_node(bone_weights[i].path)) {
  200. continue; //node does not exist
  201. }
  202. Bone2D *bone = Object::cast_to<Bone2D>(skeleton_node->get_node(bone_weights[i].path));
  203. if (!bone) {
  204. continue;
  205. }
  206. int bone_index = bone->get_index_in_skeleton();
  207. PoolVector<float>::Read r = bone_weights[i].weights.read();
  208. for (int j = 0; j < vc; j++) {
  209. if (r[j] == 0.0)
  210. continue; //weight is unpainted, skip
  211. //find an index with a weight
  212. for (int k = 0; k < 4; k++) {
  213. if (weightsw[j * 4 + k] < r[j]) {
  214. //this is less than this weight, insert weight!
  215. for (int l = 3; l > k; l--) {
  216. weightsw[j * 4 + l] = weightsw[j * 4 + l - 1];
  217. bonesw[j * 4 + l] = bonesw[j * 4 + l - 1];
  218. }
  219. weightsw[j * 4 + k] = r[j];
  220. bonesw[j * 4 + k] = bone_index;
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. //normalize the weights
  227. for (int i = 0; i < vc; i++) {
  228. float tw = 0;
  229. for (int j = 0; j < 4; j++) {
  230. tw += weightsw[i * 4 + j];
  231. }
  232. if (tw == 0)
  233. continue; //unpainted, do nothing
  234. //normalize
  235. for (int j = 0; j < 4; j++) {
  236. weightsw[i * 4 + j] /= tw;
  237. }
  238. }
  239. }
  240. Vector<Color> colors;
  241. if (vertex_colors.size() == points.size()) {
  242. colors.resize(len);
  243. PoolVector<Color>::Read color_r = vertex_colors.read();
  244. for (int i = 0; i < len; i++) {
  245. colors.write[i] = color_r[i];
  246. }
  247. } else {
  248. colors.push_back(color);
  249. }
  250. // Vector<int> indices = Geometry::triangulate_polygon(points);
  251. // VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID());
  252. if (invert || polygons.size() == 0) {
  253. Vector<int> indices = Geometry::triangulate_polygon(points);
  254. if (indices.size()) {
  255. VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID());
  256. }
  257. } else {
  258. //draw individual polygons
  259. Vector<int> total_indices;
  260. for (int i = 0; i < polygons.size(); i++) {
  261. PoolVector<int> src_indices = polygons[i];
  262. int ic = src_indices.size();
  263. if (ic < 3)
  264. continue;
  265. PoolVector<int>::Read r = src_indices.read();
  266. Vector<Vector2> tmp_points;
  267. tmp_points.resize(ic);
  268. for (int j = 0; j < ic; j++) {
  269. int idx = r[j];
  270. ERR_CONTINUE(idx < 0 || idx >= points.size());
  271. tmp_points.write[j] = points[r[j]];
  272. }
  273. Vector<int> indices = Geometry::triangulate_polygon(tmp_points);
  274. int ic2 = indices.size();
  275. const int *r2 = indices.ptr();
  276. int bic = total_indices.size();
  277. total_indices.resize(bic + ic2);
  278. int *w2 = total_indices.ptrw();
  279. for (int j = 0; j < ic2; j++) {
  280. w2[j + bic] = r[r2[j]];
  281. }
  282. }
  283. if (total_indices.size()) {
  284. VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), total_indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID());
  285. }
  286. #if 0
  287. //use splits
  288. Vector<int> loop;
  289. int sc = splits.size();
  290. PoolVector<int>::Read r = splits.read();
  291. print_line("has splits, amount " + itos(splits.size()));
  292. Vector<Vector<int> > loops;
  293. // find a point that can be used to begin, must not be in a split, and have to the left and right the same one
  294. // like this one -> x---o
  295. // \ / \ .
  296. // o---o
  297. int base_point = -1;
  298. {
  299. int current_point = -1;
  300. int base_point_prev_split = -1;
  301. for (int i = 0; i < points.size(); i++) {
  302. //find if this point is in a split
  303. int split_index = -1;
  304. bool has_prev_split = false;
  305. int min_dist_to_end = 0x7FFFFFFF;
  306. for (int j = 0; j < sc; j += 2) {
  307. int split_pos = -1;
  308. int split_end = -1;
  309. if (r[j + 0] == i) { //found split in first point
  310. split_pos = r[j + 0];
  311. split_end = r[j + 1];
  312. } else if (r[j + 1] == i) { //found split in second point
  313. split_pos = r[j + 1];
  314. split_end = r[j + 0];
  315. }
  316. if (split_pos == split_end) {
  317. continue; //either nothing found or begin == end, this not a split in either case
  318. }
  319. if (j == base_point_prev_split) {
  320. has_prev_split = true;
  321. }
  322. //compute distance from split pos to split end in current traversal direction
  323. int dist_to_end = split_end > split_pos ? split_end - split_pos : (last - split_pos + split_end);
  324. if (dist_to_end < min_dist_to_end) {
  325. //always keep the valid split with the least distance to the loop
  326. min_dist_to_end = dist_to_end;
  327. split_index = j;
  328. }
  329. }
  330. if (split_index == -1) {
  331. current_point = i; //no split here, we are testing this point
  332. } else if (has_prev_split) {
  333. base_point = current_point; // there is a split and it contains the previous visited split, success
  334. break;
  335. } else {
  336. //invalidate current point and keep split
  337. current_point = -1;
  338. base_point_prev_split = split_index;
  339. }
  340. }
  341. }
  342. print_line("found base point: " + itos(base_point));
  343. if (base_point != -1) {
  344. int point = base_point;
  345. int last = base_point;
  346. //go through all the points, find splits
  347. do {
  348. int split;
  349. int last_dist_to_end = -1; //maximum valid distance to end
  350. do {
  351. loop.push_back(point); //push current point
  352. split = -1;
  353. int end = -1;
  354. int max_dist_to_end = 0;
  355. //find if this point is in a split
  356. for (int j = 0; j < sc; j += 2) {
  357. int split_pos = -1;
  358. int split_end = -1;
  359. if (r[j + 0] == point) { //match first split index
  360. split_pos = r[j + 0];
  361. split_end = r[j + 1];
  362. } else if (r[j + 1] == point) { //match second split index
  363. split_pos = r[j + 1];
  364. split_end = r[j + 0];
  365. }
  366. if (split_pos == split_end) {
  367. continue; //either nothing found or begin == end, this not a split in either case
  368. }
  369. //compute distance from split pos to split end
  370. int dist_to_end = split_end > split_pos ? split_end - split_pos : (points.size() - split_pos + split_end);
  371. if (last_dist_to_end != -1 && dist_to_end >= last_dist_to_end) {
  372. //distance must be shorter than in last iteration, means we've tested this before so ignore
  373. continue;
  374. } else if (dist_to_end > max_dist_to_end) {
  375. //always keep the valid point with the most distance (as long as it's valid)
  376. max_dist_to_end = dist_to_end;
  377. split = split_pos;
  378. end = split_end;
  379. }
  380. }
  381. if (split != -1) {
  382. //found a split!
  383. int from = end;
  384. //add points until last is reached
  385. while (true) {
  386. //find if point is in a split
  387. loop.push_back(from);
  388. if (from == last) {
  389. break;
  390. }
  391. from++;
  392. if (from >= points.size()) { //wrap if reached end
  393. from = 0;
  394. }
  395. if (from == loop[0]) {
  396. break; //end because we reached split source
  397. }
  398. }
  399. loops.push_back(loop); //done with this loop
  400. loop.clear();
  401. last_dist_to_end = max_dist_to_end;
  402. last = end; //algorithm can safely finish in this split point
  403. }
  404. } while (split != -1);
  405. } while (point != last);
  406. }
  407. if (loop.size() >=2 ) { //points remained
  408. //points remain
  409. loop.push_back(last); //no splits found, use last
  410. loops.push_back(loop);
  411. }
  412. print_line("total loops: " + itos(loops.size()));
  413. if (loops.size()) { //loops found
  414. Vector<int> indices;
  415. for (int i = 0; i < loops.size(); i++) {
  416. Vector<int> loop = loops[i];
  417. Vector<Vector2> vertices;
  418. vertices.resize(loop.size());
  419. for (int j = 0; j < vertices.size(); j++) {
  420. vertices.write[j] = points[loop[j]];
  421. }
  422. Vector<int> sub_indices = Geometry::triangulate_polygon(vertices);
  423. int from = indices.size();
  424. indices.resize(from + sub_indices.size());
  425. for (int j = 0; j < sub_indices.size(); j++) {
  426. indices.write[from + j] = loop[sub_indices[j]];
  427. }
  428. }
  429. VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID());
  430. }
  431. #endif
  432. }
  433. } break;
  434. }
  435. }
  436. void Polygon2D::set_polygon(const PoolVector<Vector2> &p_polygon) {
  437. polygon = p_polygon;
  438. rect_cache_dirty = true;
  439. update();
  440. }
  441. PoolVector<Vector2> Polygon2D::get_polygon() const {
  442. return polygon;
  443. }
  444. void Polygon2D::set_internal_vertex_count(int p_count) {
  445. internal_vertices = p_count;
  446. }
  447. int Polygon2D::get_internal_vertex_count() const {
  448. return internal_vertices;
  449. }
  450. void Polygon2D::set_uv(const PoolVector<Vector2> &p_uv) {
  451. uv = p_uv;
  452. update();
  453. }
  454. PoolVector<Vector2> Polygon2D::get_uv() const {
  455. return uv;
  456. }
  457. void Polygon2D::set_polygons(const Array &p_polygons) {
  458. polygons = p_polygons;
  459. update();
  460. }
  461. Array Polygon2D::get_polygons() const {
  462. return polygons;
  463. }
  464. void Polygon2D::set_color(const Color &p_color) {
  465. color = p_color;
  466. update();
  467. }
  468. Color Polygon2D::get_color() const {
  469. return color;
  470. }
  471. void Polygon2D::set_vertex_colors(const PoolVector<Color> &p_colors) {
  472. vertex_colors = p_colors;
  473. update();
  474. }
  475. PoolVector<Color> Polygon2D::get_vertex_colors() const {
  476. return vertex_colors;
  477. }
  478. void Polygon2D::set_texture(const Ref<Texture> &p_texture) {
  479. texture = p_texture;
  480. /*if (texture.is_valid()) {
  481. uint32_t flags=texture->get_flags();
  482. flags&=~Texture::FLAG_REPEAT;
  483. if (tex_tile)
  484. flags|=Texture::FLAG_REPEAT;
  485. texture->set_flags(flags);
  486. }*/
  487. update();
  488. }
  489. Ref<Texture> Polygon2D::get_texture() const {
  490. return texture;
  491. }
  492. void Polygon2D::set_texture_offset(const Vector2 &p_offset) {
  493. tex_ofs = p_offset;
  494. update();
  495. }
  496. Vector2 Polygon2D::get_texture_offset() const {
  497. return tex_ofs;
  498. }
  499. void Polygon2D::set_texture_rotation(float p_rot) {
  500. tex_rot = p_rot;
  501. update();
  502. }
  503. float Polygon2D::get_texture_rotation() const {
  504. return tex_rot;
  505. }
  506. void Polygon2D::set_texture_rotation_degrees(float p_rot) {
  507. set_texture_rotation(Math::deg2rad(p_rot));
  508. }
  509. float Polygon2D::get_texture_rotation_degrees() const {
  510. return Math::rad2deg(get_texture_rotation());
  511. }
  512. void Polygon2D::set_texture_scale(const Size2 &p_scale) {
  513. tex_scale = p_scale;
  514. update();
  515. }
  516. Size2 Polygon2D::get_texture_scale() const {
  517. return tex_scale;
  518. }
  519. void Polygon2D::set_invert(bool p_invert) {
  520. invert = p_invert;
  521. update();
  522. }
  523. bool Polygon2D::get_invert() const {
  524. return invert;
  525. }
  526. void Polygon2D::set_antialiased(bool p_antialiased) {
  527. antialiased = p_antialiased;
  528. update();
  529. }
  530. bool Polygon2D::get_antialiased() const {
  531. return antialiased;
  532. }
  533. void Polygon2D::set_invert_border(float p_invert_border) {
  534. invert_border = p_invert_border;
  535. update();
  536. }
  537. float Polygon2D::get_invert_border() const {
  538. return invert_border;
  539. }
  540. void Polygon2D::set_offset(const Vector2 &p_offset) {
  541. offset = p_offset;
  542. rect_cache_dirty = true;
  543. update();
  544. _change_notify("offset");
  545. }
  546. Vector2 Polygon2D::get_offset() const {
  547. return offset;
  548. }
  549. void Polygon2D::add_bone(const NodePath &p_path, const PoolVector<float> &p_weights) {
  550. Bone bone;
  551. bone.path = p_path;
  552. bone.weights = p_weights;
  553. bone_weights.push_back(bone);
  554. }
  555. int Polygon2D::get_bone_count() const {
  556. return bone_weights.size();
  557. }
  558. NodePath Polygon2D::get_bone_path(int p_index) const {
  559. ERR_FAIL_INDEX_V(p_index, bone_weights.size(), NodePath());
  560. return bone_weights[p_index].path;
  561. }
  562. PoolVector<float> Polygon2D::get_bone_weights(int p_index) const {
  563. ERR_FAIL_INDEX_V(p_index, bone_weights.size(), PoolVector<float>());
  564. return bone_weights[p_index].weights;
  565. }
  566. void Polygon2D::erase_bone(int p_idx) {
  567. ERR_FAIL_INDEX(p_idx, bone_weights.size());
  568. bone_weights.remove(p_idx);
  569. }
  570. void Polygon2D::clear_bones() {
  571. bone_weights.clear();
  572. }
  573. void Polygon2D::set_bone_weights(int p_index, const PoolVector<float> &p_weights) {
  574. ERR_FAIL_INDEX(p_index, bone_weights.size());
  575. bone_weights.write[p_index].weights = p_weights;
  576. update();
  577. }
  578. void Polygon2D::set_bone_path(int p_index, const NodePath &p_path) {
  579. ERR_FAIL_INDEX(p_index, bone_weights.size());
  580. bone_weights.write[p_index].path = p_path;
  581. update();
  582. }
  583. Array Polygon2D::_get_bones() const {
  584. Array bones;
  585. for (int i = 0; i < get_bone_count(); i++) {
  586. bones.push_back(get_bone_path(i));
  587. bones.push_back(get_bone_weights(i));
  588. }
  589. return bones;
  590. }
  591. void Polygon2D::_set_bones(const Array &p_bones) {
  592. ERR_FAIL_COND(p_bones.size() & 1);
  593. clear_bones();
  594. for (int i = 0; i < p_bones.size(); i += 2) {
  595. add_bone(p_bones[i], p_bones[i + 1]);
  596. }
  597. }
  598. void Polygon2D::set_skeleton(const NodePath &p_skeleton) {
  599. if (skeleton == p_skeleton)
  600. return;
  601. skeleton = p_skeleton;
  602. update();
  603. }
  604. NodePath Polygon2D::get_skeleton() const {
  605. return skeleton;
  606. }
  607. void Polygon2D::_bind_methods() {
  608. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &Polygon2D::set_polygon);
  609. ClassDB::bind_method(D_METHOD("get_polygon"), &Polygon2D::get_polygon);
  610. ClassDB::bind_method(D_METHOD("set_uv", "uv"), &Polygon2D::set_uv);
  611. ClassDB::bind_method(D_METHOD("get_uv"), &Polygon2D::get_uv);
  612. ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color);
  613. ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color);
  614. ClassDB::bind_method(D_METHOD("set_polygons", "polygons"), &Polygon2D::set_polygons);
  615. ClassDB::bind_method(D_METHOD("get_polygons"), &Polygon2D::get_polygons);
  616. ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors);
  617. ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors);
  618. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Polygon2D::set_texture);
  619. ClassDB::bind_method(D_METHOD("get_texture"), &Polygon2D::get_texture);
  620. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Polygon2D::set_texture_offset);
  621. ClassDB::bind_method(D_METHOD("get_texture_offset"), &Polygon2D::get_texture_offset);
  622. ClassDB::bind_method(D_METHOD("set_texture_rotation", "texture_rotation"), &Polygon2D::set_texture_rotation);
  623. ClassDB::bind_method(D_METHOD("get_texture_rotation"), &Polygon2D::get_texture_rotation);
  624. ClassDB::bind_method(D_METHOD("set_texture_rotation_degrees", "texture_rotation"), &Polygon2D::set_texture_rotation_degrees);
  625. ClassDB::bind_method(D_METHOD("get_texture_rotation_degrees"), &Polygon2D::get_texture_rotation_degrees);
  626. ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Polygon2D::set_texture_scale);
  627. ClassDB::bind_method(D_METHOD("get_texture_scale"), &Polygon2D::get_texture_scale);
  628. ClassDB::bind_method(D_METHOD("set_invert", "invert"), &Polygon2D::set_invert);
  629. ClassDB::bind_method(D_METHOD("get_invert"), &Polygon2D::get_invert);
  630. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &Polygon2D::set_antialiased);
  631. ClassDB::bind_method(D_METHOD("get_antialiased"), &Polygon2D::get_antialiased);
  632. ClassDB::bind_method(D_METHOD("set_invert_border", "invert_border"), &Polygon2D::set_invert_border);
  633. ClassDB::bind_method(D_METHOD("get_invert_border"), &Polygon2D::get_invert_border);
  634. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Polygon2D::set_offset);
  635. ClassDB::bind_method(D_METHOD("get_offset"), &Polygon2D::get_offset);
  636. ClassDB::bind_method(D_METHOD("add_bone", "path", "weights"), &Polygon2D::add_bone);
  637. ClassDB::bind_method(D_METHOD("get_bone_count"), &Polygon2D::get_bone_count);
  638. ClassDB::bind_method(D_METHOD("get_bone_path", "index"), &Polygon2D::get_bone_path);
  639. ClassDB::bind_method(D_METHOD("get_bone_weights", "index"), &Polygon2D::get_bone_weights);
  640. ClassDB::bind_method(D_METHOD("erase_bone", "index"), &Polygon2D::erase_bone);
  641. ClassDB::bind_method(D_METHOD("clear_bones"), &Polygon2D::clear_bones);
  642. ClassDB::bind_method(D_METHOD("set_bone_path", "index", "path"), &Polygon2D::set_bone_path);
  643. ClassDB::bind_method(D_METHOD("set_bone_weights", "index", "weights"), &Polygon2D::set_bone_weights);
  644. ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &Polygon2D::set_skeleton);
  645. ClassDB::bind_method(D_METHOD("get_skeleton"), &Polygon2D::get_skeleton);
  646. ClassDB::bind_method(D_METHOD("set_internal_vertex_count", "internal_vertex_count"), &Polygon2D::set_internal_vertex_count);
  647. ClassDB::bind_method(D_METHOD("get_internal_vertex_count"), &Polygon2D::get_internal_vertex_count);
  648. ClassDB::bind_method(D_METHOD("_set_bones", "bones"), &Polygon2D::_set_bones);
  649. ClassDB::bind_method(D_METHOD("_get_bones"), &Polygon2D::_get_bones);
  650. ClassDB::bind_method(D_METHOD("_skeleton_bone_setup_changed"), &Polygon2D::_skeleton_bone_setup_changed);
  651. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  652. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  653. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased");
  654. ADD_GROUP("Texture", "");
  655. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  656. ADD_GROUP("Texture", "texture_");
  657. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_offset"), "set_texture_offset", "get_texture_offset");
  658. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_scale"), "set_texture_scale", "get_texture_scale");
  659. ADD_PROPERTY(PropertyInfo(Variant::REAL, "texture_rotation_degrees", PROPERTY_HINT_RANGE, "-1080,1080,0.1,or_lesser,or_greater"), "set_texture_rotation_degrees", "get_texture_rotation_degrees");
  660. ADD_PROPERTY(PropertyInfo(Variant::REAL, "texture_rotation", PROPERTY_HINT_NONE, "", 0), "set_texture_rotation", "get_texture_rotation");
  661. ADD_GROUP("Skeleton", "");
  662. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton2D"), "set_skeleton", "get_skeleton");
  663. ADD_GROUP("Invert", "invert_");
  664. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_enable"), "set_invert", "get_invert");
  665. ADD_PROPERTY(PropertyInfo(Variant::REAL, "invert_border", PROPERTY_HINT_RANGE, "0.1,16384,0.1"), "set_invert_border", "get_invert_border");
  666. ADD_GROUP("Data", "");
  667. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  668. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv");
  669. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors");
  670. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons"), "set_polygons", "get_polygons");
  671. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_bones", "_get_bones");
  672. ADD_PROPERTY(PropertyInfo(Variant::INT, "internal_vertex_count", PROPERTY_HINT_RANGE, "0,1000"), "set_internal_vertex_count", "get_internal_vertex_count");
  673. }
  674. Polygon2D::Polygon2D() {
  675. invert = 0;
  676. invert_border = 100;
  677. antialiased = false;
  678. tex_rot = 0;
  679. tex_tile = true;
  680. tex_scale = Vector2(1, 1);
  681. color = Color(1, 1, 1);
  682. rect_cache_dirty = true;
  683. internal_vertices = 0;
  684. current_skeleton_id = 0;
  685. }