Added node for Navigation links

This commit is contained in:
Josh Jones
2022-01-30 15:39:52 -08:00
parent 4808d01b2b
commit 3dd59013f4
34 changed files with 2563 additions and 49 deletions

View File

@@ -0,0 +1,191 @@
/*************************************************************************/
/* navigation_link_2d_editor_plugin.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 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 "navigation_link_2d_editor_plugin.h"
#include "canvas_item_editor_plugin.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "editor/editor_undo_redo_manager.h"
#include "servers/navigation_server_3d.h"
void NavigationLink2DEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
get_tree()->connect("node_removed", callable_mp(this, &NavigationLink2DEditor::_node_removed));
} break;
case NOTIFICATION_EXIT_TREE: {
get_tree()->disconnect("node_removed", callable_mp(this, &NavigationLink2DEditor::_node_removed));
} break;
}
}
void NavigationLink2DEditor::_node_removed(Node *p_node) {
if (p_node == node) {
node = nullptr;
}
}
bool NavigationLink2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
if (!node || !node->is_visible_in_tree()) {
return false;
}
real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
if (mb->is_pressed()) {
// Start location
if (xform.xform(node->get_start_location()).distance_to(mb->get_position()) < grab_threshold) {
start_grabbed = true;
original_start_location = node->get_start_location();
return true;
} else {
start_grabbed = false;
}
// End location
if (xform.xform(node->get_end_location()).distance_to(mb->get_position()) < grab_threshold) {
end_grabbed = true;
original_end_location = node->get_end_location();
return true;
} else {
end_grabbed = false;
}
} else {
if (start_grabbed) {
undo_redo->create_action(TTR("Set start_location"));
undo_redo->add_do_method(node, "set_start_location", node->get_start_location());
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
undo_redo->add_undo_method(node, "set_start_location", original_start_location);
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
undo_redo->commit_action();
start_grabbed = false;
return true;
}
if (end_grabbed) {
undo_redo->create_action(TTR("Set end_location"));
undo_redo->add_do_method(node, "set_end_location", node->get_end_location());
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
undo_redo->add_undo_method(node, "set_end_location", original_end_location);
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
undo_redo->commit_action();
end_grabbed = false;
return true;
}
}
}
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
point = node->get_global_transform().affine_inverse().xform(point);
if (start_grabbed) {
node->set_start_location(point);
canvas_item_editor->update_viewport();
return true;
}
if (end_grabbed) {
node->set_end_location(point);
canvas_item_editor->update_viewport();
return true;
}
}
return false;
}
void NavigationLink2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
if (!node || !node->is_visible_in_tree()) {
return;
}
Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
Vector2 global_start_location = gt.xform(node->get_start_location());
Vector2 global_end_location = gt.xform(node->get_end_location());
// Only drawing the handles here, since the debug rendering will fill in the rest.
const Ref<Texture2D> handle = get_theme_icon(SNAME("EditorHandle"), SNAME("EditorIcons"));
p_overlay->draw_texture(handle, global_start_location - handle->get_size() / 2);
p_overlay->draw_texture(handle, global_end_location - handle->get_size() / 2);
}
void NavigationLink2DEditor::edit(NavigationLink2D *p_node) {
if (!canvas_item_editor) {
canvas_item_editor = CanvasItemEditor::get_singleton();
}
if (p_node) {
node = p_node;
} else {
node = nullptr;
}
canvas_item_editor->update_viewport();
}
NavigationLink2DEditor::NavigationLink2DEditor() {
undo_redo = EditorNode::get_undo_redo();
}
///////////////////////
void NavigationLink2DEditorPlugin::edit(Object *p_object) {
editor->edit(Object::cast_to<NavigationLink2D>(p_object));
}
bool NavigationLink2DEditorPlugin::handles(Object *p_object) const {
return Object::cast_to<NavigationLink2D>(p_object) != nullptr;
}
void NavigationLink2DEditorPlugin::make_visible(bool p_visible) {
if (!p_visible) {
edit(nullptr);
}
}
NavigationLink2DEditorPlugin::NavigationLink2DEditorPlugin() {
editor = memnew(NavigationLink2DEditor);
EditorNode::get_singleton()->get_gui_base()->add_child(editor);
}

View File

@@ -0,0 +1,83 @@
/*************************************************************************/
/* navigation_link_2d_editor_plugin.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 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. */
/*************************************************************************/
#ifndef NAVIGATION_LINK_2D_EDITOR_PLUGIN_H
#define NAVIGATION_LINK_2D_EDITOR_PLUGIN_H
#include "editor/editor_plugin.h"
#include "scene/2d/navigation_link_2d.h"
class CanvasItemEditor;
class EditorUndoRedoManager;
class NavigationLink2DEditor : public Control {
GDCLASS(NavigationLink2DEditor, Control);
Ref<EditorUndoRedoManager> undo_redo;
CanvasItemEditor *canvas_item_editor = nullptr;
NavigationLink2D *node;
bool start_grabbed = false;
Vector2 original_start_location;
bool end_grabbed = false;
Vector2 original_end_location;
protected:
void _notification(int p_what);
void _node_removed(Node *p_node);
public:
bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
void forward_canvas_draw_over_viewport(Control *p_overlay);
void edit(NavigationLink2D *p_node);
NavigationLink2DEditor();
};
class NavigationLink2DEditorPlugin : public EditorPlugin {
GDCLASS(NavigationLink2DEditorPlugin, EditorPlugin);
NavigationLink2DEditor *editor = nullptr;
public:
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return editor->forward_canvas_gui_input(p_event); }
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { editor->forward_canvas_draw_over_viewport(p_overlay); }
virtual String get_name() const override { return "NavigationLink2D"; }
bool has_main_screen() const override { return false; }
virtual void edit(Object *p_object) override;
virtual bool handles(Object *p_object) const override;
virtual void make_visible(bool p_visible) override;
NavigationLink2DEditorPlugin();
};
#endif // NAVIGATION_LINK_2D_EDITOR_PLUGIN_H

