Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Allows retrieval of the Oculus primary controller id #111

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ovr_guardian_system = null;
var ovr_tracking_transform = null;
var ovr_utilities = null;
var ovr_vr_api_proxy = null;
var ovr_input = null;

# Dictionary tracking the remaining duration for controllers vibration
var controllers_vibration_duration = {}
Expand Down Expand Up @@ -66,6 +67,7 @@ func _initialize_ovr_mobile_arvr_interface():
ovr_tracking_transform = load("res://addons/godot_ovrmobile/OvrTrackingTransform.gdns");
ovr_utilities = load("res://addons/godot_ovrmobile/OvrUtilities.gdns");
ovr_vr_api_proxy = load("res://addons/godot_ovrmobile/OvrVrApiProxy.gdns");
ovr_input = load("res://addons/godot_ovrmobile/OvrInput.gdns")

# and now instance the .gdns classes for use if load was successfull
if (ovr_display_refresh_rate): ovr_display_refresh_rate = ovr_display_refresh_rate.new()
Expand All @@ -74,6 +76,7 @@ func _initialize_ovr_mobile_arvr_interface():
if (ovr_tracking_transform): ovr_tracking_transform = ovr_tracking_transform.new()
if (ovr_utilities): ovr_utilities = ovr_utilities.new()
if (ovr_vr_api_proxy): ovr_vr_api_proxy = ovr_vr_api_proxy.new()
if (ovr_input): ovr_input = ovr_input.new()

# Connect to the plugin signals
_connect_to_signals()
Expand Down Expand Up @@ -212,6 +215,8 @@ enum CONTROLLER_BUTTON {

# this is a function connected to the button release signal from the controller
func _on_LeftTouchController_button_pressed(button):
print("Primary controller id: " + str(ovr_input.get_primary_controller_id()))

if (button == CONTROLLER_BUTTON.YB):
# examples on using the ovr api from gdscript
if (ovr_guardian_system):
Expand Down Expand Up @@ -246,6 +251,8 @@ func _on_LeftTouchController_button_pressed(button):
_start_controller_vibration($LeftTouchController, 40, 0.5)

func _on_RightTouchController_button_pressed(button):
print("Primary controller id: " + str(ovr_input.get_primary_controller_id()))

if (button == CONTROLLER_BUTTON.YB):
if (ovr_utilities):
# use this for fade to black for example: here we just do a color change
Expand Down
26 changes: 26 additions & 0 deletions plugin/src/main/cpp/api/ovr_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
#include "api_common.h"

namespace ovrmobile {

OvrInput::OvrInput() {}

OvrInput::~OvrInput() {}

void OvrInput::_init() {}

void OvrInput::_register_methods() {
register_method("get_primary_controller_id", &OvrInput::get_primary_controller_id);
}

int OvrInput::get_primary_controller_id() {
return ovrmobile::get_primary_controller_id(OvrMobileSession::get_singleton_instance());
}

bool vibrate_controller(OvrMobileSession* session,
int controller_id,
int duration_in_ms,
Expand All @@ -16,4 +31,15 @@ bool vibrate_controller(OvrMobileSession* session,
return result;
}, []() { return false; });
}

int get_primary_controller_id(OvrMobileSession* session) {
return check_session_initialized<int>(session, [&]() {
int controller_id = kInvalidGodotControllerId;
OvrMobileController* controller = session->get_ovr_mobile_controller();
if (controller) {
controller_id = controller->get_active_controller_id();
}
return controller_id;
}, []() { return kInvalidGodotControllerId; });
}
} // namespace ovrmobile
26 changes: 24 additions & 2 deletions plugin/src/main/cpp/api/ovr_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@
#include "ovr_mobile_session.h"

namespace ovrmobile {
// Vibrate the controller matching the given controller ID.
// Returns true if the controller was vibrated, false otherwise.
namespace {
using namespace godot;
}

class OvrInput : public Reference {
GODOT_CLASS(OvrInput, Reference)

public:
OvrInput();

~OvrInput();

static void _register_methods();

void _init();

int get_primary_controller_id();
};

/// Vibrate the controller matching the given controller ID.
/// Returns true if the controller was vibrated, false otherwise.
bool vibrate_controller(OvrMobileSession* session,
int controller_id,
int duration_in_ms,
float intensity);

/// Return the id for the primary controller.
int get_primary_controller_id(OvrMobileSession* session);
} // namespace ovrmobile

#endif // OVR_INPUT_H
5 changes: 3 additions & 2 deletions plugin/src/main/cpp/gdnative/godot_ovrmobile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

#include "arvr_interface.h"

#include "api/ovr_input.h"
#include "gdnative/nativescript/ovr_display_refresh_rate_ns.h"
#include "gdnative/nativescript/ovr_guardian_system_ns.h"
#include "gdnative/nativescript/ovr_init_config_ns.h"
#include "gdnative/nativescript/ovr_performance_ns.h"
#include "gdnative/nativescript/ovr_tracking_transform_ns.h"
#include "gdnative/nativescript/ovr_utilities_ns.h"
#include "gdnative/nativescript/ovr_hand_tracking_ns.h"
#include "gdnative/nativescript/ovr_input_ns.h"

