Page:
Box Selection
Clone
1
Box Selection
Thibaud edited this page 2026-03-19 22:40:54 +01:00
Plan: Box Selection
Overview
Add drag-to-select functionality for selecting multiple shapes within a rectangle.
Current State
- Click selects single shape (in Controller.mousePressed)
- Shift+click adds to selection
- Selection uses SelectionAttributes
Implementation Plan
1. Controller changes
- Add state:
boxSelectOrigin(Point),boxSelectActive(boolean) - Add
boxSelectionRectangle(Rectangle) for current drag area
2. Mouse handling
mousePressed:- If click on empty area (no shape), start box selection
- Store origin point
- Clear current selection unless Shift is held
mouseDragged:- If boxSelectActive, update rectangle from origin to current point
- Repaint to show selection box
mouseReleased:- Calculate final box rectangle
- Find all shapes whose bounds intersect with box
- Add to selection (or replace if no Shift)
- Clear box selection state, repaint
3. Visual feedback
- Draw semi-transparent rectangle during drag in ShapesView
- Use XOR mode or overlay for drawing
- Box outline: dashed line, gray color
4. Selection behavior
- Default: box replaces current selection
- Shift+drag: box adds to current selection
- Box entirely within shape = shape selected
- Partial overlap: select shape (standard behavior)
5. Intersection check
- Use
shape.getBounds().intersects(boxRect) - Handle composite shapes (SCollection) - check children
Files to modify
ui/Controller.java- mouse handlingui/ShapesView.java- draw box overlay in paintComponent
Edge cases
- Drag on existing selection: allow move instead of box select
- Very small drag (< 5px): treat as click, select shape under point
- Empty box: clear selection