polygon_2d.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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_2d.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. const Vector2 *r = polygon.ptr();
  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 Geometry2D::is_point_in_polygon(p_point - get_offset(), polygon2d);
  79. }
  80. #endif
  81. void Polygon2D::_validate_property(PropertyInfo &p_property) const {
  82. if (!invert && p_property.name == "invert_border") {
  83. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  84. }
  85. }
  86. void Polygon2D::_skeleton_bone_setup_changed() {
  87. queue_redraw();
  88. }
  89. void Polygon2D::_notification(int p_what) {
  90. if (p_what == NOTIFICATION_TRANSFORM_CHANGED && !Engine::get_singleton()->is_editor_hint()) {
  91. return; // Mesh recreation for NOTIFICATION_TRANSFORM_CHANGED is only needed in editor.
  92. }
  93. switch (p_what) {
  94. case NOTIFICATION_TRANSFORM_CHANGED:
  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;
  104. if (skeleton_node && !invert && bone_weights.size()) {
  105. RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), skeleton_node->get_skeleton());
  106. new_skeleton_id = skeleton_node->get_instance_id();
  107. } else {
  108. RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID());
  109. }
  110. if (new_skeleton_id != current_skeleton_id) {
  111. Object *old_skeleton = ObjectDB::get_instance(current_skeleton_id);
  112. if (old_skeleton) {
  113. old_skeleton->disconnect("bone_setup_changed", callable_mp(this, &Polygon2D::_skeleton_bone_setup_changed));
  114. }
  115. if (skeleton_node) {
  116. skeleton_node->connect("bone_setup_changed", callable_mp(this, &Polygon2D::_skeleton_bone_setup_changed));
  117. }
  118. current_skeleton_id = new_skeleton_id;
  119. }
  120. Vector<Vector2> points;
  121. Vector<Vector2> uvs;
  122. Vector<int> bones;
  123. Vector<float> weights;
  124. int len = polygon.size();
  125. if ((invert || polygons.size() == 0) && internal_vertices > 0) {
  126. //if no polygons are around, internal vertices must not be drawn, else let them be
  127. len -= internal_vertices;
  128. }
  129. if (len <= 0) {
  130. return;
  131. }
  132. points.resize(len);
  133. {
  134. const Vector2 *polyr = polygon.ptr();
  135. for (int i = 0; i < len; i++) {
  136. points.write[i] = polyr[i] + offset;
  137. }
  138. }
  139. if (invert) {
  140. Rect2 bounds;
  141. int highest_idx = -1;
  142. real_t highest_y = -1e20;
  143. real_t sum = 0.0;
  144. for (int i = 0; i < len; i++) {
  145. if (i == 0) {
  146. bounds.position = points[i];
  147. } else {
  148. bounds.expand_to(points[i]);
  149. }
  150. if (points[i].y > highest_y) {
  151. highest_idx = i;
  152. highest_y = points[i].y;
  153. }
  154. int ni = (i + 1) % len;
  155. sum += (points[ni].x - points[i].x) * (points[ni].y + points[i].y);
  156. }
  157. bounds = bounds.grow(invert_border);
  158. Vector2 ep[7] = {
  159. Vector2(points[highest_idx].x, points[highest_idx].y + invert_border),
  160. Vector2(bounds.position + bounds.size),
  161. Vector2(bounds.position + Vector2(bounds.size.x, 0)),
  162. Vector2(bounds.position),
  163. Vector2(bounds.position + Vector2(0, bounds.size.y)),
  164. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y + invert_border),
  165. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y),
  166. };
  167. if (sum > 0) {
  168. SWAP(ep[1], ep[4]);
  169. SWAP(ep[2], ep[3]);
  170. SWAP(ep[5], ep[0]);
  171. SWAP(ep[6], points.write[highest_idx]);
  172. }
  173. points.resize(points.size() + 7);
  174. for (int i = points.size() - 1; i >= highest_idx + 7; i--) {
  175. points.write[i] = points[i - 7];
  176. }
  177. for (int i = 0; i < 7; i++) {
  178. points.write[highest_idx + i + 1] = ep[i];
  179. }
  180. len = points.size();
  181. }
  182. if (texture.is_valid()) {
  183. Transform2D texmat(tex_rot, tex_ofs);
  184. texmat.scale(tex_scale);
  185. Size2 tex_size = texture->get_size();
  186. uvs.resize(len);
  187. if (points.size() == uv.size()) {
  188. const Vector2 *uvr = uv.ptr();
  189. for (int i = 0; i < len; i++) {
  190. uvs.write[i] = texmat.xform(uvr[i]) / tex_size;
  191. }
  192. } else {
  193. for (int i = 0; i < len; i++) {
  194. uvs.write[i] = texmat.xform(points[i]) / tex_size;
  195. }
  196. }
  197. }
  198. if (skeleton_node && !invert && bone_weights.size()) {
  199. //a skeleton is set! fill indices and weights
  200. int vc = len;
  201. bones.resize(vc * 4);
  202. weights.resize(vc * 4);
  203. int *bonesw = bones.ptrw();
  204. float *weightsw = weights.ptrw();
  205. for (int i = 0; i < vc * 4; i++) {
  206. bonesw[i] = 0;
  207. weightsw[i] = 0;
  208. }
  209. for (int i = 0; i < bone_weights.size(); i++) {
  210. if (bone_weights[i].weights.size() != points.size()) {
  211. continue; //different number of vertices, sorry not using.
  212. }
  213. if (!skeleton_node->has_node(bone_weights[i].path)) {
  214. continue; //node does not exist
  215. }
  216. Bone2D *bone = Object::cast_to<Bone2D>(skeleton_node->get_node(bone_weights[i].path));
  217. if (!bone) {
  218. continue;
  219. }
  220. int bone_index = bone->get_index_in_skeleton();
  221. const float *r = bone_weights[i].weights.ptr();
  222. for (int j = 0; j < vc; j++) {
  223. if (r[j] == 0.0) {
  224. continue; //weight is unpainted, skip
  225. }
  226. //find an index with a weight
  227. for (int k = 0; k < 4; k++) {
  228. if (weightsw[j * 4 + k] < r[j]) {
  229. //this is less than this weight, insert weight!
  230. for (int l = 3; l > k; l--) {
  231. weightsw[j * 4 + l] = weightsw[j * 4 + l - 1];
  232. bonesw[j * 4 + l] = bonesw[j * 4 + l - 1];
  233. }
  234. weightsw[j * 4 + k] = r[j];
  235. bonesw[j * 4 + k] = bone_index;
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. //normalize the weights
  242. for (int i = 0; i < vc; i++) {
  243. real_t tw = 0.0;
  244. for (int j = 0; j < 4; j++) {
  245. tw += weightsw[i * 4 + j];
  246. }
  247. if (tw == 0) {
  248. continue; //unpainted, do nothing
  249. }
  250. //normalize
  251. for (int j = 0; j < 4; j++) {
  252. weightsw[i * 4 + j] /= tw;
  253. }
  254. }
  255. }
  256. Vector<Color> colors;
  257. colors.resize(len);
  258. if (vertex_colors.size() == points.size()) {
  259. const Color *color_r = vertex_colors.ptr();
  260. for (int i = 0; i < len; i++) {
  261. colors.write[i] = color_r[i];
  262. }
  263. } else {
  264. for (int i = 0; i < len; i++) {
  265. colors.write[i] = color;
  266. }
  267. }
  268. Vector<int> index_array;
  269. if (invert || polygons.size() == 0) {
  270. index_array = Geometry2D::triangulate_polygon(points);
  271. } else {
  272. //draw individual polygons
  273. for (int i = 0; i < polygons.size(); i++) {
  274. Vector<int> src_indices = polygons[i];
  275. int ic = src_indices.size();
  276. if (ic < 3) {
  277. continue;
  278. }
  279. const int *r = src_indices.ptr();
  280. Vector<Vector2> tmp_points;
  281. tmp_points.resize(ic);
  282. for (int j = 0; j < ic; j++) {
  283. int idx = r[j];
  284. ERR_CONTINUE(idx < 0 || idx >= points.size());
  285. tmp_points.write[j] = points[r[j]];
  286. }
  287. Vector<int> indices = Geometry2D::triangulate_polygon(tmp_points);
  288. int ic2 = indices.size();
  289. const int *r2 = indices.ptr();
  290. int bic = index_array.size();
  291. index_array.resize(bic + ic2);
  292. int *w2 = index_array.ptrw();
  293. for (int j = 0; j < ic2; j++) {
  294. w2[j + bic] = r[r2[j]];
  295. }
  296. }
  297. }
  298. RS::get_singleton()->mesh_clear(mesh);
  299. if (index_array.size()) {
  300. Array arr;
  301. arr.resize(RS::ARRAY_MAX);
  302. arr[RS::ARRAY_VERTEX] = points;
  303. if (uvs.size() == points.size()) {
  304. arr[RS::ARRAY_TEX_UV] = uvs;
  305. }
  306. if (colors.size() == points.size()) {
  307. arr[RS::ARRAY_COLOR] = colors;
  308. }
  309. if (bones.size() == points.size() * 4) {
  310. arr[RS::ARRAY_BONES] = bones;
  311. arr[RS::ARRAY_WEIGHTS] = weights;
  312. }
  313. arr[RS::ARRAY_INDEX] = index_array;
  314. RS::SurfaceData sd;
  315. if (skeleton_node) {
  316. // Compute transform between mesh and skeleton for runtime AABB compute.
  317. const Transform2D mesh_transform = get_global_transform();
  318. const Transform2D skeleton_transform = skeleton_node->get_global_transform();
  319. const Transform2D mesh_to_sk2d = skeleton_transform.affine_inverse() * mesh_transform;
  320. // Convert 2d transform to 3d.
  321. sd.mesh_to_skeleton_xform.basis.rows[0][0] = mesh_to_sk2d.columns[0][0];
  322. sd.mesh_to_skeleton_xform.basis.rows[1][0] = mesh_to_sk2d.columns[0][1];
  323. sd.mesh_to_skeleton_xform.origin.x = mesh_to_sk2d.get_origin().x;
  324. sd.mesh_to_skeleton_xform.basis.rows[0][1] = mesh_to_sk2d.columns[1][0];
  325. sd.mesh_to_skeleton_xform.basis.rows[1][1] = mesh_to_sk2d.columns[1][1];
  326. sd.mesh_to_skeleton_xform.origin.y = mesh_to_sk2d.get_origin().y;
  327. }
  328. Error err = RS::get_singleton()->mesh_create_surface_data_from_arrays(&sd, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  329. if (err != OK) {
  330. return;
  331. }
  332. RS::get_singleton()->mesh_add_surface(mesh, sd);
  333. RS::get_singleton()->canvas_item_add_mesh(get_canvas_item(), mesh, Transform2D(), Color(1, 1, 1), texture.is_valid() ? texture->get_rid() : RID());
  334. }
  335. } break;
  336. }
  337. }
  338. void Polygon2D::set_polygon(const Vector<Vector2> &p_polygon) {
  339. polygon = p_polygon;
  340. rect_cache_dirty = true;
  341. queue_redraw();
  342. }
  343. Vector<Vector2> Polygon2D::get_polygon() const {
  344. return polygon;
  345. }
  346. void Polygon2D::set_internal_vertex_count(int p_count) {
  347. internal_vertices = p_count;
  348. }
  349. int Polygon2D::get_internal_vertex_count() const {
  350. return internal_vertices;
  351. }
  352. void Polygon2D::set_uv(const Vector<Vector2> &p_uv) {
  353. uv = p_uv;
  354. queue_redraw();
  355. }
  356. Vector<Vector2> Polygon2D::get_uv() const {
  357. return uv;
  358. }
  359. void Polygon2D::set_polygons(const Array &p_polygons) {
  360. polygons = p_polygons;
  361. queue_redraw();
  362. }
  363. Array Polygon2D::get_polygons() const {
  364. return polygons;
  365. }
  366. void Polygon2D::set_color(const Color &p_color) {
  367. color = p_color;
  368. queue_redraw();
  369. }
  370. Color Polygon2D::get_color() const {
  371. return color;
  372. }
  373. void Polygon2D::set_vertex_colors(const Vector<Color> &p_colors) {
  374. vertex_colors = p_colors;
  375. queue_redraw();
  376. }
  377. Vector<Color> Polygon2D::get_vertex_colors() const {
  378. return vertex_colors;
  379. }
  380. void Polygon2D::set_texture(const Ref<Texture2D> &p_texture) {
  381. texture = p_texture;
  382. queue_redraw();
  383. }
  384. Ref<Texture2D> Polygon2D::get_texture() const {
  385. return texture;
  386. }
  387. void Polygon2D::set_texture_offset(const Vector2 &p_offset) {
  388. tex_ofs = p_offset;
  389. queue_redraw();
  390. }
  391. Vector2 Polygon2D::get_texture_offset() const {
  392. return tex_ofs;
  393. }
  394. void Polygon2D::set_texture_rotation(real_t p_rot) {
  395. tex_rot = p_rot;
  396. queue_redraw();
  397. }
  398. real_t Polygon2D::get_texture_rotation() const {
  399. return tex_rot;
  400. }
  401. void Polygon2D::set_texture_scale(const Size2 &p_scale) {
  402. tex_scale = p_scale;
  403. queue_redraw();
  404. }
  405. Size2 Polygon2D::get_texture_scale() const {
  406. return tex_scale;
  407. }
  408. void Polygon2D::set_invert(bool p_invert) {
  409. invert = p_invert;
  410. queue_redraw();
  411. notify_property_list_changed();
  412. }
  413. bool Polygon2D::get_invert() const {
  414. return invert;
  415. }
  416. void Polygon2D::set_antialiased(bool p_antialiased) {
  417. antialiased = p_antialiased;
  418. queue_redraw();
  419. }
  420. bool Polygon2D::get_antialiased() const {
  421. return antialiased;
  422. }
  423. void Polygon2D::set_invert_border(real_t p_invert_border) {
  424. invert_border = p_invert_border;
  425. queue_redraw();
  426. }
  427. real_t Polygon2D::get_invert_border() const {
  428. return invert_border;
  429. }
  430. void Polygon2D::set_offset(const Vector2 &p_offset) {
  431. offset = p_offset;
  432. rect_cache_dirty = true;
  433. queue_redraw();
  434. }
  435. Vector2 Polygon2D::get_offset() const {
  436. return offset;
  437. }
  438. void Polygon2D::add_bone(const NodePath &p_path, const Vector<float> &p_weights) {
  439. Bone bone;
  440. bone.path = p_path;
  441. bone.weights = p_weights;
  442. bone_weights.push_back(bone);
  443. }
  444. int Polygon2D::get_bone_count() const {
  445. return bone_weights.size();
  446. }
  447. NodePath Polygon2D::get_bone_path(int p_index) const {
  448. ERR_FAIL_INDEX_V(p_index, bone_weights.size(), NodePath());
  449. return bone_weights[p_index].path;
  450. }
  451. Vector<float> Polygon2D::get_bone_weights(int p_index) const {
  452. ERR_FAIL_INDEX_V(p_index, bone_weights.size(), Vector<float>());
  453. return bone_weights[p_index].weights;
  454. }
  455. void Polygon2D::erase_bone(int p_idx) {
  456. ERR_FAIL_INDEX(p_idx, bone_weights.size());
  457. bone_weights.remove_at(p_idx);
  458. }
  459. void Polygon2D::clear_bones() {
  460. bone_weights.clear();
  461. }
  462. void Polygon2D::set_bone_weights(int p_index, const Vector<float> &p_weights) {
  463. ERR_FAIL_INDEX(p_index, bone_weights.size());
  464. bone_weights.write[p_index].weights = p_weights;
  465. queue_redraw();
  466. }
  467. void Polygon2D::set_bone_path(int p_index, const NodePath &p_path) {
  468. ERR_FAIL_INDEX(p_index, bone_weights.size());
  469. bone_weights.write[p_index].path = p_path;
  470. queue_redraw();
  471. }
  472. Array Polygon2D::_get_bones() const {
  473. Array bones;
  474. for (int i = 0; i < get_bone_count(); i++) {
  475. // Convert path property to String to avoid errors due to invalid node path in editor,
  476. // because it's relative to the Skeleton2D node and not Polygon2D.
  477. bones.push_back(String(get_bone_path(i)));
  478. bones.push_back(get_bone_weights(i));
  479. }
  480. return bones;
  481. }
  482. void Polygon2D::_set_bones(const Array &p_bones) {
  483. ERR_FAIL_COND(p_bones.size() & 1);
  484. clear_bones();
  485. for (int i = 0; i < p_bones.size(); i += 2) {
  486. // Convert back from String to NodePath.
  487. add_bone(NodePath(p_bones[i]), p_bones[i + 1]);
  488. }
  489. }
  490. void Polygon2D::set_skeleton(const NodePath &p_skeleton) {
  491. if (skeleton == p_skeleton) {
  492. return;
  493. }
  494. skeleton = p_skeleton;
  495. queue_redraw();
  496. }
  497. NodePath Polygon2D::get_skeleton() const {
  498. return skeleton;
  499. }
  500. void Polygon2D::_bind_methods() {
  501. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &Polygon2D::set_polygon);
  502. ClassDB::bind_method(D_METHOD("get_polygon"), &Polygon2D::get_polygon);
  503. ClassDB::bind_method(D_METHOD("set_uv", "uv"), &Polygon2D::set_uv);
  504. ClassDB::bind_method(D_METHOD("get_uv"), &Polygon2D::get_uv);
  505. ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color);
  506. ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color);
  507. ClassDB::bind_method(D_METHOD("set_polygons", "polygons"), &Polygon2D::set_polygons);
  508. ClassDB::bind_method(D_METHOD("get_polygons"), &Polygon2D::get_polygons);
  509. ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors);
  510. ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors);
  511. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Polygon2D::set_texture);
  512. ClassDB::bind_method(D_METHOD("get_texture"), &Polygon2D::get_texture);
  513. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Polygon2D::set_texture_offset);
  514. ClassDB::bind_method(D_METHOD("get_texture_offset"), &Polygon2D::get_texture_offset);
  515. ClassDB::bind_method(D_METHOD("set_texture_rotation", "texture_rotation"), &Polygon2D::set_texture_rotation);
  516. ClassDB::bind_method(D_METHOD("get_texture_rotation"), &Polygon2D::get_texture_rotation);
  517. ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Polygon2D::set_texture_scale);
  518. ClassDB::bind_method(D_METHOD("get_texture_scale"), &Polygon2D::get_texture_scale);
  519. ClassDB::bind_method(D_METHOD("set_invert_enabled", "invert"), &Polygon2D::set_invert);
  520. ClassDB::bind_method(D_METHOD("get_invert_enabled"), &Polygon2D::get_invert);
  521. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &Polygon2D::set_antialiased);
  522. ClassDB::bind_method(D_METHOD("get_antialiased"), &Polygon2D::get_antialiased);
  523. ClassDB::bind_method(D_METHOD("set_invert_border", "invert_border"), &Polygon2D::set_invert_border);
  524. ClassDB::bind_method(D_METHOD("get_invert_border"), &Polygon2D::get_invert_border);
  525. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Polygon2D::set_offset);
  526. ClassDB::bind_method(D_METHOD("get_offset"), &Polygon2D::get_offset);
  527. ClassDB::bind_method(D_METHOD("add_bone", "path", "weights"), &Polygon2D::add_bone);
  528. ClassDB::bind_method(D_METHOD("get_bone_count"), &Polygon2D::get_bone_count);
  529. ClassDB::bind_method(D_METHOD("get_bone_path", "index"), &Polygon2D::get_bone_path);
  530. ClassDB::bind_method(D_METHOD("get_bone_weights", "index"), &Polygon2D::get_bone_weights);
  531. ClassDB::bind_method(D_METHOD("erase_bone", "index"), &Polygon2D::erase_bone);
  532. ClassDB::bind_method(D_METHOD("clear_bones"), &Polygon2D::clear_bones);
  533. ClassDB::bind_method(D_METHOD("set_bone_path", "index", "path"), &Polygon2D::set_bone_path);
  534. ClassDB::bind_method(D_METHOD("set_bone_weights", "index", "weights"), &Polygon2D::set_bone_weights);
  535. ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &Polygon2D::set_skeleton);
  536. ClassDB::bind_method(D_METHOD("get_skeleton"), &Polygon2D::get_skeleton);
  537. ClassDB::bind_method(D_METHOD("set_internal_vertex_count", "internal_vertex_count"), &Polygon2D::set_internal_vertex_count);
  538. ClassDB::bind_method(D_METHOD("get_internal_vertex_count"), &Polygon2D::get_internal_vertex_count);
  539. ClassDB::bind_method(D_METHOD("_set_bones", "bones"), &Polygon2D::_set_bones);
  540. ClassDB::bind_method(D_METHOD("_get_bones"), &Polygon2D::_get_bones);
  541. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  542. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  543. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased");
  544. ADD_GROUP("Texture", "texture_");
  545. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  546. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_offset", "get_texture_offset");
  547. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_scale", PROPERTY_HINT_LINK), "set_texture_scale", "get_texture_scale");
  548. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texture_rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_texture_rotation", "get_texture_rotation");
  549. ADD_GROUP("Skeleton", "");
  550. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton2D"), "set_skeleton", "get_skeleton");
  551. ADD_GROUP("Invert", "invert_");
  552. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_enabled"), "set_invert_enabled", "get_invert_enabled");
  553. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "invert_border", PROPERTY_HINT_RANGE, "0.1,16384,0.1,suffix:px"), "set_invert_border", "get_invert_border");
  554. ADD_GROUP("Data", "");
  555. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  556. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv");
  557. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors");
  558. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons"), "set_polygons", "get_polygons");
  559. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "_set_bones", "_get_bones");
  560. ADD_PROPERTY(PropertyInfo(Variant::INT, "internal_vertex_count", PROPERTY_HINT_RANGE, "0,1000"), "set_internal_vertex_count", "get_internal_vertex_count");
  561. }
  562. Polygon2D::Polygon2D() {
  563. mesh = RS::get_singleton()->mesh_create();
  564. }
  565. Polygon2D::~Polygon2D() {
  566. // This will free the internally-allocated mesh instance, if allocated.
  567. ERR_FAIL_NULL(RenderingServer::get_singleton());
  568. RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID());
  569. RS::get_singleton()->free(mesh);
  570. }