diff options
Diffstat (limited to 'scene/resources')
-rw-r--r-- | scene/resources/capsule_shape_2d.cpp | 29 | ||||
-rw-r--r-- | scene/resources/capsule_shape_2d.h | 3 | ||||
-rw-r--r-- | scene/resources/circle_shape_2d.cpp | 6 | ||||
-rw-r--r-- | scene/resources/circle_shape_2d.h | 2 | ||||
-rw-r--r-- | scene/resources/concave_polygon_shape_2d.cpp | 17 | ||||
-rw-r--r-- | scene/resources/concave_polygon_shape_2d.h | 2 | ||||
-rw-r--r-- | scene/resources/convex_polygon_shape_2d.cpp | 5 | ||||
-rw-r--r-- | scene/resources/convex_polygon_shape_2d.h | 2 | ||||
-rw-r--r-- | scene/resources/segment_shape_2d.cpp | 7 | ||||
-rw-r--r-- | scene/resources/segment_shape_2d.h | 2 | ||||
-rw-r--r-- | scene/resources/shape_2d.h | 2 | ||||
-rw-r--r-- | scene/resources/shape_line_2d.cpp | 15 | ||||
-rw-r--r-- | scene/resources/shape_line_2d.h | 2 |
13 files changed, 85 insertions, 9 deletions
diff --git a/scene/resources/capsule_shape_2d.cpp b/scene/resources/capsule_shape_2d.cpp index 7b2dbf8e8c..2722ff7207 100644 --- a/scene/resources/capsule_shape_2d.cpp +++ b/scene/resources/capsule_shape_2d.cpp @@ -32,6 +32,25 @@ #include "servers/physics_2d_server.h" #include "servers/visual_server.h" +Vector<Vector2> CapsuleShape2D::_get_points() const { + + Vector<Vector2> points; + for (int i = 0; i < 24; i++) { + Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -get_height() * 0.5 : get_height() * 0.5); + + points.push_back(Vector2(Math::sin(i * Math_PI * 2 / 24.0), Math::cos(i * Math_PI * 2 / 24.0)) * get_radius() + ofs); + if (i == 6 || i == 18) + points.push_back(Vector2(Math::sin(i * Math_PI * 2 / 24.0), Math::cos(i * Math_PI * 2 / 24.0)) * get_radius() - ofs); + } + + return points; +} + +bool CapsuleShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { + + return Geometry::is_point_in_polygon(p_point, _get_points()); +} + void CapsuleShape2D::_update_shape() { Physics2DServer::get_singleton()->shape_set_data(get_rid(), Vector2(radius, height)); @@ -62,15 +81,7 @@ real_t CapsuleShape2D::get_height() const { void CapsuleShape2D::draw(const RID &p_to_rid, const Color &p_color) { - Vector<Vector2> points; - for (int i = 0; i < 24; i++) { - Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -get_height() * 0.5 : get_height() * 0.5); - - points.push_back(Vector2(Math::sin(i * Math_PI * 2 / 24.0), Math::cos(i * Math_PI * 2 / 24.0)) * get_radius() + ofs); - if (i == 6 || i == 18) - points.push_back(Vector2(Math::sin(i * Math_PI * 2 / 24.0), Math::cos(i * Math_PI * 2 / 24.0)) * get_radius() - ofs); - } - + Vector<Vector2> points = _get_points(); Vector<Color> col; col.push_back(p_color); VisualServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col); diff --git a/scene/resources/capsule_shape_2d.h b/scene/resources/capsule_shape_2d.h index 7ce6a4ed63..784f2c02c8 100644 --- a/scene/resources/capsule_shape_2d.h +++ b/scene/resources/capsule_shape_2d.h @@ -39,11 +39,14 @@ class CapsuleShape2D : public Shape2D { real_t radius; void _update_shape(); + Vector<Vector2> _get_points() const; protected: static void _bind_methods(); public: + virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const; + void set_height(real_t p_height); real_t get_height() const; diff --git a/scene/resources/circle_shape_2d.cpp b/scene/resources/circle_shape_2d.cpp index d9d0ddbe8f..669b1d8e7d 100644 --- a/scene/resources/circle_shape_2d.cpp +++ b/scene/resources/circle_shape_2d.cpp @@ -31,6 +31,12 @@ #include "servers/physics_2d_server.h" #include "servers/visual_server.h" + +bool CircleShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { + + return p_point.length() < get_radius() + p_tolerance; +} + void CircleShape2D::_update_shape() { Physics2DServer::get_singleton()->shape_set_data(get_rid(), radius); diff --git a/scene/resources/circle_shape_2d.h b/scene/resources/circle_shape_2d.h index f14f8f6776..3668521a55 100644 --- a/scene/resources/circle_shape_2d.h +++ b/scene/resources/circle_shape_2d.h @@ -42,6 +42,8 @@ protected: static void _bind_methods(); public: + virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const; + void set_radius(real_t p_radius); real_t get_radius() const; diff --git a/scene/resources/concave_polygon_shape_2d.cpp b/scene/resources/concave_polygon_shape_2d.cpp index 19985d4acf..69765796f6 100644 --- a/scene/resources/concave_polygon_shape_2d.cpp +++ b/scene/resources/concave_polygon_shape_2d.cpp @@ -32,6 +32,23 @@ #include "servers/physics_2d_server.h" #include "servers/visual_server.h" +bool ConcavePolygonShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { + + PoolVector<Vector2> s = get_segments(); + int len = s.size(); + if (len == 0 || (len % 2) == 1) + return false; + + PoolVector<Vector2>::Read r = s.read(); + for (int i = 0; i < len; i += 2) { + Vector2 closest = Geometry::get_closest_point_to_segment_2d(p_point, &r[i]); + if (p_point.distance_to(closest) < p_tolerance) + return true; + } + + return false; +} + void ConcavePolygonShape2D::set_segments(const PoolVector<Vector2> &p_segments) { Physics2DServer::get_singleton()->shape_set_data(get_rid(), p_segments); diff --git a/scene/resources/concave_polygon_shape_2d.h b/scene/resources/concave_polygon_shape_2d.h index 4a5defe72c..7ca14d4d66 100644 --- a/scene/resources/concave_polygon_shape_2d.h +++ b/scene/resources/concave_polygon_shape_2d.h @@ -39,6 +39,8 @@ protected: static void _bind_methods(); public: + virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const; + void set_segments(const PoolVector<Vector2> &p_segments); PoolVector<Vector2> get_segments() const; diff --git a/scene/resources/convex_polygon_shape_2d.cpp b/scene/resources/convex_polygon_shape_2d.cpp index 98ef2ca7c6..0402a70898 100644 --- a/scene/resources/convex_polygon_shape_2d.cpp +++ b/scene/resources/convex_polygon_shape_2d.cpp @@ -33,6 +33,11 @@ #include "servers/physics_2d_server.h" #include "servers/visual_server.h" +bool ConvexPolygonShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { + + return Geometry::is_point_in_polygon(p_point, points); +} + void ConvexPolygonShape2D::_update_shape() { Physics2DServer::get_singleton()->shape_set_data(get_rid(), points); diff --git a/scene/resources/convex_polygon_shape_2d.h b/scene/resources/convex_polygon_shape_2d.h index f492b7651b..320cf94a5e 100644 --- a/scene/resources/convex_polygon_shape_2d.h +++ b/scene/resources/convex_polygon_shape_2d.h @@ -42,6 +42,8 @@ protected: static void _bind_methods(); public: + virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const; + void set_point_cloud(const Vector<Vector2> &p_points); void set_points(const Vector<Vector2> &p_points); Vector<Vector2> get_points() const; diff --git a/scene/resources/segment_shape_2d.cpp b/scene/resources/segment_shape_2d.cpp index 4682ef5cb3..346599b25a 100644 --- a/scene/resources/segment_shape_2d.cpp +++ b/scene/resources/segment_shape_2d.cpp @@ -32,6 +32,13 @@ #include "servers/physics_2d_server.h" #include "servers/visual_server.h" +bool SegmentShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { + + Vector2 l[2] = { a, b }; + Vector2 closest = Geometry::get_closest_point_to_segment_2d(p_point, l); + return p_point.distance_to(closest) < p_tolerance; +} + void SegmentShape2D::_update_shape() { Rect2 r; diff --git a/scene/resources/segment_shape_2d.h b/scene/resources/segment_shape_2d.h index 1285ae4f82..39a71f61d5 100644 --- a/scene/resources/segment_shape_2d.h +++ b/scene/resources/segment_shape_2d.h @@ -44,6 +44,8 @@ protected: static void _bind_methods(); public: + virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const; + void set_a(const Vector2 &p_a); void set_b(const Vector2 &p_b); diff --git a/scene/resources/shape_2d.h b/scene/resources/shape_2d.h index 5c2c24ed74..877a159aee 100644 --- a/scene/resources/shape_2d.h +++ b/scene/resources/shape_2d.h @@ -44,6 +44,8 @@ protected: Shape2D(const RID &p_rid); public: + virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { return true; } + void set_custom_solver_bias(real_t p_bias); real_t get_custom_solver_bias() const; diff --git a/scene/resources/shape_line_2d.cpp b/scene/resources/shape_line_2d.cpp index c6cc3659f6..9548d35370 100644 --- a/scene/resources/shape_line_2d.cpp +++ b/scene/resources/shape_line_2d.cpp @@ -30,6 +30,21 @@ #include "shape_line_2d.h" #include "servers/physics_2d_server.h" #include "servers/visual_server.h" + +bool LineShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { + + Vector2 point = get_d() * get_normal(); + Vector2 l[2][2] = { { point - get_normal().tangent() * 100, point + get_normal().tangent() * 100 }, { point, point + get_normal() * 30 } }; + + for (int i = 0; i < 2; i++) { + Vector2 closest = Geometry::get_closest_point_to_segment_2d(p_point, l[i]); + if (p_point.distance_to(closest) < p_tolerance) + return true; + } + + return false; +} + void LineShape2D::_update_shape() { Array arr; diff --git a/scene/resources/shape_line_2d.h b/scene/resources/shape_line_2d.h index 747500b0ac..fe6f2d675e 100644 --- a/scene/resources/shape_line_2d.h +++ b/scene/resources/shape_line_2d.h @@ -44,6 +44,8 @@ protected: static void _bind_methods(); public: + virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const; + void set_normal(const Vector2 &p_normal); void set_d(real_t p_d); |