2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* collision_shape_2d_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2015-06-23 15:20:34 +00:00
|
|
|
#include "collision_shape_2d_editor_plugin.h"
|
|
|
|
|
|
|
|
#include "canvas_item_editor_plugin.h"
|
|
|
|
#include "scene/resources/capsule_shape_2d.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "scene/resources/circle_shape_2d.h"
|
2015-06-23 15:20:34 +00:00
|
|
|
#include "scene/resources/concave_polygon_shape_2d.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "scene/resources/convex_polygon_shape_2d.h"
|
|
|
|
#include "scene/resources/rectangle_shape_2d.h"
|
|
|
|
#include "scene/resources/segment_shape_2d.h"
|
|
|
|
#include "scene/resources/shape_line_2d.h"
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
Variant CollisionShape2DEditor::get_handle_value(int idx) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (shape_type) {
|
2015-06-23 15:20:34 +00:00
|
|
|
case CAPSULE_SHAPE: {
|
|
|
|
Ref<CapsuleShape2D> capsule = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
2015-06-23 15:20:34 +00:00
|
|
|
return capsule->get_radius();
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (idx == 1) {
|
2015-06-23 15:20:34 +00:00
|
|
|
return capsule->get_height();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CIRCLE_SHAPE: {
|
|
|
|
Ref<CircleShape2D> circle = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
2015-06-23 15:20:34 +00:00
|
|
|
return circle->get_radius();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONCAVE_POLYGON_SHAPE: {
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONVEX_POLYGON_SHAPE: {
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case LINE_SHAPE: {
|
2015-11-19 11:01:28 +00:00
|
|
|
Ref<LineShape2D> line = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
2015-11-19 11:01:28 +00:00
|
|
|
return line->get_d();
|
|
|
|
} else {
|
|
|
|
return line->get_normal();
|
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case RAY_SHAPE: {
|
|
|
|
Ref<RayShape2D> ray = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
2015-06-23 15:20:34 +00:00
|
|
|
return ray->get_length();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case RECTANGLE_SHAPE: {
|
|
|
|
Ref<RectangleShape2D> rect = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx < 2) {
|
2015-06-23 15:20:34 +00:00
|
|
|
return rect->get_extents().abs();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SEGMENT_SHAPE: {
|
|
|
|
Ref<SegmentShape2D> seg = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
2015-06-23 15:20:34 +00:00
|
|
|
return seg->get_a();
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (idx == 1) {
|
2015-06-23 15:20:34 +00:00
|
|
|
return seg->get_b();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void CollisionShape2DEditor::set_handle(int idx, Point2 &p_point) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (shape_type) {
|
2015-06-23 15:20:34 +00:00
|
|
|
case CAPSULE_SHAPE: {
|
|
|
|
if (idx < 2) {
|
|
|
|
Ref<CapsuleShape2D> capsule = node->get_shape();
|
|
|
|
|
|
|
|
real_t parameter = Math::abs(p_point[idx]);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
2015-06-23 15:20:34 +00:00
|
|
|
capsule->set_radius(parameter);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (idx == 1) {
|
|
|
|
capsule->set_height(parameter * 2 - capsule->get_radius() * 2);
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CIRCLE_SHAPE: {
|
|
|
|
Ref<CircleShape2D> circle = node->get_shape();
|
|
|
|
circle->set_radius(p_point.length());
|
|
|
|
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONCAVE_POLYGON_SHAPE: {
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONVEX_POLYGON_SHAPE: {
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case LINE_SHAPE: {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx < 2) {
|
2015-11-18 18:20:50 +00:00
|
|
|
Ref<LineShape2D> line = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
2015-11-18 18:20:50 +00:00
|
|
|
line->set_d(p_point.length());
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2015-11-19 11:01:28 +00:00
|
|
|
line->set_normal(p_point.normalized());
|
2015-11-18 18:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:20:34 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case RAY_SHAPE: {
|
|
|
|
Ref<RayShape2D> ray = node->get_shape();
|
|
|
|
|
|
|
|
ray->set_length(Math::abs(p_point.y));
|
|
|
|
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case RECTANGLE_SHAPE: {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx < 2) {
|
2015-06-23 15:20:34 +00:00
|
|
|
Ref<RectangleShape2D> rect = node->get_shape();
|
|
|
|
|
|
|
|
Vector2 extents = rect->get_extents();
|
|
|
|
extents[idx] = p_point[idx];
|
|
|
|
|
|
|
|
rect->set_extents(extents.abs());
|
|
|
|
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SEGMENT_SHAPE: {
|
|
|
|
if (edit_handle < 2) {
|
|
|
|
Ref<SegmentShape2D> seg = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
2015-06-23 15:20:34 +00:00
|
|
|
seg->set_a(p_point);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (idx == 1) {
|
2015-06-23 15:20:34 +00:00
|
|
|
seg->set_b(p_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void CollisionShape2DEditor::commit_handle(int idx, Variant &p_org) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Control *c = canvas_item_editor->get_viewport_control();
|
2016-05-04 01:25:37 +00:00
|
|
|
undo_redo->create_action(TTR("Set Handle"));
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (shape_type) {
|
2015-06-23 15:20:34 +00:00
|
|
|
case CAPSULE_SHAPE: {
|
|
|
|
Ref<CapsuleShape2D> capsule = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
|
|
|
undo_redo->add_do_method(capsule.ptr(), "set_radius", capsule->get_radius());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(capsule.ptr(), "set_radius", p_org);
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
} else if (idx == 1) {
|
|
|
|
undo_redo->add_do_method(capsule.ptr(), "set_height", capsule->get_height());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(capsule.ptr(), "set_height", p_org);
|
|
|
|
undo_redo->add_undo_method(c, "update");
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CIRCLE_SHAPE: {
|
|
|
|
Ref<CircleShape2D> circle = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
undo_redo->add_do_method(circle.ptr(), "set_radius", circle->get_radius());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(circle.ptr(), "set_radius", p_org);
|
|
|
|
undo_redo->add_undo_method(c, "update");
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONCAVE_POLYGON_SHAPE: {
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CONVEX_POLYGON_SHAPE: {
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case LINE_SHAPE: {
|
2015-11-18 18:20:50 +00:00
|
|
|
Ref<LineShape2D> line = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
|
|
|
undo_redo->add_do_method(line.ptr(), "set_d", line->get_d());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(line.ptr(), "set_d", p_org);
|
|
|
|
undo_redo->add_undo_method(c, "update");
|
2015-11-18 18:20:50 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
undo_redo->add_do_method(line.ptr(), "set_normal", line->get_normal());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(line.ptr(), "set_normal", p_org);
|
|
|
|
undo_redo->add_undo_method(c, "update");
|
2015-11-18 18:20:50 +00:00
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case RAY_SHAPE: {
|
|
|
|
Ref<RayShape2D> ray = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
undo_redo->add_do_method(ray.ptr(), "set_length", ray->get_length());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(ray.ptr(), "set_length", p_org);
|
|
|
|
undo_redo->add_undo_method(c, "update");
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case RECTANGLE_SHAPE: {
|
|
|
|
Ref<RectangleShape2D> rect = node->get_shape();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
undo_redo->add_do_method(rect.ptr(), "set_extents", rect->get_extents());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(rect.ptr(), "set_extents", p_org);
|
|
|
|
undo_redo->add_undo_method(c, "update");
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SEGMENT_SHAPE: {
|
|
|
|
Ref<SegmentShape2D> seg = node->get_shape();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx == 0) {
|
|
|
|
undo_redo->add_do_method(seg.ptr(), "set_a", seg->get_a());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(seg.ptr(), "set_a", p_org);
|
|
|
|
undo_redo->add_undo_method(c, "update");
|
|
|
|
} else if (idx == 1) {
|
|
|
|
undo_redo->add_do_method(seg.ptr(), "set_b", seg->get_b());
|
|
|
|
undo_redo->add_do_method(c, "update");
|
|
|
|
undo_redo->add_undo_method(seg.ptr(), "set_b", p_org);
|
|
|
|
undo_redo->add_undo_method(c, "update");
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
bool CollisionShape2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node->get_shape().is_valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shape_type == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb.is_valid()) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Transform2D gt = 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
|
|
|
Point2 gpoint(mb->get_pos().x, mb->get_pos().y);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb->get_button_index() == BUTTON_LEFT) {
|
|
|
|
if (mb->is_pressed()) {
|
|
|
|
for (int i = 0; i < handles.size(); i++) {
|
|
|
|
if (gt.xform(handles[i]).distance_to(gpoint) < 8) {
|
|
|
|
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
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
original = get_handle_value(edit_handle);
|
|
|
|
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()) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (edit_handle == -1 || !pressed) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Point2 gpoint = mm->get_pos();
|
|
|
|
Point2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint);
|
|
|
|
cpoint = canvas_item_editor->snap_point(cpoint);
|
|
|
|
cpoint = node->get_global_transform().affine_inverse().xform(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
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionShape2DEditor::_get_current_shape_type() {
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Shape2D> s = node->get_shape();
|
|
|
|
|
|
|
|
if (!s.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->cast_to<CapsuleShape2D>()) {
|
|
|
|
shape_type = CAPSULE_SHAPE;
|
|
|
|
} else if (s->cast_to<CircleShape2D>()) {
|
|
|
|
shape_type = CIRCLE_SHAPE;
|
|
|
|
} else if (s->cast_to<ConcavePolygonShape2D>()) {
|
|
|
|
shape_type = CONCAVE_POLYGON_SHAPE;
|
|
|
|
} else if (s->cast_to<ConvexPolygonShape2D>()) {
|
|
|
|
shape_type = CONVEX_POLYGON_SHAPE;
|
|
|
|
} else if (s->cast_to<LineShape2D>()) {
|
|
|
|
shape_type = LINE_SHAPE;
|
|
|
|
} else if (s->cast_to<RayShape2D>()) {
|
|
|
|
shape_type = RAY_SHAPE;
|
|
|
|
} else if (s->cast_to<RectangleShape2D>()) {
|
|
|
|
shape_type = RECTANGLE_SHAPE;
|
|
|
|
} else if (s->cast_to<SegmentShape2D>()) {
|
|
|
|
shape_type = SEGMENT_SHAPE;
|
|
|
|
} else {
|
|
|
|
shape_type = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionShape2DEditor::_canvas_draw() {
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node->get_shape().is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_get_current_shape_type();
|
|
|
|
|
|
|
|
if (shape_type == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Control *c = canvas_item_editor->get_viewport_control();
|
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
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Ref<Texture> h = get_icon("EditorHandle", "EditorIcons");
|
|
|
|
Vector2 size = h->get_size() * 0.5;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
handles.clear();
|
|
|
|
|
|
|
|
switch (shape_type) {
|
|
|
|
case CAPSULE_SHAPE: {
|
|
|
|
Ref<CapsuleShape2D> shape = node->get_shape();
|
|
|
|
|
|
|
|
handles.resize(2);
|
|
|
|
float radius = shape->get_radius();
|
2017-03-05 15:44:50 +00:00
|
|
|
float height = shape->get_height() / 2;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
handles[0] = Point2(radius, -height);
|
2017-03-05 15:44:50 +00:00
|
|
|
handles[1] = Point2(0, -(height + radius));
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c->draw_texture(h, gt.xform(handles[0]) - size);
|
|
|
|
c->draw_texture(h, gt.xform(handles[1]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CIRCLE_SHAPE: {
|
|
|
|
Ref<CircleShape2D> shape = node->get_shape();
|
|
|
|
|
|
|
|
handles.resize(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
handles[0] = Point2(shape->get_radius(), 0);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c->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;
|
|
|
|
|
|
|
|
case LINE_SHAPE: {
|
2015-11-18 18:20:50 +00:00
|
|
|
Ref<LineShape2D> shape = node->get_shape();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2015-11-18 18:20:50 +00:00
|
|
|
handles.resize(2);
|
|
|
|
handles[0] = shape->get_normal() * shape->get_d();
|
|
|
|
handles[1] = shape->get_normal() * (shape->get_d() + 30.0);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c->draw_texture(h, gt.xform(handles[0]) - size);
|
|
|
|
c->draw_texture(h, gt.xform(handles[1]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case RAY_SHAPE: {
|
|
|
|
Ref<RayShape2D> shape = node->get_shape();
|
|
|
|
|
|
|
|
handles.resize(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
handles[0] = Point2(0, shape->get_length());
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c->draw_texture(h, gt.xform(handles[0]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case RECTANGLE_SHAPE: {
|
|
|
|
Ref<RectangleShape2D> shape = node->get_shape();
|
|
|
|
|
|
|
|
handles.resize(2);
|
|
|
|
Vector2 ext = shape->get_extents();
|
2017-03-05 15:44:50 +00:00
|
|
|
handles[0] = Point2(ext.x, 0);
|
|
|
|
handles[1] = Point2(0, -ext.y);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c->draw_texture(h, gt.xform(handles[0]) - size);
|
|
|
|
c->draw_texture(h, gt.xform(handles[1]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SEGMENT_SHAPE: {
|
|
|
|
Ref<SegmentShape2D> shape = node->get_shape();
|
|
|
|
|
|
|
|
handles.resize(2);
|
|
|
|
handles[0] = shape->get_a();
|
|
|
|
handles[1] = shape->get_b();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c->draw_texture(h, gt.xform(handles[0]) - size);
|
|
|
|
c->draw_texture(h, gt.xform(handles[1]) - size);
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void CollisionShape2DEditor::edit(Node *p_node) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
if (!canvas_item_editor) {
|
2017-03-05 15:44:50 +00:00
|
|
|
canvas_item_editor = CanvasItemEditor::get_singleton();
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_node) {
|
2017-03-05 15:44:50 +00:00
|
|
|
node = p_node->cast_to<CollisionShape2D>();
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!canvas_item_editor->get_viewport_control()->is_connected("draw", this, "_canvas_draw"))
|
|
|
|
canvas_item_editor->get_viewport_control()->connect("draw", this, "_canvas_draw");
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
_get_current_shape_type();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
edit_handle = -1;
|
|
|
|
shape_type = -1;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (canvas_item_editor->get_viewport_control()->is_connected("draw", this, "_canvas_draw"))
|
|
|
|
canvas_item_editor->get_viewport_control()->disconnect("draw", this, "_canvas_draw");
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
node = NULL;
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionShape2DEditor::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method("_canvas_draw", &CollisionShape2DEditor::_canvas_draw);
|
|
|
|
ClassDB::bind_method("_get_current_shape_type", &CollisionShape2DEditor::_get_current_shape_type);
|
2015-06-23 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
CollisionShape2DEditor::CollisionShape2DEditor(EditorNode *p_editor) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
node = NULL;
|
|
|
|
canvas_item_editor = NULL;
|
|
|
|
editor = p_editor;
|
|
|
|
|
|
|
|
undo_redo = p_editor->get_undo_redo();
|
|
|
|
|
|
|
|
edit_handle = -1;
|
|
|
|
pressed = false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void CollisionShape2DEditorPlugin::edit(Object *p_obj) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
|
|
|
collision_shape_2d_editor->edit(p_obj->cast_to<Node>());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool CollisionShape2DEditorPlugin::handles(Object *p_obj) const {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
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) {
|
|
|
|
edit(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
CollisionShape2DEditorPlugin::CollisionShape2DEditorPlugin(EditorNode *p_node) {
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
editor = p_node;
|
2015-06-23 15:20:34 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
collision_shape_2d_editor = memnew(CollisionShape2DEditor(p_node));
|
2015-06-23 15:20:34 +00:00
|
|
|
p_node->get_gui_base()->add_child(collision_shape_2d_editor);
|
|
|
|
}
|
|
|
|
|
|
|
|
CollisionShape2DEditorPlugin::~CollisionShape2DEditorPlugin() {
|
|
|
|
}
|