Search in sources :

Example 21 with BoundingBox

use of javafx.geometry.BoundingBox in project JFoenix by jfoenixadmin.

the class JFXMasonryPane method layoutChildren.

/**
 * {@inheritDoc}
 */
@Override
protected void layoutChildren() {
    performingLayout = true;
    int col, row;
    col = (int) Math.floor((getWidth() + getHSpacing() - snappedLeftInset() - snappedRightInset()) / (getCellWidth() + getHSpacing()));
    col = getLimitColumn() != -1 && col > getLimitColumn() ? getLimitColumn() : col;
    if (matrix != null && col == matrix[0].length) {
        performingLayout = false;
        return;
    }
    // (int) Math.floor(this.getHeight() / (cellH + 2*vSpacing));
    row = getLimitRow();
    matrix = new int[row][col];
    double minWidth = -1;
    double minHeight = -1;
    List<BoundingBox> newBoxes;
    List<Region> managedChildren = getManagedChildren();
    // filter Region nodes
    for (int i = 0; i < managedChildren.size(); i++) {
        if (!(managedChildren.get(i) instanceof Region)) {
            managedChildren.remove(i);
            i--;
        }
    }
    // get bounding boxes layout
    newBoxes = layoutMode.get().fillGrid(matrix, managedChildren, getCellWidth(), getCellHeight(), row, col, getHSpacing(), getVSpacing());
    if (newBoxes == null) {
        performingLayout = false;
        return;
    }
    HashMap<Node, BoundingBox> oldBoxes = boundingBoxes;
    if (dirtyBoxes) {
        boundingBoxes = new HashMap<>();
    }
    for (int i = 0; i < managedChildren.size() && i < newBoxes.size(); i++) {
        final Region child = managedChildren.get(i);
        final BoundingBox boundingBox = newBoxes.get(i);
        if (!(child instanceof GridPane)) {
            double blockX;
            double blockY;
            double blockWidth;
            double blockHeight;
            if (boundingBox != null) {
                blockX = boundingBox.getMinY() * getCellWidth() + boundingBox.getMinY() * getHSpacing() + snappedLeftInset();
                blockY = boundingBox.getMinX() * getCellHeight() + boundingBox.getMinX() * getVSpacing() + snappedTopInset();
                blockWidth = boundingBox.getWidth() * getCellWidth() + (boundingBox.getWidth() - 1) * getHSpacing();
                blockHeight = boundingBox.getHeight() * getCellHeight() + (boundingBox.getHeight() - 1) * getVSpacing();
            } else {
                blockX = child.getLayoutX();
                blockY = child.getLayoutY();
                blockWidth = -1;
                blockHeight = -1;
            }
            if (animationMap == null) {
                // init static children
                child.setPrefSize(blockWidth, blockHeight);
                child.resizeRelocate(blockX, blockY, blockWidth, blockHeight);
            } else {
                BoundingBox oldBoundingBox = oldBoxes.get(child);
                if (oldBoundingBox == null || (!oldBoundingBox.equals(boundingBox) && dirtyBoxes)) {
                    // handle new children
                    child.setOpacity(0);
                    child.setPrefSize(blockWidth, blockHeight);
                    child.resizeRelocate(blockX, blockY, blockWidth, blockHeight);
                }
                if (boundingBox != null) {
                    // handle children repositioning
                    if (child.getWidth() != blockWidth || child.getHeight() != blockHeight) {
                        child.setOpacity(0);
                        child.setPrefSize(blockWidth, blockHeight);
                        child.resizeRelocate(blockX, blockY, blockWidth, blockHeight);
                    }
                    final KeyFrame keyFrame = new KeyFrame(Duration.millis(2000), new KeyValue(child.opacityProperty(), 1, Interpolator.LINEAR), new KeyValue(child.layoutXProperty(), blockX, Interpolator.LINEAR), new KeyValue(child.layoutYProperty(), blockY, Interpolator.LINEAR));
                    animationMap.put(child, new CachedTransition(child, new Timeline(keyFrame)) {

                        {
                            setCycleDuration(Duration.seconds(0.320));
                            setDelay(Duration.seconds(0));
                            setOnFinished((finish) -> {
                                child.setLayoutX(blockX);
                                child.setLayoutY(blockY);
                                child.setOpacity(1);
                            });
                        }
                    });
                } else {
                    // handle children is being hidden ( cause it can't fit in the pane )
                    final KeyFrame keyFrame = new KeyFrame(Duration.millis(2000), new KeyValue(child.opacityProperty(), 0, Interpolator.LINEAR), new KeyValue(child.layoutXProperty(), blockX, Interpolator.LINEAR), new KeyValue(child.layoutYProperty(), blockY, Interpolator.LINEAR));
                    animationMap.put(child, new CachedTransition(child, new Timeline(keyFrame)) {

                        {
                            setCycleDuration(Duration.seconds(0.320));
                            setDelay(Duration.seconds(0));
                            setOnFinished((finish) -> {
                                child.setLayoutX(blockX);
                                child.setLayoutY(blockY);
                                child.setOpacity(0);
                            });
                        }
                    });
                }
            }
            // update bounding box
            boundingBoxes.put(child, boundingBox);
            if (boundingBox != null) {
                if (blockX + blockWidth > minWidth) {
                    minWidth = blockX + blockWidth;
                }
                if (blockY + blockHeight > minHeight) {
                    minHeight = blockY + blockHeight;
                }
            }
        }
    }
    if (minHeight != -1) {
        minHeight += snappedBottomInset();
        setPrefHeight(minHeight);
    }
    if (animationMap == null) {
        animationMap = new HashMap<>();
    }
    trans.stop();
    ParallelTransition newTransition = new ParallelTransition();
    newTransition.getChildren().addAll(animationMap.values());
    newTransition.play();
    trans = newTransition;
    dirtyBoxes = false;
    performingLayout = false;
}
Also used : KeyFrame(javafx.animation.KeyFrame) ObjectProperty(javafx.beans.property.ObjectProperty) BoundingBox(javafx.geometry.BoundingBox) Node(javafx.scene.Node) Timeline(javafx.animation.Timeline) DoubleProperty(javafx.beans.property.DoubleProperty) HashMap(java.util.HashMap) IntegerProperty(javafx.beans.property.IntegerProperty) Transition(javafx.animation.Transition) ArrayList(java.util.ArrayList) ParallelTransition(javafx.animation.ParallelTransition) CachedTransition(com.jfoenix.transitions.CachedTransition) Duration(javafx.util.Duration) List(java.util.List) Region(javafx.scene.layout.Region) Interpolator(javafx.animation.Interpolator) ListChangeListener(javafx.collections.ListChangeListener) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) WeakListChangeListener(javafx.collections.WeakListChangeListener) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) KeyValue(javafx.animation.KeyValue) GridPane(javafx.scene.layout.GridPane) Pane(javafx.scene.layout.Pane) GridPane(javafx.scene.layout.GridPane) KeyValue(javafx.animation.KeyValue) Node(javafx.scene.Node) Timeline(javafx.animation.Timeline) BoundingBox(javafx.geometry.BoundingBox) Region(javafx.scene.layout.Region) KeyFrame(javafx.animation.KeyFrame) CachedTransition(com.jfoenix.transitions.CachedTransition) ParallelTransition(javafx.animation.ParallelTransition)

