Path: blob/master/scene/resources/2d/world_boundary_shape_2d.cpp
9898 views
/**************************************************************************/1/* world_boundary_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 "world_boundary_shape_2d.h"3132#include "core/math/geometry_2d.h"33#include "servers/physics_server_2d.h"34#include "servers/rendering_server.h"3536bool WorldBoundaryShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {37const Vector2 shape_center = distance * normal;38// Orthogonal part of the shape editor gizmo (the flat line).39const Vector2 ortho_segment_a = shape_center - normal.orthogonal() * 100;40const Vector2 ortho_segment_b = shape_center + normal.orthogonal() * 100;41const Vector2 ortho_closest = Geometry2D::get_closest_point_to_segment(p_point, ortho_segment_a, ortho_segment_b);42if (p_point.distance_to(ortho_closest) < p_tolerance) {43return true;44}45// Normal part of the shape editor gizmo (the arrow).46const Vector2 normal_segment_a = shape_center;47const Vector2 normal_segment_b = shape_center + normal * 30;48const Vector2 normal_closest = Geometry2D::get_closest_point_to_segment(p_point, normal_segment_a, normal_segment_b);49if (p_point.distance_to(normal_closest) < p_tolerance) {50return true;51}52return false;53}5455void WorldBoundaryShape2D::_update_shape() {56Array arr = { normal, distance };57PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), arr);58emit_changed();59}6061void WorldBoundaryShape2D::set_normal(const Vector2 &p_normal) {62// Can be non-unit but prevent zero.63ERR_FAIL_COND(p_normal.is_zero_approx());64if (normal == p_normal) {65return;66}67normal = p_normal;68_update_shape();69}7071void WorldBoundaryShape2D::set_distance(real_t p_distance) {72if (distance == p_distance) {73return;74}75distance = p_distance;76_update_shape();77}7879Vector2 WorldBoundaryShape2D::get_normal() const {80return normal;81}8283real_t WorldBoundaryShape2D::get_distance() const {84return distance;85}8687void WorldBoundaryShape2D::draw(const RID &p_to_rid, const Color &p_color) {88Vector2 point = distance * normal;89real_t line_width = 3.0;9091// Draw collision shape line.92PackedVector2Array line_points = {93point - normal.orthogonal() * 100,94point - normal.orthogonal() * 60,95point + normal.orthogonal() * 60,96point + normal.orthogonal() * 10097};9899Color transparent_color = Color(p_color, 0);100PackedColorArray line_colors = {101transparent_color,102p_color,103p_color,104transparent_color105};106107RS::get_singleton()->canvas_item_add_polyline(p_to_rid, line_points, line_colors, line_width);108109// Draw arrow.110Color arrow_color = p_color.inverted();111112Transform2D xf;113xf.rotate(normal.angle());114115Vector<Vector2> arrow_points = {116xf.xform(Vector2(distance + line_width / 2, -2.5)),117xf.xform(Vector2(distance + 20, -2.5)),118xf.xform(Vector2(distance + 20, -10)),119xf.xform(Vector2(distance + 40, 0)),120xf.xform(Vector2(distance + 20, 10)),121xf.xform(Vector2(distance + 20, 2.5)),122xf.xform(Vector2(distance + line_width / 2, 2.5)),123};124125RS::get_singleton()->canvas_item_add_polyline(p_to_rid, arrow_points, { arrow_color }, line_width / 2);126}127128Rect2 WorldBoundaryShape2D::get_rect() const {129Vector2 point = distance * normal;130131Vector2 l1[2] = { point - normal.orthogonal() * 100, point + normal.orthogonal() * 100 };132Vector2 l2[2] = { point, point + normal * 30 };133Rect2 rect;134rect.position = l1[0];135rect.expand_to(l1[1]);136rect.expand_to(l2[0]);137rect.expand_to(l2[1]);138return rect;139}140141real_t WorldBoundaryShape2D::get_enclosing_radius() const {142return distance;143}144145void WorldBoundaryShape2D::_bind_methods() {146ClassDB::bind_method(D_METHOD("set_normal", "normal"), &WorldBoundaryShape2D::set_normal);147ClassDB::bind_method(D_METHOD("get_normal"), &WorldBoundaryShape2D::get_normal);148149ClassDB::bind_method(D_METHOD("set_distance", "distance"), &WorldBoundaryShape2D::set_distance);150ClassDB::bind_method(D_METHOD("get_distance"), &WorldBoundaryShape2D::get_distance);151152ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal"), "set_normal", "get_normal");153ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance", PROPERTY_HINT_RANGE, "-1024,1024,0.01,or_greater,or_less,suffix:px"), "set_distance", "get_distance");154}155156WorldBoundaryShape2D::WorldBoundaryShape2D() :157Shape2D(PhysicsServer2D::get_singleton()->world_boundary_shape_create()) {158_update_shape();159}160161162