handle multiple selection with shift

This commit is contained in:
Thibaud Gasser 2025-02-19 13:44:23 +01:00
parent 5f0bcda8ba
commit 02dd84c1f4
6 changed files with 70 additions and 38 deletions

View File

@ -2,23 +2,30 @@ package ovh.gasser.newshapes;
import ovh.gasser.newshapes.attributes.SelectionAttributes; import ovh.gasser.newshapes.attributes.SelectionAttributes;
import ovh.gasser.newshapes.shapes.Shape; import ovh.gasser.newshapes.shapes.Shape;
import ovh.gasser.newshapes.util.Streamable;
public class Selection { import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
private final SelectionAttributes attributes; public class Selection implements Streamable<Shape> {
public final Shape shape; private final List<Shape> selectedShapes = new ArrayList<>();
public Selection(final Shape shape, boolean selected) { @Override
this(shape); public Iterator<Shape> iterator() {
attributes.selected = selected; return selectedShapes.iterator();
} }
private Selection(final Shape shape) { public void clear() {
attributes = (SelectionAttributes) shape.getAttributes(SelectionAttributes.ID); for (Shape shape : selectedShapes) {
this.shape = shape; shape.addAttributes(new SelectionAttributes(false));
} }
public void unselect() { selectedShapes.clear();
attributes.selected = false; }
public void add(Shape s) {
selectedShapes.add(s);
s.addAttributes(new SelectionAttributes(true));
} }
} }

View File

