use super::XRPlatform;
use std::collections::{HashMap, BTreeMap};
use std::time::SystemTime;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XRMode {
VR,
AR,
MR,
}
impl Default for XRMode {
fn default() -> Self {
Self::VR
}
}
#[derive(Debug, Clone)]
pub struct XRConfig {
pub mode: XRMode,
pub platform: XRPlatform,
pub enable_hand_tracking: bool,
pub enable_eye_tracking: bool,
}
impl Default for XRConfig {
fn default() -> Self {
Self {
mode: XRMode::VR,
platform: XRPlatform::WebXR,
enable_hand_tracking: false,
enable_eye_tracking: false,
}
}
}
pub struct XRRuntime {
config: XRConfig,
session_active: bool,
frame_number: u64,
}
impl XRRuntime {
pub fn new(config: XRConfig) -> Result<Self, XRError> {
Ok(Self {
config,
session_active: false,
frame_number: 0,
})
}
pub fn config(&self) -> &XRConfig {
&self.config
}
pub fn supports_hand_tracking(&self) -> bool {
self.config.enable_hand_tracking
}
pub fn supports_eye_tracking(&self) -> bool {
self.config.enable_eye_tracking
}
pub fn is_webxr_compatible(&self) -> bool {
matches!(self.config.platform, XRPlatform::WebXR)
}
pub fn start_session(&mut self) -> Result<(), XRError> {
self.session_active = true;
Ok(())
}
pub fn end_session(&mut self) {
self.session_active = false;
}
pub fn is_session_active(&self) -> bool {
self.session_active
}
pub fn get_frame(&mut self) -> Result<XRFrame, XRError> {
self.frame_number += 1;
Ok(XRFrame {
frame_number: self.frame_number,
timestamp_ns: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos() as u64,
view_count: if self.config.mode == XRMode::VR { 2 } else { 1 },
})
}
}
#[derive(Debug, Clone)]
pub struct XRFrame {
pub frame_number: u64,
pub timestamp_ns: u64,
pub view_count: u32,
}
#[derive(Debug, Clone)]
pub enum XRError {
InitializationFailed(String),
SessionCreationFailed(String),
DeviceNotFound,
UnsupportedMode(XRMode),
}
impl std::fmt::Display for XRError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InitializationFailed(msg) => write!(f, "XR 初始化失败: {}", msg),
Self::SessionCreationFailed(msg) => write!(f, "会话创建失败: {}", msg),
Self::DeviceNotFound => write!(f, "XR 设备未找到"),
Self::UnsupportedMode(mode) => write!(f, "不支持的 XR 模式: {:?}", mode),
}
}
}
impl std::error::Error for XRError {}