Skip to content

Commit

Permalink
Fix shape group mouse-transparency when drawing. (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfl28 committed Nov 17, 2023
1 parent 41df50f commit 3c086c9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ public BoundingPolygonDrawer(ImageView imageView, ToggleGroup toggleGroup, List<
@Override
public void initializeShape(MouseEvent event, ObjectCategory objectCategory) {
if(event.getEventType().equals(MouseEvent.MOUSE_PRESSED) && event.getButton().equals(MouseButton.PRIMARY)) {
boundingShapes.forEach(boundingShapeViewable -> ((Node) boundingShapeViewable).setMouseTransparent(true));
boundingPolygonView = new BoundingPolygonView(objectCategory);
boundingPolygonView.setToggleGroup(toggleGroup);
boundingPolygonView.setConstructing(true);

boundingShapes.add(boundingPolygonView);

boundingPolygonView.autoScaleWithBounds(imageView.boundsInParentProperty());
boundingPolygonView.setMouseTransparent(true);
boundingPolygonView.setVisible(true);
toggleGroup.selectToggle(boundingPolygonView);

Expand All @@ -77,7 +75,6 @@ public void finalizeShape() {
boundingPolygonView.setConstructing(false);
boundingPolygonView.setEditing(false);

boundingShapes.forEach(boundingShapeViewable -> ((Node) boundingShapeViewable).setMouseTransparent(false));
drawingInProgress = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class EditorImagePaneView extends ScrollPane implements View {
private static final int MAXIMUM_IMAGE_WIDTH = 3072;
private static final int MAXIMUM_IMAGE_HEIGHT = 3072;
private static final double ZOOM_SCALE_DELTA = 0.05;
private static final String BOUNDING_SHAPE_SCENE_GROUP_ID = "bounding-shape-scene-group";

private final ImageView imageView = new ImageView();
private final SimpleBooleanProperty maximizeImageView = new SimpleBooleanProperty(true);
Expand Down Expand Up @@ -93,6 +94,7 @@ public class EditorImagePaneView extends ScrollPane implements View {
setHbarPolicy(ScrollBarPolicy.NEVER);

boundingShapeSceneGroup.setManaged(false);
boundingShapeSceneGroup.setId(BOUNDING_SHAPE_SCENE_GROUP_ID);

setUpImageView();
setUpInternalListeners();
Expand Down Expand Up @@ -129,6 +131,7 @@ public void initializeBoundingShapeDrawing(MouseEvent event) {

if (boundingShapeDrawer != null) {
boundingShapeDrawer.initializeShape(event, selectedCategory.get());
boundingShapeSceneGroup.setMouseTransparent(true);
}
}
}
Expand All @@ -142,6 +145,7 @@ public void updateBoundingShapeDrawing(MouseEvent event) {
public void finalizeBoundingShapeDrawing() {
if (boundingShapeDrawer != null && boundingShapeDrawer.isDrawingInProgress()) {
boundingShapeDrawer.finalizeShape();
boundingShapeSceneGroup.setMouseTransparent(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ protected void moveRelativeToImageView(FxRobot robot, Point2D startPointRatios,
.release(MouseButton.PRIMARY);
}

protected void moveRelativeToImageViewNoRelease(FxRobot robot, Point2D startPointRatios, Point2D endPointRatios) {
Point2D startPoint = getScreenPointFromRatios(mainView.getEditorImageView(), startPointRatios);
Point2D endPoint = getScreenPointFromRatios(mainView.getEditorImageView(), endPointRatios);

robot.moveTo(startPoint)
.press(MouseButton.PRIMARY)
.moveTo(endPoint);
}

protected void moveAndClickRelativeToImageView(FxRobot robot, MouseButton mousebutton, Point2D... points) {
for(Point2D point : points) {
robot.moveTo(getScreenPointFromRatios(mainView.getEditorImageView(), point)).clickOn(mousebutton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void onOpeningNewImageFolder_WhenBoundingBoxesExist_ShouldResetCorrectly(FxRobot
saveScreenshot(testinfo));

// Draw a bounding box.
moveRelativeToImageView(robot, new Point2D(0.25, 0.25), new Point2D(0.75, 0.75));
moveRelativeToImageViewNoRelease(robot, new Point2D(0.25, 0.25), new Point2D(0.75, 0.75));
WaitForAsyncUtils.waitForFxEvents();

int drawnBoundingBoxFileIndex = model.getCurrentFileIndex();
Expand All @@ -98,6 +98,18 @@ void onOpeningNewImageFolder_WhenBoundingBoxesExist_ShouldResetCorrectly(FxRobot
" found in " +
TIMEOUT_DURATION_IN_SEC +
" sec."));
verifyThat(mainView.getEditorImagePane().isDrawingInProgress(), Matchers.is(true));
verifyThat(mainView.getEditorImagePane().getCurrentBoundingShapeDrawingMode(),
Matchers.equalTo(EditorImagePaneView.DrawingMode.BOX));
verifyThat(robot.lookup("#bounding-shape-scene-group").query().isMouseTransparent(), Matchers.is(true));

robot.release(MouseButton.PRIMARY);
WaitForAsyncUtils.waitForFxEvents();

verifyThat(mainView.getEditorImagePane().isDrawingInProgress(), Matchers.is(false));
verifyThat(mainView.getEditorImagePane().getCurrentBoundingShapeDrawingMode(),
Matchers.equalTo(EditorImagePaneView.DrawingMode.NONE));
verifyThat(robot.lookup("#bounding-shape-scene-group").query().isMouseTransparent(), Matchers.is(false));

verifyThat(model.getCategoryToAssignedBoundingShapesCountMap().get(testCategoryName), Matchers.equalTo(1),
saveScreenshot(testinfo));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@
package com.github.mfl28.boundingboxeditor.ui;

import com.github.mfl28.boundingboxeditor.BoundingBoxEditorTestBase;
import javafx.css.Match;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.stage.Stage;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.hamcrest.Matchers;
import org.hamcrest.core.AllOf;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -132,6 +127,10 @@ void onOpeningNewImageFolder_WhenBoundingPolygonsExist_ShouldResetCorrectly(FxRo

verifyThat(mainView.getEditorImagePane().getBoundingShapeSelectionGroup().getSelectedToggle(),
Matchers.equalTo(drawnBoundingPolygon), saveScreenshot(testinfo));
verifyThat(mainView.getEditorImagePane().isDrawingInProgress(), Matchers.is(true));
verifyThat(mainView.getEditorImagePane().getCurrentBoundingShapeDrawingMode(),
Matchers.equalTo(EditorImagePaneView.DrawingMode.POLYGON));
verifyThat(robot.lookup("#bounding-shape-scene-group").query().isMouseTransparent(), Matchers.is(true));

robot.rightClickOn(mainView.getEditorImageView());
WaitForAsyncUtils.waitForFxEvents();
Expand All @@ -141,6 +140,11 @@ void onOpeningNewImageFolder_WhenBoundingPolygonsExist_ShouldResetCorrectly(FxRo
verifyThat(drawnBoundingPolygon.isEditing(), Matchers.equalTo(false), saveScreenshot(testinfo));
verifyThat(mainView.getEditorImagePane().getBoundingShapeSelectionGroup().getSelectedToggle(),
Matchers.nullValue(), saveScreenshot(testinfo));
verifyThat(mainView.getEditorImagePane().isDrawingInProgress(), Matchers.is(false));
verifyThat(mainView.getEditorImagePane().getCurrentBoundingShapeDrawingMode(),
Matchers.equalTo(EditorImagePaneView.DrawingMode.NONE));
verifyThat(robot.lookup("#bounding-shape-scene-group").query().isMouseTransparent(), Matchers.is(false));


robot.clickOn(drawnBoundingPolygon, MouseButton.MIDDLE);
WaitForAsyncUtils.waitForFxEvents();
Expand Down Expand Up @@ -422,6 +426,10 @@ void onFreehandDrawing_WhenImageFolderLoaded_ShouldCorrectlyCreatePolygons(FxRob
verifyThat(mainView.getEditorImagePane().getCurrentBoundingShapeDrawingMode(), Matchers.equalTo(EditorImagePaneView.DrawingMode.FREEHAND));

int numPathElements = boundingFreehandShapeView.getElements().size();
verifyThat(mainView.getEditorImagePane().isDrawingInProgress(), Matchers.is(true));
verifyThat(mainView.getEditorImagePane().getCurrentBoundingShapeDrawingMode(),
Matchers.equalTo(EditorImagePaneView.DrawingMode.FREEHAND));
verifyThat(robot.lookup("#bounding-shape-scene-group").query().isMouseTransparent(), Matchers.is(true));


robot.moveTo(screenPoints.get(1));
Expand Down Expand Up @@ -454,7 +462,9 @@ void onFreehandDrawing_WhenImageFolderLoaded_ShouldCorrectlyCreatePolygons(FxRob
saveScreenshot(testinfo));

verifyThat(mainView.getEditorImagePane().getCurrentBoundingShapeDrawingMode(),
Matchers.not(Matchers.equalTo(EditorImagePaneView.DrawingMode.FREEHAND)));
Matchers.equalTo(EditorImagePaneView.DrawingMode.NONE));
verifyThat(mainView.getEditorImagePane().isDrawingInProgress(), Matchers.is(false));
verifyThat(robot.lookup("#bounding-shape-scene-group").query().isMouseTransparent(), Matchers.is(false));

verifyThat(mainView.getCurrentBoundingShapes().get(0), Matchers.instanceOf(BoundingPolygonView.class),
saveScreenshot(testinfo));
Expand Down

0 comments on commit 3c086c9

Please sign in to comment.