Example 22 with BoundingBox

use of javafx.geometry.BoundingBox in project JFoenix by jfoenixadmin.

the class JFXDecorator method maximize.

private void maximize(SVGGlyph resizeMin, SVGGlyph resizeMax) {
    if (!isCustomMaximize()) {
        primaryStage.setMaximized(!primaryStage.isMaximized());
        maximized = primaryStage.isMaximized();
        if (primaryStage.isMaximized()) {
            btnMax.setGraphic(resizeMin);
            btnMax.setTooltip(new Tooltip("Restore Down"));
        } else {
            btnMax.setGraphic(resizeMax);
            btnMax.setTooltip(new Tooltip("Maximize"));
        }
    } else {
        if (!maximized) {
            // store original bounds
            originalBox = new BoundingBox(primaryStage.getX(), primaryStage.getY(), primaryStage.getWidth(), primaryStage.getHeight());
            // get the max stage bounds
            Screen screen = Screen.getScreensForRectangle(primaryStage.getX(), primaryStage.getY(), primaryStage.getWidth(), primaryStage.getHeight()).get(0);
            Rectangle2D bounds = screen.getVisualBounds();
            maximizedBox = new BoundingBox(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight());
            // maximized the stage
            primaryStage.setX(maximizedBox.getMinX());
            primaryStage.setY(maximizedBox.getMinY());
            primaryStage.setWidth(maximizedBox.getWidth());
            primaryStage.setHeight(maximizedBox.getHeight());
            btnMax.setGraphic(resizeMin);
            btnMax.setTooltip(new Tooltip("Restore Down"));
        } else {
            // restore stage to its original size
            primaryStage.setX(originalBox.getMinX());
            primaryStage.setY(originalBox.getMinY());
            primaryStage.setWidth(originalBox.getWidth());
            primaryStage.setHeight(originalBox.getHeight());
            originalBox = null;
            btnMax.setGraphic(resizeMax);
            btnMax.setTooltip(new Tooltip("Maximize"));
        }
        maximized = !maximized;
    }
}
Also used : Screen(javafx.stage.Screen) Tooltip(javafx.scene.control.Tooltip) BoundingBox(javafx.geometry.BoundingBox) Rectangle2D(javafx.geometry.Rectangle2D)

