Now more than one Naviugation2D servers can be registered.

This commit is contained in:
Relintai 2023-04-16 18:19:57 +02:00
parent f6c1c1dc8d
commit 86bdecbfe9
4 changed files with 102 additions and 13 deletions

View File

@ -62,7 +62,8 @@ void register_navigation_types(ModuleRegistrationLevel p_level) {
NavigationServerManager::register_server("PandemoniumNavigation", new_server);
NavigationServerManager::set_default_server("PandemoniumNavigation");
Navigation2DServerManager::set_default_server(new_2d_server);
Navigation2DServerManager::register_server("PandemoniumNavigation", new_2d_server);
Navigation2DServerManager::set_default_server("PandemoniumNavigation");
#ifndef _3D_DISABLED
_nav_mesh_generator = memnew(NavigationMeshGenerator);

View File

@ -30,9 +30,7 @@
#include "servers/navigation_2d_server.h"
#include "core/math/transform.h"
#include "core/math/transform_2d.h"
#include "scene/resources/navigation_mesh.h"
#include "core/config/project_settings.h"
#include "servers/navigation_server.h"
Navigation2DServer *Navigation2DServer::singleton = nullptr;
@ -100,13 +98,63 @@ Navigation2DServer::~Navigation2DServer() {
singleton = nullptr;
}
Navigation2DServerCallback Navigation2DServerManager::create_callback = nullptr;
Vector<Navigation2DServerManager::ClassInfo> Navigation2DServerManager::navigation_servers;
int Navigation2DServerManager::default_server_id = -1;
int Navigation2DServerManager::default_server_priority = -1;
const String Navigation2DServerManager::setting_property_name("navigation/2d/navigation_engine");
void Navigation2DServerManager::set_default_server(Navigation2DServerCallback p_callback) {
create_callback = p_callback;
void Navigation2DServerManager::on_servers_changed() {
String navigation_servers2("DEFAULT");
for (int i = get_servers_count() - 1; 0 <= i; --i) {
navigation_servers2 += "," + get_server_name(i);
}
ProjectSettings::get_singleton()->set_custom_property_info(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, navigation_servers2));
}
void Navigation2DServerManager::register_server(const String &p_name, CreateNavigation2DServerCallback p_creat_callback) {
ERR_FAIL_COND(!p_creat_callback);
ERR_FAIL_COND(find_server_id(p_name) != -1);
navigation_servers.push_back(ClassInfo(p_name, p_creat_callback));
on_servers_changed();
}
void Navigation2DServerManager::set_default_server(const String &p_name, int p_priority) {
const int id = find_server_id(p_name);
ERR_FAIL_COND(id == -1); // Not found
if (default_server_priority < p_priority) {
default_server_id = id;
default_server_priority = p_priority;
}
}
int Navigation2DServerManager::find_server_id(const String &p_name) {
for (int i = navigation_servers.size() - 1; 0 <= i; --i) {
if (p_name == navigation_servers[i].name) {
return i;
}
}
return -1;
}
int Navigation2DServerManager::get_servers_count() {
return navigation_servers.size();
}
String Navigation2DServerManager::get_server_name(int p_id) {
ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
return navigation_servers[p_id].name;
}
Navigation2DServer *Navigation2DServerManager::new_default_server() {
ERR_FAIL_COND_V(create_callback == nullptr, nullptr);
return create_callback();
ERR_FAIL_COND_V(default_server_id == -1, nullptr);
return navigation_servers[default_server_id].create_callback();
}
Navigation2DServer *Navigation2DServerManager::new_server(const String &p_name) {
int id = find_server_id(p_name);
if (id == -1) {
return nullptr;
} else {
return navigation_servers[id].create_callback();
}
}

View File

@ -31,8 +31,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/object/object.h"
#include "core/containers/rid.h"
#include "core/object/object.h"
#include "scene/2d/navigation_polygon.h"
// This server exposes the 3D `NavigationServer` features in the 2D world.
@ -188,15 +188,51 @@ public:
virtual ~Navigation2DServer();
};
typedef Navigation2DServer *(*Navigation2DServerCallback)();
typedef Navigation2DServer *(*CreateNavigation2DServerCallback)();
/// Manager used for the server singleton registration
class Navigation2DServerManager {
static Navigation2DServerCallback create_callback;
struct ClassInfo {
String name;
CreateNavigation2DServerCallback create_callback;
ClassInfo() :
name(""),
create_callback(nullptr) {}
ClassInfo(String p_name, CreateNavigation2DServerCallback p_create_callback) :
name(p_name),
create_callback(p_create_callback) {}
ClassInfo(const ClassInfo &p_ci) :
name(p_ci.name),
create_callback(p_ci.create_callback) {}
ClassInfo operator=(const ClassInfo &p_ci) {
name = p_ci.name;
create_callback = p_ci.create_callback;
return *this;
}
};
static Vector<ClassInfo> navigation_servers;
static int default_server_id;
static int default_server_priority;
public:
static void set_default_server(Navigation2DServerCallback p_callback);
static const String setting_property_name;
private:
static void on_servers_changed();
public:
static void register_server(const String &p_name, CreateNavigation2DServerCallback p_create_callback);
static void set_default_server(const String &p_name, int p_priority = 0);
static int find_server_id(const String &p_name);
static int get_servers_count();
static String get_server_name(int p_id);
static Navigation2DServer *new_default_server();
static Navigation2DServer *new_server(const String &p_name);
};
#endif

View File

@ -191,6 +191,10 @@ void register_server_types() {
PhysicsServerManager::register_server("PandemoniumPhysics", &_createPandemoniumPhysicsCallback);
PhysicsServerManager::set_default_server("PandemoniumPhysics");
// Navigation 2D
GLOBAL_DEF(Navigation2DServerManager::setting_property_name, "DEFAULT");
ProjectSettings::get_singleton()->set_custom_property_info(Navigation2DServerManager::setting_property_name, PropertyInfo(Variant::STRING, Navigation2DServerManager::setting_property_name, PROPERTY_HINT_ENUM, "DEFAULT"));
// Navigation 3D
GLOBAL_DEF(NavigationServerManager::setting_property_name, "DEFAULT");
ProjectSettings::get_singleton()->set_custom_property_info(NavigationServerManager::setting_property_name, PropertyInfo(Variant::STRING, NavigationServerManager::setting_property_name, PROPERTY_HINT_ENUM, "DEFAULT"));