wolfrpg_map_parser/command/input_key_command/
input_key.rs1#[cfg(feature = "serde")]
2use serde::{Serialize, Deserialize};
3use crate::byte_utils::as_u32_le;
4use crate::command::input_key_command::input_key::input_type::InputType;
5use crate::command::input_key_command::input_key::state::State;
6
7pub mod state;
8pub mod basic;
9pub mod basic_options;
10pub mod direction_keys;
11pub mod input_type;
12pub mod keyboard_or_pad;
13pub mod key_options;
14pub mod mouse_target;
15pub mod mouse_options;
16pub mod mouse;
17
18#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
19#[derive(PartialEq, Clone)]
20pub struct InputKey {
21 variable: u32,
22 input_type: InputType,
23 specific_key: bool,
24 state: State
25}
26
27impl InputKey {
28 fn parse(bytes: &[u8], parse_state: fn(&[u8], &InputType) -> (usize, State)) -> (usize, Self) {
29 let mut offset: usize = 0;
30
31 let variable: u32 = as_u32_le(&bytes[offset..offset+4]);
32 offset += 4;
33
34 let input_type: u8 = bytes[offset + 1];
35 let (input_type, specific_key): (InputType, bool) = Self::parse_input(input_type);
36
37 let (bytes_read, state): (usize, State) = parse_state(&bytes[offset..], &input_type);
38 offset += bytes_read;
39
40 offset += 3; (offset, Self {
43 variable,
44 input_type,
45 specific_key,
46 state
47 })
48 }
49
50 fn parse_input(input: u8) -> (InputType, bool) {
51 (InputType::new(input & 0x0f), input & 0b00010000 != 0)
52 }
53
54 pub(crate) fn parse_base(bytes: &[u8]) -> (usize, Self) {
55 Self::parse(bytes, State::parse_base)
56 }
57
58 pub(crate) fn parse_keyboard_or_pad(bytes: &[u8]) -> (usize, Self) {
59 Self::parse(bytes, State::parse_keyboard_or_pad)
60 }
61
62 pub fn variable(&self) -> u32 {
63 self.variable
64 }
65
66 pub fn variable_mut(&mut self) -> &mut u32 {
67 &mut self.variable
68 }
69
70 pub fn input_type(&self) -> &InputType {
71 &self.input_type
72 }
73
74 pub fn input_type_mut(&mut self) -> &mut InputType {
75 &mut self.input_type
76 }
77
78 pub fn specific_key(&self) -> bool {
79 self.specific_key
80 }
81
82 pub fn specific_key_mut(&mut self) -> &mut bool {
83 &mut self.specific_key
84 }
85
86 pub fn state(&self) -> &State {
87 &self.state
88 }
89
90 pub fn state_mut(&mut self) -> &mut State {
91 &mut self.state
92 }
93}