line_2d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*************************************************************************/
  2. /* line_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. Node2D() {
  39. _joint_mode = LINE_JOINT_SHARP;
  40. _begin_cap_mode = LINE_CAP_NONE;
  41. _end_cap_mode = LINE_CAP_NONE;
  42. _width = 10;
  43. _default_color = Color(0.4, 0.5, 1);
  44. _sharp_limit = 2.f;
  45. _round_precision = 8;
  46. }
  47. Rect2 Line2D::_edit_get_rect() const {
  48. if (_points.size() == 0)
  49. return Rect2(0, 0, 0, 0);
  50. Vector2 d = Vector2(_width, _width);
  51. Rect2 aabb = Rect2(_points[0] - d, 2 * d);
  52. for (int i = 1; i < _points.size(); i++) {
  53. aabb.expand_to(_points[i] - d);
  54. aabb.expand_to(_points[i] + d);
  55. }
  56. return aabb;
  57. }
  58. bool Line2D::_edit_use_rect() const {
  59. return true;
  60. }
  61. bool Line2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  62. const real_t d = _width / 2 + p_tolerance;
  63. PoolVector<Vector2>::Read points = _points.read();
  64. for (int i = 0; i < _points.size() - 1; i++) {
  65. Vector2 p = Geometry::get_closest_point_to_segment_2d(p_point, &points[i]);
  66. if (p.distance_to(p_point) <= d)
  67. return true;
  68. }
  69. return false;
  70. }
  71. void Line2D::set_points(const PoolVector<Vector2> &p_points) {
  72. _points = p_points;
  73. update();
  74. }
  75. void Line2D::set_width(float width) {
  76. if (width < 0.0)
  77. width = 0.0;
  78. _width = width;
  79. update();
  80. }
  81. float Line2D::get_width() const {
  82. return _width;
  83. }
  84. PoolVector<Vector2> Line2D::get_points() const {
  85. return _points;
  86. }
  87. void Line2D::set_point_position(int i, Vector2 pos) {
  88. _points.set(i, pos);
  89. update();
  90. }
  91. Vector2 Line2D::get_point_position(int i) const {
  92. return _points.get(i);
  93. }
  94. int Line2D::get_point_count() const {
  95. return _points.size();
  96. }
  97. void Line2D::add_point(Vector2 pos) {
  98. _points.append(pos);
  99. update();
  100. }
  101. void Line2D::remove_point(int i) {
  102. _points.remove(i);
  103. update();
  104. }
  105. void Line2D::set_default_color(Color color) {
  106. _default_color = color;
  107. update();
  108. }
  109. Color Line2D::get_default_color() const {
  110. return _default_color;
  111. }
  112. void Line2D::set_gradient(const Ref<Gradient> &gradient) {
  113. // Cleanup previous connection if any
  114. if (_gradient.is_valid()) {
  115. (**_gradient).disconnect(CoreStringNames::get_singleton()->changed, this, "_gradient_changed");
  116. }
  117. _gradient = gradient;
  118. // Connect to the gradient so the line will update when the ColorRamp is changed
  119. if (_gradient.is_valid()) {
  120. (**_gradient).connect(CoreStringNames::get_singleton()->changed, this, "_gradient_changed");
  121. }
  122. update();
  123. }
  124. Ref<Gradient> Line2D::get_gradient() const {
  125. return _gradient;
  126. }
  127. void Line2D::set_texture(const Ref<Texture> &texture) {
  128. _texture = texture;
  129. update();
  130. }
  131. Ref<Texture> Line2D::get_texture() const {
  132. return _texture;
  133. }
  134. void Line2D::set_texture_mode(const LineTextureMode mode) {
  135. _texture_mode = mode;
  136. update();
  137. }
  138. Line2D::LineTextureMode Line2D::get_texture_mode() const {
  139. return _texture_mode;
  140. }
  141. void Line2D::set_joint_mode(LineJointMode mode) {
  142. _joint_mode = mode;
  143. update();
  144. }
  145. Line2D::LineJointMode Line2D::get_joint_mode() const {
  146. return _joint_mode;
  147. }
  148. void Line2D::set_begin_cap_mode(LineCapMode mode) {
  149. _begin_cap_mode = mode;
  150. update();
  151. }
  152. Line2D::LineCapMode Line2D::get_begin_cap_mode() const {
  153. return _begin_cap_mode;
  154. }
  155. void Line2D::set_end_cap_mode(LineCapMode mode) {
  156. _end_cap_mode = mode;
  157. update();
  158. }
  159. Line2D::LineCapMode Line2D::get_end_cap_mode() const {
  160. return _end_cap_mode;
  161. }
  162. void Line2D::_notification(int p_what) {
  163. switch (p_what) {
  164. case NOTIFICATION_DRAW:
  165. _draw();
  166. break;
  167. }
  168. }
  169. void Line2D::set_sharp_limit(float limit) {
  170. if (limit < 0.f)
  171. limit = 0.f;
  172. _sharp_limit = limit;
  173. update();
  174. }
  175. float Line2D::get_sharp_limit() const {
  176. return _sharp_limit;
  177. }
  178. void Line2D::set_round_precision(int precision) {
  179. if (precision < 1)
  180. precision = 1;
  181. _round_precision = precision;
  182. update();
  183. }
  184. int Line2D::get_round_precision() const {
  185. return _round_precision;
  186. }
  187. void Line2D::_draw() {
  188. if (_points.size() <= 1 || _width == 0.f)
  189. return;
  190. // TODO Is this really needed?
  191. // Copy points for faster access
  192. Vector<Vector2> points;
  193. points.resize(_points.size());
  194. int len = points.size();
  195. {
  196. PoolVector<Vector2>::Read points_read = _points.read();
  197. for (int i = 0; i < len; ++i) {
  198. points.write[i] = points_read[i];
  199. }
  200. }
  201. // TODO Maybe have it as member rather than copying parameters and allocating memory?
  202. LineBuilder lb;
  203. lb.points = points;
  204. lb.default_color = _default_color;
  205. lb.gradient = *_gradient;
  206. lb.texture_mode = _texture_mode;
  207. lb.joint_mode = _joint_mode;
  208. lb.begin_cap_mode = _begin_cap_mode;
  209. lb.end_cap_mode = _end_cap_mode;
  210. lb.round_precision = _round_precision;
  211. lb.sharp_limit = _sharp_limit;
  212. lb.width = _width;
  213. RID texture_rid;
  214. if (_texture.is_valid()) {
  215. texture_rid = (**_texture).get_rid();
  216. lb.tile_aspect = _texture->get_size().aspect();
  217. }
  218. lb.build();
  219. VS::get_singleton()->canvas_item_add_triangle_array(
  220. get_canvas_item(),
  221. lb.indices,
  222. lb.vertices,
  223. lb.colors,
  224. lb.uvs, Vector<int>(), Vector<float>(),
  225. texture_rid);
  226. // DEBUG
  227. // Draw wireframe
  228. // if(lb.indices.size() % 3 == 0) {
  229. // Color col(0,0,0);
  230. // for(int i = 0; i < lb.indices.size(); i += 3) {
  231. // int vi = lb.indices[i];
  232. // int lbvsize = lb.vertices.size();
  233. // Vector2 a = lb.vertices[lb.indices[i]];
  234. // Vector2 b = lb.vertices[lb.indices[i+1]];
  235. // Vector2 c = lb.vertices[lb.indices[i+2]];
  236. // draw_line(a, b, col);
  237. // draw_line(b, c, col);
  238. // draw_line(c, a, col);
  239. // }
  240. // for(int i = 0; i < lb.vertices.size(); ++i) {
  241. // Vector2 p = lb.vertices[i];
  242. // draw_rect(Rect2(p.x-1, p.y-1, 2, 2), Color(0,0,0,0.5));
  243. // }
  244. // }
  245. }
  246. void Line2D::_gradient_changed() {
  247. update();
  248. }
  249. // static
  250. void Line2D::_bind_methods() {
  251. ClassDB::bind_method(D_METHOD("set_points", "points"), &Line2D::set_points);
  252. ClassDB::bind_method(D_METHOD("get_points"), &Line2D::get_points);
  253. ClassDB::bind_method(D_METHOD("set_point_position", "i", "position"), &Line2D::set_point_position);
  254. ClassDB::bind_method(D_METHOD("get_point_position", "i"), &Line2D::get_point_position);
  255. ClassDB::bind_method(D_METHOD("get_point_count"), &Line2D::get_point_count);
  256. ClassDB::bind_method(D_METHOD("add_point", "position"), &Line2D::add_point);
  257. ClassDB::bind_method(D_METHOD("remove_point", "i"), &Line2D::remove_point);
  258. ClassDB::bind_method(D_METHOD("set_width", "width"), &Line2D::set_width);
  259. ClassDB::bind_method(D_METHOD("get_width"), &Line2D::get_width);
  260. ClassDB::bind_method(D_METHOD("set_default_color", "color"), &Line2D::set_default_color);
  261. ClassDB::bind_method(D_METHOD("get_default_color"), &Line2D::get_default_color);
  262. ClassDB::bind_method(D_METHOD("set_gradient", "color"), &Line2D::set_gradient);
  263. ClassDB::bind_method(D_METHOD("get_gradient"), &Line2D::get_gradient);
  264. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Line2D::set_texture);
  265. ClassDB::bind_method(D_METHOD("get_texture"), &Line2D::get_texture);
  266. ClassDB::bind_method(D_METHOD("set_texture_mode", "mode"), &Line2D::set_texture_mode);
  267. ClassDB::bind_method(D_METHOD("get_texture_mode"), &Line2D::get_texture_mode);
  268. ClassDB::bind_method(D_METHOD("set_joint_mode", "mode"), &Line2D::set_joint_mode);
  269. ClassDB::bind_method(D_METHOD("get_joint_mode"), &Line2D::get_joint_mode);
  270. ClassDB::bind_method(D_METHOD("set_begin_cap_mode", "mode"), &Line2D::set_begin_cap_mode);
  271. ClassDB::bind_method(D_METHOD("get_begin_cap_mode"), &Line2D::get_begin_cap_mode);
  272. ClassDB::bind_method(D_METHOD("set_end_cap_mode", "mode"), &Line2D::set_end_cap_mode);
  273. ClassDB::bind_method(D_METHOD("get_end_cap_mode"), &Line2D::get_end_cap_mode);
  274. ClassDB::bind_method(D_METHOD("set_sharp_limit", "limit"), &Line2D::set_sharp_limit);
  275. ClassDB::bind_method(D_METHOD("get_sharp_limit"), &Line2D::get_sharp_limit);
  276. ClassDB::bind_method(D_METHOD("set_round_precision", "precision"), &Line2D::set_round_precision);
  277. ClassDB::bind_method(D_METHOD("get_round_precision"), &Line2D::get_round_precision);
  278. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "points"), "set_points", "get_points");
  279. ADD_PROPERTY(PropertyInfo(Variant::REAL, "width"), "set_width", "get_width");
  280. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "default_color"), "set_default_color", "get_default_color");
  281. ADD_GROUP("Fill", "");
  282. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_gradient", "get_gradient");
  283. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  284. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_mode", PROPERTY_HINT_ENUM, "None,Tile,Stretch"), "set_texture_mode", "get_texture_mode");
  285. ADD_GROUP("Capping", "");
  286. ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_mode", PROPERTY_HINT_ENUM, "Sharp,Bevel,Round"), "set_joint_mode", "get_joint_mode");
  287. ADD_PROPERTY(PropertyInfo(Variant::INT, "begin_cap_mode", PROPERTY_HINT_ENUM, "None,Box,Round"), "set_begin_cap_mode", "get_begin_cap_mode");
  288. ADD_PROPERTY(PropertyInfo(Variant::INT, "end_cap_mode", PROPERTY_HINT_ENUM, "None,Box,Round"), "set_end_cap_mode", "get_end_cap_mode");
  289. ADD_GROUP("Border", "");
  290. ADD_PROPERTY(PropertyInfo(Variant::REAL, "sharp_limit"), "set_sharp_limit", "get_sharp_limit");
  291. ADD_PROPERTY(PropertyInfo(Variant::INT, "round_precision"), "set_round_precision", "get_round_precision");
  292. BIND_ENUM_CONSTANT(LINE_JOINT_SHARP);
  293. BIND_ENUM_CONSTANT(LINE_JOINT_BEVEL);
  294. BIND_ENUM_CONSTANT(LINE_JOINT_ROUND);
  295. BIND_ENUM_CONSTANT(LINE_CAP_NONE);
  296. BIND_ENUM_CONSTANT(LINE_CAP_BOX);
  297. BIND_ENUM_CONSTANT(LINE_CAP_ROUND);
  298. BIND_ENUM_CONSTANT(LINE_TEXTURE_NONE);
  299. BIND_ENUM_CONSTANT(LINE_TEXTURE_TILE);
  300. BIND_ENUM_CONSTANT(LINE_TEXTURE_STRETCH);
  301. ClassDB::bind_method(D_METHOD("_gradient_changed"), &Line2D::_gradient_changed);
  302. }