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());
}
}
}
}
}
}
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));
}
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));
}
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);
}
}
}
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;
}
Aggregations