Path: blob/master/scene/resources/2d/rectangle_shape_2d.cpp
9903 views
/**************************************************************************/1/* rectangle_shape_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "rectangle_shape_2d.h"3132#include "servers/physics_server_2d.h"33#include "servers/rendering_server.h"34void RectangleShape2D::_update_shape() {35PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), size * 0.5);36emit_changed();37}3839#ifndef DISABLE_DEPRECATED40bool RectangleShape2D::_set(const StringName &p_name, const Variant &p_value) {41if (p_name == "extents") { // Compatibility with Godot 3.x.42// Convert to `size`, twice as big.43set_size((Size2)p_value * 2);44return true;45}46return false;47}4849bool RectangleShape2D::_get(const StringName &p_name, Variant &r_property) const {50if (p_name == "extents") { // Compatibility with Godot 3.x.51// Convert to `extents`, half as big.52r_property = size / 2;53return true;54}55return false;56}57#endif // DISABLE_DEPRECATED5859void RectangleShape2D::set_size(const Size2 &p_size) {60ERR_FAIL_COND_MSG(p_size.x < 0 || p_size.y < 0, "RectangleShape2D size cannot be negative.");61if (size == p_size) {62return;63}64size = p_size;65_update_shape();66}6768Size2 RectangleShape2D::get_size() const {69return size;70}7172void RectangleShape2D::draw(const RID &p_to_rid, const Color &p_color) {73RenderingServer::get_singleton()->canvas_item_add_rect(p_to_rid, Rect2(-size * 0.5, size), p_color);74if (is_collision_outline_enabled()) {75// Draw an outlined rectangle to make individual shapes easier to distinguish.76Vector<Vector2> stroke_points;77stroke_points.resize(5);78stroke_points.write[0] = -size * 0.5;79stroke_points.write[1] = Vector2(size.x, -size.y) * 0.5;80stroke_points.write[2] = size * 0.5;81stroke_points.write[3] = Vector2(-size.x, size.y) * 0.5;82stroke_points.write[4] = -size * 0.5;8384Vector<Color> stroke_colors = { Color(p_color, 1.0) };8586RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, stroke_points, stroke_colors);87}88}8990Rect2 RectangleShape2D::get_rect() const {91return Rect2(-size * 0.5, size);92}9394real_t RectangleShape2D::get_enclosing_radius() const {95return size.length() / 2;96}9798void RectangleShape2D::_bind_methods() {99ClassDB::bind_method(D_METHOD("set_size", "size"), &RectangleShape2D::set_size);100ClassDB::bind_method(D_METHOD("get_size"), &RectangleShape2D::get_size);101102ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");103}104105RectangleShape2D::RectangleShape2D() :106Shape2D(PhysicsServer2D::get_singleton()->rectangle_shape_create()) {107size = Size2(20, 20);108_update_shape();109}110111112