line_2d.cpp 13 KB

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