line_2d.cpp 14 KB

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