Skip to content

Commit

Permalink
Merge pull request #559 from Luke-Nukem/master
Browse files Browse the repository at this point in the history
For review: implementing `mouse_state()`
  • Loading branch information
AngryLawyer authored Nov 21, 2016
2 parents 6e9a00a + ed1ced7 commit b64d7a6
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 95 deletions.
Binary file added assets/animate.bmp
Binary file not shown.
55 changes: 55 additions & 0 deletions examples/animation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
extern crate sdl2;
use std::path::Path;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::rect::Rect;
use sdl2::rect::Point;
use std::time::Duration;

fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();

let window = video_subsystem.window("SDL2", 640, 480)
.position_centered().build().unwrap();

let mut renderer = window.renderer()
.accelerated().build().unwrap();

renderer.set_draw_color(sdl2::pixels::Color::RGBA(0,0,0,255));

let mut timer = sdl_context.timer().unwrap();

let mut event_pump = sdl_context.event_pump().unwrap();

let temp_surface = sdl2::surface::Surface::load_bmp(Path::new("assets/animate.bmp")).unwrap();
let texture = renderer.create_texture_from_surface(&temp_surface).unwrap();
let texture_query = texture.query();

let mut center = Point::new(320,240);
let mut source_rect = Rect::new(0, 0, 128, 82);
let mut dest_rect = Rect::new(0,0, 128, 82);
dest_rect.center_on(center);

let mut running = true;
while running {
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} | Event::KeyDown {keycode: Some(Keycode::Escape), ..} => {
running = false;
},
_ => {}
}
}

let ticks = timer.ticks();

source_rect.set_x((128 * ((ticks / 100) % 6) ) as i32);
renderer.clear();
renderer.copy_ex(&texture, Some(source_rect), Some(dest_rect), 10.0, None, true, false);
renderer.present();

std::thread::sleep(Duration::from_millis(100));
}
}
47 changes: 47 additions & 0 deletions examples/mouse-state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
extern crate sdl2;

use sdl2::event::Event;
use sdl2::mouse::MouseButton;
use std::collections::HashSet;
use std::time::Duration;

pub fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();

let _window = video_subsystem.window("Mouse", 800, 600)
.position_centered()
.build()
.unwrap();

let mut events = sdl_context.event_pump().unwrap();

let mut prev_buttons = HashSet::new();

'running: loop {
for event in events.poll_iter() {
match event {
Event::Quit {..} => break 'running,
_ => ()
}
}

// get a mouse state
let state = events.mouse_state();

// Create a set of pressed Keys.
let buttons = state.pressed_mouse_buttons().collect();

// Get the difference between the new and old sets.
let new_buttons = &buttons - &prev_buttons;
let old_buttons = &prev_buttons - &buttons;

if !new_buttons.is_empty() || !old_buttons.is_empty() {
println!("X = {:?}, Y = {:?} : {:?} -> {:?}", state.x(), state.y(), new_buttons, old_buttons);
}

prev_buttons = buttons;

std::thread::sleep(Duration::from_millis(100));
}
}
39 changes: 39 additions & 0 deletions examples/relative-mouse-state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extern crate sdl2;

use sdl2::event::Event;
use sdl2::mouse::MouseButton;
use std::time::Duration;

pub fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();

let _window = video_subsystem.window("Mouse", 800, 600)
.position_centered()
.build()
.unwrap();

let mut events = sdl_context.event_pump().unwrap();

let mut state = events.relative_mouse_state();
let mut old_x = 0;
let mut old_y = 0;

'running: loop {
for event in events.poll_iter() {
match event {
Event::Quit {..} => break 'running,
_ => ()
}
}

// get a mouse state using mouse_state() so as not to call
// relative_mouse_state() twice and get a false position reading
if events.mouse_state().is_mouse_button_pressed(MouseButton::Left) {
state = events.relative_mouse_state();
println!("Relative - X = {:?}, Y = {:?}", state.x(), state.y());
}

