use openxr::{Duration, HapticVibration};
use crate::resources::{HapticContext, XrContext};
static HAPTIC_FREQUENCY: f32 = 400.;
static HAPTIC_DURATION: i64 = 1e+8 as _;
pub fn apply_haptic_feedback(xr_context: &mut XrContext, haptic_context: &mut HapticContext) {
let haptic_duration = Duration::from_nanos(HAPTIC_DURATION);
if haptic_context.left_hand_amplitude_this_frame != 0. {
let event = HapticVibration::new()
.amplitude(haptic_context.left_hand_amplitude_this_frame)
.frequency(HAPTIC_FREQUENCY)
.duration(haptic_duration);
xr_context
.haptic_feedback_action
.apply_feedback(
&xr_context.session,
xr_context.left_hand_subaction_path,
&event,
)
.expect("Unable to apply haptic feedback!");
haptic_context.left_hand_amplitude_this_frame = 0.;
}
if haptic_context.right_hand_amplitude_this_frame != 0. {
let event = HapticVibration::new()
.amplitude(haptic_context.right_hand_amplitude_this_frame)
.frequency(HAPTIC_FREQUENCY)
.duration(haptic_duration);
xr_context
.haptic_feedback_action
.apply_feedback(
&xr_context.session,
xr_context.right_hand_subaction_path,
&event,
)
.expect("Unable to apply haptic feedback!");
haptic_context.right_hand_amplitude_this_frame = 0.;
}
}
#[cfg(target_os = "windows")]
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn apply_haptic_feedback_test() {
let (mut xr_context, _) = XrContext::new().unwrap();
let mut haptic_context = HapticContext::default();
apply_haptic_feedback(&mut xr_context, &mut haptic_context);
}
}