Add and refactor events
This commit is contained in:
parent
23af18020e
commit
b8bc006fc1
|
@ -24,42 +24,11 @@ impl WindowHandler for MyProgram {
|
||||||
|
|
||||||
fn on_event(&mut self, window: &mut Window, event: Event) {
|
fn on_event(&mut self, window: &mut Window, event: Event) {
|
||||||
match event {
|
match event {
|
||||||
Event::CursorMotion(x, y) => {
|
Event::Interval(delta_time) => println!("Update interval, delta time: {}", delta_time),
|
||||||
println!("Cursor moved, x: {}, y: {}", x, y);
|
Event::Mouse(e) => println!("Mouse event: {:?}", e),
|
||||||
}
|
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
|
||||||
Event::MouseDown(button_id) => {
|
Event::Window(e) => println!("Window event: {:?}", e),
|
||||||
println!("Mouse down, button id: {:?}", button_id);
|
Event::FileDrop(e) => println!("File drop event: {:?}", e),
|
||||||
}
|
|
||||||
Event::MouseUp(button_id) => {
|
|
||||||
println!("Mouse up, button id: {:?}", button_id);
|
|
||||||
}
|
|
||||||
Event::MouseScroll(mouse_scroll) => {
|
|
||||||
println!("Mouse scroll, {:?}", mouse_scroll);
|
|
||||||
}
|
|
||||||
Event::MouseClick(mouse_click) => {
|
|
||||||
println!("Mouse click, {:?}", mouse_click);
|
|
||||||
}
|
|
||||||
Event::KeyDown(keycode) => {
|
|
||||||
println!("Key down, keycode: {}", keycode);
|
|
||||||
}
|
|
||||||
Event::KeyUp(keycode) => {
|
|
||||||
println!("Key up, keycode: {}", keycode);
|
|
||||||
}
|
|
||||||
Event::CharacterInput(char_code) => {
|
|
||||||
println!("Character input, char_code: {}", char_code);
|
|
||||||
}
|
|
||||||
Event::WindowResized(window_info) => {
|
|
||||||
println!("Window resized, {:?}", window_info);
|
|
||||||
}
|
|
||||||
Event::WindowFocus => {
|
|
||||||
println!("Window focused");
|
|
||||||
}
|
|
||||||
Event::WindowUnfocus => {
|
|
||||||
println!("Window unfocused");
|
|
||||||
}
|
|
||||||
Event::WillClose => {
|
|
||||||
println!("Window will close");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
107
src/event.rs
107
src/event.rs
|
@ -1,5 +1,14 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub enum KeyboardEvent {
|
||||||
|
KeyPressed(u32),
|
||||||
|
KeyReleased(u32),
|
||||||
|
CharacterInput(char),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub enum MouseButtonID {
|
pub enum MouseButton {
|
||||||
Left,
|
Left,
|
||||||
Middle,
|
Middle,
|
||||||
Right,
|
Right,
|
||||||
|
@ -8,39 +17,95 @@ pub enum MouseButtonID {
|
||||||
Other(u8),
|
Other(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
/// A scroll movement.
|
||||||
pub struct MouseScroll {
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub x_delta: f64,
|
pub enum ScrollDelta {
|
||||||
pub y_delta: f64,
|
/// A line-based scroll movement
|
||||||
|
Lines {
|
||||||
|
/// The number of horizontal lines scrolled
|
||||||
|
x: f32,
|
||||||
|
|
||||||
|
/// The number of vertical lines scrolled
|
||||||
|
y: f32,
|
||||||
|
},
|
||||||
|
/// A pixel-based scroll movement
|
||||||
|
Pixels {
|
||||||
|
/// The number of horizontal pixels scrolled
|
||||||
|
x: f32,
|
||||||
|
/// The number of vertical pixels scrolled
|
||||||
|
y: f32,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
pub struct MouseClick {
|
pub struct MouseClick {
|
||||||
pub id: MouseButtonID,
|
pub button: MouseButton,
|
||||||
pub click_count: usize,
|
pub click_count: usize,
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32,
|
pub y: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub enum MouseEvent {
|
||||||
|
/// The mouse cursor was moved
|
||||||
|
CursorMoved {
|
||||||
|
/// The X coordinate of the mouse position
|
||||||
|
x: i32,
|
||||||
|
/// The Y coordinate of the mouse position
|
||||||
|
y: i32,
|
||||||
|
},
|
||||||
|
|
||||||
|
/// A mouse button was pressed.
|
||||||
|
ButtonPressed(MouseButton),
|
||||||
|
|
||||||
|
/// A mouse button was released.
|
||||||
|
ButtonReleased(MouseButton),
|
||||||
|
|
||||||
|
/// A mouse button was clicked.
|
||||||
|
Click(MouseClick),
|
||||||
|
|
||||||
|
/// The mouse wheel was scrolled.
|
||||||
|
WheelScrolled(ScrollDelta),
|
||||||
|
|
||||||
|
/// The mouse cursor entered the window.
|
||||||
|
CursorEntered,
|
||||||
|
|
||||||
|
/// The mouse cursor left the window.
|
||||||
|
CursorLeft,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
pub struct WindowInfo {
|
pub struct WindowInfo {
|
||||||
pub width: u32,
|
pub width: u32,
|
||||||
pub height: u32,
|
pub height: u32,
|
||||||
pub scale: f64,
|
pub scale_factor: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub enum WindowEvent {
|
||||||
|
Resized(WindowInfo),
|
||||||
|
Focused,
|
||||||
|
Unfocused,
|
||||||
|
WillClose,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Clone, Debug)]
|
||||||
|
pub enum FileDropEvent {
|
||||||
|
/// A file is being hovered over the window.
|
||||||
|
FileHovered(PathBuf),
|
||||||
|
|
||||||
|
/// A file has beend dropped into the window.
|
||||||
|
FileDropped(PathBuf),
|
||||||
|
|
||||||
|
/// A file was hovered, but has exited the window.
|
||||||
|
FilesHoveredLeft,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
CursorMotion(i32, i32), // new (x, y) relative to window
|
Interval(f64), // delta time passed
|
||||||
MouseDown(MouseButtonID),
|
Mouse(MouseEvent),
|
||||||
MouseUp(MouseButtonID),
|
Keyboard(KeyboardEvent),
|
||||||
MouseScroll(MouseScroll),
|
Window(WindowEvent),
|
||||||
MouseClick(MouseClick),
|
FileDrop(FileDropEvent),
|
||||||
KeyDown(u8), // keycode
|
|
||||||
KeyUp(u8), // keycode
|
|
||||||
CharacterInput(u32), // character code
|
|
||||||
WindowResized(WindowInfo), // new (width, height)
|
|
||||||
WindowFocus,
|
|
||||||
WindowUnfocus,
|
|
||||||
WillClose,
|
|
||||||
}
|
}
|
||||||
|
|
26
src/keyboard.rs
Normal file
26
src/keyboard.rs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
// TODO: Add a method to the Window that returns the
|
||||||
|
// current modifier state.
|
||||||
|
|
||||||
|
/// The current state of the keyboard modifiers.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||||
|
pub struct ModifiersState {
|
||||||
|
pub shift: bool,
|
||||||
|
pub control: bool,
|
||||||
|
pub alt: bool,
|
||||||
|
pub logo: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ModifiersState {
|
||||||
|
/// Returns true if the current [`ModifiersState`] has at least the same
|
||||||
|
/// modifiers enabled as the given value, and false otherwise.
|
||||||
|
///
|
||||||
|
/// [`ModifiersState`]: struct.ModifiersState.html
|
||||||
|
pub fn matches_atleast(&self, modifiers: ModifiersState) -> bool {
|
||||||
|
let shift = !modifiers.shift || self.shift;
|
||||||
|
let control = !modifiers.control || self.control;
|
||||||
|
let alt = !modifiers.alt || self.alt;
|
||||||
|
let logo = !modifiers.logo || self.logo;
|
||||||
|
|
||||||
|
shift && control && alt && logo
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,7 +16,11 @@ mod macos;
|
||||||
pub use macos::*;
|
pub use macos::*;
|
||||||
|
|
||||||
mod event;
|
mod event;
|
||||||
|
mod keyboard;
|
||||||
|
mod mouse_cursor;
|
||||||
pub use event::*;
|
pub use event::*;
|
||||||
|
pub use keyboard::*;
|
||||||
|
pub use mouse_cursor::MouseCursor;
|
||||||
|
|
||||||
pub enum Parent {
|
pub enum Parent {
|
||||||
None,
|
None,
|
||||||
|
|
|
@ -10,7 +10,10 @@ use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString};
|
||||||
|
|
||||||
use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle};
|
use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle};
|
||||||
|
|
||||||
use crate::{MouseScroll, WindowHandler, WindowOpenOptions};
|
use crate::{
|
||||||
|
Event, FileDropEvent, KeyboardEvent, MouseButton, MouseEvent, ScrollDelta, WindowEvent,
|
||||||
|
WindowHandler, WindowOpenOptions,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
ns_window: id,
|
ns_window: id,
|
||||||
|
|
18
src/mouse_cursor.rs
Normal file
18
src/mouse_cursor.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)]
|
||||||
|
pub enum MouseCursor {
|
||||||
|
Idle,
|
||||||
|
Pointer,
|
||||||
|
Grab,
|
||||||
|
Text,
|
||||||
|
Crosshair,
|
||||||
|
Working,
|
||||||
|
Grabbing,
|
||||||
|
ResizingHorizontally,
|
||||||
|
ResizingVertically,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for MouseCursor {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Idle
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,7 +18,10 @@ use std::rc::Rc;
|
||||||
|
|
||||||
use raw_window_handle::{windows::WindowsHandle, HasRawWindowHandle, RawWindowHandle};
|
use raw_window_handle::{windows::WindowsHandle, HasRawWindowHandle, RawWindowHandle};
|
||||||
|
|
||||||
use crate::{Event, Parent::WithParent, WindowHandler, WindowInfo, WindowOpenOptions};
|
use crate::{
|
||||||
|
Event, FileDropEvent, KeyboardEvent, MouseButton, MouseEvent, Parent::WithParent, ScrollDelta,
|
||||||
|
WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions,
|
||||||
|
};
|
||||||
|
|
||||||
unsafe fn message_box(title: &str, msg: &str) {
|
unsafe fn message_box(title: &str, msg: &str) {
|
||||||
let title = (title.to_owned() + "\0").as_ptr() as *const i8;
|
let title = (title.to_owned() + "\0").as_ptr() as *const i8;
|
||||||
|
|
|
@ -2,7 +2,10 @@ use std::ffi::CStr;
|
||||||
use std::os::raw::{c_ulong, c_void};
|
use std::os::raw::{c_ulong, c_void};
|
||||||
|
|
||||||
use super::XcbConnection;
|
use super::XcbConnection;
|
||||||
use crate::{Event, MouseButtonID, MouseScroll, Parent, WindowHandler, WindowOpenOptions};
|
use crate::{
|
||||||
|
Event, FileDropEvent, KeyboardEvent, MouseButton, MouseEvent, Parent, ScrollDelta, WindowEvent,
|
||||||
|
WindowHandler, WindowOpenOptions,
|
||||||
|
};
|
||||||
|
|
||||||
use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle};
|
use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle};
|
||||||
|
|
||||||
|
@ -157,7 +160,10 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
if detail != 4 && detail != 5 {
|
if detail != 4 && detail != 5 {
|
||||||
handler.on_event(
|
handler.on_event(
|
||||||
window,
|
window,
|
||||||
Event::CursorMotion(event.event_x() as i32, event.event_y() as i32),
|
Event::Mouse(MouseEvent::CursorMoved {
|
||||||
|
x: event.event_x() as i32,
|
||||||
|
y: event.event_y() as i32,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,24 +175,27 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
4 => {
|
4 => {
|
||||||
handler.on_event(
|
handler.on_event(
|
||||||
window,
|
window,
|
||||||
Event::MouseScroll(MouseScroll {
|
Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines {
|
||||||
x_delta: 0.0,
|
x: 0.0,
|
||||||
y_delta: 1.0,
|
y: 1.0,
|
||||||
}),
|
})),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
5 => {
|
5 => {
|
||||||
handler.on_event(
|
handler.on_event(
|
||||||
window,
|
window,
|
||||||
Event::MouseScroll(MouseScroll {
|
Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines {
|
||||||
x_delta: 0.0,
|
x: 0.0,
|
||||||
y_delta: -1.0,
|
y: -1.0,
|
||||||
}),
|
})),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
detail => {
|
detail => {
|
||||||
let button_id = mouse_id(detail);
|
let button_id = mouse_id(detail);
|
||||||
handler.on_event(window, Event::MouseDown(button_id));
|
handler.on_event(
|
||||||
|
window,
|
||||||
|
Event::Mouse(MouseEvent::ButtonPressed(button_id)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,20 +205,27 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
|
|
||||||
if detail != 4 && detail != 5 {
|
if detail != 4 && detail != 5 {
|
||||||
let button_id = mouse_id(detail);
|
let button_id = mouse_id(detail);
|
||||||
handler.on_event(window, Event::MouseUp(button_id));
|
handler
|
||||||
|
.on_event(window, Event::Mouse(MouseEvent::ButtonReleased(button_id)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xcb::KEY_PRESS => {
|
xcb::KEY_PRESS => {
|
||||||
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
|
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
|
||||||
let detail = event.detail();
|
let detail = event.detail();
|
||||||
|
|
||||||
handler.on_event(window, Event::KeyDown(detail));
|
handler.on_event(
|
||||||
|
window,
|
||||||
|
Event::Keyboard(KeyboardEvent::KeyPressed(detail as u32)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
xcb::KEY_RELEASE => {
|
xcb::KEY_RELEASE => {
|
||||||
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
|
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
|
||||||
let detail = event.detail();
|
let detail = event.detail();
|
||||||
|
|
||||||
handler.on_event(window, Event::KeyUp(detail));
|
handler.on_event(
|
||||||
|
window,
|
||||||
|
Event::Keyboard(KeyboardEvent::KeyReleased(detail as u32)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Unhandled event type: {:?}", event_type);
|
println!("Unhandled event type: {:?}", event_type);
|
||||||
|
@ -300,13 +316,13 @@ fn get_scaling_screen_dimensions(xcb_connection: &XcbConnection) -> Option<f64>
|
||||||
Some(yscale)
|
Some(yscale)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mouse_id(id: u8) -> MouseButtonID {
|
fn mouse_id(id: u8) -> MouseButton {
|
||||||
match id {
|
match id {
|
||||||
1 => MouseButtonID::Left,
|
1 => MouseButton::Left,
|
||||||
2 => MouseButtonID::Middle,
|
2 => MouseButton::Middle,
|
||||||
3 => MouseButtonID::Right,
|
3 => MouseButton::Right,
|
||||||
6 => MouseButtonID::Back,
|
6 => MouseButton::Back,
|
||||||
7 => MouseButtonID::Forward,
|
7 => MouseButton::Forward,
|
||||||
id => MouseButtonID::Other(id),
|
id => MouseButton::Other(id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue