Debian Codegraph @

scene/resources/2d/rectangle_shape_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* rectangle_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 "rectangle_shape_2d.h"
32
33 #include "servers/physics_2d/physics_server_2d.h"
34 #include "servers/rendering/rendering_server.h"
35 void RectangleShape2D::_update_shape() {
36 PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), size * 0.5);
37 emit_changed();
38 }
39
40 #ifndef DISABLE_DEPRECATED
41 bool RectangleShape2D::_set(const StringName &p_name, const Variant &p_value) {
42 if (p_name == "extents") { // Compatibility with Godot 3.x.
43 // Convert to `size`, twice as big.
44 set_size((Size2)p_value * 2);
45 return true;
46 }
47 return false;
48 }
49
50 bool RectangleShape2D::_get(const StringName &p_name, Variant &r_property) const {
51 if (p_name == "extents") { // Compatibility with Godot 3.x.
52 // Convert to `extents`, half as big.
53 r_property = size / 2;
54 return true;
55 }
56 return false;
57 }
58 #endif // DISABLE_DEPRECATED
59
60 void RectangleShape2D::set_size(const Size2 &p_size) {
61 ERR_FAIL_COND_MSG(p_size.x < 0 || p_size.y < 0, "RectangleShape2D size cannot be negative.");
62 if (size == p_size) {
63 return;
64 }
65 size = p_size;
66 _update_shape();
67 }
68
69 Size2 RectangleShape2D::get_size() const {
70 return size;
71 }
72
73 void RectangleShape2D::draw(const RID &p_to_rid, const Color &p_color) {
74 RenderingServer::get_singleton()->canvas_item_add_rect(p_to_rid, Rect2(-size * 0.5, size), p_color);
75 if (is_collision_outline_enabled()) {
76 // Draw an outlined rectangle to make individual shapes easier to distinguish.
77 Vector<Vector2> stroke_points;
78 stroke_points.resize(5);
79 stroke_points.write[0] = -size * 0.5;
80 stroke_points.write[1] = Vector2(size.x, -size.y) * 0.5;
81 stroke_points.write[2] = size * 0.5;
82 stroke_points.write[3] = Vector2(-size.x, size.y) * 0.5;
83 stroke_points.write[4] = -size * 0.5;
84
85 Vector<Color> stroke_colors = { Color(p_color, 1.0) };
86
87 RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, stroke_points, stroke_colors);
88 }
89 }
90
91 Rect2 RectangleShape2D::get_rect() const {
92 return Rect2(-size * 0.5, size);
93 }
94
95 real_t RectangleShape2D::get_enclosing_radius() const {
96 return size.length() / 2;
97 }
98
99 void RectangleShape2D::_bind_methods() {
100 ClassDB::bind_method(D_METHOD("set_size", "size"), &RectangleShape2D::set_size);
101 ClassDB::bind_method(D_METHOD("get_size"), &RectangleShape2D::get_size);
102
103 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
104 }
105
106 RectangleShape2D::RectangleShape2D() :
107 Shape2D(PhysicsServer2D::get_singleton()->rectangle_shape_create()) {
108 size = Size2(20, 20);
109 _update_shape();
110 }