From 2af5dcfac43e31cc641d2b8bb05f975ef461262c Mon Sep 17 00:00:00 2001 From: Dan Spencer Date: Sun, 14 Jun 2015 01:02:34 -0600 Subject: [PATCH] Move Keycode and Scancode into the keyboard module, adjust spelling * The `keycode` and `scancode` modules only contained a single enum each, so it makes more sense to put them into the `keyboard` module. * KeyCode -> Keycode, ScanCode -> Scancode. A small detail, but this is consistent with SDL_Keycode and SDL_Scancode. --- examples/demo.rs | 4 +-- examples/keyboard-state.rs | 4 +-- examples/renderer-texture.rs | 4 +-- examples/renderer-yuv.rs | 4 +-- examples/window-properties.rs | 4 +-- src/sdl2/event.rs | 12 ++++----- src/sdl2/{ => keyboard}/keycode.rs | 22 ++++++++-------- src/sdl2/{keyboard.rs => keyboard/mod.rs} | 32 +++++++++++++---------- src/sdl2/{ => keyboard}/scancode.rs | 22 ++++++++-------- src/sdl2/lib.rs | 3 --- 10 files changed, 56 insertions(+), 55 deletions(-) rename src/sdl2/{ => keyboard}/keycode.rs (98%) rename src/sdl2/{keyboard.rs => keyboard/mod.rs} (86%) rename src/sdl2/{ => keyboard}/scancode.rs (98%) diff --git a/examples/demo.rs b/examples/demo.rs index 0c36a930..9cf9a2bf 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -1,7 +1,7 @@ extern crate sdl2; use sdl2::pixels::Color; -use sdl2::keycode::KeyCode; +use sdl2::keyboard::Keycode; pub fn main() { let mut sdl_context = sdl2::init().video().unwrap(); @@ -26,7 +26,7 @@ pub fn main() { use sdl2::event::Event; match event { - Event::Quit {..} | Event::KeyDown { keycode: Some(KeyCode::Escape), .. } => { + Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { running = false }, _ => {} diff --git a/examples/keyboard-state.rs b/examples/keyboard-state.rs index f260e565..0451bdda 100644 --- a/examples/keyboard-state.rs +++ b/examples/keyboard-state.rs @@ -1,6 +1,6 @@ extern crate sdl2; -use sdl2::keycode::KeyCode; +use sdl2::keyboard::Keycode; use std::collections::HashSet; pub fn main() { @@ -26,7 +26,7 @@ pub fn main() { } // Create a set of pressed Keys. - let keys = sdl_context.keyboard_state().pressed_scancodes().filter_map(KeyCode::from_scancode).collect(); + let keys = sdl_context.keyboard_state().pressed_scancodes().filter_map(Keycode::from_scancode).collect(); // Get the difference between the new and old sets. let new_keys = &keys - &prev_keys; diff --git a/examples/renderer-texture.rs b/examples/renderer-texture.rs index d6807920..381f7b51 100644 --- a/examples/renderer-texture.rs +++ b/examples/renderer-texture.rs @@ -2,7 +2,7 @@ extern crate sdl2; use sdl2::pixels::PixelFormatEnum; use sdl2::rect::Rect; -use sdl2::keycode::KeyCode; +use sdl2::keyboard::Keycode; pub fn main() { let mut sdl_context = sdl2::init().video().unwrap(); @@ -41,7 +41,7 @@ pub fn main() { use sdl2::event::Event; match event { - Event::Quit {..} | Event::KeyDown { keycode: Some(KeyCode::Escape), .. } => { + Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { running = false }, _ => {} diff --git a/examples/renderer-yuv.rs b/examples/renderer-yuv.rs index 34e26381..57ce938f 100644 --- a/examples/renderer-yuv.rs +++ b/examples/renderer-yuv.rs @@ -2,7 +2,7 @@ extern crate sdl2; use sdl2::pixels::PixelFormatEnum; use sdl2::rect::Rect; -use sdl2::keycode::KeyCode; +use sdl2::keyboard::Keycode; pub fn main() { let mut sdl_context = sdl2::init().video().unwrap(); @@ -57,7 +57,7 @@ pub fn main() { use sdl2::event::Event; match event { - Event::Quit {..} | Event::KeyDown { keycode: Some(KeyCode::Escape), .. } => { + Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { running = false }, _ => {} diff --git a/examples/window-properties.rs b/examples/window-properties.rs index 8ed22a33..93549eff 100644 --- a/examples/window-properties.rs +++ b/examples/window-properties.rs @@ -18,10 +18,10 @@ pub fn main() { while running { for event in sdl_context.event_pump().poll_iter() { use sdl2::event::Event; - use sdl2::keycode::KeyCode; + use sdl2::keyboard::Keycode; match event { - Event::Quit {..} | Event::KeyDown { keycode: Some(KeyCode::Escape), .. } => { + Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { running = false }, _ => {} diff --git a/src/sdl2/event.rs b/src/sdl2/event.rs index 4a37f946..286b48a3 100644 --- a/src/sdl2/event.rs +++ b/src/sdl2/event.rs @@ -18,10 +18,10 @@ use joystick::HatState; use keyboard; use keyboard::Mod; use sys::keycode::SDL_Keymod; -use keycode::KeyCode; +use keyboard::Keycode; use mouse; use mouse::{Mouse, MouseState}; -use scancode::ScanCode; +use keyboard::Scancode; use get_error; use SdlResult; use Sdl; @@ -211,16 +211,16 @@ pub enum Event { KeyDown { timestamp: u32 , window_id: u32, - keycode: Option, - scancode: Option, + keycode: Option, + scancode: Option, keymod: Mod, repeat: bool }, KeyUp { timestamp: u32 , window_id: u32, - keycode: Option, - scancode: Option, + keycode: Option, + scancode: Option, keymod: Mod, repeat: bool }, diff --git a/src/sdl2/keycode.rs b/src/sdl2/keyboard/keycode.rs similarity index 98% rename from src/sdl2/keycode.rs rename to src/sdl2/keyboard/keycode.rs index a6efaf53..f64c448d 100644 --- a/src/sdl2/keycode.rs +++ b/src/sdl2/keyboard/keycode.rs @@ -4,7 +4,7 @@ use std::ffi::{CString, CStr}; use sys::keycode as ll; #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] -pub enum KeyCode { +pub enum Keycode { Backspace = ll::SDLK_BACKSPACE as isize, Tab = ll::SDLK_TAB as isize, Return = ll::SDLK_RETURN as isize, @@ -242,7 +242,7 @@ pub enum KeyCode { Sleep = ll::SDLK_SLEEP as isize, } -impl ToPrimitive for KeyCode { +impl ToPrimitive for Keycode { #[inline] fn to_i64(&self) -> Option { Some(*self as i64) @@ -259,9 +259,9 @@ impl ToPrimitive for KeyCode { } } -impl FromPrimitive for KeyCode { - fn from_i64(n: i64) -> Option { - use self::KeyCode::*; +impl FromPrimitive for Keycode { + fn from_i64(n: i64) -> Option { + use self::Keycode::*; Some( match n as ll::SDL_Keycode { ll::SDLK_UNKNOWN => return None, @@ -504,24 +504,24 @@ impl FromPrimitive for KeyCode { }) } - fn from_u64(n: u64) -> Option { + fn from_u64(n: u64) -> Option { FromPrimitive::from_i64(n as i64) } } use std::fmt; -impl fmt::Display for KeyCode { +impl fmt::Display for Keycode { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{}", self.name()) } } -use scancode::ScanCode; +use keyboard::Scancode; -impl KeyCode { +impl Keycode { /// Gets the virtual key from a scancode. Returns None if there is no corresponding virtual key. - pub fn from_scancode(scancode: ScanCode) -> Option { + pub fn from_scancode(scancode: Scancode) -> Option { unsafe { match ::sys::keyboard::SDL_GetKeyFromScancode(scancode as u32) { ll::SDLK_UNKNOWN => None, @@ -530,7 +530,7 @@ impl KeyCode { } } - pub fn from_name(name: &str) -> Option { + pub fn from_name(name: &str) -> Option { unsafe { match CString::new(name) { Ok(name) => match ::sys::keyboard::SDL_GetKeyFromName(name.as_ptr()) { diff --git a/src/sdl2/keyboard.rs b/src/sdl2/keyboard/mod.rs similarity index 86% rename from src/sdl2/keyboard.rs rename to src/sdl2/keyboard/mod.rs index bd1c8df8..5131a74f 100644 --- a/src/sdl2/keyboard.rs +++ b/src/sdl2/keyboard/mod.rs @@ -3,11 +3,15 @@ use std::ptr; use Sdl; use rect::Rect; -use scancode::ScanCode; use video::Window; use sys::keyboard as ll; +mod keycode; +mod scancode; +pub use self::keycode::Keycode; +pub use self::scancode::Scancode; + bitflags! { flags Mod: u32 { const NOMOD = 0x0000, @@ -57,13 +61,13 @@ impl<'sdl> KeyboardState<'sdl> { /// /// # Example /// ```no_run - /// use sdl2::scancode::ScanCode; + /// use sdl2::keyboard::Scancode; /// /// fn is_a_pressed(sdl_context: &mut sdl2::Sdl) -> bool { - /// sdl_context.keyboard_state().is_scancode_pressed(ScanCode::A) + /// sdl_context.keyboard_state().is_scancode_pressed(Scancode::A) /// } /// ``` - pub fn is_scancode_pressed(&self, scancode: ScanCode) -> bool { + pub fn is_scancode_pressed(&self, scancode: Scancode) -> bool { self.keyboard_state[ToPrimitive::to_isize(&scancode).unwrap() as usize] != 0 } @@ -79,21 +83,21 @@ impl<'sdl> KeyboardState<'sdl> { /// /// # Example /// ```no_run - /// use sdl2::keycode::KeyCode; - /// use sdl2::scancode::ScanCode; + /// use sdl2::keyboard::Keycode; + /// use sdl2::keyboard::Scancode; /// use std::collections::HashSet; /// - /// fn pressed_scancode_set(sdl_context: &sdl2::Sdl) -> HashSet { + /// fn pressed_scancode_set(sdl_context: &sdl2::Sdl) -> HashSet { /// sdl_context.keyboard_state().pressed_scancodes().collect() /// } /// - /// fn pressed_keycode_set(sdl_context: &sdl2::Sdl) -> HashSet { + /// fn pressed_keycode_set(sdl_context: &sdl2::Sdl) -> HashSet { /// sdl_context.keyboard_state().pressed_scancodes() - /// .filter_map(KeyCode::from_scancode) + /// .filter_map(Keycode::from_scancode) /// .collect() /// } /// - /// fn newly_pressed(old: &HashSet, new: &HashSet) -> HashSet { + /// fn newly_pressed(old: &HashSet, new: &HashSet) -> HashSet { /// new - old /// // sugar for: new.difference(old).collect() /// } @@ -111,9 +115,9 @@ pub struct ScancodeIterator<'a> { } impl<'a> Iterator for ScancodeIterator<'a> { - type Item = (ScanCode, bool); + type Item = (Scancode, bool); - fn next(&mut self) -> Option<(ScanCode, bool)> { + fn next(&mut self) -> Option<(Scancode, bool)> { if self.index < self.keyboard_state.len() { let index = self.index; self.index += 1; @@ -136,9 +140,9 @@ pub struct PressedScancodeIterator<'a> { } impl<'a> Iterator for PressedScancodeIterator<'a> { - type Item = ScanCode; + type Item = Scancode; - fn next(&mut self) -> Option { + fn next(&mut self) -> Option { while let Some((scancode, pressed)) = self.iter.next() { if pressed { return Some(scancode) } } diff --git a/src/sdl2/scancode.rs b/src/sdl2/keyboard/scancode.rs similarity index 98% rename from src/sdl2/scancode.rs rename to src/sdl2/keyboard/scancode.rs index 63cfb6d3..5071254e 100644 --- a/src/sdl2/scancode.rs +++ b/src/sdl2/keyboard/scancode.rs @@ -4,7 +4,7 @@ use std::ffi::{CString, CStr}; use sys::scancode as ll; #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] -pub enum ScanCode { +pub enum Scancode { A = ll::SDL_SCANCODE_A as isize, B = ll::SDL_SCANCODE_B as isize, C = ll::SDL_SCANCODE_C as isize, @@ -248,7 +248,7 @@ pub enum ScanCode { Num = ll::SDL_NUM_SCANCODES as isize, } -impl ToPrimitive for ScanCode { +impl ToPrimitive for Scancode { #[inline] fn to_i64(&self) -> Option { Some(*self as i64) @@ -265,9 +265,9 @@ impl ToPrimitive for ScanCode { } } -impl FromPrimitive for ScanCode { - fn from_i64(n: i64) -> Option { - use self::ScanCode::*; +impl FromPrimitive for Scancode { + fn from_i64(n: i64) -> Option { + use self::Scancode::*; Some( match n as ll::SDL_Scancode { ll::SDL_SCANCODE_UNKNOWN => return None, @@ -516,22 +516,22 @@ impl FromPrimitive for ScanCode { }) } - fn from_u64(n: u64) -> Option { FromPrimitive::from_i64(n as i64) } + fn from_u64(n: u64) -> Option { FromPrimitive::from_i64(n as i64) } } use std::fmt; -impl fmt::Display for ScanCode { +impl fmt::Display for Scancode { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{}", self.name()) } } -use keycode::KeyCode; +use keyboard::Keycode; -impl ScanCode { +impl Scancode { /// Gets the scancode from a virtual key. Returns None if there is no corresponding scancode. - pub fn from_keycode(keycode: KeyCode) -> Option { + pub fn from_keycode(keycode: Keycode) -> Option { unsafe { match ::sys::keyboard::SDL_GetScancodeFromKey(keycode as i32) { ll::SDL_SCANCODE_UNKNOWN => None, @@ -540,7 +540,7 @@ impl ScanCode { } } - pub fn from_name(name: &str) -> Option { + pub fn from_name(name: &str) -> Option { unsafe { match CString::new(name) { Ok(name) => match ::sys::keyboard::SDL_GetScancodeFromName(name.as_ptr()) { diff --git a/src/sdl2/lib.rs b/src/sdl2/lib.rs index 25938fd4..aab91d20 100644 --- a/src/sdl2/lib.rs +++ b/src/sdl2/lib.rs @@ -9,9 +9,6 @@ extern crate sdl2_sys as sys; pub use sdl::*; -pub mod keycode; -pub mod scancode; - pub mod clipboard; pub mod cpuinfo; #[macro_use] pub mod macros;