| 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_server_2d.h" |
| 34 | #include "servers/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 | size = p_size; |
| 63 | _update_shape(); |
| 64 | } |
| 65 | |
| 66 | Size2 RectangleShape2D::get_size() const { |
| 67 | return size; |
| 68 | } |
| 69 | |
| 70 | void RectangleShape2D::draw(const RID &p_to_rid, const Color &p_color) { |
| 71 | RenderingServer::get_singleton()->canvas_item_add_rect(p_to_rid, Rect2(-size * 0.5, size), p_color); |
| 72 | if (is_collision_outline_enabled()) { |
| 73 | // Draw an outlined rectangle to make individual shapes easier to distinguish. |
| 74 | Vector<Vector2> stroke_points; |
| 75 | stroke_points.resize(5); |
| 76 | stroke_points.write[0] = -size * 0.5; |
| 77 | stroke_points.write[1] = Vector2(size.x, -size.y) * 0.5; |
| 78 | stroke_points.write[2] = size * 0.5; |
| 79 | stroke_points.write[3] = Vector2(-size.x, size.y) * 0.5; |
| 80 | stroke_points.write[4] = -size * 0.5; |
| 81 | |
| 82 | Vector<Color> stroke_colors = { Color(p_color, 1.0) }; |
| 83 | |
| 84 | RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, stroke_points, stroke_colors); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | Rect2 RectangleShape2D::get_rect() const { |
| 89 | return Rect2(-size * 0.5, size); |
| 90 | } |
| 91 | |
| 92 | real_t RectangleShape2D::get_enclosing_radius() const { |
| 93 | return size.length() / 2; |
| 94 | } |
| 95 | |
| 96 | void RectangleShape2D::_bind_methods() { |
| 97 | ClassDB::bind_method(D_METHOD("set_size" , "size" ), &RectangleShape2D::set_size); |
| 98 | ClassDB::bind_method(D_METHOD("get_size" ), &RectangleShape2D::get_size); |
| 99 | |
| 100 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size" , PROPERTY_HINT_NONE, "suffix:px" ), "set_size" , "get_size" ); |
| 101 | } |
| 102 | |
| 103 | RectangleShape2D::RectangleShape2D() : |
| 104 | Shape2D(PhysicsServer2D::get_singleton()->rectangle_shape_create()) { |
| 105 | size = Size2(20, 20); |
| 106 | _update_shape(); |
| 107 | } |
| 108 | |