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 base::generic_channel::GenericReceiver;
6use euclid::{Point2D, Rect, RigidTransform3D, Transform3D};
7use profile_traits::generic_callback::GenericCallback as ProfileGenericCallback;
8use serde::{Deserialize, Serialize};
9
10use crate::{
11    DiscoveryAPI, Display, EntityType, Error, Floor, Handedness, Input, InputId, InputSource,
12    LeftEye, Native, RightEye, SelectEvent, SelectKind, TargetRayMode, Triangle, Viewer, Viewport,
13    Visibility,
14};
15
16/// A trait for discovering mock XR devices
17pub trait MockDiscoveryAPI<GL>: 'static {
18    fn simulate_device_connection(
19        &mut self,
20        init: MockDeviceInit,
21        receiver: GenericReceiver<MockDeviceMsg>,
22    ) -> Result<Box<dyn DiscoveryAPI<GL>>, Error>;
23}
24
25#[derive(Clone, Debug, 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, Serialize, Deserialize)]
38pub struct MockViewInit<Eye> {
39    pub transform: RigidTransform3D<f32, Viewer, Eye>,
40    pub projection: Transform3D<f32, Eye, Display>,
41    pub viewport: Rect<i32, Viewport>,
42    /// field of view values, in radians
43    pub fov: Option<(f32, f32, f32, f32)>,
44}
45
46#[derive(Clone, Debug, Serialize, Deserialize)]
47pub enum MockViewsInit {
48    Mono(MockViewInit<Viewer>),
49    Stereo(MockViewInit<LeftEye>, MockViewInit<RightEye>),
50}
51
52#[derive(Debug, Serialize, Deserialize)]
53pub enum MockDeviceMsg {
54    SetViewerOrigin(Option<RigidTransform3D<f32, Viewer, Native>>),
55    SetFloorOrigin(Option<RigidTransform3D<f32, Floor, Native>>),
56    SetViews(MockViewsInit),
57    AddInputSource(MockInputInit),
58    MessageInputSource(InputId, MockInputMsg),
59    VisibilityChange(Visibility),
60    SetWorld(MockWorld),
61    ClearWorld,
62    Disconnect(ProfileGenericCallback<()>),
63    SetBoundsGeometry(Vec<Point2D<f32, Floor>>),
64    SimulateResetPose,
65}
66
67#[derive(Clone, Debug, Serialize, Deserialize)]
68pub struct MockInputInit {
69    pub source: InputSource,
70    pub pointer_origin: Option<RigidTransform3D<f32, Input, Native>>,
71    pub grip_origin: Option<RigidTransform3D<f32, Input, Native>>,
72    pub supported_buttons: Vec<MockButton>,
73}
74
75#[derive(Debug, Serialize, Deserialize)]
76pub enum MockInputMsg {
77    SetHandedness(Handedness),
78    SetTargetRayMode(TargetRayMode),
79    SetProfiles(Vec<String>),
80    SetPointerOrigin(Option<RigidTransform3D<f32, Input, Native>>),
81    SetGripOrigin(Option<RigidTransform3D<f32, Input, Native>>),
82    /// Note: SelectEvent::Select here refers to a complete Select event,
83    /// not just the end event, i.e. it refers to
84    /// <https://immersive-web.github.io/webxr-test-api/#dom-fakexrinputcontroller-simulateselect>
85    TriggerSelect(SelectKind, SelectEvent),
86    Disconnect,
87    Reconnect,
88    SetSupportedButtons(Vec<MockButton>),
89    UpdateButtonState(MockButton),
90}
91
92#[derive(Clone, Debug, Serialize, Deserialize)]
93pub struct MockRegion {
94    pub faces: Vec<Triangle>,
95    pub ty: EntityType,
96}
97
98#[derive(Clone, Debug, Serialize, Deserialize)]
99pub struct MockWorld {
100    pub regions: Vec<MockRegion>,
101}
102
103#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
104pub enum MockButtonType {
105    Grip,
106    Touchpad,
107    Thumbstick,
108    OptionalButton,
109    OptionalThumbstick,
110}
111
112#[derive(Clone, Debug, Serialize, Deserialize)]
113pub struct MockButton {
114    pub button_type: MockButtonType,
115    pub pressed: bool,
116    pub touched: bool,
117    pub pressed_value: f32,
118    pub x_value: f32,
119    pub y_value: f32,
120}