Search in sources :

Example 6 with WiresMagnet

use of com.ait.lienzo.client.core.shape.wires.WiresMagnet in project kie-wb-common by kiegroup.

the class WiresConnectorView method updateConnection.

private static void updateConnection(final Connection connection, final MagnetManager.Magnets magnets, final com.ait.lienzo.client.core.types.Point2D abeLocation, final Consumer<Boolean> isAutoConnectionConsumer, final Consumer<WiresMagnet> magnetConsumer) {
    final WiresMagnet[] magnet = new WiresMagnet[] { null };
    final boolean[] auto = new boolean[] { false };
    if (null != connection) {
        final DiscreteConnection dc = connection instanceof DiscreteConnection ? (DiscreteConnection) connection : null;
        if (null != dc) {
            // Obtain the magnet index and auto flag, if the connection is a discrete type and it has been already set.
            dc.getMagnetIndex().ifPresent(index -> magnet[0] = magnets.getMagnet(index));
            auto[0] = dc.isAuto();
        }
        // If still no magnet found from the connection's cache, figure it out as by the connection's location.
        if (null == magnet[0]) {
            magnet[0] = getMagnetForConnection(connection, magnets, abeLocation);
            // Update the discrete connection magnet cached index, if possible.
            if (null != dc) {
                dc.setIndex(magnet[0].getIndex());
            }
        }
    }
    // Call the magnet and auto connection consumers to assign the new values.
    isAutoConnectionConsumer.accept(auto[0]);
    magnetConsumer.accept(magnet[0]);
}
Also used : DiscreteConnection(org.kie.workbench.common.stunner.core.graph.content.view.DiscreteConnection) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet)

Example 7 with WiresMagnet

use of com.ait.lienzo.client.core.shape.wires.WiresMagnet in project kie-wb-common by kiegroup.

the class WiresConnectorViewTest method testConnectMagnets.

@Test
public void testConnectMagnets() {
    MagnetManager.Magnets headMagnets = mock(MagnetManager.Magnets.class);
    WiresMagnet headMagnet = mock(WiresMagnet.class);
    when(headMagnets.getMagnet(eq(3))).thenReturn(headMagnet);
    when(headMagnet.getIndex()).thenReturn(3);
    Point2D headPoint = new Point2D(10, 20);
    MagnetConnection headConnection = new MagnetConnection.Builder().atX(10).atY(20).magnet(3).build();
    MagnetManager.Magnets tailMagnets = mock(MagnetManager.Magnets.class);
    WiresMagnet tailMagnet = mock(WiresMagnet.class);
    when(tailMagnets.getMagnet(eq(7))).thenReturn(tailMagnet);
    when(tailMagnet.getIndex()).thenReturn(7);
    Point2D tailPoint = new Point2D(100, 200);
    MagnetConnection tailConnection = new MagnetConnection.Builder().atX(100).atY(200).magnet(7).auto(true).build();
    WiresConnection headWiresConnection = mock(WiresConnection.class);
    WiresConnection tailWiresConnection = mock(WiresConnection.class);
    tested.setHeadConnection(headWiresConnection);
    tested.setTailConnection(tailWiresConnection);
    tested.connect(headMagnets, headPoint, headConnection, tailMagnets, tailPoint, tailConnection);
    verify(headWiresConnection, times(1)).setXOffset(eq(0d));
    verify(headWiresConnection, times(1)).setYOffset(eq(0d));
    verify(headWiresConnection, times(1)).setAutoConnection(eq(false));
    verify(headWiresConnection, times(1)).setMagnet(eq(headMagnet));
    verify(tailWiresConnection, times(1)).setXOffset(eq(0d));
    verify(tailWiresConnection, times(1)).setYOffset(eq(0d));
    verify(tailWiresConnection, times(1)).setAutoConnection(eq(true));
    verify(tailWiresConnection, times(1)).setMagnet(eq(tailMagnet));
}
Also used : MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) Point2D(com.ait.lienzo.client.core.types.Point2D) WiresConnection(com.ait.lienzo.client.core.shape.wires.WiresConnection) MagnetManager(com.ait.lienzo.client.core.shape.wires.MagnetManager) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet) Test(org.junit.Test)

Example 8 with WiresMagnet

use of com.ait.lienzo.client.core.shape.wires.WiresMagnet in project kie-wb-common by kiegroup.

the class WiresShapeViewTest method testConsumeChildrenAndConnectors.

