use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender;
use webxr_api::{
Handedness, InputId, MockButton, MockButtonType, MockDeviceMsg, MockInputMsg, SelectEvent,
SelectKind, TargetRayMode,
};
use crate::conversions::Convert;
use crate::dom::bindings::codegen::Bindings::FakeXRDeviceBinding::FakeXRRigidTransformInit;
use crate::dom::bindings::codegen::Bindings::FakeXRInputControllerBinding::{
FakeXRButtonStateInit, FakeXRButtonType, FakeXRInputControllerMethods,
};
use crate::dom::bindings::codegen::Bindings::XRInputSourceBinding::{
XRHandedness, XRTargetRayMode,
};
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::fakexrdevice::get_origin;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
#[dom_struct]
pub struct FakeXRInputController {
reflector: Reflector,
#[ignore_malloc_size_of = "defined in ipc-channel"]
#[no_trace]
sender: IpcSender<MockDeviceMsg>,
#[ignore_malloc_size_of = "defined in webxr-api"]
#[no_trace]
id: InputId,
}
impl FakeXRInputController {
pub fn new_inherited(sender: IpcSender<MockDeviceMsg>, id: InputId) -> FakeXRInputController {
FakeXRInputController {
reflector: Reflector::new(),
sender,
id,
}
}
pub fn new(
global: &GlobalScope,
sender: IpcSender<MockDeviceMsg>,
id: InputId,
) -> DomRoot<FakeXRInputController> {
reflect_dom_object(
Box::new(FakeXRInputController::new_inherited(sender, id)),
global,
CanGc::note(),
)
}
fn send_message(&self, msg: MockInputMsg) {
let _ = self
.sender
.send(MockDeviceMsg::MessageInputSource(self.id, msg));
}
}
impl FakeXRInputControllerMethods<crate::DomTypeHolder> for FakeXRInputController {
fn SetPointerOrigin(&self, origin: &FakeXRRigidTransformInit, _emulated: bool) -> Fallible<()> {
self.send_message(MockInputMsg::SetPointerOrigin(Some(get_origin(origin)?)));
Ok(())
}
fn SetGripOrigin(&self, origin: &FakeXRRigidTransformInit, _emulated: bool) -> Fallible<()> {
self.send_message(MockInputMsg::SetGripOrigin(Some(get_origin(origin)?)));
Ok(())
}
fn ClearGripOrigin(&self) {
self.send_message(MockInputMsg::SetGripOrigin(None))
}
fn Disconnect(&self) {
self.send_message(MockInputMsg::Disconnect)
}
fn Reconnect(&self) {
self.send_message(MockInputMsg::Reconnect)
}
fn StartSelection(&self) {
self.send_message(MockInputMsg::TriggerSelect(
SelectKind::Select,
SelectEvent::Start,
))
}
fn EndSelection(&self) {
self.send_message(MockInputMsg::TriggerSelect(
SelectKind::Select,
SelectEvent::End,
))
}
fn SimulateSelect(&self) {
self.send_message(MockInputMsg::TriggerSelect(
SelectKind::Select,
SelectEvent::Select,
))
}
fn SetHandedness(&self, handedness: XRHandedness) {
let h = match handedness {
XRHandedness::None => Handedness::None,
XRHandedness::Left => Handedness::Left,
XRHandedness::Right => Handedness::Right,
};
self.send_message(MockInputMsg::SetHandedness(h));
}
fn SetTargetRayMode(&self, target_ray_mode: XRTargetRayMode) {
let t = match target_ray_mode {
XRTargetRayMode::Gaze => TargetRayMode::Gaze,
XRTargetRayMode::Tracked_pointer => TargetRayMode::TrackedPointer,
XRTargetRayMode::Screen => TargetRayMode::Screen,
XRTargetRayMode::Transient_pointer => TargetRayMode::TransientPointer,
};
self.send_message(MockInputMsg::SetTargetRayMode(t));
}
fn SetProfiles(&self, profiles: Vec<DOMString>) {
let t = profiles.into_iter().map(String::from).collect();
self.send_message(MockInputMsg::SetProfiles(t));
}
fn SetSupportedButtons(&self, supported_buttons: Vec<FakeXRButtonStateInit>) {
let supported = init_to_mock_buttons(&supported_buttons);
self.send_message(MockInputMsg::SetSupportedButtons(supported));
}
fn UpdateButtonState(&self, button_state: &FakeXRButtonStateInit) -> Fallible<()> {
if (button_state.pressed || *button_state.pressedValue > 0.0) && !button_state.touched {
return Err(Error::Type("Pressed button must also be touched".into()));
}
if *button_state.pressedValue < 0.0 {
return Err(Error::Type("Pressed value must be non-negative".into()));
}
Ok(())
}
}
impl Convert<MockButtonType> for FakeXRButtonType {
fn convert(self) -> MockButtonType {
match self {
FakeXRButtonType::Grip => MockButtonType::Grip,
FakeXRButtonType::Touchpad => MockButtonType::Touchpad,
FakeXRButtonType::Thumbstick => MockButtonType::Thumbstick,
FakeXRButtonType::Optional_button => MockButtonType::OptionalButton,
FakeXRButtonType::Optional_thumbstick => MockButtonType::OptionalThumbstick,
}
}
}
pub fn init_to_mock_buttons(buttons: &[FakeXRButtonStateInit]) -> Vec<MockButton> {
let supported: Vec<MockButton> = buttons
.iter()
.map(|b| MockButton {
button_type: b.buttonType.convert(),
pressed: b.pressed,
touched: b.touched,
pressed_value: *b.pressedValue,
x_value: *b.xValue,
y_value: *b.yValue,
})
.collect();
supported
}