2020-01-10 12:22:34 +01:00
|
|
|
/*************************************************************************/
|
2020-02-20 17:56:30 +01:00
|
|
|
/* navigation_region.cpp */
|
2020-01-10 12:22:34 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
#include "navigation_region.h"
|
2020-01-10 12:22:34 +01:00
|
|
|
#include "core/os/thread.h"
|
|
|
|
#include "mesh_instance.h"
|
|
|
|
#include "navigation.h"
|
|
|
|
#include "servers/navigation_server.h"
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
void NavigationRegion::set_enabled(bool p_enabled) {
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
if (enabled == p_enabled)
|
|
|
|
return;
|
|
|
|
enabled = p_enabled;
|
|
|
|
|
|
|
|
if (!is_inside_tree())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
|
|
|
NavigationServer::get_singleton()->region_set_map(region, RID());
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (navigation) {
|
|
|
|
|
|
|
|
NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug_view) {
|
|
|
|
MeshInstance *dm = Object::cast_to<MeshInstance>(debug_view);
|
|
|
|
if (is_enabled()) {
|
|
|
|
dm->set_material_override(get_tree()->get_debug_navigation_material());
|
|
|
|
} else {
|
|
|
|
dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_gizmo();
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
bool NavigationRegion::is_enabled() const {
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
void NavigationRegion::_notification(int p_what) {
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
|
|
|
|
Spatial *c = this;
|
|
|
|
while (c) {
|
|
|
|
|
|
|
|
navigation = Object::cast_to<Navigation>(c);
|
|
|
|
if (navigation) {
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
|
|
|
NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = c->get_parent_spatial();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (navmesh.is_valid() && get_tree()->is_debugging_navigation_hint()) {
|
|
|
|
|
|
|
|
MeshInstance *dm = memnew(MeshInstance);
|
|
|
|
dm->set_mesh(navmesh->get_debug_mesh());
|
|
|
|
if (is_enabled()) {
|
|
|
|
dm->set_material_override(get_tree()->get_debug_navigation_material());
|
|
|
|
} else {
|
|
|
|
dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
|
|
|
|
}
|
|
|
|
add_child(dm);
|
|
|
|
debug_view = dm;
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
|
|
|
|
|
|
|
NavigationServer::get_singleton()->region_set_transform(region, get_global_transform());
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
|
|
|
|
if (navigation) {
|
|
|
|
|
|
|
|
NavigationServer::get_singleton()->region_set_map(region, RID());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug_view) {
|
|
|
|
debug_view->queue_delete();
|
|
|
|
debug_view = NULL;
|
|
|
|
}
|
|
|
|
navigation = NULL;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
void NavigationRegion::set_navigation_mesh(const Ref<NavigationMesh> &p_navmesh) {
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
if (p_navmesh == navmesh)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (navmesh.is_valid()) {
|
|
|
|
navmesh->remove_change_receptor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
navmesh = p_navmesh;
|
|
|
|
|
|
|
|
if (navmesh.is_valid()) {
|
|
|
|
navmesh->add_change_receptor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NavigationServer::get_singleton()->region_set_navmesh(region, p_navmesh);
|
|
|
|
|
|
|
|
if (debug_view && navmesh.is_valid()) {
|
|
|
|
Object::cast_to<MeshInstance>(debug_view)->set_mesh(navmesh->get_debug_mesh());
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_signal("navigation_mesh_changed");
|
|
|
|
|
|
|
|
update_gizmo();
|
|
|
|
update_configuration_warning();
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
Ref<NavigationMesh> NavigationRegion::get_navigation_mesh() const {
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
return navmesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BakeThreadsArgs {
|
2020-03-08 17:33:34 +01:00
|
|
|
NavigationRegion *nav_region;
|
2020-01-10 12:22:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void _bake_navigation_mesh(void *p_user_data) {
|
|
|
|
BakeThreadsArgs *args = static_cast<BakeThreadsArgs *>(p_user_data);
|
|
|
|
|
2020-03-08 17:33:34 +01:00
|
|
|
if (args->nav_region->get_navigation_mesh().is_valid()) {
|
|
|
|
Ref<NavigationMesh> nav_mesh = args->nav_region->get_navigation_mesh()->duplicate();
|
2020-01-10 12:22:34 +01:00
|
|
|
|
2020-03-08 17:33:34 +01:00
|
|
|
NavigationServer::get_singleton()->region_bake_navmesh(nav_mesh, args->nav_region);
|
|
|
|
args->nav_region->call_deferred("_bake_finished", nav_mesh);
|
2020-01-10 12:22:34 +01:00
|
|
|
memdelete(args);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
ERR_PRINT("Can't bake the navigation mesh if the `NavigationMesh` resource doesn't exist");
|
2020-03-08 17:33:34 +01:00
|
|
|
args->nav_region->call_deferred("_bake_finished", Ref<NavigationMesh>());
|
2020-01-10 12:22:34 +01:00
|
|
|
memdelete(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
void NavigationRegion::bake_navigation_mesh() {
|
2020-01-10 12:22:34 +01:00
|
|
|
ERR_FAIL_COND(bake_thread != NULL);
|
|
|
|
|
|
|
|
BakeThreadsArgs *args = memnew(BakeThreadsArgs);
|
2020-03-08 17:33:34 +01:00
|
|
|
args->nav_region = this;
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
bake_thread = Thread::create(_bake_navigation_mesh, args);
|
|
|
|
ERR_FAIL_COND(bake_thread == NULL);
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
void NavigationRegion::_bake_finished(Ref<NavigationMesh> p_nav_mesh) {
|
2020-01-10 12:22:34 +01:00
|
|
|
set_navigation_mesh(p_nav_mesh);
|
|
|
|
bake_thread = NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
String NavigationRegion::get_configuration_warning() const {
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
if (!is_visible_in_tree() || !is_inside_tree())
|
|
|
|
return String();
|
|
|
|
|
|
|
|
if (!navmesh.is_valid()) {
|
|
|
|
return TTR("A NavigationMesh resource must be set or created for this node to work.");
|
|
|
|
}
|
|
|
|
const Spatial *c = this;
|
|
|
|
while (c) {
|
|
|
|
|
|
|
|
if (Object::cast_to<Navigation>(c))
|
|
|
|
return String();
|
|
|
|
|
|
|
|
c = Object::cast_to<Spatial>(c->get_parent());
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
return TTR("NavigationRegion must be a child or grandchild to a Navigation node. It only provides navigation data.");
|
2020-01-10 12:22:34 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
void NavigationRegion::_bind_methods() {
|
2020-01-10 12:22:34 +01:00
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navmesh"), &NavigationRegion::set_navigation_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationRegion::get_navigation_mesh);
|
2020-01-10 12:22:34 +01:00
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion::set_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion::is_enabled);
|
2020-01-10 12:22:34 +01:00
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("bake_navigation_mesh"), &NavigationRegion::bake_navigation_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("_bake_finished", "nav_mesh"), &NavigationRegion::_bake_finished);
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
|
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("navigation_mesh_changed"));
|
|
|
|
ADD_SIGNAL(MethodInfo("bake_finished"));
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
void NavigationRegion::_changed_callback(Object *p_changed, const char *p_prop) {
|
2020-01-10 12:22:34 +01:00
|
|
|
update_gizmo();
|
|
|
|
update_configuration_warning();
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
NavigationRegion::NavigationRegion() {
|
2020-01-10 12:22:34 +01:00
|
|
|
|
|
|
|
enabled = true;
|
|
|
|
set_notify_transform(true);
|
|
|
|
region = NavigationServer::get_singleton()->region_create();
|
|
|
|
|
|
|
|
navigation = NULL;
|
|
|
|
debug_view = NULL;
|
|
|
|
bake_thread = NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:56:30 +01:00
|
|
|
NavigationRegion::~NavigationRegion() {
|
2020-01-10 12:22:34 +01:00
|
|
|
if (navmesh.is_valid())
|
|
|
|
navmesh->remove_change_receptor(this);
|
|
|
|
NavigationServer::get_singleton()->free(region);
|
|
|
|
}
|