std::thread::sleep(Duration::from_millis(100));
}
}
22 changes: 11 additions & 11 deletions sdl2-sys/src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ pub const SDL_SYSTEM_CURSOR_HAND: SDL_SystemCursor = 11;
pub const SDL_NUM_SYSTEM_CURSORS: SDL_SystemCursor = 12;



pub const SDL_BUTTON_LEFT: u8 = 1;
pub const SDL_BUTTON_MIDDLE: u8 = 2;
pub const SDL_BUTTON_RIGHT: u8 = 3;
pub const SDL_BUTTON_X1: u8 = 4;
pub const SDL_BUTTON_X2: u8 = 5;
pub const SDL_BUTTON_LMASK: u32 = 0x01;
pub const SDL_BUTTON_MMASK: u32 = 0x02;
pub const SDL_BUTTON_RMASK: u32 = 0x04;
pub const SDL_BUTTON_X1MASK: u32 = 0x08;
pub const SDL_BUTTON_X2MASK: u32 = 0x10;
pub const SDL_BUTTON_UNKNOWN: u8 = 0;
pub const SDL_BUTTON_LEFT : u8 = 1;
pub const SDL_BUTTON_MIDDLE : u8 = 2;
pub const SDL_BUTTON_RIGHT : u8 = 3;
pub const SDL_BUTTON_X1 : u8 = 4;
pub const SDL_BUTTON_X2 : u8 = 5;
pub const SDL_BUTTON_LMASK : u32 = 0x01;
pub const SDL_BUTTON_MMASK : u32 = 0x02;
pub const SDL_BUTTON_RMASK : u32 = 0x04;
pub const SDL_BUTTON_X1MASK : u32 = 0x08;
pub const SDL_BUTTON_X2MASK : u32 = 0x10;


