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

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 { ...@@ -21,7 +21,6 @@ public class DefaultModeAdapter implements IMode {
@Override @Override
public void mouseClicked(final MouseEvent event) { public void mouseClicked(final MouseEvent event) {
panelModel.setMousePosition(event.getPoint()); panelModel.setMousePosition(event.getPoint());
panelModel.setSelectedElement(panelModel.getMousePosition());
panelModel.notifyObservers(); panelModel.notifyObservers();
} }
...@@ -65,6 +64,7 @@ public class DefaultModeAdapter implements IMode { ...@@ -65,6 +64,7 @@ public class DefaultModeAdapter implements IMode {
@Override @Override
public void mouseMoved(MouseEvent e) { public void mouseMoved(MouseEvent e) {
panelModel.setMousePosition(e.getPoint()); panelModel.setMousePosition(e.getPoint());
panelModel.setSelectedElement(panelModel.getMousePosition());
panelModel.notifyObservers(); panelModel.notifyObservers();
} }
......
package org.vadere.gui.topographycreator.control; package org.vadere.gui.topographycreator.control;
import java.awt.Point; import org.lwjgl.system.CallbackI;
import java.awt.event.MouseEvent;
import javax.swing.undo.UndoableEdit;
import javax.swing.undo.UndoableEditSupport;
import org.vadere.gui.components.control.DefaultSelectionMode; import org.vadere.gui.components.control.DefaultSelectionMode;
import org.vadere.gui.components.control.IMode; import org.vadere.gui.components.control.IMode;
import org.vadere.gui.components.model.IDefaultModel;
import org.vadere.gui.topographycreator.model.IDrawPanelModel; import org.vadere.gui.topographycreator.model.IDrawPanelModel;
import org.vadere.state.scenario.ScenarioElement; import org.vadere.state.scenario.ScenarioElement;
import org.vadere.util.geometry.shapes.VPoint; import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape; 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 * In this mode the user can select a ScenarioElement with his mouse (click) and he can move
* elements around (press -> drag -> release). * elements around (press -> drag -> release).
...@@ -21,11 +23,27 @@ import org.vadere.util.geometry.shapes.VShape; ...@@ -21,11 +23,27 @@ import org.vadere.util.geometry.shapes.VShape;
public class SelectElementMode extends DefaultSelectionMode { public class SelectElementMode extends DefaultSelectionMode {
private final UndoableEditSupport undoSupport; private final UndoableEditSupport undoSupport;
private final IDrawPanelModel panelModel; 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) { public SelectElementMode(final IDrawPanelModel panelModel, final UndoableEditSupport undoSupport) {
super(panelModel); super(panelModel);
this.undoSupport = undoSupport; this.undoSupport = undoSupport;
this.panelModel = panelModel; this.panelModel = panelModel;
this.resizeElement = false;
} }
private Point startPoint; private Point startPoint;
...@@ -51,15 +69,25 @@ public class SelectElementMode extends DefaultSelectionMode { ...@@ -51,15 +69,25 @@ public class SelectElementMode extends DefaultSelectionMode {
startPoint = e.getPoint(); startPoint = e.getPoint();
panelModel.setPrototypeShape(panelModel.getSelectedElement().getShape()); panelModel.setPrototypeShape(panelModel.getSelectedElement().getShape());
panelModel.showPrototypeShape(); panelModel.showPrototypeShape();
resizeElement = panelModel.getSelectedElement().getShape().atBorder(panelModel.translateVectorCoordinates(startPoint));
isModifying = true;
} else { } else {
super.mousePressed(e); super.mousePressed(e);
} }
} }
@Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
panelModel.setMouseSelectionMode(this);
}
@Override @Override
public void mouseDragged(final MouseEvent e) { public void mouseDragged(final MouseEvent e) {
if (isMouseOnPrototypeShape()) { if (isMouseOnPrototypeShape() || isModifying) {
VShape shape = //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.translate(new Point(e.getPoint().x - startPoint.x, e.getPoint().y - startPoint.y));
panelModel.setPrototypeShape(shape); panelModel.setPrototypeShape(shape);
panelModel.showPrototypeShape(); panelModel.showPrototypeShape();
...@@ -72,20 +100,26 @@ public class SelectElementMode extends DefaultSelectionMode { ...@@ -72,20 +100,26 @@ public class SelectElementMode extends DefaultSelectionMode {
@Override @Override
public void mouseReleased(final MouseEvent e) { public void mouseReleased(final MouseEvent e) {
ScenarioElement element = panelModel.getSelectedElement(); ScenarioElement element = panelModel.getSelectedElement();
if (isMouseOnPrototypeShape()) { if (isMouseOnPrototypeShape() || isModifying) {
VShape oldShape = element.getShape(); 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)); panelModel.translate(new Point(e.getPoint().x - startPoint.x, e.getPoint().y - startPoint.y));
AttributeModifier.setShapeToAttributes(element, newShape); AttributeModifier.setShapeToAttributes(element, newShape);
// tell the panelModel that the selected element has changed! // tell the panelModel that the selected element has changed!
panelModel.setSelectedElement(element); panelModel.setSelectedElement(element);
element.getId();
UndoableEdit edit = new EditUpdateElementShape(panelModel, element, oldShape); UndoableEdit edit = new EditUpdateElementShape(panelModel, element, oldShape);
undoSupport.postEdit(edit); undoSupport.postEdit(edit);
} else { } else {
super.mouseReleased(e); super.mouseReleased(e);
} }
resizeElement = false;
isModifying = false;
startPoint = null;
panelModel.hidePrototypeShape(); panelModel.hidePrototypeShape();
panelModel.notifyObservers(); panelModel.notifyObservers();
} }
...@@ -93,13 +127,31 @@ public class SelectElementMode extends DefaultSelectionMode { ...@@ -93,13 +127,31 @@ public class SelectElementMode extends DefaultSelectionMode {
private boolean isMouseOnSelectedElement() { private boolean isMouseOnSelectedElement() {
ScenarioElement element = panelModel.getSelectedElement(); ScenarioElement element = panelModel.getSelectedElement();
VPoint cursor = panelModel.getMousePosition(); 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() { private boolean isMouseOnPrototypeShape() {
VShape shape = panelModel.getPrototypeShape(); VShape shape = panelModel.getPrototypeShape();
VPoint cursor = panelModel.getMousePosition(); 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 @Override
......
package org.vadere.gui.topographycreator.model; 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.jetbrains.annotations.NotNull;
import org.vadere.gui.components.control.IMode; import org.vadere.gui.components.control.IMode;
import org.vadere.gui.components.model.DefaultConfig; import org.vadere.gui.components.model.DefaultConfig;
...@@ -24,6 +15,12 @@ import org.vadere.util.geometry.shapes.VPoint; ...@@ -24,6 +15,12 @@ import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VRectangle; import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.geometry.shapes.VShape; 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> { public interface IDrawPanelModel<T extends DefaultConfig> extends IDefaultModel<T>, Iterable<ScenarioElement> {
@Override @Override
...@@ -163,6 +160,8 @@ public interface IDrawPanelModel<T extends DefaultConfig> extends IDefaultModel< ...@@ -163,6 +160,8 @@ public interface IDrawPanelModel<T extends DefaultConfig> extends IDefaultModel<
VShape translate(Point vector); VShape translate(Point vector);
VShape resize(Point start, Point end);
boolean isPrototypeVisble(); boolean isPrototypeVisble();
VShape getPrototypeShape(); VShape getPrototypeShape();
...@@ -190,4 +189,9 @@ public interface IDrawPanelModel<T extends DefaultConfig> extends IDefaultModel< ...@@ -190,4 +189,9 @@ public interface IDrawPanelModel<T extends DefaultConfig> extends IDefaultModel<
List<MeasurementArea> getMeasurementAreas(); List<MeasurementArea> getMeasurementAreas();
Rectangle2D.Double getBounds(); 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; 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.jetbrains.annotations.NotNull;
import org.vadere.gui.components.control.IMode; import org.vadere.gui.components.control.IMode;
import org.vadere.gui.components.model.DefaultConfig; import org.vadere.gui.components.model.DefaultConfig;
...@@ -26,6 +16,16 @@ import org.vadere.util.geometry.shapes.VPoint; ...@@ -26,6 +16,16 @@ import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VRectangle; import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.geometry.shapes.VShape; 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. * The data of the DrawPanel. Its holds the whole data of one scenario.
...@@ -355,6 +355,13 @@ public class TopographyCreatorModel extends DefaultModel implements IDrawPanelMo ...@@ -355,6 +355,13 @@ public class TopographyCreatorModel extends DefaultModel implements IDrawPanelMo
setChanged(); 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 @Override
public VShape translate(final Point vector) { public VShape translate(final Point vector) {
VPoint worldVector = new VPoint(vector.x / getScaleFactor(), -vector.y / getScaleFactor()); VPoint worldVector = new VPoint(vector.x / getScaleFactor(), -vector.y / getScaleFactor());
......
...@@ -7,6 +7,7 @@ import java.util.TreeSet; ...@@ -7,6 +7,7 @@ import java.util.TreeSet;
import org.vadere.util.geometry.GeometryUtils; import org.vadere.util.geometry.GeometryUtils;
import org.vadere.util.geometry.shapes.VPoint; import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.geometry.shapes.VTriangle; import org.vadere.util.geometry.shapes.VTriangle;
/** /**
...@@ -42,7 +43,6 @@ public class DataTriangle extends VTriangle { ...@@ -42,7 +43,6 @@ public class DataTriangle extends VTriangle {
* @param p1 * @param p1
* @param p2 * @param p2
* @param p3 * @param p3
* @param mp
*/ */
public DataTriangle(DataPoint p1, DataPoint p2, DataPoint p3) { public DataTriangle(DataPoint p1, DataPoint p2, DataPoint p3) {
super(p1, p2, p3); super(p1, p2, p3);
......
...@@ -16,4 +16,5 @@ public class VDisc extends VCircle { ...@@ -16,4 +16,5 @@ public class VDisc extends VCircle {
public double distance(@NotNull final IPoint pos) { public double distance(@NotNull final IPoint pos) {
return getCenter().distance(pos) - getRadius(); return getCenter().distance(pos) - getRadius();
} }
} }
package org.vadere.util.geometry.shapes; package org.vadere.util.geometry.shapes;
import org.vadere.util.geometry.GeometryUtils;
import java.awt.*;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.vadere.util.geometry.GeometryUtils;
@SuppressWarnings("serial") @SuppressWarnings("serial")
/** /**
* Note: A rectangle which has the same corner points as a polygon is not * 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 { ...@@ -115,8 +116,32 @@ public class VRectangle extends Rectangle2D.Double implements VShape {
} }
@Override @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)) { if (intersectingLine.intersects(this)) {
return true; return true;
} }
...@@ -137,18 +162,18 @@ public class VRectangle extends Rectangle2D.Double implements VShape { ...@@ -137,18 +162,18 @@ public class VRectangle extends Rectangle2D.Double implements VShape {
return new VPolygon(this); return new VPolygon(this);
} }
@Override @Override
public boolean intersects(final VShape shape) { public boolean intersects(final VShape shape) {
if(shape instanceof VRectangle){ if(shape instanceof VRectangle){
return super.intersects(((VRectangle)shape)); return super.intersects(((VRectangle)shape));
} }
else if(shape instanceof VPolygon) { else if(shape instanceof VPolygon) {
return ((VPolygon)shape).intersects(this); return ((VPolygon)shape).intersects(this);
} }
else { else {
return VShape.super.intersects(shape); return VShape.super.intersects(shape);
} }
} }
@Override @Override
public List<VPoint> getPath() { public List<VPoint> getPath() {
......
package org.vadere.util.geometry.shapes; package org.vadere.util.geometry.shapes;
import java.awt.Shape; import java.awt.*;
import java.awt.geom.Area; import java.awt.geom.Area;
import java.awt.geom.Path2D; import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.vadere.util.geometry.shapes.ShapeType;
/** /**
* Geometric shape and position. * Geometric shape and position.
*/ */
public interface VShape extends Shape, Cloneable { public interface VShape extends Shape, Cloneable {
double BORDER_TOLERANCE = 0.1;
double distance(IPoint point); double distance(IPoint point);
VPoint closestPoint(IPoint point); VPoint closestPoint(IPoint point);
...@@ -25,6 +26,28 @@ public interface VShape extends Shape, Cloneable { ...@@ -25,6 +26,28 @@ public interface VShape extends Shape, Cloneable {
VShape scale(final double scalar); 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); boolean intersects(VLine intersectingLine);
VPoint getCentroid(); VPoint getCentroid();
...@@ -98,4 +121,16 @@ public interface VShape extends Shape, Cloneable { ...@@ -98,4 +121,16 @@ public interface VShape extends Shape, Cloneable {
thisShape.subtract(otherShape); thisShape.subtract(otherShape);
return !thisShape.equals(thisShapeCpy); 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