Initial commit...
This commit is contained in:
225
Java Peojects/TileMaper/src/Controller.java
Normal file
225
Java Peojects/TileMaper/src/Controller.java
Normal file
@@ -0,0 +1,225 @@
|
||||
package com.itdominator.tilemaper;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.stage.DirectoryChooser;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.scene.canvas.Canvas;
|
||||
import javafx.scene.canvas.GraphicsContext;
|
||||
import javafx.scene.SnapshotParameters;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.WritableImage;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.geometry.Rectangle2D;
|
||||
|
||||
import java.lang.Integer;
|
||||
import java.lang.Exception;
|
||||
import java.lang.Double;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.awt.image.RenderedImage;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
|
||||
public class Controller {
|
||||
// Classes
|
||||
SaveController saveController = new SaveController();
|
||||
|
||||
// FXML Stuff
|
||||
@FXML private Canvas canvas;
|
||||
@FXML private VBox tileImagesVbox;
|
||||
@FXML private ComboBox<String> imgSizeComboBox;
|
||||
@FXML private TextField offsetTextField, columnCountTextField;
|
||||
|
||||
// Generics
|
||||
private DirectoryChooser folderChooser = new DirectoryChooser();
|
||||
private FileChooser fileChooser = new FileChooser();
|
||||
private SnapshotParameters params = new SnapshotParameters();
|
||||
private GraphicsContext graphicsContext;
|
||||
private File[] fileList;
|
||||
private File directory, file;
|
||||
private ImageView imgView;
|
||||
private Image img;
|
||||
private String path = "";
|
||||
private double offset = 5.0;
|
||||
private double x = 0;
|
||||
private double y = 0;
|
||||
private int columnCount = 2;
|
||||
|
||||
|
||||
@FXML void initialize() {
|
||||
assert imgSizeComboBox != null : "fx:id=\"imgSizeComboBox\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
assert offsetTextField != null : "fx:id=\"offsetTextField\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
assert columnCountTextField != null : "fx:id=\"columnCountTextField\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
assert tileImagesVbox != null : "fx:id=\"tileImagesVbox\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
assert canvas != null : "fx:id=\"canvas\" was not injected: check your FXML file 'TileMaper.fxml'.";
|
||||
|
||||
params.setFill(Color.TRANSPARENT);
|
||||
graphicsContext = canvas.getGraphicsContext2D();
|
||||
}
|
||||
|
||||
@FXML void importFromDir(ActionEvent event) {
|
||||
try {
|
||||
Stage stage = new Stage();
|
||||
directory = folderChooser.showDialog(stage);
|
||||
fileList = directory.listFiles();
|
||||
} catch (Exception e) { return; }
|
||||
|
||||
tileImagesVbox.getChildren().clear();
|
||||
|
||||
for (int i = 0; i < fileList.length; i++) {
|
||||
path = "file://" + fileList[i];
|
||||
if (path.toLowerCase().matches("^.*?(png|jpg|jpeg|gif).*$")) {
|
||||
img = new Image(path);
|
||||
imgView = new ImageView(img);
|
||||
imgView.setFitWidth(128);
|
||||
imgView.setFitHeight(128);
|
||||
tileImagesVbox.getChildren().add(imgView);
|
||||
}
|
||||
}
|
||||
|
||||
drawImgsToCanvas(fileList);
|
||||
}
|
||||
|
||||
@FXML void importFromFile(ActionEvent event) {
|
||||
try {
|
||||
Stage stage = new Stage();
|
||||
file = fileChooser.showOpenDialog(stage);
|
||||
} catch (Exception e) { return; }
|
||||
|
||||
tileImagesVbox.getChildren().clear();
|
||||
img = new Image("file://" + file);
|
||||
graphicsContext.clearRect(0.0, 0.0, img.getWidth(), img.getHeight()); // Clear rectangle
|
||||
setNewCanvasSize(img.getWidth(), img.getHeight());
|
||||
drawImage(0.0, 0.0, img.getWidth(), img.getHeight());
|
||||
}
|
||||
|
||||
@FXML void saveCanvas(ActionEvent event) {
|
||||
saveController.saveCanvas(canvas, params);
|
||||
}
|
||||
|
||||
@FXML void saveTilesToImages(ActionEvent event) {
|
||||
saveController.saveTilesToImages(canvas, params, tileImagesVbox);
|
||||
}
|
||||
|
||||
@FXML void splitIntoTiles(ActionEvent event) {
|
||||
drawToImgs();
|
||||
}
|
||||
|
||||
@FXML void updateTileSize(ActionEvent event) {
|
||||
if (fileList != null) {
|
||||
drawImgsToCanvas(fileList);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML void setOffset(KeyEvent event) {
|
||||
try {
|
||||
offset = Double.parseDouble(offsetTextField.getText());
|
||||
drawImgsToCanvas(fileList);
|
||||
} catch (Exception e) { return; }
|
||||
|
||||
System.out.println("Offset value set to: " + offset);
|
||||
}
|
||||
|
||||
@FXML void setColumnCount(KeyEvent event) {
|
||||
try {
|
||||
columnCount = Integer.parseInt(columnCountTextField.getText());
|
||||
drawImgsToCanvas(fileList);
|
||||
} catch (Exception e) { return; }
|
||||
|
||||
System.out.println("Column count set to: " + columnCount);
|
||||
}
|
||||
|
||||
private void drawImgsToCanvas(File[] tileFiles) {
|
||||
// Initial value is offset to get proper placement.
|
||||
x = offset;
|
||||
y = offset;
|
||||
int tileSize = Integer.parseInt(imgSizeComboBox.getValue().trim());
|
||||
int fileCount = tileFiles.length;
|
||||
int counter = 0;
|
||||
double width = (tileSize * columnCount) +
|
||||
(offset * (columnCount + 1));
|
||||
double height = ((fileCount / columnCount ) * tileSize) +
|
||||
(offset * ((fileCount / columnCount ) + 1));
|
||||
|
||||
// Make sure there are enough rows for the tiles
|
||||
if (fileCount % columnCount > 0) {
|
||||
height += tileSize;
|
||||
}
|
||||
|
||||
System.out.println("Canvas Width: " + width +
|
||||
"\nCanvas Height: " + height);
|
||||
|
||||
setNewCanvasSize(width, height);
|
||||
graphicsContext.clearRect(0.0, 0.0, width, height); // Clear rectangle
|
||||
|
||||
for (int i = 0; i < fileCount; i++) {
|
||||
path = "file://" + tileFiles[i];
|
||||
|
||||
if (path.toLowerCase().matches("^.*?(png|jpg|jpeg|gif).*$")) {
|
||||
img = new Image(path);
|
||||
drawImage(x, y, tileSize, tileSize);
|
||||
|
||||
x = x + offset + tileSize;
|
||||
counter++;
|
||||
|
||||
if (counter == columnCount) {
|
||||
counter = 0;
|
||||
x = offset;
|
||||
y = y + offset + tileSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawToImgs() {
|
||||
// Initial value is offset to get proper placement.
|
||||
x = offset;
|
||||
y = offset;
|
||||
int counter = 0;
|
||||
int yTileCount = -1; // Set to neg one b/c of offseting from count
|
||||
int tileSize = Integer.parseInt(imgSizeComboBox.getValue().trim());
|
||||
double width = canvas.getWidth();
|
||||
double height = canvas.getHeight();
|
||||
|
||||
tileImagesVbox.getChildren().clear();
|
||||
|
||||
// We can use col count and by way of itteration based on offset and size,
|
||||
// add to a tile count by traversing "rows" through the column size.
|
||||
while (y <= height) {
|
||||
WritableImage writableImage = new WritableImage(tileSize, tileSize);
|
||||
params.setViewport(new Rectangle2D(x, y, (double) tileSize, (double) tileSize));
|
||||
img = canvas.snapshot(params, writableImage);
|
||||
imgView = new ImageView(img);
|
||||
imgView.setFitWidth(128);
|
||||
imgView.setFitHeight(128);
|
||||
tileImagesVbox.getChildren().add(imgView);
|
||||
|
||||
x = x + offset + tileSize;
|
||||
counter++;
|
||||
|
||||
if (counter == columnCount) {
|
||||
counter = 0;
|
||||
x = offset;
|
||||
y = y + offset + tileSize;
|
||||
yTileCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawImage(double x, double y, double w, double h) {
|
||||
graphicsContext.drawImage(img, x, y, w, h); // x,y,w,h
|
||||
}
|
||||
|
||||
private void setNewCanvasSize(double w, double h) {
|
||||
canvas.setWidth(w);
|
||||
canvas.setHeight(h);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user