View File

@@ -54,6 +54,7 @@
#include "scene/3d/lightmap_probe.h"
#include "scene/3d/marker_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/navigation_link_3d.h"
#include "scene/3d/navigation_region_3d.h"
#include "scene/3d/occluder_instance_3d.h"
#include "scene/3d/ray_cast_3d.h"
@@ -4998,6 +4999,175 @@ void NavigationRegion3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
}
}
////
NavigationLink3DGizmoPlugin::NavigationLink3DGizmoPlugin() {
create_material("navigation_link_material", NavigationServer3D::get_singleton()->get_debug_navigation_link_connection_color());
create_material("navigation_link_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_link_connection_disabled_color());
create_handle_material("handles");
}
bool NavigationLink3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
return Object::cast_to<NavigationLink3D>(p_spatial) != nullptr;
}
String NavigationLink3DGizmoPlugin::get_gizmo_name() const {
return "NavigationLink3D";
}
int NavigationLink3DGizmoPlugin::get_priority() const {
return -1;
}
void NavigationLink3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_spatial_node());
RID nav_map = link->get_world_3d()->get_navigation_map();
real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map);
Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map);
Vector3::Axis up_axis = up_vector.max_axis_index();
Vector3 start_location = link->get_start_location();
Vector3 end_location = link->get_end_location();
Ref<Material> link_material = get_material("navigation_link_material", p_gizmo);
Ref<Material> link_material_disabled = get_material("navigation_link_material_disabled", p_gizmo);
Ref<Material> handles_material = get_material("handles");
p_gizmo->clear();
// Draw line between the points.
Vector<Vector3> lines;
lines.append(start_location);
lines.append(end_location);
// Draw start location search radius
for (int i = 0; i < 30; i++) {
// Create a circle
const float ra = Math::deg_to_rad((float)(i * 12));
const float rb = Math::deg_to_rad((float)((i + 1) * 12));
const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;
// Draw axis-aligned circle
switch (up_axis) {
case Vector3::AXIS_X:
lines.append(start_location + Vector3(0, a.x, a.y));
lines.append(start_location + Vector3(0, b.x, b.y));
break;
case Vector3::AXIS_Y:
lines.append(start_location + Vector3(a.x, 0, a.y));
lines.append(start_location + Vector3(b.x, 0, b.y));
break;
case Vector3::AXIS_Z:
lines.append(start_location + Vector3(a.x, a.y, 0));
lines.append(start_location + Vector3(b.x, b.y, 0));
break;
}
}
// Draw end location search radius
for (int i = 0; i < 30; i++) {
// Create a circle
const float ra = Math::deg_to_rad((float)(i * 12));
const float rb = Math::deg_to_rad((float)((i + 1) * 12));
const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;
// Draw axis-aligned circle
switch (up_axis) {
case Vector3::AXIS_X:
lines.append(end_location + Vector3(0, a.x, a.y));
lines.append(end_location + Vector3(0, b.x, b.y));
break;
case Vector3::AXIS_Y:
lines.append(end_location + Vector3(a.x, 0, a.y));
lines.append(end_location + Vector3(b.x, 0, b.y));
break;
case Vector3::AXIS_Z:
lines.append(end_location + Vector3(a.x, a.y, 0));
lines.append(end_location + Vector3(b.x, b.y, 0));
break;
}
}
p_gizmo->add_lines(lines, link->is_enabled() ? link_material : link_material_disabled);
p_gizmo->add_collision_segments(lines);
Vector<Vector3> handles;
handles.append(start_location);
handles.append(end_location);
p_gizmo->add_handles(handles, handles_material);
}
String NavigationLink3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
return p_id == 0 ? TTR("Start Location") : TTR("End Location");
}
Variant NavigationLink3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_spatial_node());
return p_id == 0 ? link->get_start_location() : link->get_end_location();
}
void NavigationLink3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_spatial_node());
Transform3D gt = link->get_global_transform();
Transform3D gi = gt.affine_inverse();
Transform3D ct = p_camera->get_global_transform();
Vector3 cam_dir = ct.basis.get_column(Vector3::AXIS_Z);
Vector3 ray_from = p_camera->project_ray_origin(p_point);
Vector3 ray_dir = p_camera->project_ray_normal(p_point);
Vector3 location = p_id == 0 ? link->get_start_location() : link->get_end_location();
Plane move_plane = Plane(cam_dir, gt.xform(location));
Vector3 intersection;
if (!move_plane.intersects_ray(ray_from, ray_dir, &intersection)) {
return;
}
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
double snap = Node3DEditor::get_singleton()->get_translate_snap();
intersection.snap(Vector3(snap, snap, snap));
}
location = gi.xform(intersection);
if (p_id == 0) {
link->set_start_location(location);
} else if (p_id == 1) {
link->set_end_location(location);
}
}
void NavigationLink3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_spatial_node());
if (p_cancel) {
if (p_id == 0) {
link->set_start_location(p_restore);
} else {
link->set_end_location(p_restore);
}
return;
}
Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
if (p_id == 0) {
ur->create_action(TTR("Change Start Location"));
ur->add_do_method(link, "set_start_location", link->get_start_location());
ur->add_undo_method(link, "set_start_location", p_restore);
} else {
ur->create_action(TTR("Change End Location"));
ur->add_do_method(link, "set_end_location", link->get_end_location());
ur->add_undo_method(link, "set_end_location", p_restore);
}
ur->commit_action();
}
//////
#define BODY_A_RADIUS 0.25

