polygon_2d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*************************************************************************/
  2. /* polygon_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. Rect2 Polygon2D::get_item_rect() const {
  32. if (rect_cache_dirty) {
  33. int l = polygon.size();
  34. PoolVector<Vector2>::Read r = polygon.read();
  35. item_rect = Rect2();
  36. for (int i = 0; i < l; i++) {
  37. Vector2 pos = r[i] + offset;
  38. if (i == 0)
  39. item_rect.position = pos;
  40. else
  41. item_rect.expand_to(pos);
  42. }
  43. item_rect = item_rect.grow(20);
  44. rect_cache_dirty = false;
  45. }
  46. return item_rect;
  47. }
  48. void Polygon2D::edit_set_pivot(const Point2 &p_pivot) {
  49. set_offset(p_pivot);
  50. }
  51. Point2 Polygon2D::edit_get_pivot() const {
  52. return get_offset();
  53. }
  54. bool Polygon2D::edit_has_pivot() const {
  55. return true;
  56. }
  57. void Polygon2D::_notification(int p_what) {
  58. switch (p_what) {
  59. case NOTIFICATION_DRAW: {
  60. if (polygon.size() < 3)
  61. return;
  62. Vector<Vector2> points;
  63. Vector<Vector2> uvs;
  64. points.resize(polygon.size());
  65. int len = points.size();
  66. {
  67. PoolVector<Vector2>::Read polyr = polygon.read();
  68. for (int i = 0; i < len; i++) {
  69. points[i] = polyr[i] + offset;
  70. }
  71. }
  72. if (invert) {
  73. Rect2 bounds;
  74. int highest_idx = -1;
  75. float highest_y = -1e20;
  76. float sum = 0;
  77. for (int i = 0; i < len; i++) {
  78. if (i == 0)
  79. bounds.position = points[i];
  80. else
  81. bounds.expand_to(points[i]);
  82. if (points[i].y > highest_y) {
  83. highest_idx = i;
  84. highest_y = points[i].y;
  85. }
  86. int ni = (i + 1) % len;
  87. sum += (points[ni].x - points[i].x) * (points[ni].y + points[i].y);
  88. }
  89. bounds = bounds.grow(invert_border);
  90. Vector2 ep[7] = {
  91. Vector2(points[highest_idx].x, points[highest_idx].y + invert_border),
  92. Vector2(bounds.position + bounds.size),
  93. Vector2(bounds.position + Vector2(bounds.size.x, 0)),
  94. Vector2(bounds.position),
  95. Vector2(bounds.position + Vector2(0, bounds.size.y)),
  96. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y + invert_border),
  97. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y),
  98. };
  99. if (sum > 0) {
  100. SWAP(ep[1], ep[4]);
  101. SWAP(ep[2], ep[3]);
  102. SWAP(ep[5], ep[0]);
  103. SWAP(ep[6], points[highest_idx]);
  104. }
  105. points.resize(points.size() + 7);
  106. for (int i = points.size() - 1; i >= highest_idx + 7; i--) {
  107. points[i] = points[i - 7];
  108. }
  109. for (int i = 0; i < 7; i++) {
  110. points[highest_idx + i + 1] = ep[i];
  111. }
  112. len = points.size();
  113. }
  114. if (texture.is_valid()) {
  115. Transform2D texmat(tex_rot, tex_ofs);
  116. texmat.scale(tex_scale);
  117. Size2 tex_size = texture->get_size();
  118. uvs.resize(points.size());
  119. if (points.size() == uv.size()) {
  120. PoolVector<Vector2>::Read uvr = uv.read();
  121. for (int i = 0; i < len; i++) {
  122. uvs[i] = texmat.xform(uvr[i]) / tex_size;
  123. }
  124. } else {
  125. for (int i = 0; i < len; i++) {
  126. uvs[i] = texmat.xform(points[i]) / tex_size;
  127. }
  128. }
  129. }
  130. Vector<Color> colors;
  131. int color_len = vertex_colors.size();
  132. colors.resize(len);
  133. {
  134. PoolVector<Color>::Read color_r = vertex_colors.read();
  135. for (int i = 0; i < color_len && i < len; i++) {
  136. colors[i] = color_r[i];
  137. }
  138. for (int i = color_len; i < len; i++) {
  139. colors[i] = color;
  140. }
  141. }
  142. // Vector<int> indices = Geometry::triangulate_polygon(points);
  143. // VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID());
  144. VS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID(), RID(), antialiased);
  145. } break;
  146. }
  147. }
  148. void Polygon2D::set_polygon(const PoolVector<Vector2> &p_polygon) {
  149. polygon = p_polygon;
  150. rect_cache_dirty = true;
  151. update();
  152. }
  153. PoolVector<Vector2> Polygon2D::get_polygon() const {
  154. return polygon;
  155. }
  156. void Polygon2D::set_uv(const PoolVector<Vector2> &p_uv) {
  157. uv = p_uv;
  158. update();
  159. }
  160. PoolVector<Vector2> Polygon2D::get_uv() const {
  161. return uv;
  162. }
  163. void Polygon2D::set_color(const Color &p_color) {
  164. color = p_color;
  165. update();
  166. }
  167. Color Polygon2D::get_color() const {
  168. return color;
  169. }
  170. void Polygon2D::set_vertex_colors(const PoolVector<Color> &p_colors) {
  171. vertex_colors = p_colors;
  172. update();
  173. }
  174. PoolVector<Color> Polygon2D::get_vertex_colors() const {
  175. return vertex_colors;
  176. }
  177. void Polygon2D::set_texture(const Ref<Texture> &p_texture) {
  178. texture = p_texture;
  179. /*if (texture.is_valid()) {
  180. uint32_t flags=texture->get_flags();
  181. flags&=~Texture::FLAG_REPEAT;
  182. if (tex_tile)
  183. flags|=Texture::FLAG_REPEAT;
  184. texture->set_flags(flags);
  185. }*/
  186. update();
  187. }
  188. Ref<Texture> Polygon2D::get_texture() const {
  189. return texture;
  190. }
  191. void Polygon2D::set_texture_offset(const Vector2 &p_offset) {
  192. tex_ofs = p_offset;
  193. update();
  194. }
  195. Vector2 Polygon2D::get_texture_offset() const {
  196. return tex_ofs;
  197. }
  198. void Polygon2D::set_texture_rotation(float p_rot) {
  199. tex_rot = p_rot;
  200. update();
  201. }
  202. float Polygon2D::get_texture_rotation() const {
  203. return tex_rot;
  204. }
  205. void Polygon2D::_set_texture_rotationd(float p_rot) {
  206. set_texture_rotation(Math::deg2rad(p_rot));
  207. }
  208. float Polygon2D::_get_texture_rotationd() const {
  209. return Math::rad2deg(get_texture_rotation());
  210. }
  211. void Polygon2D::set_texture_scale(const Size2 &p_scale) {
  212. tex_scale = p_scale;
  213. update();
  214. }
  215. Size2 Polygon2D::get_texture_scale() const {
  216. return tex_scale;
  217. }
  218. void Polygon2D::set_invert(bool p_invert) {
  219. invert = p_invert;
  220. update();
  221. }
  222. bool Polygon2D::get_invert() const {
  223. return invert;
  224. }
  225. void Polygon2D::set_antialiased(bool p_antialiased) {
  226. antialiased = p_antialiased;
  227. update();
  228. }
  229. bool Polygon2D::get_antialiased() const {
  230. return antialiased;
  231. }
  232. void Polygon2D::set_invert_border(float p_invert_border) {
  233. invert_border = p_invert_border;
  234. update();
  235. }
  236. float Polygon2D::get_invert_border() const {
  237. return invert_border;
  238. }
  239. void Polygon2D::set_offset(const Vector2 &p_offset) {
  240. offset = p_offset;
  241. rect_cache_dirty = true;
  242. update();
  243. }
  244. Vector2 Polygon2D::get_offset() const {
  245. return offset;
  246. }
  247. void Polygon2D::_bind_methods() {
  248. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &Polygon2D::set_polygon);
  249. ClassDB::bind_method(D_METHOD("get_polygon"), &Polygon2D::get_polygon);
  250. ClassDB::bind_method(D_METHOD("set_uv", "uv"), &Polygon2D::set_uv);
  251. ClassDB::bind_method(D_METHOD("get_uv"), &Polygon2D::get_uv);
  252. ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color);
  253. ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color);
  254. ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors);
  255. ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors);
  256. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Polygon2D::set_texture);
  257. ClassDB::bind_method(D_METHOD("get_texture"), &Polygon2D::get_texture);
  258. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Polygon2D::set_texture_offset);
  259. ClassDB::bind_method(D_METHOD("get_texture_offset"), &Polygon2D::get_texture_offset);
  260. ClassDB::bind_method(D_METHOD("set_texture_rotation", "texture_rotation"), &Polygon2D::set_texture_rotation);
  261. ClassDB::bind_method(D_METHOD("get_texture_rotation"), &Polygon2D::get_texture_rotation);
  262. ClassDB::bind_method(D_METHOD("_set_texture_rotationd", "texture_rotation"), &Polygon2D::_set_texture_rotationd);
  263. ClassDB::bind_method(D_METHOD("_get_texture_rotationd"), &Polygon2D::_get_texture_rotationd);
  264. ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Polygon2D::set_texture_scale);
  265. ClassDB::bind_method(D_METHOD("get_texture_scale"), &Polygon2D::get_texture_scale);
  266. ClassDB::bind_method(D_METHOD("set_invert", "invert"), &Polygon2D::set_invert);
  267. ClassDB::bind_method(D_METHOD("get_invert"), &Polygon2D::get_invert);
  268. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &Polygon2D::set_antialiased);
  269. ClassDB::bind_method(D_METHOD("get_antialiased"), &Polygon2D::get_antialiased);
  270. ClassDB::bind_method(D_METHOD("set_invert_border", "invert_border"), &Polygon2D::set_invert_border);
  271. ClassDB::bind_method(D_METHOD("get_invert_border"), &Polygon2D::get_invert_border);
  272. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Polygon2D::set_offset);
  273. ClassDB::bind_method(D_METHOD("get_offset"), &Polygon2D::get_offset);
  274. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  275. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv");
  276. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  277. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors");
  278. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  279. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased");
  280. ADD_GROUP("Texture", "");
  281. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  282. ADD_GROUP("Texture", "texture_");
  283. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_offset"), "set_texture_offset", "get_texture_offset");
  284. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_scale"), "set_texture_scale", "get_texture_scale");
  285. ADD_PROPERTY(PropertyInfo(Variant::REAL, "texture_rotation", PROPERTY_HINT_RANGE, "-1440,1440,0.1"), "_set_texture_rotationd", "_get_texture_rotationd");
  286. ADD_GROUP("Invert", "invert_");
  287. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_enable"), "set_invert", "get_invert");
  288. ADD_PROPERTY(PropertyInfo(Variant::REAL, "invert_border", PROPERTY_HINT_RANGE, "0.1,16384,0.1"), "set_invert_border", "get_invert_border");
  289. }
  290. Polygon2D::Polygon2D() {
  291. invert = 0;
  292. invert_border = 100;
  293. antialiased = false;
  294. tex_rot = 0;
  295. tex_tile = true;
  296. tex_scale = Vector2(1, 1);
  297. color = Color(1, 1, 1);
  298. rect_cache_dirty = true;
  299. }