Example 23 with BoundingBox

use of javafx.geometry.BoundingBox in project JFoenix by jfoenixadmin.

the class JFXHighlighter method getMatchingBounds.

private ArrayList<Bounds> getMatchingBounds(String query, Text text) {
    // find local text bounds in parent
    Bounds textBounds = parent.sceneToLocal(text.localToScene(text.getBoundsInLocal()));
    ArrayList<Bounds> rectBounds = new ArrayList<>();
    TextLayout textLayout = null;
    try {
        textLayout = (TextLayout) textLayoutMethod.invoke(text);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    int queryLength = query.length();
    TextLine[] lines = textLayout.getLines();
    // handle matches in all lines
    for (int i = 0; i < lines.length; i++) {
        TextLine line = lines[i];
        String lineText = text.getText().substring(line.getStart(), line.getStart() + line.getLength());
        final String lineTextLow = lineText.toLowerCase();
        final String queryLow = query.toLowerCase();
        int beginIndex = lineTextLow.indexOf(queryLow);
        if (beginIndex == -1) {
            continue;
        }
        RectBounds lineBounds = (line.getBounds());
        // compute Y layout
        double height = Math.round(lineBounds.getMaxY()) - Math.round(lineBounds.getMinY());
        double startY = height * i;
        // handle multiple matches in one line
        while (beginIndex != -1) {
            // compute X layout
            Text temp = new Text(lineText.substring(beginIndex, beginIndex + queryLength));
            temp.setFont(text.getFont());
            temp.applyCss();
            double width = temp.getLayoutBounds().getWidth();
            temp.setText(lineText.substring(0, beginIndex + queryLength));
            temp.applyCss();
            double maxX = temp.getLayoutBounds().getMaxX();
            double startX = maxX - width;
            rectBounds.add(new BoundingBox(textBounds.getMinX() + startX, textBounds.getMinY() + startY, width, temp.getLayoutBounds().getHeight()));
            beginIndex = lineTextLow.indexOf(queryLow, beginIndex + queryLength);
        }
    }
    return rectBounds;
}
Also used : TextLine(com.sun.javafx.scene.text.TextLine) RectBounds(com.sun.javafx.geom.RectBounds) Bounds(javafx.geometry.Bounds) Text(javafx.scene.text.Text) InvocationTargetException(java.lang.reflect.InvocationTargetException) CacheHint(javafx.scene.CacheHint) Paint(javafx.scene.paint.Paint) TextLayout(com.sun.javafx.scene.text.TextLayout) BoundingBox(javafx.geometry.BoundingBox) RectBounds(com.sun.javafx.geom.RectBounds)

Aggregations

BoundingBox (javafx.geometry.BoundingBox)23 Bounds (javafx.geometry.Bounds)11 Test (org.junit.Test)7 Point2D (javafx.geometry.Point2D)6 Node (javafx.scene.Node)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 DoubleProperty (javafx.beans.property.DoubleProperty)2 ObjectProperty (javafx.beans.property.ObjectProperty)2 SimpleDoubleProperty (javafx.beans.property.SimpleDoubleProperty)2 SimpleObjectProperty (javafx.beans.property.SimpleObjectProperty)2 Rectangle2D (javafx.geometry.Rectangle2D)2 Paint (javafx.scene.paint.Paint)2 Rectangle (javafx.scene.shape.Rectangle)2 JavaFXLibraryNonFatalException (javafxlibrary.exceptions.JavaFXLibraryNonFatalException)2 RobotKeyword (org.robotframework.javalib.annotation.RobotKeyword)2 BSBObject (blue.orchestra.blueSynthBuilder.BSBObject)1 ShapeType (com.almasb.fxgl.physics.box2d.collision.shapes.ShapeType)1 CachedTransition (com.jfoenix.transitions.CachedTransition)1 RectBounds (com.sun.javafx.geom.RectBounds)1