use std::time::Instant;
use cranpose_app_shell::FrameUpdateResult;
use winit::event_loop::ControlFlow;
#[cfg(feature = "robot")]
use super::robot_driver::{RobotController, bound_robot_park};
use crate::{app_launcher::AppSettings, wgpu_surface::PresentStep};
#[derive(Default)]
pub(super) struct RobotSeat {
#[cfg(feature = "robot")]
pub(super) controller: Option<RobotController>,
#[cfg(feature = "robot")]
pub(super) app_hook: Option<Box<crate::RobotAppHook>>,
#[cfg(feature = "robot")]
pub(super) presented_frame_generation: u64,
#[cfg(feature = "robot")]
pub(super) unpresentable_frames: u32,
#[cfg(feature = "robot")]
pub(super) visible_surface_dirty: bool,
}
impl RobotSeat {
#[cfg_attr(
not(feature = "robot"),
expect(unused_variables, reason = "only a robot build keeps the app hook")
)]
pub(super) fn new(settings: &mut AppSettings) -> Self {
#[cfg(feature = "robot")]
return Self {
app_hook: settings.robot_app_hook.take(),
..Self::default()
};
#[cfg(not(feature = "robot"))]
Self::default()
}
pub(super) fn drives(&self) -> bool {
#[cfg(feature = "robot")]
return self.controller.is_some();
#[cfg(not(feature = "robot"))]
false
}
pub(super) fn synthetic_primary_down(&self) -> bool {
#[cfg(feature = "robot")]
return self
.controller
.as_ref()
.is_some_and(RobotController::synthetic_primary_down);
#[cfg(not(feature = "robot"))]
false
}
#[cfg_attr(
not(feature = "robot"),
expect(unused_variables, reason = "only a robot reads the update result")
)]
pub(super) fn record_idle_update_result(&mut self, result: FrameUpdateResult) {
#[cfg(feature = "robot")]
if let Some(controller) = &mut self.controller {
controller.record_idle_update_result(result);
}
}
pub(super) fn stage_pending_command(&mut self) -> bool {
#[cfg(feature = "robot")]
return self
.controller
.as_mut()
.is_some_and(RobotController::stage_pending_command);
#[cfg(not(feature = "robot"))]
false
}
pub(super) fn visible_surface_dirty(&self) -> bool {
#[cfg(feature = "robot")]
return self.visible_surface_dirty;
#[cfg(not(feature = "robot"))]
false
}
pub(super) fn note_unpresentable(&mut self) {
#[cfg(feature = "robot")]
{
self.unpresentable_frames = self.unpresentable_frames.saturating_add(1);
}
}
pub(super) fn note_presented(&mut self) {
#[cfg(feature = "robot")]
{
self.presented_frame_generation = self.presented_frame_generation.saturating_add(1);
self.unpresentable_frames = 0;
self.visible_surface_dirty = false;
}
}
#[cfg_attr(
not(feature = "robot"),
expect(unused_variables, reason = "only a robot tracks the visible surface")
)]
pub(super) fn note_skipped_present(&mut self, step: PresentStep) {
#[cfg(feature = "robot")]
if step == PresentStep::Shown {
self.note_presented();
} else {
self.visible_surface_dirty = false;
}
}
pub(super) fn awaiting_progress(&mut self) -> bool {
#[cfg(feature = "robot")]
return self
.controller
.as_mut()
.is_some_and(RobotController::awaiting_progress);
#[cfg(not(feature = "robot"))]
false
}
#[cfg_attr(
not(feature = "robot"),
expect(unused_variables, reason = "only a robot bounds the park")
)]
pub(super) fn bound_park(&self, control_flow: ControlFlow, now: Instant) -> ControlFlow {
#[cfg(feature = "robot")]
if self.controller.is_some() {
return bound_robot_park(control_flow, now);
}
control_flow
}
}