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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//! Hit zone system for mouse event dispatch.
//!
//! Provides `HitZone` (with callbacks), `HitZoneGroup` (multi-zone dispatcher),
//! `ScopedZone` (simple geometry-only zone), and `ScopedZoneRegistry`
//! for per-frame scoped hit testing.
use crate::input::event::{KeyModifiers, MouseButton, MouseEventKind};
use std::time::{Duration, Instant};
/// Describes the multiplicity of a click sequence.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClickKind {
/// A single click.
Single,
/// A double click within the timeout window.
Double,
/// A triple click within the timeout window.
Triple,
}
/// Describes a drag interaction and its phase.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DragState {
/// Drag started at the given coordinates.
/// Drag started at the given coordinates.
Started {
/// X coordinate.
x: u16,
/// Y coordinate.
y: u16,
},
/// Drag moved to the given coordinates.
/// Drag moved to the given coordinates.
Moved {
/// X coordinate.
x: u16,
/// Y coordinate.
y: u16,
},
/// Drag ended at the given coordinates.
/// Drag ended at the given coordinates.
Ended {
/// X coordinate.
x: u16,
/// Y coordinate.
y: u16,
},
}
/// A rectangular interactive zone with typed ID and mouse/drag callbacks.
pub struct HitZone<T> {
/// Arbitrary ID identifying this zone.
pub id: T,
/// Left edge column.
pub x: u16,
/// Top edge row.
pub y: u16,
/// Width in columns.
pub width: u16,
/// Height in rows.
pub height: u16,
on_click: Option<Box<dyn FnMut(ClickKind)>>,
on_right_click: Option<Box<dyn FnMut()>>,
on_drag_start: Option<Box<dyn FnMut(DragState)>>,
on_drag_move: Option<Box<dyn FnMut(DragState)>>,
on_drag_end: Option<Box<dyn FnMut(DragState)>>,
double_click_timeout: Duration,
last_click_time: Option<Instant>,
last_click_pos: Option<(u16, u16)>,
click_count: u8,
drag_active: bool,
}
impl<T: Clone + 'static> HitZone<T> {
/// Creates a new `HitZone` with the given id and geometry.
pub fn new(id: T, x: u16, y: u16, width: u16, height: u16) -> Self {
Self {
id,
x,
y,
width,
height,
on_click: None,
on_right_click: None,
on_drag_start: None,
on_drag_move: None,
on_drag_end: None,
double_click_timeout: Duration::from_millis(500),
last_click_time: None,
last_click_pos: None,
click_count: 0,
drag_active: false,
}
}
/// Registers a callback invoked on left-click with the click multiplicity.
pub fn on_click(mut self, f: impl FnMut(ClickKind) + 'static) -> Self {
self.on_click = Some(Box::new(f));
self
}
/// Registers a callback invoked on right-click.
pub fn on_right_click(mut self, f: impl FnMut() + 'static) -> Self {
self.on_right_click = Some(Box::new(f));
self
}
/// Registers a callback invoked when drag starts within this zone.
pub fn on_drag_start(mut self, f: impl FnMut(DragState) + 'static) -> Self {
self.on_drag_start = Some(Box::new(f));
self
}
/// Registers a callback invoked when drag moves within this zone.
pub fn on_drag_move(mut self, f: impl FnMut(DragState) + 'static) -> Self {
self.on_drag_move = Some(Box::new(f));
self
}
/// Registers a callback invoked when drag ends within this zone.
pub fn on_drag_end(mut self, f: impl FnMut(DragState) + 'static) -> Self {
self.on_drag_end = Some(Box::new(f));
self
}
/// Returns `true` if `(col, row)` lies within this zone's rectangle.
pub fn contains(&self, col: u16, row: u16) -> bool {
col >= self.x
&& col < self.x.saturating_add(self.width)
&& row >= self.y
&& row < self.y.saturating_add(self.height)
}
/// Routes a mouse event to the appropriate callback.
/// Automatically detects single/double/triple click.
pub fn handle_mouse(
&mut self,
kind: MouseEventKind,
col: u16,
row: u16,
_modifiers: KeyModifiers,
) {
if !self.contains(col, row) {
return;
}
match kind {
MouseEventKind::Down(btn) => {
if btn == MouseButton::Right {
if let Some(f) = self.on_right_click.as_mut() {
f();
}
return;
}
if btn != MouseButton::Left {
return;
}
let now = Instant::now();
let is_double = self
.last_click_time
.zip(self.last_click_pos)
.map(|(t, (px, py))| {
t.elapsed() < self.double_click_timeout
&& (px as i32 - col as i32).abs() <= 1
&& (py as i32 - row as i32).abs() <= 1
})
.unwrap_or(false);
self.click_count = if is_double { 2 } else { 1 };
self.last_click_time = Some(now);
self.last_click_pos = Some((col, row));
let kind = match self.click_count {
1 => ClickKind::Single,
2 => ClickKind::Double,
_ => ClickKind::Triple,
};
if let Some(f) = self.on_click.as_mut() {
f(kind);
}
if self.on_drag_start.is_some() {
self.drag_active = true;
if let Some(f) = self.on_drag_start.as_mut() {
f(DragState::Started { x: col, y: row });
}
}
}
MouseEventKind::Drag(_) if self.drag_active => {
if let Some(f) = self.on_drag_move.as_mut() {
f(DragState::Moved { x: col, y: row });
}
}
MouseEventKind::Up(_) if self.drag_active => {
self.drag_active = false;
if let Some(f) = self.on_drag_end.as_mut() {
f(DragState::Ended { x: col, y: row });
}
}
_ => {}
}
}
/// Like `handle_mouse` but returns `Some(self.id.clone())` if the event hit this zone.
pub fn dispatch_mouse(
&mut self,
kind: MouseEventKind,
col: u16,
row: u16,
modifiers: KeyModifiers,
) -> Option<T> {
if !self.contains(col, row) {
return None;
}
self.handle_mouse(kind, col, row, modifiers);
Some(self.id.clone())
}
}
/// A group of `HitZone`s that dispatches to the first matching zone.
pub struct HitZoneGroup<T> {
zones: Vec<HitZone<T>>,
}
impl<T: Clone + 'static> HitZoneGroup<T> {
/// Creates an empty group.
pub fn new() -> Self {
Self { zones: Vec::new() }
}
/// Adds a pre-built `HitZone` to the group.
pub fn zone(mut self, zone: HitZone<T>) -> Self {
self.zones.push(zone);
self
}
/// Convenience: adds a full-width single-row zone with a click callback.
pub fn add_row(&mut self, id: T, y: u16, width: u16, f: impl FnMut(ClickKind) + 'static) {
let zone = HitZone::new(id, 0, y, width, 1).on_click(f);
self.zones.push(zone);
}
/// Returns a slice of all zones.
pub fn zones(&self) -> &[HitZone<T>] {
&self.zones
}
/// Returns a mutable reference to the zone vector.
pub fn zones_mut(&mut self) -> &mut Vec<HitZone<T>> {
&mut self.zones
}
/// Iterates zones in order and dispatches to the first containing the event point.
/// Returns `Some(id)` of the hit zone, or `None`.
pub fn dispatch_mouse(
&mut self,
kind: MouseEventKind,
col: u16,
row: u16,
modifiers: KeyModifiers,
) -> Option<T> {
for zone in self.zones.iter_mut() {
if zone.contains(col, row) {
zone.handle_mouse(kind, col, row, modifiers);
return Some(zone.id.clone());
}
}
None
}
}
/// A lightweight rectangular zone with only geometry and an ID — no callbacks.
pub struct ScopedZone<T> {
/// Arbitrary ID identifying this zone.
pub id: T,
/// Left edge column.
pub x: u16,
/// Top edge row.
pub y: u16,
/// Width in columns.
pub width: u16,
/// Height in rows.
pub height: u16,
}
impl<T> ScopedZone<T> {
/// Creates a new `ScopedZone` with the given id and geometry.
pub fn new(id: T, x: u16, y: u16, width: u16, height: u16) -> Self {
Self {
id,
x,
y,
width,
height,
}
}
/// Returns `true` if `(col, row)` lies within this zone's rectangle.
pub fn contains(&self, col: u16, row: u16) -> bool {
col >= self.x
&& col < self.x.saturating_add(self.width)
&& row >= self.y
&& row < self.y.saturating_add(self.height)
}
}
/// A registry of `ScopedZone`s for bulk per-frame dispatch.
#[derive(Default)]
pub struct ScopedZoneRegistry<T> {
zones: Vec<ScopedZone<T>>,
}
impl<T: Clone + 'static> ScopedZoneRegistry<T> {
/// Creates an empty registry.
pub fn new() -> Self {
Self { zones: Vec::new() }
}
/// Removes all zones (call at the start of each frame).
pub fn clear(&mut self) {
self.zones.clear();
}
/// Adds a pre-built `ScopedZone`.
pub fn add(&mut self, zone: ScopedZone<T>) {
self.zones.push(zone);
}
/// Convenience: constructs and adds a `ScopedZone` from raw coordinates.
pub fn register(&mut self, id: T, x: u16, y: u16, width: u16, height: u16) {
self.zones.push(ScopedZone::new(id, x, y, width, height));
}
/// Returns `Some(id)` of the first zone containing `(col, row)`, or `None`.
pub fn dispatch(&self, col: u16, row: u16) -> Option<T> {
for zone in &self.zones {
if zone.contains(col, row) {
return Some(zone.id.clone());
}
}
None
}
/// Returns a slice of all registered zones.
pub fn zones(&self) -> &[ScopedZone<T>] {
&self.zones
}
}
impl<T: Clone + 'static> Default for HitZoneGroup<T> {
fn default() -> Self {
Self::new()
}
}