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
mod gamepad;
mod keyboard;
mod mouse;
use hashbrown::HashSet;
use crate::math::Vec2;
use crate::{Context, Result};
pub use gamepad::*;
pub use keyboard::*;
pub use mouse::*;
pub(crate) struct InputContext {
keys_down: HashSet<Key>,
keys_pressed: HashSet<Key>,
keys_released: HashSet<Key>,
mouse_buttons_down: HashSet<MouseButton>,
mouse_buttons_pressed: HashSet<MouseButton>,
mouse_buttons_released: HashSet<MouseButton>,
mouse_position: Vec2<f32>,
mouse_wheel_movement: Vec2<i32>,
current_text_input: Option<String>,
pads: Vec<Option<GamepadState>>,
}
impl InputContext {
pub(crate) fn new() -> InputContext {
InputContext {
keys_down: HashSet::new(),
keys_pressed: HashSet::new(),
keys_released: HashSet::new(),
mouse_buttons_down: HashSet::new(),
mouse_buttons_pressed: HashSet::new(),
mouse_buttons_released: HashSet::new(),
mouse_position: Vec2::zero(),
mouse_wheel_movement: Vec2::zero(),
current_text_input: None,
pads: Vec::new(),
}
}
}
pub(crate) fn clear(ctx: &mut Context) {
ctx.input.keys_pressed.clear();
ctx.input.keys_released.clear();
ctx.input.mouse_buttons_pressed.clear();
ctx.input.mouse_buttons_released.clear();
ctx.input.mouse_wheel_movement = Vec2::zero();
ctx.input.current_text_input = None;
for slot in &mut ctx.input.pads {
if let Some(pad) = slot {
pad.buttons_pressed.clear();
pad.buttons_released.clear();
}
}
}
pub fn get_text_input(ctx: &Context) -> Option<&str> {
ctx.input.current_text_input.as_deref()
}
pub fn get_clipboard_text(ctx: &Context) -> Result<String> {
ctx.window.get_clipboard_text()
}
pub fn set_clipboard_text(ctx: &Context, text: &str) -> Result {
ctx.window.set_clipboard_text(text)
}
pub(crate) fn push_text_input(ctx: &mut Context, text: &str) {
match &mut ctx.input.current_text_input {
Some(existing) => existing.push_str(text),
x @ None => *x = Some(text.to_string()),
}
}