1use std::any::Any;
4
5use crate::{
6 AfterRenderEvent, ButtonEvent, CloseEvent, ControllerAxisEvent, CursorEvent, Event, EventId,
7 FocusEvent, IdleEvent, Input, Loop, Motion, MouseCursorEvent, MouseRelativeEvent,
8 MouseScrollEvent, PressEvent, ReleaseEvent, RenderEvent, ResizeEvent, TextEvent, TimeStamp,
9 TouchEvent, UpdateEvent,
10};
11
12pub trait GenericEvent:
23 Sized
24 + AfterRenderEvent
25 + CloseEvent
26 + ControllerAxisEvent
27 + CursorEvent
28 + FocusEvent
29 + IdleEvent
30 + MouseCursorEvent
31 + MouseRelativeEvent
32 + MouseScrollEvent
33 + ButtonEvent
34 + PressEvent
35 + ReleaseEvent
36 + RenderEvent
37 + ResizeEvent
38 + TextEvent
39 + TouchEvent
40 + UpdateEvent
41 + From<Input>
42 + From<Loop>
43 + Into<Option<Input>>
44 + Into<Option<Loop>>
45{
46 fn event_id(&self) -> EventId;
48 fn with_args<F, U>(&'_ self, f: F) -> U
50 where
51 F: FnMut(&dyn Any) -> U;
52 fn time_stamp(&self) -> Option<TimeStamp>;
56}
57
58impl GenericEvent for Event {
59 fn event_id(&self) -> EventId {
60 use crate::event_id::*;
61
62 match *self {
63 Event::Input(Input::Cursor(_), _) => CURSOR,
64 Event::Input(Input::Focus(_), _) => FOCUS,
65 Event::Input(Input::Close(_), _) => CLOSE,
66 Event::Input(Input::Move(Motion::MouseCursor(_)), _) => MOUSE_CURSOR,
67 Event::Input(Input::Move(Motion::MouseRelative(_)), _) => MOUSE_RELATIVE,
68 Event::Input(Input::Move(Motion::MouseScroll(_)), _) => MOUSE_SCROLL,
69 Event::Input(Input::Move(Motion::ControllerAxis(_)), _) => CONTROLLER_AXIS,
70 Event::Input(Input::Move(Motion::Touch(_)), _) => TOUCH,
71 Event::Input(Input::Button(_), _) => BUTTON,
72 Event::Input(Input::Resize(_), _) => RESIZE,
73 Event::Input(Input::Text(_), _) => TEXT,
74 Event::Input(Input::FileDrag(_), _) => FILE_DRAG,
75 Event::Loop(Loop::Update(_)) => UPDATE,
76 Event::Loop(Loop::Render(_)) => RENDER,
77 Event::Loop(Loop::AfterRender(_)) => AFTER_RENDER,
78 Event::Loop(Loop::Idle(_)) => IDLE,
79 Event::Custom(event_id, _, _) => event_id,
80 }
81 }
82
83 fn with_args<F, U>(&'_ self, mut f: F) -> U
84 where
85 F: FnMut(&dyn Any) -> U,
86 {
87 match *self {
88 Event::Input(Input::Cursor(cursor), _) => f(&cursor as &dyn Any),
89 Event::Input(Input::Focus(focused), _) => f(&focused as &dyn Any),
90 Event::Input(Input::Close(ref args), _) => f(args as &dyn Any),
91 Event::Input(Input::Move(Motion::ControllerAxis(args)), _) => f(&args as &dyn Any),
92 Event::Input(Input::Move(Motion::MouseCursor(pos)), _) => f(&pos as &dyn Any),
93 Event::Input(Input::Move(Motion::MouseRelative(pos)), _) => f(&pos as &dyn Any),
94 Event::Input(Input::Move(Motion::MouseScroll(pos)), _) => f(&pos as &dyn Any),
95 Event::Input(Input::Move(Motion::Touch(args)), _) => f(&args as &dyn Any),
96 Event::Input(Input::Button(ref args), _) => f(args as &dyn Any),
97 Event::Input(Input::Resize(ref args), _) => f(args as &dyn Any),
98 Event::Input(Input::Text(ref text), _) => f(text as &dyn Any),
99 Event::Input(Input::FileDrag(ref file_drag), _) => f(file_drag as &dyn Any),
100 Event::Loop(Loop::Update(ref args)) => f(args as &dyn Any),
101 Event::Loop(Loop::Render(ref args)) => f(args as &dyn Any),
102 Event::Loop(Loop::AfterRender(ref args)) => f(args as &dyn Any),
103 Event::Loop(Loop::Idle(ref args)) => f(args as &dyn Any),
104 Event::Custom(_, ref args, _) => f(args),
105 }
106 }
107
108 fn time_stamp(&self) -> Option<TimeStamp> {
109 match self {
110 Event::Input(_, x) => *x,
111 Event::Loop(_) => None,
112 Event::Custom(_, _, x) => *x,
113 }
114 }
115}