123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- /*************************************************************************/
- /* collision_shape_2d_editor_plugin.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
- /* */
- /* 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. */
- /*************************************************************************/
- #include "collision_shape_2d_editor_plugin.h"
- #include "canvas_item_editor_plugin.h"
- #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"
- #include "scene/resources/shape_line_2d.h"
- Variant CollisionShape2DEditor::get_handle_value(int idx) const {
- switch (shape_type) {
- case CAPSULE_SHAPE: {
- Ref<CapsuleShape2D> capsule = node->get_shape();
- if (idx == 0) {
- return capsule->get_radius();
- } else if (idx == 1) {
- return capsule->get_height();
- }
- } 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;
- case LINE_SHAPE: {
- Ref<LineShape2D> line = node->get_shape();
- if (idx == 0) {
- return line->get_d();
- } else {
- return line->get_normal();
- }
- } break;
- case RAY_SHAPE: {
- Ref<RayShape2D> ray = node->get_shape();
- if (idx == 0) {
- return ray->get_length();
- }
- } break;
- case RECTANGLE_SHAPE: {
- Ref<RectangleShape2D> rect = node->get_shape();
- if (idx < 2) {
- return rect->get_extents().abs();
- }
- } 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) {
- capsule->set_height(parameter * 2 - capsule->get_radius() * 2);
- }
- 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: {
- if (idx < 2) {
- Ref<LineShape2D> line = node->get_shape();
- if (idx == 0) {
- line->set_d(p_point.length());
- } else {
- line->set_normal(p_point.normalized());
- }
- canvas_item_editor->get_viewport_control()->update();
- }
- } 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: {
- if (idx < 2) {
- 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();
- if (idx == 0) {
- seg->set_a(p_point);
- } else if (idx == 1) {
- seg->set_b(p_point);
- }
- canvas_item_editor->get_viewport_control()->update();
- }
- } break;
- }
- }
- void CollisionShape2DEditor::commit_handle(int idx, Variant &p_org) {
- Control *c = canvas_item_editor->get_viewport_control();
- undo_redo->create_action(TTR("Set Handle"));
- switch (shape_type) {
- case CAPSULE_SHAPE: {
- Ref<CapsuleShape2D> capsule = node->get_shape();
- 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");
- }
- } 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_do_method(c, "update");
- undo_redo->add_undo_method(circle.ptr(), "set_radius", p_org);
- undo_redo->add_undo_method(c, "update");
- } break;
- case CONCAVE_POLYGON_SHAPE: {
- } break;
- case CONVEX_POLYGON_SHAPE: {
- } break;
- case LINE_SHAPE: {
- Ref<LineShape2D> line = node->get_shape();
- 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");
- } else {
- 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");
- }
- } break;
- case RAY_SHAPE: {
- Ref<RayShape2D> ray = node->get_shape();
- 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");
- } break;
- case RECTANGLE_SHAPE: {
- Ref<RectangleShape2D> rect = node->get_shape();
- 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");
- } 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_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");
- }
- } break;
- }
- undo_redo->commit_action();
- }
- bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
- if (!node) {
- return false;
- }
- if (!node->get_shape().is_valid()) {
- return false;
- }
- if (shape_type == -1) {
- return false;
- }
- Ref<InputEventMouseButton> mb = p_event;
- Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
- if (mb.is_valid()) {
- Vector2 gpoint = mb->get_position();
- Vector2 cpoint = node->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mb->get_position())));
- if (mb->get_button_index() == BUTTON_LEFT) {
- if (mb->is_pressed()) {
- for (int i = 0; i < handles.size(); i++) {
- if (xform.xform(handles[i]).distance_to(gpoint) < 8) {
- edit_handle = i;
- break;
- }
- }
- if (edit_handle == -1) {
- pressed = false;
- return false;
- }
- original = get_handle_value(edit_handle);
- pressed = true;
- return true;
- } else {
- if (pressed) {
- commit_handle(edit_handle, original);
- edit_handle = -1;
- pressed = false;
- return true;
- }
- }
- }
- return false;
- }
- Ref<InputEventMouseMotion> mm = p_event;
- if (mm.is_valid()) {
- if (edit_handle == -1 || !pressed) {
- return false;
- }
- Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
- cpoint = node->get_global_transform().affine_inverse().xform(cpoint);
- set_handle(edit_handle, cpoint);
- return true;
- }
- return false;
- }
- void CollisionShape2DEditor::_get_current_shape_type() {
- if (!node) {
- return;
- }
- Ref<Shape2D> s = node->get_shape();
- if (!s.is_valid()) {
- return;
- }
- if (Object::cast_to<CapsuleShape2D>(*s)) {
- shape_type = CAPSULE_SHAPE;
- } else if (Object::cast_to<CircleShape2D>(*s)) {
- shape_type = CIRCLE_SHAPE;
- } else if (Object::cast_to<ConcavePolygonShape2D>(*s)) {
- shape_type = CONCAVE_POLYGON_SHAPE;
- } else if (Object::cast_to<ConvexPolygonShape2D>(*s)) {
- shape_type = CONVEX_POLYGON_SHAPE;
- } else if (Object::cast_to<LineShape2D>(*s)) {
- shape_type = LINE_SHAPE;
- } else if (Object::cast_to<RayShape2D>(*s)) {
- shape_type = RAY_SHAPE;
- } else if (Object::cast_to<RectangleShape2D>(*s)) {
- shape_type = RECTANGLE_SHAPE;
- } else if (Object::cast_to<SegmentShape2D>(*s)) {
- shape_type = SEGMENT_SHAPE;
- } else {
- shape_type = -1;
- }
- canvas_item_editor->get_viewport_control()->update();
- }
- void CollisionShape2DEditor::forward_draw_over_canvas(Control *p_canvas) {
- if (!node) {
- return;
- }
- if (!node->get_shape().is_valid()) {
- return;
- }
- _get_current_shape_type();
- if (shape_type == -1) {
- return;
- }
- Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
- Ref<Texture> h = get_icon("EditorHandle", "EditorIcons");
- Vector2 size = h->get_size() * 0.5;
- handles.clear();
- switch (shape_type) {
- case CAPSULE_SHAPE: {
- Ref<CapsuleShape2D> shape = node->get_shape();
- handles.resize(2);
- float radius = shape->get_radius();
- float height = shape->get_height() / 2;
- handles[0] = Point2(radius, -height);
- handles[1] = Point2(0, -(height + radius));
- p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
- p_canvas->draw_texture(h, gt.xform(handles[1]) - size);
- } break;
- case CIRCLE_SHAPE: {
- Ref<CircleShape2D> shape = node->get_shape();
- handles.resize(1);
- handles[0] = Point2(shape->get_radius(), 0);
- p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
- } break;
- case CONCAVE_POLYGON_SHAPE: {
- } break;
- case CONVEX_POLYGON_SHAPE: {
- } break;
- case LINE_SHAPE: {
- Ref<LineShape2D> shape = node->get_shape();
- handles.resize(2);
- handles[0] = shape->get_normal() * shape->get_d();
- handles[1] = shape->get_normal() * (shape->get_d() + 30.0);
- p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
- p_canvas->draw_texture(h, gt.xform(handles[1]) - size);
- } break;
- case RAY_SHAPE: {
- Ref<RayShape2D> shape = node->get_shape();
- handles.resize(1);
- handles[0] = Point2(0, shape->get_length());
- p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
- } break;
- case RECTANGLE_SHAPE: {
- Ref<RectangleShape2D> shape = node->get_shape();
- handles.resize(2);
- Vector2 ext = shape->get_extents();
- handles[0] = Point2(ext.x, 0);
- handles[1] = Point2(0, -ext.y);
- p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
- p_canvas->draw_texture(h, gt.xform(handles[1]) - size);
- } break;
- case SEGMENT_SHAPE: {
- Ref<SegmentShape2D> shape = node->get_shape();
- handles.resize(2);
- handles[0] = shape->get_a();
- handles[1] = shape->get_b();
- p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
- p_canvas->draw_texture(h, gt.xform(handles[1]) - size);
- } break;
- }
- }
- void CollisionShape2DEditor::edit(Node *p_node) {
- if (!canvas_item_editor) {
- canvas_item_editor = CanvasItemEditor::get_singleton();
- }
- if (p_node) {
- node = Object::cast_to<CollisionShape2D>(p_node);
- _get_current_shape_type();
- } else {
- edit_handle = -1;
- shape_type = -1;
- node = NULL;
- }
- canvas_item_editor->get_viewport_control()->update();
- }
- void CollisionShape2DEditor::_bind_methods() {
- ClassDB::bind_method("_get_current_shape_type", &CollisionShape2DEditor::_get_current_shape_type);
- }
- CollisionShape2DEditor::CollisionShape2DEditor(EditorNode *p_editor) {
- node = NULL;
- canvas_item_editor = NULL;
- editor = p_editor;
- undo_redo = p_editor->get_undo_redo();
- edit_handle = -1;
- pressed = false;
- }
- void CollisionShape2DEditorPlugin::edit(Object *p_obj) {
- collision_shape_2d_editor->edit(Object::cast_to<Node>(p_obj));
- }
- bool CollisionShape2DEditorPlugin::handles(Object *p_obj) const {
- return p_obj->is_class("CollisionShape2D");
- }
- void CollisionShape2DEditorPlugin::make_visible(bool visible) {
- if (!visible) {
- edit(NULL);
- }
- }
- CollisionShape2DEditorPlugin::CollisionShape2DEditorPlugin(EditorNode *p_editor) {
- editor = p_editor;
- collision_shape_2d_editor = memnew(CollisionShape2DEditor(p_editor));
- p_editor->get_gui_base()->add_child(collision_shape_2d_editor);
- }
- CollisionShape2DEditorPlugin::~CollisionShape2DEditorPlugin() {
- }
|