From 55739cdb98e178b18aa74e54bcd595113a0081f6 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sat, 16 Apr 2016 13:41:52 +0100 Subject: [PATCH] added image support for designer --- .../manual/designer/ManualDesigner.java | 29 +++++++++++++++++++ .../manual/designer/fileUtils/LoadSystem.java | 29 +++++++++++++------ .../manual/designer/fileUtils/SaveSystem.java | 6 +++- .../windows/MainWindowController.java | 29 +++++++++++++++++++ .../techreborn/designer/mainWindow.fxml | 7 +++-- 5 files changed, 88 insertions(+), 12 deletions(-) diff --git a/src/main/java/techreborn/manual/designer/ManualDesigner.java b/src/main/java/techreborn/manual/designer/ManualDesigner.java index dd3105e2a..393f18850 100644 --- a/src/main/java/techreborn/manual/designer/ManualDesigner.java +++ b/src/main/java/techreborn/manual/designer/ManualDesigner.java @@ -15,6 +15,7 @@ import techreborn.manual.designer.fileUtils.SaveSystem; import techreborn.manual.designer.windows.MainWindowController; import techreborn.manual.saveFormat.Entry; +import java.io.File; import java.net.URL; /** @@ -73,10 +74,30 @@ public class ManualDesigner extends Application { if(entry.data.data.containsKey("text")){ controller.textInput.setText(entry.data.data.get("text")); } + if(entry.data.data.containsKey("image")){ + controller.imageTextArea.setText(entry.data.data.get("image")); + if(SaveSystem.lastSave != null){ + File imagesDir = new File(SaveSystem.lastSave, "images"); + if(imagesDir.exists()){ + File image = new File(imagesDir, entry.data.data.get("image") + ".png"); + if(image.exists()){ + controller.pageimage.setImage(new Image(image.toURI().toString())); + } + } + } + } else { + controller.imageTextArea.setText(""); + } //TODO Improve this } else { controller.textInput.setText(""); } + if(entry.type.equalsIgnoreCase("image")){ + controller.imageTextArea.setDisable(false); + } else { + controller.imageTextArea.setDisable(true); + } + controller.infoLabel.setText("Settings: " + entry.type); controller.nameTextArea.setText(entry.registryName); } } @@ -96,6 +117,14 @@ public class ManualDesigner extends Application { controller.image.fitWidthProperty().bind(controller.renderPane.widthProperty()); controller.image.fitHeightProperty().bind(controller.renderPane.heightProperty()); + controller.pageimage.setPreserveRatio(true); + controller.pageimage.setSmooth(true); + controller.pageimage.setCache(true); + controller.pageimage.setFitHeight(1000); + controller.pageimage.setFitWidth(1000); + controller.pageimage.fitWidthProperty().bind(controller.renderPane.widthProperty()); + controller.pageimage.fitHeightProperty().bind(controller.renderPane.heightProperty()); + controller.load(); Alert alert = new Alert(Alert.AlertType.INFORMATION); diff --git a/src/main/java/techreborn/manual/designer/fileUtils/LoadSystem.java b/src/main/java/techreborn/manual/designer/fileUtils/LoadSystem.java index 14ea777a0..0e035acf4 100644 --- a/src/main/java/techreborn/manual/designer/fileUtils/LoadSystem.java +++ b/src/main/java/techreborn/manual/designer/fileUtils/LoadSystem.java @@ -1,7 +1,9 @@ package techreborn.manual.designer.fileUtils; import com.google.gson.Gson; +import javafx.scene.control.Alert; import javafx.scene.control.TreeItem; +import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import techreborn.manual.designer.ManualCatergories; import techreborn.manual.designer.ManualDesigner; @@ -19,20 +21,28 @@ import java.io.FileReader; public class LoadSystem { public static void load() throws FileNotFoundException { - FileChooser fileChooser = new FileChooser(); - fileChooser.setTitle("Open master.json"); - fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("Jason File (*.json)", "*.json")); - fileChooser.setInitialFileName("master.json"); + DirectoryChooser fileChooser = new DirectoryChooser(); + fileChooser.setTitle("Open Folder"); fileChooser.setInitialDirectory(new File(".")); - File file = fileChooser.showOpenDialog(ManualDesigner.stage); - if(file != null){ - if(file.getName().endsWith(".json")){ + File folder = fileChooser.showDialog(ManualDesigner.stage); + if(folder != null){ + File masterJson = new File(folder, "master.json"); + if(!masterJson.exists()){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Could not find project"); + alert.setHeaderText("Could not find project!"); + alert.setContentText("Could not find the project in this directory!"); + alert.show(); + } else if(masterJson.getName().endsWith(".json")){ //Things - BufferedReader reader = new BufferedReader(new FileReader(file)); + File imageDir = new File(folder, "images"); + if(!imageDir.exists()){ + imageDir.mkdir(); + } + BufferedReader reader = new BufferedReader(new FileReader(masterJson)); Gson gson = new Gson(); ManualFormat format; format = gson.fromJson(reader, ManualFormat.class); - System.out.println(format.entries); for(Entry entry : format.entries){ System.out.println(entry.type); TreeItem parentTree = null; @@ -49,6 +59,7 @@ public class LoadSystem { parentTree.setExpanded(true); SaveSystem.entries.put(newItem, entry); } + SaveSystem.lastSave = folder; } } } diff --git a/src/main/java/techreborn/manual/designer/fileUtils/SaveSystem.java b/src/main/java/techreborn/manual/designer/fileUtils/SaveSystem.java index 85ae42b77..44dd2bcdc 100644 --- a/src/main/java/techreborn/manual/designer/fileUtils/SaveSystem.java +++ b/src/main/java/techreborn/manual/designer/fileUtils/SaveSystem.java @@ -24,7 +24,7 @@ public class SaveSystem { public static ManualFormat master; - static File lastSave = null; + public static File lastSave = null; public static void export(){ List entryList = new ArrayList<>(entries.values()); @@ -40,6 +40,10 @@ public class SaveSystem { chooser.setInitialDirectory(openLocation); File selectedDirectory = chooser.showDialog(ManualDesigner.stage); lastSave = selectedDirectory; + File imageDir = new File(selectedDirectory, "images"); + if(!imageDir.exists()){ + imageDir.mkdir(); + } File masterJson = new File(selectedDirectory, "master.json"); if(masterJson.exists()){ masterJson.delete(); diff --git a/src/main/java/techreborn/manual/designer/windows/MainWindowController.java b/src/main/java/techreborn/manual/designer/windows/MainWindowController.java index 41df6b95f..e656b7d25 100644 --- a/src/main/java/techreborn/manual/designer/windows/MainWindowController.java +++ b/src/main/java/techreborn/manual/designer/windows/MainWindowController.java @@ -4,6 +4,7 @@ import javafx.event.ActionEvent; import javafx.event.Event; import javafx.scene.control.Button; import javafx.scene.control.*; +import javafx.scene.control.Label; import javafx.scene.control.MenuBar; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; @@ -34,6 +35,9 @@ public class MainWindowController { public TextArea textInput; public Button buttonDelete; public TextField nameTextArea; + public TextField imageTextArea; + public ImageView pageimage; + public Label infoLabel; public void newItem(Event event) { if(treeList.getSelectionModel().getSelectedItem() instanceof TreeItem){ @@ -114,6 +118,9 @@ public class MainWindowController { nameTextArea.textProperty().addListener((observable, oldValue, newValue) -> { nameChange(); }); + imageTextArea.textProperty().addListener((observable, oldValue, newValue) -> { + imageAreaChanged(); + }); } @@ -140,6 +147,28 @@ public class MainWindowController { } } + public void imageAreaChanged() { + if(!treeList.getSelectionModel().isEmpty()){ + if(SaveSystem.entries.containsKey(treeList.getSelectionModel().getSelectedItem())){ + Entry entry = SaveSystem.entries.get(treeList.getSelectionModel().getSelectedItem()); + if(entry != null){ + if(entry.data == null){ + entry.data = new EntryData(); + } + if(entry.data.data == null){ + entry.data.data = new HashMap<>(); + } + if(entry.data.data.containsKey("image")){ + entry.data.data.replace("image", imageTextArea.getText()); + } else { + entry.data.data.put("image", imageTextArea.getText()); + } + } + SaveSystem.entries.replace((TreeItem) treeList.getSelectionModel().getSelectedItem(), entry); + } + } + } + public void nameChange() { if(!treeList.getSelectionModel().isEmpty()){ if(SaveSystem.entries.containsKey(treeList.getSelectionModel().getSelectedItem())){ diff --git a/src/main/resources/assets/techreborn/designer/mainWindow.fxml b/src/main/resources/assets/techreborn/designer/mainWindow.fxml index 03306f611..8cf7c5b7d 100644 --- a/src/main/resources/assets/techreborn/designer/mainWindow.fxml +++ b/src/main/resources/assets/techreborn/designer/mainWindow.fxml @@ -13,7 +13,7 @@ - + @@ -52,12 +52,15 @@ + -