Ключевые события с вложенными панелями привязки в сцене JavaFX - PullRequest
0 голосов
/ 08 апреля 2020

У меня есть класс GameSceneController, у которого есть поле AnchorPane с намерением назначить его различным «игровым комнатам». У меня есть GamePaneController, который является контроллером для F XML.

1002 * Я пытаюсь сделать заказ «спрайт» класс перемещение внутри вложенной anchorpane с ключевой стрелкой пресс. Я могу добиться этого с помощью кнопки, но каждый раз, когда я нажимаю клавиши со стрелками, он выделяет кнопки в сцене root, а не перемещает спрайт в пределах панели.

Спасибо!

Здесь мой код:

GameSceneController для обработки «внешнего» AnchorPane.

public class GameSceneController implements Initializable, SceneController {

    private GameController gc;
    private Sprite charSprite;
    private Scene menuScene;
    private Scene gameScene;
    private String[] panePaths = {"fxml/Room1.fxml", "fxml/Room2.fxml", "fxml/Room3.fxml"};
    private GamePaneController gPaneController;

    // testing
    int gCtr = 0;
    @FXML
    Button gCtrPress;
    @FXML
    Button nextRoomButton;
    @FXML
    AnchorPane gamePane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        gamePane.getChildren().add(new Region()); // this group
    }


    public void setMenuScene(Scene scene) {
        this.menuScene = scene;
    }
    public void setGameScene(Scene scene){
        this.gameScene = scene;
    }


    //   TESTING
    @FXML
    public void gCtrInc() {
        //gc.run(); //  TESTING
        if (gCtr < 2){
            gCtr++;
        } else {
            gCtr = 0;
        }
        System.out.println(gCtr);
    }
    //  TESTING

    @FXML
    public void openMenuScene(ActionEvent actionEvent) {
        Stage primaryStage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
        primaryStage.setScene(menuScene);
    }

    @Override
    public void setGameController(GameController gc) {
        this.gc = gc;
        setSprite();
    }
    public void setSprite(){
        this.charSprite = this.gc.getSprite();
    }

    @FXML
    public void setGamePane(ActionEvent actionEvent) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource(panePaths[this.gCtr]));
        this.gamePane.getChildren().set(0, loader.load());
        gPaneController = loader.getController();
        gPaneController.setGameController(this.gc);
        gPaneController.setGameScene(this.gameScene);
        gCtrInc();
    }

    @FXML
    public void moveBtn(ActionEvent actionEvent){
        System.out.println("DEBUG: " +   ((AnchorPane)this.gamePane.getChildren().get(0)).getChildren());
        System.out.println(gPaneController.getSprite());
        gPaneController.getSprite().moveColumn(1);
    }

}

GamePaneController для обработки «вложенного» AnchorPane.

public class GamePaneController implements Initializable {

    private GameController gc;
    private Scene gameScene;
    private Sprite charSprite;

    @FXML
    AnchorPane gamePane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("DEBUG - Game Controller");
    }

    public void setGameScene(Scene scene){
        this.gameScene = scene;
        setUserKeyInput();
    }
    public void setUserKeyInput(){
        this.gamePane.setOnKeyPressed(e -> {
            e.consume();
            if (e.getCode() == KeyCode.UP) {
                this.charSprite.moveRow(-1);
            } else if (e.getCode() == KeyCode.DOWN) {
                this.charSprite.moveRow(1);
            } else if (e.getCode() == KeyCode.LEFT) {
                this.charSprite.moveColumn(-1);
            } else if (e.getCode() == KeyCode.RIGHT){
                this.charSprite.moveColumn(1);
            } else {};
        });
    }

    public void setGameController(GameController gc) {
        this.gc = gc;
        setSprite();
    }
    public void setSprite(){
        this.charSprite = this.gc.getSprite();
        this.gamePane.getChildren().add(this.charSprite);
    }

    public Sprite getSprite(){
        return this.charSprite;
    }
}

GameScene F XML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<AnchorPane
prefWidth="1080"            
prefHeight="720"
xmlns:fx="https://google.com/"
fx:controller="io.javasmithy.controller.scene.GameSceneController">
    <children>
        <AnchorPane fx:id = "gamePane" AnchorPane.leftAnchor="100" AnchorPane.topAnchor="100" prefHeight="600" prefWidth="800"/>
        <Label text="Game SCENE" fx:id="gameSceneTitle" AnchorPane.leftAnchor="310" AnchorPane.topAnchor="10"/>
        <Button fx:id="gCtrPress" text="inc Ctr" onAction="#moveBtn" AnchorPane.leftAnchor="800" AnchorPane.topAnchor="800"/>
        <Button fx:id="menuButton" text="mainMenu" onAction="#openMenuScene" AnchorPane.leftAnchor="10" AnchorPane.topAnchor="10"/>
        <Button fx:id="nextRoomButton" text="Next Room" onAction="#setGamePane" AnchorPane.leftAnchor="900" AnchorPane.topAnchor="600"/>
    </children>

</AnchorPane>

F XML для «вложенного» AnchorPane

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:id="gamePane"
            fx:controller="io.javasmithy.controller.scene.GamePaneController"
            prefHeight="600.0" prefWidth="800.0"
            stylesheets="@/style/GamePaneStyle.css"
            styleClass="background">

    <children>
        <Label text="Room 1" AnchorPane.topAnchor="10" AnchorPane.leftAnchor="25"/>
    </children>

</AnchorPane>

Sprite Class, если вы хотите его увидеть.

public class Sprite extends Rectangle{
    private static final Random random = new Random();
    int row;
    int column;

    PointGrid grid;

    public Sprite(PointGrid grid){
        super();
        this.grid = grid;
        this.row = 0;
        this.column = 0;
        setWidth(32.0);
        setHeight(32.0);
        setPos();
    }

    public void moveRow(int delta){
        if (row + delta < 0 || row + delta > this.grid.getHeight()-1) {
            return;
        } else {row+=delta;}
        setPos();
    }
    public void moveColumn(int delta){
        if (column + delta < 0 || column + delta > this.grid.getWidth()-1) {
            return;
        } else {column+=delta;}
        setPos();
    }
    private void setPos(){
        this.setX(grid.getPoint2D(row, column).getX());
        this.setY(grid.getPoint2D(row, column).getY());
    }

    public void moveRandomly(){
        int axis = random.nextInt(2);
        int direction = random.nextInt(2);
        int delta = 1;
        if (direction == 0) delta = -delta;
        if (axis ==0 ) {moveRow(delta);} else {moveColumn(delta);}
    }
}
...