diff options
Diffstat (limited to 'src/events.rs')
-rw-r--r-- | src/events.rs | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/events.rs b/src/events.rs new file mode 100644 index 0000000..b738e5b --- /dev/null +++ b/src/events.rs @@ -0,0 +1,115 @@ +use crate::*; + +use winit::dpi::PhysicalSize; +use winit::event::ElementState; +use winit::keyboard::KeyCode; +use winit::window::CursorIcon; + +use std::path::PathBuf; + + +pub enum Request { + SetTitle(String), + SetSize(Dimensions), + SetSizeBounds(SizeBounds), + SetResizable(bool), + SetFullscreen(bool), + SetVisible(bool), + SetPixelScale(u32), + SetCursor(Option<CursorIcon>), + Redraw, + CreateWindow(WindowBuilder), + CloseWindow, +} + + +#[derive(Debug)] +pub enum Event { + Initialise, + CloseRequest, + Close, + Resize(Dimensions), + FocusChange(bool), + CursorEnter, + CursorExit, + CursorMove(Position), + ScrollLines { axis: Axis, distance: f32 }, + ScrollPixels { axis: Axis, distance: f32 }, + MouseButton { button: MouseButton, action: Action }, + KeyboardInput { key: KeyCode, action: Action }, + CharacterInput(char), + ModifierChange(ModifiersState), + FileDrop(PathBuf), +} + + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum Action { + Pressed, + Released +} + +impl Action { + pub fn is_pressed(self) -> bool { + self == Action::Pressed + } +} + +impl From<ElementState> for Action { + fn from(value: ElementState) -> Self { + match value { + ElementState::Pressed => Action::Pressed, + ElementState::Released => Action::Released, + } + } +} + + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum Axis { + Horizontal, + Vertical, +} + + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum MouseButton { + Left, + Middle, + Right, +} + + +#[derive(Copy, Clone, Debug, PartialEq)] +pub struct SizeBounds { + pub min_width: Option<u32>, + pub max_width: Option<u32>, + pub min_height: Option<u32>, + pub max_height: Option<u32>, +} + +impl SizeBounds { + pub fn as_min_max_size(&self, scale: u32) -> (PhysicalSize<u32>, PhysicalSize<u32>) { + ( + PhysicalSize { + width: self.min_width.unwrap_or(0).saturating_mul(scale), + height: self.min_height.unwrap_or(0).saturating_mul(scale), + }, + PhysicalSize { + width: self.max_width.unwrap_or(u32::MAX).saturating_mul(scale), + height: self.max_height.unwrap_or(u32::MAX).saturating_mul(scale), + }, + ) + } +} + +impl Default for SizeBounds { + fn default() -> Self { + Self { + min_width: None, + max_width: None, + min_height: None, + max_height: None, + } + } +} |