1
0
Fork 0

Add and refactor events

This commit is contained in:
Billy Messenger 2020-09-11 10:21:05 -05:00
parent 23af18020e
commit b8bc006fc1
8 changed files with 184 additions and 80 deletions

View file

@ -24,42 +24,11 @@ impl WindowHandler for MyProgram {
fn on_event(&mut self, window: &mut Window, event: Event) {
match event {
Event::CursorMotion(x, y) => {
println!("Cursor moved, x: {}, y: {}", x, y);
}
Event::MouseDown(button_id) => {
println!("Mouse down, button id: {:?}", button_id);
}
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");
}
Event::Interval(delta_time) => println!("Update interval, delta time: {}", delta_time),
Event::Mouse(e) => println!("Mouse event: {:?}", e),
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
Event::Window(e) => println!("Window event: {:?}", e),
Event::FileDrop(e) => println!("File drop event: {:?}", e),
}
}

View file

@ -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)]
pub enum MouseButtonID {
pub enum MouseButton {
Left,
Middle,
Right,
@ -8,39 +17,95 @@ pub enum MouseButtonID {
Other(u8),
}
#[derive(Debug, Copy, Clone)]
pub struct MouseScroll {
pub x_delta: f64,
pub y_delta: f64,
/// A scroll movement.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScrollDelta {
/// 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 id: MouseButtonID,
pub button: MouseButton,
pub click_count: usize,
pub x: 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 width: 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)]
pub enum Event {
CursorMotion(i32, i32), // new (x, y) relative to window
MouseDown(MouseButtonID),
MouseUp(MouseButtonID),
MouseScroll(MouseScroll),
MouseClick(MouseClick),
KeyDown(u8), // keycode
KeyUp(u8), // keycode
CharacterInput(u32), // character code
WindowResized(WindowInfo), // new (width, height)
WindowFocus,
WindowUnfocus,
WillClose,
Interval(f64), // delta time passed
Mouse(MouseEvent),
Keyboard(KeyboardEvent),
Window(WindowEvent),
FileDrop(FileDropEvent),
}

26
src/keyboard.rs Normal file
View 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
}
}

View file

@ -16,7 +16,11 @@ mod macos;
pub use macos::*;
mod event;
mod keyboard;
mod mouse_cursor;
pub use event::*;
pub use keyboard::*;
pub use mouse_cursor::MouseCursor;
pub enum Parent {
None,

View file

@ -10,7 +10,10 @@ use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString};
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 {
ns_window: id,

18
src/mouse_cursor.rs Normal file
View 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
}
}

View file

@ -18,7 +18,10 @@ use std::rc::Rc;
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) {
let title = (title.to_owned() + "\0").as_ptr() as *const i8;

View file

@ -2,7 +2,10 @@ use std::ffi::CStr;
use std::os::raw::{c_ulong, c_void};
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};
@ -157,7 +160,10 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
if detail != 4 && detail != 5 {
handler.on_event(
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 => {
handler.on_event(
window,
Event::MouseScroll(MouseScroll {
x_delta: 0.0,
y_delta: 1.0,
}),
Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines {
x: 0.0,
y: 1.0,
})),
);
}
5 => {
handler.on_event(
window,
Event::MouseScroll(MouseScroll {
x_delta: 0.0,
y_delta: -1.0,
}),
Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines {
x: 0.0,
y: -1.0,
})),
);
}
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 {
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 => {
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
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 => {
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
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);
@ -300,13 +316,13 @@ fn get_scaling_screen_dimensions(xcb_connection: &XcbConnection) -> Option<f64>
Some(yscale)
}
fn mouse_id(id: u8) -> MouseButtonID {
fn mouse_id(id: u8) -> MouseButton {
match id {
1 => MouseButtonID::Left,
2 => MouseButtonID::Middle,
3 => MouseButtonID::Right,
6 => MouseButtonID::Back,
7 => MouseButtonID::Forward,
id => MouseButtonID::Other(id),
1 => MouseButton::Left,
2 => MouseButton::Middle,
3 => MouseButton::Right,
6 => MouseButton::Back,
7 => MouseButton::Forward,
id => MouseButton::Other(id),
}
}