@Test
@SuppressWarnings("unchecked")
public void testConsumeChildrenAndConnectors() {
    // Setup children shapes and connectors.
    final Consumer<IDrawable> consumer = mock(Consumer.class);
    final WiresShapeView child1 = newShape();
    final WiresShapeView child2 = newShape();
    tested.add(child1);
    tested.add(child2);
    final WiresConnectorView connector1 = newConnector();
    final WiresConnectorView connector2 = newConnector();
    final MagnetManager.Magnets magnets1 = mock(MagnetManager.Magnets.class);
    final MagnetManager.Magnets magnets2 = mock(MagnetManager.Magnets.class);
    final WiresMagnet magnet1 = mock(WiresMagnet.class);
    final WiresMagnet magnet2 = mock(WiresMagnet.class);
    final WiresConnection connection1 = mock(WiresConnection.class);
    final WiresConnection connection2 = mock(WiresConnection.class);
    final NFastArrayList<WiresConnection> connections1 = new NFastArrayList<WiresConnection>().add(connection1);
    final NFastArrayList<WiresConnection> connections2 = new NFastArrayList<WiresConnection>().add(connection2);
    when(magnets1.size()).thenReturn(1);
    when(magnets1.getMagnet(eq(0))).thenReturn(magnet1);
    when(magnets2.size()).thenReturn(1);
    when(magnets2.getMagnet(eq(0))).thenReturn(magnet2);
    when(magnet1.getConnections()).thenReturn(connections1);
    when(magnet2.getConnections()).thenReturn(connections2);
    when(connection1.getConnector()).thenReturn(connector1);
    when(connection2.getConnector()).thenReturn(connector2);
    child1.setMagnets(magnets1);
    child2.setMagnets(magnets2);
    // Consume.
    tested.consumeChildrenAndConnectors(consumer);
    // Verify.
    verify(consumer, times(1)).accept(eq(tested.getContainer()));
    verify(consumer, times(1)).accept(eq(child1.getContainer()));
    verify(consumer, times(1)).accept(eq(child2.getContainer()));
    verify(consumer, times(1)).accept(eq(connector1.getGroup()));
    verify(consumer, times(1)).accept(eq(connector2.getGroup()));
}
Also used : WiresConnection(com.ait.lienzo.client.core.shape.wires.WiresConnection) MagnetManager(com.ait.lienzo.client.core.shape.wires.MagnetManager) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet) IDrawable(com.ait.lienzo.client.core.shape.IDrawable) Test(org.junit.Test)

Example 9 with WiresMagnet

use of com.ait.lienzo.client.core.shape.wires.WiresMagnet in project lienzo-core by ahome-it.

the class ShapeControlUtils method checkForAndApplyLineSplice.

