From 4cce36e35dca6363371ab8ecb426e00019be9ab2 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Mon, 29 Mar 2021 21:07:48 +1100 Subject: [PATCH] Change ARVRPositionalTracker to a reference and better expose it to GDNative --- doc/classes/ARVRPositionalTracker.xml | 2 +- doc/classes/ARVRServer.xml | 45 +++++++++++++++++++ .../gdnative/arvr/arvr_interface_gdnative.cpp | 25 ++++++----- modules/webxr/webxr_interface.h | 2 +- modules/webxr/webxr_interface_js.cpp | 10 ++--- modules/webxr/webxr_interface_js.h | 2 +- scene/3d/arvr_nodes.cpp | 32 ++++++------- servers/arvr/arvr_positional_tracker.h | 4 +- servers/arvr_server.cpp | 23 ++++++---- servers/arvr_server.h | 10 ++--- 10 files changed, 103 insertions(+), 52 deletions(-) diff --git a/doc/classes/ARVRPositionalTracker.xml b/doc/classes/ARVRPositionalTracker.xml index 73defd36e5c..a09174603d4 100644 --- a/doc/classes/ARVRPositionalTracker.xml +++ b/doc/classes/ARVRPositionalTracker.xml @@ -1,5 +1,5 @@ - + A tracked object. diff --git a/doc/classes/ARVRServer.xml b/doc/classes/ARVRServer.xml index f966f0f65f6..45fafb8a54f 100644 --- a/doc/classes/ARVRServer.xml +++ b/doc/classes/ARVRServer.xml @@ -10,6 +10,24 @@ https://docs.godotengine.org/en/3.3/tutorials/vr/index.html + + + + + + + Registers an [ARVRInterface] object. + + + + + + + + + Registers a new [ARVRPositionalTracker] that tracks a spatial location in real space. + + @@ -26,6 +44,15 @@ You should call this method after a few seconds have passed. For instance, when the user requests a realignment of the display holding a designated button on a controller for a short period of time, or when implementing a teleport mechanism. + + + + + + + Clears our current primary interface if it is set to the provided interface. + + @@ -109,6 +136,24 @@ Returns the number of trackers currently registered. + + + + + + + Removes this interface. + + + + + + + + + Removes this positional tracker. + + diff --git a/modules/gdnative/arvr/arvr_interface_gdnative.cpp b/modules/gdnative/arvr/arvr_interface_gdnative.cpp index 46eccdd0422..fbd1bfb03ec 100644 --- a/modules/gdnative/arvr/arvr_interface_gdnative.cpp +++ b/modules/gdnative/arvr/arvr_interface_gdnative.cpp @@ -320,7 +320,8 @@ godot_int GDAPI godot_arvr_add_controller(char *p_device_name, godot_int p_hand, InputDefault *input = (InputDefault *)Input::get_singleton(); ERR_FAIL_NULL_V(input, 0); - ARVRPositionalTracker *new_tracker = memnew(ARVRPositionalTracker); + Ref new_tracker; + new_tracker.instance(); new_tracker->set_name(p_device_name); new_tracker->set_type(ARVRServer::TRACKER_CONTROLLER); if (p_hand == 1) { @@ -359,8 +360,8 @@ void GDAPI godot_arvr_remove_controller(godot_int p_controller_id) { InputDefault *input = (InputDefault *)Input::get_singleton(); ERR_FAIL_NULL(input); - ARVRPositionalTracker *remove_tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); - if (remove_tracker != NULL) { + Ref remove_tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); + if (remove_tracker.is_valid()) { // unset our joystick if applicable int joyid = remove_tracker->get_joy_id(); if (joyid != -1) { @@ -370,7 +371,7 @@ void GDAPI godot_arvr_remove_controller(godot_int p_controller_id) { // remove our tracker from our server arvr_server->remove_tracker(remove_tracker); - memdelete(remove_tracker); + remove_tracker.unref(); } } @@ -378,8 +379,8 @@ void GDAPI godot_arvr_set_controller_transform(godot_int p_controller_id, godot_ ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL(arvr_server); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); - if (tracker != NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); + if (tracker.is_valid()) { Transform *transform = (Transform *)p_transform; if (p_tracks_orientation) { tracker->set_orientation(transform->basis); @@ -397,8 +398,8 @@ void GDAPI godot_arvr_set_controller_button(godot_int p_controller_id, godot_int InputDefault *input = (InputDefault *)Input::get_singleton(); ERR_FAIL_NULL(input); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); - if (tracker != NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); + if (tracker.is_valid()) { int joyid = tracker->get_joy_id(); if (joyid != -1) { input->joy_button(joyid, p_button, p_is_pressed); @@ -413,8 +414,8 @@ void GDAPI godot_arvr_set_controller_axis(godot_int p_controller_id, godot_int p InputDefault *input = (InputDefault *)Input::get_singleton(); ERR_FAIL_NULL(input); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); - if (tracker != NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); + if (tracker.is_valid()) { int joyid = tracker->get_joy_id(); if (joyid != -1) { InputDefault::JoyAxis jx; @@ -429,8 +430,8 @@ godot_real GDAPI godot_arvr_get_controller_rumble(godot_int p_controller_id) { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL_V(arvr_server, 0.0); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); - if (tracker != NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id); + if (tracker.is_valid()) { return tracker->get_rumble(); } diff --git a/modules/webxr/webxr_interface.h b/modules/webxr/webxr_interface.h index 22598fa5153..396fac5849d 100644 --- a/modules/webxr/webxr_interface.h +++ b/modules/webxr/webxr_interface.h @@ -57,7 +57,7 @@ public: virtual void set_requested_reference_space_types(String p_requested_reference_space_types) = 0; virtual String get_requested_reference_space_types() const = 0; virtual String get_reference_space_type() const = 0; - virtual ARVRPositionalTracker *get_controller(int p_controller_id) const = 0; + virtual Ref get_controller(int p_controller_id) const = 0; virtual String get_visibility_state() const = 0; virtual PoolVector3Array get_bounds_geometry() const = 0; }; diff --git a/modules/webxr/webxr_interface_js.cpp b/modules/webxr/webxr_interface_js.cpp index 4d63df57d33..9aed3e46b7d 100644 --- a/modules/webxr/webxr_interface_js.cpp +++ b/modules/webxr/webxr_interface_js.cpp @@ -162,7 +162,7 @@ String WebXRInterfaceJS::get_reference_space_type() const { return reference_space_type; } -ARVRPositionalTracker *WebXRInterfaceJS::get_controller(int p_controller_id) const { +Ref WebXRInterfaceJS::get_controller(int p_controller_id) const { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL_V(arvr_server, nullptr); @@ -382,10 +382,10 @@ void WebXRInterfaceJS::_update_tracker(int p_controller_id) { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL(arvr_server); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id + 1); + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id + 1); if (godot_webxr_is_controller_connected(p_controller_id)) { - if (tracker == nullptr) { - tracker = memnew(ARVRPositionalTracker); + if (tracker.is_null()) { + tracker.instance(); tracker->set_type(ARVRServer::TRACKER_CONTROLLER); // Controller id's 0 and 1 are always the left and right hands. if (p_controller_id < 2) { @@ -425,7 +425,7 @@ void WebXRInterfaceJS::_update_tracker(int p_controller_id) { } free(axes); } - } else if (tracker) { + } else if (tracker.is_valid()) { arvr_server->remove_tracker(tracker); } } diff --git a/modules/webxr/webxr_interface_js.h b/modules/webxr/webxr_interface_js.h index 258b17cc4c9..38bf6be65b0 100644 --- a/modules/webxr/webxr_interface_js.h +++ b/modules/webxr/webxr_interface_js.h @@ -71,7 +71,7 @@ public: virtual String get_requested_reference_space_types() const; void _set_reference_space_type(String p_reference_space_type); virtual String get_reference_space_type() const; - virtual ARVRPositionalTracker *get_controller(int p_controller_id) const; + virtual Ref get_controller(int p_controller_id) const; virtual String get_visibility_state() const; virtual PoolVector3Array get_bounds_geometry() const; diff --git a/scene/3d/arvr_nodes.cpp b/scene/3d/arvr_nodes.cpp index db82ef76bbf..a606dc363df 100644 --- a/scene/3d/arvr_nodes.cpp +++ b/scene/3d/arvr_nodes.cpp @@ -195,8 +195,8 @@ void ARVRController::_notification(int p_what) { ERR_FAIL_NULL(arvr_server); // find the tracker for our controller - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); - if (tracker == NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); + if (!tracker.is_valid()) { // this controller is currently turned off is_active = false; button_states = 0; @@ -282,8 +282,8 @@ String ARVRController::get_controller_name(void) const { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL_V(arvr_server, String()); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); - if (tracker == NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); + if (!tracker.is_valid()) { return String("Not connected"); }; @@ -295,8 +295,8 @@ int ARVRController::get_joystick_id() const { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL_V(arvr_server, 0); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); - if (tracker == NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); + if (!tracker.is_valid()) { // No tracker? no joystick id... (0 is our first joystick) return -1; }; @@ -327,8 +327,8 @@ real_t ARVRController::get_rumble() const { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL_V(arvr_server, 0.0); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); - if (tracker == NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); + if (!tracker.is_valid()) { return 0.0; }; @@ -340,8 +340,8 @@ void ARVRController::set_rumble(real_t p_rumble) { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL(arvr_server); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); - if (tracker != NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); + if (tracker.is_valid()) { tracker->set_rumble(p_rumble); }; }; @@ -359,8 +359,8 @@ ARVRPositionalTracker::TrackerHand ARVRController::get_hand() const { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL_V(arvr_server, ARVRPositionalTracker::TRACKER_HAND_UNKNOWN); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); - if (tracker == NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, controller_id); + if (!tracker.is_valid()) { return ARVRPositionalTracker::TRACKER_HAND_UNKNOWN; }; @@ -417,8 +417,8 @@ void ARVRAnchor::_notification(int p_what) { ERR_FAIL_NULL(arvr_server); // find the tracker for our anchor - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_ANCHOR, anchor_id); - if (tracker == NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_ANCHOR, anchor_id); + if (!tracker.is_valid()) { // this anchor is currently not available is_active = false; } else { @@ -489,8 +489,8 @@ String ARVRAnchor::get_anchor_name(void) const { ARVRServer *arvr_server = ARVRServer::get_singleton(); ERR_FAIL_NULL_V(arvr_server, String()); - ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_ANCHOR, anchor_id); - if (tracker == NULL) { + Ref tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_ANCHOR, anchor_id); + if (!tracker.is_valid()) { return String("Not connected"); }; diff --git a/servers/arvr/arvr_positional_tracker.h b/servers/arvr/arvr_positional_tracker.h index bb614a33dc9..aad20408210 100644 --- a/servers/arvr/arvr_positional_tracker.h +++ b/servers/arvr/arvr_positional_tracker.h @@ -43,8 +43,8 @@ This is where potentially additional AR/VR interfaces may be active as there are AR/VR SDKs that solely deal with positional tracking. */ -class ARVRPositionalTracker : public Object { - GDCLASS(ARVRPositionalTracker, Object); +class ARVRPositionalTracker : public Reference { + GDCLASS(ARVRPositionalTracker, Reference); _THREAD_SAFE_CLASS_ public: diff --git a/servers/arvr_server.cpp b/servers/arvr_server.cpp index cf1936f8ac9..b0c69d086fe 100644 --- a/servers/arvr_server.cpp +++ b/servers/arvr_server.cpp @@ -48,12 +48,17 @@ void ARVRServer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::REAL, "world_scale"), "set_world_scale", "get_world_scale"); + ClassDB::bind_method(D_METHOD("add_interface", "interface"), &ARVRServer::add_interface); + ClassDB::bind_method(D_METHOD("clear_primary_interface_if", "interface"), &ARVRServer::clear_primary_interface_if); ClassDB::bind_method(D_METHOD("get_interface_count"), &ARVRServer::get_interface_count); + ClassDB::bind_method(D_METHOD("remove_interface", "interface"), &ARVRServer::remove_interface); ClassDB::bind_method(D_METHOD("get_interface", "idx"), &ARVRServer::get_interface); ClassDB::bind_method(D_METHOD("get_interfaces"), &ARVRServer::get_interfaces); ClassDB::bind_method(D_METHOD("find_interface", "name"), &ARVRServer::find_interface); ClassDB::bind_method(D_METHOD("get_tracker_count"), &ARVRServer::get_tracker_count); ClassDB::bind_method(D_METHOD("get_tracker", "idx"), &ARVRServer::get_tracker); + ClassDB::bind_method(D_METHOD("add_tracker", "tracker"), &ARVRServer::add_tracker); + ClassDB::bind_method(D_METHOD("remove_tracker", "tracker"), &ARVRServer::remove_tracker); ClassDB::bind_method(D_METHOD("get_primary_interface"), &ARVRServer::get_primary_interface); ClassDB::bind_method(D_METHOD("set_primary_interface", "interface"), &ARVRServer::set_primary_interface); @@ -265,15 +270,15 @@ int ARVRServer::get_free_tracker_id_for_type(TrackerType p_tracker_type) { return tracker_id; }; -void ARVRServer::add_tracker(ARVRPositionalTracker *p_tracker) { - ERR_FAIL_NULL(p_tracker); +void ARVRServer::add_tracker(Ref p_tracker) { + ERR_FAIL_COND(p_tracker.is_null()); trackers.push_back(p_tracker); emit_signal("tracker_added", p_tracker->get_name(), p_tracker->get_type(), p_tracker->get_tracker_id()); }; -void ARVRServer::remove_tracker(ARVRPositionalTracker *p_tracker) { - ERR_FAIL_NULL(p_tracker); +void ARVRServer::remove_tracker(Ref p_tracker) { + ERR_FAIL_COND(p_tracker.is_null()); int idx = -1; for (int i = 0; i < trackers.size(); i++) { @@ -295,14 +300,14 @@ int ARVRServer::get_tracker_count() const { return trackers.size(); }; -ARVRPositionalTracker *ARVRServer::get_tracker(int p_index) const { - ERR_FAIL_INDEX_V(p_index, trackers.size(), NULL); +Ref ARVRServer::get_tracker(int p_index) const { + ERR_FAIL_INDEX_V(p_index, trackers.size(), Ref()); return trackers[p_index]; }; -ARVRPositionalTracker *ARVRServer::find_by_type_and_id(TrackerType p_tracker_type, int p_tracker_id) const { - ERR_FAIL_COND_V(p_tracker_id == 0, NULL); +Ref ARVRServer::find_by_type_and_id(TrackerType p_tracker_type, int p_tracker_id) const { + ERR_FAIL_COND_V(p_tracker_id == 0, Ref()); for (int i = 0; i < trackers.size(); i++) { if (trackers[i]->get_type() == p_tracker_type && trackers[i]->get_tracker_id() == p_tracker_id) { @@ -310,7 +315,7 @@ ARVRPositionalTracker *ARVRServer::find_by_type_and_id(TrackerType p_tracker_typ }; }; - return NULL; + return Ref(); }; Ref ARVRServer::get_primary_interface() const { diff --git a/servers/arvr_server.h b/servers/arvr_server.h index bfb44145392..5c32f371b36 100644 --- a/servers/arvr_server.h +++ b/servers/arvr_server.h @@ -77,7 +77,7 @@ public: private: Vector > interfaces; - Vector trackers; + Vector > trackers; Ref primary_interface; /* we'll identify one interface as primary, this will be used by our viewports */ @@ -167,11 +167,11 @@ public: */ bool is_tracker_id_in_use_for_type(TrackerType p_tracker_type, int p_tracker_id) const; int get_free_tracker_id_for_type(TrackerType p_tracker_type); - void add_tracker(ARVRPositionalTracker *p_tracker); - void remove_tracker(ARVRPositionalTracker *p_tracker); + void add_tracker(Ref p_tracker); + void remove_tracker(Ref p_tracker); int get_tracker_count() const; - ARVRPositionalTracker *get_tracker(int p_index) const; - ARVRPositionalTracker *find_by_type_and_id(TrackerType p_tracker_type, int p_tracker_id) const; + Ref get_tracker(int p_index) const; + Ref find_by_type_and_id(TrackerType p_tracker_type, int p_tracker_id) const; uint64_t get_last_process_usec(); uint64_t get_last_commit_usec();