|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Markus Fleischhacker <[email protected]> |
| 3 | + * |
| 4 | + * This file is part of Bounding Box Editor |
| 5 | + * |
| 6 | + * Bounding Box Editor is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Bounding Box Editor is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with Bounding Box Editor. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package com.github.mfl28.boundingboxeditor.ui; |
| 20 | + |
| 21 | +import com.github.mfl28.boundingboxeditor.model.data.ObjectCategory; |
| 22 | +import com.github.mfl28.boundingboxeditor.utils.MathUtils; |
| 23 | +import javafx.beans.property.BooleanProperty; |
| 24 | +import javafx.beans.property.DoubleProperty; |
| 25 | +import javafx.geometry.Point2D; |
| 26 | +import javafx.scene.control.ToggleGroup; |
| 27 | +import javafx.scene.image.ImageView; |
| 28 | +import javafx.scene.input.MouseButton; |
| 29 | +import javafx.scene.input.MouseEvent; |
| 30 | +import javafx.scene.shape.ClosePath; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +public class BoundingFreeHandShapeDrawer implements BoundingShapeDrawer { |
| 35 | + |
| 36 | + private final ImageView imageView; |
| 37 | + private final ToggleGroup toggleGroup; |
| 38 | + private final List<BoundingShapeViewable> boundingShapes; |
| 39 | + private final BooleanProperty autoSimplify; |
| 40 | + private final DoubleProperty simplifyRelativeDistanceTolerance; |
| 41 | + private boolean drawingInProgress = false; |
| 42 | + private BoundingFreehandShapeView boundingFreehandShapeView; |
| 43 | + |
| 44 | + public BoundingFreeHandShapeDrawer(ImageView imageView, ToggleGroup toggleGroup, List<BoundingShapeViewable> boundingShapes, |
| 45 | + BooleanProperty autoSimplify, DoubleProperty simplifyRelativeDistanceTolerance) { |
| 46 | + this.imageView = imageView; |
| 47 | + this.toggleGroup = toggleGroup; |
| 48 | + this.boundingShapes = boundingShapes; |
| 49 | + this.autoSimplify = autoSimplify; |
| 50 | + this.simplifyRelativeDistanceTolerance = simplifyRelativeDistanceTolerance; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void initializeShape(MouseEvent event, ObjectCategory objectCategory) { |
| 55 | + if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED) && event.getButton().equals(MouseButton.PRIMARY)) { |
| 56 | + Point2D parentCoordinates = imageView.localToParent(event.getX(), event.getY()); |
| 57 | + |
| 58 | + boundingFreehandShapeView = new BoundingFreehandShapeView(objectCategory); |
| 59 | + boundingFreehandShapeView.setToggleGroup(toggleGroup); |
| 60 | + |
| 61 | + boundingShapes.add(boundingFreehandShapeView); |
| 62 | + |
| 63 | + boundingFreehandShapeView.autoScaleWithBounds(imageView.boundsInParentProperty()); |
| 64 | + |
| 65 | + boundingFreehandShapeView.setVisible(true); |
| 66 | + toggleGroup.selectToggle(boundingFreehandShapeView); |
| 67 | + boundingFreehandShapeView.addMoveTo(parentCoordinates.getX(), parentCoordinates.getY()); |
| 68 | + drawingInProgress = true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void updateShape(MouseEvent event) { |
| 74 | + if (event.getEventType().equals(MouseEvent.MOUSE_DRAGGED) && event.getButton().equals(MouseButton.PRIMARY)) { |
| 75 | + final Point2D clampedEventXY = |
| 76 | + MathUtils.clampWithinBounds(event.getX(), event.getY(), imageView.getBoundsInLocal()); |
| 77 | + |
| 78 | + Point2D parentCoordinates = |
| 79 | + imageView.localToParent(clampedEventXY.getX(), clampedEventXY.getY()); |
| 80 | + |
| 81 | + boundingFreehandShapeView.addLineTo(parentCoordinates.getX(), parentCoordinates.getY()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void finalizeShape() { |
| 87 | + boundingFreehandShapeView.getElements().add(new ClosePath()); |
| 88 | + |
| 89 | + BoundingPolygonView boundingPolygonView = new BoundingPolygonView( |
| 90 | + boundingFreehandShapeView.getViewData().getObjectCategory()); |
| 91 | + |
| 92 | + final List<Double> pointsInImage = boundingFreehandShapeView.getPointsInImage(); |
| 93 | + |
| 94 | + boundingPolygonView.setEditing(true); |
| 95 | + |
| 96 | + for(int i = 0; i < pointsInImage.size(); i += 2) { |
| 97 | + boundingPolygonView.appendNode(pointsInImage.get(i), pointsInImage.get(i + 1)); |
| 98 | + } |
| 99 | + |
| 100 | + if(autoSimplify.get()) { |
| 101 | + boundingPolygonView.simplify(simplifyRelativeDistanceTolerance.get(), |
| 102 | + boundingFreehandShapeView.getViewData().autoScaleBounds().getValue()); |
| 103 | + } |
| 104 | + |
| 105 | + boundingPolygonView.setToggleGroup(toggleGroup); |
| 106 | + |
| 107 | + boundingShapes.remove(boundingFreehandShapeView); |
| 108 | + |
| 109 | + ObjectCategoryTreeItem parentTreeItem = (ObjectCategoryTreeItem) boundingFreehandShapeView.getViewData() |
| 110 | + .getTreeItem().getParent(); |
| 111 | + parentTreeItem.detachBoundingShapeTreeItemChild(boundingFreehandShapeView.getViewData().getTreeItem()); |
| 112 | + |
| 113 | + if(parentTreeItem.getChildren().isEmpty()) { |
| 114 | + parentTreeItem.getParent().getChildren().remove(parentTreeItem); |
| 115 | + } |
| 116 | + |
| 117 | + boundingShapes.add(boundingPolygonView); |
| 118 | + |
| 119 | + boundingPolygonView.autoScaleWithBounds(imageView.boundsInParentProperty()); |
| 120 | + boundingPolygonView.setVisible(true); |
| 121 | + toggleGroup.selectToggle(boundingPolygonView); |
| 122 | + |
| 123 | + boundingPolygonView.setConstructing(false); |
| 124 | + boundingPolygonView.setEditing(false); |
| 125 | + |
| 126 | + drawingInProgress = false; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean isDrawingInProgress() { |
| 131 | + return drawingInProgress; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public EditorImagePaneView.DrawingMode getDrawingMode() { |
| 136 | + return EditorImagePaneView.DrawingMode.FREEHAND; |
| 137 | + } |
| 138 | +} |
0 commit comments