196 lines
4.5 KiB
Rust
196 lines
4.5 KiB
Rust
use crate::{point, Keystroke, Modifiers, Pixels, Point};
|
|
use std::{any::Any, ops::Deref};
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct KeyDownEvent {
|
|
pub keystroke: Keystroke,
|
|
pub is_held: bool,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct KeyUpEvent {
|
|
pub keystroke: Keystroke,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct ModifiersChangedEvent {
|
|
pub modifiers: Modifiers,
|
|
}
|
|
|
|
impl Deref for ModifiersChangedEvent {
|
|
type Target = Modifiers;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.modifiers
|
|
}
|
|
}
|
|
|
|
/// The phase of a touch motion event.
|
|
/// Based on the winit enum of the same name,
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub enum TouchPhase {
|
|
Started,
|
|
Moved,
|
|
Ended,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct MouseDownEvent {
|
|
pub button: MouseButton,
|
|
pub position: Point<Pixels>,
|
|
pub modifiers: Modifiers,
|
|
pub click_count: usize,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct MouseUpEvent {
|
|
pub button: MouseButton,
|
|
pub position: Point<Pixels>,
|
|
pub modifiers: Modifiers,
|
|
pub click_count: usize,
|
|
}
|
|
|
|
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
|
|
pub enum MouseButton {
|
|
Left,
|
|
Right,
|
|
Middle,
|
|
Navigate(NavigationDirection),
|
|
}
|
|
|
|
impl MouseButton {
|
|
pub fn all() -> Vec<Self> {
|
|
vec![
|
|
MouseButton::Left,
|
|
MouseButton::Right,
|
|
MouseButton::Middle,
|
|
MouseButton::Navigate(NavigationDirection::Back),
|
|
MouseButton::Navigate(NavigationDirection::Forward),
|
|
]
|
|
}
|
|
}
|
|
|
|
impl Default for MouseButton {
|
|
fn default() -> Self {
|
|
Self::Left
|
|
}
|
|
}
|
|
|
|
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
|
|
pub enum NavigationDirection {
|
|
Back,
|
|
Forward,
|
|
}
|
|
|
|
impl Default for NavigationDirection {
|
|
fn default() -> Self {
|
|
Self::Back
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct MouseMovedEvent {
|
|
pub position: Point<Pixels>,
|
|
pub pressed_button: Option<MouseButton>,
|
|
pub modifiers: Modifiers,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct ScrollWheelEvent {
|
|
pub position: Point<Pixels>,
|
|
pub delta: ScrollDelta,
|
|
pub modifiers: Modifiers,
|
|
pub touch_phase: TouchPhase,
|
|
}
|
|
|
|
impl Deref for ScrollWheelEvent {
|
|
type Target = Modifiers;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.modifiers
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub enum ScrollDelta {
|
|
Pixels(Point<Pixels>),
|
|
Lines(Point<f32>),
|
|
}
|
|
|
|
impl Default for ScrollDelta {
|
|
fn default() -> Self {
|
|
Self::Lines(Default::default())
|
|
}
|
|
}
|
|
|
|
impl ScrollDelta {
|
|
pub fn precise(&self) -> bool {
|
|
match self {
|
|
ScrollDelta::Pixels(_) => true,
|
|
ScrollDelta::Lines(_) => false,
|
|
}
|
|
}
|
|
|
|
pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
|
|
match self {
|
|
ScrollDelta::Pixels(delta) => *delta,
|
|
ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct MouseExitedEvent {
|
|
pub position: Point<Pixels>,
|
|
pub pressed_button: Option<MouseButton>,
|
|
pub modifiers: Modifiers,
|
|
}
|
|
|
|
impl Deref for MouseExitedEvent {
|
|
type Target = Modifiers;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.modifiers
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub enum Event {
|
|
KeyDown(KeyDownEvent),
|
|
KeyUp(KeyUpEvent),
|
|
ModifiersChanged(ModifiersChangedEvent),
|
|
MouseDown(MouseDownEvent),
|
|
MouseUp(MouseUpEvent),
|
|
MouseMoved(MouseMovedEvent),
|
|
MouseExited(MouseExitedEvent),
|
|
ScrollWheel(ScrollWheelEvent),
|
|
}
|
|
|
|
impl Event {
|
|
pub fn position(&self) -> Option<Point<Pixels>> {
|
|
match self {
|
|
Event::KeyDown { .. } => None,
|
|
Event::KeyUp { .. } => None,
|
|
Event::ModifiersChanged { .. } => None,
|
|
Event::MouseDown(event) => Some(event.position),
|
|
Event::MouseUp(event) => Some(event.position),
|
|
Event::MouseMoved(event) => Some(event.position),
|
|
Event::MouseExited(event) => Some(event.position),
|
|
Event::ScrollWheel(event) => Some(event.position),
|
|
}
|
|
}
|
|
|
|
pub fn mouse_event<'a>(&'a self) -> Option<&'a dyn Any> {
|
|
match self {
|
|
Event::KeyDown { .. } => None,
|
|
Event::KeyUp { .. } => None,
|
|
Event::ModifiersChanged { .. } => None,
|
|
Event::MouseDown(event) => Some(event),
|
|
Event::MouseUp(event) => Some(event),
|
|
Event::MouseMoved(event) => Some(event),
|
|
Event::MouseExited(event) => Some(event),
|
|
Event::ScrollWheel(event) => Some(event),
|
|
}
|
|
}
|
|
}
|