Search in sources :

Example 1 with WiresMagnet

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

the class WiresShapeView method consumeChildrenAndConnectors.

void consumeChildrenAndConnectors(final Consumer<IDrawable> primConsumer) {
    // Move this shape.
    primConsumer.accept(getContainer());
    // Move child shapes.
    final NFastArrayList<WiresShape> childShapes = getChildShapes();
    if (null != childShapes) {
        for (int i = 0; i < childShapes.size(); i++) {
            final WiresShape shape = childShapes.get(i);
            if (shape instanceof WiresShapeView) {
                ((WiresShapeView) shape).consumeChildrenAndConnectors(primConsumer);
            } else {
                primConsumer.accept(shape.getContainer());
            }
        }
    }
    // Move connectors.
    if (null != getMagnets()) {
        for (int i = 0; i < getMagnets().size(); i++) {
            final WiresMagnet magnet = getMagnets().getMagnet(i);
            final NFastArrayList<WiresConnection> connections = magnet.getConnections();
            if (null != connections) {
                for (int j = 0; j < connections.size(); j++) {
                    final WiresConnection connection = connections.get(j);
                    if (null != connection.getConnector()) {
                        primConsumer.accept(connection.getConnector().getGroup());
                    }
                }
            }
        }
    }
}
Also used : WiresShape(com.ait.lienzo.client.core.shape.wires.WiresShape) WiresConnection(com.ait.lienzo.client.core.shape.wires.WiresConnection) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet)

Example 2 with WiresMagnet

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

the class ConnectionAcceptorControlImplTest method testCreateConnections.

@Test
public void testCreateConnections() {
    // New default connection for a graph element.
    Element element = mock(Element.class);
    View<?> content = mock(View.class);
    BoundsImpl bounds = new BoundsImpl(new BoundImpl(0d, 0d), new BoundImpl(10d, 20d));
    when(element.getContent()).thenReturn(content);
    when(content.getBounds()).thenReturn(bounds);
    MagnetConnection c1 = ConnectionAcceptorControlImpl.createConnection(element);
    assertEquals(5, c1.getLocation().getX(), 0);
    assertEquals(10, c1.getLocation().getY(), 0);
    assertEquals(MagnetConnection.MAGNET_CENTER, c1.getMagnetIndex().getAsInt());
    assertFalse(c1.isAuto());
    // New default connection for wires.
    WiresConnection wiresConnection = mock(WiresConnection.class);
    when(wiresConnection.isAutoConnection()).thenReturn(true);
    WiresMagnet wiresMagnet = mock(WiresMagnet.class);
    when(wiresMagnet.getX()).thenReturn(122d);
    when(wiresMagnet.getY()).thenReturn(543d);
    when(wiresMagnet.getIndex()).thenReturn(7);
    MagnetConnection c2 = ConnectionAcceptorControlImpl.createConnection(wiresConnection, wiresMagnet);
    assertEquals(122, c2.getLocation().getX(), 0);
    assertEquals(543, c2.getLocation().getY(), 0);
    assertEquals(7, c2.getMagnetIndex().getAsInt());
    assertTrue(c2.isAuto());
    // Connections (view magnets) can be nullified.
    assertNull(ConnectionAcceptorControlImpl.createConnection(null));
    assertNull(ConnectionAcceptorControlImpl.createConnection(wiresConnection, null));
    assertNull(ConnectionAcceptorControlImpl.createConnection(null, null));
}
Also used : MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) WiresConnection(com.ait.lienzo.client.core.shape.wires.WiresConnection) Element(org.kie.workbench.common.stunner.core.graph.Element) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl) Test(org.junit.Test)

Example 3 with WiresMagnet

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

the class WiresConnectorViewTest method testConnectMagnetsByLocation.

