summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: b61048658571f76b634bbd9f59db172cfca70fcd (plain) (blame)
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
mod render;
mod window;
mod window_controller;
mod window_manager;

pub use render::*;
pub use window::*;
pub use window_controller::*;
pub use window_manager::*;

pub use buffer::{Buffer, Colour};
pub use winit::{
    event::{ModifiersState, ElementState},
    event::VirtualKeyCode as KeyCode,
    window::CursorIcon,
};

pub type Point = geometry::Point<i32>;
pub type Dimensions = geometry::Dimensions<u32>;
pub type Rect = geometry::Rect<i32, u32>;

// -----------------------------------------------------------------------------

#[derive(Copy, Clone)]
pub struct KeyboardInput {
    pub action: Action,
    pub key: KeyCode,
}

impl TryFrom<winit::event::KeyboardInput> for KeyboardInput {
    type Error = ();

    fn try_from(input: winit::event::KeyboardInput) -> Result<Self, ()> {
        if let Some(key) = input.virtual_keycode {
            Ok( Self { action: input.state.into(), key } )
        } else {
            Err(())
        }
    }
}

// -----------------------------------------------------------------------------

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Action { Pressed, Released }

impl Action {
    pub fn is_pressed(&self)  -> bool { *self == Self::Pressed  }
    pub fn is_released(&self) -> bool { *self == Self::Released }
}

impl From<ElementState> for Action {
    fn from(value: ElementState) -> Self {
        match value {
            ElementState::Pressed => Action::Pressed,
            ElementState::Released => Action::Released,
        }
    }
}