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
pub mod mouse;
pub mod keyboard;
pub mod drag;
use glutin;
use glutin::ElementState;
use webrender;
use event::{EventHandler, EventArgs};
use input::mouse::{MouseMoved, MouseButton, MouseWheel, CursorLeftWindow};
use input::keyboard::{KeyboardInput, ReceivedCharacter};
use geometry::Point;
use app::App;
#[derive(Clone)]
pub struct InputEvent(pub glutin::WindowEvent);
impl App {
pub fn add_input_handlers(&mut self) {
self.add_handler(|event: &InputEvent, args: EventArgs| {
let InputEvent(event) = event.clone();
match event {
glutin::WindowEvent::Closed => {
args.ui.close();
}
glutin::WindowEvent::MouseWheel { delta, .. } => {
args.widget.event(MouseWheel(delta));
}
glutin::WindowEvent::MouseInput { state, button, .. } => {
args.widget.event(MouseButton(state, button));
}
glutin::WindowEvent::CursorMoved { position, .. } => {
let point = Point::new(position.0 as f32, position.1 as f32);
args.widget.event(MouseMoved(point));
}
glutin::WindowEvent::CursorLeft { .. } => {
args.widget.event(CursorLeftWindow);
}
glutin::WindowEvent::KeyboardInput { input, .. } => {
let key_input = KeyboardInput(input);
args.widget.event(key_input);
}
glutin::WindowEvent::ReceivedCharacter(char) => {
args.widget.event(ReceivedCharacter(char));
}
_ => (),
}
});
}
}
#[derive(Debug, Copy, Clone)]
pub struct EscKeyCloseHandler;
impl EventHandler<KeyboardInput> for EscKeyCloseHandler {
fn handle(&mut self, event: &KeyboardInput, args: EventArgs) {
if let Some(glutin::VirtualKeyCode::Escape) = event.0.virtual_keycode {
args.ui.close();
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct DebugSettingsHandler {
debug_on: bool
}
impl Default for DebugSettingsHandler {
fn default() -> Self {
DebugSettingsHandler {
debug_on: false,
}
}
}
impl DebugSettingsHandler {
pub fn new() -> Self {
Self::default()
}
}
impl EventHandler<KeyboardInput> for DebugSettingsHandler {
fn handle(&mut self, event: &KeyboardInput, args: EventArgs) {
let ui = args.ui;
let &KeyboardInput(input) = event;
if ElementState::Released == input.state {
match input.virtual_keycode {
Some(glutin::VirtualKeyCode::F1) => {
self.debug_on = !self.debug_on;
ui.set_debug_draw_bounds(self.debug_on);
},
Some(glutin::VirtualKeyCode::F2) => ui.solver.debug_constraints(),
Some(glutin::VirtualKeyCode::F3) => ui.debug_widget_positions(),
Some(glutin::VirtualKeyCode::F4) => ui.solver.debug_variables(),
Some(glutin::VirtualKeyCode::F5) => ui.render.toggle_flags(webrender::DebugFlags::PROFILER_DBG),
Some(glutin::VirtualKeyCode::F6) => ui.print_widgets(),
_ => {}
}
}
}
}