mirror of
https://github.com/encounter/rust-sdl2.git
synced 2026-07-10 21:18:41 -07:00
codegen: use #[deriving(FromPrimitive)].
This commit is contained in:
+1
-25
@@ -311,7 +311,7 @@ pub fn generate(output_dir: &Path) -> IoResult<()> {
|
||||
|
||||
use std::hash::{mod, Hash};
|
||||
|
||||
#[deriving(PartialEq, Eq, Show)]
|
||||
#[deriving(PartialEq, Eq, FromPrimitive, Show)]
|
||||
pub enum KeyCode {
|
||||
".as_bytes()));
|
||||
for &entry in entries.iter() {
|
||||
@@ -340,31 +340,7 @@ impl ToPrimitive for KeyCode {".as_bytes()));
|
||||
|
||||
try!(out.write("
|
||||
}
|
||||
|
||||
impl FromPrimitive for KeyCode {
|
||||
|
||||
/// Get a *registered* key code.
|
||||
///
|
||||
/// This will return UnknownKey if an unknown code is passed.
|
||||
///
|
||||
/// For example, `from_int(13)` will return `ReturnKey`.
|
||||
".as_bytes()));
|
||||
for primitive_type in types.iter() {
|
||||
try!(out.write(format!("
|
||||
fn from_{}(n: {}) -> Option<KeyCode> {{
|
||||
match n {{
|
||||
", *primitive_type, *primitive_type).container_as_bytes()));
|
||||
for &entry in entries.iter() {
|
||||
try!(out.write(format!(" {} => Some({}),\n", entry.code, entry.ident()).container_as_bytes()));
|
||||
}
|
||||
try!(out.write("
|
||||
_ => { Some(UnknownKey) }
|
||||
}
|
||||
}\n".as_bytes()));
|
||||
}
|
||||
|
||||
try!(out.write("
|
||||
}".as_bytes()));
|
||||
try!(out.flush());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+10
-6
@@ -15,10 +15,10 @@ use joystick::HatState;
|
||||
use keyboard;
|
||||
use keyboard::Mod;
|
||||
use keyboard::ll::SDL_Keymod;
|
||||
use keycode::KeyCode;
|
||||
use keycode::{KeyCode, UnknownKey};
|
||||
use mouse;
|
||||
use mouse::{Mouse, MouseState};
|
||||
use scancode::ScanCode;
|
||||
use scancode::{ScanCode, UnknownScanCode};
|
||||
use video;
|
||||
use get_error;
|
||||
use SdlResult;
|
||||
@@ -760,8 +760,10 @@ impl Event {
|
||||
};
|
||||
|
||||
KeyDownEvent(event.timestamp as uint, window,
|
||||
FromPrimitive::from_int(event.keysym.sym as int).unwrap(),
|
||||
FromPrimitive::from_int(event.keysym.scancode as int).unwrap(),
|
||||
FromPrimitive::from_int(event.keysym.sym as int)
|
||||
.unwrap_or(UnknownKey),
|
||||
FromPrimitive::from_int(event.keysym.scancode as int)
|
||||
.unwrap_or(UnknownScanCode),
|
||||
keyboard::Mod::from_bits(event.keysym._mod as SDL_Keymod).unwrap())
|
||||
}
|
||||
KeyUpEventType => {
|
||||
@@ -774,8 +776,10 @@ impl Event {
|
||||
};
|
||||
|
||||
KeyUpEvent(event.timestamp as uint, window,
|
||||
FromPrimitive::from_int(event.keysym.sym as int).unwrap(),
|
||||
FromPrimitive::from_int(event.keysym.scancode as int).unwrap(),
|
||||
FromPrimitive::from_int(event.keysym.sym as int)
|
||||
.unwrap_or(UnknownKey),
|
||||
FromPrimitive::from_int(event.keysym.scancode as int)
|
||||
.unwrap_or(UnknownScanCode),
|
||||
keyboard::Mod::from_bits(event.keysym._mod as SDL_Keymod).unwrap())
|
||||
}
|
||||
TextEditingEventType => {
|
||||
|
||||
+12
-7
@@ -4,9 +4,9 @@ use std::ptr;
|
||||
use std::string;
|
||||
use std::vec;
|
||||
|
||||
use keycode::KeyCode;
|
||||
use keycode::{KeyCode, UnknownKey};
|
||||
use rect::Rect;
|
||||
use scancode::ScanCode;
|
||||
use scancode::{ScanCode, UnknownScanCode};
|
||||
use video::Window;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
@@ -86,7 +86,8 @@ pub fn get_keyboard_state() -> HashMap<ScanCode, bool> {
|
||||
|
||||
let mut current = 0;
|
||||
while current < raw.len() {
|
||||
state.insert(FromPrimitive::from_int(current as int).unwrap(),
|
||||
state.insert(FromPrimitive::from_int(current as int)
|
||||
.unwrap_or(UnknownScanCode),
|
||||
raw[current] == 1);
|
||||
current += 1;
|
||||
}
|
||||
@@ -104,13 +105,15 @@ pub fn set_mod_state(flags: Mod) {
|
||||
|
||||
pub fn get_key_from_scancode(scancode: ScanCode) -> KeyCode {
|
||||
unsafe {
|
||||
FromPrimitive::from_int(ll::SDL_GetKeyFromScancode(scancode as u32) as int).unwrap()
|
||||
FromPrimitive::from_int(ll::SDL_GetKeyFromScancode(scancode as u32) as int)
|
||||
.unwrap_or(UnknownKey)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_scancode_from_key(key: KeyCode) -> ScanCode {
|
||||
unsafe {
|
||||
FromPrimitive::from_int(ll::SDL_GetScancodeFromKey(key as i32) as int).unwrap()
|
||||
FromPrimitive::from_int(ll::SDL_GetScancodeFromKey(key as i32) as int)
|
||||
.unwrap_or(UnknownScanCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +127,8 @@ pub fn get_scancode_name(scancode: ScanCode) -> String {
|
||||
pub fn get_scancode_from_name(name: &str) -> ScanCode {
|
||||
unsafe {
|
||||
name.with_c_str(|name| {
|
||||
FromPrimitive::from_int(ll::SDL_GetScancodeFromName(name) as int).unwrap()
|
||||
FromPrimitive::from_int(ll::SDL_GetScancodeFromName(name) as int)
|
||||
.unwrap_or(UnknownScanCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -139,7 +143,8 @@ pub fn get_key_name(key: KeyCode) -> String {
|
||||
pub fn get_key_from_name(name: &str) -> KeyCode {
|
||||
unsafe {
|
||||
name.with_c_str(|name| {
|
||||
FromPrimitive::from_int(ll::SDL_GetKeyFromName(name) as int).unwrap()
|
||||
FromPrimitive::from_int(ll::SDL_GetKeyFromName(name) as int)
|
||||
.unwrap_or(UnknownKey)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user