godot/editor/plugins/path_3d_editor_plugin.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

813 lines
28 KiB
C++
Raw Normal View History

/**************************************************************************/
/* path_3d_editor_plugin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "path_3d_editor_plugin.h"
#include "core/math/geometry_2d.h"
#include "core/math/geometry_3d.h"
#include "core/os/keyboard.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
2022-03-25 17:06:46 +00:00
#include "editor/editor_undo_redo_manager.h"
#include "node_3d_editor_plugin.h"
#include "scene/gui/menu_button.h"
2015-10-08 18:00:40 +00:00
#include "scene/resources/curve.h"
String Path3DGizmo::get_handle_name(int p_id, bool p_secondary) const {
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
2022-11-17 15:42:00 +00:00
// Primary handles: position.
if (!p_secondary) {
return TTR("Curve Point #") + itos(p_id);
2015-10-08 18:00:40 +00:00
}
2022-11-17 15:42:00 +00:00
// Secondary handles: in, out, tilt.
const HandleInfo info = _secondary_handles_info[p_id];
switch (info.type) {
case HandleType::HANDLE_TYPE_IN:
return TTR("Handle In #") + itos(info.point_idx);
case HandleType::HANDLE_TYPE_OUT:
return TTR("Handle Out #") + itos(info.point_idx);
case HandleType::HANDLE_TYPE_TILT:
return TTR("Handle Tilt #") + itos(info.point_idx);
}
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +00:00
return "";
2015-10-08 18:00:40 +00:00
}
Variant Path3DGizmo::get_handle_value(int p_id, bool p_secondary) const {
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
2022-11-17 15:42:00 +00:00
// Primary handles: position.
if (!p_secondary) {
original = c->get_point_position(p_id);
2015-10-08 18:00:40 +00:00
return original;
}
2022-11-17 15:42:00 +00:00
// Secondary handles: in, out, tilt.
const HandleInfo info = _secondary_handles_info[p_id];
2015-10-08 18:00:40 +00:00
Vector3 ofs;
2022-11-17 15:42:00 +00:00
switch (info.type) {
case HandleType::HANDLE_TYPE_TILT:
return c->get_point_tilt(info.point_idx);
case HandleType::HANDLE_TYPE_IN:
ofs = c->get_point_in(info.point_idx);
break;
case HandleType::HANDLE_TYPE_OUT:
ofs = c->get_point_out(info.point_idx);
break;
}
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +00:00
original = ofs + c->get_point_position(info.point_idx);
2015-10-08 18:00:40 +00:00
return ofs;
}
void Path3DGizmo::set_handle(int p_id, bool p_secondary, Camera3D *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
2022-11-17 15:42:00 +00:00
const Transform3D gt = path->get_global_transform();
const Transform3D gi = gt.affine_inverse();
const Vector3 ray_from = p_camera->project_ray_origin(p_point);
const Vector3 ray_dir = p_camera->project_ray_normal(p_point);
const Plane p = Plane(p_camera->get_transform().basis.get_column(2), gt.xform(original));
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +00:00
// Primary handles: position.
if (!p_secondary) {
2015-10-08 18:00:40 +00:00
Vector3 inters;
2022-11-17 15:42:00 +00:00
// Special cas for primary handle, the handle id equals control point id.
const int idx = p_id;
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
float snap = Node3DEditor::get_singleton()->get_translate_snap();
inters.snap(Vector3(snap, snap, snap));
}
2015-10-08 18:00:40 +00:00
Vector3 local = gi.xform(inters);
2022-11-17 15:42:00 +00:00
c->set_point_position(idx, local);
2015-10-08 18:00:40 +00:00
}
return;
}
2022-11-17 15:42:00 +00:00
// Secondary handles: in, out, tilt.
const HandleInfo info = _secondary_handles_info[p_id];
switch (info.type) {
case HandleType::HANDLE_TYPE_OUT:
case HandleType::HANDLE_TYPE_IN: {
const int idx = info.point_idx;
const Vector3 base = c->get_point_position(idx);
Vector3 inters;
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
if (!Path3DEditorPlugin::singleton->is_handle_clicked()) {
orig_in_length = c->get_point_in(idx).length();
orig_out_length = c->get_point_out(idx).length();
Path3DEditorPlugin::singleton->set_handle_clicked(true);
}
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +00:00
Vector3 local = gi.xform(inters) - base;
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
float snap = Node3DEditor::get_singleton()->get_translate_snap();
local.snap(Vector3(snap, snap, snap));
}
2022-11-17 15:42:00 +00:00
if (info.type == HandleType::HANDLE_TYPE_IN) {
c->set_point_in(idx, local);
if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
c->set_point_out(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_out_length));
}
} else {
c->set_point_out(idx, local);
if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
c->set_point_in(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_in_length));
}
}
}
break;
2019-07-03 10:11:12 +00:00
}
2022-11-17 15:42:00 +00:00
case HandleType::HANDLE_TYPE_TILT: {
const int idx = info.point_idx;
const Vector3 position = c->get_point_position(idx);
const Basis posture = c->get_point_baked_posture(idx);
const Vector3 tangent = -posture.get_column(2);
const Vector3 up = posture.get_column(1);
const Plane p_tilt = Plane(tangent, position);
Vector3 intersection;
if (p_tilt.intersects_ray(ray_from, ray_dir, &intersection)) {
Vector3 direction = intersection - position;
direction.normalize(); // FIXME: redundant?
real_t tilt_angle = up.signed_angle_to(direction, tangent);
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
real_t snap = Node3DEditor::get_singleton()->get_rotate_snap();
tilt_angle = Math::rad_to_deg(tilt_angle) + snap * 0.5; // Else it won't reach +180.
tilt_angle -= Math::fmod(tilt_angle, snap);
tilt_angle = Math::deg_to_rad(tilt_angle);
}
2019-07-03 10:11:12 +00:00
2022-11-17 15:42:00 +00:00
c->set_point_tilt(idx, tilt_angle);
}
2022-11-17 15:42:00 +00:00
break;
2015-10-08 18:00:40 +00:00
}
}
}
void Path3DGizmo::commit_handle(int p_id, bool p_secondary, 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
2022-12-23 22:53:16 +00:00
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +00:00
// Primary handles: position.
if (!p_secondary) {
2022-11-17 15:42:00 +00:00
// Special cas for primary handle, the handle id equals control point id.
const int idx = p_id;
2015-10-08 18:00:40 +00:00
if (p_cancel) {
2022-11-17 15:42:00 +00:00
c->set_point_position(idx, p_restore);
2015-10-08 18:00:40 +00:00
return;
}
ur->create_action(TTR("Set Curve Point Position"));
2022-11-17 15:42:00 +00:00
ur->add_do_method(c.ptr(), "set_point_position", idx, c->get_point_position(idx));
ur->add_undo_method(c.ptr(), "set_point_position", idx, p_restore);
2015-10-08 18:00:40 +00:00
ur->commit_action();
return;
}
2022-11-17 15:42:00 +00:00
// Secondary handles: in, out, tilt.
const HandleInfo info = _secondary_handles_info[p_id];
const int idx = info.point_idx;
switch (info.type) {
case HandleType::HANDLE_TYPE_OUT: {
if (p_cancel) {
c->set_point_out(idx, p_restore);
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +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
2022-11-17 15:42:00 +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);
2022-11-17 15:42:00 +00:00
if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
ur->add_do_method(c.ptr(), "set_point_in", idx, Path3DEditorPlugin::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, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_in_length));
}
ur->commit_action();
break;
}
2022-11-17 15:42:00 +00:00
case HandleType::HANDLE_TYPE_IN: {
if (p_cancel) {
c->set_point_in(idx, p_restore);
return;
}
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +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);
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
2022-11-17 15:42:00 +00:00
if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
ur->add_do_method(c.ptr(), "set_point_out", idx, Path3DEditorPlugin::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, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_out_length));
}
ur->commit_action();
break;
2015-10-08 18:00:40 +00:00
}
2022-11-17 15:42:00 +00:00
case HandleType::HANDLE_TYPE_TILT: {
if (p_cancel) {
c->set_point_tilt(idx, p_restore);
return;
}
ur->create_action(TTR("Set Curve Point Tilt"));
ur->add_do_method(c.ptr(), "set_point_tilt", idx, c->get_point_tilt(idx));
ur->add_undo_method(c.ptr(), "set_point_tilt", idx, p_restore);
ur->commit_action();
break;
}
2015-10-08 18:00:40 +00:00
}
}
void Path3DGizmo::redraw() {
2015-10-08 18:00:40 +00:00
clear();
Ref<StandardMaterial3D> path_material = gizmo_plugin->get_material("path_material", this);
Ref<StandardMaterial3D> path_thin_material = gizmo_plugin->get_material("path_thin_material", this);
Ref<StandardMaterial3D> handles_material = gizmo_plugin->get_material("handles");
Ref<StandardMaterial3D> sec_handles_material = gizmo_plugin->get_material("sec_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
real_t interval = 0.1;
const real_t length = c->get_baked_length();
2015-10-08 18:00:40 +00:00
// 1. Draw curve and bones.
if (length > CMP_EPSILON) {
const int sample_count = int(length / interval) + 2;
interval = length / (sample_count - 1); // Recalculate real interval length.
Vector<Transform3D> frames;
frames.resize(sample_count);
{
Transform3D *w = frames.ptrw();
for (int i = 0; i < sample_count; i++) {
w[i] = c->sample_baked_with_rotation(i * interval, true, true);
}
}
const Transform3D *r = frames.ptr();
2022-11-17 15:42:00 +00:00
Vector<Vector3> _collision_segments;
Vector<Vector3> bones;
Vector<Vector3> ribbon;
for (int i = 0; i < sample_count; i++) {
const Vector3 p1 = r[i].origin;
const Vector3 side = r[i].basis.get_column(0);
const Vector3 up = r[i].basis.get_column(1);
const Vector3 forward = r[i].basis.get_column(2);
2022-11-17 15:42:00 +00:00
// Collision segments.
if (i != sample_count) {
const Vector3 p2 = r[i + 1].origin;
_collision_segments.push_back(p1);
_collision_segments.push_back(p2);
}
// Path3D as a ribbon.
ribbon.push_back(p1);
// Fish Bone.
2022-11-17 15:42:00 +00:00
const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
bones.push_back(p1);
bones.push_back(p_left);
2022-11-17 15:42:00 +00:00
bones.push_back(p1);
bones.push_back(p_right);
}
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +00:00
add_collision_segments(_collision_segments);
add_lines(bones, path_material);
add_vertices(ribbon, path_material, Mesh::PRIMITIVE_LINE_STRIP);
}
2015-10-08 18:00:40 +00:00
2022-11-17 15:42:00 +00:00
// 2. Draw handles when selected.
if (Path3DEditorPlugin::singleton->get_edited_path() == path) {
2022-11-17 15:42:00 +00:00
PackedVector3Array handle_lines;
PackedVector3Array primary_handle_points;
PackedVector3Array secondary_handle_points;
PackedInt32Array collected_secondary_handle_ids; // Avoid shadowing member on Node3DEditorGizmo.
_secondary_handles_info.resize(c->get_point_count() * 3);
for (int idx = 0; idx < c->get_point_count(); idx++) {
// Collect primary-handles.
const Vector3 pos = c->get_point_position(idx);
primary_handle_points.append(pos);
HandleInfo info;
info.point_idx = idx;
// Collect in-handles except for the first point.
if (idx > 0) {
info.type = HandleType::HANDLE_TYPE_IN;
const int handle_idx = idx * 3 + 0;
collected_secondary_handle_ids.append(handle_idx);
_secondary_handles_info.write[handle_idx] = info;
const Vector3 in = c->get_point_in(idx);
secondary_handle_points.append(pos + in);
handle_lines.append(pos);
handle_lines.append(pos + in);
}
// Collect out-handles except for the last point.
if (idx < c->get_point_count()) {
info.type = HandleType::HANDLE_TYPE_OUT;
const int handle_idx = idx * 3 + 1;
collected_secondary_handle_ids.append(handle_idx);
_secondary_handles_info.write[handle_idx] = info;
const Vector3 out = c->get_point_out(idx);
secondary_handle_points.append(pos + out);
handle_lines.append(pos);
handle_lines.append(pos + out);
2015-10-08 18:00:40 +00:00
}
2022-11-17 15:42:00 +00:00
// Collect tilt-handles.
{
{
info.type = HandleType::HANDLE_TYPE_TILT;
const int handle_idx = idx * 3 + 2;
collected_secondary_handle_ids.append(handle_idx);
_secondary_handles_info.write[handle_idx] = info;
const Basis posture = c->get_point_baked_posture(idx, true);
const Vector3 up = posture.get_column(1);
secondary_handle_points.append(pos + up);
handle_lines.append(pos);
handle_lines.append(pos + up);
}
// Tilt disk.
{
const Basis posture = c->get_point_baked_posture(idx, false);
const Vector3 up = posture.get_column(1);
const Vector3 side = posture.get_column(0);
PackedVector3Array disk;
disk.append(pos);
const int n = 24;
for (int i = 0; i <= n; i++) {
const float a = Math_TAU * i / n;
const Vector3 edge = sin(a) * side + cos(a) * up;
disk.append(pos + edge);
}
add_vertices(disk, path_material, Mesh::PRIMITIVE_LINE_STRIP);
}
}
2015-10-08 18:00:40 +00:00
}
2022-11-17 15:42:00 +00:00
if (handle_lines.size() > 1) {
add_lines(handle_lines, path_thin_material);
}
2022-11-17 15:42:00 +00:00
if (primary_handle_points.size()) {
add_handles(primary_handle_points, handles_material);
}
2022-11-17 15:42:00 +00:00
if (secondary_handle_points.size()) {
add_handles(secondary_handle_points, sec_handles_material, collected_secondary_handle_ids, false, true);
}
2015-10-08 18:00:40 +00:00
}
}
Path3DGizmo::Path3DGizmo(Path3D *p_path) {
path = p_path;
set_node_3d(p_path);
orig_in_length = 0;
orig_out_length = 0;
2015-10-08 18:00:40 +00:00
}
EditorPlugin::AfterGUIInput Path3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
if (!path) {
return EditorPlugin::AFTER_GUI_INPUT_PASS;
}
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
return EditorPlugin::AFTER_GUI_INPUT_PASS;
}
Transform3D gt = path->get_global_transform();
Transform3D it = gt.affine_inverse();
2015-10-08 18:00:40 +00:00
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() == MouseButton::LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->is_command_or_control_pressed()))) {
2015-10-08 18:00:40 +00:00
//click into curve, break it down
Vector<Vector3> v3a = c->tessellate();
int rc = v3a.size();
int closest_seg = -1;
2015-10-08 18:00:40 +00:00
Vector3 closest_seg_point;
if (rc >= 2) {
2022-04-07 10:23:40 +00:00
int idx = 0;
const Vector3 *r = v3a.ptr();
2022-04-07 10:23:40 +00:00
float closest_d = 1e20;
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) {
return EditorPlugin::AFTER_GUI_INPUT_PASS; //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) {
return EditorPlugin::AFTER_GUI_INPUT_PASS; //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 = Geometry2D::get_closest_point_to_segment(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;
Geometry3D::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
}
}
2022-12-23 22:53:16 +00:00
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
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();
return EditorPlugin::AFTER_GUI_INPUT_STOP;
2015-10-08 18:00:40 +00:00
} else {
Vector3 origin;
if (c->get_point_count() == 0) {
origin = path->get_transform().get_origin();
} else {
origin = gt.xform(c->get_point_position(c->get_point_count() - 1));
}
Plane p(p_camera->get_transform().basis.get_column(2), origin);
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();
return EditorPlugin::AFTER_GUI_INPUT_STOP;
2015-10-08 18:00:40 +00:00
}
//add new at pos
}
2021-08-13 21:31:57 +00:00
} else if (mb->is_pressed() && ((mb->get_button_index() == MouseButton::LEFT && curve_del->is_pressed()) || (mb->get_button_index() == MouseButton::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);
2022-11-17 15:42:00 +00:00
real_t dist_to_p_up = p_camera->unproject_position(gt.xform(c->get_point_position(i) + c->get_point_baked_posture(i, true).get_column(1))).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) {
2022-12-23 22:53:16 +00:00
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
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 EditorPlugin::AFTER_GUI_INPUT_STOP;
} else if (dist_to_p_out < click_dist) {
2022-12-23 22:53:16 +00:00
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
2022-11-17 15:42:00 +00:00
ur->create_action(TTR("Reset 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 EditorPlugin::AFTER_GUI_INPUT_STOP;
} else if (dist_to_p_in < click_dist) {
2022-12-23 22:53:16 +00:00
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
2022-11-17 15:42:00 +00:00
ur->create_action(TTR("Reset 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 EditorPlugin::AFTER_GUI_INPUT_STOP;
2022-11-17 15:42:00 +00:00
} else if (dist_to_p_up < click_dist) {
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
ur->create_action(TTR("Reset Point Tilt"));
ur->add_do_method(c.ptr(), "set_point_tilt", i, 0.0f);
ur->add_undo_method(c.ptr(), "set_point_tilt", i, c->get_point_tilt(i));
ur->commit_action();
return EditorPlugin::AFTER_GUI_INPUT_STOP;
}
2015-10-08 18:00:40 +00:00
}
}
if (curve_edit_curve->is_pressed()) {
mb->set_shift_pressed(true);
}
2015-10-08 18:00:40 +00:00
}
return EditorPlugin::AFTER_GUI_INPUT_PASS;
2015-10-08 18:00:40 +00:00
}
void Path3DEditorPlugin::edit(Object *p_object) {
2015-10-08 18:00:40 +00:00
if (p_object) {
path = Object::cast_to<Path3D>(p_object);
2015-10-08 18:00:40 +00:00
if (path) {
if (path->get_curve().is_valid()) {
path->get_curve()->emit_signal(SNAME("changed"));
2015-10-08 18:00:40 +00:00
}
}
} else {
Path3D *pre = path;
2020-04-01 23:20:12 +00:00
path = nullptr;
2015-10-08 18:00:40 +00:00
if (pre) {
pre->get_curve()->emit_signal(SNAME("changed"));
2015-10-08 18:00:40 +00:00
}
}
2022-11-17 15:42:00 +00:00
update_overlays();
//collision_polygon_editor->edit(Object::cast_to<Node>(p_object));
2015-10-08 18:00:40 +00:00
}
bool Path3DEditorPlugin::handles(Object *p_object) const {
return p_object->is_class("Path3D");
2015-10-08 18:00:40 +00:00
}
void Path3DEditorPlugin::make_visible(bool p_visible) {
2015-10-08 18:00:40 +00:00
if (p_visible) {
topmenu_bar->show();
2015-10-08 18:00:40 +00:00
} else {
topmenu_bar->hide();
2015-10-08 18:00:40 +00:00
{
Path3D *pre = path;
2020-04-01 23:20:12 +00:00
path = nullptr;
2015-10-08 18:00:40 +00:00
if (pre && pre->get_curve().is_valid()) {
pre->get_curve()->emit_signal(SNAME("changed"));
2015-10-08 18:00:40 +00:00
}
}
}
}
void Path3DEditorPlugin::_mode_changed(int p_mode) {
curve_create->set_pressed(p_mode == MODE_CREATE);
curve_edit_curve->set_pressed(p_mode == MODE_EDIT_CURVE);
curve_edit->set_pressed(p_mode == MODE_EDIT);
curve_del->set_pressed(p_mode == MODE_DELETE);
2015-10-08 18:00:40 +00:00
}
void Path3DEditorPlugin::_close_curve() {
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
return;
}
if (c->get_point_count() < 2) {
return;
}
2021-09-15 01:24:55 +00:00
if (c->get_point_position(0) == c->get_point_position(c->get_point_count() - 1)) {
return;
}
2022-12-23 22:53:16 +00:00
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
2021-09-15 01:24:55 +00:00
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 Path3DEditorPlugin::_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;
}
}
void Path3DEditorPlugin::_update_theme() {
// TODO: Split the EditorPlugin instance from the UI instance and connect this properly.
// See the 2D path editor for inspiration.
curve_edit->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveEdit"), EditorStringName(EditorIcons)));
curve_edit_curve->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveCurve"), EditorStringName(EditorIcons)));
curve_create->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveCreate"), EditorStringName(EditorIcons)));
curve_del->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveDelete"), EditorStringName(EditorIcons)));
curve_close->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveClose"), EditorStringName(EditorIcons)));
}
void Path3DEditorPlugin::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
curve_create->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_CREATE));
curve_edit_curve->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_CURVE));
curve_edit->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT));
curve_del->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_DELETE));
curve_close->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_close_curve));
2022-08-29 09:04:31 +00:00
_update_theme();
} break;
case NOTIFICATION_READY: {
Node3DEditor::get_singleton()->connect("theme_changed", callable_mp(this, &Path3DEditorPlugin::_update_theme));
} break;
}
2015-10-08 18:00:40 +00:00
}
void Path3DEditorPlugin::_bind_methods() {
2015-10-08 18:00:40 +00:00
}
2020-04-01 23:20:12 +00:00
Path3DEditorPlugin *Path3DEditorPlugin::singleton = nullptr;
2015-10-08 18:00:40 +00:00
Path3DEditorPlugin::Path3DEditorPlugin() {
2020-04-01 23:20:12 +00:00
path = nullptr;
singleton = this;
mirror_handle_angle = true;
mirror_handle_length = true;
2015-10-08 18:00:40 +00:00
Ref<Path3DGizmoPlugin> gizmo_plugin;
gizmo_plugin.instantiate();
Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
2015-10-08 18:00:40 +00:00
topmenu_bar = memnew(HBoxContainer);
topmenu_bar->hide();
Node3DEditor::get_singleton()->add_control_to_menu_panel(topmenu_bar);
curve_edit = memnew(Button);
curve_edit->set_theme_type_variation("FlatButton");
2015-10-08 18:00:40 +00:00
curve_edit->set_toggle_mode(true);
curve_edit->set_focus_mode(Control::FOCUS_NONE);
curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Drag: Select Control Points") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point"));
topmenu_bar->add_child(curve_edit);
curve_edit_curve = memnew(Button);
curve_edit_curve->set_theme_type_variation("FlatButton");
curve_edit_curve->set_toggle_mode(true);
curve_edit_curve->set_focus_mode(Control::FOCUS_NONE);
curve_edit_curve->set_tooltip_text(TTR("Select Control Points (Shift+Drag)"));
topmenu_bar->add_child(curve_edit_curve);
curve_create = memnew(Button);
curve_create->set_theme_type_variation("FlatButton");
2015-10-08 18:00:40 +00:00
curve_create->set_toggle_mode(true);
curve_create->set_focus_mode(Control::FOCUS_NONE);
curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
topmenu_bar->add_child(curve_create);
curve_del = memnew(Button);
curve_del->set_theme_type_variation("FlatButton");
2015-10-08 18:00:40 +00:00
curve_del->set_toggle_mode(true);
curve_del->set_focus_mode(Control::FOCUS_NONE);
curve_del->set_tooltip_text(TTR("Delete Point"));
topmenu_bar->add_child(curve_del);
curve_close = memnew(Button);
curve_close->set_theme_type_variation("FlatButton");
2015-10-08 18:00:40 +00:00
curve_close->set_focus_mode(Control::FOCUS_NONE);
curve_close->set_tooltip_text(TTR("Close Curve"));
topmenu_bar->add_child(curve_close);
2015-10-08 18:00:40 +00:00
PopupMenu *menu;
handle_menu = memnew(MenuButton);
handle_menu->set_flat(false);
handle_menu->set_theme_type_variation("FlatMenuButton");
handle_menu->set_text(TTR("Options"));
topmenu_bar->add_child(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", callable_mp(this, &Path3DEditorPlugin::_handle_option_pressed));
2015-10-08 18:00:40 +00:00
curve_edit->set_pressed(true);
}
2015-10-08 18:00:40 +00:00
Path3DEditorPlugin::~Path3DEditorPlugin() {
}
Ref<EditorNode3DGizmo> Path3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {
Ref<Path3DGizmo> ref;
2015-10-08 18:00:40 +00:00
Path3D *path = Object::cast_to<Path3D>(p_spatial);
if (path) {
ref = Ref<Path3DGizmo>(memnew(Path3DGizmo(path)));
}
2015-10-08 18:00:40 +00:00
return ref;
2015-10-08 18:00:40 +00:00
}
String Path3DGizmoPlugin::get_gizmo_name() const {
return "Path3D";
}
int Path3DGizmoPlugin::get_priority() const {
return -1;
}
Path3DGizmoPlugin::Path3DGizmoPlugin() {
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", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
create_handle_material("sec_handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorCurveHandle"), EditorStringName(EditorIcons)));
2015-10-08 18:00:40 +00:00
}