@ -4,6 +4,14 @@ public class SelectionAttributes implements Attributes {
public static final String ID = "SELECTION_ATTRS"; public static final String ID = "SELECTION_ATTRS";
public boolean selected; public boolean selected;
public SelectionAttributes() {
this(false);
}
public SelectionAttributes(boolean selected) {
this.selected = selected;
}
@Override @Override
public String getID() { public String getID() {
return ID; return ID;

View File

@ -29,11 +29,6 @@ public abstract class AbstractShape implements Shape {
attributes.put(attrs.getID(), attrs); attributes.put(attrs.getID(), attrs);
} }
@Override
public void setLoc(Point newLoc) {
getBounds().setLocation(newLoc);
}
@Override @Override
public void translate(int dx, int dy) { public void translate(int dx, int dy) {
getBounds().translate(dx, dy); getBounds().translate(dx, dy);

View File

@ -12,7 +12,6 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Spliterator; import java.util.Spliterator;
import java.util.stream.Collectors;
public class SCollection extends AbstractShape implements Streamable<Shape> { public class SCollection extends AbstractShape implements Streamable<Shape> {
private final static Logger logger = LoggerFactory.getLogger(SCollection.class); private final static Logger logger = LoggerFactory.getLogger(SCollection.class);
@ -53,9 +52,8 @@ public class SCollection extends AbstractShape implements Streamable<Shape> {
} }
@Override @Override
public void setLoc(Point newLoc) { public void translate(int dx, int dy) {
final Point loc = getBounds().getLocation(); children.forEach(s -> s.translate(dx, dy));
children.forEach(s -> s.translate(newLoc.x - loc.x, newLoc.y - loc.y));
} }
@Override @Override

View File

@ -7,7 +7,6 @@ import java.awt.*;
public interface Shape { public interface Shape {
void accept(ShapeVisitor visitor); void accept(ShapeVisitor visitor);
void setLoc(Point newLoc);
void translate(int dx, int dy); void translate(int dx, int dy);
Attributes getAttributes(String key); Attributes getAttributes(String key);
void addAttributes(Attributes attr); void addAttributes(Attributes attr);

View File

@ -20,11 +20,13 @@ public class Controller {
private final static Logger logger = LoggerFactory.getLogger(Controller.class); private final static Logger logger = LoggerFactory.getLogger(Controller.class);
private final ShapesView view; private final ShapesView view;
private final SCollection model; private final SCollection model;
private Selection selection; private final Selection selection;
private boolean shiftPressed;
Controller(ShapesView view, SCollection model) { Controller(ShapesView view, SCollection model) {
this.view = view; this.view = view;
this.model = model; this.model = model;
this.selection = new Selection();
var adapter = new MouseAdapter() { var adapter = new MouseAdapter() {
@Override @Override
@ -44,11 +46,20 @@ public class Controller {
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
handleKeyPressed(e); handleKeyPressed(e);
} }
@Override
public void keyReleased(KeyEvent e) {
handleKeyReleased(e);
}
}); });
} }
private void handleMouseDragged(MouseEvent evt) { private void handleMouseDragged(MouseEvent evt) {
if (selection != null) selection.shape.setLoc(evt.getPoint()); for (Shape shape : selection) {
int dx = evt.getX() - shape.getBounds().x;
int dy = evt.getY() - shape.getBounds().y;
shape.translate(dx, dy);
}
view.repaint(); view.repaint();
} }
@ -56,13 +67,13 @@ public class Controller {
getTarget(evt, this.model) getTarget(evt, this.model)
.ifPresentOrElse( .ifPresentOrElse(
s -> { s -> {
if (selection != null) resetSelection(); if (!shiftPressed) {
selection = new Selection(s, true); resetSelection();
logger.debug("Selecting {}", selection.shape);
},
() -> {
if (selection != null) resetSelection();
} }
selection.add(s);
logger.debug("Selecting {}", s);
},
this::resetSelection
); );
view.repaint(); view.repaint();
} }
@ -73,10 +84,15 @@ public class Controller {
case KeyEvent.VK_C -> copySelection(); case KeyEvent.VK_C -> copySelection();
case KeyEvent.VK_A -> changeSelectionColor(); case KeyEvent.VK_A -> changeSelectionColor();
case KeyEvent.VK_H -> exportHtml(); case KeyEvent.VK_H -> exportHtml();
case KeyEvent.VK_SHIFT -> shiftPressed = true;
default -> logger.warn("Pressed unhandled key: {}", evt.getKeyChar()); default -> logger.warn("Pressed unhandled key: {}", evt.getKeyChar());
} }
} }
private void handleKeyReleased(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_SHIFT) shiftPressed = false;
}
private void exportHtml() { private void exportHtml() {
logger.info("Exporting view to html"); logger.info("Exporting view to html");
try { try {
@ -91,11 +107,15 @@ public class Controller {
logger.debug("No selection to change color of"); logger.debug("No selection to change color of");
return; return;
} }
if (selection.shape instanceof SCollection collection) {
for (Shape s : selection) {
if (s instanceof SCollection collection) {
collection.forEach(shape -> shape.addAttributes(new ColorAttributes(false, true, Color.BLACK, new Color((int) (Math.random() * 0x1000000))))); collection.forEach(shape -> shape.addAttributes(new ColorAttributes(false, true, Color.BLACK, new Color((int) (Math.random() * 0x1000000)))));
} else { } else {
selection.shape.addAttributes(new ColorAttributes(false, true, Color.BLACK, new Color((int) (Math.random() * 0x1000000)))); s.addAttributes(new ColorAttributes(false, true, Color.BLACK, new Color((int) (Math.random() * 0x1000000))));
} }
}
view.repaint(); view.repaint();
} }
@ -104,22 +124,27 @@ public class Controller {
logger.debug("No selection to copy"); logger.debug("No selection to copy");
return; return;
} }
this.model.add(selection.shape.clone());
for (Shape shape : selection) {
this.model.add(shape.clone());
}
view.repaint(); view.repaint();
} }
public void deleteSelected() { public void deleteSelected() {
if (selection == null) return; if (selection == null) return;
logger.debug("Deleting selected shape(s)"); logger.debug("Deleting selected shape(s)");
this.model.remove(selection.shape); for (Shape s : selection) {
this.model.remove(s);
}
resetSelection(); resetSelection();
view.repaint(); view.repaint();
} }
private void resetSelection() { private void resetSelection() {
logger.debug("Un-selecting {}", selection.shape); logger.debug("Resetting selection");
selection.unselect(); selection.clear();
selection = null;
} }
private Optional<Shape> getTarget(MouseEvent evt, SCollection sc) { private Optional<Shape> getTarget(MouseEvent evt, SCollection sc) {