// experimental low-level VrApi access
#include "gdnative/nativescript/ovr_vr_api_proxy_ns.h"
Expand All @@ -43,7 +43,8 @@ void GDN_EXPORT godot_ovrmobile_nativescript_init(void *handle) {
register_gdnative_utilities(handle);
register_gdnative_hand_tracking(handle);
register_gdnative_vr_api_proxy(handle);
register_gdnative_input(handle);

godot::register_class<ovrmobile::OvrInput>();
}

void GDN_EXPORT godot_ovrmobile_nativescript_terminate(void *handle) {
Expand Down
29 changes: 0 additions & 29 deletions plugin/src/main/cpp/gdnative/nativescript/ovr_input_ns.cpp

This file was deleted.

20 changes: 0 additions & 20 deletions plugin/src/main/cpp/gdnative/nativescript/ovr_input_ns.h

This file was deleted.

6 changes: 5 additions & 1 deletion plugin/src/main/cpp/jni/ovr_input_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
extern "C" {

JNIEXPORT void JNICALL
JNI_METHOD(vibrateController)(JNIEnv* env,
JNI_METHOD(vibrateController)(JNIEnv*,
jclass,
jobject,
jint controller_id,
Expand All @@ -24,4 +24,8 @@ JNI_METHOD(vibrateController)(JNIEnv* env,
ovrmobile::vibrate_controller(get_session(), controller_id, duration_in_ms, intensity);
}

JNIEXPORT jint JNICALL JNI_METHOD(getPrimaryControllerId)(JNIEnv*, jclass, jobject) {
return ovrmobile::get_primary_controller_id(get_session());
}

};
10 changes: 5 additions & 5 deletions plugin/src/main/cpp/ovr_mobile_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void OvrMobileController::update_controller_tracking_state_hand(ovrMobile *ovr,


void OvrMobileController::update_controllers_connection_state(ovrMobile *ovr, ovrJava *java) {
// Reset the controllers connected and active state.
// Reset the controllers connected and primary state.
for (auto &controller : controllers) {
controller.connected = false;
controller.primary = false;
Expand Down Expand Up @@ -299,16 +299,16 @@ void OvrMobileController::update_controllers_connection_state(ovrMobile *ovr, ov
}
}

// Get the active input device id.
int active_input_device_id = -1;
vrapi_GetPropertyInt(java, VRAPI_ACTIVE_INPUT_DEVICE_ID, &active_input_device_id);
// Get the primary input device id.
int primary_input_device_id = -1;
vrapi_GetPropertyInt(java, VRAPI_ACTIVE_INPUT_DEVICE_ID, &primary_input_device_id);

for (int hand = 0; hand < MAX_HANDS; hand++) {
ControllerState *controller = &controllers[hand];

// Notify Godot of the updated connection states.
if (controller->connected) {
controller->primary = controller->capability_header.DeviceID == active_input_device_id;
controller->primary = controller->capability_header.DeviceID == primary_input_device_id;

if (controller->godot_controller_id == kInvalidGodotControllerId) {
// Register the controller with Godot.
Expand Down
20 changes: 10 additions & 10 deletions plugin/src/main/cpp/ovr_mobile_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ class OvrMobileController {
}
}

int get_active_controller_id() const {
for (int hand = 0; hand < MAX_HANDS; hand++) {
auto* controller = &controllers[hand];
if (controller->connected && controller->primary) {
return controller->godot_controller_id;
}
}
return kInvalidGodotControllerId;
}

private:
struct ControllerVibration {
float intensity;
Expand All @@ -92,16 +102,6 @@ class OvrMobileController {
ControllerState controllers[MAX_HANDS];
std::map<int, ControllerVibration> controllers_vibrations;

int get_active_controller_id() const {
for (int hand = 0; hand < MAX_HANDS; hand++) {
auto* controller = &controllers[hand];
if (controller->connected && controller->primary) {
return controller->godot_controller_id;
}
}
return kInvalidGodotControllerId;
}

inline bool has_analog_grip_trigger(const ovrInputTrackedRemoteCapabilities &capabilities) const {
return check_bit(capabilities.ControllerCapabilities, ovrControllerCaps_HasAnalogGripTrigger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ private const val PRIMARY_CONTROLLER_ID = -1
*/
@JvmOverloads
external fun OvrMobilePlugin.vibrateController(controllerId: Int = PRIMARY_CONTROLLER_ID, durationInMs: Int, intensity: Float)

/**
* Return the id for the primary controller.
* Returns -1 if there is no primary controller.
*/
external fun OvrMobilePlugin.getPrimaryControllerId(): Int