1 | /**************************************************************************/ |
2 | /* world_boundary_shape_2d.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
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 "world_boundary_shape_2d.h" |
32 | |
33 | #include "core/math/geometry_2d.h" |
34 | #include "servers/physics_server_2d.h" |
35 | #include "servers/rendering_server.h" |
36 | |
37 | bool WorldBoundaryShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { |
38 | Vector2 point = distance * normal; |
39 | Vector2 l[2][2] = { { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 }, { point, point + normal * 30 } }; |
40 | |
41 | for (int i = 0; i < 2; i++) { |
42 | Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, l[i]); |
43 | if (p_point.distance_to(closest) < p_tolerance) { |
44 | return true; |
45 | } |
46 | } |
47 | |
48 | return false; |
49 | } |
50 | |
51 | void WorldBoundaryShape2D::_update_shape() { |
52 | Array arr; |
53 | arr.push_back(normal); |
54 | arr.push_back(distance); |
55 | PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), arr); |
56 | emit_changed(); |
57 | } |
58 | |
59 | void WorldBoundaryShape2D::set_normal(const Vector2 &p_normal) { |
60 | // Can be non-unit but prevent zero. |
61 | ERR_FAIL_COND(p_normal.is_zero_approx()); |
62 | if (normal == p_normal) { |
63 | return; |
64 | } |
65 | normal = p_normal; |
66 | _update_shape(); |
67 | } |
68 | |
69 | void WorldBoundaryShape2D::set_distance(real_t p_distance) { |
70 | if (distance == p_distance) { |
71 | return; |
72 | } |
73 | distance = p_distance; |
74 | _update_shape(); |
75 | } |
76 | |
77 | Vector2 WorldBoundaryShape2D::get_normal() const { |
78 | return normal; |
79 | } |
80 | |
81 | real_t WorldBoundaryShape2D::get_distance() const { |
82 | return distance; |
83 | } |
84 | |
85 | void WorldBoundaryShape2D::draw(const RID &p_to_rid, const Color &p_color) { |
86 | Vector2 point = distance * normal; |
87 | real_t line_width = 3.0; |
88 | |
89 | Vector2 l1[2] = { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 }; |
90 | RS::get_singleton()->canvas_item_add_line(p_to_rid, l1[0], l1[1], p_color, line_width); |
91 | Vector2 l2[2] = { point + normal.normalized() * (0.5 * line_width), point + normal * 30 }; |
92 | RS::get_singleton()->canvas_item_add_line(p_to_rid, l2[0], l2[1], p_color, line_width); |
93 | } |
94 | |
95 | Rect2 WorldBoundaryShape2D::get_rect() const { |
96 | Vector2 point = distance * normal; |
97 | |
98 | Vector2 l1[2] = { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 }; |
99 | Vector2 l2[2] = { point, point + normal * 30 }; |
100 | Rect2 rect; |
101 | rect.position = l1[0]; |
102 | rect.expand_to(l1[1]); |
103 | rect.expand_to(l2[0]); |
104 | rect.expand_to(l2[1]); |
105 | return rect; |
106 | } |
107 | |
108 | real_t WorldBoundaryShape2D::get_enclosing_radius() const { |
109 | return distance; |
110 | } |
111 | |
112 | void WorldBoundaryShape2D::_bind_methods() { |
113 | ClassDB::bind_method(D_METHOD("set_normal" , "normal" ), &WorldBoundaryShape2D::set_normal); |
114 | ClassDB::bind_method(D_METHOD("get_normal" ), &WorldBoundaryShape2D::get_normal); |
115 | |
116 | ClassDB::bind_method(D_METHOD("set_distance" , "distance" ), &WorldBoundaryShape2D::set_distance); |
117 | ClassDB::bind_method(D_METHOD("get_distance" ), &WorldBoundaryShape2D::get_distance); |
118 | |
119 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal" ), "set_normal" , "get_normal" ); |
120 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance" , PROPERTY_HINT_RANGE, "-1024,1024,0.01,or_greater,or_less,suffix:px" ), "set_distance" , "get_distance" ); |
121 | } |
122 | |
123 | WorldBoundaryShape2D::WorldBoundaryShape2D() : |
124 | Shape2D(PhysicsServer2D::get_singleton()->world_boundary_shape_create()) { |
125 | _update_shape(); |
126 | } |
127 | |