aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomaka17 <pierre.krieger1708@gmail.com>2014-08-13 17:04:57 +0200
committerTomaka17 <pierre.krieger1708@gmail.com>2014-08-13 17:23:03 +0200
commitae65b423ddf2be0bb7981eba81366a93c686724f (patch)
treeebf9c4b9a38c5a06ce324d010ad3e6e4297f5d65
parent3aab801f291c513f5b662589097fa5d5653d8ffe (diff)
downloadglutin-ae65b423ddf2be0bb7981eba81366a93c686724f.tar.gz
glutin-ae65b423ddf2be0bb7981eba81366a93c686724f.zip
Implement new events system
-rw-r--r--src/events.rs81
-rw-r--r--src/win32/event.rs6
-rw-r--r--src/win32/ffi.rs1
-rw-r--r--src/win32/init.rs61
-rw-r--r--src/x11/events.rs4
-rw-r--r--src/x11/mod.rs60
6 files changed, 117 insertions, 96 deletions
diff --git a/src/events.rs b/src/events.rs
index 41bfcfc..5232b44 100644
--- a/src/events.rs
+++ b/src/events.rs
@@ -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),
+
+ /// 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),
+}
+
+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,
+}
- /// An element has been released.
- Released(Element),
+#[deriving(Show, Hash, PartialEq, Eq, Clone)]
+pub enum MouseButton {
+ LeftMouseButton,
+ RightMouseButton,
+ MiddleMouseButton,
+ OtherMouseButton(u8),
}
-#[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,
+#[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,
diff --git a/src/win32/event.rs b/src/win32/event.rs
index 83d775d..03eebfc 100644
--- a/src/win32/event.rs
+++ b/src/win32/event.rs
@@ -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,
diff --git a/src/win32/ffi.rs b/src/win32/ffi.rs
index 4480155..7f06064 100644
--- a/src/win32/ffi.rs
+++ b/src/win32/ffi.rs
@@ -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;
diff --git a/src/win32/init.rs b/src/win32/init.rs
index 77a864e..14c61a4 100644
--- a/src/win32/init.rs
+++ b/src/win32/init.rs
@@ -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
},
diff --git a/src/x11/events.rs b/src/x11/events.rs
index 78d310c..d5b559a 100644
--- a/src/x11/events.rs
+++ b/src/x11/events.rs
@@ -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,
diff --git a/src/x11/mod.rs b/src/x11/mod.rs
index 3286b99..455f43c 100644
--- a/src/x11/mod.rs
+++ b/src/x11/mod.rs
@@ -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 => ()
+ };
},
_ => ()