diff options
Diffstat (limited to 'scene/2d/line_2d.cpp')
-rw-r--r-- | scene/2d/line_2d.cpp | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/scene/2d/line_2d.cpp b/scene/2d/line_2d.cpp index c45eab70df..37eb45c21d 100644 --- a/scene/2d/line_2d.cpp +++ b/scene/2d/line_2d.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,6 +31,7 @@ #include "line_2d.h" #include "core/core_string_names.h" +#include "core/math/geometry_2d.h" #include "line_builder.h" // Needed so we can bind functions @@ -39,22 +40,13 @@ VARIANT_ENUM_CAST(Line2D::LineCapMode) VARIANT_ENUM_CAST(Line2D::LineTextureMode) Line2D::Line2D() { - _joint_mode = LINE_JOINT_SHARP; - _begin_cap_mode = LINE_CAP_NONE; - _end_cap_mode = LINE_CAP_NONE; - _width = 10; - _default_color = Color(0.4, 0.5, 1); - _texture_mode = LINE_TEXTURE_NONE; - _sharp_limit = 2.f; - _round_precision = 8; - _antialiased = false; } #ifdef TOOLS_ENABLED Rect2 Line2D::_edit_get_rect() const { - - if (_points.size() == 0) + if (_points.size() == 0) { return Rect2(0, 0, 0, 0); + } Vector2 d = Vector2(_width, _width); Rect2 aabb = Rect2(_points[0] - d, 2 * d); for (int i = 1; i < _points.size(); i++) { @@ -69,13 +61,13 @@ bool Line2D::_edit_use_rect() const { } bool Line2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { - const real_t d = _width / 2 + p_tolerance; const Vector2 *points = _points.ptr(); for (int i = 0; i < _points.size() - 1; i++) { - Vector2 p = Geometry::get_closest_point_to_segment_2d(p_point, &points[i]); - if (p.distance_to(p_point) <= d) + Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, &points[i]); + if (p.distance_to(p_point) <= d) { return true; + } } return false; @@ -88,8 +80,9 @@ void Line2D::set_points(const Vector<Vector2> &p_points) { } void Line2D::set_width(float p_width) { - if (p_width < 0.0) + if (p_width < 0.0) { p_width = 0.0; + } _width = p_width; update(); } @@ -123,6 +116,7 @@ Vector<Vector2> Line2D::get_points() const { } void Line2D::set_point_position(int i, Vector2 p_pos) { + ERR_FAIL_INDEX(i, _points.size()); _points.set(i, p_pos); update(); } @@ -168,7 +162,6 @@ Color Line2D::get_default_color() const { } void Line2D::set_gradient(const Ref<Gradient> &p_gradient) { - // Cleanup previous connection if any if (_gradient.is_valid()) { _gradient->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Line2D::_gradient_changed)); @@ -176,7 +169,7 @@ void Line2D::set_gradient(const Ref<Gradient> &p_gradient) { _gradient = p_gradient; - // Connect to the gradient so the line will update when the ColorRamp is changed + // Connect to the gradient so the line will update when the Gradient is changed if (_gradient.is_valid()) { _gradient->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Line2D::_gradient_changed)); } @@ -242,8 +235,9 @@ void Line2D::_notification(int p_what) { } void Line2D::set_sharp_limit(float p_limit) { - if (p_limit < 0.f) + if (p_limit < 0.f) { p_limit = 0.f; + } _sharp_limit = p_limit; update(); } @@ -253,8 +247,9 @@ float Line2D::get_sharp_limit() const { } void Line2D::set_round_precision(int p_precision) { - if (p_precision < 1) + if (p_precision < 1) { p_precision = 1; + } _round_precision = p_precision; update(); } @@ -273,8 +268,9 @@ bool Line2D::get_antialiased() const { } void Line2D::_draw() { - if (_points.size() <= 1 || _width == 0.f) + if (_points.size() <= 1 || _width == 0.f) { return; + } // TODO Is this really needed? // Copy points for faster access @@ -350,7 +346,6 @@ void Line2D::_curve_changed() { // static void Line2D::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_points", "points"), &Line2D::set_points); ClassDB::bind_method(D_METHOD("get_points"), &Line2D::get_points); |