Debian Codegraph @

modules/navigation_3d/editor/navigation_region_3d_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* navigation_region_3d_editor_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_region_3d_editor_plugin.h"
32
33 #include "editor/editor_node.h"
34 #include "editor/editor_string_names.h"
35 #include "editor/inspector/multi_node_edit.h"
36 #include "editor/scene/3d/node_3d_editor_plugin.h"
37 #include "scene/3d/navigation/navigation_region_3d.h"
38 #include "scene/gui/box_container.h"
39 #include "scene/gui/button.h"
40 #include "scene/gui/dialogs.h"
41 #include "scene/gui/label.h"
42 #include "servers/navigation_3d/navigation_server_3d.h"
43
44 void NavigationRegion3DEditor::_node_removed(Node *p_node) {
45 if (selected_regions.is_empty()) {
46 return;
47 }
48
49 NavigationRegion3D *region = Object::cast_to<NavigationRegion3D>(p_node);
50
51 if (region && selected_regions.has(region)) {
52 selected_regions.erase(region);
53 if (selected_regions.is_empty()) {
54 hide();
55 }
56 }
57 }
58
59 void NavigationRegion3DEditor::_notification(int p_what) {
60 switch (p_what) {
61 case NOTIFICATION_ENTER_TREE: {
62 button_bake->set_button_icon(get_theme_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
63 button_reset->set_button_icon(get_theme_icon(SNAME("Reload"), EditorStringName(EditorIcons)));
64 } break;
65 case NOTIFICATION_PROCESS: {
66 if (currently_baking_region) {
67 const String bake_state_msg = NavigationServer3D::get_singleton()->get_baking_navigation_mesh_state_msg(currently_baking_region->get_navigation_mesh());
68 multibake_dialog->set_text(itos(processed_regions_to_bake_count) + " / " + itos(processed_regions_to_bake_count_max) + " - Baking navmesh from region '" + currently_baking_region->get_name() + "'.\n\nBake state: " + bake_state_msg + "\n\nDo NOT change nodes by any means while the baking is parsing the SceneTree.");
69 } else {
70 multibake_dialog->set_text("");
71 }
72 }
73 }
74 }
75
76 void NavigationRegion3DEditor::_bake_pressed() {
77 button_bake->set_pressed(false);
78
79 if (selected_regions.is_empty()) {
80 return;
81 }
82
83 if (bake_in_process) {
84 return;
85 }
86
87 HashSet<Ref<NavigationMesh>> unique_navmeshes;
88 regions_to_bake.clear();
89 regions_with_navmesh_to_bake.clear();
90
91 for (NavigationRegion3D *region : selected_regions) {
92 ERR_CONTINUE(region == nullptr);
93 Ref<NavigationMesh> navmesh = region->get_navigation_mesh();
94 if (navmesh.is_null()) {
95 err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work."));
96 err_dialog->popup_centered();
97 return;
98 }
99
100 String path = navmesh->get_path();
101 if (!path.is_resource_file()) {
102 int srpos = path.find("::");
103 if (srpos != -1) {
104 String base = path.substr(0, srpos);
105 if (ResourceLoader::get_resource_type(base) == "PackedScene") {
106 if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
107 err_dialog->set_text(TTR("Cannot generate navigation mesh because it does not belong to the edited scene. Make it unique first."));
108 err_dialog->popup_centered();
109 return;
110 }
111 } else {
112 if (FileAccess::exists(base + ".import")) {
113 err_dialog->set_text(TTR("Cannot generate navigation mesh because it belongs to a resource which was imported."));
114 err_dialog->popup_centered();
115 return;
116 }
117 }
118 }
119 } else {
120 if (FileAccess::exists(path + ".import")) {
121 err_dialog->set_text(TTR("Cannot generate navigation mesh because the resource was imported from another type."));
122 err_dialog->popup_centered();
123 return;
124 }
125 }
126
127 regions_to_bake.push_back(region);
128
129 if (unique_navmeshes.has(navmesh)) {
130 // No point (re)baking the same resource in case of multi select.
131 // Trying to bake the same navmesh twice would trigger an error.
132 continue;
133 }
134 unique_navmeshes.insert(navmesh);
135 regions_with_navmesh_to_bake.push_back(region);
136 }
137
138 if (!regions_with_navmesh_to_bake.is_empty()) {
139 multibake_dialog->set_ok_button_text(TTR("Bake"));
140 multibake_dialog->get_ok_button()->set_disabled(false);
141 multibake_dialog->set_text("Attempting to bake " + itos(regions_with_navmesh_to_bake.size()) + " unique navmesh(es) from " + itos(regions_to_bake.size()) + " selected NavigationRegion3D node(s).\n\nThis can take some time and freeze the Editor temporarily.\n\nDo NOT change nodes by any means while the baking is parsing the SceneTree.");
142 multibake_dialog->popup_centered();
143 if (regions_with_navmesh_to_bake.size() == 1) {
144 // If we only have a single region start bake immediately.
145 _on_navmesh_multibake_confirmed();
146 }
147 }
148 }
149
150 void NavigationRegion3DEditor::_on_navmesh_multibake_confirmed() {
151 multibake_dialog->get_ok_button()->set_disabled(true);
152
153 bake_in_process = true;
154 region_baking_canceled = false;
155 processed_regions_to_bake_count = 0;
156 processed_regions_to_bake_count_max = regions_with_navmesh_to_bake.size();
157
158 set_process(true);
159 _process_regions_to_bake();
160 }
161
162 void NavigationRegion3DEditor::_process_regions_to_bake() {
163 if (region_baking_canceled) {
164 region_baking_canceled = false;
165 regions_with_navmesh_to_bake.clear();
166 }
167
168 if (regions_with_navmesh_to_bake.is_empty()) {
169 regions_to_bake.clear();
170 multibake_dialog->set_visible(false);
171 set_process(false);
172 currently_baking_region = nullptr;
173 bake_in_process = false;
174 return;
175 }
176
177 NavigationRegion3D *region_to_bake = regions_with_navmesh_to_bake[0];
178 regions_with_navmesh_to_bake.remove_at_unordered(0);
179 processed_regions_to_bake_count += 1;
180 if (region_to_bake && region_to_bake->get_navigation_mesh().is_valid()) {
181 currently_baking_region = region_to_bake;
182 region_to_bake->connect(SNAME("bake_finished"), callable_mp(this, &NavigationRegion3DEditor::_process_regions_to_bake), CONNECT_ONE_SHOT);
183 region_to_bake->bake_navigation_mesh(true);
184 return;
185 } else {
186 _process_regions_to_bake();
187 }
188 }
189
190 void NavigationRegion3DEditor::_on_navmesh_multibake_canceled() {
191 if (bake_in_process) {
192 region_baking_canceled = true;
193 return;
194 }
195
196 multibake_dialog->set_visible(false);
197 regions_to_bake.clear();
198 regions_with_navmesh_to_bake.clear();
199 processed_regions_to_bake_count = 0;
200 processed_regions_to_bake_count_max = 0;
201 region_baking_canceled = false;
202 currently_baking_region = nullptr;
203 bake_in_process = false;
204 }
205
206 void NavigationRegion3DEditor::_clear_pressed() {
207 button_bake->set_pressed(false);
208 bake_info->set_text("");
209
210 if (!selected_regions.is_empty()) {
211 for (NavigationRegion3D *region : selected_regions) {
212 if (region->get_navigation_mesh().is_valid()) {
213 region->get_navigation_mesh()->clear();
214 region->update_gizmos();
215 }
216 }
217 }
218 }
219
220 void NavigationRegion3DEditor::edit(LocalVector<NavigationRegion3D *> p_regions) {
221 if (p_regions.is_empty()) {
222 return;
223 }
224
225 selected_regions = p_regions;
226 }
227
228 NavigationRegion3DEditor::NavigationRegion3DEditor() {
229 bake_hbox = memnew(HBoxContainer);
230
231 button_bake = memnew(Button);
232 button_bake->set_theme_type_variation(SceneStringName(FlatButton));
233 bake_hbox->add_child(button_bake);
234 button_bake->set_toggle_mode(true);
235 button_bake->set_text(TTR("Bake NavigationMesh"));
236 button_bake->set_tooltip_text(TTR("Bakes the NavigationMesh by first parsing the scene for source geometry and then creating the navigation mesh vertices and polygons."));
237 button_bake->connect(SceneStringName(pressed), callable_mp(this, &NavigationRegion3DEditor::_bake_pressed));
238
239 button_reset = memnew(Button);
240 button_reset->set_theme_type_variation(SceneStringName(FlatButton));
241 bake_hbox->add_child(button_reset);
242 button_reset->set_text(TTR("Clear NavigationMesh"));
243 button_reset->set_tooltip_text(TTR("Clears the internal NavigationMesh vertices and polygons."));
244 button_reset->connect(SceneStringName(pressed), callable_mp(this, &NavigationRegion3DEditor::_clear_pressed));
245
246 bake_info = memnew(Label);
247 bake_info->set_focus_mode(FOCUS_ACCESSIBILITY);
248 bake_hbox->add_child(bake_info);
249
250 err_dialog = memnew(AcceptDialog);
251 add_child(err_dialog);
252
253 multibake_dialog = memnew(ConfirmationDialog);
254 add_child(multibake_dialog);
255 multibake_dialog->connect(SceneStringName(confirmed), callable_mp(this, &NavigationRegion3DEditor::_on_navmesh_multibake_confirmed));
256 multibake_dialog->connect(SNAME("canceled"), callable_mp(this, &NavigationRegion3DEditor::_on_navmesh_multibake_canceled));
257 multibake_dialog->set_hide_on_ok(false);
258 multibake_dialog->set_title(TTR("Baking NavigationMesh ..."));
259 }
260
261 void NavigationRegion3DEditorPlugin::edit(Object *p_object) {
262 LocalVector<NavigationRegion3D *> regions;
263
264 {
265 NavigationRegion3D *region = Object::cast_to<NavigationRegion3D>(p_object);
266 if (region) {
267 regions.push_back(region);
268 navigation_region_editor->edit(regions);
269 return;
270 }
271 }
272
273 Ref<MultiNodeEdit> mne = Ref<MultiNodeEdit>(p_object);
274 Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
275 if (mne.is_valid() && edited_scene) {
276 for (int i = 0; i < mne->get_node_count(); i++) {
277 NavigationRegion3D *region = Object::cast_to<NavigationRegion3D>(edited_scene->get_node(mne->get_node(i)));
278 if (region) {
279 regions.push_back(region);
280 }
281 }
282 }
283
284 navigation_region_editor->edit(regions);
285 }
286
287 bool NavigationRegion3DEditorPlugin::handles(Object *p_object) const {
288 if (Object::cast_to<NavigationRegion3D>(p_object)) {
289 return true;
290 }
291
292 Ref<MultiNodeEdit> mne = Ref<MultiNodeEdit>(p_object);
293 Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
294 if (mne.is_valid() && edited_scene) {
295 for (int i = 0; i < mne->get_node_count(); i++) {
296 if (Object::cast_to<NavigationRegion3D>(edited_scene->get_node(mne->get_node(i)))) {
297 return true;
298 }
299 }
300 }
301 return false;
302 }
303
304 void NavigationRegion3DEditorPlugin::make_visible(bool p_visible) {
305 if (p_visible) {
306 navigation_region_editor->show();
307 navigation_region_editor->bake_hbox->show();
308 } else {
309 navigation_region_editor->hide();
310 navigation_region_editor->bake_hbox->hide();
311 navigation_region_editor->edit(LocalVector<NavigationRegion3D *>());
312 }
313 }
314
315 NavigationRegion3DEditorPlugin::NavigationRegion3DEditorPlugin() {
316 navigation_region_editor = memnew(NavigationRegion3DEditor);
317 EditorNode::get_singleton()->get_gui_base()->add_child(navigation_region_editor);
318 add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_region_editor->bake_hbox);
319 navigation_region_editor->hide();
320 navigation_region_editor->bake_hbox->hide();
321
322 gizmo_plugin.instantiate();
323 Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
324 }