@Test
public void testConnectMagnetsByLocation() {
    // Mocks for head stuff.
    IControlHandleList headControlHandleList = new ControlHandleList(mock(Shape.class));
    WiresMagnet headMagnet1 = mock(WiresMagnet.class);
    when(headMagnet1.getX()).thenReturn(10d);
    when(headMagnet1.getY()).thenReturn(20d);
    when(headMagnet1.getIndex()).thenReturn(0);
    IPrimitive ch1 = mock(IPrimitive.class);
    when(ch1.getLocation()).thenReturn(new Point2D(10d, 20d));
    when(headMagnet1.getControl()).thenReturn(ch1);
    WiresMagnet headMagnet2 = mock(WiresMagnet.class);
    when(headMagnet2.getX()).thenReturn(100d);
    when(headMagnet2.getY()).thenReturn(200d);
    IPrimitive ch2 = mock(IPrimitive.class);
    when(ch2.getLocation()).thenReturn(new Point2D(100d, 200d));
    when(headMagnet2.getControl()).thenReturn(ch2);
    headControlHandleList.add(headMagnet1);
    headControlHandleList.add(headMagnet2);
    MagnetManager.Magnets headMagnets = mock(MagnetManager.Magnets.class);
    when(headMagnets.getMagnets()).thenReturn(headControlHandleList);
    Point2D headAbs = new Point2D(0, 0);
    MagnetConnection headConnection = MagnetConnection.Builder.at(10, 20);
    // Mocks for head tail.
    IControlHandleList tailControlHandleList = new ControlHandleList(mock(Shape.class));
    WiresMagnet tailMagnet1 = mock(WiresMagnet.class);
    when(tailMagnet1.getX()).thenReturn(10d);
    when(tailMagnet1.getY()).thenReturn(20d);
    IPrimitive th1 = mock(IPrimitive.class);
    when(th1.getLocation()).thenReturn(new Point2D(10d, 20d));
    when(tailMagnet1.getControl()).thenReturn(th1);
    WiresMagnet tailMagnet2 = mock(WiresMagnet.class);
    when(tailMagnet2.getX()).thenReturn(100d);
    when(tailMagnet2.getY()).thenReturn(200d);
    when(tailMagnet2.getIndex()).thenReturn(2);
    IPrimitive th2 = mock(IPrimitive.class);
    when(th2.getLocation()).thenReturn(new Point2D(100d, 200d));
    when(tailMagnet2.getControl()).thenReturn(th2);
    tailControlHandleList.add(tailMagnet1);
    tailControlHandleList.add(tailMagnet2);
    MagnetManager.Magnets tailMagnets = mock(MagnetManager.Magnets.class);
    when(tailMagnets.getMagnets()).thenReturn(tailControlHandleList);
    Point2D tailAbs = new Point2D(0, 0);
    MagnetConnection tailConnection = MagnetConnection.Builder.at(100, 200);
    // Mocks both source/target actual connections.
    WiresConnection headWiresConnection = mock(WiresConnection.class);
    WiresConnection tailWiresConnection = mock(WiresConnection.class);
    tested.setHeadConnection(headWiresConnection);
    tested.setTailConnection(tailWiresConnection);
    // Perform the connection.
    tested.connect(headMagnets, headAbs, headConnection, tailMagnets, tailAbs, tailConnection);
    // Verify it sets the right magnets (closest to the location):
    verify(headWiresConnection, never()).setXOffset(anyDouble());
    verify(headWiresConnection, never()).setYOffset(anyDouble());
    verify(headWiresConnection, times(1)).setAutoConnection(eq(false));
    verify(headWiresConnection, times(1)).setMagnet(eq(headMagnet1));
    verify(tailWiresConnection, times(1)).setXOffset(eq(0d));
    verify(tailWiresConnection, times(1)).setYOffset(eq(0d));
    verify(tailWiresConnection, times(1)).setAutoConnection(eq(false));
    verify(tailWiresConnection, times(1)).setMagnet(eq(tailMagnet2));
}
Also used : MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) Shape(com.ait.lienzo.client.core.shape.Shape) 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) IControlHandleList(com.ait.lienzo.client.core.shape.wires.IControlHandleList) ControlHandleList(com.ait.lienzo.client.core.shape.wires.ControlHandleList) IPrimitive(com.ait.lienzo.client.core.shape.IPrimitive) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet) IControlHandleList(com.ait.lienzo.client.core.shape.wires.IControlHandleList) Test(org.junit.Test)

Example 4 with WiresMagnet

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

the class WiresMagnetsControlImpl method shapeMoved.

private void shapeMoved(final double x, final double y) {
    if (null != getMagnets()) {
        final IControlHandleList controlHandles = getMagnets().getMagnets();
        for (int i = 0; i < controlHandles.size(); i++) {
            final WiresMagnet m = (WiresMagnet) controlHandles.getHandle(i);
            m.shapeMoved(x, y);
        }
    }
}
Also used : WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet) IControlHandleList(com.ait.lienzo.client.core.shape.wires.IControlHandleList)

Example 5 with WiresMagnet

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

the class WiresConnectionControlImpl method onMove.

@Override
public boolean onMove(final double dx, final double dy) {
    final Point2D dxy = new Point2D(dx, dy);
    m_adjust = new Point2D(0, 0);
    // this is redetermined on each drag adjust
    m_current_magnet = null;
    final int x = (int) (m_startX + dxy.getX());
    final int y = (int) (m_startY + dxy.getY());
    final String colorKey = BackingColorMapUtils.findColorAtPoint(m_shapesBacking, x, y);
    if ((m_colorKey != null) && (colorKey != null) && !colorKey.equals(m_colorKey)) {
        if (null != m_magnets) {
            m_magnets.hide();
        }
        m_magnets = null;
        m_colorKey = null;
    }
    boolean isAllowed = true;
    if (m_magnets == null) {
        isAllowed = checkAllowAndShowMagnets(colorKey);
    }
    // can be used during drag, as we know the current connection will have a null shape
    // this will cause the other side to be updated
    m_connector.updateForSpecialConnections(false);
    if (isAllowed) {
        if (null != m_magnets) {
            final String magnetColorKey = BackingColorMapUtils.findColorAtPoint(m_magnetsBacking, x, y);
            if (magnetColorKey == null) {
                if (null != m_magnets) {
                    m_magnets.hide();
                }
                m_magnets = null;
                m_colorKey = null;
            } else {
                // Take into account that it can be null, when over the main shape, instead of a magnet
                final WiresMagnet potentialMagnet = m_magnet_color_map.get(magnetColorKey);
                if ((m_connector.getHeadConnection().getMagnet() != potentialMagnet) && (m_connector.getTailConnection().getMagnet() != potentialMagnet)) {
                    // make sure we don't add a connection's head and tail to the same magnet
                    m_current_magnet = potentialMagnet;
                } else if (potentialMagnet == null) {
                    m_current_magnet = null;
                }
            }
        }
        if (null != m_current_magnet) {
            final Shape<?> control = m_current_magnet.getControl().asShape();
            if (control != null) {
                // If there is a control, snap to it
                final Point2D absControl = control.getComputedLocation();
                final double targetX = absControl.getX();
                final double targetY = absControl.getY();
                final double tx = targetX - m_startX - dxy.getX();
                final double ty = targetY - m_startY - dxy.getY();
                if ((tx != 0) || (ty != 0)) {
                    m_adjust = new Point2D(dxy.getX() + tx, dxy.getY() + ty);
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Point2D(com.ait.lienzo.client.core.types.Point2D) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet)

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