Search in sources :

Example 1 with ViewShape

use of net.sf.latexdraw.view.jfx.ViewShape in project latexdraw by arnobl.

the class Hand method getViewShape.

private static Optional<ViewShape<?>> getViewShape(final Optional<Node> node) {
    if (node.isPresent()) {
        final Node value = node.get();
        Node parent = value.getParent();
        while (parent != null && !(parent instanceof ViewShape<?>)) {
            parent = parent.getParent();
        }
        return Optional.ofNullable(getRealViewShape((ViewShape<?>) parent));
    }
    return Optional.empty();
}
Also used : Node(javafx.scene.Node) ViewShape(net.sf.latexdraw.view.jfx.ViewShape)

Example 2 with ViewShape

use of net.sf.latexdraw.view.jfx.ViewShape in project latexdraw by arnobl.

the class DistributeShapes method distributeEq.

/**
 * Distributes at equal distance between the shapes.
 */
private void distributeEq() {
    final List<ViewShape<?>> sortedSh = new ArrayList<>();
    final List<Double> mins = new ArrayList<>();
    final List<Double> maxs = new ArrayList<>();
    for (final ViewShape<?> view : views) {
        final double coord = distribution == Distribution.HORIZ_EQ ? view.getBoundsInLocal().getMinX() : view.getBoundsInLocal().getMinY();
        final OptionalInt res = IntStream.range(0, mins.size()).filter(index -> coord < mins.get(index)).findFirst();
        if (res.isPresent()) {
            final int i = res.getAsInt();
            sortedSh.add(i, view);
            mins.add(i, coord);
            maxs.add(i, distribution == Distribution.HORIZ_EQ ? view.getBoundsInLocal().getMaxX() : view.getBoundsInLocal().getMaxY());
        } else {
            sortedSh.add(view);
            mins.add(coord);
            maxs.add(distribution == Distribution.HORIZ_EQ ? view.getBoundsInLocal().getMaxX() : view.getBoundsInLocal().getMaxY());
        }
    }
    double gap = mins.get(mins.size() - 1) - maxs.get(0);
    for (int i = 1, size = sortedSh.size() - 1; i < size; i++) {
        gap -= maxs.get(i) - mins.get(i);
    }
    gap /= sortedSh.size() - 1;
    final double finalGap = gap;
    if (Distribution.isVertical(distribution)) {
        IntStream.range(1, sortedSh.size() - 1).forEach(i -> ((IShape) sortedSh.get(i).getUserData()).translate(0d, sortedSh.get(i - 1).getBoundsInLocal().getMaxY() + finalGap - mins.get(i)));
    } else {
        IntStream.range(1, sortedSh.size() - 1).forEach(i -> ((IShape) sortedSh.get(i).getUserData()).translate(sortedSh.get(i - 1).getBoundsInLocal().getMaxX() + finalGap - mins.get(i), 0d));
    }
}
Also used : IntStream(java.util.stream.IntStream) ViewShape(net.sf.latexdraw.view.jfx.ViewShape) List(java.util.List) IShape(net.sf.latexdraw.models.interfaces.shape.IShape) LangTool(net.sf.latexdraw.util.LangTool) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) Canvas(net.sf.latexdraw.view.jfx.Canvas) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) OptionalInt(java.util.OptionalInt) ViewShape(net.sf.latexdraw.view.jfx.ViewShape)

Example 3 with ViewShape

use of net.sf.latexdraw.view.jfx.ViewShape in project latexdraw by arnobl.

the class DistributeShapes method distributeNotEq.

/**
 * Distributes using bottom/top/left/right reference.
 */
