|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 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 javafx.beans.binding.Bindings; |
| 23 | +import javafx.beans.property.*; |
| 24 | +import javafx.geometry.Bounds; |
| 25 | +import javafx.geometry.Rectangle2D; |
| 26 | +import javafx.scene.control.Toggle; |
| 27 | +import javafx.scene.control.ToggleGroup; |
| 28 | +import javafx.scene.paint.Color; |
| 29 | +import javafx.scene.shape.*; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Objects; |
| 34 | + |
| 35 | +public class BoundingFreehandShapeView extends Path implements View, Toggle, |
| 36 | + BoundingShapeViewable { |
| 37 | + private static final String BOUNDING_FREEHAND_SHAPE_ID = "bounding-freehand-shape"; |
| 38 | + private static final double HIGHLIGHTED_FILL_OPACITY = 0.3; |
| 39 | + private static final double SELECTED_FILL_OPACITY = 0.5; |
| 40 | + private final BoundingShapeViewData boundingShapeViewData; |
| 41 | + |
| 42 | + public BoundingFreehandShapeView(ObjectCategory category) { |
| 43 | + this.boundingShapeViewData = new BoundingShapeViewData(this, category); |
| 44 | + |
| 45 | + setManaged(false); |
| 46 | + setFill(Color.TRANSPARENT); |
| 47 | + setId(BOUNDING_FREEHAND_SHAPE_ID); |
| 48 | + |
| 49 | + boundingShapeViewData.getNodeGroup().setManaged(false); |
| 50 | + boundingShapeViewData.getNodeGroup().setViewOrder(0); |
| 51 | + |
| 52 | + setUpInternalListeners(); |
| 53 | + } |
| 54 | + |
| 55 | + public List<Double> getPointsInImage() { |
| 56 | + List<Double> points = new ArrayList<>((getElements().size() - 1) * 2); |
| 57 | + |
| 58 | + for(PathElement pathElement : getElements()) { |
| 59 | + if(pathElement instanceof MoveTo moveToElement) { |
| 60 | + points.add(moveToElement.getX()); |
| 61 | + points.add(moveToElement.getY()); |
| 62 | + } else if(pathElement instanceof LineTo lineToElement) { |
| 63 | + points.add(lineToElement.getX()); |
| 64 | + points.add(lineToElement.getY()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return points; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public BoundingShapeViewData getViewData() { |
| 73 | + return boundingShapeViewData; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void autoScaleWithBoundsAndInitialize(ReadOnlyObjectProperty<Bounds> autoScaleBounds, double imageWidth, |
| 78 | + double imageHeight) { |
| 79 | + autoScaleWithBounds(autoScaleBounds); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Rectangle2D getRelativeOutlineRectangle() { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int hashCode() { |
| 89 | + return Objects.hash(boundingShapeViewData, getElements()); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean equals(Object obj) { |
| 94 | + if(this == obj) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + if(!(obj instanceof BoundingFreehandShapeView other)) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + if(!Objects.equals(boundingShapeViewData, other.boundingShapeViewData) || |
| 103 | + getElements().size() != other.getElements().size()) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + return Objects.equals(getElements(), other.getElements()); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public BoundingShapeTreeItem toTreeItem() { |
| 112 | + return new BoundingPolygonTreeItem(this); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public ToggleGroup getToggleGroup() { |
| 117 | + return boundingShapeViewData.getToggleGroup(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void setToggleGroup(ToggleGroup toggleGroup) { |
| 122 | + boundingShapeViewData.setToggleGroup(toggleGroup); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public ObjectProperty<ToggleGroup> toggleGroupProperty() { |
| 127 | + return boundingShapeViewData.toggleGroupProperty(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean isSelected() { |
| 132 | + return boundingShapeViewData.isSelected(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void setSelected(boolean selected) { |
| 137 | + boundingShapeViewData.setSelected(selected); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public BooleanProperty selectedProperty() { |
| 142 | + return boundingShapeViewData.selectedProperty(); |
| 143 | + } |
| 144 | + |
| 145 | + public void addMoveTo(double x, double y) { |
| 146 | + getElements().add(new MoveTo(x, y)); |
| 147 | + } |
| 148 | + |
| 149 | + public void addLineTo(double x, double y) { |
| 150 | + getElements().add(new LineTo(x, y)); |
| 151 | + } |
| 152 | + |
| 153 | + void autoScaleWithBounds(ReadOnlyObjectProperty<Bounds> autoScaleBounds) { |
| 154 | + boundingShapeViewData.autoScaleBounds().bind(autoScaleBounds); |
| 155 | + addAutoScaleListener(); |
| 156 | + } |
| 157 | + |
| 158 | + private void setUpInternalListeners() { |
| 159 | + fillProperty().bind(Bindings.when(selectedProperty()) |
| 160 | + .then(Bindings.createObjectBinding( |
| 161 | + () -> Color.web(strokeProperty().get().toString(), SELECTED_FILL_OPACITY), |
| 162 | + strokeProperty())) |
| 163 | + .otherwise(Bindings.when(boundingShapeViewData.highlightedProperty()) |
| 164 | + .then(Bindings.createObjectBinding(() -> Color |
| 165 | + .web(strokeProperty().get().toString(), |
| 166 | + HIGHLIGHTED_FILL_OPACITY), strokeProperty())) |
| 167 | + .otherwise(Color.TRANSPARENT))); |
| 168 | + |
| 169 | + boundingShapeViewData.getSelected().addListener((observable, oldValue, newValue) -> { |
| 170 | + if(Boolean.TRUE.equals(newValue)) { |
| 171 | + boundingShapeViewData.getHighlighted().set(false); |
| 172 | + } |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + private void addAutoScaleListener() { |
| 177 | + boundingShapeViewData.autoScaleBounds().addListener((observable, oldValue, newValue) -> { |
| 178 | + double xScaleFactor = newValue.getWidth() / oldValue.getWidth(); |
| 179 | + double yScaleFactor = newValue.getHeight() / oldValue.getHeight(); |
| 180 | + |
| 181 | + for(PathElement pathElement : getElements()) { |
| 182 | + if(pathElement instanceof MoveTo moveToElement) { |
| 183 | + moveToElement.setX(newValue.getMinX() + (moveToElement.getX() - oldValue.getMinX()) * xScaleFactor); |
| 184 | + moveToElement.setY(newValue.getMinY() + (moveToElement.getY() - oldValue.getMinY()) * yScaleFactor); |
| 185 | + } else if(pathElement instanceof LineTo lineToElement) { |
| 186 | + lineToElement.setX(newValue.getMinX() + (lineToElement.getX() - oldValue.getMinX()) * xScaleFactor); |
| 187 | + lineToElement.setY(newValue.getMinY() + (lineToElement.getY() - oldValue.getMinY()) * yScaleFactor); |
| 188 | + } |
| 189 | + } |
| 190 | + }); |
| 191 | + } |
| 192 | +} |
0 commit comments