Search in sources :

Example 1 with FlowIndicatorPositionHandle

use of org.osate.ge.gef.ui.editor.overlays.FlowIndicatorPositionHandle in project osate2 by osate.

the class MoveConnectionPointInteraction method onMouseDragged.

@Override
protected Interaction.InteractionState onMouseDragged(final MouseEvent e) {
    if (e.getButton() != MouseButton.PRIMARY) {
        return super.onMouseDragged(e);
    }
    final Transform sceneToDiagramTransform = editor.getGefDiagram().getSceneNode().getSceneToLocalTransform();
    final Point2D eventInDiagram = sceneToDiagramTransform.transform(e.getSceneX(), e.getSceneY());
    final Point2D snappedDiagramPosition = InputEventHandlerUtil.snap(editor, eventInDiagram, false);
    if (activeHandle instanceof FlowIndicatorPositionHandle) {
        final FlowIndicatorNode c = ((FlowIndicatorPositionHandle) activeHandle).getSceneNode();
        final Node positioningReference = c.getPositioningReferenceOrThrow();
        // The the position relative to the reference
        final Point2D newPoint = getLocalPositionFromDiagram(editor.getGefDiagram(), snappedDiagramPosition, positioningReference);
        final Point2D oldPosition = PreferredPosition.get(activeHandle.getSceneNode());
        PreferredPosition.set(activeHandle.getSceneNode(), newPoint);
        // Adjust positions of control points so that only the ending position of the flow indicator is moved.
        if (oldPosition != null) {
            final double dx = newPoint.getX() - oldPosition.getX();
            final double dy = newPoint.getY() - oldPosition.getY();
            c.getInnerConnection().setControlPoints(c.getInnerConnection().getControlPoints().stream().map(p -> new Point(p.x - dx, p.y - dy)).collect(Collectors.toList()));
        }
    } else if (controlPointIndex != null) {
        final BaseConnectionNode c = activeHandle.getSceneNode();
        final Connection ic = c.getInnerConnection();
        final Point2D newPosition = getLocalPositionFromDiagram(editor.getGefDiagram(), snappedDiagramPosition, ic);
        // Get a list of the control points and the start and end points of connection.
        final List<Point> allPoints = ic.getPointsUnmodifiable();
        final List<org.eclipse.gef.geometry.planar.Point> controlPoints = ic.getControlPoints();
        final ArrayList<org.eclipse.gef.geometry.planar.Point> endAndControlPoints = new ArrayList<>(controlPoints.size() + 2);
        endAndControlPoints.add(allPoints.get(0));
        endAndControlPoints.addAll(controlPoints);
        endAndControlPoints.add(allPoints.get(allPoints.size() - 1));
        // Determine whether the point should be removed or added
        final boolean remove = controlPointExists && distanceToLineSegment(newPosition, endAndControlPoints, controlPointIndex, controlPointIndex + 2) <= REMOVE_POINT_DISTANCE;
        final boolean add = !remove && !controlPointExists && distanceToLineSegment(newPosition, endAndControlPoints, controlPointIndex, controlPointIndex + 1) >= ADD_POINT_DISTANCE;
        if (remove) {
            c.getInnerConnection().removeControlPoint(controlPointIndex);
            controlPointExists = false;
        } else if (add) {
            controlPoints.add(controlPointIndex, FX2Geometry.toPoint(newPosition));
            ic.setControlPoints(controlPoints);
            controlPointExists = true;
        } else if (controlPointExists) {
            ic.setControlPoint(controlPointIndex, FX2Geometry.toPoint(newPosition));
        }
    }
    return InteractionState.IN_PROGRESS;
}
Also used : BaseConnectionNode(org.osate.ge.gef.BaseConnectionNode) Node(javafx.scene.Node) FlowIndicatorNode(org.osate.ge.gef.FlowIndicatorNode) Connection(org.eclipse.gef.fx.nodes.Connection) ArrayList(java.util.ArrayList) Point(org.eclipse.gef.geometry.planar.Point) FlowIndicatorPositionHandle(org.osate.ge.gef.ui.editor.overlays.FlowIndicatorPositionHandle) FlowIndicatorNode(org.osate.ge.gef.FlowIndicatorNode) Point2D(javafx.geometry.Point2D) ArrayList(java.util.ArrayList) List(java.util.List) BaseConnectionNode(org.osate.ge.gef.BaseConnectionNode) Transform(javafx.scene.transform.Transform)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Point2D (javafx.geometry.Point2D)1 Node (javafx.scene.Node)1 Transform (javafx.scene.transform.Transform)1 Connection (org.eclipse.gef.fx.nodes.Connection)1 Point (org.eclipse.gef.geometry.planar.Point)1 BaseConnectionNode (org.osate.ge.gef.BaseConnectionNode)1 FlowIndicatorNode (org.osate.ge.gef.FlowIndicatorNode)1 FlowIndicatorPositionHandle (org.osate.ge.gef.ui.editor.overlays.FlowIndicatorPositionHandle)1