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
use fnv::FnvHashSet;
use glm::Vec2;
use graphics;
use Context;
pub use sdl2::keyboard::Keycode as Key;
pub(crate) struct InputContext {
pub(crate) current_key_state: FnvHashSet<Key>,
pub(crate) previous_key_state: FnvHashSet<Key>,
pub(crate) mouse_position: Vec2,
}
impl InputContext {
pub(crate) fn new() -> InputContext {
InputContext {
current_key_state: FnvHashSet::default(),
previous_key_state: FnvHashSet::default(),
mouse_position: Vec2::zeros(),
}
}
}
pub fn is_key_down(ctx: &Context, key: Key) -> bool {
ctx.input.current_key_state.contains(&key)
}
pub fn is_key_up(ctx: &Context, key: Key) -> bool {
!ctx.input.current_key_state.contains(&key)
}
pub fn is_key_pressed(ctx: &Context, key: Key) -> bool {
!ctx.input.previous_key_state.contains(&key) && ctx.input.current_key_state.contains(&key)
}
pub fn is_key_released(ctx: &Context, key: Key) -> bool {
ctx.input.previous_key_state.contains(&key) && !ctx.input.current_key_state.contains(&key)
}
pub fn get_mouse_x(ctx: &Context) -> f32 {
(ctx.input.mouse_position.x / graphics::get_window_width(ctx) as f32)
* graphics::get_width(ctx) as f32
}
pub fn get_mouse_y(ctx: &Context) -> f32 {
(ctx.input.mouse_position.y / graphics::get_window_height(ctx) as f32)
* graphics::get_height(ctx) as f32
}
pub fn get_mouse_position(ctx: &Context) -> Vec2 {
Vec2::new(get_mouse_x(ctx), get_mouse_y(ctx))
}