line_2d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**************************************************************************/
  2. /* line_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 "line_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "line_builder.h"
  33. Line2D::Line2D() {
  34. }
  35. #ifdef DEBUG_ENABLED
  36. Rect2 Line2D::_edit_get_rect() const {
  37. if (_points.size() == 0) {
  38. return Rect2(0, 0, 0, 0);
  39. }
  40. Vector2 d = Vector2(_width, _width);
  41. Rect2 bounding_rect = Rect2(_points[0] - d, 2 * d);
  42. for (int i = 1; i < _points.size(); i++) {
  43. bounding_rect.expand_to(_points[i] - d);
  44. bounding_rect.expand_to(_points[i] + d);
  45. }
  46. return bounding_rect;
  47. }
  48. bool Line2D::_edit_use_rect() const {
  49. return true;
  50. }
  51. bool Line2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  52. const real_t d = _width / 2 + p_tolerance;
  53. const Vector2 *points = _points.ptr();
  54. for (int i = 0; i < _points.size() - 1; i++) {
  55. Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, &points[i]);
  56. if (p_point.distance_to(p) <= d) {
  57. return true;
  58. }
  59. }
  60. if (_closed && _points.size() > 2) {
  61. const Vector2 closing_segment[2] = { points[0], points[_points.size() - 1] };
  62. Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, closing_segment);
  63. if (p_point.distance_to(p) <= d) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. #endif
  70. void Line2D::set_points(const Vector<Vector2> &p_points) {
  71. _points = p_points;
  72. queue_redraw();
  73. }
  74. void Line2D::set_closed(bool p_closed) {
  75. _closed = p_closed;
  76. queue_redraw();
  77. }
  78. bool Line2D::is_closed() const {
  79. return _closed;
  80. }
  81. void Line2D::set_width(float p_width) {
  82. if (p_width < 0.0) {
  83. p_width = 0.0;
  84. }
  85. _width = p_width;
  86. queue_redraw();
  87. }
  88. float Line2D::get_width() const {
  89. return _width;
  90. }
  91. void Line2D::set_curve(const Ref<Curve> &p_curve) {
  92. if (_curve.is_valid()) {
  93. _curve->disconnect_changed(callable_mp(this, &Line2D::_curve_changed));
  94. }
  95. _curve = p_curve;
  96. if (_curve.is_valid()) {
  97. _curve->connect_changed(callable_mp(this, &Line2D::_curve_changed));
  98. }
  99. queue_redraw();
  100. }
  101. Ref<Curve> Line2D::get_curve() const {
  102. return _curve;
  103. }
  104. Vector<Vector2> Line2D::get_points() const {
  105. return _points;
  106. }
  107. void Line2D::set_point_position(int i, Vector2 p_pos) {
  108. ERR_FAIL_INDEX(i, _points.size());
  109. _points.set(i, p_pos);
  110. queue_redraw();
  111. }
  112. Vector2 Line2D::get_point_position(int i) const {
  113. ERR_FAIL_INDEX_V(i, _points.size(), Vector2());
  114. return _points.get(i);
  115. }
  116. int Line2D::get_point_count() const {
  117. return _points.size();
  118. }
  119. void Line2D::clear_points() {
  120. int count = _points.size();
  121. if (count > 0) {
  122. _points.clear();
  123. queue_redraw();
  124. }
  125. }
  126. void Line2D::add_point(Vector2 p_pos, int p_atpos) {
  127. if (p_atpos < 0 || _points.size() < p_atpos) {
  128. _points.push_back(p_pos);
  129. } else {
  130. _points.insert(p_atpos, p_pos);
  131. }
  132. queue_redraw();
  133. }
  134. void Line2D::remove_point(int i) {
  135. _points.remove_at(i);
  136. queue_redraw();
  137. }
  138. void Line2D::set_default_color(Color p_color) {
  139. _default_color = p_color;
  140. queue_redraw();
  141. }
  142. Color Line2D::get_default_color() const {
  143. return _default_color;
  144. }
  145. void Line2D::set_gradient(const Ref<Gradient> &p_gradient) {
  146. if (_gradient.is_valid()) {
  147. _gradient->disconnect_changed(callable_mp(this, &Line2D::_gradient_changed));
  148. }
  149. _gradient = p_gradient;
  150. if (_gradient.is_valid()) {
  151. _gradient->connect_changed(callable_mp(this, &Line2D::_gradient_changed));
  152. }
  153. queue_redraw();
  154. }
  155. Ref<Gradient> Line2D::get_gradient() const {
  156. return _gradient;
  157. }
  158. void Line2D::set_texture(const Ref<Texture2D> &p_texture) {
  159. _texture = p_texture;
  160. queue_redraw();
  161. }
  162. Ref<Texture2D> Line2D::get_texture() const {
  163. return _texture;
  164. }
  165. void Line2D::set_texture_mode(const LineTextureMode p_mode) {
  166. _texture_mode = p_mode;
  167. queue_redraw();
  168. }
  169. Line2D::LineTextureMode Line2D::get_texture_mode() const {
  170. return _texture_mode;
  171. }
  172. void Line2D::set_joint_mode(LineJointMode p_mode) {
  173. _joint_mode = p_mode;
  174. queue_redraw();
  175. }
  176. Line2D::LineJointMode Line2D::get_joint_mode() const {
  177. return _joint_mode;
  178. }
  179. void Line2D::set_begin_cap_mode(LineCapMode p_mode) {
  180. _begin_cap_mode = p_mode;
  181. queue_redraw();
  182. }
  183. Line2D::LineCapMode Line2D::get_begin_cap_mode() const {
  184. return _begin_cap_mode;
  185. }
  186. void Line2D::set_end_cap_mode(LineCapMode p_mode) {
  187. _end_cap_mode = p_mode;
  188. queue_redraw();
  189. }
  190. Line2D::LineCapMode Line2D::get_end_cap_mode() const {
  191. return _end_cap_mode;
  192. }
  193. void Line2D::_notification(int p_what) {
  194. switch (p_what) {
  195. case NOTIFICATION_DRAW: {
  196. _draw();
  197. } break;
  198. }
  199. }
  200. void Line2D::set_sharp_limit(float p_limit) {
  201. if (p_limit < 0.f) {
  202. p_limit = 0.f;
  203. }
  204. _sharp_limit = p_limit;
  205. queue_redraw();
  206. }
  207. float Line2D::get_sharp_limit() const {
  208. return _sharp_limit;
  209. }
  210. void Line2D::set_round_precision(int p_precision) {
  211. _round_precision = MAX(1, p_precision);
  212. queue_redraw();
  213. }
  214. int Line2D::get_round_precision() const {
  215. return _round_precision;
  216. }
  217. void Line2D::set_antialiased(bool p_antialiased) {
  218. _antialiased = p_antialiased;
  219. queue_redraw();
  220. }
  221. bool Line2D::get_antialiased() const {
  222. return _antialiased;
  223. }
  224. void Line2D::_draw() {
  225. int len = _points.size();
  226. if (len <= 1 || _width == 0.f) {
  227. return;
  228. }
  229. // TODO Maybe have it as member rather than copying parameters and allocating memory?
  230. LineBuilder lb;
  231. lb.points = _points;
  232. lb.closed = _closed;
  233. lb.default_color = _default_color;
  234. lb.gradient = *_gradient;
  235. lb.texture_mode = _texture_mode;
  236. lb.joint_mode = _joint_mode;
  237. lb.begin_cap_mode = _begin_cap_mode;
  238. lb.end_cap_mode = _end_cap_mode;
  239. lb.round_precision = _round_precision;
  240. lb.sharp_limit = _sharp_limit;
  241. lb.width = _width;
  242. lb.curve = *_curve;
  243. RID texture_rid;
  244. if (_texture.is_valid()) {
  245. texture_rid = _texture->get_rid();
  246. lb.tile_aspect = _texture->get_size().aspect();
  247. }
  248. lb.build();
  249. RS::get_singleton()->canvas_item_add_triangle_array(
  250. get_canvas_item(),
  251. lb.indices,
  252. lb.vertices,
  253. lb.colors,
  254. lb.uvs, Vector<int>(), Vector<float>(),
  255. texture_rid);
  256. // DEBUG: Draw wireframe
  257. // if (lb.indices.size() % 3 == 0) {
  258. // Color col(0, 0, 0);
  259. // for (int i = 0; i < lb.indices.size(); i += 3) {
  260. // Vector2 a = lb.vertices[lb.indices[i]];
  261. // Vector2 b = lb.vertices[lb.indices[i+1]];
  262. // Vector2 c = lb.vertices[lb.indices[i+2]];
  263. // draw_line(a, b, col);
  264. // draw_line(b, c, col);
  265. // draw_line(c, a, col);
  266. // }
  267. // for (int i = 0; i < lb.vertices.size(); ++i) {
  268. // Vector2 p = lb.vertices[i];
  269. // draw_rect(Rect2(p.x - 1, p.y - 1, 2, 2), Color(0, 0, 0, 0.5));
  270. // }
  271. // }
  272. }
  273. void Line2D::_gradient_changed() {
  274. queue_redraw();
  275. }
  276. void Line2D::_curve_changed() {
  277. queue_redraw();
  278. }
  279. // static
  280. void Line2D::_bind_methods() {
  281. ClassDB::bind_method(D_METHOD("set_points", "points"), &Line2D::set_points);
  282. ClassDB::bind_method(D_METHOD("get_points"), &Line2D::get_points);
  283. ClassDB::bind_method(D_METHOD("set_point_position", "index", "position"), &Line2D::set_point_position);
  284. ClassDB::bind_method(D_METHOD("get_point_position", "index"), &Line2D::get_point_position);
  285. ClassDB::bind_method(D_METHOD("get_point_count"), &Line2D::get_point_count);
  286. ClassDB::bind_method(D_METHOD("add_point", "position", "index"), &Line2D::add_point, DEFVAL(-1));
  287. ClassDB::bind_method(D_METHOD("remove_point", "index"), &Line2D::remove_point);
  288. ClassDB::bind_method(D_METHOD("clear_points"), &Line2D::clear_points);
  289. ClassDB::bind_method(D_METHOD("set_closed", "closed"), &Line2D::set_closed);
  290. ClassDB::bind_method(D_METHOD("is_closed"), &Line2D::is_closed);
  291. ClassDB::bind_method(D_METHOD("set_width", "width"), &Line2D::set_width);
  292. ClassDB::bind_method(D_METHOD("get_width"), &Line2D::get_width);
  293. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Line2D::set_curve);
  294. ClassDB::bind_method(D_METHOD("get_curve"), &Line2D::get_curve);
  295. ClassDB::bind_method(D_METHOD("set_default_color", "color"), &Line2D::set_default_color);
  296. ClassDB::bind_method(D_METHOD("get_default_color"), &Line2D::get_default_color);
  297. ClassDB::bind_method(D_METHOD("set_gradient", "color"), &Line2D::set_gradient);
  298. ClassDB::bind_method(D_METHOD("get_gradient"), &Line2D::get_gradient);
  299. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Line2D::set_texture);
  300. ClassDB::bind_method(D_METHOD("get_texture"), &Line2D::get_texture);
  301. ClassDB::bind_method(D_METHOD("set_texture_mode", "mode"), &Line2D::set_texture_mode);
  302. ClassDB::bind_method(D_METHOD("get_texture_mode"), &Line2D::get_texture_mode);
  303. ClassDB::bind_method(D_METHOD("set_joint_mode", "mode"), &Line2D::set_joint_mode);
  304. ClassDB::bind_method(D_METHOD("get_joint_mode"), &Line2D::get_joint_mode);
  305. ClassDB::bind_method(D_METHOD("set_begin_cap_mode", "mode"), &Line2D::set_begin_cap_mode);
  306. ClassDB::bind_method(D_METHOD("get_begin_cap_mode"), &Line2D::get_begin_cap_mode);
  307. ClassDB::bind_method(D_METHOD("set_end_cap_mode", "mode"), &Line2D::set_end_cap_mode);
  308. ClassDB::bind_method(D_METHOD("get_end_cap_mode"), &Line2D::get_end_cap_mode);
  309. ClassDB::bind_method(D_METHOD("set_sharp_limit", "limit"), &Line2D::set_sharp_limit);
  310. ClassDB::bind_method(D_METHOD("get_sharp_limit"), &Line2D::get_sharp_limit);
  311. ClassDB::bind_method(D_METHOD("set_round_precision", "precision"), &Line2D::set_round_precision);
  312. ClassDB::bind_method(D_METHOD("get_round_precision"), &Line2D::get_round_precision);
  313. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &Line2D::set_antialiased);
  314. ClassDB::bind_method(D_METHOD("get_antialiased"), &Line2D::get_antialiased);
  315. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "points"), "set_points", "get_points");
  316. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
  317. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width", PROPERTY_HINT_NONE, "suffix:px"), "set_width", "get_width");
  318. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "width_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  319. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "default_color"), "set_default_color", "get_default_color");
  320. ADD_GROUP("Fill", "");
  321. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_gradient", "get_gradient");
  322. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  323. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_mode", PROPERTY_HINT_ENUM, "None,Tile,Stretch"), "set_texture_mode", "get_texture_mode");
  324. ADD_GROUP("Capping", "");
  325. ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_mode", PROPERTY_HINT_ENUM, "Sharp,Bevel,Round"), "set_joint_mode", "get_joint_mode");
  326. ADD_PROPERTY(PropertyInfo(Variant::INT, "begin_cap_mode", PROPERTY_HINT_ENUM, "None,Box,Round"), "set_begin_cap_mode", "get_begin_cap_mode");
  327. ADD_PROPERTY(PropertyInfo(Variant::INT, "end_cap_mode", PROPERTY_HINT_ENUM, "None,Box,Round"), "set_end_cap_mode", "get_end_cap_mode");
  328. ADD_GROUP("Border", "");
  329. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sharp_limit"), "set_sharp_limit", "get_sharp_limit");
  330. ADD_PROPERTY(PropertyInfo(Variant::INT, "round_precision", PROPERTY_HINT_RANGE, "1,32,1"), "set_round_precision", "get_round_precision");
  331. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased");
  332. BIND_ENUM_CONSTANT(LINE_JOINT_SHARP);
  333. BIND_ENUM_CONSTANT(LINE_JOINT_BEVEL);
  334. BIND_ENUM_CONSTANT(LINE_JOINT_ROUND);
  335. BIND_ENUM_CONSTANT(LINE_CAP_NONE);
  336. BIND_ENUM_CONSTANT(LINE_CAP_BOX);
  337. BIND_ENUM_CONSTANT(LINE_CAP_ROUND);
  338. BIND_ENUM_CONSTANT(LINE_TEXTURE_NONE);
  339. BIND_ENUM_CONSTANT(LINE_TEXTURE_TILE);
  340. BIND_ENUM_CONSTANT(LINE_TEXTURE_STRETCH);
  341. }