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)]
pub enum Event {
/// The size of the window has changed.
@ -13,45 +12,59 @@ pub enum Event {
/// The window received a unicode character.
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 parameter is true if the window has gained focus, and false if it has lost focus.
Focused(bool),
/// An element has been pressed.
Pressed(Element),
/// An event from the keyboard has been received.
KeyboardInput(ElementState, ScanCode, Option<VirtualKeyCode>, KeyModifiers),
/// An element has been released.
Released(Element),
/// 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.
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 enum Element {
Slider0,
Slider1,
Slider2,
Slider3,
Button0,
Button1,
Button2,
Button3,
Button4,
Button5,
Button6,
Button7,
Button8,
Button9,
Button10,
Button11,
Button12,
Button13,
Button14,
Button15,
pub type ScanCode = u8;
bitflags!(
#[deriving(Show)]
flags KeyModifiers: u8 {
static LeftControlModifier = 1,
static RightControlModifier = 2,
static LeftShitModifier = 4,
static RightShitModifier = 8,
static LeftAltModifier = 16,
static RightRightModifier = 32,
static NumLockModifier = 64,
static CapsLockModifier = 128
}
)
#[deriving(Show, Hash, PartialEq, Eq, Clone)]
pub enum ElementState {
Pressed,
Released,
}
#[deriving(Show, Hash, PartialEq, Eq, Clone)]
pub enum MouseButton {
LeftMouseButton,
RightMouseButton,
MiddleMouseButton,
OtherMouseButton(u8),
}
#[deriving(Show, Hash, PartialEq, Eq, Clone)]
pub enum VirtualKeyCode {
Key0,
Key1,
Key2,
@ -130,7 +143,6 @@ pub enum Element {
Mute,
MyComputer,
N,
Next,
NextTrack,
NoConvert,
Numlock,
@ -150,12 +162,13 @@ pub enum Element {
O,
OEM102,
P,
PageDown,
PageUp,
Pause,
Period,
Playpause,
Power,
Prevtrack,
Prior,
Q,
R,
RBracket,

View file

@ -1,7 +1,7 @@
use events;
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 {
//ffi::VK_LBUTTON => events::Lbutton,
//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_MODECHANGE => events::Modechange,
ffi::VK_SPACE => events::Space,
ffi::VK_PRIOR => events::Prior,
ffi::VK_NEXT => events::Next,
ffi::VK_PRIOR => events::PageUp,
ffi::VK_NEXT => events::PageDown,
ffi::VK_END => events::End,
ffi::VK_HOME => events::Home,
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_MBUTTONUP: UINT = 0x0208;
pub static WM_MOUSEMOVE: UINT = 0x0200;
pub static WM_MOUSEWHEEL: UINT = 0x020A;
pub static WM_MOVE: UINT = 0x0003;
pub static WM_PAINT: UINT = 0x000F;
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 => {
use CursorPositionChanged;
use MouseMoved;
let x = ffi::GET_X_LPARAM(lparam) as uint;
let y = ffi::GET_Y_LPARAM(lparam) as uint;
let x = ffi::GET_X_LPARAM(lparam) as int;
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
},
ffi::WM_KEYDOWN => {
use events::Pressed;
let element = event::vkeycode_to_element(wparam);
if element.is_some() {
send_event(window, Pressed(element.unwrap()));
}
use events::{KeyboardInput, KeyModifiers, Pressed};
let scancode = ((lparam >> 16) & 0xff) as u8;
let vkey = event::vkeycode_to_element(wparam);
send_event(window, KeyboardInput(Pressed, scancode, vkey, KeyModifiers::empty()));
0
},
ffi::WM_KEYUP => {
use events::Released;
let element = event::vkeycode_to_element(wparam);
if element.is_some() {
send_event(window, Released(element.unwrap()));
}
use events::{KeyboardInput, KeyModifiers, Released};
let scancode = ((lparam >> 16) & 0xff) as u8;
let vkey = event::vkeycode_to_element(wparam);
send_event(window, KeyboardInput(Released, scancode, vkey, KeyModifiers::empty()));
0
},
ffi::WM_LBUTTONDOWN => {
use events::{Pressed, Button0};
send_event(window, Pressed(Button0));
use events::{Pressed, MouseInput, LeftMouseButton};
send_event(window, MouseInput(Pressed, LeftMouseButton));
0
},
ffi::WM_LBUTTONUP => {
use events::{Released, Button0};
send_event(window, Released(Button0));
use events::{Released, MouseInput, LeftMouseButton};
send_event(window, MouseInput(Released, LeftMouseButton));
0
},
ffi::WM_RBUTTONDOWN => {
use events::{Pressed, Button1};
send_event(window, Pressed(Button1));
use events::{Pressed, MouseInput, RightMouseButton};
send_event(window, MouseInput(Pressed, RightMouseButton));
0
},
ffi::WM_RBUTTONUP => {
use events::{Released, Button1};
send_event(window, Released(Button1));
use events::{Released, MouseInput, RightMouseButton};
send_event(window, MouseInput(Released, RightMouseButton));
0
},
ffi::WM_MBUTTONDOWN => {
use events::{Pressed, Button2};
send_event(window, Pressed(Button2));
use events::{Pressed, MouseInput, MiddleMouseButton};
send_event(window, MouseInput(Pressed, MiddleMouseButton));
0
},
ffi::WM_MBUTTONUP => {
use events::{Released, Button2};
send_event(window, Released(Button2));
use events::{Released, MouseInput, MiddleMouseButton};
send_event(window, MouseInput(Released, MiddleMouseButton));
0
},

View file

@ -1,8 +1,8 @@
use {events, libc};
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 {
//ffi::XK_BackSpace => events::Backspace,
ffi::XK_Tab => events::Tab,

View file

@ -331,13 +331,13 @@ impl Window {
},
ffi::MotionNotify => {
use CursorPositionChanged;
use MouseMoved;
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 => {
use {Pressed, Released, ReceivedCharacter};
use {KeyboardInput, Pressed, Released, ReceivedCharacter, KeyModifiers};
let event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&xev) };
if event.type_ == ffi::KeyPress {
@ -345,7 +345,7 @@ impl 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 {
use std::str;
@ -356,48 +356,46 @@ impl Window {
mem::transmute(buffer.as_mut_ptr()),
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() {
events.push(ReceivedCharacter(chr));
}
match events::keycode_to_element(keysym as libc::c_uint) {
Some(elem) if xev.type_ == ffi::KeyPress => {
events.push(Pressed(elem));
},
Some(elem) if xev.type_ == ffi::KeyRelease => {
events.push(Released(elem));
},
_ => ()
}
let keysym = unsafe {
ffi::XKeycodeToKeysym(self.display, event.keycode as ffi::KeyCode, 0)
};
let vkey = events::keycode_to_element(keysym as libc::c_uint);
events.push(KeyboardInput(state, event.keycode as u8,
vkey, KeyModifiers::empty()));
//
},
ffi::ButtonPress | ffi::ButtonRelease => {
use {Pressed, Released};
use events;
use {MouseInput, Pressed, Released};
use {LeftMouseButton, RightMouseButton, MiddleMouseButton, OtherMouseButton};
let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) };
let elem = match event.button {
ffi::Button1 => Some(events::Button1),
ffi::Button2 => Some(events::Button2),
ffi::Button3 => Some(events::Button3),
ffi::Button4 => Some(events::Button4),
ffi::Button5 => Some(events::Button5),
let state = if xev.type_ == ffi::ButtonPress { Pressed } else { Released };
let button = match event.button {
ffi::Button1 => Some(LeftMouseButton),
ffi::Button2 => Some(MiddleMouseButton),
ffi::Button3 => Some(RightMouseButton),
ffi::Button4 => Some(OtherMouseButton(4)),
ffi::Button5 => Some(OtherMouseButton(5)),
_ => None
};
if elem.is_some() {
let elem = elem.unwrap();
if xev.type_ == ffi::ButtonPress {
events.push(Pressed(elem));
} else if xev.type_ == ffi::ButtonRelease {
events.push(Released(elem));
}
}
match button {
Some(button) =>
events.push(MouseInput(state, button)),
None => ()
};
},
_ => ()