vtui_core/
events.rs

1use std::any::{Any, TypeId};
2
3use crate::input::{KeyCode, MouseButton, MouseScrollDirection};
4
5pub trait Event: Send + 'static {}
6
7pub struct Message {
8    event_type_id: TypeId,
9    event: Box<dyn Any + Send>,
10}
11
12impl<E: Event> From<E> for Message {
13    fn from(value: E) -> Self {
14        Self {
15            event_type_id: TypeId::of::<E>(),
16            event: Box::new(value),
17        }
18    }
19}
20
21impl Message {
22    pub fn new<E: Event>(event: E) -> Self {
23        Self::from(event)
24    }
25
26    pub(crate) fn event_type_id(&self) -> TypeId {
27        self.event_type_id
28    }
29
30    pub(crate) fn downcast_ref<E: Event>(&self) -> Option<&E> {
31        self.event.downcast_ref::<E>()
32    }
33}
34
35pub struct Tick {}
36
37impl Event for Tick {}
38
39pub struct MouseDown {
40    pub x: u16,
41    pub y: u16,
42    pub button: MouseButton,
43}
44
45impl Event for MouseDown {}
46
47pub struct MouseUp {
48    pub x: u16,
49    pub y: u16,
50    pub button: MouseButton,
51}
52
53impl Event for MouseUp {}
54
55pub struct MouseHover {
56    pub x: u16,
57    pub y: u16,
58}
59
60impl Event for MouseHover {}
61
62pub struct MouseDrag {
63    pub x: u16,
64    pub y: u16,
65    pub button: MouseButton,
66}
67
68impl Event for MouseDrag {}
69
70pub struct MouseScroll {
71    pub x: u16,
72    pub y: u16,
73    pub direction: MouseScrollDirection,
74}
75
76impl Event for MouseScroll {}
77
78pub struct KeyPress {
79    pub key: KeyCode,
80}
81
82impl Event for KeyPress {}
83
84pub struct KeyRepeat {
85    pub key: KeyCode,
86}
87
88impl Event for KeyRepeat {}
89
90pub struct KeyRelease {
91    pub key: KeyCode,
92}
93
94impl Event for KeyRelease {}
95
96pub struct Resize {
97    pub width: u16,
98    pub height: u16,
99}
100
101impl Event for Resize {}