下面列出了javafx.scene.Node#localToScreen ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static EventHandler<KeyEvent> keyPressedHandler(
final PainteraGateway gateway,
final Node target,
Consumer<Exception> exceptionHandler,
Predicate<KeyEvent> check,
final String menuText,
final PainteraBaseView viewer,
final Supplier<String> projectDirectory,
final DoubleSupplier x,
final DoubleSupplier y)
{
return event -> {
if (check.test(event))
{
event.consume();
OpenDialogMenu m = gateway.openDialogMenu();
Optional<ContextMenu> cm = m.getContextMenu(menuText, viewer, projectDirectory, exceptionHandler);
Bounds bounds = target.localToScreen(target.getBoundsInLocal());
cm.ifPresent(menu -> menu.show(target, x.getAsDouble() + bounds.getMinX(), y.getAsDouble() + bounds.getMinY()));
}
};
}
public void show(Node anchorNode) {
PopupSelectboxContainer<T> container = new PopupSelectboxContainer<>();
container.setItems(this.items.get());
container.setCellFactory(this.cellFactory.get());
container.setSelectedItem(this.selectedItem);
container.getStylesheets().addAll(this.styleSheets);
container.headerContents().addAll(this.headerContents);
container.init();
Popup stage = new Popup();
stage.getContent().addAll(container);
stage.setAutoHide(true);
stage.setAnchorLocation(PopupWindow.AnchorLocation.CONTENT_TOP_LEFT);
Bounds screen = anchorNode.localToScreen(anchorNode.getLayoutBounds());
stage.show(anchorNode.getScene().getWindow(), screen.getMinX(), screen.getMaxY());
}
/**
* Show a transient popup with a message, to let the user know an action
* was performed.
*
* @param owner Node next to which the popup will be shown
* @return
*/
public static EventStream<?> showActionFeedback(@NonNull Node owner,
@Nullable Node graphic,
@NonNull String message,
double offsetX,
boolean stick,
String... cssClasses) {
Popup popup = new Popup();
Label label = new Label(message, graphic);
StackPane pane = new StackPane();
DesignerUtil.addCustomStyleSheets(pane, "designer");
pane.getStyleClass().addAll("action-feedback");
pane.getStyleClass().addAll(cssClasses);
pane.getChildren().addAll(label);
popup.getContent().addAll(pane);
Animation fadeTransition = stick ? fadeInAnimation(pane) : bounceFadeAnimation(pane);
EventSource<?> closeTick = new EventSource<>();
if (stick) {
pane.setOnMouseClicked(evt -> {
popup.hide();
closeTick.push(null);
});
} else {
fadeTransition.setOnFinished(e -> {
popup.hide();
closeTick.push(null);
});
}
popup.setOnShowing(e -> fadeTransition.play());
Bounds screenBounds = owner.localToScreen(owner.getBoundsInLocal());
popup.show(owner, screenBounds.getMaxX() + offsetX, screenBounds.getMinY());
return closeTick;
}
public void locateImage(Node region, boolean right) {
if (!imageCheck.isSelected()) {
imagePop.hide();
return;
}
Bounds bounds = region.localToScreen(region.getBoundsInLocal());
double x = right ? (bounds.getMaxX() + 200) : (bounds.getMinX() - 550);
imagePop.show(region, x, bounds.getMinY() - 50);
FxmlControl.refreshStyle(imagePop.getOwnerNode().getParent());
}
private void dispatchMouseEvent(Node node, Node target, PickResult pickResult, boolean popupTrigger, int clickCount,
MouseButton buttons, double x, double y) {
ensureVisible(node);
Point2D screenXY = node.localToScreen(new Point2D(x, y));
if (node != deviceState.getNode()) {
if (deviceState.getNode() != null) {
dispatchEvent(createMouseEvent(MouseEvent.MOUSE_EXITED, target, pickResult, x, y, screenXY.getX(), screenXY.getY(),
buttons, clickCount, deviceState.shiftPressed, deviceState.ctrlPressed, deviceState.altPressed,
deviceState.metaPressed, false, false, false, false, popupTrigger, false, node));
}
dispatchEvent(createMouseEvent(MouseEvent.MOUSE_ENTERED, target, pickResult, x, y, screenXY.getX(), screenXY.getY(),
buttons, clickCount, deviceState.shiftPressed, deviceState.ctrlPressed, deviceState.altPressed,
deviceState.metaPressed, false, false, false, false, popupTrigger, false, node));
}
for (int n = 1; n <= clickCount; n++) {
dispatchEvent(createMouseEvent(MouseEvent.MOUSE_PRESSED, target, pickResult, x, y, screenXY.getX(), screenXY.getY(),
buttons, n, deviceState.shiftPressed, deviceState.ctrlPressed, deviceState.altPressed, deviceState.metaPressed,
buttons == MouseButton.PRIMARY, buttons == MouseButton.MIDDLE, buttons == MouseButton.SECONDARY, false,
popupTrigger, false, node));
dispatchEvent(createMouseEvent(MouseEvent.MOUSE_RELEASED, target, pickResult, x, y, screenXY.getX(), screenXY.getY(),
buttons, n, deviceState.shiftPressed, deviceState.ctrlPressed, deviceState.altPressed, deviceState.metaPressed,
buttons == MouseButton.PRIMARY, buttons == MouseButton.MIDDLE, buttons == MouseButton.SECONDARY, false,
popupTrigger, false, node));
dispatchEvent(createMouseEvent(MouseEvent.MOUSE_CLICKED, target, pickResult, x, y, screenXY.getX(), screenXY.getY(),
buttons, n, deviceState.shiftPressed, deviceState.ctrlPressed, deviceState.altPressed, deviceState.metaPressed,
buttons == MouseButton.PRIMARY, buttons == MouseButton.MIDDLE, buttons == MouseButton.SECONDARY, false,
popupTrigger, false, node));
}
}
@Override
public void recordClick2(final RFXComponent r, MouseEvent e, boolean withCellInfo) {
final JSONObject event = new JSONObject();
event.put("type", "click");
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));
double x = e.getX();
double y = e.getY();
Node source = (Node) e.getSource();
Node target = r.getComponent();
Point2D sts = source.localToScreen(new Point2D(0, 0));
Point2D tts = target.localToScreen(new Point2D(0, 0));
x = e.getX() - tts.getX() + sts.getX();
y = e.getY() - tts.getY() + sts.getY();
event.put("x", x);
event.put("y", y);
if (withCellInfo) {
event.put("cellinfo", r.getCellInfo());
}
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);
}
}
public MouseEvent getContextMenuMouseEvent(Node source) {
Bounds boundsInParent = source.getBoundsInParent();
double x = boundsInParent.getWidth() / 2;
double y = boundsInParent.getHeight() / 2;
Point2D screenXY = source.localToScreen(x, y);
MouseEvent e = new MouseEvent(source, source, MouseEvent.MOUSE_PRESSED, x, y, screenXY.getX(), screenXY.getY(),
MouseButton.SECONDARY, 1, false, true, false, false, false, false, true, true, true, false, null);
return e;
}
/** Create list picker dialog
* @param parent Parent next to which the dialog will be positioned
* @param choices Options to show
* @param initial Initial selection (null will select first item)
*/
public ListPickerDialog(final Node parent, final Collection<String> choices, final String initial)
{
this(choices, initial);
initOwner(parent.getScene().getWindow());
final Bounds bounds = parent.localToScreen(parent.getBoundsInLocal());
setX(bounds.getMinX());
setY(bounds.getMinY());
}
/** returns true if (x,y) point in eventSource coordinates is contained by eventTarget node */
public static boolean contains(Node eventSource, Node eventTarget, double x, double y)
{
Point2D p = eventSource.localToScreen(x, y);
if(p != null)
{
p = eventTarget.screenToLocal(p);
if(p != null)
{
return eventTarget.contains(p);
}
}
return false;
}
/**
* returns margin between the node and its containing window.
* WARNING: does not check if window is indeed a right one.
*/
public static Insets getInsetsInWindow(Window w, Node n)
{
Bounds b = n.localToScreen(n.getBoundsInLocal());
double left = b.getMinX() - w.getX();
double top = b.getMinY() - w.getY();
double right = w.getX() + w.getWidth() - b.getMaxX();
double bottom = w.getY() + w.getHeight() - b.getMaxY();
return new Insets(top, right, bottom, left);
}
public void addRect(Node ref, double x, double y, double w, double h)
{
BoundingBox screenr = new BoundingBox(x, y, w, h);
Bounds b = ref.localToScreen(screenr);
b = target.screenToLocal(b);
Region r = new Region();
r.relocate(b.getMinX(), b.getMinY());
r.resize(b.getWidth(), b.getHeight());
r.setBackground(FX.background(Color.color(0, 0, 0, 0.3)));
add(r);
}
public void addOutline(Node ref, double x, double y, double w, double h)
{
BoundingBox screenr = new BoundingBox(x, y, w, h);
Bounds b = ref.localToScreen(screenr);
b = target.screenToLocal(b);
Region r = new Region();
r.relocate(b.getMinX(), b.getMinY());
r.resize(b.getWidth(), b.getHeight());
r.setBackground(FX.background(Color.color(0, 0, 0, 0.1)));
add(r);
}
/**
* マウスがこのアンカーノードに入るときに呼び出される関数を定義します。
*
* @param event {@link MouseEvent}
*/
protected void setOnMouseEntered(MouseEvent event) {
Node anchorNode = (Node) event.getSource();
Popup popup = this.initPopup(anchorNode);
Bounds screen = anchorNode.localToScreen(anchorNode.getLayoutBounds());
popup.setAnchorLocation(PopupWindow.AnchorLocation.CONTENT_TOP_LEFT);
popup.show(anchorNode.getScene().getWindow(), screen.getMinX(), screen.getMaxY());
this.setLocation(popup, anchorNode, event);
}
/**
* Shows the pop over in a position relative to the edges of the given owner
* node. The position is dependent on the arrow location. If the arrow is
* pointing to the right then the pop over will be placed to the left of the
* given owner. If the arrow points up then the pop over will be placed
* below the given owner node.
*
* @param owner
* the owner of the pop over
* @param offset
* if negative specifies the distance to the owner node or when
* positive specifies the number of pixels that the arrow will
* overlap with the owner node (positive values are recommended)
*/
public final void show(Node owner, double offset) {
requireNonNull(owner);
Bounds bounds = owner.localToScreen(owner.getBoundsInLocal());
switch (getArrowLocation()) {
case BOTTOM_CENTER:
case BOTTOM_LEFT:
case BOTTOM_RIGHT:
show(owner, bounds.getMinX() + bounds.getWidth() / 2,
bounds.getMinY() + offset);
break;
case LEFT_BOTTOM:
case LEFT_CENTER:
case LEFT_TOP:
show(owner, bounds.getMaxX() - offset,
bounds.getMinY() + bounds.getHeight() / 2);
break;
case RIGHT_BOTTOM:
case RIGHT_CENTER:
case RIGHT_TOP:
show(owner, bounds.getMinX() + offset,
bounds.getMinY() + bounds.getHeight() / 2);
break;
case TOP_CENTER:
case TOP_LEFT:
case TOP_RIGHT:
show(owner, bounds.getMinX() + bounds.getWidth() / 2,
bounds.getMinY() + bounds.getHeight() - offset);
break;
default:
break;
}
}
private static boolean checkViewCollision(Node n, Point2D gaze, Rectangle roi)
{
javafx.geometry.Point2D screenCoord = n.localToScreen(0d, 0d);
roi.setX(Math.round(screenCoord.getX()));
roi.setY(Math.round(screenCoord.getY()));
roi.setWidth(n.getBoundsInParent().getWidth());
roi.setHeight(n.getBoundsInParent().getHeight());
return roi.contains(
(int)Math.round(gaze.x),
(int)Math.round(gaze.y)
);
}
public static void locateBelow(Node node, PopupWindow window) {
Bounds bounds = node.localToScreen(node.getBoundsInLocal());
window.show(node, bounds.getMinX() + 2, bounds.getMinY() + bounds.getHeight() + 5);
}