private void distributeNotEq() {
    final List<ViewShape<?>> sortedSh = new ArrayList<>();
    final List<Double> centres = new ArrayList<>();
    for (final ViewShape<?> view : views) {
        double x = 0;
        switch(distribution) {
            case HORIZ_LEFT:
                x = view.getBoundsInLocal().getMinX();
                break;
            case HORIZ_MID:
                x = (view.getBoundsInLocal().getMinX() + view.getBoundsInLocal().getMaxX()) / 2d;
                break;
            case HORIZ_RIGHT:
                x = view.getBoundsInLocal().getMaxX();
                break;
            case VERT_BOT:
                x = view.getBoundsInLocal().getMaxY();
                break;
            case VERT_MID:
                x = (view.getBoundsInLocal().getMinY() + view.getBoundsInLocal().getMaxY()) / 2d;
                break;
            case VERT_TOP:
                x = view.getBoundsInLocal().getMinY();
                break;
            default:
        }
        final double finalX = x;
        final OptionalInt res = IntStream.range(0, centres.size()).filter(index -> finalX < centres.get(index)).findFirst();
        if (res.isPresent()) {
            final int i = res.getAsInt();
            sortedSh.add(i, view);
            centres.add(i, x);
        } else {
            sortedSh.add(view);
            centres.add(x);
        }
    }
    final double gap = (centres.get(centres.size() - 1) - centres.get(0)) / (views.size() - 1);
    if (Distribution.isVertical(distribution)) {
        IntStream.range(1, sortedSh.size() - 1).forEach(i -> ((IShape) sortedSh.get(i).getUserData()).translate(0d, centres.get(0) + i * gap - centres.get(i)));
    } else {
        IntStream.range(1, sortedSh.size() - 1).forEach(i -> ((IShape) sortedSh.get(i).getUserData()).translate(centres.get(0) + i * gap - centres.get(i), 0d));
    }
}
Also used : IntStream(java.util.stream.IntStream) ViewShape(net.sf.latexdraw.view.jfx.ViewShape) List(java.util.List) IShape(net.sf.latexdraw.models.interfaces.shape.IShape) LangTool(net.sf.latexdraw.util.LangTool) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) Canvas(net.sf.latexdraw.view.jfx.Canvas) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) OptionalInt(java.util.OptionalInt) ViewShape(net.sf.latexdraw.view.jfx.ViewShape)

Example 4 with ViewShape

use of net.sf.latexdraw.view.jfx.ViewShape in project latexdraw by arnobl.

the class TestAlignShape method setUp.

@Override
@Before
public void setUp() {
    super.setUp();
    canvas = Mockito.mock(Canvas.class);
    final IShapeFactory fac = ShapeFactory.INST;
    shapes = fac.createGroup();
    shapes.addShape(fac.createRectangle(fac.createPoint(10d, -2d), 6d, 6d));
    shapes.addShape(fac.createRectangle(fac.createPoint(-5d, 20d), 12d, 15d));
    shapes.addShape(fac.createRectangle(fac.createPoint(14d, 60d), 20d, 16d));
    views = new Group();
    IntStream.range(0, shapes.size()).forEach(i -> {
        views.getChildren().add(ViewFactory.INSTANCE.createView(shapes.getShapeAt(i)).get());
        Mockito.when(canvas.getViewFromShape(shapes.getShapeAt(i))).thenReturn(Optional.of((ViewShape<?>) views.getChildren().get(i)));
    });
}
Also used : IGroup(net.sf.latexdraw.models.interfaces.shape.IGroup) Group(javafx.scene.Group) Canvas(net.sf.latexdraw.view.jfx.Canvas) ViewShape(net.sf.latexdraw.view.jfx.ViewShape) IShapeFactory(net.sf.latexdraw.models.interfaces.shape.IShapeFactory) Before(org.junit.Before)

Aggregations

ViewShape (net.sf.latexdraw.view.jfx.ViewShape)4 Canvas (net.sf.latexdraw.view.jfx.Canvas)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 OptionalInt (java.util.OptionalInt)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 IShape (net.sf.latexdraw.models.interfaces.shape.IShape)2 LangTool (net.sf.latexdraw.util.LangTool)2 Group (javafx.scene.Group)1 Node (javafx.scene.Node)1 IGroup (net.sf.latexdraw.models.interfaces.shape.IGroup)1 IShapeFactory (net.sf.latexdraw.models.interfaces.shape.IShapeFactory)1 Before (org.junit.Before)1