wolfrpg_map_parser/command/input_key_command/
automatic_input.rs

1#[cfg(feature = "serde")]
2use serde::{Serialize, Deserialize};
3use crate::command::input_key_command::automatic_input::input_type::InputType;
4use crate::command::input_key_command::automatic_input::state::State;
5
6pub mod basic;
7pub mod state;
8pub mod basic_options;
9pub mod input_type;
10pub mod keyboard;
11pub mod mouse;
12pub mod mouse_options;
13pub mod mouse_type;
14
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16#[derive(PartialEq, Clone)]
17pub struct AutomaticInput {
18    input_type: InputType,
19    state: State,
20}
21
22impl AutomaticInput {
23    fn parse(bytes: &[u8], parse_state: fn(&[u8], &InputType) -> (usize, State)) -> (usize, Self) {
24        let mut offset: usize = 0;
25
26        let input_type: u8 = bytes[offset + 3];
27        let input_type: InputType = InputType::new(input_type);
28
29        let (bytes_read, state): (usize, State) = parse_state(&bytes[offset..], &input_type);
30        offset += bytes_read;
31
32        offset += 3; // Command end signature
33
34        (offset, Self {
35            input_type,
36            state
37        })
38    }
39
40    pub(crate) fn parse_base(bytes: &[u8]) -> (usize, Self) {
41        Self::parse(bytes, State::parse_base)
42    }
43
44    pub(crate) fn parse_keyboard(bytes: &[u8]) -> (usize, Self) {
45        Self::parse(bytes, State::parse_keyboard)
46    }
47
48    pub fn input_type(&self) -> &InputType {
49        &self.input_type
50    }
51
52    pub fn input_type_mut(&mut self) -> &mut InputType {
53        &mut self.input_type
54    }
55
56    pub fn state(&self) -> &State {
57        &self.state
58    }
59
60    pub fn state_mut(&mut self) -> &mut State {
61        &mut self.state
62    }
63}