extern "C" {
Expand Down
47 changes: 31 additions & 16 deletions src/sdl2/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use keyboard::Mod;
use sys::keycode::SDL_Keymod;
use keyboard::Keycode;
use mouse;
use mouse::{Mouse, MouseState, MouseWheelDirection};
use mouse::{MouseButton, MouseState, MouseWheelDirection};
use keyboard::Scancode;
use get_error;

Expand Down Expand Up @@ -456,7 +456,7 @@ pub enum Event {
AppDidEnterForeground { timestamp: u32 },

Window {
timestamp: u32 ,
timestamp: u32,
window_id: u32,
win_event_id: WindowEventId,
data1: i32,
Expand All @@ -465,15 +465,15 @@ pub enum Event {
// TODO: SysWMEvent

KeyDown {
timestamp: u32 ,
timestamp: u32,
window_id: u32,
keycode: Option<Keycode>,
scancode: Option<Scancode>,
keymod: Mod,
repeat: bool
},
KeyUp {
timestamp: u32 ,
timestamp: u32,
window_id: u32,
keycode: Option<Keycode>,
scancode: Option<Scancode>,
Expand Down Expand Up @@ -510,15 +510,15 @@ pub enum Event {
timestamp: u32,
window_id: u32,
which: u32,
mouse_btn: Mouse,
mouse_btn: Option<MouseButton>,
x: i32,
y: i32
},
MouseButtonUp {
timestamp: u32,
window_id: u32,
which: u32,
mouse_btn: Mouse,
mouse_btn: Option<MouseButton>,
x: i32,
y: i32
},
Expand Down Expand Up @@ -755,6 +755,11 @@ fn mk_keysym(scancode: Option<Scancode>,
}
}

/// Helper function is only to unwrap a mouse_button to u8
fn mk_mouse_button(mouse_button: Option<MouseButton>) -> u8 {
mouse_button.unwrap().to_ll().unwrap()
}

// TODO: Remove this when from_utf8 is updated in Rust
// This would honestly be nice if it took &self instead of self,
// but Event::User's raw pointers kind of removes that possibility.
Expand Down Expand Up @@ -870,7 +875,7 @@ impl Event {
xrel,
yrel
} => {
let state = mousestate.to_flags();
let state = mousestate.to_sdl_state();
let event = ll::SDL_MouseMotionEvent {
type_: ll::SDL_MOUSEMOTION,
timestamp: timestamp,
Expand All @@ -896,7 +901,7 @@ impl Event {
x,
y
} => {
let button = mouse_btn.to_ll();
let button = mk_mouse_button(mouse_btn);
let event = ll::SDL_MouseButtonEvent {
type_: ll::SDL_MOUSEBUTTONDOWN,
timestamp: timestamp,
Expand All @@ -922,7 +927,7 @@ impl Event {
x,
y
} => {
let button = mouse_btn.to_ll();
let button = mk_mouse_button(mouse_btn);
let event = ll::SDL_MouseButtonEvent {
type_: ll::SDL_MOUSEBUTTONUP,
timestamp: timestamp,
Expand Down Expand Up @@ -1355,7 +1360,7 @@ impl Event {
timestamp: event.timestamp,
window_id: event.windowID,
which: event.which,
mousestate: mouse::MouseState::from_flags(event.state),
mousestate: mouse::MouseState::from_sdl_state(event.state),
x: event.x,
y: event.y,
xrel: event.xrel,
Expand All @@ -1369,7 +1374,7 @@ impl Event {
timestamp: event.timestamp,
window_id: event.windowID,
which: event.which,
mouse_btn: mouse::Mouse::from_ll(event.button),
mouse_btn: mouse::MouseButton::from_ll(event.button),
x: event.x,
y: event.y
}
Expand All @@ -1381,7 +1386,7 @@ impl Event {
timestamp: event.timestamp,
window_id: event.windowID,
which: event.which,
mouse_btn: mouse::Mouse::from_ll(event.button),
mouse_btn: mouse::MouseButton::from_ll(event.button),
x: event.x,
y: event.y
}
Expand Down Expand Up @@ -1784,6 +1789,16 @@ impl ::EventPump {
pub fn keyboard_state(&self) -> ::keyboard::KeyboardState {
::keyboard::KeyboardState::new(self)
}

#[inline]
pub fn mouse_state(&self) -> ::mouse::MouseState {
::mouse::MouseState::new(self)
}

#[inline]
pub fn relative_mouse_state(&self) -> ::mouse::RelativeMouseState {
::mouse::RelativeMouseState::new(self)
}
}

/// An iterator that calls `EventPump::poll_event()`.
Expand Down Expand Up @@ -1827,7 +1842,7 @@ mod test {
use super::WindowEventId;
use super::super::controller::{Button, Axis};
use super::super::joystick::{HatState};
use super::super::mouse::{Mouse, MouseState, MouseWheelDirection};
use super::super::mouse::{MouseButton, MouseState, MouseWheelDirection};
use super::super::keyboard::{Keycode, Scancode, Mod};

// Tests a round-trip conversion from an Event type to
Expand Down Expand Up @@ -1880,7 +1895,7 @@ mod test {
timestamp: 0,
window_id: 0,
which: 1,
mousestate: MouseState::from_flags(1),
mousestate: MouseState::from_sdl_state(1),
x: 3,
y: 91,
xrel: -1,
Expand All @@ -1894,7 +1909,7 @@ mod test {
timestamp: 5634,
window_id: 2,
which: 0,
mouse_btn: Mouse::Left,
mouse_btn: Some(MouseButton::Left),
x: 543,
y: 345,
};
Expand All @@ -1906,7 +1921,7 @@ mod test {
timestamp: 0,
window_id: 2,
which: 0,
mouse_btn: Mouse::Left,
mouse_btn: Some(MouseButton::Left),
x: 543,
y: 345,

Expand Down
Loading

0 comments on commit b64d7a6

Please sign in to comment.