Implement new events system

This commit is contained in:
Tomaka17 2014-08-13 17:04:57 +02:00
parent 3aab801f29
commit ae65b423dd
6 changed files with 117 additions and 96 deletions

View file

@ -1,4 +1,3 @@
#[deriving(Clone,Show)] #[deriving(Clone,Show)]
pub enum Event { pub enum Event {
/// The size of the window has changed. /// The size of the window has changed.
@ -13,45 +12,59 @@ pub enum Event {
/// The window received a unicode character. /// The window received a unicode character.
ReceivedCharacter(char), ReceivedCharacter(char),
/// The cursor has moved on the window.
///
/// The parameter are the (x,y) coords in pixels relative to the top-left corner of the window.
CursorPositionChanged(uint, uint),
/// The window gained or lost focus. /// The window gained or lost focus.
/// ///
/// The parameter is true if the window has gained focus, and false if it has lost focus. /// The parameter is true if the window has gained focus, and false if it has lost focus.
Focused(bool), Focused(bool),
/// An element has been pressed. /// An event from the keyboard has been received.
Pressed(Element), KeyboardInput(ElementState, ScanCode, Option<VirtualKeyCode>, KeyModifiers),
/// An element has been released. /// The cursor has moved on the window.
Released(Element), ///
/// The parameter are the (x,y) coords in pixels relative to the top-left corner of the window.
MouseMoved((int, int)),
/// A positive value indicates that the wheel was rotated forward, away from the user;
/// a negative value indicates that the wheel was rotated backward, toward the user.
MouseWheel(i32),
/// An event from the mouse has been received.
MouseInput(ElementState, MouseButton),
} }
#[deriving(Show, Clone)] pub type ScanCode = u8;
pub enum Element {
Slider0, bitflags!(
Slider1, #[deriving(Show)]
Slider2, flags KeyModifiers: u8 {
Slider3, static LeftControlModifier = 1,
Button0, static RightControlModifier = 2,
Button1, static LeftShitModifier = 4,
Button2, static RightShitModifier = 8,
Button3, static LeftAltModifier = 16,
Button4, static RightRightModifier = 32,
Button5, static NumLockModifier = 64,
Button6, static CapsLockModifier = 128
Button7, }
Button8, )
Button9,
Button10, #[deriving(Show, Hash, PartialEq, Eq, Clone)]
Button11, pub enum ElementState {
Button12, Pressed,
Button13, Released,
Button14, }
Button15,
#[deriving(Show, Hash, PartialEq, Eq, Clone)]
pub enum MouseButton {
LeftMouseButton,
RightMouseButton,
MiddleMouseButton,
OtherMouseButton(u8),
}
#[deriving(Show, Hash, PartialEq, Eq, Clone)]
pub enum VirtualKeyCode {
Key0, Key0,
Key1, Key1,
Key2, Key2,
@ -130,7 +143,6 @@ pub enum Element {
Mute, Mute,
MyComputer, MyComputer,
N, N,
Next,
NextTrack, NextTrack,
NoConvert, NoConvert,
Numlock, Numlock,
@ -150,12 +162,13 @@ pub enum Element {
O, O,
OEM102, OEM102,
P, P,
PageDown,
PageUp,
Pause, Pause,
Period, Period,
Playpause, Playpause,
Power, Power,
Prevtrack, Prevtrack,
Prior,
Q, Q,
R, R,
RBracket, RBracket,

View file

@ -1,7 +1,7 @@
use events; use events;
use super::ffi; use super::ffi;
pub fn vkeycode_to_element(code: ffi::WPARAM) -> Option<events::Element> { pub fn vkeycode_to_element(code: ffi::WPARAM) -> Option<events::VirtualKeyCode> {
Some(match code { Some(match code {
//ffi::VK_LBUTTON => events::Lbutton, //ffi::VK_LBUTTON => events::Lbutton,
//ffi::VK_RBUTTON => events::Rbutton, //ffi::VK_RBUTTON => events::Rbutton,
@ -31,8 +31,8 @@ pub fn vkeycode_to_element(code: ffi::WPARAM) -> Option<events::Element> {
//ffi::VK_ACCEPT => events::Accept, //ffi::VK_ACCEPT => events::Accept,
//ffi::VK_MODECHANGE => events::Modechange, //ffi::VK_MODECHANGE => events::Modechange,
ffi::VK_SPACE => events::Space, ffi::VK_SPACE => events::Space,
ffi::VK_PRIOR => events::Prior, ffi::VK_PRIOR => events::PageUp,
ffi::VK_NEXT => events::Next, ffi::VK_NEXT => events::PageDown,
ffi::VK_END => events::End, ffi::VK_END => events::End,
ffi::VK_HOME => events::Home, ffi::VK_HOME => events::Home,
ffi::VK_LEFT => events::Left, ffi::VK_LEFT => events::Left,

View file

@ -409,6 +409,7 @@ pub static WM_KILLFOCUS: UINT = 0x0008;
pub static WM_MBUTTONDOWN: UINT = 0x0207; pub static WM_MBUTTONDOWN: UINT = 0x0207;
pub static WM_MBUTTONUP: UINT = 0x0208; pub static WM_MBUTTONUP: UINT = 0x0208;
pub static WM_MOUSEMOVE: UINT = 0x0200; pub static WM_MOUSEMOVE: UINT = 0x0200;
pub static WM_MOUSEWHEEL: UINT = 0x020A;
pub static WM_MOVE: UINT = 0x0003; pub static WM_MOVE: UINT = 0x0003;
pub static WM_PAINT: UINT = 0x000F; pub static WM_PAINT: UINT = 0x000F;
pub static WM_RBUTTONDOWN: UINT = 0x0204; pub static WM_RBUTTONDOWN: UINT = 0x0204;

View file

@ -431,67 +431,76 @@ extern "stdcall" fn callback(window: ffi::HWND, msg: ffi::UINT,
}, },
ffi::WM_MOUSEMOVE => { ffi::WM_MOUSEMOVE => {
use CursorPositionChanged; use MouseMoved;
let x = ffi::GET_X_LPARAM(lparam) as uint; let x = ffi::GET_X_LPARAM(lparam) as int;
let y = ffi::GET_Y_LPARAM(lparam) as uint; let y = ffi::GET_Y_LPARAM(lparam) as int;
send_event(window, CursorPositionChanged(x, y)); send_event(window, MouseMoved((x, y)));
0
},
ffi::WM_MOUSEWHEEL => {
use events::MouseWheel;
let value = (wparam >> 16) as i16;
let value = value as i32;
send_event(window, MouseWheel(value));
0 0
}, },
ffi::WM_KEYDOWN => { ffi::WM_KEYDOWN => {
use events::Pressed; use events::{KeyboardInput, KeyModifiers, Pressed};
let element = event::vkeycode_to_element(wparam); let scancode = ((lparam >> 16) & 0xff) as u8;
if element.is_some() { let vkey = event::vkeycode_to_element(wparam);
send_event(window, Pressed(element.unwrap())); send_event(window, KeyboardInput(Pressed, scancode, vkey, KeyModifiers::empty()));
}
0 0
}, },
ffi::WM_KEYUP => { ffi::WM_KEYUP => {
use events::Released; use events::{KeyboardInput, KeyModifiers, Released};
let element = event::vkeycode_to_element(wparam); let scancode = ((lparam >> 16) & 0xff) as u8;
if element.is_some() { let vkey = event::vkeycode_to_element(wparam);
send_event(window, Released(element.unwrap())); send_event(window, KeyboardInput(Released, scancode, vkey, KeyModifiers::empty()));
}
0 0
}, },
ffi::WM_LBUTTONDOWN => { ffi::WM_LBUTTONDOWN => {
use events::{Pressed, Button0}; use events::{Pressed, MouseInput, LeftMouseButton};
send_event(window, Pressed(Button0)); send_event(window, MouseInput(Pressed, LeftMouseButton));
0 0
}, },
ffi::WM_LBUTTONUP => { ffi::WM_LBUTTONUP => {
use events::{Released, Button0}; use events::{Released, MouseInput, LeftMouseButton};
send_event(window, Released(Button0)); send_event(window, MouseInput(Released, LeftMouseButton));
0 0
}, },
ffi::WM_RBUTTONDOWN => { ffi::WM_RBUTTONDOWN => {
use events::{Pressed, Button1}; use events::{Pressed, MouseInput, RightMouseButton};
send_event(window, Pressed(Button1)); send_event(window, MouseInput(Pressed, RightMouseButton));
0 0
}, },
ffi::WM_RBUTTONUP => { ffi::WM_RBUTTONUP => {
use events::{Released, Button1}; use events::{Released, MouseInput, RightMouseButton};
send_event(window, Released(Button1)); send_event(window, MouseInput(Released, RightMouseButton));
0 0
}, },
ffi::WM_MBUTTONDOWN => { ffi::WM_MBUTTONDOWN => {
use events::{Pressed, Button2}; use events::{Pressed, MouseInput, MiddleMouseButton};
send_event(window, Pressed(Button2)); send_event(window, MouseInput(Pressed, MiddleMouseButton));
0 0
}, },
ffi::WM_MBUTTONUP => { ffi::WM_MBUTTONUP => {
use events::{Released, Button2}; use events::{Released, MouseInput, MiddleMouseButton};
send_event(window, Released(Button2)); send_event(window, MouseInput(Released, MiddleMouseButton));
0 0
}, },

View file

@ -1,8 +1,8 @@
use {events, libc}; use {events, libc};
use super::ffi; use super::ffi;
use Element; use VirtualKeyCode;
pub fn keycode_to_element(scancode: libc::c_uint) -> Option<Element> { pub fn keycode_to_element(scancode: libc::c_uint) -> Option<VirtualKeyCode> {
Some(match scancode { Some(match scancode {
//ffi::XK_BackSpace => events::Backspace, //ffi::XK_BackSpace => events::Backspace,
ffi::XK_Tab => events::Tab, ffi::XK_Tab => events::Tab,

View file

@ -331,13 +331,13 @@ impl Window {
}, },
ffi::MotionNotify => { ffi::MotionNotify => {
use CursorPositionChanged; use MouseMoved;
let event: &ffi::XMotionEvent = unsafe { mem::transmute(&xev) }; let event: &ffi::XMotionEvent = unsafe { mem::transmute(&xev) };
events.push(CursorPositionChanged(event.x as uint, event.y as uint)); events.push(MouseMoved((event.x as int, event.y as int)));
}, },
ffi::KeyPress | ffi::KeyRelease => { ffi::KeyPress | ffi::KeyRelease => {
use {Pressed, Released, ReceivedCharacter}; use {KeyboardInput, Pressed, Released, ReceivedCharacter, KeyModifiers};
let event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&xev) }; let event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&xev) };
if event.type_ == ffi::KeyPress { if event.type_ == ffi::KeyPress {
@ -345,7 +345,7 @@ impl Window {
unsafe { ffi::XFilterEvent(mem::transmute(raw_ev), self.window) }; unsafe { ffi::XFilterEvent(mem::transmute(raw_ev), self.window) };
} }
let keysym = unsafe { ffi::XKeycodeToKeysym(self.display, event.keycode as ffi::KeyCode, 0) }; let state = if xev.type_ == ffi::KeyPress { Pressed } else { Released };
let written = unsafe { let written = unsafe {
use std::str; use std::str;
@ -356,48 +356,46 @@ impl Window {
mem::transmute(buffer.as_mut_ptr()), mem::transmute(buffer.as_mut_ptr()),
buffer.len() as libc::c_int, ptr::mut_null(), ptr::mut_null()); buffer.len() as libc::c_int, ptr::mut_null(), ptr::mut_null());
str::from_utf8(buffer.as_slice().slice_to(count as uint)).unwrap_or("").to_string() str::from_utf8(buffer.as_slice().slice_to(count as uint))
.unwrap_or("").to_string()
}; };
for chr in written.as_slice().chars() { for chr in written.as_slice().chars() {
events.push(ReceivedCharacter(chr)); events.push(ReceivedCharacter(chr));
} }
match events::keycode_to_element(keysym as libc::c_uint) { let keysym = unsafe {
Some(elem) if xev.type_ == ffi::KeyPress => { ffi::XKeycodeToKeysym(self.display, event.keycode as ffi::KeyCode, 0)
events.push(Pressed(elem)); };
},
Some(elem) if xev.type_ == ffi::KeyRelease => { let vkey = events::keycode_to_element(keysym as libc::c_uint);
events.push(Released(elem));
}, events.push(KeyboardInput(state, event.keycode as u8,
_ => () vkey, KeyModifiers::empty()));
}
// //
}, },
ffi::ButtonPress | ffi::ButtonRelease => { ffi::ButtonPress | ffi::ButtonRelease => {
use {Pressed, Released}; use {MouseInput, Pressed, Released};
use events; use {LeftMouseButton, RightMouseButton, MiddleMouseButton, OtherMouseButton};
let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) }; let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) };
let elem = match event.button { let state = if xev.type_ == ffi::ButtonPress { Pressed } else { Released };
ffi::Button1 => Some(events::Button1),
ffi::Button2 => Some(events::Button2), let button = match event.button {
ffi::Button3 => Some(events::Button3), ffi::Button1 => Some(LeftMouseButton),
ffi::Button4 => Some(events::Button4), ffi::Button2 => Some(MiddleMouseButton),
ffi::Button5 => Some(events::Button5), ffi::Button3 => Some(RightMouseButton),
ffi::Button4 => Some(OtherMouseButton(4)),
ffi::Button5 => Some(OtherMouseButton(5)),
_ => None _ => None
}; };
if elem.is_some() { match button {
let elem = elem.unwrap(); Some(button) =>
events.push(MouseInput(state, button)),
if xev.type_ == ffi::ButtonPress { None => ()
events.push(Pressed(elem)); };
} else if xev.type_ == ffi::ButtonRelease {
events.push(Released(elem));
}
}
}, },
_ => () _ => ()