polygon_2d.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /**************************************************************************/
  2. /* polygon_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 "polygon_2d.h"
  31. #include "core/math/geometry.h"
  32. #include "skeleton_2d.h"
  33. #ifdef TOOLS_ENABLED
  34. Dictionary Polygon2D::_edit_get_state() const {
  35. Dictionary state = Node2D::_edit_get_state();
  36. state["offset"] = offset;
  37. return state;
  38. }
  39. void Polygon2D::_edit_set_state(const Dictionary &p_state) {
  40. Node2D::_edit_set_state(p_state);
  41. set_offset(p_state["offset"]);
  42. }
  43. void Polygon2D::_edit_set_pivot(const Point2 &p_pivot) {
  44. set_position(get_transform().xform(p_pivot));
  45. set_offset(get_offset() - p_pivot);
  46. }
  47. Point2 Polygon2D::_edit_get_pivot() const {
  48. return Vector2();
  49. }
  50. bool Polygon2D::_edit_use_pivot() const {
  51. return true;
  52. }
  53. Rect2 Polygon2D::_edit_get_rect() const {
  54. if (rect_cache_dirty) {
  55. int l = polygon.size();
  56. PoolVector<Vector2>::Read r = polygon.read();
  57. item_rect = Rect2();
  58. for (int i = 0; i < l; i++) {
  59. Vector2 pos = r[i] + offset;
  60. if (i == 0) {
  61. item_rect.position = pos;
  62. } else {
  63. item_rect.expand_to(pos);
  64. }
  65. }
  66. rect_cache_dirty = false;
  67. }
  68. return item_rect;
  69. }
  70. bool Polygon2D::_edit_use_rect() const {
  71. return polygon.size() > 0;
  72. }
  73. bool Polygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  74. Vector<Vector2> polygon2d = Variant(polygon);
  75. if (internal_vertices > 0) {
  76. polygon2d.resize(polygon2d.size() - internal_vertices);
  77. }
  78. return Geometry::is_point_in_polygon(p_point - get_offset(), polygon2d);
  79. }
  80. #endif
  81. void Polygon2D::_skeleton_bone_setup_changed() {
  82. update();
  83. }
  84. void Polygon2D::_notification(int p_what) {
  85. switch (p_what) {
  86. case NOTIFICATION_ENTER_TREE: {
  87. // Must re-establish any existing links with skeletons on re-entering the tree.
  88. update();
  89. } break;
  90. case NOTIFICATION_EXIT_TREE: {
  91. // Always detach skeleton when exiting the tree, so skeletons don't inform
  92. // Polygon2Ds outside the tree that they have moved (this would be useless work).
  93. VS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID());
  94. } break;
  95. case NOTIFICATION_DRAW: {
  96. if (polygon.size() < 3) {
  97. return;
  98. }
  99. Skeleton2D *skeleton_node = nullptr;
  100. if (has_node(skeleton)) {
  101. skeleton_node = Object::cast_to<Skeleton2D>(get_node(skeleton));
  102. }
  103. ObjectID new_skeleton_id = 0;
  104. if (skeleton_node) {
  105. VS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), skeleton_node->get_skeleton());
  106. new_skeleton_id = skeleton_node->get_instance_id();
  107. // Sync the offset transform between the Polygon2D and the skeleton.
  108. // This is needed for accurate culling in VisualServer.
  109. Transform2D global_xform_skel = skeleton_node->get_global_transform();
  110. Transform2D global_xform_poly = get_global_transform();
  111. // find the difference
  112. Transform2D global_xform_offset = global_xform_skel.affine_inverse() * global_xform_poly;
  113. VS::get_singleton()->canvas_item_set_skeleton_relative_xform(get_canvas_item(), global_xform_offset);
  114. } else {
  115. VS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID());
  116. }
  117. if (new_skeleton_id != current_skeleton_id) {
  118. Object *old_skeleton = ObjectDB::get_instance(current_skeleton_id);
  119. if (old_skeleton) {
  120. old_skeleton->disconnect("bone_setup_changed", this, "_skeleton_bone_setup_changed");
  121. }
  122. if (skeleton_node) {
  123. skeleton_node->connect("bone_setup_changed", this, "_skeleton_bone_setup_changed");
  124. }
  125. current_skeleton_id = new_skeleton_id;
  126. }
  127. Vector<Vector2> points;
  128. Vector<Vector2> uvs;
  129. Vector<int> bones;
  130. Vector<float> weights;
  131. int len = polygon.size();
  132. if ((invert || polygons.size() == 0) && internal_vertices > 0) {
  133. //if no polygons are around, internal vertices must not be drawn, else let them be
  134. len -= internal_vertices;
  135. }
  136. if (len <= 0) {
  137. return;
  138. }
  139. points.resize(len);
  140. {
  141. PoolVector<Vector2>::Read polyr = polygon.read();
  142. for (int i = 0; i < len; i++) {
  143. points.write[i] = polyr[i] + offset;
  144. }
  145. }
  146. if (invert) {
  147. Rect2 bounds;
  148. int highest_idx = -1;
  149. float highest_y = -1e20;
  150. float sum = 0;
  151. for (int i = 0; i < len; i++) {
  152. if (i == 0) {
  153. bounds.position = points[i];
  154. } else {
  155. bounds.expand_to(points[i]);
  156. }
  157. if (points[i].y > highest_y) {
  158. highest_idx = i;
  159. highest_y = points[i].y;
  160. }
  161. int ni = (i + 1) % len;
  162. sum += (points[ni].x - points[i].x) * (points[ni].y + points[i].y);
  163. }
  164. bounds = bounds.grow(invert_border);
  165. Vector2 ep[7] = {
  166. Vector2(points[highest_idx].x, points[highest_idx].y + invert_border),
  167. Vector2(bounds.position + bounds.size),
  168. Vector2(bounds.position + Vector2(bounds.size.x, 0)),
  169. Vector2(bounds.position),
  170. Vector2(bounds.position + Vector2(0, bounds.size.y)),
  171. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y + invert_border),
  172. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y),
  173. };
  174. if (sum > 0) {
  175. SWAP(ep[1], ep[4]);
  176. SWAP(ep[2], ep[3]);
  177. SWAP(ep[5], ep[0]);
  178. SWAP(ep[6], points.write[highest_idx]);
  179. }
  180. points.resize(points.size() + 7);
  181. for (int i = points.size() - 1; i >= highest_idx + 7; i--) {
  182. points.write[i] = points[i - 7];
  183. }
  184. for (int i = 0; i < 7; i++) {
  185. points.write[highest_idx + i + 1] = ep[i];
  186. }
  187. len = points.size();
  188. }
  189. if (texture.is_valid()) {
  190. Transform2D texmat(tex_rot, tex_ofs);
  191. texmat.scale(tex_scale);
  192. Size2 tex_size = texture->get_size();
  193. uvs.resize(len);
  194. if (points.size() == uv.size()) {
  195. PoolVector<Vector2>::Read uvr = uv.read();
  196. for (int i = 0; i < len; i++) {
  197. uvs.write[i] = texmat.xform(uvr[i]) / tex_size;
  198. }
  199. } else {
  200. for (int i = 0; i < len; i++) {
  201. uvs.write[i] = texmat.xform(points[i]) / tex_size;
  202. }
  203. }
  204. }
  205. if (skeleton_node && !invert && bone_weights.size()) {
  206. //a skeleton is set! fill indices and weights
  207. int vc = len;
  208. bones.resize(vc * 4);
  209. weights.resize(vc * 4);
  210. int *bonesw = bones.ptrw();
  211. float *weightsw = weights.ptrw();
  212. for (int i = 0; i < vc * 4; i++) {
  213. bonesw[i] = 0;
  214. weightsw[i] = 0;
  215. }
  216. for (int i = 0; i < bone_weights.size(); i++) {
  217. if (bone_weights[i].weights.size() != points.size()) {
  218. continue; //different number of vertices, sorry not using.
  219. }
  220. if (!skeleton_node->has_node(bone_weights[i].path)) {
  221. continue; //node does not exist
  222. }
  223. Bone2D *bone = Object::cast_to<Bone2D>(skeleton_node->get_node(bone_weights[i].path));
  224. if (!bone) {
  225. continue;
  226. }
  227. int bone_index = bone->get_index_in_skeleton();
  228. PoolVector<float>::Read r = bone_weights[i].weights.read();
  229. for (int j = 0; j < vc; j++) {
  230. if (r[j] == 0.0) {
  231. continue; //weight is unpainted, skip
  232. }
  233. //find an index with a weight
  234. for (int k = 0; k < 4; k++) {
  235. if (weightsw[j * 4 + k] < r[j]) {
  236. //this is less than this weight, insert weight!
  237. for (int l = 3; l > k; l--) {
  238. weightsw[j * 4 + l] = weightsw[j * 4 + l - 1];
  239. bonesw[j * 4 + l] = bonesw[j * 4 + l - 1];
  240. }
  241. weightsw[j * 4 + k] = r[j];
  242. bonesw[j * 4 + k] = bone_index;
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. //normalize the weights
  249. for (int i = 0; i < vc; i++) {
  250. float tw = 0;
  251. for (int j = 0; j < 4; j++) {
  252. tw += weightsw[i * 4 + j];
  253. }
  254. if (tw == 0) {
  255. continue; //unpainted, do nothing
  256. }
  257. //normalize
  258. for (int j = 0; j < 4; j++) {
  259. weightsw[i * 4 + j] /= tw;
  260. }
  261. }
  262. }
  263. Vector<Color> colors;
  264. if (vertex_colors.size() == points.size()) {
  265. colors.resize(len);
  266. PoolVector<Color>::Read color_r = vertex_colors.read();
  267. for (int i = 0; i < len; i++) {
  268. colors.write[i] = color_r[i];
  269. }
  270. } else {
  271. colors.push_back(color);
  272. }
  273. // Vector<int> indices = Geometry::triangulate_polygon(points);
  274. // VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID());
  275. if (invert || polygons.size() == 0) {
  276. Vector<int> indices = Geometry::triangulate_polygon(points);
  277. if (indices.size()) {
  278. VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID(), -1, RID(), antialiased);
  279. }
  280. } else {
  281. //draw individual polygons
  282. Vector<int> total_indices;
  283. for (int i = 0; i < polygons.size(); i++) {
  284. PoolVector<int> src_indices = polygons[i];
  285. int ic = src_indices.size();
  286. if (ic < 3) {
  287. continue;
  288. }
  289. PoolVector<int>::Read r = src_indices.read();
  290. Vector<Vector2> tmp_points;
  291. tmp_points.resize(ic);
  292. for (int j = 0; j < ic; j++) {
  293. int idx = r[j];
  294. ERR_CONTINUE(idx < 0 || idx >= points.size());
  295. tmp_points.write[j] = points[r[j]];
  296. }
  297. Vector<int> indices = Geometry::triangulate_polygon(tmp_points);
  298. int ic2 = indices.size();
  299. const int *r2 = indices.ptr();
  300. int bic = total_indices.size();
  301. total_indices.resize(bic + ic2);
  302. int *w2 = total_indices.ptrw();
  303. for (int j = 0; j < ic2; j++) {
  304. w2[j + bic] = r[r2[j]];
  305. }
  306. }
  307. if (total_indices.size()) {
  308. 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(), -1, RID(), antialiased);
  309. }
  310. }
  311. } break;
  312. }
  313. }
  314. void Polygon2D::set_polygon(const PoolVector<Vector2> &p_polygon) {
  315. polygon = p_polygon;
  316. rect_cache_dirty = true;
  317. update();
  318. }
  319. PoolVector<Vector2> Polygon2D::get_polygon() const {
  320. return polygon;
  321. }
  322. void Polygon2D::set_internal_vertex_count(int p_count) {
  323. internal_vertices = p_count;
  324. }
  325. int Polygon2D::get_internal_vertex_count() const {
  326. return internal_vertices;
  327. }
  328. void Polygon2D::set_uv(const PoolVector<Vector2> &p_uv) {
  329. uv = p_uv;
  330. update();
  331. }
  332. PoolVector<Vector2> Polygon2D::get_uv() const {
  333. return uv;
  334. }
  335. void Polygon2D::set_polygons(const Array &p_polygons) {
  336. polygons = p_polygons;
  337. update();
  338. }
  339. Array Polygon2D::get_polygons() const {
  340. return polygons;
  341. }
  342. void Polygon2D::set_color(const Color &p_color) {
  343. color = p_color;
  344. update();
  345. }
  346. Color Polygon2D::get_color() const {
  347. return color;
  348. }
  349. void Polygon2D::set_vertex_colors(const PoolVector<Color> &p_colors) {
  350. vertex_colors = p_colors;
  351. update();
  352. }
  353. PoolVector<Color> Polygon2D::get_vertex_colors() const {
  354. return vertex_colors;
  355. }
  356. void Polygon2D::set_texture(const Ref<Texture> &p_texture) {
  357. texture = p_texture;
  358. /*if (texture.is_valid()) {
  359. uint32_t flags=texture->get_flags();
  360. flags&=~Texture::FLAG_REPEAT;
  361. if (tex_tile)
  362. flags|=Texture::FLAG_REPEAT;
  363. texture->set_flags(flags);
  364. }*/
  365. update();
  366. }
  367. Ref<Texture> Polygon2D::get_texture() const {
  368. return texture;
  369. }
  370. void Polygon2D::set_texture_offset(const Vector2 &p_offset) {
  371. tex_ofs = p_offset;
  372. update();
  373. }
  374. Vector2 Polygon2D::get_texture_offset() const {
  375. return tex_ofs;
  376. }
  377. void Polygon2D::set_texture_rotation(float p_rot) {
  378. tex_rot = p_rot;
  379. update();
  380. }
  381. float Polygon2D::get_texture_rotation() const {
  382. return tex_rot;
  383. }
  384. void Polygon2D::set_texture_rotation_degrees(float p_rot) {
  385. set_texture_rotation(Math::deg2rad(p_rot));
  386. }
  387. float Polygon2D::get_texture_rotation_degrees() const {
  388. return Math::rad2deg(get_texture_rotation());
  389. }
  390. void Polygon2D::set_texture_scale(const Size2 &p_scale) {
  391. tex_scale = p_scale;
  392. update();
  393. }
  394. Size2 Polygon2D::get_texture_scale() const {
  395. return tex_scale;
  396. }
  397. void Polygon2D::set_invert(bool p_invert) {
  398. invert = p_invert;
  399. update();
  400. }
  401. bool Polygon2D::get_invert() const {
  402. return invert;
  403. }
  404. void Polygon2D::set_antialiased(bool p_antialiased) {
  405. antialiased = p_antialiased;
  406. update();
  407. }
  408. bool Polygon2D::get_antialiased() const {
  409. return antialiased;
  410. }
  411. void Polygon2D::set_invert_border(float p_invert_border) {
  412. invert_border = p_invert_border;
  413. update();
  414. }
  415. float Polygon2D::get_invert_border() const {
  416. return invert_border;
  417. }
  418. void Polygon2D::set_offset(const Vector2 &p_offset) {
  419. offset = p_offset;
  420. rect_cache_dirty = true;
  421. update();
  422. _change_notify("offset");
  423. }
  424. Vector2 Polygon2D::get_offset() const {
  425. return offset;
  426. }
  427. void Polygon2D::add_bone(const NodePath &p_path, const PoolVector<float> &p_weights) {
  428. Bone bone;
  429. bone.path = p_path;
  430. bone.weights = p_weights;
  431. bone_weights.push_back(bone);
  432. }
  433. int Polygon2D::get_bone_count() const {
  434. return bone_weights.size();
  435. }
  436. NodePath Polygon2D::get_bone_path(int p_index) const {
  437. ERR_FAIL_INDEX_V(p_index, bone_weights.size(), NodePath());
  438. return bone_weights[p_index].path;
  439. }
  440. PoolVector<float> Polygon2D::get_bone_weights(int p_index) const {
  441. ERR_FAIL_INDEX_V(p_index, bone_weights.size(), PoolVector<float>());
  442. return bone_weights[p_index].weights;
  443. }
  444. void Polygon2D::erase_bone(int p_idx) {
  445. ERR_FAIL_INDEX(p_idx, bone_weights.size());
  446. bone_weights.remove(p_idx);
  447. }
  448. void Polygon2D::clear_bones() {
  449. bone_weights.clear();
  450. }
  451. void Polygon2D::set_bone_weights(int p_index, const PoolVector<float> &p_weights) {
  452. ERR_FAIL_INDEX(p_index, bone_weights.size());
  453. bone_weights.write[p_index].weights = p_weights;
  454. update();
  455. }
  456. void Polygon2D::set_bone_path(int p_index, const NodePath &p_path) {
  457. ERR_FAIL_INDEX(p_index, bone_weights.size());
  458. bone_weights.write[p_index].path = p_path;
  459. update();
  460. }
  461. Array Polygon2D::_get_bones() const {
  462. Array bones;
  463. for (int i = 0; i < get_bone_count(); i++) {
  464. // Convert path property to String to avoid errors due to invalid node path in editor,
  465. // because it's relative to the Skeleton2D node and not Polygon2D.
  466. bones.push_back(String(get_bone_path(i)));
  467. bones.push_back(get_bone_weights(i));
  468. }
  469. return bones;
  470. }
  471. void Polygon2D::_set_bones(const Array &p_bones) {
  472. ERR_FAIL_COND(p_bones.size() & 1);
  473. clear_bones();
  474. for (int i = 0; i < p_bones.size(); i += 2) {
  475. // Convert back from String to NodePath.
  476. add_bone(NodePath(p_bones[i].operator String()), p_bones[i + 1]);
  477. }
  478. }
  479. void Polygon2D::set_skeleton(const NodePath &p_skeleton) {
  480. if (skeleton == p_skeleton) {
  481. return;
  482. }
  483. skeleton = p_skeleton;
  484. update();
  485. }
  486. NodePath Polygon2D::get_skeleton() const {
  487. return skeleton;
  488. }
  489. void Polygon2D::_bind_methods() {
  490. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &Polygon2D::set_polygon);
  491. ClassDB::bind_method(D_METHOD("get_polygon"), &Polygon2D::get_polygon);
  492. ClassDB::bind_method(D_METHOD("set_uv", "uv"), &Polygon2D::set_uv);
  493. ClassDB::bind_method(D_METHOD("get_uv"), &Polygon2D::get_uv);
  494. ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color);
  495. ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color);
  496. ClassDB::bind_method(D_METHOD("set_polygons", "polygons"), &Polygon2D::set_polygons);
  497. ClassDB::bind_method(D_METHOD("get_polygons"), &Polygon2D::get_polygons);
  498. ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors);
  499. ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors);
  500. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Polygon2D::set_texture);
  501. ClassDB::bind_method(D_METHOD("get_texture"), &Polygon2D::get_texture);
  502. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Polygon2D::set_texture_offset);
  503. ClassDB::bind_method(D_METHOD("get_texture_offset"), &Polygon2D::get_texture_offset);
  504. ClassDB::bind_method(D_METHOD("set_texture_rotation", "texture_rotation"), &Polygon2D::set_texture_rotation);
  505. ClassDB::bind_method(D_METHOD("get_texture_rotation"), &Polygon2D::get_texture_rotation);
  506. ClassDB::bind_method(D_METHOD("set_texture_rotation_degrees", "texture_rotation"), &Polygon2D::set_texture_rotation_degrees);
  507. ClassDB::bind_method(D_METHOD("get_texture_rotation_degrees"), &Polygon2D::get_texture_rotation_degrees);
  508. ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Polygon2D::set_texture_scale);
  509. ClassDB::bind_method(D_METHOD("get_texture_scale"), &Polygon2D::get_texture_scale);
  510. ClassDB::bind_method(D_METHOD("set_invert", "invert"), &Polygon2D::set_invert);
  511. ClassDB::bind_method(D_METHOD("get_invert"), &Polygon2D::get_invert);
  512. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &Polygon2D::set_antialiased);
  513. ClassDB::bind_method(D_METHOD("get_antialiased"), &Polygon2D::get_antialiased);
  514. ClassDB::bind_method(D_METHOD("set_invert_border", "invert_border"), &Polygon2D::set_invert_border);
  515. ClassDB::bind_method(D_METHOD("get_invert_border"), &Polygon2D::get_invert_border);
  516. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Polygon2D::set_offset);
  517. ClassDB::bind_method(D_METHOD("get_offset"), &Polygon2D::get_offset);
  518. ClassDB::bind_method(D_METHOD("add_bone", "path", "weights"), &Polygon2D::add_bone);
  519. ClassDB::bind_method(D_METHOD("get_bone_count"), &Polygon2D::get_bone_count);
  520. ClassDB::bind_method(D_METHOD("get_bone_path", "index"), &Polygon2D::get_bone_path);
  521. ClassDB::bind_method(D_METHOD("get_bone_weights", "index"), &Polygon2D::get_bone_weights);
  522. ClassDB::bind_method(D_METHOD("erase_bone", "index"), &Polygon2D::erase_bone);
  523. ClassDB::bind_method(D_METHOD("clear_bones"), &Polygon2D::clear_bones);
  524. ClassDB::bind_method(D_METHOD("set_bone_path", "index", "path"), &Polygon2D::set_bone_path);
  525. ClassDB::bind_method(D_METHOD("set_bone_weights", "index", "weights"), &Polygon2D::set_bone_weights);
  526. ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &Polygon2D::set_skeleton);
  527. ClassDB::bind_method(D_METHOD("get_skeleton"), &Polygon2D::get_skeleton);
  528. ClassDB::bind_method(D_METHOD("set_internal_vertex_count", "internal_vertex_count"), &Polygon2D::set_internal_vertex_count);
  529. ClassDB::bind_method(D_METHOD("get_internal_vertex_count"), &Polygon2D::get_internal_vertex_count);
  530. ClassDB::bind_method(D_METHOD("_set_bones", "bones"), &Polygon2D::_set_bones);
  531. ClassDB::bind_method(D_METHOD("_get_bones"), &Polygon2D::_get_bones);
  532. ClassDB::bind_method(D_METHOD("_skeleton_bone_setup_changed"), &Polygon2D::_skeleton_bone_setup_changed);
  533. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  534. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  535. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased");
  536. ADD_GROUP("Texture", "");
  537. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  538. ADD_GROUP("Texture", "texture_");
  539. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_offset"), "set_texture_offset", "get_texture_offset");
  540. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_scale", PROPERTY_HINT_LINK), "set_texture_scale", "get_texture_scale");
  541. ADD_PROPERTY(PropertyInfo(Variant::REAL, "texture_rotation_degrees", PROPERTY_HINT_RANGE, "-360,360,0.1,or_lesser,or_greater"), "set_texture_rotation_degrees", "get_texture_rotation_degrees");
  542. ADD_PROPERTY(PropertyInfo(Variant::REAL, "texture_rotation", PROPERTY_HINT_NONE, "", 0), "set_texture_rotation", "get_texture_rotation");
  543. ADD_GROUP("Skeleton", "");
  544. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton2D"), "set_skeleton", "get_skeleton");
  545. ADD_GROUP("Invert", "invert_");
  546. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_enable"), "set_invert", "get_invert");
  547. ADD_PROPERTY(PropertyInfo(Variant::REAL, "invert_border", PROPERTY_HINT_RANGE, "0.1,16384,0.1"), "set_invert_border", "get_invert_border");
  548. ADD_GROUP("Data", "");
  549. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  550. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv");
  551. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors");
  552. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons"), "set_polygons", "get_polygons");
  553. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_bones", "_get_bones");
  554. ADD_PROPERTY(PropertyInfo(Variant::INT, "internal_vertex_count", PROPERTY_HINT_RANGE, "0,1000"), "set_internal_vertex_count", "get_internal_vertex_count");
  555. }
  556. Polygon2D::Polygon2D() {
  557. invert = false;
  558. invert_border = 100;
  559. antialiased = false;
  560. tex_rot = 0;
  561. tex_tile = true;
  562. tex_scale = Vector2(1, 1);
  563. color = Color(1, 1, 1);
  564. rect_cache_dirty = true;
  565. internal_vertices = 0;
  566. current_skeleton_id = 0;
  567. }
  568. Polygon2D::~Polygon2D() {
  569. // Most definitely don't want to leave references to this deleted canvas item
  570. // in the skeleton.
  571. if (get_canvas_item().is_valid()) {
  572. VS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID());
  573. }
  574. }