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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#![deny(missing_docs)]
extern crate vecmath;
extern crate input;
use std::time::Instant;
use math::{Matrix2d, Rectangle};
use input::GenericEvent;
pub mod math;
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ButtonEvent {
MouseEnter,
MouseLeave,
Press,
Click,
Cancel,
}
pub struct ButtonController {
pub mouse_cursor_inside: bool,
pub was_inside: bool,
pub pressed: bool,
pub pressed_instant: Option<Instant>,
pub events: Vec<ButtonEvent>,
}
impl ButtonController {
pub fn new() -> ButtonController {
ButtonController {
mouse_cursor_inside: false,
was_inside: false,
pressed: false,
pressed_instant: None,
events: vec![],
}
}
pub fn event<E: GenericEvent>(&mut self, layout: Rectangle, transform: Matrix2d, e: &E) {
use math::is_inside;
use input::MouseButton;
self.events.clear();
if let Some(pos) = e.mouse_cursor_args() {
let inside = is_inside(pos, transform, layout);
if inside {
if !self.mouse_cursor_inside {
self.mouse_cursor_inside = true;
self.events.push(ButtonEvent::MouseEnter);
}
} else {
if self.mouse_cursor_inside {
self.mouse_cursor_inside = false;
self.events.push(ButtonEvent::MouseLeave);
}
}
}
if let Some(input::Button::Mouse(MouseButton::Left)) = e.press_args() {
if self.mouse_cursor_inside {
self.pressed = true;
self.was_inside = true;
self.events.push(ButtonEvent::Press);
self.pressed_instant = Some(Instant::now());
}
}
if let Some(input::Button::Mouse(MouseButton::Left)) = e.release_args() {
self.pressed = false;
if self.mouse_cursor_inside {
self.events.push(ButtonEvent::Click);
} else if self.was_inside {
self.events.push(ButtonEvent::Cancel);
}
self.was_inside = false;
}
}
pub fn touch_event<E: GenericEvent, S: Into<[u32; 2]>>(&mut self,
layout: Rectangle,
transform: Matrix2d,
window_size: S,
e: &E) {
use input::Touch;
use math::is_inside;
self.events.clear();
let window_size = window_size.into();
if let Some(args) = e.touch_args() {
let pos = args.position();
let pos = [pos[0] * window_size[0] as f64, pos[1] * window_size[1] as f64];
let inside = is_inside(pos, transform, layout);
match args.touch {
Touch::Start => {
if inside {
self.pressed = true;
self.was_inside = true;
self.mouse_cursor_inside = true;
self.events.push(ButtonEvent::Press);
self.pressed_instant = Some(Instant::now());
}
}
Touch::Move => {
if inside {
if !self.mouse_cursor_inside {
self.mouse_cursor_inside = true;
self.events.push(ButtonEvent::MouseEnter);
}
} else {
if self.mouse_cursor_inside {
self.mouse_cursor_inside = false;
self.events.push(ButtonEvent::MouseLeave);
}
}
}
Touch::End => {
self.pressed = false;
if self.mouse_cursor_inside {
self.events.push(ButtonEvent::Click);
} else if self.was_inside {
self.events.push(ButtonEvent::Cancel);
}
self.was_inside = false;
self.mouse_cursor_inside = false;
}
Touch::Cancel => {}
}
}
}
pub fn appear_pressed(&self, pressed_duration_secs: f64) -> bool {
if let Some(ref instant) = self.pressed_instant {
if math::duration_to_secs(&instant.elapsed()) < pressed_duration_secs {
true
} else {
self.pressed
}
} else {
self.pressed
}
}
pub fn state(&self, pressed_duration_secs: f64) -> ButtonState {
ButtonVisual {
appear_pressed: self.appear_pressed(pressed_duration_secs),
mouse_cursor_inside: self.mouse_cursor_inside,
}.state()
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ButtonState {
Inactive,
Hover,
Press,
Cancel,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct ButtonVisual {
pub appear_pressed: bool,
pub mouse_cursor_inside: bool,
}
impl ButtonVisual {
pub fn state(&self) -> ButtonState {
use ButtonState as S;
match (self.appear_pressed, self.mouse_cursor_inside) {
(true, true) => S::Press,
(true, false) => S::Cancel,
(false, false) => S::Inactive,
(false, true) => S::Hover,
}
}
}