1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! Events from the event loop.

use std::path::PathBuf;

use crate::prelude::*;
#[cfg(feature = "egui")]
use egui_winit_vulkano::egui::Context;
pub use winit::event::{ElementState, MouseButton, VirtualKeyCode};

/// Describes an event coming from the event loop.
#[derive(Debug, Clone)]
pub enum Event {
    /// The EGUI context.
    #[cfg(feature = "egui")]
    Egui(Context),
    /// Events that happened to the window.
    Window(WindowEvent),
    /// Input events.
    Input(InputEvent),
    /// Update event that happens before every frame.
    Update,
    /// Update that happens after every frame has been drawn.
    FrameUpdate,
    /// The last event to be called in this loop.
    /// This is the "do on quit" event.
    Destroyed,
    /// The loop has started.
    Ready,
}

/// An event coming with window context.
#[derive(Debug, Clone)]
pub enum WindowEvent {
    /// In case the window has been resized the new size is given here.
    Resized(PhysicalSize<u32>),
    /// The window has been requested to close.
    /// Happens when the X button gets pressed on the title bar, the X gets pressed in the task bar, the Alt f4 combination gets pressed or any other ways to request a close to the window.
    CloseRequested,
    /// The window no more.
    Destroyed,
    /// A file is getting hovered over the window.
    ///
    /// This event happens separately for every file in case a multitude of files have been dragged in.
    HoveredFile(PathBuf),
    /// The file has been dropped inside the window.
    ///
    /// This event happens separately for every file in case a multitude of files have been dragged in.
    DroppedFile(PathBuf),
    /// In case files have been hovered over the window but have left the window without getting dropped in.
    ///
    /// This event gets called once, no matter how many files were hovered over the window.
    HoveredFileCancelled,
    /// `True` if the window was focused.
    /// `False` if it lost focus.
    Focused(bool),
    /// The cursor has entered the window.
    CursorEntered,
    /// The cursor has left the window.
    CursorLeft,
    /// THe cursor has moved on the window.
    ///
    /// Cursor position in pixels relative the the top left corner of the screen.
    CursorMoved(PhysicalPosition<f64>),
    /// Mouse scroll event on the window.
    MouseWheel(ScrollDelta),
}

/// An event coming from device input.
#[derive(Debug, Clone)]
pub enum InputEvent {
    /// Raw mouse motion in delta.
    ///
    /// The given units may be different depending on the device.
    MouseMotion(Vec2),
    /// Mouse scroll event.
    MouseWheel(ScrollDelta),
    /// A mouse button was pressed.
    MouseInput(MouseButton, ElementState),
    /// A unicode character was received by the window.
    ReceivedCharacter(char),
    /// Input by the keyboard.
    KeyboardInput { input: KeyboardInput },
    /// The modifiers were changed.
    /// Gets called when either shift, ctrl, alt or the super key get pressed.
    ///
    /// The changes can be taken from the [INPUT](Input) struct.
    ModifiersChanged,
}

/// The delta of a mouse scroll.
#[derive(Debug, Clone)]
pub enum ScrollDelta {
    LineDelta(Vec2),
    /// A scroll with exact pixels to be moved.
    PixelDelta(PhysicalPosition<f64>),
}

/// Input received from the keyboard.
#[derive(Debug, Clone)]
pub struct KeyboardInput {
    /// Hardware dependent scancode.
    ///
    /// Does not change when you change the keyboard map.
    /// Only keeps track of the physical keyboard key scancodes.
    pub scancode: u32,
    /// The meaning of the key.
    pub keycode: Option<VirtualKeyCode>,
    pub state: ElementState,
}