rectangle_shape_2d.cpp 4.6 KB

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