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
use std::any::Any;
use {AfterRenderEvent, ButtonEvent, CloseEvent, ControllerAxisEvent, CursorEvent, FocusEvent,
IdleEvent, MouseCursorEvent, MouseRelativeEvent, MouseScrollEvent, PressEvent, ReleaseEvent,
RenderEvent, ResizeEvent, TextEvent, TouchEvent, UpdateEvent};
use {Event, EventId, Input, Loop, Motion};
use {AFTER_RENDER, BUTTON, CONTROLLER_AXIS, CURSOR, FOCUS, CLOSE, IDLE, MOUSE_CURSOR, MOUSE_RELATIVE,
MOUSE_SCROLL, RENDER, RESIZE, TEXT, TOUCH, UPDATE};
pub trait GenericEvent: Sized +
AfterRenderEvent + CloseEvent + ControllerAxisEvent + CursorEvent + FocusEvent + IdleEvent +
MouseCursorEvent + MouseRelativeEvent + MouseScrollEvent + ButtonEvent + PressEvent +
ReleaseEvent + RenderEvent + ResizeEvent + TextEvent + TouchEvent + UpdateEvent +
From<Input> + From<Loop> + Into<Option<Input>> + Into<Option<Loop>>
{
fn event_id(&self) -> EventId;
fn with_args<'a, F, U>(&'a self, f: F) -> U
where F: FnMut(&Any) -> U
;
}
impl GenericEvent for Event {
fn event_id(&self) -> EventId {
match *self {
Event::Input(Input::Cursor(_)) => CURSOR,
Event::Input(Input::Focus(_)) => FOCUS,
Event::Input(Input::Close(_)) => CLOSE,
Event::Input(Input::Move(Motion::MouseCursor(_, _))) => MOUSE_CURSOR,
Event::Input(Input::Move(Motion::MouseRelative(_, _))) => MOUSE_RELATIVE,
Event::Input(Input::Move(Motion::MouseScroll(_, _))) => MOUSE_SCROLL,
Event::Input(Input::Move(Motion::ControllerAxis(_))) => CONTROLLER_AXIS,
Event::Input(Input::Move(Motion::Touch(_))) => TOUCH,
Event::Input(Input::Button(_)) => BUTTON,
Event::Input(Input::Resize(_, _)) => RESIZE,
Event::Input(Input::Text(_)) => TEXT,
Event::Loop(Loop::Update(_)) => UPDATE,
Event::Loop(Loop::Render(_)) => RENDER,
Event::Loop(Loop::AfterRender(_)) => AFTER_RENDER,
Event::Loop(Loop::Idle(_)) => IDLE,
Event::Custom(event_id, _) => event_id,
}
}
fn with_args<'a, F, U>(&'a self, mut f: F) -> U
where F: FnMut(&Any) -> U
{
match *self {
Event::Input(Input::Cursor(cursor)) => f(&cursor as &Any),
Event::Input(Input::Focus(focused)) => f(&focused as &Any),
Event::Input(Input::Close(ref args)) => f(args as &Any),
Event::Input(Input::Move(Motion::ControllerAxis(args))) => f(&args as &Any),
Event::Input(Input::Move(Motion::MouseCursor(x, y))) => f(&(x, y) as &Any),
Event::Input(Input::Move(Motion::MouseRelative(x, y))) => f(&(x, y) as &Any),
Event::Input(Input::Move(Motion::MouseScroll(x, y))) => f(&(x, y) as &Any),
Event::Input(Input::Move(Motion::Touch(args))) => f(&args as &Any),
Event::Input(Input::Button(ref args)) => f(args as &Any),
Event::Input(Input::Resize(w, h)) => f(&(w, h) as &Any),
Event::Input(Input::Text(ref text)) => f(text as &Any),
Event::Loop(Loop::Update(ref args)) => f(args as &Any),
Event::Loop(Loop::Render(ref args)) => f(args as &Any),
Event::Loop(Loop::AfterRender(ref args)) => f(args as &Any),
Event::Loop(Loop::Idle(ref args)) => f(args as &Any),
Event::Custom(_, ref args) => f(args),
}
}
}