mirror of
https://github.com/encounter/rust-sdl2.git
synced 2026-07-10 21:18:41 -07:00
Merge pull request #556 from Xaeroxe23/issue-555
Add direction field to MouseWheel event
This commit is contained in:
@@ -158,6 +158,7 @@ pub struct SDL_MouseWheelEvent {
|
||||
pub which: uint32_t,
|
||||
pub x: int32_t,
|
||||
pub y: int32_t,
|
||||
pub direction: uint32_t,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
|
||||
+27
-22
@@ -23,7 +23,7 @@ use keyboard::Mod;
|
||||
use sys::keycode::SDL_Keymod;
|
||||
use keyboard::Keycode;
|
||||
use mouse;
|
||||
use mouse::{Mouse, MouseState};
|
||||
use mouse::{Mouse, MouseState, MouseWheelDirection};
|
||||
use keyboard::Scancode;
|
||||
use get_error;
|
||||
|
||||
@@ -167,7 +167,7 @@ impl ::EventSubsystem {
|
||||
const ERR_NR:u32 = ::std::u32::MAX - 1;
|
||||
|
||||
match result {
|
||||
ERR_NR => {
|
||||
ERR_NR => {
|
||||
Err("No more user events can be created; SDL_LASTEVENT reached"
|
||||
.to_owned())
|
||||
},
|
||||
@@ -240,14 +240,14 @@ impl ::EventSubsystem {
|
||||
|
||||
let user_event_id = *match cet.type_id_to_sdl_id.get(&type_id) {
|
||||
Some(id) => id,
|
||||
None => {
|
||||
None => {
|
||||
return Err(
|
||||
"Type is not registered as a custom event type!".to_owned()
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let event_box = Box::new(event);
|
||||
|
||||
let event_box = Box::new(event);
|
||||
let event = Event::User {
|
||||
timestamp: 0,
|
||||
window_id: 0,
|
||||
@@ -528,7 +528,8 @@ pub enum Event {
|
||||
window_id: u32,
|
||||
which: u32,
|
||||
x: i32,
|
||||
y: i32
|
||||
y: i32,
|
||||
direction: MouseWheelDirection,
|
||||
},
|
||||
|
||||
JoyAxisMotion {
|
||||
@@ -945,7 +946,8 @@ impl Event {
|
||||
window_id,
|
||||
which,
|
||||
x,
|
||||
y
|
||||
y,
|
||||
direction,
|
||||
} => {
|
||||
let event = ll::SDL_MouseWheelEvent {
|
||||
type_: ll::SDL_MOUSEWHEEL,
|
||||
@@ -954,6 +956,7 @@ impl Event {
|
||||
which: which,
|
||||
x: x,
|
||||
y: y,
|
||||
direction : direction.to_ll(),
|
||||
};
|
||||
unsafe {
|
||||
ptr::copy(&event, &mut ret as *mut ll::SDL_Event as *mut ll::SDL_MouseWheelEvent, 1);
|
||||
@@ -1006,7 +1009,7 @@ impl Event {
|
||||
}
|
||||
Some(ret)
|
||||
|
||||
},
|
||||
},
|
||||
Event::JoyHatMotion{
|
||||
timestamp,
|
||||
which,
|
||||
@@ -1216,17 +1219,17 @@ impl Event {
|
||||
|
||||
},
|
||||
|
||||
|
||||
Event::FingerDown{..} |
|
||||
Event::FingerUp{..} |
|
||||
Event::FingerMotion{..} |
|
||||
Event::DollarGesture{..} |
|
||||
Event::DollarRecord{..} |
|
||||
Event::MultiGesture{..} |
|
||||
Event::ClipboardUpdate{..} |
|
||||
|
||||
Event::FingerDown{..} |
|
||||
Event::FingerUp{..} |
|
||||
Event::FingerMotion{..} |
|
||||
Event::DollarGesture{..} |
|
||||
Event::DollarRecord{..} |
|
||||
Event::MultiGesture{..} |
|
||||
Event::ClipboardUpdate{..} |
|
||||
Event::DropFile{..} |
|
||||
Event::TextEditing{..} |
|
||||
Event::TextInput{..} |
|
||||
Event::TextEditing{..} |
|
||||
Event::TextInput{..} |
|
||||
Event::Unknown{..} |
|
||||
_ => {
|
||||
// don't know how to convert!
|
||||
@@ -1391,7 +1394,8 @@ impl Event {
|
||||
window_id: event.windowID,
|
||||
which: event.which,
|
||||
x: event.x,
|
||||
y: event.y
|
||||
y: event.y,
|
||||
direction: mouse::MouseWheelDirection::from_ll(event.direction),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1823,7 +1827,7 @@ mod test {
|
||||
use super::WindowEventId;
|
||||
use super::super::controller::{Button, Axis};
|
||||
use super::super::joystick::{HatState};
|
||||
use super::super::mouse::{Mouse, MouseState};
|
||||
use super::super::mouse::{Mouse, MouseState, MouseWheelDirection};
|
||||
use super::super::keyboard::{Keycode, Scancode, Mod};
|
||||
|
||||
// Tests a round-trip conversion from an Event type to
|
||||
@@ -1916,7 +1920,8 @@ mod test {
|
||||
window_id: 0,
|
||||
which: 32,
|
||||
x: 23,
|
||||
y: 91
|
||||
y: 91,
|
||||
direction: MouseWheelDirection::Flipped,
|
||||
};
|
||||
let e2 = Event::from_ll(e.clone().to_ll().unwrap());
|
||||
assert_eq!(e, e2);
|
||||
@@ -2038,6 +2043,6 @@ mod test {
|
||||
let e2 = Event::from_ll(e.clone().to_ll().unwrap());
|
||||
assert_eq!(e, e2);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +80,49 @@ impl Cursor {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
||||
pub enum MouseWheelDirection {
|
||||
Normal,
|
||||
Flipped,
|
||||
Unknown(u32),
|
||||
}
|
||||
|
||||
// 0 and 1 are not fixed values in the SDL source code. This value is defined as an enum which is then cast to a Uint32.
|
||||
// The enum in C is defined as such:
|
||||
|
||||
/**
|
||||
* \brief Scroll direction types for the Scroll event
|
||||
*/
|
||||
//typedef enum
|
||||
//{
|
||||
// SDL_MOUSEWHEEL_NORMAL, /**< The scroll direction is normal */
|
||||
// SDL_MOUSEWHEEL_FLIPPED /**< The scroll direction is flipped / natural */
|
||||
//} SDL_MouseWheelDirection;
|
||||
|
||||
// Since no value is given in the enum definition these values are auto assigned by the C compiler starting at 0.
|
||||
// Normally I would prefer to use the enum rather than hard code what it is implied to represent however
|
||||
// the mouse wheel direction value could be described equally as well by a bool, so I don't think changes
|
||||
// to this enum in the C source code are going to be a problem.
|
||||
|
||||
impl MouseWheelDirection {
|
||||
#[inline]
|
||||
pub fn from_ll(direction: u32) -> MouseWheelDirection {
|
||||
match direction {
|
||||
0 => MouseWheelDirection::Normal,
|
||||
1 => MouseWheelDirection::Flipped,
|
||||
_ => MouseWheelDirection::Unknown(direction),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn to_ll(&self) -> u32 {
|
||||
match *self {
|
||||
MouseWheelDirection::Normal => 0,
|
||||
MouseWheelDirection::Flipped => 1,
|
||||
MouseWheelDirection::Unknown(direction) => direction,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
||||
pub enum Mouse {
|
||||
Left,
|
||||
|
||||
Reference in New Issue
Block a user