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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use fnv::FnvHashSet;
use glm::Vec2;
use crate::graphics;
use crate::Context;
pub use sdl2::keyboard::Keycode as Key;
pub use sdl2::mouse::MouseButton;
pub(crate) struct InputContext {
pub(crate) current_key_state: FnvHashSet<Key>,
pub(crate) previous_key_state: FnvHashSet<Key>,
pub(crate) current_mouse_state: FnvHashSet<MouseButton>,
pub(crate) previous_mouse_state: FnvHashSet<MouseButton>,
pub(crate) current_text_input: Option<String>,
pub(crate) mouse_position: Vec2,
}
impl InputContext {
pub(crate) fn new() -> InputContext {
InputContext {
current_key_state: FnvHashSet::default(),
previous_key_state: FnvHashSet::default(),
current_mouse_state: FnvHashSet::default(),
previous_mouse_state: FnvHashSet::default(),
current_text_input: None,
mouse_position: Vec2::zeros(),
}
}
pub(crate) fn cleanup_after_state_update(&mut self) {
self.previous_key_state = self.current_key_state.clone();
self.previous_mouse_state = self.current_mouse_state.clone();
self.current_text_input = None;
}
}
pub fn get_text_input(ctx: &Context) -> Option<&str> {
ctx.input.current_text_input.as_ref().map(String::as_str)
}
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_keys_down(ctx: &Context) -> impl Iterator<Item = &Key> {
ctx.input.current_key_state.iter()
}
pub fn get_keys_pressed(ctx: &Context) -> impl Iterator<Item = &Key> {
ctx.input
.current_key_state
.difference(&ctx.input.previous_key_state)
}
pub fn get_keys_released(ctx: &Context) -> impl Iterator<Item = &Key> {
ctx.input
.previous_key_state
.difference(&ctx.input.current_key_state)
}
pub fn is_mouse_button_down(ctx: &Context, button: MouseButton) -> bool {
ctx.input.current_mouse_state.contains(&button)
}
pub fn is_mouse_button_up(ctx: &Context, button: MouseButton) -> bool {
!ctx.input.current_mouse_state.contains(&button)
}
pub fn is_mouse_button_pressed(ctx: &Context, button: MouseButton) -> bool {
!ctx.input.previous_mouse_state.contains(&button)
&& ctx.input.current_mouse_state.contains(&button)
}
pub fn is_mouse_button_released(ctx: &Context, button: MouseButton) -> bool {
ctx.input.previous_mouse_state.contains(&button)
&& !ctx.input.current_mouse_state.contains(&button)
}
pub fn get_mouse_x(ctx: &Context) -> f32 {
let internal_width = graphics::get_internal_width(ctx) as f32;
let screen_rect = graphics::get_screen_rect(ctx);
((ctx.input.mouse_position.x - screen_rect.x) / screen_rect.width) * internal_width
}
pub fn get_mouse_y(ctx: &Context) -> f32 {
let internal_height = graphics::get_internal_height(ctx) as f32;
let screen_rect = graphics::get_screen_rect(ctx);
((ctx.input.mouse_position.y - screen_rect.y) / screen_rect.height) * internal_height
}
pub fn get_mouse_position(ctx: &Context) -> Vec2 {
Vec2::new(get_mouse_x(ctx), get_mouse_y(ctx))
}