line_2d.cpp 11 KB

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