Path: blob/master/scene/resources/2d/separation_ray_shape_2d.cpp
9898 views
/**************************************************************************/1/* separation_ray_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 "separation_ray_shape_2d.h"3132#include "servers/physics_server_2d.h"33#include "servers/rendering_server.h"3435void SeparationRayShape2D::_update_shape() {36Dictionary d;37d["length"] = length;38d["slide_on_slope"] = slide_on_slope;39PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), d);40emit_changed();41}4243void SeparationRayShape2D::draw(const RID &p_to_rid, const Color &p_color) {44const Vector2 target_position = Vector2(0, get_length());4546const float max_arrow_size = 6;47const float line_width = 1.4;48bool no_line = target_position.length() < line_width;49float arrow_size = CLAMP(target_position.length() * 2 / 3, line_width, max_arrow_size);5051if (no_line) {52arrow_size = target_position.length();53} else {54RS::get_singleton()->canvas_item_add_line(p_to_rid, Vector2(), target_position - target_position.normalized() * arrow_size, p_color, line_width);55}5657Transform2D xf;58xf.rotate(target_position.angle());59xf.translate_local(Vector2(no_line ? 0 : target_position.length() - arrow_size, 0));6061Vector<Vector2> pts = {62xf.xform(Vector2(arrow_size, 0)),63xf.xform(Vector2(0, 0.5 * arrow_size)),64xf.xform(Vector2(0, -0.5 * arrow_size))65};6667Vector<Color> cols = { p_color, p_color, p_color };6869RS::get_singleton()->canvas_item_add_primitive(p_to_rid, pts, cols, Vector<Point2>(), RID());70}7172Rect2 SeparationRayShape2D::get_rect() const {73Rect2 rect;74rect.position = Vector2();75rect.expand_to(Vector2(0, length));76rect = rect.grow(Math::SQRT12 * 4);77return rect;78}7980real_t SeparationRayShape2D::get_enclosing_radius() const {81return length;82}8384void SeparationRayShape2D::_bind_methods() {85ClassDB::bind_method(D_METHOD("set_length", "length"), &SeparationRayShape2D::set_length);86ClassDB::bind_method(D_METHOD("get_length"), &SeparationRayShape2D::get_length);8788ClassDB::bind_method(D_METHOD("set_slide_on_slope", "active"), &SeparationRayShape2D::set_slide_on_slope);89ClassDB::bind_method(D_METHOD("get_slide_on_slope"), &SeparationRayShape2D::get_slide_on_slope);9091ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_length", "get_length");92ADD_PROPERTY(PropertyInfo(Variant::BOOL, "slide_on_slope"), "set_slide_on_slope", "get_slide_on_slope");93}9495void SeparationRayShape2D::set_length(real_t p_length) {96if (length == p_length) {97return;98}99length = p_length;100_update_shape();101}102103real_t SeparationRayShape2D::get_length() const {104return length;105}106107void SeparationRayShape2D::set_slide_on_slope(bool p_active) {108if (slide_on_slope == p_active) {109return;110}111slide_on_slope = p_active;112_update_shape();113}114115bool SeparationRayShape2D::get_slide_on_slope() const {116return slide_on_slope;117}118119SeparationRayShape2D::SeparationRayShape2D() :120Shape2D(PhysicsServer2D::get_singleton()->separation_ray_shape_create()) {121_update_shape();122}123124125