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