Skip to content

Commit 6e078fe

Browse files
authored
Merge pull request #1214 from Sorenon/master
Implement controller touchpad events
2 parents 8e6c289 + f647d16 commit 6e078fe

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

examples/game-controller.rs

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ fn main() -> Result<(), String> {
8787
}
8888
Event::ControllerButtonDown { button, .. } => println!("Button {:?} down", button),
8989
Event::ControllerButtonUp { button, .. } => println!("Button {:?} up", button),
90+
Event::ControllerTouchpadDown { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} down finger:{finger} x:{x} y:{y}"),
91+
Event::ControllerTouchpadMotion { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} move finger:{finger} x:{x} y:{y}"),
92+
Event::ControllerTouchpadUp { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} up finger:{finger} x:{x} y:{y}"),
9093
Event::Quit { .. } => break,
9194
_ => (),
9295
}

src/sdl2/event.rs

+91
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ pub enum EventType {
300300
ControllerDeviceAdded = SDL_EventType::SDL_CONTROLLERDEVICEADDED as u32,
301301
ControllerDeviceRemoved = SDL_EventType::SDL_CONTROLLERDEVICEREMOVED as u32,
302302
ControllerDeviceRemapped = SDL_EventType::SDL_CONTROLLERDEVICEREMAPPED as u32,
303+
ControllerTouchpadDown = SDL_EventType::SDL_CONTROLLERTOUCHPADDOWN as u32,
304+
ControllerTouchpadMotion = SDL_EventType::SDL_CONTROLLERTOUCHPADMOTION as u32,
305+
ControllerTouchpadUp = SDL_EventType::SDL_CONTROLLERTOUCHPADUP as u32,
303306
#[cfg(feature = "hidapi")]
304307
ControllerSensorUpdated = SDL_EventType::SDL_CONTROLLERSENSORUPDATE as u32,
305308

@@ -371,6 +374,9 @@ impl TryFrom<u32> for EventType {
371374
SDL_CONTROLLERDEVICEADDED => ControllerDeviceAdded,
372375
SDL_CONTROLLERDEVICEREMOVED => ControllerDeviceRemoved,
373376
SDL_CONTROLLERDEVICEREMAPPED => ControllerDeviceRemapped,
377+
SDL_CONTROLLERTOUCHPADDOWN => ControllerTouchpadDown,
378+
SDL_CONTROLLERTOUCHPADMOTION => ControllerTouchpadMotion,
379+
SDL_CONTROLLERTOUCHPADUP => ControllerTouchpadUp,
374380
#[cfg(feature = "hidapi")]
375381
SDL_CONTROLLERSENSORUPDATE => ControllerSensorUpdated,
376382

@@ -746,6 +752,52 @@ pub enum Event {
746752
which: u32,
747753
},
748754

755+
ControllerTouchpadDown {
756+
timestamp: u32,
757+
/// The joystick instance id
758+
which: u32,
759+
/// The index of the touchpad
760+
touchpad: u32,
761+
/// The index of the finger on the touchpad
762+
finger: u32,
763+
/// Normalized in the range 0...1 with 0 being on the left
764+
x: f32,
765+
/// Normalized in the range 0...1 with 0 being at the top
766+
y: f32,
767+
/// Normalized in the range 0...1
768+
pressure: f32,
769+
},
770+
ControllerTouchpadMotion {
771+
timestamp: u32,
772+
/// The joystick instance id
773+
which: u32,
774+
/// The index of the touchpad
775+
touchpad: u32,
776+
/// The index of the finger on the touchpad
777+
finger: u32,
778+
/// Normalized in the range 0...1 with 0 being on the left
779+
x: f32,
780+
/// Normalized in the range 0...1 with 0 being at the top
781+
y: f32,
782+
/// Normalized in the range 0...1
783+
pressure: f32,
784+
},
785+
ControllerTouchpadUp {
786+
timestamp: u32,
787+
/// The joystick instance id
788+
which: u32,
789+
/// The index of the touchpad
790+
touchpad: u32,
791+
/// The index of the finger on the touchpad
792+
finger: u32,
793+
/// Normalized in the range 0...1 with 0 being on the left
794+
x: f32,
795+
/// Normalized in the range 0...1 with 0 being at the top
796+
y: f32,
797+
/// Normalized in the range 0...1
798+
pressure: f32,
799+
},
800+
749801
/// Triggered when the gyroscope or accelerometer is updated
750802
#[cfg(feature = "hidapi")]
751803
ControllerSensorUpdated {
@@ -1728,6 +1780,42 @@ impl Event {
17281780
which: event.which as u32,
17291781
}
17301782
}
1783+
EventType::ControllerTouchpadDown => {
1784+
let event = raw.ctouchpad;
1785+
Event::ControllerTouchpadDown {
1786+
timestamp: event.timestamp,
1787+
which: event.which as u32,
1788+
touchpad: event.touchpad as u32,
1789+
finger: event.finger as u32,
1790+
x: event.x,
1791+
y: event.y,
1792+
pressure: event.pressure,
1793+
}
1794+
}
1795+
EventType::ControllerTouchpadMotion => {
1796+
let event = raw.ctouchpad;
1797+
Event::ControllerTouchpadMotion {
1798+
timestamp: event.timestamp,
1799+
which: event.which as u32,
1800+
touchpad: event.touchpad as u32,
1801+
finger: event.finger as u32,
1802+
x: event.x,
1803+
y: event.y,
1804+
pressure: event.pressure,
1805+
}
1806+
}
1807+
EventType::ControllerTouchpadUp => {
1808+
let event = raw.ctouchpad;
1809+
Event::ControllerTouchpadUp {
1810+
timestamp: event.timestamp,
1811+
which: event.which as u32,
1812+
touchpad: event.touchpad as u32,
1813+
finger: event.finger as u32,
1814+
x: event.x,
1815+
y: event.y,
1816+
pressure: event.pressure,
1817+
}
1818+
}
17311819
#[cfg(feature = "hidapi")]
17321820
EventType::ControllerSensorUpdated => {
17331821
let event = raw.csensor;
@@ -2077,6 +2165,9 @@ impl Event {
20772165
Self::ControllerDeviceAdded { timestamp, .. } => timestamp,
20782166
Self::ControllerDeviceRemoved { timestamp, .. } => timestamp,
20792167
Self::ControllerDeviceRemapped { timestamp, .. } => timestamp,
2168+
Self::ControllerTouchpadDown { timestamp, .. } => timestamp,
2169+
Self::ControllerTouchpadMotion { timestamp, .. } => timestamp,
2170+
Self::ControllerTouchpadUp { timestamp, .. } => timestamp,
20802171
#[cfg(feature = "hidapi")]
20812172
Self::ControllerSensorUpdated { timestamp, .. } => timestamp,
20822173
Self::FingerDown { timestamp, .. } => timestamp,

0 commit comments

Comments
 (0)