wolfrpg_map_parser/command/input_key_command/
input_key.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#[cfg(feature = "serde")]
use serde::Serialize;
use crate::byte_utils::as_u32_le;
use crate::command::input_key_command::input_key::input_type::InputType;
use crate::command::input_key_command::input_key::state::State;

pub mod state;
pub mod basic;
pub mod basic_options;
pub mod direction_keys;
pub mod input_type;
pub mod keyboard_or_pad;
pub mod key_options;
pub mod mouse_target;
pub mod mouse_options;
pub mod mouse;

#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct InputKey {
    variable: u32,
    input_type: InputType,
    specific_key: bool,
    state: State
}

impl InputKey {
    fn parse(bytes: &[u8], parse_state: fn(&[u8], &InputType) -> (usize, State)) -> (usize, Self) {
        let mut offset: usize = 0;

        let variable: u32 = as_u32_le(&bytes[offset..offset+4]);
        offset += 4;

        let input_type: u8 = bytes[offset + 1];
        let (input_type, specific_key): (InputType, bool) = Self::parse_input(input_type);

        let (bytes_read, state): (usize, State) = parse_state(&bytes[offset..], &input_type);
        offset += bytes_read;

        offset += 3; // Command end signature

        (offset, Self {
            variable,
            input_type,
            specific_key,
            state
        })
    }

    fn parse_input(input: u8) -> (InputType, bool) {
        (InputType::new(input & 0x0f), input & 0b00010000 != 0)
    }

    pub fn parse_base(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_base)
    }

    pub fn parse_keyboard_or_pad(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_keyboard_or_pad)
    }

    pub fn variable(&self) -> u32 {
        self.variable
    }

    pub fn variable_mut(&mut self) -> &mut u32 {
        &mut self.variable
    }

    pub fn input_type(&self) -> &InputType {
        &self.input_type
    }

    pub fn input_type_mut(&mut self) -> &mut InputType {
        &mut self.input_type
    }

    pub fn specific_key(&self) -> bool {
        self.specific_key
    }

    pub fn specific_key_mut(&mut self) -> &mut bool {
        &mut self.specific_key
    }

    pub fn state(&self) -> &State {
        &self.state
    }

    pub fn state_mut(&mut self) -> &mut State {
        &mut self.state
    }
}