Commit 45ebccbe authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier

Merge branch 'master' of gitlab.lrz.de:vadere/vadere

parents e87f9542 c91dad12
Pipeline #105453 passed with stages
in 110 minutes and 49 seconds
......@@ -21,7 +21,6 @@ public class DefaultModeAdapter implements IMode {
@Override
public void mouseClicked(final MouseEvent event) {
panelModel.setMousePosition(event.getPoint());
panelModel.setSelectedElement(panelModel.getMousePosition());
panelModel.notifyObservers();
}
......@@ -65,6 +64,7 @@ public class DefaultModeAdapter implements IMode {
@Override
public void mouseMoved(MouseEvent e) {
panelModel.setMousePosition(e.getPoint());
panelModel.setSelectedElement(panelModel.getMousePosition());
panelModel.notifyObservers();
}
......
package org.vadere.gui.topographycreator.control;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.undo.UndoableEdit;
import javax.swing.undo.UndoableEditSupport;
import org.lwjgl.system.CallbackI;
import org.vadere.gui.components.control.DefaultSelectionMode;
import org.vadere.gui.components.control.IMode;
import org.vadere.gui.components.model.IDefaultModel;
import org.vadere.gui.topographycreator.model.IDrawPanelModel;
import org.vadere.state.scenario.ScenarioElement;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import javax.swing.undo.UndoableEdit;
import javax.swing.undo.UndoableEditSupport;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Optional;
/**
* In this mode the user can select a ScenarioElement with his mouse (click) and he can move
* elements around (press -> drag -> release).
......@@ -21,11 +23,27 @@ import org.vadere.util.geometry.shapes.VShape;
public class SelectElementMode extends DefaultSelectionMode {
private final UndoableEditSupport undoSupport;
private final IDrawPanelModel panelModel;
private boolean resizeElement;
private boolean isModifying;
private static final int[] DIRECTIONAL_CURSOR_CODES;
static{
DIRECTIONAL_CURSOR_CODES = new int[8];
DIRECTIONAL_CURSOR_CODES[0] = Cursor.E_RESIZE_CURSOR;
DIRECTIONAL_CURSOR_CODES[1] = Cursor.NE_RESIZE_CURSOR;
DIRECTIONAL_CURSOR_CODES[2] = Cursor.N_RESIZE_CURSOR;
DIRECTIONAL_CURSOR_CODES[3] = Cursor.NW_RESIZE_CURSOR;
DIRECTIONAL_CURSOR_CODES[4] = Cursor.W_RESIZE_CURSOR;
DIRECTIONAL_CURSOR_CODES[5] = Cursor.SW_RESIZE_CURSOR;
DIRECTIONAL_CURSOR_CODES[6] = Cursor.S_RESIZE_CURSOR;
DIRECTIONAL_CURSOR_CODES[7] = Cursor.SE_RESIZE_CURSOR;
}
public SelectElementMode(final IDrawPanelModel panelModel, final UndoableEditSupport undoSupport) {
super(panelModel);
this.undoSupport = undoSupport;
this.panelModel = panelModel;
this.resizeElement = false;
}
private Point startPoint;
......@@ -51,15 +69,25 @@ public class SelectElementMode extends DefaultSelectionMode {
startPoint = e.getPoint();
panelModel.setPrototypeShape(panelModel.getSelectedElement().getShape());
panelModel.showPrototypeShape();
resizeElement = panelModel.getSelectedElement().getShape().atBorder(panelModel.translateVectorCoordinates(startPoint));
isModifying = true;
} else {
super.mousePressed(e);
}
}
@Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
panelModel.setMouseSelectionMode(this);
}
@Override
public void mouseDragged(final MouseEvent e) {
if (isMouseOnPrototypeShape()) {
VShape shape =
if (isMouseOnPrototypeShape() || isModifying) {
//VShape shape = panelModel.translate(new Point(e.getPoint().x - startPoint.x, e.getPoint().y - startPoint.y));
VShape shape = resizeElement ?
panelModel.resize(startPoint, e.getPoint()) :
panelModel.translate(new Point(e.getPoint().x - startPoint.x, e.getPoint().y - startPoint.y));
panelModel.setPrototypeShape(shape);
panelModel.showPrototypeShape();
......@@ -72,20 +100,26 @@ public class SelectElementMode extends DefaultSelectionMode {
@Override
public void mouseReleased(final MouseEvent e) {
ScenarioElement element = panelModel.getSelectedElement();
if (isMouseOnPrototypeShape()) {
if (isMouseOnPrototypeShape() || isModifying) {
VShape oldShape = element.getShape();
VShape newShape =
VShape newShape = resizeElement ?
panelModel.resize(startPoint, e.getPoint()) :
panelModel.translate(new Point(e.getPoint().x - startPoint.x, e.getPoint().y - startPoint.y));
AttributeModifier.setShapeToAttributes(element, newShape);
// tell the panelModel that the selected element has changed!
panelModel.setSelectedElement(element);
element.getId();
UndoableEdit edit = new EditUpdateElementShape(panelModel, element, oldShape);
undoSupport.postEdit(edit);
} else {
super.mouseReleased(e);
}
resizeElement = false;
isModifying = false;
startPoint = null;
panelModel.hidePrototypeShape();
panelModel.notifyObservers();
}
......@@ -93,13 +127,31 @@ public class SelectElementMode extends DefaultSelectionMode {
private boolean isMouseOnSelectedElement() {
ScenarioElement element = panelModel.getSelectedElement();
VPoint cursor = panelModel.getMousePosition();
return element != null && element.getShape().intersects(cursor.x, cursor.y, 0.001, 0.001);
return element != null && element.getShape().intersects(cursor.x - 0.01, cursor.y - 0.01, 0.02, 0.02);
//return element != null && element.getShape().contains(cursor);
}
private boolean isMouseOnPrototypeShape() {
VShape shape = panelModel.getPrototypeShape();
VPoint cursor = panelModel.getMousePosition();
return panelModel.isPrototypeVisble() && shape.intersects(cursor.x, cursor.y, 0.001, 0.001);
return panelModel.isPrototypeVisble() && shape.intersects(cursor.x - 0.01, cursor.y - 0.01, 0.02, 0.02);
}
@Override
public Cursor getCursor(){
VShape selectedShape = panelModel.getSelectedElement() == null ? null : panelModel.getSelectedElement().getShape();
VPoint mousePosition = panelModel.getMousePosition();
boolean directionalCursorCondition = isMouseOnSelectedElement() && (resizeElement || selectedShape.atBorder(mousePosition));
if (directionalCursorCondition) {
return Cursor.getPredefinedCursor(
DIRECTIONAL_CURSOR_CODES[
selectedShape.getDirectionalCode(
startPoint == null ? mousePosition : new VPoint(startPoint),
DIRECTIONAL_CURSOR_CODES.length
)]
);
}
return super.getCursor();
}
@Override
......
package org.vadere.gui.topographycreator.model;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.Observer;
import java.util.function.Predicate;
import org.jetbrains.annotations.NotNull;
import org.vadere.gui.components.control.IMode;
import org.vadere.gui.components.model.DefaultConfig;
......@@ -24,6 +15,12 @@ import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.geometry.shapes.VShape;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.Observer;
import java.util.function.Predicate;
public interface IDrawPanelModel<T extends DefaultConfig> extends IDefaultModel<T>, Iterable<ScenarioElement> {
@Override
......@@ -163,6 +160,8 @@ public interface IDrawPanelModel<T extends DefaultConfig> extends IDefaultModel<
VShape translate(Point vector);
VShape resize(Point start, Point end);
boolean isPrototypeVisble();
VShape getPrototypeShape();
......@@ -190,4 +189,9 @@ public interface IDrawPanelModel<T extends DefaultConfig> extends IDefaultModel<
List<MeasurementArea> getMeasurementAreas();
Rectangle2D.Double getBounds();
default VPoint translateVectorCoordinates(Point point) {
return new VPoint(point.x / getScaleFactor(), getTopography().getBounds().height - point.y / getScaleFactor());
}
}
package org.vadere.gui.topographycreator.model;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Observer;
import java.util.function.Predicate;
import org.jetbrains.annotations.NotNull;
import org.vadere.gui.components.control.IMode;
import org.vadere.gui.components.model.DefaultConfig;
......@@ -26,6 +16,16 @@ import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.geometry.shapes.VShape;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Observer;
import java.util.function.Predicate;
/**
* The data of the DrawPanel. Its holds the whole data of one scenario.
......@@ -355,6 +355,13 @@ public class TopographyCreatorModel extends DefaultModel implements IDrawPanelMo
setChanged();
}
@Override
public VShape resize(final Point start, final Point end) {
VPoint startVector = translateVectorCoordinates(start);
VPoint endVector = translateVectorCoordinates(end);
return getSelectedElement().getShape().resize(startVector, endVector);
}
@Override
public VShape translate(final Point vector) {
VPoint worldVector = new VPoint(vector.x / getScaleFactor(), -vector.y / getScaleFactor());
......
......@@ -7,6 +7,7 @@ import java.util.TreeSet;
import org.vadere.util.geometry.GeometryUtils;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.geometry.shapes.VTriangle;
/**
......@@ -42,7 +43,6 @@ public class DataTriangle extends VTriangle {
* @param p1
* @param p2
* @param p3
* @param mp
*/
public DataTriangle(DataPoint p1, DataPoint p2, DataPoint p3) {
super(p1, p2, p3);
......
......@@ -16,4 +16,5 @@ public class VDisc extends VCircle {
public double distance(@NotNull final IPoint pos) {
return getCenter().distance(pos) - getRadius();
}
}
package org.vadere.util.geometry.shapes;
import org.vadere.util.geometry.GeometryUtils;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
import java.util.List;
import org.vadere.util.geometry.GeometryUtils;
@SuppressWarnings("serial")
/**
* Note: A rectangle which has the same corner points as a polygon is not
......@@ -115,8 +116,32 @@ public class VRectangle extends Rectangle2D.Double implements VShape {
}
@Override
public boolean intersects(VLine intersectingLine) {
public VRectangle resize(IPoint start, IPoint end){
double minX = Math.abs(start.getX() - getMinX()) < BORDER_TOLERANCE ? end.getX() : getMinX();
double minY = Math.abs(start.getY() - getMinY()) < BORDER_TOLERANCE ? end.getY() : getMinY();
double maxX = Math.abs(start.getX() - getMaxX()) < BORDER_TOLERANCE ? end.getX() : getMaxX();
double maxY = Math.abs(start.getY() - getMaxY()) < BORDER_TOLERANCE ? end.getY() : getMaxY();
return new VRectangle(minX, minY, maxX - minX, maxY - minY);
}
@Override
public int getDirectionalCode(IPoint startPoint, int directions){
double horizontalRatio = (startPoint.getX() - getCenterX()) / (getWidth() / 2);
double verticalRatio = (startPoint.getY() - getCenterY()) / (getHeight() / 2);
if (Math.abs(horizontalRatio - verticalRatio) < BORDER_TOLERANCE) {
return horizontalRatio > 0 ? 1 : 5;
} else if (Math.abs(horizontalRatio + verticalRatio) < BORDER_TOLERANCE) {
return horizontalRatio > 0 ? 3 : 7;
} else if (Math.abs(horizontalRatio) > Math.abs(verticalRatio)) {
return horizontalRatio > 0 ? 0 : 4;
}
return verticalRatio > 0 ? 2 : 6;
}
@Override
public boolean intersects(VLine intersectingLine) {
if (intersectingLine.intersects(this)) {
return true;
}
......@@ -137,18 +162,18 @@ public class VRectangle extends Rectangle2D.Double implements VShape {
return new VPolygon(this);
}
@Override
public boolean intersects(final VShape shape) {
if(shape instanceof VRectangle){
return super.intersects(((VRectangle)shape));
}
else if(shape instanceof VPolygon) {
return ((VPolygon)shape).intersects(this);
}
else {
return VShape.super.intersects(shape);
}
}
@Override
public boolean intersects(final VShape shape) {
if(shape instanceof VRectangle){
return super.intersects(((VRectangle)shape));
}
else if(shape instanceof VPolygon) {
return ((VPolygon)shape).intersects(this);
}
else {
return VShape.super.intersects(shape);
}
}
@Override
public List<VPoint> getPath() {
......
package org.vadere.util.geometry.shapes;
import java.awt.Shape;
import java.awt.*;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import org.vadere.util.geometry.shapes.ShapeType;
/**
* Geometric shape and position.
*/
public interface VShape extends Shape, Cloneable {
double BORDER_TOLERANCE = 0.1;
double distance(IPoint point);
VPoint closestPoint(IPoint point);
......@@ -25,6 +26,28 @@ public interface VShape extends Shape, Cloneable {
VShape scale(final double scalar);
default boolean atBorder(final VPoint point){
VShape circle = new VCircle(new VPoint(point.getX(), point.getY()), BORDER_TOLERANCE);
return intersects(circle) && !containsShape(circle);
}
default VShape resize(final IPoint start, final IPoint end){
double startDistance = distanceToCenter(start);
double endDistance = distanceToCenter(end);
VPoint center = this.getCentroid();
VShape scaled = this.scale(endDistance / startDistance);
return scaled.translatePrecise(center.subtract(scaled.getCentroid()));
}
default double distanceToCenter(final IPoint point){
final int squareExponent = 2;
double deltaXSquared = Math.pow(point.getX() - this.getCentroid().getX(), squareExponent);
double deltaYSquared = Math.pow(point.getY() - this.getCentroid().getY(), squareExponent);
return Math.sqrt(deltaXSquared + deltaYSquared);
}
boolean intersects(VLine intersectingLine);
VPoint getCentroid();
......@@ -98,4 +121,16 @@ public interface VShape extends Shape, Cloneable {
thisShape.subtract(otherShape);
return !thisShape.equals(thisShapeCpy);
}
default int getDirectionalCode(Point startPoint, int directions) {
return getDirectionalCode(new VPoint(startPoint), directions);
}
default int getDirectionalCode(IPoint startPoint, int directions) {
VPoint direction = new VPoint(startPoint).subtract(getCentroid());
double angle = Math.atan(direction.getY() / direction.getX());
angle += Math.PI + Math.PI / (directions);
double indexRatio = (angle) / (2 * Math.PI);
return (int)(indexRatio * directions);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment