2023-01-05 13:25:55 +01:00
/**************************************************************************/
/* navigation_mesh_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. */
/**************************************************************************/
2018-01-05 00:50:27 +01:00
2017-02-28 13:10:29 +01:00
# include "navigation_mesh_editor_plugin.h"
2018-05-30 15:59:42 +02:00
2022-03-28 14:10:28 +02:00
# ifdef TOOLS_ENABLED
2018-09-11 18:13:45 +02:00
# include "core/io/marshalls.h"
# include "core/io/resource_saver.h"
2022-02-12 02:46:22 +01:00
# include "editor/editor_node.h"
2023-08-13 02:33:39 +02:00
# include "editor/editor_string_names.h"
2020-03-26 22:49:16 +01:00
# include "scene/3d/mesh_instance_3d.h"
2023-07-07 15:59:10 +02:00
# include "scene/3d/navigation_region_3d.h"
2017-02-28 13:10:29 +01:00
# include "scene/gui/box_container.h"
2023-04-07 18:59:49 +02:00
# include "scene/gui/button.h"
# include "scene/gui/dialogs.h"
# include "scene/gui/label.h"
2023-05-23 19:55:31 +02:00
# include "scene/resources/navigation_mesh_source_geometry_data_3d.h"
2017-02-28 13:10:29 +01:00
void NavigationMeshEditor : : _node_removed ( Node * p_node ) {
if ( p_node = = node ) {
2020-04-02 01:20:12 +02:00
node = nullptr ;
2017-02-28 13:10:29 +01:00
hide ( ) ;
}
}
2022-02-16 15:17:55 +01:00
void NavigationMeshEditor : : _notification ( int p_what ) {
switch ( p_what ) {
case NOTIFICATION_ENTER_TREE : {
2023-08-13 02:33:39 +02:00
button_bake - > set_icon ( get_theme_icon ( SNAME ( " Bake " ) , EditorStringName ( EditorIcons ) ) ) ;
button_reset - > set_icon ( get_theme_icon ( SNAME ( " Reload " ) , EditorStringName ( EditorIcons ) ) ) ;
2022-02-16 15:17:55 +01:00
} break ;
2017-02-28 13:10:29 +01:00
}
}
void NavigationMeshEditor : : _bake_pressed ( ) {
2019-05-23 08:37:58 +02:00
button_bake - > set_pressed ( false ) ;
2017-02-28 13:10:29 +01:00
ERR_FAIL_COND ( ! node ) ;
2022-04-29 08:06:48 +02:00
Ref < NavigationMesh > navmesh = node - > get_navigation_mesh ( ) ;
if ( ! navmesh . is_valid ( ) ) {
2020-01-10 12:22:34 +01:00
err_dialog - > set_text ( TTR ( " A NavigationMesh resource must be set or created for this node to work. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2017-02-28 13:10:29 +01:00
return ;
}
2022-04-29 08:06:48 +02:00
String path = navmesh - > get_path ( ) ;
if ( ! path . is_resource_file ( ) ) {
int srpos = path . find ( " :: " ) ;
if ( srpos ! = - 1 ) {
String base = path . substr ( 0 , srpos ) ;
if ( ResourceLoader : : get_resource_type ( base ) = = " PackedScene " ) {
if ( ! get_tree ( ) - > get_edited_scene_root ( ) | | get_tree ( ) - > get_edited_scene_root ( ) - > get_scene_file_path ( ) ! = base ) {
err_dialog - > set_text ( TTR ( " Cannot generate navigation mesh because it does not belong to the edited scene. Make it unique first. " ) ) ;
err_dialog - > popup_centered ( ) ;
return ;
}
} else {
if ( FileAccess : : exists ( base + " .import " ) ) {
err_dialog - > set_text ( TTR ( " Cannot generate navigation mesh because it belongs to a resource which was imported. " ) ) ;
err_dialog - > popup_centered ( ) ;
return ;
}
}
}
} else {
if ( FileAccess : : exists ( path + " .import " ) ) {
err_dialog - > set_text ( TTR ( " Cannot generate navigation mesh because the resource was imported from another type. " ) ) ;
err_dialog - > popup_centered ( ) ;
return ;
}
}
2023-07-07 15:59:10 +02:00
node - > bake_navigation_mesh ( false ) ;
2017-02-28 13:10:29 +01:00
2021-06-23 16:49:50 +02:00
node - > update_gizmos ( ) ;
2017-02-28 13:10:29 +01:00
}
void NavigationMeshEditor : : _clear_pressed ( ) {
2020-05-14 16:41:43 +02:00
if ( node ) {
2023-07-07 15:59:10 +02:00
if ( node - > get_navigation_mesh ( ) . is_valid ( ) ) {
node - > get_navigation_mesh ( ) - > clear ( ) ;
}
2020-05-14 16:41:43 +02:00
}
2017-02-28 13:10:29 +01:00
button_bake - > set_pressed ( false ) ;
bake_info - > set_text ( " " ) ;
if ( node ) {
2021-06-23 16:49:50 +02:00
node - > update_gizmos ( ) ;
2017-02-28 13:10:29 +01:00
}
}
2020-03-26 22:49:16 +01:00
void NavigationMeshEditor : : edit ( NavigationRegion3D * p_nav_region ) {
2020-04-02 01:20:12 +02:00
if ( p_nav_region = = nullptr | | node = = p_nav_region ) {
2017-02-28 13:10:29 +01:00
return ;
}
2020-03-08 17:33:34 +01:00
node = p_nav_region ;
2017-02-28 13:10:29 +01:00
}
void NavigationMeshEditor : : _bind_methods ( ) {
}
NavigationMeshEditor : : NavigationMeshEditor ( ) {
bake_hbox = memnew ( HBoxContainer ) ;
2018-10-22 04:31:51 +02:00
2020-06-19 20:49:04 +02:00
button_bake = memnew ( Button ) ;
2023-09-19 18:03:10 +02:00
button_bake - > set_theme_type_variation ( " FlatButton " ) ;
2018-10-22 04:31:51 +02:00
bake_hbox - > add_child ( button_bake ) ;
2017-02-28 13:10:29 +01:00
button_bake - > set_toggle_mode ( true ) ;
2023-07-07 15:59:10 +02:00
button_bake - > set_text ( TTR ( " Bake NavigationMesh " ) ) ;
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. " ) ) ;
2020-02-21 18:28:45 +01:00
button_bake - > connect ( " pressed " , callable_mp ( this , & NavigationMeshEditor : : _bake_pressed ) ) ;
2017-02-28 13:10:29 +01:00
2020-06-19 20:49:04 +02:00
button_reset = memnew ( Button ) ;
2023-09-19 18:03:10 +02:00
button_reset - > set_theme_type_variation ( " FlatButton " ) ;
2017-02-28 13:10:29 +01:00
bake_hbox - > add_child ( button_reset ) ;
2023-07-07 15:59:10 +02:00
button_reset - > set_text ( TTR ( " Clear NavigationMesh " ) ) ;
button_reset - > set_tooltip_text ( TTR ( " Clears the internal NavigationMesh vertices and polygons. " ) ) ;
2020-02-21 18:28:45 +01:00
button_reset - > connect ( " pressed " , callable_mp ( this , & NavigationMeshEditor : : _clear_pressed ) ) ;
2018-10-22 04:31:51 +02:00
bake_info = memnew ( Label ) ;
2017-02-28 13:10:29 +01:00
bake_hbox - > add_child ( bake_info ) ;
err_dialog = memnew ( AcceptDialog ) ;
add_child ( err_dialog ) ;
2020-04-02 01:20:12 +02:00
node = nullptr ;
2017-02-28 13:10:29 +01:00
}
NavigationMeshEditor : : ~ NavigationMeshEditor ( ) {
}
void NavigationMeshEditorPlugin : : edit ( Object * p_object ) {
2020-03-26 22:49:16 +01:00
navigation_mesh_editor - > edit ( Object : : cast_to < NavigationRegion3D > ( p_object ) ) ;
2017-02-28 13:10:29 +01:00
}
bool NavigationMeshEditorPlugin : : handles ( Object * p_object ) const {
2020-03-26 22:49:16 +01:00
return p_object - > is_class ( " NavigationRegion3D " ) ;
2017-02-28 13:10:29 +01:00
}
void NavigationMeshEditorPlugin : : make_visible ( bool p_visible ) {
if ( p_visible ) {
navigation_mesh_editor - > show ( ) ;
navigation_mesh_editor - > bake_hbox - > show ( ) ;
} else {
navigation_mesh_editor - > hide ( ) ;
navigation_mesh_editor - > bake_hbox - > hide ( ) ;
2020-04-02 01:20:12 +02:00
navigation_mesh_editor - > edit ( nullptr ) ;
2017-02-28 13:10:29 +01:00
}
}
2022-02-14 17:59:06 +01:00
NavigationMeshEditorPlugin : : NavigationMeshEditorPlugin ( ) {
2017-02-28 13:10:29 +01:00
navigation_mesh_editor = memnew ( NavigationMeshEditor ) ;
2022-09-07 01:30:54 +02:00
EditorNode : : get_singleton ( ) - > get_main_screen_control ( ) - > add_child ( navigation_mesh_editor ) ;
2017-02-28 13:10:29 +01:00
add_control_to_container ( CONTAINER_SPATIAL_EDITOR_MENU , navigation_mesh_editor - > bake_hbox ) ;
navigation_mesh_editor - > hide ( ) ;
navigation_mesh_editor - > bake_hbox - > hide ( ) ;
}
NavigationMeshEditorPlugin : : ~ NavigationMeshEditorPlugin ( ) {
}
2020-01-10 12:22:34 +01:00
2022-03-28 14:10:28 +02:00
# endif // TOOLS_ENABLED