Java Code Examples for javafx.scene.Node#localToScene()

The following examples show how to use javafx.scene.Node#localToScene() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FXEventQueueDevice.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
protected void ensureVisible(Node target) {
    ScrollPane scrollPane = getParentScrollPane(target);
    if (scrollPane == null) {
        return;
    }
    Node content = scrollPane.getContent();
    Bounds contentBounds = content.localToScene(content.getBoundsInLocal());
    Bounds viewportBounds = scrollPane.getViewportBounds();
    Bounds nodeBounds = target.localToScene(target.getBoundsInLocal());
    if (scrollPane.contains(nodeBounds.getMinX() - contentBounds.getMinX(), nodeBounds.getMinY() - contentBounds.getMinY())) {
        return;
    }
    double toVScroll = (nodeBounds.getMinY() - contentBounds.getMinY())
            * ((scrollPane.getVmax() - scrollPane.getVmin()) / (contentBounds.getHeight() - viewportBounds.getHeight()));
    scrollPane.setVvalue(toVScroll);
    double toHScroll = (nodeBounds.getMinX() - contentBounds.getMinX())
            * ((scrollPane.getHmax() - scrollPane.getHmin()) / (contentBounds.getWidth() - viewportBounds.getWidth()));
    scrollPane.setHvalue(toHScroll);
}
 
Example 2
Source File: JavaFXElementPropertyAccessor.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
protected int getIndexAt(ListView<?> listView, Point2D point) {
    if (point == null) {
        return listView.getSelectionModel().getSelectedIndex();
    }
    point = listView.localToScene(point);
    Set<Node> lookupAll = getListCells(listView);
    ListCell<?> selected = null;
    for (Node cellNode : lookupAll) {
        Bounds boundsInScene = cellNode.localToScene(cellNode.getBoundsInLocal(), true);
        if (boundsInScene.contains(point)) {
            selected = (ListCell<?>) cellNode;
            break;
        }
    }
    if (selected == null) {
        return -1;
    }
    return selected.getIndex();
}
 
Example 3
Source File: JavaFXElementPropertyAccessor.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
public int getRowAt(TreeView<?> treeView, Point2D point) {
    if (point == null) {
        return treeView.getSelectionModel().getSelectedIndex();
    }
    point = treeView.localToScene(point);
    int itemCount = treeView.getExpandedItemCount();
    @SuppressWarnings("rawtypes")
    List<TreeCell> cells = new ArrayList<>();
    for (int i = 0; i < itemCount; i++) {
        cells.add(getCellAt(treeView, i));
    }
    TreeCell<?> selected = null;
    for (Node cellNode : cells) {
        Bounds boundsInScene = cellNode.localToScene(cellNode.getBoundsInLocal(), true);
        if (boundsInScene.contains(point)) {
            selected = (TreeCell<?>) cellNode;
            break;
        }
    }
    if (selected == null) {
        return -1;
    }
    return selected.getIndex();
}
 
Example 4
Source File: JFXTooltip.java    From JFoenix with Apache License 2.0 6 votes vote down vote up
public void showOnAnchors(Node ownerNode, double anchorX, double anchorY) {
    hiding = false;
    final Bounds sceneBounds = ownerNode.localToScene(ownerNode.getBoundsInLocal());
    if (isShowing()) {
        animation.setOnFinished(null);
        animation.reverseAndContinue();
        anchorX += ownerX(ownerNode, sceneBounds);
        anchorY += ownerY(ownerNode, sceneBounds);
        setAnchorX(getUpdatedAnchorX(anchorX));
        setAnchorY(getUpdatedAnchorY(anchorY));
    } else {
        anchorX += ownerX(ownerNode, sceneBounds);
        anchorY += ownerY(ownerNode, sceneBounds);
        super.show(ownerNode, anchorX, anchorY);
    }
}
 
