#include "camera_server.h"
#include "core/variant/typed_array.h"
#include "rendering_server.h"
#include "servers/camera/camera_feed.h"
CameraServer::CreateFunc CameraServer::create_func = …;
void CameraServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_feed", "index"), &CameraServer::get_feed);
ClassDB::bind_method(D_METHOD("get_feed_count"), &CameraServer::get_feed_count);
ClassDB::bind_method(D_METHOD("feeds"), &CameraServer::get_feeds);
ClassDB::bind_method(D_METHOD("add_feed", "feed"), &CameraServer::add_feed);
ClassDB::bind_method(D_METHOD("remove_feed", "feed"), &CameraServer::remove_feed);
ADD_SIGNAL(MethodInfo("camera_feed_added", PropertyInfo(Variant::INT, "id")));
ADD_SIGNAL(MethodInfo("camera_feed_removed", PropertyInfo(Variant::INT, "id")));
BIND_ENUM_CONSTANT(FEED_RGBA_IMAGE);
BIND_ENUM_CONSTANT(FEED_YCBCR_IMAGE);
BIND_ENUM_CONSTANT(FEED_Y_IMAGE);
BIND_ENUM_CONSTANT(FEED_CBCR_IMAGE);
};
CameraServer *CameraServer::singleton = …;
CameraServer *CameraServer::get_singleton() {
return singleton;
};
int CameraServer::get_free_id() {
bool id_exists = true;
int newid = 0;
while (id_exists) {
newid++;
id_exists = false;
for (int i = 0; i < feeds.size() && !id_exists; i++) {
if (feeds[i]->get_id() == newid) {
id_exists = true;
};
};
};
return newid;
};
int CameraServer::get_feed_index(int p_id) {
for (int i = 0; i < feeds.size(); i++) {
if (feeds[i]->get_id() == p_id) {
return i;
};
};
return -1;
};
Ref<CameraFeed> CameraServer::get_feed_by_id(int p_id) {
int index = get_feed_index(p_id);
if (index == -1) {
return nullptr;
} else {
return feeds[index];
}
};
void CameraServer::add_feed(const Ref<CameraFeed> &p_feed) {
ERR_FAIL_COND(p_feed.is_null());
feeds.push_back(p_feed);
print_verbose("CameraServer: Registered camera " + p_feed->get_name() + " with ID " + itos(p_feed->get_id()) + " and position " + itos(p_feed->get_position()) + " at index " + itos(feeds.size() - 1));
emit_signal(SNAME("camera_feed_added"), p_feed->get_id());
};
void CameraServer::remove_feed(const Ref<CameraFeed> &p_feed) {
for (int i = 0; i < feeds.size(); i++) {
if (feeds[i] == p_feed) {
int feed_id = p_feed->get_id();
print_verbose("CameraServer: Removed camera " + p_feed->get_name() + " with ID " + itos(feed_id) + " and position " + itos(p_feed->get_position()));
feeds.remove_at(i);
emit_signal(SNAME("camera_feed_removed"), feed_id);
return;
};
};
};
Ref<CameraFeed> CameraServer::get_feed(int p_index) {
ERR_FAIL_INDEX_V(p_index, feeds.size(), nullptr);
return feeds[p_index];
};
int CameraServer::get_feed_count() {
return feeds.size();
};
TypedArray<CameraFeed> CameraServer::get_feeds() {
TypedArray<CameraFeed> return_feeds;
int cc = get_feed_count();
return_feeds.resize(cc);
for (int i = 0; i < feeds.size(); i++) {
return_feeds[i] = get_feed(i);
};
return return_feeds;
};
RID CameraServer::feed_texture(int p_id, CameraServer::FeedImage p_texture) {
int index = get_feed_index(p_id);
ERR_FAIL_COND_V(index == -1, RID());
Ref<CameraFeed> feed = get_feed(index);
return feed->get_texture(p_texture);
};
CameraServer::CameraServer() {
singleton = this;
};
CameraServer::~CameraServer() {
singleton = nullptr;
};