Skip to main content

pfx_input/
backend.rs

1use std::collections::VecDeque;
2
3use crate::event::InputEvent;
4use crate::rumble::MotorCommand;
5
6pub trait Backend {
7    fn poll(&mut self, events: &mut Vec<InputEvent>);
8
9    fn set_motors(&mut self, command: &MotorCommand) -> bool;
10
11    fn steam_input(&mut self, _active: bool) {}
12}
13
14#[derive(Clone, Debug, Default)]
15pub struct Script {
16    steps: VecDeque<Vec<InputEvent>>,
17    pub motors: Vec<MotorCommand>,
18}
19
20impl Script {
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    pub fn push(&mut self, events: Vec<InputEvent>) {
26        self.steps.push_back(events);
27    }
28
29    pub fn remaining(&self) -> usize {
30        self.steps.len()
31    }
32}
33
34impl Backend for Script {
35    fn poll(&mut self, events: &mut Vec<InputEvent>) {
36        if let Some(step) = self.steps.pop_front() {
37            events.extend(step);
38        }
39    }
40
41    fn set_motors(&mut self, command: &MotorCommand) -> bool {
42        self.motors.push(*command);
43        true
44    }
45}