Implement Box Selection (drag to select multiple shapes) #33

Open
opened 2026-03-27 23:17:57 +00:00 by thib8956 · 0 comments
Owner

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 handling
  • ui/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
## 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 handling - ui/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
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: thib8956/new-shapes#33