2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* collision_shape_2d_editor_plugin.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
#include "collision_shape_2d_editor_plugin.h"
|
|
|
|
|
|
|
|
#include "canvas_item_editor_plugin.h"
|
2020-04-10 12:04:08 +00:00
|
|
|
#include "core/os/keyboard.h"
|
2022-02-12 01:46:22 +00:00
|
|
|
#include "editor/editor_node.h"
|
2023-04-05 20:04:00 +00:00
|
|
|
#include "editor/editor_settings.h"
|
2022-03-25 17:06:46 +00:00
|
|
|
#include "editor/editor_undo_redo_manager.h"
|
2015-06-23 15:20:34 +00:00
|
|
|
#include "scene/resources/capsule_shape_2d.h"
|
|
|
|
#include "scene/resources/circle_shape_2d.h"
|
|
|
|
#include "scene/resources/concave_polygon_shape_2d.h"
|
|
|
|
#include "scene/resources/convex_polygon_shape_2d.h"
|
|
|
|
#include "scene/resources/rectangle_shape_2d.h"
|
|
|
|
#include "scene/resources/segment_shape_2d.h"
|
2021-08-19 18:21:56 +00:00
|
|
|
#include "scene/resources/separation_ray_shape_2d.h"
|
2021-09-14 17:52:35 +00:00
|
|
|
#include "scene/resources/world_boundary_shape_2d.h"
|
2023-04-27 00:01:51 +00:00
|
|
|
#include "scene/scene_string_names.h"
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2023-04-05 20:04:00 +00:00
|
|
|
CollisionShape2DEditor::CollisionShape2DEditor() {
|
|
|
|
grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
|
|
|
|
}
|
|
|
|
|
2020-03-29 17:59:04 +00:00
|
|
|
void CollisionShape2DEditor::_node_removed(Node *p_node) {
|
|
|
|
if (p_node == node) {
|
2020-04-01 23:20:12 +00:00
|
|
|
node = nullptr;
|
2020-03-29 17:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
Variant CollisionShape2DEditor::get_handle_value(int idx) const {
|
|
|
|
switch (shape_type) {
|
|
|
|
case CAPSULE_SHAPE: {
|
|
|
|
Ref<CapsuleShape2D> capsule = node->get_shape();
|
2021-08-12 18:26:47 +00:00
|
|
|
return Vector2(capsule->get_radius(), capsule->get_height());
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CIRCLE_SHAPE: {
|
|
|
|
Ref<CircleShape2D> circle = node->get_shape();
|
|
|
|
|
|
|
|
if (idx == 0) {
|
|
|
|
return circle->get_radius();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONCAVE_POLYGON_SHAPE: {
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONVEX_POLYGON_SHAPE: {
|
|
|
|
} break;
|
|
|
|
|
2021-09-14 17:52:35 +00:00
|
|
|
case WORLD_BOUNDARY_SHAPE: {
|
|
|
|
Ref<WorldBoundaryShape2D> world_boundary = node->get_shape();
|
2015-11-19 11:01:28 +00:00
|
|
|
|
|
|
|
if (idx == 0) {
|
2021-09-14 17:52:35 +00:00
|
|
|
return world_boundary->get_distance();
|
2015-11-19 11:01:28 +00:00
|
|
|
} else {
|
2021-09-14 17:52:35 +00:00
|
|
|
return world_boundary->get_normal();
|
2015-11-19 11:01:28 +00:00
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2021-08-19 18:21:56 +00:00
|
|
|
case SEPARATION_RAY_SHAPE: {
|
|
|
|
Ref<SeparationRayShape2D> ray = node->get_shape();
|
2021-08-17 17:15:11 +00:00
|
|
|
|
|
|
|
if (idx == 0) {
|
|
|
|
return ray->get_length();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
case RECTANGLE_SHAPE: {
|
|
|
|
Ref<RectangleShape2D> rect = node->get_shape();
|
|
|
|
|
2020-04-10 12:04:08 +00:00
|
|
|
if (idx < 8) {
|
2020-12-07 19:58:47 +00:00
|
|
|
return rect->get_size().abs();
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SEGMENT_SHAPE: {
|
|
|
|
Ref<SegmentShape2D> seg = node->get_shape();
|
|
|
|
|
|
|
|
if (idx == 0) {
|
|
|
|
return seg->get_a();
|
|
|
|
} else if (idx == 1) {
|
|
|
|
return seg->get_b();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionShape2DEditor::set_handle(int idx, Point2 &p_point) {
|
|
|
|
switch (shape_type) {
|
|
|
|
case CAPSULE_SHAPE: {
|
|
|
|
if (idx < 2) {
|
|
|
|
Ref<CapsuleShape2D> capsule = node->get_shape();
|
|
|
|
|
|
|
|
real_t parameter = Math::abs(p_point[idx]);
|
|
|
|
|
|
|
|
if (idx == 0) {
|
|
|
|
capsule->set_radius(parameter);
|
|
|
|
} else if (idx == 1) {
|
2020-08-30 21:15:00 +00:00
|
|
|
capsule->set_height(parameter * 2);
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CIRCLE_SHAPE: {
|
|
|
|
Ref<CircleShape2D> circle = node->get_shape();
|
|
|
|
circle->set_radius(p_point.length());
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONCAVE_POLYGON_SHAPE: {
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONVEX_POLYGON_SHAPE: {
|
|
|
|
} break;
|
|
|
|
|
2021-09-14 17:52:35 +00:00
|
|
|
case WORLD_BOUNDARY_SHAPE: {
|
2015-11-18 18:20:50 +00:00
|
|
|
if (idx < 2) {
|
2021-09-14 17:52:35 +00:00
|
|
|
Ref<WorldBoundaryShape2D> world_boundary = node->get_shape();
|
2015-11-18 18:20:50 +00:00
|
|
|
|
|
|
|
if (idx == 0) {
|
2023-05-19 11:28:50 +00:00
|
|
|
Vector2 normal = world_boundary->get_normal();
|
|
|
|
world_boundary->set_distance(p_point.dot(normal) / normal.length_squared());
|
2015-11-18 18:20:50 +00:00
|
|
|
} else {
|
2021-09-14 17:52:35 +00:00
|
|
|
world_boundary->set_normal(p_point.normalized());
|
2015-11-18 18:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
} break;
|
|
|
|
|
2021-08-19 18:21:56 +00:00
|
|
|
case SEPARATION_RAY_SHAPE: {
|
|
|
|
Ref<SeparationRayShape2D> ray = node->get_shape();
|
2021-08-17 17:15:11 +00:00
|
|
|
|
|
|
|
ray->set_length(Math::abs(p_point.y));
|
|
|
|
} break;
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
case RECTANGLE_SHAPE: {
|
2020-04-10 12:04:08 +00:00
|
|
|
if (idx < 8) {
|
2015-06-23 15:20:34 +00:00
|
|
|
Ref<RectangleShape2D> rect = node->get_shape();
|
2020-04-10 12:04:08 +00:00
|
|
|
Vector2 size = (Point2)original;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2020-04-10 12:04:08 +00:00
|
|
|
if (RECT_HANDLES[idx].x != 0) {
|
|
|
|
size.x = p_point.x * RECT_HANDLES[idx].x * 2;
|
|
|
|
}
|
|
|
|
if (RECT_HANDLES[idx].y != 0) {
|
|
|
|
size.y = p_point.y * RECT_HANDLES[idx].y * 2;
|
|
|
|
}
|
|
|
|
|
2021-08-13 21:31:57 +00:00
|
|
|
if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
|
2020-04-10 12:04:08 +00:00
|
|
|
rect->set_size(size.abs());
|
|
|
|
node->set_global_position(original_transform.get_origin());
|
2019-01-25 08:49:28 +00:00
|
|
|
} else {
|
2020-04-10 12:04:08 +00:00
|
|
|
rect->set_size(((Point2)original + (size - (Point2)original) * 0.5).abs());
|
|
|
|
Point2 pos = original_transform.affine_inverse().xform(original_transform.get_origin());
|
|
|
|
pos += (size - (Point2)original) * 0.5 * RECT_HANDLES[idx] * 0.5;
|
|
|
|
node->set_global_position(original_transform.xform(pos));
|
2019-01-25 08:49:28 +00:00
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SEGMENT_SHAPE: {
|
|
|
|
if (edit_handle < 2) {
|
|
|
|
Ref<SegmentShape2D> seg = node->get_shape();
|
|
|
|
|
|
|
|
if (idx == 0) {
|
|
|
|
seg->set_a(p_point);
|
|
|
|
} else if (idx == 1) {
|
|
|
|
seg->set_b(p_point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionShape2DEditor::commit_handle(int idx, Variant &p_org) {
|
2022-12-23 22:53:16 +00:00
|
|
|
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
|
2016-05-04 01:25:37 +00:00
|
|
|
undo_redo->create_action(TTR("Set Handle"));
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
switch (shape_type) {
|
|
|
|
case CAPSULE_SHAPE: {
|
|
|
|
Ref<CapsuleShape2D> capsule = node->get_shape();
|
|
|
|
|
2021-08-12 18:26:47 +00:00
|
|
|
Vector2 values = p_org;
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
if (idx == 0) {
|
|
|
|
undo_redo->add_do_method(capsule.ptr(), "set_radius", capsule->get_radius());
|
|
|
|
} else if (idx == 1) {
|
|
|
|
undo_redo->add_do_method(capsule.ptr(), "set_height", capsule->get_height());
|
|
|
|
}
|
2021-08-12 18:26:47 +00:00
|
|
|
undo_redo->add_undo_method(capsule.ptr(), "set_radius", values[0]);
|
|
|
|
undo_redo->add_undo_method(capsule.ptr(), "set_height", values[1]);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CIRCLE_SHAPE: {
|
|
|
|
Ref<CircleShape2D> circle = node->get_shape();
|
|
|
|
|
|
|
|
undo_redo->add_do_method(circle.ptr(), "set_radius", circle->get_radius());
|
|
|
|
undo_redo->add_undo_method(circle.ptr(), "set_radius", p_org);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONCAVE_POLYGON_SHAPE: {
|
2020-07-03 12:45:17 +00:00
|
|
|
// Cannot be edited directly, use CollisionPolygon2D instead.
|
2015-06-23 15:20:34 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONVEX_POLYGON_SHAPE: {
|
2020-07-03 12:45:17 +00:00
|
|
|
// Cannot be edited directly, use CollisionPolygon2D instead.
|
2015-06-23 15:20:34 +00:00
|
|
|
} break;
|
|
|
|
|
2021-09-14 17:52:35 +00:00
|
|
|
case WORLD_BOUNDARY_SHAPE: {
|
|
|
|
Ref<WorldBoundaryShape2D> world_boundary = node->get_shape();
|
2015-11-18 18:20:50 +00:00
|
|
|
|
|
|
|
if (idx == 0) {
|
2021-09-14 17:52:35 +00:00
|
|
|
undo_redo->add_do_method(world_boundary.ptr(), "set_distance", world_boundary->get_distance());
|
|
|
|
undo_redo->add_undo_method(world_boundary.ptr(), "set_distance", p_org);
|
2015-11-18 18:20:50 +00:00
|
|
|
} else {
|
2021-09-14 17:52:35 +00:00
|
|
|
undo_redo->add_do_method(world_boundary.ptr(), "set_normal", world_boundary->get_normal());
|
|
|
|
undo_redo->add_undo_method(world_boundary.ptr(), "set_normal", p_org);
|
2015-11-18 18:20:50 +00:00
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2021-08-19 18:21:56 +00:00
|
|
|
case SEPARATION_RAY_SHAPE: {
|
|
|
|
Ref<SeparationRayShape2D> ray = node->get_shape();
|
2021-08-17 17:15:11 +00:00
|
|
|
|
|
|
|
undo_redo->add_do_method(ray.ptr(), "set_length", ray->get_length());
|
|
|
|
undo_redo->add_undo_method(ray.ptr(), "set_length", p_org);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
case RECTANGLE_SHAPE: {
|
|
|
|
Ref<RectangleShape2D> rect = node->get_shape();
|
|
|
|
|
2020-12-07 19:58:47 +00:00
|
|
|
undo_redo->add_do_method(rect.ptr(), "set_size", rect->get_size());
|
2020-04-10 12:04:08 +00:00
|
|
|
undo_redo->add_do_method(node, "set_global_transform", node->get_global_transform());
|
2020-12-07 19:58:47 +00:00
|
|
|
undo_redo->add_undo_method(rect.ptr(), "set_size", p_org);
|
2020-04-10 12:04:08 +00:00
|
|
|
undo_redo->add_undo_method(node, "set_global_transform", original_transform);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SEGMENT_SHAPE: {
|
|
|
|
Ref<SegmentShape2D> seg = node->get_shape();
|
|
|
|
if (idx == 0) {
|
|
|
|
undo_redo->add_do_method(seg.ptr(), "set_a", seg->get_a());
|
|
|
|
undo_redo->add_undo_method(seg.ptr(), "set_a", p_org);
|
|
|
|
} else if (idx == 1) {
|
|
|
|
undo_redo->add_do_method(seg.ptr(), "set_b", seg->get_b());
|
|
|
|
undo_redo->add_undo_method(seg.ptr(), "set_b", p_org);
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
2017-10-15 20:33:25 +00:00
|
|
|
bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
|
2015-06-23 15:20:34 +00:00
|
|
|
if (!node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:44:17 +00:00
|
|
|
if (!node->is_visible_in_tree()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
if (shape_type == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2017-10-15 20:33:25 +00:00
|
|
|
Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb.is_valid()) {
|
2017-10-15 20:33:25 +00:00
|
|
|
Vector2 gpoint = mb->get_position();
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2021-08-13 21:31:57 +00:00
|
|
|
if (mb->get_button_index() == MouseButton::LEFT) {
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb->is_pressed()) {
|
|
|
|
for (int i = 0; i < handles.size(); i++) {
|
2023-04-05 20:04:00 +00:00
|
|
|
if (xform.xform(handles[i]).distance_to(gpoint) < grab_threshold) {
|
2017-05-20 15:38:03 +00:00
|
|
|
edit_handle = i;
|
|
|
|
|
|
|
|
break;
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (edit_handle == -1) {
|
|
|
|
pressed = false;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2023-01-10 11:30:07 +00:00
|
|
|
original_point = handles[edit_handle];
|
2017-05-20 15:38:03 +00:00
|
|
|
original = get_handle_value(edit_handle);
|
2020-04-10 12:04:08 +00:00
|
|
|
original_transform = node->get_global_transform();
|
|
|
|
last_point = original;
|
2017-05-20 15:38:03 +00:00
|
|
|
pressed = true;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
return true;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
} else {
|
|
|
|
if (pressed) {
|
|
|
|
commit_handle(edit_handle, original);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
edit_handle = -1;
|
|
|
|
pressed = false;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
return true;
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mm.is_valid()) {
|
|
|
|
if (edit_handle == -1 || !pressed) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-10-15 20:33:25 +00:00
|
|
|
Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
|
2020-04-10 12:04:08 +00:00
|
|
|
cpoint = original_transform.affine_inverse().xform(cpoint);
|
|
|
|
last_point = cpoint;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
set_handle(edit_handle, cpoint);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
return true;
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:04:08 +00:00
|
|
|
Ref<InputEventKey> k = p_event;
|
|
|
|
|
|
|
|
if (k.is_valid()) {
|
|
|
|
if (edit_handle == -1 || !pressed || k->is_echo()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-13 21:31:57 +00:00
|
|
|
if (shape_type == RECTANGLE_SHAPE && k->get_keycode() == Key::ALT) {
|
2020-04-10 12:04:08 +00:00
|
|
|
set_handle(edit_handle, last_point); // Update handle when Alt key is toggled.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-27 00:01:51 +00:00
|
|
|
void CollisionShape2DEditor::_shape_changed() {
|
|
|
|
canvas_item_editor->update_viewport();
|
|
|
|
|
|
|
|
if (current_shape.is_valid()) {
|
|
|
|
current_shape->disconnect(SceneStringNames::get_singleton()->changed, callable_mp(canvas_item_editor, &CanvasItemEditor::update_viewport));
|
|
|
|
current_shape = Ref<Shape2D>();
|
|
|
|
shape_type = -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-27 00:01:51 +00:00
|
|
|
current_shape = node->get_shape();
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2023-04-27 00:01:51 +00:00
|
|
|
if (current_shape.is_valid()) {
|
|
|
|
current_shape->connect(SceneStringNames::get_singleton()->changed, callable_mp(canvas_item_editor, &CanvasItemEditor::update_viewport));
|
|
|
|
} else {
|
2015-06-23 15:20:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-27 00:01:51 +00:00
|
|
|
if (Object::cast_to<CapsuleShape2D>(*current_shape)) {
|
2015-06-23 15:20:34 +00:00
|
|
|
shape_type = CAPSULE_SHAPE;
|
2023-04-27 00:01:51 +00:00
|
|
|
} else if (Object::cast_to<CircleShape2D>(*current_shape)) {
|
2015-06-23 15:20:34 +00:00
|
|
|
shape_type = CIRCLE_SHAPE;
|
2023-04-27 00:01:51 +00:00
|
|
|
} else if (Object::cast_to<ConcavePolygonShape2D>(*current_shape)) {
|
2015-06-23 15:20:34 +00:00
|
|
|
shape_type = CONCAVE_POLYGON_SHAPE;
|
2023-04-27 00:01:51 +00:00
|
|
|
} else if (Object::cast_to<ConvexPolygonShape2D>(*current_shape)) {
|
2015-06-23 15:20:34 +00:00
|
|
|
shape_type = CONVEX_POLYGON_SHAPE;
|
2023-04-27 00:01:51 +00:00
|
|
|
} else if (Object::cast_to<WorldBoundaryShape2D>(*current_shape)) {
|
2021-09-14 17:52:35 +00:00
|
|
|
shape_type = WORLD_BOUNDARY_SHAPE;
|
2023-04-27 00:01:51 +00:00
|
|
|
} else if (Object::cast_to<SeparationRayShape2D>(*current_shape)) {
|
2021-08-19 18:21:56 +00:00
|
|
|
shape_type = SEPARATION_RAY_SHAPE;
|
2023-04-27 00:01:51 +00:00
|
|
|
} else if (Object::cast_to<RectangleShape2D>(*current_shape)) {
|
2015-06-23 15:20:34 +00:00
|
|
|
shape_type = RECTANGLE_SHAPE;
|
2023-04-27 00:01:51 +00:00
|
|
|
} else if (Object::cast_to<SegmentShape2D>(*current_shape)) {
|
2015-06-23 15:20:34 +00:00
|
|
|
shape_type = SEGMENT_SHAPE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 18:00:07 +00:00
|
|
|
void CollisionShape2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
|
2015-06-23 15:20:34 +00:00
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:44:17 +00:00
|
|
|
if (!node->is_visible_in_tree()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
if (shape_type == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 03:52:51 +00:00
|
|
|
Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> h = get_theme_icon(SNAME("EditorHandle"), SNAME("EditorIcons"));
|
2015-06-23 15:20:34 +00:00
|
|
|
Vector2 size = h->get_size() * 0.5;
|
|
|
|
|
|
|
|
handles.clear();
|
|
|
|
|
|
|
|
switch (shape_type) {
|
|
|
|
case CAPSULE_SHAPE: {
|
2023-05-07 00:32:20 +00:00
|
|
|
Ref<CapsuleShape2D> shape = current_shape;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
handles.resize(2);
|
|
|
|
float radius = shape->get_radius();
|
|
|
|
float height = shape->get_height() / 2;
|
|
|
|
|
2020-08-30 21:15:00 +00:00
|
|
|
handles.write[0] = Point2(radius, 0);
|
|
|
|
handles.write[1] = Point2(0, height);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-10-29 19:32:09 +00:00
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
|
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[1]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CIRCLE_SHAPE: {
|
2023-05-07 00:32:20 +00:00
|
|
|
Ref<CircleShape2D> shape = current_shape;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
handles.resize(1);
|
2018-07-25 01:11:03 +00:00
|
|
|
handles.write[0] = Point2(shape->get_radius(), 0);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-10-29 19:32:09 +00:00
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONCAVE_POLYGON_SHAPE: {
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONVEX_POLYGON_SHAPE: {
|
|
|
|
} break;
|
|
|
|
|
2021-09-14 17:52:35 +00:00
|
|
|
case WORLD_BOUNDARY_SHAPE: {
|
2023-05-07 00:32:20 +00:00
|
|
|
Ref<WorldBoundaryShape2D> shape = current_shape;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2015-11-18 18:20:50 +00:00
|
|
|
handles.resize(2);
|
2020-05-09 12:24:46 +00:00
|
|
|
handles.write[0] = shape->get_normal() * shape->get_distance();
|
|
|
|
handles.write[1] = shape->get_normal() * (shape->get_distance() + 30.0);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-10-29 19:32:09 +00:00
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
|
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[1]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2021-08-19 18:21:56 +00:00
|
|
|
case SEPARATION_RAY_SHAPE: {
|
2023-05-07 00:32:20 +00:00
|
|
|
Ref<SeparationRayShape2D> shape = current_shape;
|
2021-08-17 17:15:11 +00:00
|
|
|
|
|
|
|
handles.resize(1);
|
|
|
|
handles.write[0] = Point2(0, shape->get_length());
|
|
|
|
|
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
case RECTANGLE_SHAPE: {
|
2023-05-07 00:32:20 +00:00
|
|
|
Ref<RectangleShape2D> shape = current_shape;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2020-04-10 12:04:08 +00:00
|
|
|
handles.resize(8);
|
2020-12-07 19:58:47 +00:00
|
|
|
Vector2 ext = shape->get_size() / 2;
|
2020-04-10 12:04:08 +00:00
|
|
|
for (int i = 0; i < handles.size(); i++) {
|
|
|
|
handles.write[i] = RECT_HANDLES[i] * ext;
|
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[i]) - size);
|
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SEGMENT_SHAPE: {
|
2023-05-07 00:32:20 +00:00
|
|
|
Ref<SegmentShape2D> shape = current_shape;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
handles.resize(2);
|
2018-07-25 01:11:03 +00:00
|
|
|
handles.write[0] = shape->get_a();
|
|
|
|
handles.write[1] = shape->get_b();
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-10-29 19:32:09 +00:00
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
|
|
|
|
p_overlay->draw_texture(h, gt.xform(handles[1]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 17:59:04 +00:00
|
|
|
void CollisionShape2DEditor::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
get_tree()->connect("node_removed", callable_mp(this, &CollisionShape2DEditor::_node_removed));
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
get_tree()->disconnect("node_removed", callable_mp(this, &CollisionShape2DEditor::_node_removed));
|
|
|
|
} break;
|
2023-04-27 00:01:51 +00:00
|
|
|
|
|
|
|
case NOTIFICATION_PROCESS: {
|
|
|
|
if (node && node->get_shape() != current_shape) {
|
|
|
|
_shape_changed();
|
|
|
|
}
|
|
|
|
} break;
|
2023-04-05 20:04:00 +00:00
|
|
|
|
|
|
|
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
|
|
|
|
if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/polygon_editor/point_grab_radius")) {
|
|
|
|
grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
|
|
|
|
}
|
|
|
|
} break;
|
2020-03-29 17:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
void CollisionShape2DEditor::edit(Node *p_node) {
|
|
|
|
if (!canvas_item_editor) {
|
|
|
|
canvas_item_editor = CanvasItemEditor::get_singleton();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_node) {
|
2017-08-24 20:58:51 +00:00
|
|
|
node = Object::cast_to<CollisionShape2D>(p_node);
|
2023-04-27 00:01:51 +00:00
|
|
|
set_process(true);
|
2015-06-23 15:20:34 +00:00
|
|
|
} else {
|
2023-01-10 11:30:07 +00:00
|
|
|
if (pressed) {
|
|
|
|
set_handle(edit_handle, original_point);
|
|
|
|
pressed = false;
|
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
edit_handle = -1;
|
2020-04-01 23:20:12 +00:00
|
|
|
node = nullptr;
|
2023-04-27 00:01:51 +00:00
|
|
|
set_process(false);
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
2023-04-27 00:01:51 +00:00
|
|
|
_shape_changed();
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionShape2DEditorPlugin::edit(Object *p_obj) {
|
2017-08-24 20:58:51 +00:00
|
|
|
collision_shape_2d_editor->edit(Object::cast_to<Node>(p_obj));
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CollisionShape2DEditorPlugin::handles(Object *p_obj) const {
|
2017-01-03 02:03:46 +00:00
|
|
|
return p_obj->is_class("CollisionShape2D");
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionShape2DEditorPlugin::make_visible(bool visible) {
|
|
|
|
if (!visible) {
|
2020-04-01 23:20:12 +00:00
|
|
|
edit(nullptr);
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:36:51 +00:00
|
|
|
CollisionShape2DEditorPlugin::CollisionShape2DEditorPlugin() {
|
|
|
|
collision_shape_2d_editor = memnew(CollisionShape2DEditor);
|
|
|
|
EditorNode::get_singleton()->get_gui_base()->add_child(collision_shape_2d_editor);
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CollisionShape2DEditorPlugin::~CollisionShape2DEditorPlugin() {
|
|
|
|
}
|