1 /*************************************************************************/
2 /*  line_2d.cpp                                                          */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #include "line_2d.h"
32 #include "line_builder.h"
33 
34 #include "core/core_string_names.h"
35 
36 // Needed so we can bind functions
37 VARIANT_ENUM_CAST(Line2D::LineJointMode)
VARIANT_ENUM_CAST(Line2D::LineCapMode)38 VARIANT_ENUM_CAST(Line2D::LineCapMode)
39 VARIANT_ENUM_CAST(Line2D::LineTextureMode)
40 
41 Line2D::Line2D() {
42 	_joint_mode = LINE_JOINT_SHARP;
43 	_begin_cap_mode = LINE_CAP_NONE;
44 	_end_cap_mode = LINE_CAP_NONE;
45 	_width = 10;
46 	_default_color = Color(0.4, 0.5, 1);
47 	_texture_mode = LINE_TEXTURE_NONE;
48 	_sharp_limit = 2.f;
49 	_round_precision = 8;
50 	_antialiased = false;
51 }
52 
53 #ifdef TOOLS_ENABLED
_edit_get_rect() const54 Rect2 Line2D::_edit_get_rect() const {
55 
56 	if (_points.size() == 0)
57 		return Rect2(0, 0, 0, 0);
58 	Vector2 d = Vector2(_width, _width);
59 	Rect2 aabb = Rect2(_points[0] - d, 2 * d);
60 	for (int i = 1; i < _points.size(); i++) {
61 		aabb.expand_to(_points[i] - d);
62 		aabb.expand_to(_points[i] + d);
63 	}
64 	return aabb;
65 }
66 
_edit_use_rect() const67 bool Line2D::_edit_use_rect() const {
68 	return true;
69 }
70 
_edit_is_selected_on_click(const Point2 & p_point,double p_tolerance) const71 bool Line2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
72 
73 	const real_t d = _width / 2 + p_tolerance;
74 	PoolVector<Vector2>::Read points = _points.read();
75 	for (int i = 0; i < _points.size() - 1; i++) {
76 		Vector2 p = Geometry::get_closest_point_to_segment_2d(p_point, &points[i]);
77 		if (p.distance_to(p_point) <= d)
78 			return true;
79 	}
80 
81 	return false;
82 }
83 #endif
84 
set_points(const PoolVector<Vector2> & p_points)85 void Line2D::set_points(const PoolVector<Vector2> &p_points) {
86 	_points = p_points;
87 	update();
88 }
89 
set_width(float p_width)90 void Line2D::set_width(float p_width) {
91 	if (p_width < 0.0)
92 		p_width = 0.0;
93 	_width = p_width;
94 	update();
95 }
96 
get_width() const97 float Line2D::get_width() const {
98 	return _width;
99 }
100 
set_curve(const Ref<Curve> & p_curve)101 void Line2D::set_curve(const Ref<Curve> &p_curve) {
102 	// Cleanup previous connection if any
103 	if (_curve.is_valid()) {
104 		_curve->disconnect(CoreStringNames::get_singleton()->changed, this, "_curve_changed");
105 	}
106 
107 	_curve = p_curve;
108 
109 	// Connect to the curve so the line will update when it is changed
110 	if (_curve.is_valid()) {
111 		_curve->connect(CoreStringNames::get_singleton()->changed, this, "_curve_changed");
112 	}
113 
114 	update();
115 }
116 
get_curve() const117 Ref<Curve> Line2D::get_curve() const {
118 	return _curve;
119 }
120 
get_points() const121 PoolVector<Vector2> Line2D::get_points() const {
122 	return _points;
123 }
124 
set_point_position(int i,Vector2 p_pos)125 void Line2D::set_point_position(int i, Vector2 p_pos) {
126 	_points.set(i, p_pos);
127 	update();
128 }
129 
get_point_position(int i) const130 Vector2 Line2D::get_point_position(int i) const {
131 	ERR_FAIL_INDEX_V(i, _points.size(), Vector2());
132 	return _points.get(i);
133 }
134 
get_point_count() const135 int Line2D::get_point_count() const {
136 	return _points.size();
137 }
138 
clear_points()139 void Line2D::clear_points() {
140 	int count = _points.size();
141 	if (count > 0) {
142 		_points.resize(0);
143 		update();
144 	}
145 }
146 
add_point(Vector2 p_pos,int p_atpos)147 void Line2D::add_point(Vector2 p_pos, int p_atpos) {
148 	if (p_atpos < 0 || _points.size() < p_atpos) {
149 		_points.append(p_pos);
150 	} else {
151 		_points.insert(p_atpos, p_pos);
152 	}
153 	update();
154 }
155 
remove_point(int i)156 void Line2D::remove_point(int i) {
157 	_points.remove(i);
158 	update();
159 }
160 
set_default_color(Color p_color)161 void Line2D::set_default_color(Color p_color) {
162 	_default_color = p_color;
163 	update();
164 }
165 
get_default_color() const166 Color Line2D::get_default_color() const {
167 	return _default_color;
168 }
169 
set_gradient(const Ref<Gradient> & p_gradient)170 void Line2D::set_gradient(const Ref<Gradient> &p_gradient) {
171 
172 	// Cleanup previous connection if any
173 	if (_gradient.is_valid()) {
174 		_gradient->disconnect(CoreStringNames::get_singleton()->changed, this, "_gradient_changed");
175 	}
176 
177 	_gradient = p_gradient;
178 
179 	// Connect to the gradient so the line will update when the ColorRamp is changed
180 	if (_gradient.is_valid()) {
181 		_gradient->connect(CoreStringNames::get_singleton()->changed, this, "_gradient_changed");
182 	}
183 
184 	update();
185 }
186 
get_gradient() const187 Ref<Gradient> Line2D::get_gradient() const {
188 	return _gradient;
189 }
190 
set_texture(const Ref<Texture> & p_texture)191 void Line2D::set_texture(const Ref<Texture> &p_texture) {
192 	_texture = p_texture;
193 	update();
194 }
195 
get_texture() const196 Ref<Texture> Line2D::get_texture() const {
197 	return _texture;
198 }
199 
set_texture_mode(const LineTextureMode p_mode)200 void Line2D::set_texture_mode(const LineTextureMode p_mode) {
201 	_texture_mode = p_mode;
202 	update();
203 }
204 
get_texture_mode() const205 Line2D::LineTextureMode Line2D::get_texture_mode() const {
206 	return _texture_mode;
207 }
208 
set_joint_mode(LineJointMode p_mode)209 void Line2D::set_joint_mode(LineJointMode p_mode) {
210 	_joint_mode = p_mode;
211 	update();
212 }
213 
get_joint_mode() const214 Line2D::LineJointMode Line2D::get_joint_mode() const {
215 	return _joint_mode;
216 }
217 
set_begin_cap_mode(LineCapMode p_mode)218 void Line2D::set_begin_cap_mode(LineCapMode p_mode) {
219 	_begin_cap_mode = p_mode;
220 	update();
221 }
222 
get_begin_cap_mode() const223 Line2D::LineCapMode Line2D::get_begin_cap_mode() const {
224 	return _begin_cap_mode;
225 }
226 
set_end_cap_mode(LineCapMode p_mode)227 void Line2D::set_end_cap_mode(LineCapMode p_mode) {
228 	_end_cap_mode = p_mode;
229 	update();
230 }
231 
get_end_cap_mode() const232 Line2D::LineCapMode Line2D::get_end_cap_mode() const {
233 	return _end_cap_mode;
234 }
235 
_notification(int p_what)236 void Line2D::_notification(int p_what) {
237 	switch (p_what) {
238 		case NOTIFICATION_DRAW:
239 			_draw();
240 			break;
241 	}
242 }
243 
set_sharp_limit(float p_limit)244 void Line2D::set_sharp_limit(float p_limit) {
245 	if (p_limit < 0.f)
246 		p_limit = 0.f;
247 	_sharp_limit = p_limit;
248 	update();
249 }
250 
get_sharp_limit() const251 float Line2D::get_sharp_limit() const {
252 	return _sharp_limit;
253 }
254 
set_round_precision(int p_precision)255 void Line2D::set_round_precision(int p_precision) {
256 	if (p_precision < 1)
257 		p_precision = 1;
258 	_round_precision = p_precision;
259 	update();
260 }
261 
get_round_precision() const262 int Line2D::get_round_precision() const {
263 	return _round_precision;
264 }
265 
set_antialiased(bool p_antialiased)266 void Line2D::set_antialiased(bool p_antialiased) {
267 	_antialiased = p_antialiased;
268 	update();
269 }
270 
get_antialiased() const271 bool Line2D::get_antialiased() const {
272 	return _antialiased;
273 }
274 
_draw()275 void Line2D::_draw() {
276 	if (_points.size() <= 1 || _width == 0.f)
277 		return;
278 
279 	// TODO Is this really needed?
280 	// Copy points for faster access
281 	Vector<Vector2> points;
282 	points.resize(_points.size());
283 	int len = points.size();
284 	{
285 		PoolVector<Vector2>::Read points_read = _points.read();
286 		for (int i = 0; i < len; ++i) {
287 			points.write[i] = points_read[i];
288 		}
289 	}
290 
291 	// TODO Maybe have it as member rather than copying parameters and allocating memory?
292 	LineBuilder lb;
293 	lb.points = points;
294 	lb.default_color = _default_color;
295 	lb.gradient = *_gradient;
296 	lb.texture_mode = _texture_mode;
297 	lb.joint_mode = _joint_mode;
298 	lb.begin_cap_mode = _begin_cap_mode;
299 	lb.end_cap_mode = _end_cap_mode;
300 	lb.round_precision = _round_precision;
301 	lb.sharp_limit = _sharp_limit;
302 	lb.width = _width;
303 	lb.curve = *_curve;
304 
305 	RID texture_rid;
306 	if (_texture.is_valid()) {
307 		texture_rid = _texture->get_rid();
308 
309 		lb.tile_aspect = _texture->get_size().aspect();
310 	}
311 
312 	lb.build();
313 
314 	VS::get_singleton()->canvas_item_add_triangle_array(
315 			get_canvas_item(),
316 			lb.indices,
317 			lb.vertices,
318 			lb.colors,
319 			lb.uvs, Vector<int>(), Vector<float>(),
320 			texture_rid, -1, RID(),
321 			_antialiased, true);
322 
323 	// DEBUG
324 	// Draw wireframe
325 	//	if(lb.indices.size() % 3 == 0) {
326 	//		Color col(0,0,0);
327 	//		for(int i = 0; i < lb.indices.size(); i += 3) {
328 	//			int vi = lb.indices[i];
329 	//			int lbvsize = lb.vertices.size();
330 	//			Vector2 a = lb.vertices[lb.indices[i]];
331 	//			Vector2 b = lb.vertices[lb.indices[i+1]];
332 	//			Vector2 c = lb.vertices[lb.indices[i+2]];
333 	//			draw_line(a, b, col);
334 	//			draw_line(b, c, col);
335 	//			draw_line(c, a, col);
336 	//		}
337 	//		for(int i = 0; i < lb.vertices.size(); ++i) {
338 	//			Vector2 p = lb.vertices[i];
339 	//			draw_rect(Rect2(p.x-1, p.y-1, 2, 2), Color(0,0,0,0.5));
340 	//		}
341 	//	}
342 }
343 
_gradient_changed()344 void Line2D::_gradient_changed() {
345 	update();
346 }
347 
_curve_changed()348 void Line2D::_curve_changed() {
349 	update();
350 }
351 
352 // static
_bind_methods()353 void Line2D::_bind_methods() {
354 
355 	ClassDB::bind_method(D_METHOD("set_points", "points"), &Line2D::set_points);
356 	ClassDB::bind_method(D_METHOD("get_points"), &Line2D::get_points);
357 
358 	ClassDB::bind_method(D_METHOD("set_point_position", "i", "position"), &Line2D::set_point_position);
359 	ClassDB::bind_method(D_METHOD("get_point_position", "i"), &Line2D::get_point_position);
360 
361 	ClassDB::bind_method(D_METHOD("get_point_count"), &Line2D::get_point_count);
362 
363 	ClassDB::bind_method(D_METHOD("add_point", "position", "at_position"), &Line2D::add_point, DEFVAL(-1));
364 	ClassDB::bind_method(D_METHOD("remove_point", "i"), &Line2D::remove_point);
365 
366 	ClassDB::bind_method(D_METHOD("clear_points"), &Line2D::clear_points);
367 
368 	ClassDB::bind_method(D_METHOD("set_width", "width"), &Line2D::set_width);
369 	ClassDB::bind_method(D_METHOD("get_width"), &Line2D::get_width);
370 
371 	ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Line2D::set_curve);
372 	ClassDB::bind_method(D_METHOD("get_curve"), &Line2D::get_curve);
373 
374 	ClassDB::bind_method(D_METHOD("set_default_color", "color"), &Line2D::set_default_color);
375 	ClassDB::bind_method(D_METHOD("get_default_color"), &Line2D::get_default_color);
376 
377 	ClassDB::bind_method(D_METHOD("set_gradient", "color"), &Line2D::set_gradient);
378 	ClassDB::bind_method(D_METHOD("get_gradient"), &Line2D::get_gradient);
379 
380 	ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Line2D::set_texture);
381 	ClassDB::bind_method(D_METHOD("get_texture"), &Line2D::get_texture);
382 
383 	ClassDB::bind_method(D_METHOD("set_texture_mode", "mode"), &Line2D::set_texture_mode);
384 	ClassDB::bind_method(D_METHOD("get_texture_mode"), &Line2D::get_texture_mode);
385 
386 	ClassDB::bind_method(D_METHOD("set_joint_mode", "mode"), &Line2D::set_joint_mode);
387 	ClassDB::bind_method(D_METHOD("get_joint_mode"), &Line2D::get_joint_mode);
388 
389 	ClassDB::bind_method(D_METHOD("set_begin_cap_mode", "mode"), &Line2D::set_begin_cap_mode);
390 	ClassDB::bind_method(D_METHOD("get_begin_cap_mode"), &Line2D::get_begin_cap_mode);
391 
392 	ClassDB::bind_method(D_METHOD("set_end_cap_mode", "mode"), &Line2D::set_end_cap_mode);
393 	ClassDB::bind_method(D_METHOD("get_end_cap_mode"), &Line2D::get_end_cap_mode);
394 
395 	ClassDB::bind_method(D_METHOD("set_sharp_limit", "limit"), &Line2D::set_sharp_limit);
396 	ClassDB::bind_method(D_METHOD("get_sharp_limit"), &Line2D::get_sharp_limit);
397 
398 	ClassDB::bind_method(D_METHOD("set_round_precision", "precision"), &Line2D::set_round_precision);
399 	ClassDB::bind_method(D_METHOD("get_round_precision"), &Line2D::get_round_precision);
400 
401 	ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &Line2D::set_antialiased);
402 	ClassDB::bind_method(D_METHOD("get_antialiased"), &Line2D::get_antialiased);
403 
404 	ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "points"), "set_points", "get_points");
405 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "width"), "set_width", "get_width");
406 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "width_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
407 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "default_color"), "set_default_color", "get_default_color");
408 	ADD_GROUP("Fill", "");
409 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_gradient", "get_gradient");
410 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
411 	ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_mode", PROPERTY_HINT_ENUM, "None,Tile,Stretch"), "set_texture_mode", "get_texture_mode");
412 	ADD_GROUP("Capping", "");
413 	ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_mode", PROPERTY_HINT_ENUM, "Sharp,Bevel,Round"), "set_joint_mode", "get_joint_mode");
414 	ADD_PROPERTY(PropertyInfo(Variant::INT, "begin_cap_mode", PROPERTY_HINT_ENUM, "None,Box,Round"), "set_begin_cap_mode", "get_begin_cap_mode");
415 	ADD_PROPERTY(PropertyInfo(Variant::INT, "end_cap_mode", PROPERTY_HINT_ENUM, "None,Box,Round"), "set_end_cap_mode", "get_end_cap_mode");
416 	ADD_GROUP("Border", "");
417 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "sharp_limit"), "set_sharp_limit", "get_sharp_limit");
418 	ADD_PROPERTY(PropertyInfo(Variant::INT, "round_precision"), "set_round_precision", "get_round_precision");
419 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased");
420 
421 	BIND_ENUM_CONSTANT(LINE_JOINT_SHARP);
422 	BIND_ENUM_CONSTANT(LINE_JOINT_BEVEL);
423 	BIND_ENUM_CONSTANT(LINE_JOINT_ROUND);
424 
425 	BIND_ENUM_CONSTANT(LINE_CAP_NONE);
426 	BIND_ENUM_CONSTANT(LINE_CAP_BOX);
427 	BIND_ENUM_CONSTANT(LINE_CAP_ROUND);
428 
429 	BIND_ENUM_CONSTANT(LINE_TEXTURE_NONE);
430 	BIND_ENUM_CONSTANT(LINE_TEXTURE_TILE);
431 	BIND_ENUM_CONSTANT(LINE_TEXTURE_STRETCH);
432 
433 	ClassDB::bind_method(D_METHOD("_gradient_changed"), &Line2D::_gradient_changed);
434 	ClassDB::bind_method(D_METHOD("_curve_changed"), &Line2D::_curve_changed);
435 }
436