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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
use crate::default_style;
use crate::{
EditorEntity,
brush::{BrushFaceEntity, BrushMeshCache},
brush_drag_ops::cursor_over_brush_face,
gizmos::handle_gizmo_hover,
selection::Selection,
viewport::{InteractionGuards, SceneViewport, ViewportCursor},
viewport_util::window_to_viewport_cursor_for,
};
use bevy::input_focus::InputFocus;
use bevy::ui::ui_transform::UiGlobalTransform;
use bevy::{
picking::mesh_picking::ray_cast::{MeshRayCast, MeshRayCastSettings, RayCastVisibility},
picking::prelude::Pickable,
prelude::*,
};
use bevy_enhanced_input::prelude::{Press, *};
use jackdaw_api::prelude::*;
use jackdaw_jsn::{Brush, BrushGroup};
/// Marker for the box-select visual overlay node.
#[derive(Component)]
struct BoxSelectOverlay;
pub struct ViewportSelectPlugin;
impl Plugin for ViewportSelectPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<BoxSelectState>()
.init_resource::<GroupEditState>()
.init_resource::<LastClick>()
.add_systems(
Update,
(
handle_viewport_click.after(handle_gizmo_hover),
box_select_pending_trigger,
box_select_promote_pending.after(box_select_pending_trigger),
update_box_select_overlay,
)
.in_set(crate::EditorInteractionSystems),
);
}
}
pub(crate) fn add_to_extension(ctx: &mut ExtensionContext) {
ctx.register_operator::<BoxSelectOp>()
.register_operator::<ViewportExitGroupEditOp>();
let ext = ctx.id();
ctx.spawn((
Action::<ViewportExitGroupEditOp>::new(),
ActionOf::<crate::core_extension::CoreExtensionInputContext>::new(ext),
bindings![(KeyCode::Escape, Press::default())],
));
}
fn group_edit_active(
group_edit: Res<GroupEditState>,
keybind_focus: crate::keybind_focus::KeybindFocus,
) -> bool {
!keybind_focus.is_typing() && group_edit.active_group.is_some()
}
/// Exit `BrushGroup` edit mode (entered via double-click on a group).
#[operator(
id = "viewport.exit_group_edit",
label = "Exit Group Edit",
description = "Stop editing the current brush group.",
is_available = group_edit_active,
)]
pub(crate) fn viewport_exit_group_edit(
_: In<OperatorParameters>,
mut group_edit: ResMut<GroupEditState>,
) -> OperatorResult {
group_edit.active_group = None;
OperatorResult::Finished
}
/// Cursor delta (in window pixels) that promotes a pending LMB-down
/// into an active box-select. Below this, the press is treated as a
/// plain click and `handle_viewport_click` keeps ownership of it.
pub const BOX_SELECT_DRAG_THRESHOLD: f32 = 5.0;
#[derive(Resource, Default)]
pub struct BoxSelectState {
pub active: bool,
pub start: Vec2,
pub current: Vec2,
/// Camera entity of the viewport the drag started in. Captured at
/// modal start so the operator keeps querying the same viewport
/// across frames even if the cursor wanders into a different one
/// (multi-viewport setups).
pub camera: Option<Entity>,
/// `SceneViewport` UI-node entity of the same viewport.
pub viewport: Option<Entity>,
/// Cursor position recorded at LMB-down before we know whether the
/// gesture is a click or a box-select drag. Cleared when promoted
/// to active or when LMB releases without crossing the threshold.
pub pending: Option<Vec2>,
}
impl BoxSelectState {
/// Begin an active box-select session, anchoring the rectangle at
/// the previously-pending press position recorded by the trigger
/// system if any, otherwise at `cursor_pos`.
pub fn activate(&mut self, cursor_pos: Vec2) {
let start = self.pending.take().unwrap_or(cursor_pos);
self.active = true;
self.start = start;
self.current = cursor_pos;
}
}
/// True when a cursor at `current` has moved far enough from `start`
/// to promote a pending press into an active box-select.
#[inline]
pub fn cursor_dragged_past_threshold(start: Vec2, current: Vec2) -> bool {
current.distance_squared(start) >= BOX_SELECT_DRAG_THRESHOLD * BOX_SELECT_DRAG_THRESHOLD
}
/// Tracks whether the user is editing inside a `BrushGroup` (entered via double-click).
#[derive(Resource, Default)]
pub struct GroupEditState {
/// The `BrushGroup` entity we're currently editing inside of.
pub active_group: Option<Entity>,
}
/// Tracks last click for double-click detection.
#[derive(Resource, Default)]
pub(crate) struct LastClick {
entity: Option<Entity>,
time: f64,
}
pub(crate) fn handle_viewport_click(
mouse: Res<ButtonInput<MouseButton>>,
keyboard: Res<ButtonInput<KeyCode>>,
vp: ViewportCursor,
scene_entities: Query<(Entity, &GlobalTransform), (Without<EditorEntity>, With<Transform>)>,
parents: Query<&ChildOf>,
brush_groups: Query<(), With<BrushGroup>>,
guards: InteractionGuards,
mut selection: ResMut<Selection>,
mut input_focus: ResMut<InputFocus>,
mut commands: Commands,
mut ray_cast: MeshRayCast,
(mut group_edit, mut last_click, time): (ResMut<GroupEditState>, ResMut<LastClick>, Res<Time>),
// One-frame memory of `draw_state.active`. `draw_brush.confirm` clears
// the state inline before this system runs, so the same mouse-press
// would otherwise fall through to `selection.clear()` and strip
// `Selected` from the just-spawned brush.
mut was_drawing: Local<bool>,
) {
let drawing_now = guards.draw_state.active.is_some();
let just_finished_draw = *was_drawing && !drawing_now;
*was_drawing = drawing_now;
// Don't select during gizmo drag, modal ops, viewport drag,
// brush edit mode, draw mode, or terrain sculpt mode. Physics
// mode IS allowed: the user needs to click-select entities to
// drag them in the physics tool.
//
// Plain LMB-down still fires here for the immediate click case;
// if the user starts dragging, `box_select_promote_pending`
// dispatches `BoxSelectOp` which then overrides whatever
// selection this handler set on press.
if !mouse.just_pressed(MouseButton::Left)
|| guards.gizmo_drag.active
|| guards.gizmo_hover.hovered_axis.is_some()
|| guards.modal.active.is_some()
|| guards.viewport_drag.active.is_some()
|| matches!(*guards.edit_mode, crate::brush::EditMode::BrushEdit(_))
|| drawing_now
|| just_finished_draw
|| matches!(
*guards.terrain_edit_mode,
crate::terrain::TerrainEditMode::Sculpt(_)
)
{
return;
}
let Ok(window) = vp.windows.single() else {
return;
};
let Some(cursor_pos) = window.cursor_position() else {
return;
};
// Bail when the cursor isn't over any viewport. Multi-viewport
// routing: the active viewport is whichever one the cursor is in.
let Some((vp_computed, vp_tf)) = vp.viewport() else {
return;
};
let scale = vp_computed.inverse_scale_factor();
let vp_pos = vp_tf.translation * scale;
let vp_size = vp_computed.size() * scale;
let vp_top_left = vp_pos - vp_size / 2.0;
let local_cursor = cursor_pos - vp_top_left;
// Clear input focus so keyboard shortcuts (G/R/S) work after viewport click
input_focus.0 = None;
let Some((camera, cam_tf)) = vp.camera() else {
return;
};
// Remap from UI-logical space to camera render-target space
let target_size = camera.logical_viewport_size().unwrap_or(vp_size);
let local_cursor = local_cursor * target_size / vp_size;
// Try mesh raycast first for accurate geometry-based selection
let mut best_entity = None;
if let Ok(ray) = camera.viewport_to_world(cam_tf, local_cursor) {
let settings = MeshRayCastSettings::default().with_visibility(RayCastVisibility::Any);
let hits = ray_cast.cast_ray(ray, &settings);
// Find the first hit that resolves to a scene entity (skip editor entities)
for (hit_entity, _) in hits {
if let Some(ancestor) = find_selectable_ancestor(
*hit_entity,
&scene_entities,
&parents,
&group_edit,
&brush_groups,
) {
best_entity = Some(ancestor);
break;
}
}
// If we'd select a different entity, but the current selection is also
// under the cursor (overlapping geometry), keep the current selection.
// This prevents re-selecting the original after Ctrl+D duplication.
if let Some(candidate) = best_entity
&& let Some(current_primary) = selection.primary()
&& candidate != current_primary
{
for (hit_entity, _) in hits {
if find_selectable_ancestor(
*hit_entity,
&scene_entities,
&parents,
&group_edit,
&brush_groups,
) == Some(current_primary)
{
return;
}
}
}
}
// Fall back to screen-space proximity for non-mesh entities (lights, empties)
if best_entity.is_none() {
let mut best_dist = 30.0_f32;
for (entity, global_tf) in &scene_entities {
let pos = global_tf.translation();
if let Ok(screen_pos) = camera.world_to_viewport(cam_tf, pos) {
let dist = (screen_pos - local_cursor).length();
if dist < best_dist {
best_dist = dist;
best_entity = Some(entity);
}
}
}
}
// Double-click detection: if same entity clicked within 400ms, enter group
let now = time.elapsed_secs_f64();
if let Some(entity) = best_entity {
let is_double_click = last_click.entity == Some(entity) && (now - last_click.time) < 0.4;
if is_double_click && brush_groups.contains(entity) {
// Double-click on a BrushGroup: enter group edit mode
group_edit.active_group = Some(entity);
last_click.entity = None;
last_click.time = 0.0;
return;
}
last_click.entity = Some(entity);
last_click.time = now;
let ctrl = keyboard.any_pressed([KeyCode::ControlLeft, KeyCode::ControlRight]);
let in_physics_mode = *guards.edit_mode == crate::brush::EditMode::Physics;
if in_physics_mode {
// In Physics mode: clicking an already-selected entity is a drag
// start, NOT a re-select. Only modify selection for unselected
// entities (add them). This preserves multi-selection.
if !selection.is_selected(entity) {
if ctrl {
selection.toggle(&mut commands, entity);
} else {
selection.select_single(&mut commands, entity);
}
}
} else if ctrl {
selection.toggle(&mut commands, entity);
} else {
selection.select_single(&mut commands, entity);
}
} else {
last_click.entity = None;
last_click.time = 0.0;
// Clicked on empty space -- exit group edit and deselect all (unless Ctrl held)
if group_edit.active_group.is_some() {
group_edit.active_group = None;
}
let ctrl = keyboard.any_pressed([KeyCode::ControlLeft, KeyCode::ControlRight]);
if !ctrl {
selection.clear(&mut commands);
}
}
}
/// LMB-down records a pending box-select start position. The press
/// stays pending until either the cursor crosses
/// [`BOX_SELECT_DRAG_THRESHOLD`] (promoted to active by
/// [`box_select_promote_pending`]) or LMB releases without movement
/// (cleared, leaving `handle_viewport_click` to handle the click as
/// a single-select). Sit outside the BEI keybind menu because
/// drag gestures aren't expressible as BEI key actions.
///
/// Yields to face-drag when the cursor is over a face of the
/// selected brush; without that guard box-select would race
/// face-drag because face-drag's hit-test runs inside its operator
/// a frame later. See `cursor_over_brush_face`.
fn box_select_pending_trigger(
mouse: Res<ButtonInput<MouseButton>>,
vp: ViewportCursor,
guards: InteractionGuards,
mut box_state: ResMut<BoxSelectState>,
viewport_query: Query<(&ComputedNode, &UiGlobalTransform), With<SceneViewport>>,
selection: Res<Selection>,
brushes: Query<(), With<Brush>>,
face_entities: Query<(Entity, &BrushFaceEntity, &GlobalTransform)>,
brush_caches: Query<&BrushMeshCache>,
) {
if box_state.active
|| box_state.pending.is_some()
|| !mouse.just_pressed(MouseButton::Left)
|| guards.gizmo_drag.active
// The gizmo invoke-trigger queues the drag operator via
// `commands.queue`, so `gizmo_drag.active` doesn't flip until
// after this Update frame ends. Without checking the hover
// state here the press both arms a pending box-select and
// starts the gizmo drag, so the marquee draws while the user
// is dragging the gizmo.
|| guards.gizmo_hover.hovered_axis.is_some()
|| matches!(*guards.edit_mode, crate::brush::EditMode::BrushEdit(_))
|| guards.draw_state.active.is_some()
|| guards.modal.active.is_some()
{
return;
}
let Ok(window) = vp.windows.single() else {
return;
};
let Some(cursor_pos) = window.cursor_position() else {
return;
};
// Bail when the cursor isn't over a viewport panel. Without this
// any LMB press anywhere in the editor (toolbar, panel header,
// tab being dragged) records a pending box-select and the
// overlay then renders across the whole window during the drag.
let Some((camera, cam_tf)) = vp.camera() else {
return;
};
let Some(viewport_entity) = vp.viewport_entity() else {
return;
};
// Yield to face-drag when the cursor is over a face of the
// selected brush. Routes through `vp` so we hit-test against the
// viewport the cursor is actually over, not a hard-coded main
// camera (multi-viewport setups can have several scene cameras).
if let Some(brush_entity) = selection.primary().filter(|&e| brushes.contains(e))
&& let Some(viewport_cursor) =
window_to_viewport_cursor_for(cursor_pos, camera, viewport_entity, &viewport_query)
&& cursor_over_brush_face(
brush_entity,
viewport_cursor,
camera,
cam_tf,
&face_entities,
&brush_caches,
)
{
return;
}
box_state.pending = Some(cursor_pos);
}
/// Promotes a pending LMB-down to an active box-select once the
/// cursor moves past [`BOX_SELECT_DRAG_THRESHOLD`]. Clears the
/// pending state on LMB release without movement (so the press
/// resolves as a plain click instead).
fn box_select_promote_pending(
mouse: Res<ButtonInput<MouseButton>>,
vp: ViewportCursor,
guards: InteractionGuards,
mut box_state: ResMut<BoxSelectState>,
mut commands: Commands,
) {
let Some(start) = box_state.pending else {
return;
};
if !mouse.pressed(MouseButton::Left) {
box_state.pending = None;
return;
}
// A modal that started in the same frame as the press (gizmo
// drag, viewport drag, transform shortcut, draw brush) won't have
// shown up in the trigger system's guard check, but it has by
// now. Drop the pending press so we don't dispatch a competing
// box-select on top of the active gesture.
if guards.gizmo_drag.active
|| guards.viewport_drag.active.is_some()
|| guards.modal.active.is_some()
|| guards.draw_state.active.is_some()
|| matches!(*guards.edit_mode, crate::brush::EditMode::BrushEdit(_))
{
box_state.pending = None;
return;
}
let Ok(window) = vp.windows.single() else {
return;
};
let Some(cursor_pos) = window.cursor_position() else {
return;
};
if cursor_dragged_past_threshold(start, cursor_pos) {
commands.queue(|world: &mut World| {
if let Err(err) = world.operator(BoxSelectOp::ID).call() {
error!("box-select dispatch failed: {err}");
}
});
}
}
#[operator(
id = "selection.box_select",
label = "Box Select",
description = "Drag a rectangle to select entities inside it.",
modal = true,
cancel = cancel_box_select,
)]
pub fn box_select(
_: In<OperatorParameters>,
mouse: Res<ButtonInput<MouseButton>>,
vp: ViewportCursor,
mut box_state: ResMut<BoxSelectState>,
scene_entities: Query<(Entity, &GlobalTransform), (Without<EditorEntity>, With<Name>)>,
mut selection: ResMut<Selection>,
mut commands: Commands,
active: ActiveModalQuery,
) -> OperatorResult {
let Ok(window) = vp.windows.single() else {
return OperatorResult::Cancelled;
};
let Some(cursor_pos) = window.cursor_position() else {
return OperatorResult::Cancelled;
};
if !active.is_modal_running() {
// Honour the press-down position recorded by
// `box_select_pending_trigger` so the rectangle anchors at
// the original click rather than where the threshold tripped.
box_state.activate(cursor_pos);
// Capture the viewport that owns this drag so subsequent
// frames keep referring to it even if the cursor wanders
// into a different viewport mid-drag.
box_state.camera = vp.camera_entity();
box_state.viewport = vp.viewport_entity();
return OperatorResult::Running;
}
box_state.current = cursor_pos;
if !mouse.just_released(MouseButton::Left) {
return OperatorResult::Running;
}
box_state.active = false;
let Some(camera_entity) = box_state.camera else {
return OperatorResult::Finished;
};
let Some(viewport_entity) = box_state.viewport else {
return OperatorResult::Finished;
};
let Some((camera, cam_tf)) = vp.camera_for(camera_entity) else {
return OperatorResult::Finished;
};
let Some((vp_computed, vp_tf)) = vp.viewport_for(viewport_entity) else {
return OperatorResult::Finished;
};
let map = crate::viewport_util::ViewportRemap::new(camera, vp_computed, vp_tf);
let start_local = box_state.start - map.top_left;
let current_local = box_state.current - map.top_left;
let min = start_local.min(current_local) * map.remap;
let max = start_local.max(current_local) * map.remap;
let selected: Vec<Entity> = scene_entities
.iter()
.filter_map(|(entity, tf)| {
let screen = camera.world_to_viewport(cam_tf, tf.translation()).ok()?;
(screen.x >= min.x && screen.x <= max.x && screen.y >= min.y && screen.y <= max.y)
.then_some(entity)
})
.collect();
if !selected.is_empty() {
selection.select_multiple(&mut commands, &selected);
}
OperatorResult::Finished
}
fn cancel_box_select(mut box_state: ResMut<BoxSelectState>) {
box_state.active = false;
box_state.pending = None;
}
fn update_box_select_overlay(
box_state: Res<BoxSelectState>,
overlay_query: Query<Entity, With<BoxSelectOverlay>>,
mut commands: Commands,
) {
if box_state.active {
let min = box_state.start.min(box_state.current);
let max = box_state.start.max(box_state.current);
let size = max - min;
let node = (
BoxSelectOverlay,
Node {
position_type: PositionType::Absolute,
left: Val::Px(min.x),
top: Val::Px(min.y),
width: Val::Px(size.x),
height: Val::Px(size.y),
border: UiRect::all(Val::Px(1.0)),
..default()
},
BackgroundColor(default_style::SELECTION_MARQUEE_BG),
BorderColor::all(default_style::SELECTION_MARQUEE_BORDER),
GlobalZIndex(50),
Pickable::IGNORE,
);
if let Some(entity) = overlay_query.iter().next() {
commands.entity(entity).insert(node);
} else {
commands.spawn(node);
}
} else {
for entity in &overlay_query {
commands.entity(entity).despawn();
}
}
}
/// Walk up the `ChildOf` hierarchy from a raycast hit entity to find the
/// top-level scene entity (one that appears in `scene_entities`).
/// Handles GLTF child meshes and brush face children.
///
/// When inside a group (`GroupEditState::active_group` is set), stops at children
/// of that group so individual fragments can be selected.
fn find_selectable_ancestor(
mut entity: Entity,
scene_entities: &Query<(Entity, &GlobalTransform), (Without<EditorEntity>, With<Transform>)>,
parents: &Query<&ChildOf>,
group_edit: &GroupEditState,
brush_groups: &Query<(), With<BrushGroup>>,
) -> Option<Entity> {
// Walk up until we find a scene entity (one that has Transform and is not EditorEntity)
// Start with the hit entity itself -- it may already be a scene entity
loop {
if scene_entities.contains(entity) {
// Check if this entity has a parent that's also a scene entity;
// if so, prefer the parent (handles GLTF sub-meshes).
if let Ok(child_of) = parents.get(entity) {
let parent = child_of.0;
if scene_entities.contains(parent) {
// If we're inside a group and this parent IS that group,
// stop here -- let the user select the child fragment.
if group_edit.active_group == Some(parent) && brush_groups.contains(parent) {
return Some(entity);
}
// Keep walking up -- the parent is also selectable
entity = parent;
continue;
}
}
return Some(entity);
}
if let Ok(child_of) = parents.get(entity) {
entity = child_of.0;
} else {
return None;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// `BoxSelectOp`'s first call hands the rectangle's anchor over
/// from `pending`. Drag-threshold promotion records the press
/// position, so the rectangle should start there, not at the
/// later promotion point.
#[test]
fn activate_uses_pending_as_start_when_set() {
let mut state = BoxSelectState {
pending: Some(Vec2::new(50.0, 60.0)),
..Default::default()
};
state.activate(Vec2::new(70.0, 90.0));
assert!(state.active);
assert_eq!(state.start, Vec2::new(50.0, 60.0));
assert_eq!(state.current, Vec2::new(70.0, 90.0));
assert!(state.pending.is_none(), "activate should consume `pending`",);
}
/// When no pending press is recorded (e.g. an external dispatch
/// of `BoxSelectOp` without going through the trigger pipeline),
/// the rectangle anchors at the cursor instead.
#[test]
fn activate_falls_back_to_cursor_without_pending() {
let mut state = BoxSelectState::default();
state.activate(Vec2::new(70.0, 90.0));
assert!(state.active);
assert_eq!(state.start, Vec2::new(70.0, 90.0));
assert_eq!(state.current, Vec2::new(70.0, 90.0));
}
/// A click that hasn't moved past the threshold must stay pending,
/// otherwise `handle_viewport_click` and box-select would race for
/// the same press.
#[test]
fn drag_below_threshold_does_not_promote() {
// Moves of 0, 2.83 (sqrt(2*2 + 2*2)), and 4.24 are all < 5.
assert!(!cursor_dragged_past_threshold(
Vec2::new(100.0, 100.0),
Vec2::new(100.0, 100.0),
));
assert!(!cursor_dragged_past_threshold(
Vec2::new(100.0, 100.0),
Vec2::new(102.0, 102.0),
));
assert!(!cursor_dragged_past_threshold(
Vec2::new(100.0, 100.0),
Vec2::new(103.0, 103.0),
));
}
/// Once the cursor has moved at least `BOX_SELECT_DRAG_THRESHOLD`
/// pixels in any direction, the press promotes to box-select.
#[test]
fn drag_at_or_above_threshold_promotes() {
// Exactly the threshold (5px right): hits the `>=` boundary.
assert!(cursor_dragged_past_threshold(
Vec2::new(100.0, 100.0),
Vec2::new(105.0, 100.0),
));
// 3-4-5 right triangle: hypotenuse = 5, exactly threshold.
assert!(cursor_dragged_past_threshold(
Vec2::new(100.0, 100.0),
Vec2::new(104.0, 103.0),
));
// Comfortably past threshold.
assert!(cursor_dragged_past_threshold(
Vec2::new(100.0, 100.0),
Vec2::new(120.0, 80.0),
));
}
}