Skip to content

Commit

Permalink
Add NavigationServer API to enable regions and links
Browse files Browse the repository at this point in the history
Adds NavigationServer API to enable regions and links.
  • Loading branch information
smix8 committed Jul 25, 2023
1 parent 202e4b2 commit 69fad39
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 36 deletions.
30 changes: 30 additions & 0 deletions doc/classes/NavigationServer2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@
Create a new link between two positions on a map.
</description>
</method>
<method name="link_get_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="link" type="RID" />
<description>
Returns [code]true[/code] if the specified [param link] is enabled.
</description>
</method>
<method name="link_get_end_position" qualifiers="const">
<return type="Vector2" />
<param index="0" name="link" type="RID" />
Expand Down Expand Up @@ -271,6 +278,14 @@
Sets whether this [param link] can be travelled in both directions.
</description>
</method>
<method name="link_set_enabled">
<return type="void" />
<param index="0" name="link" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] is [code]true[/code] the specified [param link] will contribute to its current navigation map.
</description>
</method>
<method name="link_set_end_position">
<return type="void" />
<param index="0" name="link" type="RID" />
Expand Down Expand Up @@ -601,6 +616,13 @@
Returns how many connections this [param region] has with other regions in the map.
</description>
</method>
<method name="region_get_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="region" type="RID" />
<description>
Returns [code]true[/code] if the specified [param region] is enabled.
</description>
</method>
<method name="region_get_enter_cost" qualifiers="const">
<return type="float" />
<param index="0" name="region" type="RID" />
Expand Down Expand Up @@ -653,6 +675,14 @@
[b]Note:[/b] If navigation meshes from different navigation regions overlap (which should be avoided in general) the result might not be what is expected.
</description>
</method>
<method name="region_set_enabled">
<return type="void" />
<param index="0" name="region" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] is [code]true[/code] the specified [param region] will contribute to its current navigation map.
</description>
</method>
<method name="region_set_enter_cost">
<return type="void" />
<param index="0" name="region" type="RID" />
Expand Down
30 changes: 30 additions & 0 deletions doc/classes/NavigationServer3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@
Create a new link between two positions on a map.
</description>
</method>
<method name="link_get_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="link" type="RID" />
<description>
Returns [code]true[/code] if the specified [param link] is enabled.
</description>
</method>
<method name="link_get_end_position" qualifiers="const">
<return type="Vector3" />
<param index="0" name="link" type="RID" />
Expand Down Expand Up @@ -312,6 +319,14 @@
Sets whether this [param link] can be travelled in both directions.
</description>
</method>
<method name="link_set_enabled">
<return type="void" />
<param index="0" name="link" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] is [code]true[/code] the specified [param link] will contribute to its current navigation map.
</description>
</method>
<method name="link_set_end_position">
<return type="void" />
<param index="0" name="link" type="RID" />
Expand Down Expand Up @@ -734,6 +749,13 @@
Returns how many connections this [param region] has with other regions in the map.
</description>
</method>
<method name="region_get_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="region" type="RID" />
<description>
Returns [code]true[/code] if the specified [param region] is enabled.
</description>
</method>
<method name="region_get_enter_cost" qualifiers="const">
<return type="float" />
<param index="0" name="region" type="RID" />
Expand Down Expand Up @@ -786,6 +808,14 @@
[b]Note:[/b] If navigation meshes from different navigation regions overlap (which should be avoided in general) the result might not be what is expected.
</description>
</method>
<method name="region_set_enabled">
<return type="void" />
<param index="0" name="region" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] is [code]true[/code] the specified [param region] will contribute to its current navigation map.
</description>
</method>
<method name="region_set_enter_cost">
<return type="void" />
<param index="0" name="region" type="RID" />
Expand Down
28 changes: 28 additions & 0 deletions modules/navigation/godot_navigation_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,20 @@ RID GodotNavigationServer::region_create() {
return rid;
}

COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled) {
NavRegion *region = region_owner.get_or_null(p_region);
ERR_FAIL_COND(region == nullptr);

region->set_enabled(p_enabled);
}

bool GodotNavigationServer::region_get_enabled(RID p_region) const {
const NavRegion *region = region_owner.get_or_null(p_region);
ERR_FAIL_COND_V(region == nullptr, false);

return region->get_enabled();
}

COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled) {
NavRegion *region = region_owner.get_or_null(p_region);
ERR_FAIL_COND(region == nullptr);
Expand Down Expand Up @@ -512,6 +526,20 @@ RID GodotNavigationServer::link_get_map(const RID p_link) const {
return RID();
}

COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled) {
NavLink *link = link_owner.get_or_null(p_link);
ERR_FAIL_COND(link == nullptr);

link->set_enabled(p_enabled);
}

bool GodotNavigationServer::link_get_enabled(RID p_link) const {
const NavLink *link = link_owner.get_or_null(p_link);
ERR_FAIL_COND_V(link == nullptr, false);

return link->get_enabled();
}

COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional) {
NavLink *link = link_owner.get_or_null(p_link);
ERR_FAIL_COND(link == nullptr);
Expand Down
5 changes: 5 additions & 0 deletions modules/navigation/godot_navigation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ class GodotNavigationServer : public NavigationServer3D {

virtual RID region_create() override;

COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);
virtual bool region_get_enabled(RID p_region) const override;

COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
virtual bool region_get_use_edge_connections(RID p_region) const override;

Expand Down Expand Up @@ -164,6 +167,8 @@ class GodotNavigationServer : public NavigationServer3D {
virtual RID link_create() override;
COMMAND_2(link_set_map, RID, p_link, RID, p_map);
virtual RID link_get_map(RID p_link) const override;
COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);
virtual bool link_get_enabled(RID p_link) const override;
COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
virtual bool link_is_bidirectional(RID p_link) const override;
COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);
Expand Down
10 changes: 10 additions & 0 deletions modules/navigation/nav_link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ void NavLink::set_map(NavMap *p_map) {
}
}

void NavLink::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled;

// TODO: This should not require a full rebuild as the link has not really changed.
link_dirty = true;
};

void NavLink::set_bidirectional(bool p_bidirectional) {
if (bidirectional == p_bidirectional) {
return;
Expand Down
4 changes: 4 additions & 0 deletions modules/navigation/nav_link.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class NavLink : public NavBase {
bool bidirectional = true;
Vector3 start_position;
Vector3 end_position;
bool enabled = true;

bool link_dirty = true;

Expand All @@ -52,6 +53,9 @@ class NavLink : public NavBase {
return map;
}

void set_enabled(bool p_enabled);
bool get_enabled() const { return enabled; }

void set_bidirectional(bool p_bidirectional);
bool is_bidirectional() const {
return bidirectional;
Expand Down
6 changes: 6 additions & 0 deletions modules/navigation/nav_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,13 +804,19 @@ void NavMap::sync() {
// Resize the polygon count.
int count = 0;
for (const NavRegion *region : regions) {
if (!region->get_enabled()) {
continue;
}
count += region->get_polygons().size();
}
polygons.resize(count);

// Copy all region polygons in the map.
count = 0;
for (const NavRegion *region : regions) {
if (!region->get_enabled()) {
continue;
}
const LocalVector<gd::Polygon> &polygons_source = region->get_polygons();
for (uint32_t n = 0; n < polygons_source.size(); n++) {
polygons[count + n] = polygons_source[n];
Expand Down
10 changes: 10 additions & 0 deletions modules/navigation/nav_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ void NavRegion::set_map(NavMap *p_map) {
}
}

void NavRegion::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled;

// TODO: This should not require a full rebuild as the region has not really changed.
polygons_dirty = true;
};

void NavRegion::set_use_edge_connections(bool p_enabled) {
if (use_edge_connections != p_enabled) {
use_edge_connections = p_enabled;
Expand Down
4 changes: 4 additions & 0 deletions modules/navigation/nav_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class NavRegion : public NavBase {
Transform3D transform;
Ref<NavigationMesh> mesh;
Vector<gd::Edge::Connection> connections;
bool enabled = true;

bool use_edge_connections = true;

Expand All @@ -58,6 +59,9 @@ class NavRegion : public NavBase {
polygons_dirty = true;
}

void set_enabled(bool p_enabled);
bool get_enabled() const { return enabled; }

void set_map(NavMap *p_map);
NavMap *get_map() const {
return map;
Expand Down
10 changes: 1 addition & 9 deletions scene/2d/navigation_link_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,7 @@ void NavigationLink2D::set_enabled(bool p_enabled) {

enabled = p_enabled;

if (!is_inside_tree()) {
return;
}

if (!enabled) {
NavigationServer2D::get_singleton()->link_set_map(link, RID());
} else {
NavigationServer2D::get_singleton()->link_set_map(link, get_world_2d()->get_navigation_map());
}
NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);

#ifdef DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_enabled()) {
Expand Down
10 changes: 1 addition & 9 deletions scene/2d/navigation_region_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,7 @@ void NavigationRegion2D::set_enabled(bool p_enabled) {

enabled = p_enabled;

if (!is_inside_tree()) {
return;
}

if (!enabled) {
_region_enter_navigation_map();
} else {
_region_exit_navigation_map();
}
NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);

#ifdef DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_navigation_enabled()) {
Expand Down
10 changes: 1 addition & 9 deletions scene/3d/navigation_link_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,7 @@ void NavigationLink3D::set_enabled(bool p_enabled) {

enabled = p_enabled;

if (!is_inside_tree()) {
return;
}

if (enabled) {
NavigationServer3D::get_singleton()->link_set_map(link, get_world_3d()->get_navigation_map());
} else {
NavigationServer3D::get_singleton()->link_set_map(link, RID());
}
NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);

#ifdef DEBUG_ENABLED
if (debug_instance.is_valid() && debug_mesh.is_valid()) {
Expand Down
10 changes: 1 addition & 9 deletions scene/3d/navigation_region_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ void NavigationRegion3D::set_enabled(bool p_enabled) {

enabled = p_enabled;

if (!is_inside_tree()) {
return;
}

if (!enabled) {
_region_enter_navigation_map();
} else {
_region_exit_navigation_map();
}
NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);

#ifdef DEBUG_ENABLED
if (debug_instance.is_valid()) {
Expand Down
8 changes: 8 additions & 0 deletions servers/navigation_server_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ void NavigationServer2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("query_path", "parameters", "result"), &NavigationServer2D::query_path);

ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer2D::region_create);
ClassDB::bind_method(D_METHOD("region_set_enabled", "region", "enabled"), &NavigationServer2D::region_set_enabled);
ClassDB::bind_method(D_METHOD("region_get_enabled", "region"), &NavigationServer2D::region_get_enabled);
ClassDB::bind_method(D_METHOD("region_set_use_edge_connections", "region", "enabled"), &NavigationServer2D::region_set_use_edge_connections);
ClassDB::bind_method(D_METHOD("region_get_use_edge_connections", "region"), &NavigationServer2D::region_get_use_edge_connections);
ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer2D::region_set_enter_cost);
Expand All @@ -413,6 +415,8 @@ void NavigationServer2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("link_create"), &NavigationServer2D::link_create);
ClassDB::bind_method(D_METHOD("link_set_map", "link", "map"), &NavigationServer2D::link_set_map);
ClassDB::bind_method(D_METHOD("link_get_map", "link"), &NavigationServer2D::link_get_map);
ClassDB::bind_method(D_METHOD("link_set_enabled", "link", "enabled"), &NavigationServer2D::link_set_enabled);
ClassDB::bind_method(D_METHOD("link_get_enabled", "link"), &NavigationServer2D::link_get_enabled);
ClassDB::bind_method(D_METHOD("link_set_bidirectional", "link", "bidirectional"), &NavigationServer2D::link_set_bidirectional);
ClassDB::bind_method(D_METHOD("link_is_bidirectional", "link"), &NavigationServer2D::link_is_bidirectional);
ClassDB::bind_method(D_METHOD("link_set_navigation_layers", "link", "navigation_layers"), &NavigationServer2D::link_set_navigation_layers);
Expand Down Expand Up @@ -536,6 +540,8 @@ RID FORWARD_2_C(map_get_closest_point_owner, RID, p_map, const Vector2 &, p_poin

RID FORWARD_0(region_create);

void FORWARD_2(region_set_enabled, RID, p_region, bool, p_enabled, rid_to_rid, bool_to_bool);
bool FORWARD_1_C(region_get_enabled, RID, p_region, rid_to_rid);
void FORWARD_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled, rid_to_rid, bool_to_bool);
bool FORWARD_1_C(region_get_use_edge_connections, RID, p_region, rid_to_rid);

Expand Down Expand Up @@ -564,6 +570,8 @@ RID FORWARD_0(link_create);

void FORWARD_2(link_set_map, RID, p_link, RID, p_map, rid_to_rid, rid_to_rid);
RID FORWARD_1_C(link_get_map, RID, p_link, rid_to_rid);
void FORWARD_2(link_set_enabled, RID, p_link, bool, p_enabled, rid_to_rid, bool_to_bool);
bool FORWARD_1_C(link_get_enabled, RID, p_link, rid_to_rid);
void FORWARD_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional, rid_to_rid, bool_to_bool);
bool FORWARD_1_C(link_is_bidirectional, RID, p_link, rid_to_rid);
void FORWARD_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers, rid_to_rid, uint32_to_uint32);
Expand Down
6 changes: 6 additions & 0 deletions servers/navigation_server_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ class NavigationServer2D : public Object {
/// Creates a new region.
virtual RID region_create();

virtual void region_set_enabled(RID p_region, bool p_enabled);
virtual bool region_get_enabled(RID p_region) const;

virtual void region_set_use_edge_connections(RID p_region, bool p_enabled);
virtual bool region_get_use_edge_connections(RID p_region) const;

Expand Down Expand Up @@ -144,6 +147,9 @@ class NavigationServer2D : public Object {
virtual void link_set_map(RID p_link, RID p_map);
virtual RID link_get_map(RID p_link) const;

virtual void link_set_enabled(RID p_link, bool p_enabled);
virtual bool link_get_enabled(RID p_link) const;

/// Set whether this link travels in both directions.
virtual void link_set_bidirectional(RID p_link, bool p_bidirectional);
virtual bool link_is_bidirectional(RID p_link) const;
Expand Down
Loading

0 comments on commit 69fad39

Please sign in to comment.