public static boolean checkForAndApplyLineSplice(final WiresManager wiresManager, final WiresShape shape) {
    if (!wiresManager.isSpliceEnabled() || (shape.getMagnets() == null)) {
        // cannot connect to a shape with no magnets.
        return true;
    }
    boolean accept = true;
    for (final WiresConnector c : wiresManager.getConnectorList()) {
        final Point2DArray linePoints = ((OrthogonalPolyLine) c.getLine()).getComputedPoint2DArray();
        final MultiPath path = shape.getPath();
        Point2DArray intersectPoints = null;
        final Point2D absLoc = path.getComputedLocation();
        intersectPoints = getIntersections(linePoints, path, intersectPoints, absLoc);
        if (((c.getHeadConnection().getMagnet() != null) && (c.getHeadConnection().getMagnet().getMagnets().getWiresShape() == shape)) || ((c.getTailConnection().getMagnet() != null) && (c.getTailConnection().getMagnet().getMagnets().getWiresShape() == shape))) {
            // don't split yourself
            return accept;
        }
        if (intersectPoints != null) {
            final WiresConnection headCon = c.getHeadConnection();
            final WiresConnection tailCon = c.getTailConnection();
            if (intersectPoints.size() == 1) {
                // one arrow end is enclosed in the shape, we can only splice/connect if that connection is not already connected.
                final BoundingBox bbox = shape.getContainer().getComputedBoundingPoints().getBoundingBox();
                if (bbox.contains(headCon.getPoint()) && (headCon.getMagnet() != null)) {
                    return accept;
                } else if (bbox.contains(tailCon.getPoint()) && (headCon.getMagnet() != null)) {
                    return accept;
                } else {
                    throw new RuntimeException("Defensive programming: should not be possible if there is a single intersection.");
                }
            }
            c.getWiresConnectorHandler().getControl().hideControlPoints();
            final Point2DArray oldPoints = c.getLine().getPoint2DArray();
            int firstSegmentIndex = Integer.MAX_VALUE;
            int lastSegmentIndex = 0;
            for (final Point2D p : intersectPoints) {
                final double x = p.getX() + absLoc.getX();
                final double y = p.getY() + absLoc.getY();
                // get first and last segment, this can happen if shape straddles multiple segments of the line
                final int pointIndex = WiresConnectorControlImpl.getIndexForSelectedSegment(c, (int) x, (int) y, oldPoints);
                if (pointIndex < firstSegmentIndex) {
                    firstSegmentIndex = pointIndex;
                }
                if (pointIndex > lastSegmentIndex) {
                    lastSegmentIndex = pointIndex;
                }
            }
            WiresConnector c2 = null;
            // record these, as they may need restoring later.
            double tailXOffset = 0;
            double tailYOffset = 0;
            boolean tailAutoConnection = false;
            Point2D tailPoint = null;
            WiresMagnet tailMagnet = null;
            if (tailCon != null) {
                tailXOffset = tailCon.getXOffset();
                tailYOffset = tailCon.getYOffset();
                tailAutoConnection = tailCon.isAutoConnection();
                tailMagnet = tailCon.getMagnet();
                tailPoint = tailCon.getPoint();
            }
            if (firstSegmentIndex > 0) {
                final Point2DArray newPoints1 = new Point2DArray();
                final Point2DArray newPoints2 = new Point2DArray();
                newPoints1.push(oldPoints.get(0));
                for (int i = 1; i < firstSegmentIndex; i++) {
                    newPoints1.push(oldPoints.get(i));
                }
                final WiresMagnet cmagnet = shape.getMagnets().getMagnet(1);
                // check if isAllowed
                WiresConnectionControlImpl.allowedMagnetAndUpdateAutoConnections(headCon, true, shape, cmagnet, false);
                accept = accept && WiresConnectionControlImpl.allowedMagnetAndUpdateAutoConnections(tailCon, false, shape, cmagnet, false);
                if (!accept) {
                    return accept;
                }
                if (intersectPoints.size() > 1) {
                    final Point2D startPoint = new Point2D(cmagnet.getControl().getX(), cmagnet.getControl().getY());
                    newPoints2.push(startPoint);
                    // will skip any segments between first and last. this happens if a shape straddles multiple segments.
                    for (int i = lastSegmentIndex; i < oldPoints.size(); i++) {
                        newPoints2.push(oldPoints.get(i));
                    }
                    final AbstractDirectionalMultiPointShape<?> line = c.getLine().copy();
                    line.setPoint2DArray(newPoints2);
                    c2 = new WiresConnector(line, c.getHeadDecorator().copy(), c.getTailDecorator().copy());
                    wiresManager.register(c2);
                    final WiresConnection headCon2 = c2.getHeadConnection();
                    headCon2.setAutoConnection(true);
                    // reset, if not already 0
                    headCon2.setXOffset(0);
                    headCon2.setYOffset(0);
                    final WiresConnection tailCon2 = c2.getTailConnection();
                    // preserve tail auto connection
                    tailCon2.setAutoConnection(tailCon.isAutoConnection());
                    tailCon2.setMagnet(tailCon.getMagnet());
                    // reset, if not already 0
                    tailCon2.setXOffset(tailCon.getXOffset());
                    tailCon2.setYOffset(tailCon.getYOffset());
                    tailCon2.setPoint(tailCon.getPoint());
                    accept = accept && WiresConnectionControlImpl.allowedMagnetAndUpdateAutoConnections(headCon2, true, shape, cmagnet, true);
                    if (!accept) {
                        // we already checked isAllowed before mutation, so this in theory should not be needed. Adding for future proofing and completeness - in
                        // case a future version doesn't require identical behavioural logic for allowed and accept.
                        tailCon2.setMagnet(null);
                        wiresManager.deregister(c2);
                        return accept;
                    }
                }
                // this is done after the potential newPoitns2, as it reads values from the original connector.
                final Point2D endPoint = new Point2D(cmagnet.getControl().getX(), cmagnet.getControl().getY());
                newPoints1.push(endPoint);
                tailCon.setAutoConnection(true);
                // reset, if not already 0
                tailCon.setXOffset(0);
                tailCon.setYOffset(0);
                tailCon.setPoint(endPoint);
                c.getLine().setPoint2DArray(newPoints1);
                accept = accept && WiresConnectionControlImpl.allowedMagnetAndUpdateAutoConnections(tailCon, false, shape, cmagnet, true);
                if (!accept) {
                    // case a future version doesn't require identical behavioural logic for allowed and accept.
                    if (c2 != null) {
                        c2.getTailConnection().setMagnet(null);
                        c2.getHeadConnection().setMagnet(null);
                        wiresManager.deregister(c2);
                    }
                    tailCon.setAutoConnection(tailAutoConnection);
                    // reset, if not already 0
                    tailCon.setXOffset(tailXOffset);
                    tailCon.setYOffset(tailYOffset);
                    tailCon.setMagnet(tailMagnet);
                    tailCon.setPoint(tailPoint);
                    return accept;
                }
            }
        }
    }
    return accept;
}
Also used : MultiPath(com.ait.lienzo.client.core.shape.MultiPath) WiresConnector(com.ait.lienzo.client.core.shape.wires.WiresConnector) WiresConnection(com.ait.lienzo.client.core.shape.wires.WiresConnection) Point2DArray(com.ait.lienzo.client.core.types.Point2DArray) Point2D(com.ait.lienzo.client.core.types.Point2D) OrthogonalPolyLine(com.ait.lienzo.client.core.shape.OrthogonalPolyLine) BoundingBox(com.ait.lienzo.client.core.types.BoundingBox) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet)

