2015-10-08 20:00:40 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* path_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2015-10-08 20:00:40 +02: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "path_editor_plugin.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2015-10-08 20:00:40 +02:00
|
|
|
#include "os/keyboard.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "scene/resources/curve.h"
|
|
|
|
#include "spatial_editor_plugin.h"
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
String PathSpatialGizmo::get_handle_name(int p_idx) const {
|
|
|
|
|
|
|
|
Ref<Curve3D> c = path->get_curve();
|
|
|
|
if (c.is_null())
|
|
|
|
return "";
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p_idx < c->get_point_count()) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
return TTR("Curve Point #") + itos(p_idx);
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
p_idx = p_idx - c->get_point_count() + 1;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
int idx = p_idx / 2;
|
|
|
|
int t = p_idx % 2;
|
|
|
|
String n = TTR("Curve Point #") + itos(idx);
|
|
|
|
if (t == 0)
|
|
|
|
n += " In";
|
2015-10-08 20:00:40 +02:00
|
|
|
else
|
2017-05-25 00:11:20 +02:00
|
|
|
n += " Out";
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2017-05-25 00:11:20 +02:00
|
|
|
Variant PathSpatialGizmo::get_handle_value(int p_idx) const {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Ref<Curve3D> c = path->get_curve();
|
|
|
|
if (c.is_null())
|
|
|
|
return Variant();
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p_idx < c->get_point_count()) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
original = c->get_point_pos(p_idx);
|
2015-10-08 20:00:40 +02:00
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
p_idx = p_idx - c->get_point_count() + 1;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
int idx = p_idx / 2;
|
|
|
|
int t = p_idx % 2;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Vector3 ofs;
|
2017-05-25 00:11:20 +02:00
|
|
|
if (t == 0)
|
|
|
|
ofs = c->get_point_in(idx);
|
2015-10-08 20:00:40 +02:00
|
|
|
else
|
2017-05-25 00:11:20 +02:00
|
|
|
ofs = c->get_point_out(idx);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
original = ofs + c->get_point_pos(idx);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
return ofs;
|
|
|
|
}
|
2017-05-25 00:11:20 +02:00
|
|
|
void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Ref<Curve3D> c = path->get_curve();
|
|
|
|
if (c.is_null())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Transform gt = path->get_global_transform();
|
|
|
|
Transform gi = gt.affine_inverse();
|
|
|
|
Vector3 ray_from = p_camera->project_ray_origin(p_point);
|
|
|
|
Vector3 ray_dir = p_camera->project_ray_normal(p_point);
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p_idx < c->get_point_count()) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Vector3 inters;
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
|
2016-11-14 15:46:08 +01:00
|
|
|
float snap = SpatialEditor::get_singleton()->get_translate_snap();
|
2017-06-30 20:47:17 +02:00
|
|
|
inters.snap(Vector3(snap, snap, snap));
|
2016-11-14 15:46:08 +01:00
|
|
|
}
|
2017-05-24 23:47:34 +02:00
|
|
|
|
2015-10-08 20:00:40 +02:00
|
|
|
Vector3 local = gi.xform(inters);
|
2017-05-25 00:11:20 +02:00
|
|
|
c->set_point_pos(p_idx, local);
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
p_idx = p_idx - c->get_point_count() + 1;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
int idx = p_idx / 2;
|
|
|
|
int t = p_idx % 2;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Vector3 base = c->get_point_pos(idx);
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Vector3 inters;
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
Vector3 local = gi.xform(inters) - base;
|
|
|
|
if (t == 0) {
|
|
|
|
c->set_point_in(idx, local);
|
2015-10-08 20:00:40 +02:00
|
|
|
} else {
|
2017-05-25 00:11:20 +02:00
|
|
|
c->set_point_out(idx, local);
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
void PathSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Ref<Curve3D> c = path->get_curve();
|
|
|
|
if (c.is_null())
|
|
|
|
return;
|
|
|
|
|
|
|
|
UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p_idx < c->get_point_count()) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
if (p_cancel) {
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
c->set_point_pos(p_idx, p_restore);
|
2015-10-08 20:00:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-05-04 03:25:37 +02:00
|
|
|
ur->create_action(TTR("Set Curve Point Pos"));
|
2017-05-25 00:11:20 +02:00
|
|
|
ur->add_do_method(c.ptr(), "set_point_pos", p_idx, c->get_point_pos(p_idx));
|
|
|
|
ur->add_undo_method(c.ptr(), "set_point_pos", p_idx, p_restore);
|
2015-10-08 20:00:40 +02:00
|
|
|
ur->commit_action();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
p_idx = p_idx - c->get_point_count() + 1;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
int idx = p_idx / 2;
|
|
|
|
int t = p_idx % 2;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Vector3 ofs;
|
|
|
|
|
|
|
|
if (p_cancel) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (t == 0) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
if (p_cancel) {
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
c->set_point_in(p_idx, p_restore);
|
2015-10-08 20:00:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-05-04 03:25:37 +02:00
|
|
|
ur->create_action(TTR("Set Curve In Pos"));
|
2017-05-25 00:11:20 +02:00
|
|
|
ur->add_do_method(c.ptr(), "set_point_in", idx, c->get_point_in(idx));
|
|
|
|
ur->add_undo_method(c.ptr(), "set_point_in", idx, p_restore);
|
2015-10-08 20:00:40 +02:00
|
|
|
ur->commit_action();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (p_cancel) {
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
c->set_point_out(idx, p_restore);
|
2015-10-08 20:00:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-05-04 03:25:37 +02:00
|
|
|
ur->create_action(TTR("Set Curve Out Pos"));
|
2017-05-25 00:11:20 +02:00
|
|
|
ur->add_do_method(c.ptr(), "set_point_out", idx, c->get_point_out(idx));
|
|
|
|
ur->add_undo_method(c.ptr(), "set_point_out", idx, p_restore);
|
2015-10-08 20:00:40 +02:00
|
|
|
ur->commit_action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
void PathSpatialGizmo::redraw() {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
clear();
|
|
|
|
|
|
|
|
Ref<Curve3D> c = path->get_curve();
|
|
|
|
if (c.is_null())
|
|
|
|
return;
|
|
|
|
|
2017-07-18 21:03:34 +02:00
|
|
|
PoolVector<Vector3> v3a = c->tessellate();
|
2017-05-24 23:47:34 +02:00
|
|
|
//PoolVector<Vector3> v3a=c->get_baked_points();
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
int v3s = v3a.size();
|
2017-05-25 00:11:20 +02:00
|
|
|
if (v3s == 0)
|
2015-10-08 20:00:40 +02:00
|
|
|
return;
|
|
|
|
Vector<Vector3> v3p;
|
2017-05-24 23:47:34 +02:00
|
|
|
PoolVector<Vector3>::Read r = v3a.read();
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-24 23:47:34 +02:00
|
|
|
// BUG: the following won't work when v3s, avoid drawing as a temporary workaround.
|
2017-05-25 00:11:20 +02:00
|
|
|
for (int i = 0; i < v3s - 1; i++) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
v3p.push_back(r[i]);
|
2017-05-25 00:11:20 +02:00
|
|
|
v3p.push_back(r[i + 1]);
|
2015-10-08 20:00:40 +02:00
|
|
|
//v3p.push_back(r[i]);
|
|
|
|
//v3p.push_back(r[i]+Vector3(0,0.2,0));
|
|
|
|
}
|
|
|
|
|
2017-08-18 20:11:16 +02:00
|
|
|
if (v3p.size() > 1) {
|
|
|
|
add_lines(v3p, PathEditorPlugin::singleton->path_material);
|
|
|
|
add_collision_segments(v3p);
|
|
|
|
}
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (PathEditorPlugin::singleton->get_edited_path() == path) {
|
2015-10-08 20:00:40 +02:00
|
|
|
v3p.clear();
|
|
|
|
Vector<Vector3> handles;
|
|
|
|
Vector<Vector3> sec_handles;
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
for (int i = 0; i < c->get_point_count(); i++) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Vector3 p = c->get_point_pos(i);
|
|
|
|
handles.push_back(p);
|
2017-05-25 00:11:20 +02:00
|
|
|
if (i > 0) {
|
2015-10-08 20:00:40 +02:00
|
|
|
v3p.push_back(p);
|
2017-05-25 00:11:20 +02:00
|
|
|
v3p.push_back(p + c->get_point_in(i));
|
|
|
|
sec_handles.push_back(p + c->get_point_in(i));
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (i < c->get_point_count() - 1) {
|
2015-10-08 20:00:40 +02:00
|
|
|
v3p.push_back(p);
|
2017-05-25 00:11:20 +02:00
|
|
|
v3p.push_back(p + c->get_point_out(i));
|
|
|
|
sec_handles.push_back(p + c->get_point_out(i));
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 20:11:16 +02:00
|
|
|
if (v3p.size() > 1) {
|
|
|
|
add_lines(v3p, PathEditorPlugin::singleton->path_thin_material);
|
|
|
|
}
|
|
|
|
if (handles.size()) {
|
|
|
|
add_handles(handles);
|
|
|
|
}
|
|
|
|
if (sec_handles.size()) {
|
|
|
|
add_handles(sec_handles, false, true);
|
|
|
|
}
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
PathSpatialGizmo::PathSpatialGizmo(Path *p_path) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
path = p_path;
|
2015-10-08 20:00:40 +02:00
|
|
|
set_spatial_node(p_path);
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
Ref<SpatialEditorGizmo> PathEditorPlugin::create_spatial_gizmo(Spatial *p_spatial) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2016-02-28 03:10:44 +01:00
|
|
|
if (p_spatial->cast_to<Path>()) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
return memnew(PathSpatialGizmo(p_spatial->cast_to<Path>()));
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 03:10:44 +01:00
|
|
|
return Ref<SpatialEditorGizmo>();
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
bool PathEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return false;
|
2017-05-25 00:11:20 +02:00
|
|
|
Ref<Curve3D> c = path->get_curve();
|
2015-10-08 20:00:40 +02:00
|
|
|
if (c.is_null())
|
|
|
|
return false;
|
|
|
|
Transform gt = path->get_global_transform();
|
|
|
|
Transform it = gt.affine_inverse();
|
|
|
|
|
|
|
|
static const int click_dist = 10; //should make global
|
|
|
|
|
2017-05-24 23:47:34 +02:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
|
|
|
|
|
|
|
if (mb.is_valid()) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-06-03 10:54:24 +02:00
|
|
|
Point2 mbpos(mb->get_position().x, mb->get_position().y);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->get_control()))) {
|
2015-10-08 20:00:40 +02:00
|
|
|
//click into curve, break it down
|
2017-07-18 21:03:34 +02:00
|
|
|
PoolVector<Vector3> v3a = c->tessellate();
|
2017-05-25 00:11:20 +02:00
|
|
|
int idx = 0;
|
|
|
|
int rc = v3a.size();
|
|
|
|
int closest_seg = -1;
|
2015-10-08 20:00:40 +02:00
|
|
|
Vector3 closest_seg_point;
|
2017-05-25 00:11:20 +02:00
|
|
|
float closest_d = 1e20;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (rc >= 2) {
|
2017-05-24 23:47:34 +02:00
|
|
|
PoolVector<Vector3>::Read r = v3a.read();
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p_camera->unproject_position(gt.xform(c->get_point_pos(0))).distance_to(mbpos) < click_dist)
|
2015-10-08 20:00:40 +02:00
|
|
|
return false; //nope, existing
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
for (int i = 0; i < c->get_point_count() - 1; i++) {
|
2015-10-08 20:00:40 +02:00
|
|
|
//find the offset and point index of the place to break up
|
2017-05-25 00:11:20 +02:00
|
|
|
int j = idx;
|
|
|
|
if (p_camera->unproject_position(gt.xform(c->get_point_pos(i + 1))).distance_to(mbpos) < click_dist)
|
2015-10-08 20:00:40 +02:00
|
|
|
return false; //nope, existing
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
while (j < rc && c->get_point_pos(i + 1) != r[j]) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
Vector3 from = r[j];
|
|
|
|
Vector3 to = r[j + 1];
|
2015-10-08 20:00:40 +02:00
|
|
|
real_t cdist = from.distance_to(to);
|
2017-05-25 00:11:20 +02:00
|
|
|
from = gt.xform(from);
|
|
|
|
to = gt.xform(to);
|
|
|
|
if (cdist > 0) {
|
2015-10-08 20:00:40 +02:00
|
|
|
Vector2 s[2];
|
|
|
|
s[0] = p_camera->unproject_position(from);
|
|
|
|
s[1] = p_camera->unproject_position(to);
|
2017-05-25 00:11:20 +02:00
|
|
|
Vector2 inters = Geometry::get_closest_point_to_segment_2d(mbpos, s);
|
2015-10-08 20:00:40 +02:00
|
|
|
float d = inters.distance_to(mbpos);
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (d < 10 && d < closest_d) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
closest_d = d;
|
|
|
|
closest_seg = i;
|
|
|
|
Vector3 ray_from = p_camera->project_ray_origin(mbpos);
|
|
|
|
Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
Vector3 ra, rb;
|
|
|
|
Geometry::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
closest_seg_point = it.xform(rb);
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
2017-05-25 00:11:20 +02:00
|
|
|
if (idx == j)
|
2015-10-08 20:00:40 +02:00
|
|
|
idx++; //force next
|
|
|
|
else
|
2017-05-25 00:11:20 +02:00
|
|
|
idx = j; //swap
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (j == rc)
|
2015-10-08 20:00:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UndoRedo *ur = editor->get_undo_redo();
|
2017-05-25 00:11:20 +02:00
|
|
|
if (closest_seg != -1) {
|
2015-10-08 20:00:40 +02:00
|
|
|
//subdivide
|
|
|
|
|
2016-05-04 03:25:37 +02:00
|
|
|
ur->create_action(TTR("Split Path"));
|
2017-05-25 00:11:20 +02:00
|
|
|
ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
|
|
|
|
ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
|
2017-01-14 18:03:38 +01:00
|
|
|
ur->commit_action();
|
2015-10-08 20:00:40 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Vector3 org;
|
2017-05-25 00:11:20 +02:00
|
|
|
if (c->get_point_count() == 0)
|
|
|
|
org = path->get_transform().get_origin();
|
2015-10-08 20:00:40 +02:00
|
|
|
else
|
2017-05-25 00:11:20 +02:00
|
|
|
org = gt.xform(c->get_point_pos(c->get_point_count() - 1));
|
|
|
|
Plane p(org, p_camera->get_transform().basis.get_axis(2));
|
|
|
|
Vector3 ray_from = p_camera->project_ray_origin(mbpos);
|
|
|
|
Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
Vector3 inters;
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2016-05-04 03:25:37 +02:00
|
|
|
ur->create_action(TTR("Add Point to Curve"));
|
2017-05-25 00:11:20 +02:00
|
|
|
ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
|
|
|
|
ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
|
2017-01-14 18:03:38 +01:00
|
|
|
ur->commit_action();
|
2015-10-08 20:00:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//add new at pos
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
} else if (mb->is_pressed() && ((mb->get_button_index() == BUTTON_LEFT && curve_del->is_pressed()) || (mb->get_button_index() == BUTTON_RIGHT && curve_edit->is_pressed()))) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
for (int i = 0; i < c->get_point_count(); i++) {
|
2016-10-27 08:18:18 +02:00
|
|
|
real_t dist_to_p = p_camera->unproject_position(gt.xform(c->get_point_pos(i))).distance_to(mbpos);
|
|
|
|
real_t dist_to_p_out = p_camera->unproject_position(gt.xform(c->get_point_pos(i) + c->get_point_out(i))).distance_to(mbpos);
|
|
|
|
real_t dist_to_p_in = p_camera->unproject_position(gt.xform(c->get_point_pos(i) + c->get_point_in(i))).distance_to(mbpos);
|
|
|
|
|
|
|
|
// Find the offset and point index of the place to break up.
|
|
|
|
// Also check for the control points.
|
|
|
|
if (dist_to_p < click_dist) {
|
|
|
|
|
|
|
|
UndoRedo *ur = editor->get_undo_redo();
|
|
|
|
ur->create_action(TTR("Remove Path Point"));
|
2017-05-25 00:11:20 +02:00
|
|
|
ur->add_do_method(c.ptr(), "remove_point", i);
|
|
|
|
ur->add_undo_method(c.ptr(), "add_point", c->get_point_pos(i), c->get_point_in(i), c->get_point_out(i), i);
|
2016-10-27 08:18:18 +02:00
|
|
|
ur->commit_action();
|
|
|
|
return true;
|
|
|
|
} else if (dist_to_p_out < click_dist) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2016-10-27 08:18:18 +02:00
|
|
|
UndoRedo *ur = editor->get_undo_redo();
|
|
|
|
ur->create_action(TTR("Remove Out-Control Point"));
|
2017-05-25 00:11:20 +02:00
|
|
|
ur->add_do_method(c.ptr(), "set_point_out", i, Vector3());
|
|
|
|
ur->add_undo_method(c.ptr(), "set_point_out", i, c->get_point_out(i));
|
2016-10-27 08:18:18 +02:00
|
|
|
ur->commit_action();
|
|
|
|
return true;
|
|
|
|
} else if (dist_to_p_in < click_dist) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2016-10-27 08:18:18 +02:00
|
|
|
UndoRedo *ur = editor->get_undo_redo();
|
|
|
|
ur->create_action(TTR("Remove In-Control Point"));
|
2017-05-25 00:11:20 +02:00
|
|
|
ur->add_do_method(c.ptr(), "set_point_in", i, Vector3());
|
|
|
|
ur->add_undo_method(c.ptr(), "set_point_in", i, c->get_point_in(i));
|
2016-10-27 08:18:18 +02:00
|
|
|
ur->commit_action();
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathEditorPlugin::edit(Object *p_object) {
|
|
|
|
|
|
|
|
if (p_object) {
|
2017-05-25 00:11:20 +02:00
|
|
|
path = p_object->cast_to<Path>();
|
2015-10-08 20:00:40 +02:00
|
|
|
if (path) {
|
|
|
|
|
|
|
|
if (path->get_curve().is_valid()) {
|
|
|
|
path->get_curve()->emit_signal("changed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-05-25 00:11:20 +02:00
|
|
|
Path *pre = path;
|
|
|
|
path = NULL;
|
2015-10-08 20:00:40 +02:00
|
|
|
if (pre) {
|
|
|
|
pre->get_curve()->emit_signal("changed");
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 12:26:56 +01:00
|
|
|
//collision_polygon_editor->edit(p_object->cast_to<Node>());
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PathEditorPlugin::handles(Object *p_object) const {
|
|
|
|
|
2017-05-24 23:47:34 +02:00
|
|
|
return p_object->is_class("Path");
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
|
|
|
|
if (p_visible) {
|
|
|
|
|
|
|
|
curve_create->show();
|
|
|
|
curve_edit->show();
|
|
|
|
curve_del->show();
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_close->show();
|
|
|
|
sep->show();
|
2015-10-08 20:00:40 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
curve_create->hide();
|
|
|
|
curve_edit->hide();
|
|
|
|
curve_del->hide();
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_close->hide();
|
|
|
|
sep->hide();
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
{
|
2017-05-25 00:11:20 +02:00
|
|
|
Path *pre = path;
|
|
|
|
path = NULL;
|
2015-10-08 20:00:40 +02:00
|
|
|
if (pre && pre->get_curve().is_valid()) {
|
|
|
|
pre->get_curve()->emit_signal("changed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathEditorPlugin::_mode_changed(int p_idx) {
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_create->set_pressed(p_idx == 0);
|
|
|
|
curve_edit->set_pressed(p_idx == 1);
|
|
|
|
curve_del->set_pressed(p_idx == 2);
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathEditorPlugin::_close_curve() {
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
Ref<Curve3D> c = path->get_curve();
|
|
|
|
if (c.is_null())
|
|
|
|
return;
|
|
|
|
if (c->get_point_count() < 2)
|
|
|
|
return;
|
|
|
|
c->add_point(c->get_point_pos(0), c->get_point_in(0), c->get_point_out(0));
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathEditorPlugin::_notification(int p_what) {
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_create->connect("pressed", this, "_mode_changed", make_binds(0));
|
|
|
|
curve_edit->connect("pressed", this, "_mode_changed", make_binds(1));
|
|
|
|
curve_del->connect("pressed", this, "_mode_changed", make_binds(2));
|
|
|
|
curve_close->connect("pressed", this, "_close_curve");
|
|
|
|
}
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathEditorPlugin::_bind_methods() {
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_mode_changed"), &PathEditorPlugin::_mode_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_close_curve"), &PathEditorPlugin::_close_curve);
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
PathEditorPlugin *PathEditorPlugin::singleton = NULL;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
|
|
|
PathEditorPlugin::PathEditorPlugin(EditorNode *p_node) {
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
path = NULL;
|
|
|
|
editor = p_node;
|
|
|
|
singleton = this;
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
path_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
|
|
|
|
path_material->set_albedo(Color(0.5, 0.5, 1.0, 0.8));
|
2017-05-24 23:47:34 +02:00
|
|
|
path_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
|
2015-10-08 20:00:40 +02:00
|
|
|
path_material->set_line_width(3);
|
2017-05-24 23:47:34 +02:00
|
|
|
path_material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
|
2017-05-25 00:11:20 +02:00
|
|
|
path_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
path_thin_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
|
|
|
|
path_thin_material->set_albedo(Color(0.5, 0.5, 1.0, 0.4));
|
2017-05-24 23:47:34 +02:00
|
|
|
path_thin_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
|
2015-10-08 20:00:40 +02:00
|
|
|
path_thin_material->set_line_width(1);
|
2017-05-24 23:47:34 +02:00
|
|
|
path_thin_material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
|
2017-05-25 00:11:20 +02:00
|
|
|
path_thin_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//SpatialEditor::get_singleton()->add_gizmo_plugin(this);
|
2015-10-08 20:00:40 +02:00
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
sep = memnew(VSeparator);
|
2015-10-08 20:00:40 +02:00
|
|
|
sep->hide();
|
|
|
|
SpatialEditor::get_singleton()->add_control_to_menu_panel(sep);
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_edit = memnew(ToolButton);
|
|
|
|
curve_edit->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
|
2015-10-08 20:00:40 +02:00
|
|
|
curve_edit->set_toggle_mode(true);
|
|
|
|
curve_edit->hide();
|
|
|
|
curve_edit->set_focus_mode(Control::FOCUS_NONE);
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_edit->set_tooltip(TTR("Select Points") + "\n" + TTR("Shift+Drag: Select Control Points") + "\n" + keycode_get_string(KEY_MASK_CMD) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point"));
|
2015-10-08 20:00:40 +02:00
|
|
|
SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_edit);
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_create = memnew(ToolButton);
|
|
|
|
curve_create->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
|
2015-10-08 20:00:40 +02:00
|
|
|
curve_create->set_toggle_mode(true);
|
|
|
|
curve_create->hide();
|
|
|
|
curve_create->set_focus_mode(Control::FOCUS_NONE);
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_create->set_tooltip(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
|
2015-10-08 20:00:40 +02:00
|
|
|
SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_create);
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_del = memnew(ToolButton);
|
|
|
|
curve_del->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
|
2015-10-08 20:00:40 +02:00
|
|
|
curve_del->set_toggle_mode(true);
|
|
|
|
curve_del->hide();
|
|
|
|
curve_del->set_focus_mode(Control::FOCUS_NONE);
|
2016-05-19 00:08:12 +02:00
|
|
|
curve_del->set_tooltip(TTR("Delete Point"));
|
2015-10-08 20:00:40 +02:00
|
|
|
SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_del);
|
2017-05-25 00:11:20 +02:00
|
|
|
curve_close = memnew(ToolButton);
|
|
|
|
curve_close->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveClose", "EditorIcons"));
|
2015-10-08 20:00:40 +02:00
|
|
|
curve_close->hide();
|
|
|
|
curve_close->set_focus_mode(Control::FOCUS_NONE);
|
2016-05-04 03:25:37 +02:00
|
|
|
curve_close->set_tooltip(TTR("Close Curve"));
|
2015-10-08 20:00:40 +02:00
|
|
|
SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_close);
|
|
|
|
|
|
|
|
curve_edit->set_pressed(true);
|
|
|
|
/*
|
|
|
|
collision_polygon_editor = memnew( PathEditor(p_node) );
|
|
|
|
editor->get_viewport()->add_child(collision_polygon_editor);
|
|
|
|
|
|
|
|
collision_polygon_editor->set_margin(MARGIN_LEFT,200);
|
|
|
|
collision_polygon_editor->set_margin(MARGIN_RIGHT,230);
|
|
|
|
collision_polygon_editor->set_margin(MARGIN_TOP,0);
|
|
|
|
collision_polygon_editor->set_margin(MARGIN_BOTTOM,10);
|
|
|
|
|
|
|
|
|
|
|
|
collision_polygon_editor->hide();
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:11:20 +02:00
|
|
|
PathEditorPlugin::~PathEditorPlugin() {
|
2015-10-08 20:00:40 +02:00
|
|
|
}
|