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
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::{
    input::{ActionState, KeyCode},
    message::{MessageMeta, MessageType},
};

/// Describes an action that can be registered with the engine.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegisterAction {
    pub id: String,
    pub default_key: KeyCode,
}

impl RegisterAction {
    pub fn new(id: String, default_key: KeyCode) -> Self {
        Self { id, default_key }
    }
}

impl<T: Into<String>> From<(T, KeyCode)> for RegisterAction {
    fn from((id, default_key): (T, KeyCode)) -> Self {
        Self::new(id.into(), default_key)
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct ActionsBuilder(Vec<RegisterAction>);

impl ActionsBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn push(mut self, id: impl Into<String>, default_key: KeyCode) -> Self {
        self.0.push(RegisterAction::new(id.into(), default_key));
        self
    }

    pub fn build(self) -> Vec<RegisterAction> {
        self.0
    }
}

/// Describes an event that is sent when an action is triggered.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActionEvent {
    pub name: String,
    pub state: ActionState,
}

impl MessageType for ActionEvent {
    const MESSAGE_META: MessageMeta = MessageMeta::new("builtin", "action_event", None);
}

/// Describes the kind of action that was triggered.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum ActionKind {
    Pressed = 1,
    Released = 2,
}