line_2d.cpp 14 KB

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