View File

@@ -631,6 +631,23 @@ public:
NavigationRegion3DGizmoPlugin();
};
class NavigationLink3DGizmoPlugin : public EditorNode3DGizmoPlugin {
GDCLASS(NavigationLink3DGizmoPlugin, EditorNode3DGizmoPlugin);
public:
bool has_gizmo(Node3D *p_spatial) override;
String get_gizmo_name() const override;
int get_priority() const override;
void redraw(EditorNode3DGizmo *p_gizmo) override;
String get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override;
Variant get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override;
void set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) override;
void commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false) override;
NavigationLink3DGizmoPlugin();
};
class JointGizmosDrawer {
public:
static Basis look_body(const Transform3D &p_joint_transform, const Transform3D &p_body_transform);

View File

@@ -7520,6 +7520,7 @@ void Node3DEditor::_register_all_gizmos() {
add_gizmo_plugin(Ref<CollisionObject3DGizmoPlugin>(memnew(CollisionObject3DGizmoPlugin)));
add_gizmo_plugin(Ref<CollisionShape3DGizmoPlugin>(memnew(CollisionShape3DGizmoPlugin)));
add_gizmo_plugin(Ref<CollisionPolygon3DGizmoPlugin>(memnew(CollisionPolygon3DGizmoPlugin)));
add_gizmo_plugin(Ref<NavigationLink3DGizmoPlugin>(memnew(NavigationLink3DGizmoPlugin)));
add_gizmo_plugin(Ref<NavigationRegion3DGizmoPlugin>(memnew(NavigationRegion3DGizmoPlugin)));
add_gizmo_plugin(Ref<Joint3DGizmoPlugin>(memnew(Joint3DGizmoPlugin)));
add_gizmo_plugin(Ref<PhysicalBone3DGizmoPlugin>(memnew(PhysicalBone3DGizmoPlugin)));