Path: blob/master/scene/resources/2d/segment_shape_2d.cpp
9898 views
/**************************************************************************/1/* segment_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 "segment_shape_2d.h"3132#include "core/math/geometry_2d.h"33#include "servers/physics_server_2d.h"34#include "servers/rendering_server.h"3536bool SegmentShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {37Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, a, b);38return p_point.distance_to(closest) < p_tolerance;39}4041void SegmentShape2D::_update_shape() {42Rect2 r;43r.position = a;44r.size = b;45PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), r);46emit_changed();47}4849void SegmentShape2D::set_a(const Vector2 &p_a) {50if (a == p_a) {51return;52}53a = p_a;54_update_shape();55}5657Vector2 SegmentShape2D::get_a() const {58return a;59}6061void SegmentShape2D::set_b(const Vector2 &p_b) {62if (b == p_b) {63return;64}65b = p_b;66_update_shape();67}6869Vector2 SegmentShape2D::get_b() const {70return b;71}7273void SegmentShape2D::draw(const RID &p_to_rid, const Color &p_color) {74RenderingServer::get_singleton()->canvas_item_add_line(p_to_rid, a, b, p_color, 3);75}7677Rect2 SegmentShape2D::get_rect() const {78Rect2 rect;79rect.position = a;80rect.expand_to(b);81return rect;82}8384real_t SegmentShape2D::get_enclosing_radius() const {85return (a + b).length();86}8788void SegmentShape2D::_bind_methods() {89ClassDB::bind_method(D_METHOD("set_a", "a"), &SegmentShape2D::set_a);90ClassDB::bind_method(D_METHOD("get_a"), &SegmentShape2D::get_a);9192ClassDB::bind_method(D_METHOD("set_b", "b"), &SegmentShape2D::set_b);93ClassDB::bind_method(D_METHOD("get_b"), &SegmentShape2D::get_b);9495ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "a", PROPERTY_HINT_NONE, "suffix:px"), "set_a", "get_a");96ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "b", PROPERTY_HINT_NONE, "suffix:px"), "set_b", "get_b");97}9899SegmentShape2D::SegmentShape2D() :100Shape2D(PhysicsServer2D::get_singleton()->segment_shape_create()) {101a = Vector2();102b = Vector2(0, 10);103_update_shape();104}105106107