commit
030388cf25
|
@ -24,42 +24,10 @@ 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::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),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
106
src/event.rs
106
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)]
|
||||
pub enum MouseButtonID {
|
||||
pub enum MouseButton {
|
||||
Left,
|
||||
Middle,
|
||||
Right,
|
||||
|
@ -8,39 +17,94 @@ 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,
|
||||
}
|
||||
|
||||
#[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,
|
||||
#[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 {
|
||||
Mouse(MouseEvent),
|
||||
Keyboard(KeyboardEvent),
|
||||
Window(WindowEvent),
|
||||
FileDrop(FileDropEvent),
|
||||
}
|
||||
|
|
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::*;
|
||||
|
||||
mod event;
|
||||
mod keyboard;
|
||||
mod mouse_cursor;
|
||||
pub use event::*;
|
||||
pub use keyboard::*;
|
||||
pub use mouse_cursor::MouseCursor;
|
||||
|
||||
pub enum Parent {
|
||||
None,
|
||||
|
|
|
@ -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
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 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;
|
||||
|
@ -74,10 +77,13 @@ unsafe extern "system" fn wnd_proc<H: WindowHandler>(
|
|||
WM_MOUSEMOVE => {
|
||||
let x = (lparam & 0xFFFF) as i32;
|
||||
let y = ((lparam >> 16) & 0xFFFF) as i32;
|
||||
window_state
|
||||
.borrow_mut()
|
||||
.handler
|
||||
.on_event(&mut window, Event::CursorMotion(x, y));
|
||||
window_state.borrow_mut().handler.on_event(
|
||||
&mut window,
|
||||
Event::Mouse(MouseEvent::CursorMoved {
|
||||
x: x as i32,
|
||||
y: y as i32,
|
||||
}),
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
WM_TIMER => {
|
||||
|
@ -91,7 +97,7 @@ unsafe extern "system" fn wnd_proc<H: WindowHandler>(
|
|||
window_state
|
||||
.borrow_mut()
|
||||
.handler
|
||||
.on_event(&mut window, Event::WillClose);
|
||||
.on_event(&mut window, Event::Window(WindowEvent::WillClose));
|
||||
return DefWindowProcA(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
@ -4,7 +4,10 @@ use std::time::*;
|
|||
use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle};
|
||||
|
||||
use super::XcbConnection;
|
||||
use crate::{Event, MouseButtonID, MouseScroll, Parent, WindowHandler, WindowOpenOptions};
|
||||
use crate::{
|
||||
Event, FileDropEvent, KeyboardEvent, MouseButton, MouseEvent, Parent, ScrollDelta, WindowEvent,
|
||||
WindowHandler, WindowOpenOptions,
|
||||
};
|
||||
|
||||
|
||||
pub struct Window {
|
||||
|
@ -216,7 +219,7 @@ impl Window {
|
|||
let wm_delete_window = self.xcb_connection.atoms.wm_delete_window.unwrap_or(xcb::NONE);
|
||||
|
||||
if wm_delete_window == data32[0] {
|
||||
handler.on_event(self, Event::WillClose);
|
||||
handler.on_event(self, Event::Window(WindowEvent::WillClose));
|
||||
|
||||
// FIXME: handler should decide whether window stays open or not
|
||||
self.event_loop_running = false;
|
||||
|
@ -234,7 +237,7 @@ impl Window {
|
|||
if detail != 4 && detail != 5 {
|
||||
handler.on_event(
|
||||
self,
|
||||
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 }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -247,24 +250,24 @@ impl Window {
|
|||
4 => {
|
||||
handler.on_event(
|
||||
self,
|
||||
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(
|
||||
self,
|
||||
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(self, Event::MouseDown(button_id));
|
||||
handler.on_event(self, Event::Mouse(MouseEvent::ButtonPressed(button_id)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -275,7 +278,7 @@ impl Window {
|
|||
|
||||
if detail != 4 && detail != 5 {
|
||||
let button_id = mouse_id(detail);
|
||||
handler.on_event(self, Event::MouseUp(button_id));
|
||||
handler.on_event(self, Event::Mouse(MouseEvent::ButtonReleased(button_id)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -287,14 +290,14 @@ impl Window {
|
|||
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
|
||||
let detail = event.detail();
|
||||
|
||||
handler.on_event(self, Event::KeyDown(detail));
|
||||
handler.on_event(self, 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(self, Event::KeyUp(detail));
|
||||
handler.on_event(self, Event::Keyboard(KeyboardEvent::KeyReleased(detail as u32)));
|
||||
}
|
||||
|
||||
_ => {
|
||||
|
@ -314,13 +317,13 @@ unsafe impl HasRawWindowHandle for Window {
|
|||
}
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue