Javafx鼠标事件y坐标偏移在子场景中不正确

Javafx鼠标事件y坐标偏移在子场景中不正确,java,javafx,javafx-11,openjfx,Java,Javafx,Javafx 11,Openjfx,我创建了一个星形打印应用程序来打印三维星形绘图仪的标签,以便将标签映射到场景的局部。我想这样做,这样每当我旋转星场时,标签总是朝前,不会随着单个恒星旋转(因为它们无法读取,所以很烦人) 在我在上面添加了一个控制面板之前,它工作得非常好。在下面的应用程序中,如果控制面板不存在,则使用星号位置标记轨迹。但对于控制面板,标签的y偏移量与控制面板高度的大小成正比 问题是“mousePosY=me.getSceneY();”返回场景本身的鼠标位置,而不是定义的子场景。我原以为“subScene.seton

我创建了一个星形打印应用程序来打印三维星形绘图仪的标签,以便将标签映射到场景的局部。我想这样做,这样每当我旋转星场时,标签总是朝前,不会随着单个恒星旋转(因为它们无法读取,所以很烦人)

在我在上面添加了一个控制面板之前,它工作得非常好。在下面的应用程序中,如果控制面板不存在,则使用星号位置标记轨迹。但对于控制面板,标签的y偏移量与控制面板高度的大小成正比

问题是“mousePosY=me.getSceneY();”返回场景本身的鼠标位置,而不是定义的子场景。我原以为“subScene.setonmousedrable((MouseEvent me)->{”会给我鼠标相对于subScene而不是场景的位置

有没有办法解决这个问题,使返回的X,Y位置仅用于次场景

下面的代码是我的应用程序的一个非常精简的版本,它显示了实际问题本身

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Random;

import static org.fxyz3d.geometry.MathUtils.clamp;

/**
 * example for flat labels
 */
@Slf4j
public class StarFieldExample extends Application {

    public static final int SCALE_X = 510;
    public static final int SCALE_Y = 540;
    public static final int SCALE_Z = 0;
    final double sceneWidth = 600;
    final double sceneHeight = 600;

    private double mousePosX;
    private double mousePosY;
    private double mouseOldX;
    private double mouseOldY;
    private double mouseDeltaX;
    private double mouseDeltaY;

    private final Font font = new Font("arial", 10);

    // We'll use custom Rotate transforms to manage the coordinate conversions
    private final Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
    private final Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
    private final Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);

    private final Group root = new Group();
    private final Group world = new Group();  //all 3D nodes in scene
    private final Group labelGroup = new Group(); //all generic 3D labels

    //All shapes and labels linked via hash for easy update during camera movement
    private final HashMap<Node, Label> shape3DToLabel = new HashMap<>();

    private SubScene subScene;

    //////  support
    private final Random random = new Random();

    private final static double RADIUS_MAX = 7;
    private final static double X_MAX = 300;
    private final static double Y_MAX = 300;
    private final static double Z_MAX = 300;

    private final Label scaleLabel = new Label("Scale: 5 ly");

    public Pane createStarField() {

        // attach our custom rotation transforms so we can update the labels dynamically
        world.getTransforms().addAll(rotateX, rotateY, rotateZ);

        subScene = new SubScene(world, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
        subScene.setFill(Color.BLACK);

        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setNearClip(0.1);
        camera.setFarClip(10000.0);
        camera.setTranslateZ(-1000);

        subScene.setCamera(camera);
        Group sceneRoot = new Group(subScene);
        sceneRoot.getChildren().add(labelGroup);

        generateRandomStars(5);

        handleMouseEvents();

        // add to the 2D portion of this component
        Pane pane = new Pane();
        pane.setPrefSize(sceneWidth, sceneHeight);
        pane.setMaxSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
        pane.setMinSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
        pane.setBackground(Background.EMPTY);
        pane.getChildren().add(sceneRoot);
        pane.setPickOnBounds(true);

        subScene.widthProperty().bind(pane.widthProperty());
        subScene.heightProperty().bind(pane.heightProperty());
        Platform.runLater(this::updateLabels);
        return (pane);
    }

    private void handleMouseEvents() {
        subScene.setOnMousePressed((MouseEvent me) -> {
                    mousePosX = me.getSceneX();
                    mousePosY = me.getSceneY();
                    mouseOldX = me.getSceneX();
                    mouseOldY = me.getSceneY();
                }
        );

        subScene.setOnMouseDragged((MouseEvent me) -> {
                    mouseOldX = mousePosX;
                    mouseOldY = mousePosY;
                    mousePosX = me.getSceneX();
                    mousePosY = me.getSceneY();
                    mouseDeltaX = (mousePosX - mouseOldX);
                    mouseDeltaY = (mousePosY - mouseOldY);
                    double modifier = 5.0;
                    double modifierFactor = 0.1;

                    if (me.isPrimaryButtonDown()) {
                        if (me.isAltDown()) { //roll
                            rotateZ.setAngle(((rotateZ.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
                        } else {
                            rotateY.setAngle(((rotateY.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
                            rotateX.setAngle(
                                    clamp(
                                            (((rotateX.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180),
                                            -60,
                                            60
                                    )
                            ); // -
                        }
                    }
                    updateLabels();
                }
        );
    }

    public void generateRandomStars(int numberStars) {
        for (int i = 0; i < numberStars; i++) {
            double radius = random.nextDouble() * RADIUS_MAX;
            Color color = randomColor();
            double x = random.nextDouble() * X_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
            double y = random.nextDouble() * Y_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
            double z = random.nextDouble() * Z_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);

            String labelText = "Star " + i;
            boolean fadeFlag = random.nextBoolean();
            createSphereLabel(radius, x, y, z, color, labelText, fadeFlag);
        }

        //Add to hashmap so updateLabels() can manage the label position

        scaleLabel.setFont(new Font("Arial", 15));
        scaleLabel.setTextFill(Color.WHEAT);
        scaleLabel.setTranslateX(SCALE_X);
        scaleLabel.setTranslateY(SCALE_Y);
        scaleLabel.setTranslateZ(SCALE_Z);
        labelGroup.getChildren().add(scaleLabel);
        log.info("shapes:{}", shape3DToLabel.size());
    }

    private Color randomColor() {
        int r = random.nextInt(255);
        int g = random.nextInt(255);
        int b = random.nextInt(255);
        return Color.rgb(r, g, b);
    }


    private void createSphereLabel(double radius, double x, double y, double z, Color color, String labelText, boolean fadeFlag) {
        Sphere sphere = new Sphere(radius);
        sphere.setTranslateX(x);
        sphere.setTranslateY(y);
        sphere.setTranslateZ(z);
        sphere.setMaterial(new PhongMaterial(color));
        //add our nodes to the group that will later be added to the 3D scene
        world.getChildren().add(sphere);

        Label label = new Label(labelText);
        label.setTextFill(color);
        label.setFont(font);

        labelGroup.getChildren().add(label);

        //Add to hashmap so updateLabels() can manage the label position
        shape3DToLabel.put(sphere, label);

    }

    private void updateLabels() {
        shape3DToLabel.forEach((node, label) -> {
            Point3D coordinates = node.localToScene(Point3D.ZERO, true);

            //Clipping Logic
            //if coordinates are outside of the scene it could
            //stretch the screen so don't transform them
            double x = coordinates.getX();
            double y = coordinates.getY();

            // is it left of the view?
            if (x < 0) {
                x = 0;
            }

            // is it right of the view?
            if ((x + label.getWidth() + 5) > subScene.getWidth()) {
                x = subScene.getWidth() - (label.getWidth() + 5);
            }

            // is it above the view?
            if (y < 0) {
                y = 0;
            }

            // is it below the view
            if ((y + label.getHeight()) > subScene.getHeight()) {
                y = subScene.getHeight() - (label.getHeight() + 5);
            }

            //update the local transform of the label.
            label.getTransforms().setAll(new Translate(x, y));
        });

        scaleLabel.setTranslateX(SCALE_X);
        scaleLabel.setTranslateY(SCALE_Y);
        scaleLabel.setTranslateZ(SCALE_Z);
    }

    //////////////////////////////////

    @Override
    public void start(Stage primaryStage) throws Exception {

        Pane controls = createControls();
        Pane pane = createStarField();
        VBox vBox = new VBox(
                controls,
                pane
        );

        root.getChildren().add(vBox);
        Scene scene = new Scene(root, sceneWidth, sceneHeight - 40);
        primaryStage.setTitle("2D Labels over 3D SubScene");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private VBox createControls() {
        VBox controls = new VBox(10, new Button("Button"));
        controls.setPadding(new Insets(10));
        return controls;
    }

    public static void main(String[] args) {
        launch(args);
    }

}
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.geometry.Insets;
导入javafx.geometry.Point3D;
导入javafx.scene.*;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.Background;
导入javafx.scene.layout.Pane;
导入javafx.scene.layout.VBox;
导入javafx.scene.paint.Color;
导入javafx.scene.paint.PhongMaterial;
导入javafx.scene.shape.Sphere;
导入javafx.scene.text.Font;
导入javafx.scene.transform.Rotate;
导入javafx.scene.transform.Translate;
导入javafx.stage.stage;
导入lombok.extern.slf4j.slf4j;
导入java.util.HashMap;
导入java.util.Random;
导入静态org.fxyz3d.geometry.MathUtils.clamp;
/**
*平面标签示例
*/
@Slf4j
公共类StarFieldExample扩展了应用程序{
公共静态最终整数刻度_X=510;
公共静态最终整数刻度_Y=540;
公共静态最终整数刻度_Z=0;
最终双场景宽度=600;
最终双场景高度=600;
私人双鼠标器;
私人双鼠标;
私人双鼠标;
私人双老鼠;
私人双滑鼠斧;
私人双滑鼠日;
专用最终字体字体=新字体(“arial”,10);
//我们将使用自定义旋转变换来管理坐标转换
私有最终旋转rotateX=新旋转(0,旋转X_轴);
私有最终旋转旋转Y=新旋转(0,旋转Y_轴);
私有最终旋转旋转Z=新旋转(0,旋转Z_轴);
私有最终组根=新组();
private final Group world=new Group();//场景中的所有3D节点
私有最终组labelGroup=new Group();//所有通用三维标签
//所有形状和标签通过散列链接,以便在相机移动过程中轻松更新
private final HashMap shape3DToLabel=new HashMap();
私有亚新世;
//////支持
私有最终随机=新随机();
专用最终静态双半径_MAX=7;
专用最终静态双X_MAX=300;
私人最终静态双Y_MAX=300;
专用最终静态双Z_MAX=300;
专用最终标签scaleLabel=新标签(“比例:5ly”);
公共窗格createStarField(){
//附加自定义旋转变换,以便动态更新标签
getTransforms().addAll(rotateX、rotateY、rotateZ);
亚新世=新亚新世(世界、场景宽度、场景高度、真实、场景化、平衡);
亚新世。刚毛填充(颜色。黑色);
透视摄影机=新透视摄影机(真);
摄像头。setNearClip(0.1);
摄像机。setFarClip(10000.0);
照相机.setTranslateZ(-1000);
亚场景设置摄像机(摄像机);
组sceneRoot=新组(亚新世);
sceneRoot.getChildren().add(labelGroup);
生成星(5);
handleMouseEvents();
//添加到此构件的二维部分
窗格=新窗格();
窗格.setPrefSize(场景宽度、场景高度);
pane.setMaxSize(pane.USE_COMPUTED_SIZE,pane.USE_COMPUTED_SIZE);
pane.setMinSize(pane.USE_COMPUTED_SIZE,pane.USE_COMPUTED_SIZE);
窗格.背景(背景.空);
pane.getChildren().add(sceneRoot);
pane.setPickOnBounds(true);
subScene.widthProperty().bind(pane.widthProperty());
subScene.heightProperty().bind(pane.heightProperty());
runLater(this::updateLabels);
返回(窗格);
}
私有无效handleMouseEvents(){
subScene.setOnMousePressed((MouseEvent me)->{
mousePosX=me.getSceneX();
mousePosY=me.getSceneY();
mouseOldX=me.getSceneX();
mouseOldY=me.getSceneY();
}
);
subScene.setonMouseDrawed((MouseEvent me)->{
mouseOldX=mousePosX;
mouseOldY=鼠标点;
mousePosX=me.getSceneX();
mousePosY=me.getSceneY();
mouseDeltaX=(mousePosX-mouseOldX);
mouseDeltaY=(mousePosY-mouseOldY);
双修饰符=5.0;
双修饰符系数=0.1;
if(me.isPrimaryButtonDown()){
如果(me.isAltDown()){//roll
rotateZ.setAngle(((rotateZ.getAngle()+mouseDeltaX*modifierFactor*modifier*2.0)%360+540)%360-180);//+
}否则{
rotateY.setAngle(((rotateY.getAngle()+mouseDeltaX*modifierFactor*modifier*2.0)%360+540)%360-180);//+
旋转设定角(
夹钳(
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import static org.fxyz3d.geometry.MathUtils.clamp;

/**
 * example for flat labels
 */
@Slf4j
public class StarFieldExample extends Application {

    public static final int SCALE_X = 510;
    public static final int SCALE_Y = 540;
    public static final int SCALE_Z = 0;
    final double sceneWidth = 600;
    final double sceneHeight = 600;

    private double mousePosX;
    private double mousePosY;
    private double mouseOldX;
    private double mouseOldY;
    private double mouseDeltaX;
    private double mouseDeltaY;

    private final Font font = new Font("arial", 10);

    // We'll use custom Rotate transforms to manage the coordinate conversions
    private final Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
    private final Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
    private final Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);

    private final Group root = new Group();
    private final Group world = new Group();  //all 3D nodes in scene
    private final Group labelGroup = new Group(); //all generic 3D labels

    //All shapes and labels linked via hash for easy update during camera movement
    private final Map<Node, Label> shape3DToLabel = new HashMap<>();

    private SubScene subScene;

    //////  support
    private final Random random = new Random();

    private final static double RADIUS_MAX = 7;
    private final static double X_MAX = 300;
    private final static double Y_MAX = 300;
    private final static double Z_MAX = 300;

    Pane pane;

    private final Label scaleLabel = new Label("Scale: 5 ly");

    public Pane createStarField() {

        PerspectiveCamera camera = setupPerspectiveCamera();

        // attach our custom rotation transforms so we can update the labels dynamically
        world.getTransforms().addAll(rotateX, rotateY, rotateZ);

        subScene = new SubScene(world, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
        subScene.setFill(Color.BLACK);
        subScene.setCamera(camera);

        Group sceneRoot = new Group(subScene);
        sceneRoot.getChildren().add(labelGroup);

        generateRandomStars(5);

        // add to the 2D portion of this component
        pane = new Pane();
        pane.setPrefSize(sceneWidth, sceneHeight);
        pane.setMaxSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
        pane.setMinSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
        pane.setBackground(Background.EMPTY);
        pane.getChildren().add(sceneRoot);
        pane.setPickOnBounds(true);

        subScene.widthProperty().bind(pane.widthProperty());
        subScene.heightProperty().bind(pane.heightProperty());
        Platform.runLater(this::updateLabels);

        handleMouseEvents();

        return (pane);
    }

    @NotNull
    private PerspectiveCamera setupPerspectiveCamera() {
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setNearClip(0.1);
        camera.setFarClip(10000.0);
        camera.setTranslateZ(-1000);
        return camera;
    }

    private void handleMouseEvents() {
        subScene.setOnMousePressed((MouseEvent me) -> {
                    mousePosX = me.getSceneX();
                    mousePosY = me.getSceneY();
                    mouseOldX = me.getSceneX();
                    mouseOldY = me.getSceneY();
                }
        );

        subScene.setOnMouseDragged((MouseEvent me) -> {
                    mouseOldX = mousePosX;
                    mouseOldY = mousePosY;
                    mousePosX = me.getSceneX();
                    mousePosY = me.getSceneY();
                    mouseDeltaX = (mousePosX - mouseOldX);
                    mouseDeltaY = (mousePosY - mouseOldY);
                    double modifier = 5.0;
                    double modifierFactor = 0.1;

                    if (me.isPrimaryButtonDown()) {
                        if (me.isAltDown()) { //roll
                            rotateZ.setAngle(((rotateZ.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
                        } else {
                            rotateY.setAngle(((rotateY.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
                            rotateX.setAngle(
                                    clamp(
                                            (((rotateX.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180),
                                            -60,
                                            60
                                    )
                            ); // -
                        }
                    }
                    updateLabels();
                }
        );
    }


    private void updateLabels() {
        shape3DToLabel.forEach((node, label) -> {
            Point3D coordinates = node.localToScene(Point3D.ZERO, true);

            //Clipping Logic
            //if coordinates are outside of the scene it could
            //stretch the screen so don't transform them
            double xs = coordinates.getX();
            double ys = coordinates.getY();

            Bounds ofParent = pane.getBoundsInParent();
            double x = xs - ofParent.getMinX();
            double y = ys - ofParent.getMinY();

            // is it left of the view?
            if (x < 0) {
                x = 0;
            }

            // is it right of the view?
            if ((x + label.getWidth() + 5) > subScene.getWidth()) {
                x = subScene.getWidth() - (label.getWidth() + 5);
            }

            // is it above the view?
            if (y < 0) {
                y = 0;
            }

            // is it below the view
            if ((y + label.getHeight()) > subScene.getHeight()) {
                y = subScene.getHeight() - (label.getHeight() + 5);
            }

            //update the local transform of the label.
            label.getTransforms().setAll(new Translate(x, y));
        });

        scaleLabel.setTranslateX(SCALE_X);
        scaleLabel.setTranslateY(SCALE_Y);
        scaleLabel.setTranslateZ(SCALE_Z);
    }

    public void generateRandomStars(int numberStars) {
        for (int i = 0; i < numberStars; i++) {
            double radius = random.nextDouble() * RADIUS_MAX;
            Color color = randomColor();
            double x = random.nextDouble() * X_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
            double y = random.nextDouble() * Y_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
            double z = random.nextDouble() * Z_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);

            String labelText = "Star " + i;
            boolean fadeFlag = random.nextBoolean();
            createSphereLabel(radius, x, y, z, color, labelText, fadeFlag);
        }

        //Add to hashmap so updateLabels() can manage the label position

        scaleLabel.setFont(new Font("Arial", 15));
        scaleLabel.setTextFill(Color.WHEAT);
        scaleLabel.setTranslateX(SCALE_X);
        scaleLabel.setTranslateY(SCALE_Y);
        scaleLabel.setTranslateZ(SCALE_Z);
        labelGroup.getChildren().add(scaleLabel);
        log.info("shapes:{}", shape3DToLabel.size());
    }

    private Color randomColor() {
        int r = random.nextInt(255);
        int g = random.nextInt(255);
        int b = random.nextInt(255);
        return Color.rgb(r, g, b);
    }


    private void createSphereLabel(double radius, double x, double y, double z, Color color, String labelText, boolean fadeFlag) {
        Sphere sphere = new Sphere(radius);
        sphere.setTranslateX(x);
        sphere.setTranslateY(y);
        sphere.setTranslateZ(z);
        sphere.setMaterial(new PhongMaterial(color));
        //add our nodes to the group that will later be added to the 3D scene
        world.getChildren().add(sphere);

        Label label = new Label(labelText);
        label.setTextFill(color);
        label.setFont(font);

        labelGroup.getChildren().add(label);

        //Add to hashmap so updateLabels() can manage the label position
        shape3DToLabel.put(sphere, label);

    }


    //////////////////////////////////

    @Override
    public void start(Stage primaryStage) throws Exception {

        Pane controls = createControls();
        Pane pane = createStarField();
        VBox vBox = new VBox(
                controls,
                pane
        );

        root.getChildren().add(vBox);
        Scene scene = new Scene(root, sceneWidth, sceneHeight - 40);
        primaryStage.setTitle("2D Labels over 3D SubScene");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private VBox createControls() {
        VBox controls = new VBox(10, new Button("Button"));
        controls.setPadding(new Insets(10));
        return controls;
    }

    public static void main(String[] args) {
        launch(args);
    }

}