Search in sources :

Example 1 with NineSliceTextureBuilder

use of com.almasb.fxgl.texture.NineSliceTextureBuilder in project FXGL by AlmasB.

the class UINineSliceSample method initUI.

@Override
protected void initUI() {
    boolean fromSingleImage = true;
    if (fromSingleImage) {
        builder = new NineSliceTextureBuilder(texture("9SliceSprite.jpg")).topLeft(new Rectangle2D(0, 0, 53, 53)).top(new Rectangle2D(53, 0, 420, 53)).topRight(new Rectangle2D(473, 0, 52, 53)).left(new Rectangle2D(0, 52, 53, 420)).center(new Rectangle2D(53, 52, 420, 420)).right(new Rectangle2D(473, 52, 52, 420)).botLeft(new Rectangle2D(0, 473, 53, 52)).botRight(new Rectangle2D(473, 473, 52, 52)).bot(new Rectangle2D(53, 473, 473 - 53, 52));
    } else {
        builder = new NineSliceTextureBuilder(texture("ui_sheet.png")).topLeft(100, 100, 35, 35).topRight(165, 100, 35, 35).botLeft(100, 165, 35, 35).botRight(165, 165, 35, 35).center(135, 135, 30, 30).top(135, 100, 30, 35).bot(135, 165, 30, 35).left(100, 135, 35, 30).right(165, 135, 35, 30);
    }
    TextField fieldW = new TextField();
    fieldW.setText("300");
    fieldW.setPrefWidth(100);
    TextField fieldH = new TextField();
    fieldH.setText("300");
    fieldH.setPrefWidth(100);
    Button generateBtn = new Button("Generate");
    generateBtn.setOnAction(e -> {
        if (texture != null) {
            getGameScene().removeUINode(texture);
            texture.dispose();
        }
        try {
            double w = Double.parseDouble(fieldW.getText());
            double h = Double.parseDouble(fieldH.getText());
            texture = builder.build((int) w, (int) h);
            texture.setTranslateX(100);
            getGameScene().addUINode(texture);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    });
    Button exportBtn = new Button("Export");
    exportBtn.setOnAction(e -> {
        if (texture != null) {
            var fileChooser = createImageSaverDialog();
            var file = fileChooser.showSaveDialog(getGameScene().getRoot().getScene().getWindow());
            if (file != null) {
                saveImageToFile(texture.getImage(), file);
            }
        }
    });
    getGameScene().addUINodes(new VBox(5, fieldW, fieldH, generateBtn, exportBtn));
    generateBtn.fire();
}
Also used : NineSliceTextureBuilder(com.almasb.fxgl.texture.NineSliceTextureBuilder) Button(javafx.scene.control.Button) Rectangle2D(javafx.geometry.Rectangle2D) TextField(javafx.scene.control.TextField) VBox(javafx.scene.layout.VBox) IOException(java.io.IOException)

Aggregations

NineSliceTextureBuilder (com.almasb.fxgl.texture.NineSliceTextureBuilder)1 IOException (java.io.IOException)1 Rectangle2D (javafx.geometry.Rectangle2D)1 Button (javafx.scene.control.Button)1 TextField (javafx.scene.control.TextField)1 VBox (javafx.scene.layout.VBox)1