Example 5
Source File: JFXTooltip.java    From JFoenix with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * this method ignores anchorX, anchorY. It shows the tooltip according
 * to tooltip {@link JFXTooltip#pos} field
 * <p>
 * NOTE: if you want to manually show the tooltip on forced local anchors
 * you can use {@link JFXTooltip#showOnAnchors(Node, double, double)} method.
 */
@Override
public void show(Node ownerNode, double anchorX, double anchorY) {
    // if tooltip hide animation still running, then hide method is not called yet
    // thus only reverse the animation to show the tooltip again
    hiding = false;
    final Bounds sceneBounds = ownerNode.localToScene(ownerNode.getBoundsInLocal());
    if (isShowing()) {
        animation.setOnFinished(null);
        animation.reverseAndContinue();
        anchorX = ownerX(ownerNode, sceneBounds) + getHPosForNode(sceneBounds);
        anchorY = ownerY(ownerNode, sceneBounds) + getVPosForNode(sceneBounds);
        setAnchorY(getUpdatedAnchorY(anchorY));
        setAnchorX(getUpdatedAnchorX(anchorX));
    } else {
        // tooltip was not showing compute its anchors and show it
        anchorX = ownerX(ownerNode, sceneBounds) + getHPosForNode(sceneBounds);
        anchorY = ownerY(ownerNode, sceneBounds) + getVPosForNode(sceneBounds);
        super.show(ownerNode, anchorX, anchorY);
    }
}
 
Example 6
Source File: ScrollPaneSample2.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
private void scrollTo(Node target) {
    ScrollPane scrollPane = getParentScrollPane(target);
    if (scrollPane == null)
        return;
    Node content = scrollPane.getContent();
    Bounds contentBounds = content.localToScene(content.getBoundsInLocal());
    Bounds viewportBounds = scrollPane.getViewportBounds();
    Bounds nodeBounds = target.localToScene(target.getBoundsInLocal());
    double toVScroll = (nodeBounds.getMinY() - contentBounds.getMinY())
            * ((scrollPane.getVmax() - scrollPane.getVmin()) / (contentBounds.getHeight() - viewportBounds.getHeight()));
    if (toVScroll >= scrollPane.getVmin() && toVScroll < scrollPane.getVmax())
        scrollPane.setVvalue(toVScroll);
    double toHScroll = (nodeBounds.getMinX() - contentBounds.getMinX())
            * ((scrollPane.getHmax() - scrollPane.getHmin()) / (contentBounds.getWidth() - viewportBounds.getWidth()));
    if (toHScroll >= scrollPane.getHmin() && toHScroll < scrollPane.getHmax())
        scrollPane.setHvalue(toHScroll);
}
 
Example 7
Source File: JFXPopup.java    From JFoenix with Apache License 2.0 6 votes vote down vote up
/**
 * show the popup according to the specified position with a certain offset
 *
 * @param vAlign      can be TOP/BOTTOM
 * @param hAlign      can be LEFT/RIGHT
 * @param initOffsetX on the x axis
 * @param initOffsetY on the y axis
 */
public void show(Node node, PopupVPosition vAlign, PopupHPosition hAlign, double initOffsetX, double initOffsetY) {
    if (!isShowing()) {
        if (node.getScene() == null || node.getScene().getWindow() == null) {
            throw new IllegalStateException("Can not show popup. The node must be attached to a scene/window.");
        }
        Window parent = node.getScene().getWindow();
        final Point2D origin = node.localToScene(0, 0);
        final double anchorX = parent.getX() + origin.getX()
            + node.getScene().getX() + (hAlign == PopupHPosition.RIGHT ? ((Region) node).getWidth() : 0);
        final double anchorY = parent.getY() + origin.getY()
            + node.getScene()
                  .getY() + (vAlign == PopupVPosition.BOTTOM ? ((Region) node).getHeight() : 0);
        this.show(parent, anchorX, anchorY);
        ((JFXPopupSkin) getSkin()).reset(vAlign, hAlign, initOffsetX, initOffsetY);
        Platform.runLater(() -> ((JFXPopupSkin) getSkin()).animate());
    }
}
 
Example 8
Source File: OrthogonalRouter.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private Polygon[] getTriangles(Connection connection, int i) {
	Node anchorage = connection.getAnchor(i).getAnchorage();
	Bounds boundsInScene = anchorage
			.localToScene(anchorage.getLayoutBounds());
	Rectangle rectangle = FX2Geometry.toRectangle(boundsInScene);
	Polygon top = new Polygon(rectangle.getTopLeft(),
			rectangle.getTopRight(), rectangle.getCenter());
	Polygon bottom = new Polygon(rectangle.getBottomLeft(),
			rectangle.getBottomRight(), rectangle.getCenter());
	Polygon left = new Polygon(rectangle.getTopLeft(),
			rectangle.getBottomLeft(), rectangle.getCenter());
	Polygon right = new Polygon(rectangle.getTopRight(),
			rectangle.getBottomRight(), rectangle.getCenter());
	return new Polygon[] { top, right, bottom, left };
}
 
Example 9
Source File: UITest.java    From HubTurbo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Like drag(from).to(to), but does not relocate the mouse if the target moves.
 */
public void dragUnconditionally(FilterPanel panelFrom, FilterPanel panelTo) {
    Node from = dragSrc(panelFrom);
    Node to = dragDest(panelTo);
    Bounds fromBound = from.localToScene(from.getBoundsInLocal());
    Bounds toBound = to.localToScene(to.getBoundsInLocal());
    drag(fromBound.getMinX(), fromBound.getMaxY(), MouseButton.PRIMARY)
        .moveTo(toBound.getMaxX(), toBound.getMaxY())
        .drop();
}
 
Example 10
Source File: WoodlandController.java    From examples-javafx-repos1 with Apache License 2.0 5 votes vote down vote up
private boolean isPicked(Node n, double sceneX, double sceneY) {
	Bounds bounds = n.getLayoutBounds();
	Bounds boundsScene = n.localToScene( bounds );
	if( (sceneX >= boundsScene.getMinX()) && (sceneY >= boundsScene.getMinY()) &&
			(sceneX <= boundsScene.getMaxX()) && ( sceneY <= boundsScene.getMaxY() ) ) {
		return true;
	}
	return false;
}
 
Example 11
Source File: AnimatedTableRow.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
private Point2D computeAnimationEndPoint(
		final TableView<Person> destinationTable) {
	// End animation at first row (bottom of table header)
	final Node toTableHeader = destinationTable.lookup(".column-header-background");
	if (toTableHeader != null) {
		final Bounds tableHeaderBounds = toTableHeader.localToScene(toTableHeader.getBoundsInLocal()); // relative to Scene
		Point2D animationEndPoint = new Point2D(tableHeaderBounds.getMinX(), tableHeaderBounds.getMaxY());
		return animationEndPoint;
	} else { // fallback in case lookup fails for some reason
	    // just approximate at 24 pixels below top of table:
	    Point2D tableLocation = destinationTable.localToScene(new Point2D(0,0));
	    return new Point2D(tableLocation.getX(), tableLocation.getY() + 24);
	}
}
 
Example 12
Source File: MarqueeOnDragHandler.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endDrag(MouseEvent e, Dimension delta) {
	if (invalidGesture) {
		return;
	}

	// compute bounding box in scene coordinates
	IRootPart<? extends Node> root = getHost().getRoot();
	Node rootVisual = root.getVisual();
	endPosInRoot = rootVisual.sceneToLocal(e.getSceneX(), e.getSceneY());
	Point2D start = rootVisual.localToScene(startPosInRoot);
	Point2D end = rootVisual.localToScene(endPosInRoot);
	double[] bbox = bbox(start, end);

	// find nodes contained in bbox
	List<Node> nodes = findContainedNodes(rootVisual.getScene().getRoot(),
			bbox[0], bbox[1], bbox[2], bbox[3]);

	// find content parts for contained nodes
	List<IContentPart<? extends Node>> parts = getParts(nodes);

	// filter out all parts that are not selectable
	Iterator<IContentPart<? extends Node>> it = parts.iterator();
	while (it.hasNext()) {
		if (!it.next().isSelectable()) {
			it.remove();
		}
	}

	// select the selectable parts contained within the marquee area
	try {
		root.getViewer().getDomain().execute(
				new SelectOperation(root.getViewer(), parts),
				new NullProgressMonitor());
	} catch (ExecutionException e1) {
		throw new IllegalStateException(e1);
	}
	removeFeedback();
}
 
Example 13
Source File: MarqueeOnDragHandler.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns a {@link List} of all {@link Node}s that are descendants of the
 * given root {@link Node} and fully contained within the bounds specified
 * by <code>[x0, y0, x1, y1]</code>.
 *
 * @param root
 *            The root {@link Node}.
 * @param x0
 *            The minimum x-coordinate.
 * @param y0
 *            The minimum y-coordinate.
 * @param x1
 *            The maximum x-coordinate.
 * @param y1
 *            The maximum y-coordinate.
 * @return A {@link List} containing all {@link Node}s that are descendants
 *         of the given root {@link Node} and fully contained within the
 *         specified bounds.
 */
// TODO: move to utility
public static List<Node> findContainedNodes(Node root, double x0, double y0,
		double x1, double y1) {
	Bounds bounds;
	double bx1, bx0, by1, by0;

	List<Node> containedNodes = new ArrayList<>();
	Queue<Node> nodes = new LinkedList<>();
	nodes.add(root);

	while (!nodes.isEmpty()) {
		Node current = nodes.remove();

		bounds = current.getBoundsInLocal();
		bounds = current.localToScene(bounds);
		bx1 = bounds.getMaxX();
		bx0 = bounds.getMinX();
		by1 = bounds.getMaxY();
		by0 = bounds.getMinY();

		if (bx1 < x0 || bx0 > x1 || by1 < y0 || by0 > y1) {
			// current node is outside of marquee bounds => dont collect
		} else {
			if (bx0 >= x0 && bx1 <= x1 && by0 >= y0 && by1 <= y1) {
				// current node is fully contained within marquee bounds
				containedNodes.add(current);
			}
			if (current instanceof Parent) {
				// add all children to nodes
				Parent p = (Parent) current;
				nodes.addAll(p.getChildrenUnmodifiable());
			}
		}
	}

	return containedNodes;
}
 
Example 14
Source File: WSRecorder.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public void recordRawMouseEvent(final RFXComponent r, MouseEvent e) {
    final JSONObject event = new JSONObject();
    event.put("type", "click_raw");
    int button = e.getButton() == MouseButton.PRIMARY ? java.awt.event.MouseEvent.BUTTON1 : java.awt.event.MouseEvent.BUTTON3;
    event.put("button", button);
    event.put("clickCount", e.getClickCount());
    event.put("modifiersEx", buildModifiersText(e));
    Node source = (Node) e.getSource();
    Node target = r.getComponent();
    Point2D sts = source.localToScene(new Point2D(e.getX(), e.getY()));
    Point2D tts = target.sceneToLocal(sts);
    event.put("x", tts.getX());
    event.put("y", tts.getY());
    final JSONObject o = new JSONObject();
    o.put("event", event);
    fill(r, o);
    if (e.getClickCount() == 1) {
        clickTimer = new Timer();
        clickTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                sendRecordMessage(o);
            }
        }, timerinterval.intValue());
    } else if (e.getClickCount() == 2) {
        if (clickTimer != null) {
            clickTimer.cancel();
            clickTimer = null;
        }
        sendRecordMessage(o);
    }
}
 
