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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use crate::rect::Rect;
use crate::video::Window;
use crate::EventPump;
use std::fmt;
use std::mem::transmute;
use crate::sys;
mod keycode;
mod scancode;
pub use self::keycode::Keycode;
pub use self::scancode::Scancode;
bitflags! {
pub struct Mod: u16 {
const NOMOD = 0x0000;
const LSHIFTMOD = 0x0001;
const RSHIFTMOD = 0x0002;
const LCTRLMOD = 0x0040;
const RCTRLMOD = 0x0080;
const LALTMOD = 0x0100;
const RALTMOD = 0x0200;
const LGUIMOD = 0x0400;
const RGUIMOD = 0x0800;
const NUMMOD = 0x1000;
const CAPSMOD = 0x2000;
const MODEMOD = 0x4000;
const RESERVEDMOD = 0x8000;
}
}
impl fmt::Display for Mod {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:04x}", *self)
}
}
pub struct KeyboardState<'a> {
keyboard_state: &'a [u8],
}
impl<'a> KeyboardState<'a> {
#[doc(alias = "SDL_GetKeyboardState")]
pub fn new(_e: &'a EventPump) -> KeyboardState<'a> {
let keyboard_state = unsafe {
let mut count = 0;
let state_ptr = sys::SDL_GetKeyboardState(&mut count);
::std::slice::from_raw_parts(state_ptr, count as usize)
};
KeyboardState { keyboard_state }
}
pub fn is_scancode_pressed(&self, scancode: Scancode) -> bool {
self.keyboard_state[scancode as i32 as usize] != 0
}
pub fn scancodes(&self) -> ScancodeIterator {
ScancodeIterator {
index: 0,
keyboard_state: self.keyboard_state,
}
}
pub fn pressed_scancodes(&self) -> PressedScancodeIterator {
PressedScancodeIterator {
iter: self.scancodes(),
}
}
}
pub struct ScancodeIterator<'a> {
index: i32,
keyboard_state: &'a [u8],
}
impl<'a> Iterator for ScancodeIterator<'a> {
type Item = (Scancode, bool);
fn next(&mut self) -> Option<(Scancode, bool)> {
if self.index < self.keyboard_state.len() as i32 {
let index = self.index;
self.index += 1;
if let Some(scancode) = Scancode::from_i32(index) {
let pressed = self.keyboard_state[index as usize] != 0;
Some((scancode, pressed))
} else {
self.next()
}
} else {
None
}
}
}
pub struct PressedScancodeIterator<'a> {
iter: ScancodeIterator<'a>,
}
impl<'a> Iterator for PressedScancodeIterator<'a> {
type Item = Scancode;
fn next(&mut self) -> Option<Scancode> {
while let Some((scancode, pressed)) = self.iter.next() {
if pressed {
return Some(scancode);
}
}
None
}
}
impl crate::Sdl {
#[inline]
pub fn keyboard(&self) -> KeyboardUtil {
KeyboardUtil {
_sdldrop: self.sdldrop(),
}
}
}
impl crate::VideoSubsystem {
#[inline]
pub fn text_input(&self) -> TextInputUtil {
TextInputUtil {
_subsystem: self.clone(),
}
}
}
pub struct KeyboardUtil {
_sdldrop: ::std::rc::Rc<crate::SdlDrop>,
}
impl KeyboardUtil {
#[doc(alias = "SDL_GetKeyboardFocus")]
pub fn focused_window_id(&self) -> Option<u32> {
let raw = unsafe { sys::SDL_GetKeyboardFocus() };
if raw.is_null() {
None
} else {
let id = unsafe { sys::SDL_GetWindowID(raw) };
Some(id)
}
}
#[doc(alias = "SDL_GetModState")]
pub fn mod_state(&self) -> Mod {
unsafe { Mod::from_bits(sys::SDL_GetModState() as u16).unwrap() }
}
#[doc(alias = "SDL_SetModState")]
pub fn set_mod_state(&self, flags: Mod) {
unsafe {
sys::SDL_SetModState(transmute::<u32, sys::SDL_Keymod>(flags.bits() as u32));
}
}
}
pub struct TextInputUtil {
_subsystem: crate::VideoSubsystem,
}
impl TextInputUtil {
#[doc(alias = "SDL_StartTextInput")]
pub fn start(&self) {
unsafe {
sys::SDL_StartTextInput();
}
}
#[doc(alias = "SDL_IsTextInputActive")]
pub fn is_active(&self) -> bool {
unsafe { sys::SDL_IsTextInputActive() == sys::SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_StopTextInput")]
pub fn stop(&self) {
unsafe {
sys::SDL_StopTextInput();
}
}
#[doc(alias = "SDL_SetTextInputRect")]
pub fn set_rect(&self, rect: Rect) {
unsafe {
sys::SDL_SetTextInputRect(rect.raw() as *mut sys::SDL_Rect);
}
}
#[doc(alias = "SDL_HasScreenKeyboardSupport")]
pub fn has_screen_keyboard_support(&self) -> bool {
unsafe { sys::SDL_HasScreenKeyboardSupport() == sys::SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_IsScreenKeyboardShown")]
pub fn is_screen_keyboard_shown(&self, window: &Window) -> bool {
unsafe { sys::SDL_IsScreenKeyboardShown(window.raw()) == sys::SDL_bool::SDL_TRUE }
}
}