#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum WindowEvent {
Pos(i32, i32),
Size(u32, u32),
Close,
Refresh,
Focus(bool),
Iconify(bool),
FramebufferSize(u32, u32),
MouseButton(MouseButton, Action, Modifiers),
CursorPos(f64, f64, Modifiers),
CursorEnter(bool),
Scroll(f64, f64, Modifiers),
Key(Key, Action, Modifiers),
Char(char),
CharModifiers(char, Modifiers),
Touch(u64, f64, f64, TouchAction, Modifiers),
}
use WindowEvent::*;
impl WindowEvent {
pub fn is_keyboard_event(&self) -> bool {
matches!(self, Key(..) | Char(..) | CharModifiers(..))
}
pub fn is_mouse_event(&self) -> bool {
matches!(
self,
MouseButton(..) | CursorPos(..) | CursorEnter(..) | Scroll(..)
)
}
pub fn is_touch_event(&self) -> bool {
matches!(self, Touch(..))
}
pub fn with_modifiers(self, modifiers: Modifiers) -> Self {
match self {
MouseButton(button, action, _) => MouseButton(button, action, modifiers),
CursorPos(x, y, _) => CursorPos(x, y, modifiers),
Scroll(x_offset, y_offset, _) => Scroll(x_offset, y_offset, modifiers),
Key(key, action, _) => Key(key, action, modifiers),
CharModifiers(character, _) => CharModifiers(character, modifiers),
Touch(id, x, y, action, _) => Touch(id, x, y, action, modifiers),
other => other,
}
}
pub fn modifiers(&self) -> Option<Modifiers> {
match *self {
MouseButton(_, _, modifiers)
| CursorPos(_, _, modifiers)
| Scroll(_, _, modifiers)
| Key(_, _, modifiers)
| CharModifiers(_, modifiers)
| Touch(_, _, _, _, modifiers) => Some(modifiers),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::{Action, Key, Modifiers, MouseButton, TouchAction, WindowEvent};
#[test]
fn with_modifiers_stamps_every_carrying_variant() {
let none = Modifiers::empty();
let mods = Modifiers::Control | Modifiers::Shift;
let carrying = [
WindowEvent::MouseButton(MouseButton::Button1, Action::Press, none),
WindowEvent::CursorPos(1.0, 2.0, none),
WindowEvent::Scroll(1.0, 2.0, none),
WindowEvent::Key(Key::S, Action::Press, none),
WindowEvent::CharModifiers('s', none),
WindowEvent::Touch(0, 1.0, 2.0, TouchAction::Start, none),
];
for event in carrying {
assert_eq!(event.modifiers(), Some(none));
assert_eq!(event.with_modifiers(mods).modifiers(), Some(mods));
}
let plain = [
WindowEvent::Close,
WindowEvent::Char('s'),
WindowEvent::CursorEnter(true),
WindowEvent::Size(1, 2),
];
for event in plain {
assert_eq!(event.modifiers(), None);
assert_eq!(event.with_modifiers(mods), event);
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Key {
Key1,
Key2,
Key3,
Key4,
Key5,
Key6,
Key7,
Key8,
Key9,
Key0,
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
Escape,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
F13,
F14,
F15,
F16,
F17,
F18,
F19,
F20,
F21,
F22,
F23,
F24,
Snapshot,
Scroll,
Pause,
Insert,
Home,
Delete,
End,
PageDown,
PageUp,
Left,
Up,
Right,
Down,
Back,
Return,
Space,
Compose,
Caret,
Numlock,
Numpad0,
Numpad1,
Numpad2,
Numpad3,
Numpad4,
Numpad5,
Numpad6,
Numpad7,
Numpad8,
Numpad9,
AbntC1,
AbntC2,
Add,
Apostrophe,
Apps,
At,
Ax,
Backslash,
Calculator,
Capital,
Colon,
Comma,
Convert,
Decimal,
Divide,
Equals,
Grave,
Kana,
Kanji,
LAlt,
LBracket,
LControl,
LShift,
LWin,
Mail,
MediaSelect,
MediaStop,
Minus,
Multiply,
Mute,
MyComputer,
NavigateForward,
NavigateBackward,
NextTrack,
NoConvert,
NumpadComma,
NumpadEnter,
NumpadEquals,
OEM102,
Period,
PlayPause,
Power,
PrevTrack,
RAlt,
RBracket,
RControl,
RShift,
RWin,
Semicolon,
Slash,
Sleep,
Stop,
Subtract,
Sysrq,
Tab,
Underline,
Unlabeled,
VolumeDown,
VolumeUp,
Wake,
WebBack,
WebFavorites,
WebForward,
WebHome,
WebRefresh,
WebSearch,
WebStop,
Yen,
Copy,
Paste,
Cut,
Unknown,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum MouseButton {
Button1,
Button2,
Button3,
Button4,
Button5,
Button6,
Button7,
Button8,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Action {
Release,
Press,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TouchAction {
Start,
End,
Move,
Cancel,
}
bitflags! {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Modifiers: i32 {
const Shift = 0b0001;
const Control = 0b0010;
const Alt = 0b0100;
const Super = 0b1000;
}
}