Example 15
Source File: JavaFXElementPropertyAccessor.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public TreeTableCell<?, ?> getTreeTableCellAt(TreeTableView<?> treeTableView, Point2D point) {
    point = treeTableView.localToScene(point);
    Set<Node> lookupAll = getTreeTableCells(treeTableView);
    TreeTableCell<?, ?> selected = null;
    for (Node cellNode : lookupAll) {
        Bounds boundsInScene = cellNode.localToScene(cellNode.getBoundsInLocal(), true);
        if (boundsInScene.contains(point)) {
            selected = (TreeTableCell<?, ?>) cellNode;
            break;
        }
    }
    return selected;
}
 
Example 16
Source File: JavaFXElementPropertyAccessor.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private TableCell<?, ?> getTableCellAt(TableView<?> tableView, Point2D point) {
    point = tableView.localToScene(point);
    Set<Node> lookupAll = getTableCells(tableView);
    TableCell<?, ?> selected = null;
    for (Node cellNode : lookupAll) {
        Bounds boundsInScene = cellNode.localToScene(cellNode.getBoundsInLocal(), true);
        if (boundsInScene.contains(point)) {
            selected = (TableCell<?, ?>) cellNode;
            break;
        }
    }
    return selected;
}
 
Example 17
Source File: CodeEditor.java    From ARMStrong with Mozilla Public License 2.0 5 votes vote down vote up
private List<Node> getVisibleElements(ScrollPane pane) {
	List<Node> visibleNodes = new ArrayList<Node>();
	Bounds paneBounds = pane.localToScene(pane.getBoundsInParent());
	if (pane.getContent() instanceof Parent) {
		for (Node n : ((Parent) pane.getContent()).getChildrenUnmodifiable()) {
			Bounds nodeBounds = n.localToScene(n.getBoundsInLocal());
			if (paneBounds.intersects(nodeBounds)) {
				visibleNodes.add(n);
			}
		}
	}
	return visibleNodes;
}
 
