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
use crate::EventPump;
use crate::rect::Rect;
use crate::video::Window;
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;
}
}
pub struct KeyboardState<'a> {
keyboard_state: &'a [u8]
}
impl<'a> KeyboardState<'a> {
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: keyboard_state
}
}
/// Returns true if the scancode is pressed.
///
/// # Example
/// ```no_run
/// use sdl2::keyboard::Scancode;
///
/// fn is_a_pressed(e: &sdl2::EventPump) -> bool {
/// e.keyboard_state().is_scancode_pressed(Scancode::A)
/// }
/// ```
pub fn is_scancode_pressed(&self, scancode: Scancode) -> bool {
self.keyboard_state[scancode as i32 as usize] != 0
}
/// Returns an iterator all scancodes with a boolean indicating if the scancode is pressed.
pub fn scancodes(&self) -> ScancodeIterator {
ScancodeIterator {
index: 0,
keyboard_state: self.keyboard_state
}
}
/// Returns an iterator of pressed scancodes.
///
/// # Example
/// ```no_run
/// use sdl2::keyboard::Keycode;
/// use sdl2::keyboard::Scancode;
/// use std::collections::HashSet;
///
/// fn pressed_scancode_set(e: &sdl2::EventPump) -> HashSet<Scancode> {
/// e.keyboard_state().pressed_scancodes().collect()
/// }
///
/// fn pressed_keycode_set(e: &sdl2::EventPump) -> HashSet<Keycode> {
/// e.keyboard_state().pressed_scancodes()
/// .filter_map(Keycode::from_scancode)
/// .collect()
/// }
///
/// fn newly_pressed(old: &HashSet<Scancode>, new: &HashSet<Scancode>) -> HashSet<Scancode> {
/// new - old
/// // sugar for: new.difference(old).collect()
/// }
/// ```
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()
}
}
}
/// Keyboard utility functions. Access with `Sdl::keyboard()`.
///
/// ```no_run
/// let sdl_context = sdl2::init().unwrap();
///
/// let focused = sdl_context.keyboard().focused_window_id().is_some();
/// ```
pub struct KeyboardUtil {
_sdldrop: ::std::rc::Rc<crate::SdlDrop>
}
impl KeyboardUtil {
/// Gets the id of the window which currently has keyboard focus.
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)
}
}
pub fn mod_state(&self) -> Mod {
unsafe { Mod::from_bits(sys::SDL_GetModState() as u16).unwrap() }
}
pub fn set_mod_state(&self, flags: Mod) {
unsafe { sys::SDL_SetModState(transmute::<u32, sys::SDL_Keymod>(flags.bits() as u32)); }
}
}
/// Text input utility functions. Access with `VideoSubsystem::text_input()`.
///
/// These functions require the video subsystem to be initialized and are not thread-safe.
///
/// ```no_run
/// let sdl_context = sdl2::init().unwrap();
/// let video_subsystem = sdl_context.video().unwrap();
///
/// // Start accepting text input events...
/// video_subsystem.text_input().start();
/// ```
pub struct TextInputUtil {
_subsystem: crate::VideoSubsystem
}
impl TextInputUtil {
pub fn start(&self) {
unsafe { sys::SDL_StartTextInput(); }
}
pub fn is_active(&self, ) -> bool {
unsafe { sys::SDL_IsTextInputActive() == sys::SDL_bool::SDL_TRUE }
}
pub fn stop(&self) {
unsafe { sys::SDL_StopTextInput(); }
}
pub fn set_rect(&self, rect: Rect) {
unsafe { sys::SDL_SetTextInputRect(rect.raw() as *mut sys::SDL_Rect); }
}
pub fn has_screen_keyboard_support(&self) -> bool {
unsafe { sys::SDL_HasScreenKeyboardSupport() == sys::SDL_bool::SDL_TRUE }
}
pub fn is_screen_keyboard_shown(&self, window: &Window) -> bool {
unsafe { sys::SDL_IsScreenKeyboardShown(window.raw()) == sys::SDL_bool::SDL_TRUE }
}
}