| 1 | /**************************************************************************/ |
| 2 | /* navigation_link_3d_gizmo_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "navigation_link_3d_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_undo_redo_manager.h" |
| 34 | #include "editor/plugins/node_3d_editor_plugin.h" |
| 35 | #include "scene/3d/navigation_link_3d.h" |
| 36 | #include "servers/navigation_server_3d.h" |
| 37 | |
| 38 | NavigationLink3DGizmoPlugin::NavigationLink3DGizmoPlugin() { |
| 39 | create_material("navigation_link_material" , NavigationServer3D::get_singleton()->get_debug_navigation_link_connection_color()); |
| 40 | create_material("navigation_link_material_disabled" , NavigationServer3D::get_singleton()->get_debug_navigation_link_connection_disabled_color()); |
| 41 | create_handle_material("handles" ); |
| 42 | } |
| 43 | |
| 44 | bool NavigationLink3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 45 | return Object::cast_to<NavigationLink3D>(p_spatial) != nullptr; |
| 46 | } |
| 47 | |
| 48 | String NavigationLink3DGizmoPlugin::get_gizmo_name() const { |
| 49 | return "NavigationLink3D" ; |
| 50 | } |
| 51 | |
| 52 | int NavigationLink3DGizmoPlugin::get_priority() const { |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | void NavigationLink3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 57 | NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d()); |
| 58 | |
| 59 | RID nav_map = link->get_world_3d()->get_navigation_map(); |
| 60 | real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map); |
| 61 | Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map); |
| 62 | Vector3::Axis up_axis = up_vector.max_axis_index(); |
| 63 | |
| 64 | Vector3 start_position = link->get_start_position(); |
| 65 | Vector3 end_position = link->get_end_position(); |
| 66 | |
| 67 | Ref<Material> link_material = get_material("navigation_link_material" , p_gizmo); |
| 68 | Ref<Material> link_material_disabled = get_material("navigation_link_material_disabled" , p_gizmo); |
| 69 | Ref<Material> handles_material = get_material("handles" ); |
| 70 | |
| 71 | p_gizmo->clear(); |
| 72 | |
| 73 | // Draw line between the points. |
| 74 | Vector<Vector3> lines; |
| 75 | lines.append(start_position); |
| 76 | lines.append(end_position); |
| 77 | |
| 78 | // Draw start position search radius |
| 79 | for (int i = 0; i < 30; i++) { |
| 80 | // Create a circle |
| 81 | const float ra = Math::deg_to_rad((float)(i * 12)); |
| 82 | const float rb = Math::deg_to_rad((float)((i + 1) * 12)); |
| 83 | const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius; |
| 84 | const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius; |
| 85 | |
| 86 | // Draw axis-aligned circle |
| 87 | switch (up_axis) { |
| 88 | case Vector3::AXIS_X: |
| 89 | lines.append(start_position + Vector3(0, a.x, a.y)); |
| 90 | lines.append(start_position + Vector3(0, b.x, b.y)); |
| 91 | break; |
| 92 | case Vector3::AXIS_Y: |
| 93 | lines.append(start_position + Vector3(a.x, 0, a.y)); |
| 94 | lines.append(start_position + Vector3(b.x, 0, b.y)); |
| 95 | break; |
| 96 | case Vector3::AXIS_Z: |
| 97 | lines.append(start_position + Vector3(a.x, a.y, 0)); |
| 98 | lines.append(start_position + Vector3(b.x, b.y, 0)); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Draw end position search radius |
| 104 | for (int i = 0; i < 30; i++) { |
| 105 | // Create a circle |
| 106 | const float ra = Math::deg_to_rad((float)(i * 12)); |
| 107 | const float rb = Math::deg_to_rad((float)((i + 1) * 12)); |
| 108 | const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius; |
| 109 | const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius; |
| 110 | |
| 111 | // Draw axis-aligned circle |
| 112 | switch (up_axis) { |
| 113 | case Vector3::AXIS_X: |
| 114 | lines.append(end_position + Vector3(0, a.x, a.y)); |
| 115 | lines.append(end_position + Vector3(0, b.x, b.y)); |
| 116 | break; |
| 117 | case Vector3::AXIS_Y: |
| 118 | lines.append(end_position + Vector3(a.x, 0, a.y)); |
| 119 | lines.append(end_position + Vector3(b.x, 0, b.y)); |
| 120 | break; |
| 121 | case Vector3::AXIS_Z: |
| 122 | lines.append(end_position + Vector3(a.x, a.y, 0)); |
| 123 | lines.append(end_position + Vector3(b.x, b.y, 0)); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | p_gizmo->add_lines(lines, link->is_enabled() ? link_material : link_material_disabled); |
| 129 | p_gizmo->add_collision_segments(lines); |
| 130 | |
| 131 | Vector<Vector3> handles; |
| 132 | handles.append(start_position); |
| 133 | handles.append(end_position); |
| 134 | p_gizmo->add_handles(handles, handles_material); |
| 135 | } |
| 136 | |
| 137 | String NavigationLink3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 138 | return p_id == 0 ? TTR("Start Location" ) : TTR("End Location" ); |
| 139 | } |
| 140 | |
| 141 | Variant NavigationLink3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 142 | NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d()); |
| 143 | return p_id == 0 ? link->get_start_position() : link->get_end_position(); |
| 144 | } |
| 145 | |
| 146 | void NavigationLink3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { |
| 147 | NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d()); |
| 148 | |
| 149 | Transform3D gt = link->get_global_transform(); |
| 150 | Transform3D gi = gt.affine_inverse(); |
| 151 | |
| 152 | Transform3D ct = p_camera->get_global_transform(); |
| 153 | Vector3 cam_dir = ct.basis.get_column(Vector3::AXIS_Z); |
| 154 | |
| 155 | Vector3 ray_from = p_camera->project_ray_origin(p_point); |
| 156 | Vector3 ray_dir = p_camera->project_ray_normal(p_point); |
| 157 | |
| 158 | Vector3 position = p_id == 0 ? link->get_start_position() : link->get_end_position(); |
| 159 | Plane move_plane = Plane(cam_dir, gt.xform(position)); |
| 160 | |
| 161 | Vector3 intersection; |
| 162 | if (!move_plane.intersects_ray(ray_from, ray_dir, &intersection)) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if (Node3DEditor::get_singleton()->is_snap_enabled()) { |
| 167 | double snap = Node3DEditor::get_singleton()->get_translate_snap(); |
| 168 | intersection.snap(Vector3(snap, snap, snap)); |
| 169 | } |
| 170 | |
| 171 | position = gi.xform(intersection); |
| 172 | if (p_id == 0) { |
| 173 | link->set_start_position(position); |
| 174 | } else if (p_id == 1) { |
| 175 | link->set_end_position(position); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void NavigationLink3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { |
| 180 | NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d()); |
| 181 | |
| 182 | if (p_cancel) { |
| 183 | if (p_id == 0) { |
| 184 | link->set_start_position(p_restore); |
| 185 | } else { |
| 186 | link->set_end_position(p_restore); |
| 187 | } |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 192 | if (p_id == 0) { |
| 193 | ur->create_action(TTR("Change Start Position" )); |
| 194 | ur->add_do_method(link, "set_start_position" , link->get_start_position()); |
| 195 | ur->add_undo_method(link, "set_start_position" , p_restore); |
| 196 | } else { |
| 197 | ur->create_action(TTR("Change End Position" )); |
| 198 | ur->add_do_method(link, "set_end_position" , link->get_end_position()); |
| 199 | ur->add_undo_method(link, "set_end_position" , p_restore); |
| 200 | } |
| 201 | |
| 202 | ur->commit_action(); |
| 203 | } |
| 204 | |