From 5fd0af58f2b508c13b2f10ce12807eadf548f080 Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Sun, 15 Sep 2013 17:02:20 -0500 Subject: [PATCH] Add touch API --- src/touch.rs | 57 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/src/touch.rs b/src/touch.rs index 8448918e..15a43e3a 100644 --- a/src/touch.rs +++ b/src/touch.rs @@ -1,19 +1,48 @@ +use std::ptr; + +pub type TouchDevice = ll::SDL_TouchID; + +#[deriving(Eq)] +pub struct Finger { + id: TouchDevice, + x: f32, + y: f32, + pressure: f32, +} pub mod ll { - use std::libc::{c_float, c_int, int64_t}; + use std::libc::{c_int, int64_t}; + use touch::Finger; - pub type SDL_TouchID = int64_t; - pub type SDL_FingerID = int64_t; + pub type SDL_TouchID = int64_t; + pub type SDL_FingerID = int64_t; + pub type SDL_Finger = Finger; - pub struct SDL_Finger { - id: SDL_FingerID, - x: c_float, - y: c_float, - pressure: c_float, - } - - externfn!(fn SDL_GetNumTouchDevices() -> c_int) - externfn!(fn SDL_GetTouchDevice(index: c_int) -> SDL_TouchID) - externfn!(fn SDL_GetNumTouchFingers(touchID: SDL_TouchID) -> c_int) - externfn!(fn SDL_GetTouchFinger(touchID: SDL_TouchID, index: c_int)) + externfn!(fn SDL_GetNumTouchDevices() -> c_int) + externfn!(fn SDL_GetTouchDevice(index: c_int) -> SDL_TouchID) + externfn!(fn SDL_GetNumTouchFingers(touchID: SDL_TouchID) -> c_int) + externfn!(fn SDL_GetTouchFinger(touchID: SDL_TouchID, index: c_int) -> + *SDL_Finger) +} + +pub fn get_num_touch_devices() -> int { + unsafe { ll::SDL_GetNumTouchDevices() as int } +} + +pub fn get_touch_device(index: int) -> TouchDevice { + unsafe { ll::SDL_GetTouchDevice(index as i32) } +} + +pub fn get_num_touch_fingers(touch: TouchDevice) -> int { + unsafe { ll::SDL_GetNumTouchFingers(touch) as int } +} + +pub fn get_touch_finger(touch: TouchDevice, index: int) -> Option { + let raw = unsafe { ll::SDL_GetTouchFinger(touch, index as i32) }; + + if raw == ptr::null() { + None + } else { + unsafe { Some(*raw) } + } }