Example 18
Source File: SnapToGrid.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Snaps the given position (in scene coordinates) to a grid position (in
 * scene coordinates). The grid positions are specified by the given
 * {@link GridModel} and the given cell size fractions.
 *
 * @param sceneX
 *            The x-coordinate of the current position (in scene
 *            coordinates).
 * @param sceneY
 *            The y-coordinate of the current position (in scene
 *            coordinates).
 * @param gridModel
 *            The {@link GridModel} that specifies the grid positions.
 * @param gridCellWidthFraction
 *            The cell width fraction that determines if the x-coordinate is
 *            snapped to full (1.0), halve (0.5), etc. grid positions.
 * @param gridCellHeightFraction
 *            The cell height fraction that determines if the y-coordinate
 *            is snapped to full (1.0), halve (0.5), etc. grid positions.
 * @param gridLocalVisual
 *            A visual within the coordinate system where grid positions are
 *            at <code>(n * grid-cell-width, m * grid-cell-height)</code>.
 * @return The resulting snapped position in scene coordinates.
 */
protected Point snapToGrid(final double sceneX, final double sceneY,
		GridModel gridModel, final double gridCellWidthFraction,
		final double gridCellHeightFraction, Node gridLocalVisual) {
	// transform to grid local coordinates
	Point2D gridLocalPosition = gridLocalVisual.sceneToLocal(sceneX,
			sceneY);
	// snap to (nearest) grid point (add 0.5 so that the nearest grid
	// position is computed)
	double gcw = gridCellWidthFraction * gridModel.getGridCellWidth();
	double nearest = gridLocalPosition.getX() > 0 ? 0.5 : -0.5;
	int xs = (int) (gridLocalPosition.getX() / gcw + nearest);
	double gch = gridCellHeightFraction * gridModel.getGridCellHeight();
	nearest = gridLocalPosition.getY() > 0 ? 0.5 : -0.5;
	int ys = (int) (gridLocalPosition.getY() / gch + nearest);
	double nx = xs * gcw;
	double ny = ys * gch;
	// transform to scene coordinates
	Point2D newPositionInScene = gridLocalVisual.localToScene(nx, ny);
	return new Point(newPositionInScene.getX(), newPositionInScene.getY());
}
 
Example 19
Source File: Tile3D.java    From scenic-view with GNU General Public License v3.0 4 votes vote down vote up
private Bounds localetoRoot(SVNode sv) {
    Node n = sv.getImpl();
    Bounds node = n.localToScene(n.getLayoutBounds());
    Bounds root = currentRoot2D.getImpl().localToScene(currentRoot2D.getImpl().getLayoutBounds());
    return new BoundingBox(node.getMinX() - root.getMinX(), node.getMinY() - root.getMinY(), node.getWidth(), node.getHeight());
}