Example 10 with WiresMagnet

use of com.ait.lienzo.client.core.shape.wires.WiresMagnet in project lienzo-core by ahome-it.

the class WiresMagnetsControlImpl method shapeChanged.

@Override
public void shapeChanged() {
    final IControlHandleList controlHandles = null != getMagnets() ? getMagnets().getMagnets() : null;
    if ((null == controlHandles) || controlHandles.isEmpty()) {
        return;
    }
    final Direction[] cardinals = controlHandles.size() == 9 ? EIGHT_CARDINALS : FOUR_CARDINALS;
    final Point2DArray points = MagnetManager.getWiresIntersectionPoints(shape, cardinals);
    final int size = controlHandles.size() <= points.size() ? controlHandles.size() : points.size();
    for (int i = 0; i < size; i++) {
        final Point2D p = points.get(i);
        final WiresMagnet m = (WiresMagnet) controlHandles.getHandle(i);
        m.setRx(p.getX()).setRy(p.getY());
    }
    shapeMoved();
}
Also used : Point2DArray(com.ait.lienzo.client.core.types.Point2DArray) Point2D(com.ait.lienzo.client.core.types.Point2D) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet) Direction(com.ait.lienzo.shared.core.types.Direction) IControlHandleList(com.ait.lienzo.client.core.shape.wires.IControlHandleList)

Aggregations

WiresMagnet (com.ait.lienzo.client.core.shape.wires.WiresMagnet)10 WiresConnection (com.ait.lienzo.client.core.shape.wires.WiresConnection)6 Point2D (com.ait.lienzo.client.core.types.Point2D)5 Test (org.junit.Test)4 IControlHandleList (com.ait.lienzo.client.core.shape.wires.IControlHandleList)3 MagnetManager (com.ait.lienzo.client.core.shape.wires.MagnetManager)3 MagnetConnection (org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection)3 Point2DArray (com.ait.lienzo.client.core.types.Point2DArray)2 IDrawable (com.ait.lienzo.client.core.shape.IDrawable)1 IPrimitive (com.ait.lienzo.client.core.shape.IPrimitive)1 MultiPath (com.ait.lienzo.client.core.shape.MultiPath)1 OrthogonalPolyLine (com.ait.lienzo.client.core.shape.OrthogonalPolyLine)1 Shape (com.ait.lienzo.client.core.shape.Shape)1 ControlHandleList (com.ait.lienzo.client.core.shape.wires.ControlHandleList)1 WiresConnector (com.ait.lienzo.client.core.shape.wires.WiresConnector)1 WiresShape (com.ait.lienzo.client.core.shape.wires.WiresShape)1 BoundingBox (com.ait.lienzo.client.core.types.BoundingBox)1 Direction (com.ait.lienzo.shared.core.types.Direction)1 Element (org.kie.workbench.common.stunner.core.graph.Element)1 BoundImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl)1