godot/editor/plugins/path_editor_plugin.cpp

639 lines
21 KiB
C++
Raw Normal View History

2015-10-08 18:00:40 +00:00
/*************************************************************************/
/* path_editor_plugin.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2015-10-08 18:00:40 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2015-10-08 18:00:40 +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-10-08 18:00:40 +00:00
#include "path_editor_plugin.h"
#include "core/os/keyboard.h"
#include "scene/resources/curve.h"
#include "spatial_editor_plugin.h"
2015-10-08 18:00:40 +00:00
String PathSpatialGizmo::get_handle_name(int p_idx) const {
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
2015-10-08 18:00:40 +00:00
return "";
}
2015-10-08 18:00:40 +00:00
if (p_idx < c->get_point_count()) {
return TTR("Curve Point #") + itos(p_idx);
2015-10-08 18:00:40 +00:00
}
p_idx = p_idx - c->get_point_count() + 1;
2015-10-08 18:00:40 +00:00
int idx = p_idx / 2;
int t = p_idx % 2;
String n = TTR("Curve Point #") + itos(idx);
if (t == 0) {
n += " In";
} else {
n += " Out";
}
2015-10-08 18:00:40 +00:00
return n;
}
Variant PathSpatialGizmo::get_handle_value(int p_idx) {
2015-10-08 18:00:40 +00:00
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
2015-10-08 18:00:40 +00:00
return Variant();
}
2015-10-08 18:00:40 +00:00
if (p_idx < c->get_point_count()) {
original = c->get_point_position(p_idx);
2015-10-08 18:00:40 +00:00
return original;
}
p_idx = p_idx - c->get_point_count() + 1;
2015-10-08 18:00:40 +00:00
int idx = p_idx / 2;
int t = p_idx % 2;
2015-10-08 18:00:40 +00:00
Vector3 ofs;
if (t == 0) {
ofs = c->get_point_in(idx);
} else {
ofs = c->get_point_out(idx);
}
2015-10-08 18:00:40 +00:00
original = ofs + c->get_point_position(idx);
2015-10-08 18:00:40 +00:00
return ofs;
}
void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
2015-10-08 18:00:40 +00:00
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
2015-10-08 18:00:40 +00:00
return;
}
2015-10-08 18:00:40 +00:00
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);
2019-07-03 10:11:12 +00:00
// Setting curve point positions
if (p_idx < c->get_point_count()) {
Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
2015-10-08 18:00:40 +00:00
Vector3 inters;
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
float snap = SpatialEditor::get_singleton()->get_translate_snap();
inters.snap(Vector3(snap, snap, snap));
}
2015-10-08 18:00:40 +00:00
Vector3 local = gi.xform(inters);
c->set_point_position(p_idx, local);
2015-10-08 18:00:40 +00:00
}
return;
}
p_idx = p_idx - c->get_point_count() + 1;
2015-10-08 18:00:40 +00:00
int idx = p_idx / 2;
int t = p_idx % 2;
2015-10-08 18:00:40 +00:00
Vector3 base = c->get_point_position(idx);
2015-10-08 18:00:40 +00:00
Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
2015-10-08 18:00:40 +00:00
Vector3 inters;
2019-07-03 10:11:12 +00:00
// Setting curve in/out positions
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
if (!PathEditorPlugin::singleton->is_handle_clicked()) {
orig_in_length = c->get_point_in(idx).length();
orig_out_length = c->get_point_out(idx).length();
PathEditorPlugin::singleton->set_handle_clicked(true);
}
Vector3 local = gi.xform(inters) - base;
2019-07-03 10:11:12 +00:00
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
float snap = SpatialEditor::get_singleton()->get_translate_snap();
local.snap(Vector3(snap, snap, snap));
}
if (t == 0) {
c->set_point_in(idx, local);
if (PathEditorPlugin::singleton->mirror_angle_enabled()) {
c->set_point_out(idx, PathEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_out_length));
}
2015-10-08 18:00:40 +00:00
} else {
c->set_point_out(idx, local);
if (PathEditorPlugin::singleton->mirror_angle_enabled()) {
c->set_point_in(idx, PathEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_in_length));
}
2015-10-08 18:00:40 +00:00
}
}
}
void PathSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
2015-10-08 18:00:40 +00:00
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
2015-10-08 18:00:40 +00:00
return;
}
2015-10-08 18:00:40 +00:00
UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
if (p_idx < c->get_point_count()) {
2015-10-08 18:00:40 +00:00
if (p_cancel) {
c->set_point_position(p_idx, p_restore);
2015-10-08 18:00:40 +00:00
return;
}
ur->create_action(TTR("Set Curve Point Position"));
ur->add_do_method(c.ptr(), "set_point_position", p_idx, c->get_point_position(p_idx));
ur->add_undo_method(c.ptr(), "set_point_position", p_idx, p_restore);
2015-10-08 18:00:40 +00:00
ur->commit_action();
return;
}
p_idx = p_idx - c->get_point_count() + 1;
2015-10-08 18:00:40 +00:00
int idx = p_idx / 2;
int t = p_idx % 2;
2015-10-08 18:00:40 +00:00
if (t == 0) {
2015-10-08 18:00:40 +00:00
if (p_cancel) {
c->set_point_in(p_idx, p_restore);
2015-10-08 18:00:40 +00:00
return;
}
Fixes logically dead code (Coverity) Fixes reported logically dead codes by Coverity * image.cpp: Doesn't really need any modification. But to remove the bug report then we have to move the MAX call away from the for loop statement. * rasterizer_gles3.cpp: Removes unnecessary elif condition since it is checked earlier in the function * collada.cpp: If stamement never reached due to macro ERR_CONTINUE does the same. * navigation_mesh.cpp: Variables should always be null - however, also checked for the very same condition in their function call. Leaving this for review (whether the function call is necessary or not) * path_editor_plugin.cpp: If cancel is true, then it should restore the edited value to the original provided. http://docs.godotengine.org/en/3.0/classes/class_editorspatialgizmo.html#class-editorspatialgizmo-commit-handle * spatial_editor_gizmos.cpp: the very condition of i >= 3 is predetermined in the if case right before it. Thus case 1 is always '1' and case 2 is always '-1' * grid_map_editor.cpp: Same as above in spatial_editor_gizmos.cpp * voxel_light_baker.cpp: Same as above in spatial_editor_gizmos.cpp * visual_server.cpp: Same as above in spatial_editor_gizmos.cpp * visual_script_expression.cpp: char '-' is already true in the switch case mechanism. Thus it can never reach to default case. * particles.cpp: Case 'PARAM_MAX' is unreachable due to index checking right before the switch execution. * shader_language.cpp: Invalid index is handled in switch default case. `type < TYPE_FLOAT && type > TYPE_VEC4` -> `(type < TYPE_FLOAT || type > TYPE_VEC4`) Fixes the "always false problem" in TODO comment.
2018-04-21 14:35:23 +00:00
ur->create_action(TTR("Set Curve In Position"));
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);
if (PathEditorPlugin::singleton->mirror_angle_enabled()) {
ur->add_do_method(c.ptr(), "set_point_out", idx, PathEditorPlugin::singleton->mirror_length_enabled() ? -c->get_point_in(idx) : (-c->get_point_in(idx).normalized() * orig_out_length));
ur->add_undo_method(c.ptr(), "set_point_out", idx, PathEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_out_length));
}
2015-10-08 18:00:40 +00:00
ur->commit_action();
} else {
if (p_cancel) {
c->set_point_out(idx, p_restore);
Fixes logically dead code (Coverity) Fixes reported logically dead codes by Coverity * image.cpp: Doesn't really need any modification. But to remove the bug report then we have to move the MAX call away from the for loop statement. * rasterizer_gles3.cpp: Removes unnecessary elif condition since it is checked earlier in the function * collada.cpp: If stamement never reached due to macro ERR_CONTINUE does the same. * navigation_mesh.cpp: Variables should always be null - however, also checked for the very same condition in their function call. Leaving this for review (whether the function call is necessary or not) * path_editor_plugin.cpp: If cancel is true, then it should restore the edited value to the original provided. http://docs.godotengine.org/en/3.0/classes/class_editorspatialgizmo.html#class-editorspatialgizmo-commit-handle * spatial_editor_gizmos.cpp: the very condition of i >= 3 is predetermined in the if case right before it. Thus case 1 is always '1' and case 2 is always '-1' * grid_map_editor.cpp: Same as above in spatial_editor_gizmos.cpp * voxel_light_baker.cpp: Same as above in spatial_editor_gizmos.cpp * visual_server.cpp: Same as above in spatial_editor_gizmos.cpp * visual_script_expression.cpp: char '-' is already true in the switch case mechanism. Thus it can never reach to default case. * particles.cpp: Case 'PARAM_MAX' is unreachable due to index checking right before the switch execution. * shader_language.cpp: Invalid index is handled in switch default case. `type < TYPE_FLOAT && type > TYPE_VEC4` -> `(type < TYPE_FLOAT || type > TYPE_VEC4`) Fixes the "always false problem" in TODO comment.
2018-04-21 14:35:23 +00:00
2015-10-08 18:00:40 +00:00
return;
}
Fixes logically dead code (Coverity) Fixes reported logically dead codes by Coverity * image.cpp: Doesn't really need any modification. But to remove the bug report then we have to move the MAX call away from the for loop statement. * rasterizer_gles3.cpp: Removes unnecessary elif condition since it is checked earlier in the function * collada.cpp: If stamement never reached due to macro ERR_CONTINUE does the same. * navigation_mesh.cpp: Variables should always be null - however, also checked for the very same condition in their function call. Leaving this for review (whether the function call is necessary or not) * path_editor_plugin.cpp: If cancel is true, then it should restore the edited value to the original provided. http://docs.godotengine.org/en/3.0/classes/class_editorspatialgizmo.html#class-editorspatialgizmo-commit-handle * spatial_editor_gizmos.cpp: the very condition of i >= 3 is predetermined in the if case right before it. Thus case 1 is always '1' and case 2 is always '-1' * grid_map_editor.cpp: Same as above in spatial_editor_gizmos.cpp * voxel_light_baker.cpp: Same as above in spatial_editor_gizmos.cpp * visual_server.cpp: Same as above in spatial_editor_gizmos.cpp * visual_script_expression.cpp: char '-' is already true in the switch case mechanism. Thus it can never reach to default case. * particles.cpp: Case 'PARAM_MAX' is unreachable due to index checking right before the switch execution. * shader_language.cpp: Invalid index is handled in switch default case. `type < TYPE_FLOAT && type > TYPE_VEC4` -> `(type < TYPE_FLOAT || type > TYPE_VEC4`) Fixes the "always false problem" in TODO comment.
2018-04-21 14:35:23 +00:00
ur->create_action(TTR("Set Curve Out Position"));
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);
if (PathEditorPlugin::singleton->mirror_angle_enabled()) {
ur->add_do_method(c.ptr(), "set_point_in", idx, PathEditorPlugin::singleton->mirror_length_enabled() ? -c->get_point_out(idx) : (-c->get_point_out(idx).normalized() * orig_in_length));
ur->add_undo_method(c.ptr(), "set_point_in", idx, PathEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_in_length));
}
2015-10-08 18:00:40 +00:00
ur->commit_action();
}
}
void PathSpatialGizmo::redraw() {
2015-10-08 18:00:40 +00:00
clear();
Ref<SpatialMaterial> path_material = gizmo_plugin->get_material("path_material", this);
Ref<SpatialMaterial> path_thin_material = gizmo_plugin->get_material("path_thin_material", this);
Ref<SpatialMaterial> handles_material = gizmo_plugin->get_material("handles");
2015-10-08 18:00:40 +00:00
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
2015-10-08 18:00:40 +00:00
return;
}
2015-10-08 18:00:40 +00:00
2017-07-18 19:03:34 +00:00
PoolVector<Vector3> v3a = c->tessellate();
//PoolVector<Vector3> v3a=c->get_baked_points();
2015-10-08 18:00:40 +00:00
int v3s = v3a.size();
if (v3s == 0) {
2015-10-08 18:00:40 +00:00
return;
}
2015-10-08 18:00:40 +00:00
Vector<Vector3> v3p;
PoolVector<Vector3>::Read r = v3a.read();
2015-10-08 18:00:40 +00:00
// BUG: the following won't work when v3s, avoid drawing as a temporary workaround.
for (int i = 0; i < v3s - 1; i++) {
2015-10-08 18:00:40 +00:00
v3p.push_back(r[i]);
v3p.push_back(r[i + 1]);
2015-10-08 18:00:40 +00:00
//v3p.push_back(r[i]);
//v3p.push_back(r[i]+Vector3(0,0.2,0));
}
if (v3p.size() > 1) {
add_lines(v3p, path_material);
add_collision_segments(v3p);
}
2015-10-08 18:00:40 +00:00
if (PathEditorPlugin::singleton->get_edited_path() == path) {
2015-10-08 18:00:40 +00:00
v3p.clear();
Vector<Vector3> handles;
Vector<Vector3> sec_handles;
for (int i = 0; i < c->get_point_count(); i++) {
Vector3 p = c->get_point_position(i);
2015-10-08 18:00:40 +00:00
handles.push_back(p);
if (i > 0) {
2015-10-08 18:00:40 +00:00
v3p.push_back(p);
v3p.push_back(p + c->get_point_in(i));
sec_handles.push_back(p + c->get_point_in(i));
2015-10-08 18:00:40 +00:00
}
if (i < c->get_point_count() - 1) {
2015-10-08 18:00:40 +00:00
v3p.push_back(p);
v3p.push_back(p + c->get_point_out(i));
sec_handles.push_back(p + c->get_point_out(i));
2015-10-08 18:00:40 +00:00
}
}
if (v3p.size() > 1) {
add_lines(v3p, path_thin_material);
}
if (handles.size()) {
add_handles(handles, handles_material);
}
if (sec_handles.size()) {
add_handles(sec_handles, handles_material, false, true);
}
2015-10-08 18:00:40 +00:00
}
}
PathSpatialGizmo::PathSpatialGizmo(Path *p_path) {
path = p_path;
2015-10-08 18:00:40 +00:00
set_spatial_node(p_path);
}
bool PathEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event) {
if (!path) {
2015-10-08 18:00:40 +00:00
return false;
}
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
2015-10-08 18:00:40 +00:00
return false;
}
2015-10-08 18:00:40 +00:00
Transform gt = path->get_global_transform();
Transform it = gt.affine_inverse();
static const int click_dist = 10; //should make global
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid()) {
Point2 mbpos(mb->get_position().x, mb->get_position().y);
2015-10-08 18:00:40 +00:00
if (!mb->is_pressed()) {
set_handle_clicked(false);
}
if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->get_control()))) {
2015-10-08 18:00:40 +00:00
//click into curve, break it down
2017-07-18 19:03:34 +00:00
PoolVector<Vector3> v3a = c->tessellate();
int idx = 0;
int rc = v3a.size();
int closest_seg = -1;
2015-10-08 18:00:40 +00:00
Vector3 closest_seg_point;
float closest_d = 1e20;
2015-10-08 18:00:40 +00:00
if (rc >= 2) {
PoolVector<Vector3>::Read r = v3a.read();
2015-10-08 18:00:40 +00:00
if (p_camera->unproject_position(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist) {
2015-10-08 18:00:40 +00:00
return false; //nope, existing
}
2015-10-08 18:00:40 +00:00
for (int i = 0; i < c->get_point_count() - 1; i++) {
2015-10-08 18:00:40 +00:00
//find the offset and point index of the place to break up
int j = idx;
if (p_camera->unproject_position(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist) {
2015-10-08 18:00:40 +00:00
return false; //nope, existing
}
2015-10-08 18:00:40 +00:00
while (j < rc && c->get_point_position(i + 1) != r[j]) {
Vector3 from = r[j];
Vector3 to = r[j + 1];
2015-10-08 18:00:40 +00:00
real_t cdist = from.distance_to(to);
from = gt.xform(from);
to = gt.xform(to);
if (cdist > 0) {
2015-10-08 18:00:40 +00:00
Vector2 s[2];
s[0] = p_camera->unproject_position(from);
s[1] = p_camera->unproject_position(to);
Vector2 inters = Geometry::get_closest_point_to_segment_2d(mbpos, s);
2015-10-08 18:00:40 +00:00
float d = inters.distance_to(mbpos);
if (d < 10 && d < closest_d) {
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 18:00:40 +00:00
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
2015-10-08 18:00:40 +00:00
closest_seg_point = it.xform(rb);
2015-10-08 18:00:40 +00:00
}
}
j++;
}
if (idx == j) {
2015-10-08 18:00:40 +00:00
idx++; //force next
} else {
idx = j; //swap
}
2015-10-08 18:00:40 +00:00
if (j == rc) {
2015-10-08 18:00:40 +00:00
break;
}
2015-10-08 18:00:40 +00:00
}
}
UndoRedo *ur = editor->get_undo_redo();
if (closest_seg != -1) {
2015-10-08 18:00:40 +00:00
//subdivide
ur->create_action(TTR("Split Path"));
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 17:03:38 +00:00
ur->commit_action();
2015-10-08 18:00:40 +00:00
return true;
} else {
Vector3 org;
if (c->get_point_count() == 0) {
org = path->get_transform().get_origin();
} else {
org = gt.xform(c->get_point_position(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 18:00:40 +00:00
Vector3 inters;
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
ur->create_action(TTR("Add Point to Curve"));
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 17:03:38 +00:00
ur->commit_action();
2015-10-08 18:00:40 +00:00
return true;
}
//add new at pos
}
} 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()))) {
for (int i = 0; i < c->get_point_count(); i++) {
real_t dist_to_p = p_camera->unproject_position(gt.xform(c->get_point_position(i))).distance_to(mbpos);
real_t dist_to_p_out = p_camera->unproject_position(gt.xform(c->get_point_position(i) + c->get_point_out(i))).distance_to(mbpos);
real_t dist_to_p_in = p_camera->unproject_position(gt.xform(c->get_point_position(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"));
ur->add_do_method(c.ptr(), "remove_point", i);
ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i);
ur->commit_action();
return true;
} else if (dist_to_p_out < click_dist) {
UndoRedo *ur = editor->get_undo_redo();
ur->create_action(TTR("Remove Out-Control Point"));
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));
ur->commit_action();
return true;
} else if (dist_to_p_in < click_dist) {
UndoRedo *ur = editor->get_undo_redo();
ur->create_action(TTR("Remove In-Control Point"));
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));
ur->commit_action();
return true;
}
2015-10-08 18:00:40 +00:00
}
}
}
return false;
}
void PathEditorPlugin::edit(Object *p_object) {
if (p_object) {
path = Object::cast_to<Path>(p_object);
2015-10-08 18:00:40 +00:00
if (path) {
if (path->get_curve().is_valid()) {
path->get_curve()->emit_signal("changed");
}
}
} else {
Path *pre = path;
path = nullptr;
2015-10-08 18:00:40 +00:00
if (pre) {
pre->get_curve()->emit_signal("changed");
}
}
//collision_polygon_editor->edit(Object::cast_to<Node>(p_object));
2015-10-08 18:00:40 +00:00
}
bool PathEditorPlugin::handles(Object *p_object) const {
return p_object->is_class("Path");
2015-10-08 18:00:40 +00:00
}
void PathEditorPlugin::make_visible(bool p_visible) {
if (p_visible) {
curve_create->show();
curve_edit->show();
curve_del->show();
curve_close->show();
handle_menu->show();
sep->show();
2015-10-08 18:00:40 +00:00
} else {
curve_create->hide();
curve_edit->hide();
curve_del->hide();
curve_close->hide();
handle_menu->hide();
sep->hide();
2015-10-08 18:00:40 +00:00
{
Path *pre = path;
path = nullptr;
2015-10-08 18:00:40 +00:00
if (pre && pre->get_curve().is_valid()) {
pre->get_curve()->emit_signal("changed");
}
}
}
}
void PathEditorPlugin::_mode_changed(int p_idx) {
curve_create->set_pressed(p_idx == 0);
curve_edit->set_pressed(p_idx == 1);
curve_del->set_pressed(p_idx == 2);
2015-10-08 18:00:40 +00:00
}
void PathEditorPlugin::_close_curve() {
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
return;
}
if (c->get_point_count() < 2) {
return;
}
2021-09-25 17:22:10 +00:00
if (c->get_point_position(0) == c->get_point_position(c->get_point_count() - 1)) {
return;
}
UndoRedo *ur = editor->get_undo_redo();
ur->create_action(TTR("Close Curve"));
ur->add_do_method(c.ptr(), "add_point", c->get_point_position(0), c->get_point_in(0), c->get_point_out(0), -1);
ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
ur->commit_action();
2015-10-08 18:00:40 +00:00
}
void PathEditorPlugin::_handle_option_pressed(int p_option) {
PopupMenu *pm;
pm = handle_menu->get_popup();
switch (p_option) {
case HANDLE_OPTION_ANGLE: {
bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
mirror_handle_angle = !is_checked;
pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
} break;
case HANDLE_OPTION_LENGTH: {
bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
mirror_handle_length = !is_checked;
pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
} break;
}
}
2015-10-08 18:00:40 +00:00
void PathEditorPlugin::_notification(int p_what) {
if (p_what == NOTIFICATION_ENTER_TREE) {
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 18:00:40 +00:00
}
void PathEditorPlugin::_bind_methods() {
ClassDB::bind_method(D_METHOD("_mode_changed"), &PathEditorPlugin::_mode_changed);
ClassDB::bind_method(D_METHOD("_close_curve"), &PathEditorPlugin::_close_curve);
ClassDB::bind_method(D_METHOD("_handle_option_pressed"), &PathEditorPlugin::_handle_option_pressed);
2015-10-08 18:00:40 +00:00
}
PathEditorPlugin *PathEditorPlugin::singleton = nullptr;
2015-10-08 18:00:40 +00:00
PathEditorPlugin::PathEditorPlugin(EditorNode *p_node) {
path = nullptr;
editor = p_node;
singleton = this;
mirror_handle_angle = true;
mirror_handle_length = true;
2015-10-08 18:00:40 +00:00
Ref<PathSpatialGizmoPlugin> gizmo_plugin;
gizmo_plugin.instance();
SpatialEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
2015-10-08 18:00:40 +00:00
sep = memnew(VSeparator);
2015-10-08 18:00:40 +00:00
sep->hide();
SpatialEditor::get_singleton()->add_control_to_menu_panel(sep);
curve_edit = memnew(ToolButton);
curve_edit->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
2015-10-08 18:00:40 +00:00
curve_edit->set_toggle_mode(true);
curve_edit->hide();
curve_edit->set_focus_mode(Control::FOCUS_NONE);
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 18:00:40 +00:00
SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_edit);
curve_create = memnew(ToolButton);
curve_create->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
2015-10-08 18:00:40 +00:00
curve_create->set_toggle_mode(true);
curve_create->hide();
curve_create->set_focus_mode(Control::FOCUS_NONE);
curve_create->set_tooltip(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
2015-10-08 18:00:40 +00:00
SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_create);
curve_del = memnew(ToolButton);
curve_del->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
2015-10-08 18:00:40 +00:00
curve_del->set_toggle_mode(true);
curve_del->hide();
curve_del->set_focus_mode(Control::FOCUS_NONE);
curve_del->set_tooltip(TTR("Delete Point"));
2015-10-08 18:00:40 +00:00
SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_del);
curve_close = memnew(ToolButton);
curve_close->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveClose", "EditorIcons"));
2015-10-08 18:00:40 +00:00
curve_close->hide();
curve_close->set_focus_mode(Control::FOCUS_NONE);
curve_close->set_tooltip(TTR("Close Curve"));
2015-10-08 18:00:40 +00:00
SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_close);
PopupMenu *menu;
handle_menu = memnew(MenuButton);
handle_menu->set_text(TTR("Options"));
handle_menu->hide();
SpatialEditor::get_singleton()->add_control_to_menu_panel(handle_menu);
menu = handle_menu->get_popup();
menu->add_check_item(TTR("Mirror Handle Angles"));
menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
menu->add_check_item(TTR("Mirror Handle Lengths"));
menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
menu->connect("id_pressed", this, "_handle_option_pressed");
2015-10-08 18:00:40 +00:00
curve_edit->set_pressed(true);
}
2015-10-08 18:00:40 +00:00
PathEditorPlugin::~PathEditorPlugin() {
}
Ref<EditorSpatialGizmo> PathSpatialGizmoPlugin::create_gizmo(Spatial *p_spatial) {
Ref<PathSpatialGizmo> ref;
2015-10-08 18:00:40 +00:00
Path *path = Object::cast_to<Path>(p_spatial);
if (path) {
ref = Ref<PathSpatialGizmo>(memnew(PathSpatialGizmo(path)));
}
2015-10-08 18:00:40 +00:00
return ref;
2015-10-08 18:00:40 +00:00
}
String PathSpatialGizmoPlugin::get_name() const {
return "Path";
}
int PathSpatialGizmoPlugin::get_priority() const {
return -1;
}
PathSpatialGizmoPlugin::PathSpatialGizmoPlugin() {
Color path_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/path", Color(0.5, 0.5, 1.0, 0.8));
create_material("path_material", path_color);
create_material("path_thin_material", Color(0.5, 0.5, 0.5));
create_handle_material("handles");
2015-10-08 18:00:40 +00:00
}