webxr_api/
mock.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use euclid::{Point2D, Rect, RigidTransform3D, Transform3D};
6#[cfg(feature = "ipc")]
7use serde::{Deserialize, Serialize};
8
9use crate::{
10    DiscoveryAPI, Display, EntityType, Error, Floor, Handedness, Input, InputId, InputSource,
11    LeftEye, Native, RightEye, SelectEvent, SelectKind, TargetRayMode, Triangle, Viewer, Viewport,
12    Visibility, WebXrReceiver, WebXrSender,
13};
14
15/// A trait for discovering mock XR devices
16pub trait MockDiscoveryAPI<GL>: 'static {
17    fn simulate_device_connection(
18        &mut self,
19        init: MockDeviceInit,
20        receiver: WebXrReceiver<MockDeviceMsg>,
21    ) -> Result<Box<dyn DiscoveryAPI<GL>>, Error>;
22}
23
24#[derive(Clone, Debug)]
25#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
26pub struct MockDeviceInit {
27    pub floor_origin: Option<RigidTransform3D<f32, Floor, Native>>,
28    pub supports_inline: bool,
29    pub supports_vr: bool,
30    pub supports_ar: bool,
31    pub viewer_origin: Option<RigidTransform3D<f32, Viewer, Native>>,
32    pub views: MockViewsInit,
33    pub supported_features: Vec<String>,
34    pub world: Option<MockWorld>,
35}
36
37#[derive(Clone, Debug)]
38#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
39pub struct MockViewInit<Eye> {
40    pub transform: RigidTransform3D<f32, Viewer, Eye>,
41    pub projection: Transform3D<f32, Eye, Display>,
42    pub viewport: Rect<i32, Viewport>,
43    /// field of view values, in radians
44    pub fov: Option<(f32, f32, f32, f32)>,
45}
46
47#[derive(Clone, Debug)]
48#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
49pub enum MockViewsInit {
50    Mono(MockViewInit<Viewer>),
51    Stereo(MockViewInit<LeftEye>, MockViewInit<RightEye>),
52}
53
54#[derive(Debug)]
55#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
56pub enum MockDeviceMsg {
57    SetViewerOrigin(Option<RigidTransform3D<f32, Viewer, Native>>),
58    SetFloorOrigin(Option<RigidTransform3D<f32, Floor, Native>>),
59    SetViews(MockViewsInit),
60    AddInputSource(MockInputInit),
61    MessageInputSource(InputId, MockInputMsg),
62    VisibilityChange(Visibility),
63    SetWorld(MockWorld),
64    ClearWorld,
65    Disconnect(WebXrSender<()>),
66    SetBoundsGeometry(Vec<Point2D<f32, Floor>>),
67    SimulateResetPose,
68}
69
70#[derive(Clone, Debug)]
71#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
72pub struct MockInputInit {
73    pub source: InputSource,
74    pub pointer_origin: Option<RigidTransform3D<f32, Input, Native>>,
75    pub grip_origin: Option<RigidTransform3D<f32, Input, Native>>,
76    pub supported_buttons: Vec<MockButton>,
77}
78
79#[derive(Debug)]
80#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
81pub enum MockInputMsg {
82    SetHandedness(Handedness),
83    SetTargetRayMode(TargetRayMode),
84    SetProfiles(Vec<String>),
85    SetPointerOrigin(Option<RigidTransform3D<f32, Input, Native>>),
86    SetGripOrigin(Option<RigidTransform3D<f32, Input, Native>>),
87    /// Note: SelectEvent::Select here refers to a complete Select event,
88    /// not just the end event, i.e. it refers to
89    /// <https://immersive-web.github.io/webxr-test-api/#dom-fakexrinputcontroller-simulateselect>
90    TriggerSelect(SelectKind, SelectEvent),
91    Disconnect,
92    Reconnect,
93    SetSupportedButtons(Vec<MockButton>),
94    UpdateButtonState(MockButton),
95}
96
97#[derive(Clone, Debug)]
98#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
99pub struct MockRegion {
100    pub faces: Vec<Triangle>,
101    pub ty: EntityType,
102}
103
104#[derive(Clone, Debug)]
105#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
106pub struct MockWorld {
107    pub regions: Vec<MockRegion>,
108}
109
110#[derive(Clone, Debug, PartialEq)]
111#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
112pub enum MockButtonType {
113    Grip,
114    Touchpad,
115    Thumbstick,
116    OptionalButton,
117    OptionalThumbstick,
118}
119
120#[derive(Clone, Debug)]
121#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
122pub struct MockButton {
123    pub button_type: MockButtonType,
124    pub pressed: bool,
125    pub touched: bool,
126    pub pressed_value: f32,
127    pub x_value: f32,
128    pub y_value: f32,
129}