1 package com.neuronrobotics.bowlerstudio.assets;
3 import com.neuronrobotics.bowlerstudio.IssueReportingExceptionHandler;
4 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
7 import javafx.fxml.FXMLLoader;
8 import javafx.scene.image.Image;
9 import javafx.scene.image.ImageView;
10 import javafx.scene.image.PixelReader;
11 import javafx.scene.image.WritableImage;
13 import javax.imageio.ImageIO;
15 import org.eclipse.jgit.api.errors.RefNotFoundException;
17 import java.io.BufferedOutputStream;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.util.HashMap;
24 import java.util.List;
28 public static final String
repo =
"BowlerStudioImageAssets";
29 private static String
gitSource =
"https://github.com/madhephaestus/" +
repo +
".git";
30 private static HashMap<String, Image>
cache =
new HashMap<>();
31 private static HashMap<String, FXMLLoader>
loaders =
new HashMap<>();
37 public static FXMLLoader
loadLayout(String file,
boolean refresh)
throws Exception {
39 URL fileURL = fxmlFIle.toURI().toURL();
41 if (
loaders.get(file) ==
null || refresh) {
42 loaders.put(file,
new FXMLLoader(fileURL));
45 loaders.get(file).setLocation(fileURL);
49 public static FXMLLoader
loadLayout(String file)
throws Exception {
53 public static File
loadFile(String file)
throws Exception {
63 int width = (int) img.getWidth();
64 int height = (int) img.getHeight();
65 PixelReader reader = img.getPixelReader();
66 byte[] buffer =
new byte[width * height * 4];
67 javafx.scene.image.WritablePixelFormat<ByteBuffer> format = javafx.scene.image.PixelFormat.getByteBgraInstance();
68 reader.getPixels(0, 0, width, height, format, buffer, 0, width * 4);
70 BufferedOutputStream out =
new BufferedOutputStream(
new FileOutputStream(file));
71 for(
int count = 0; count < buffer.length; count += 4) {
72 out.write(buffer[count + 2]);
73 out.write(buffer[count + 1]);
74 out.write(buffer[count]);
75 out.write(buffer[count + 3]);
79 }
catch(IOException e) {
84 @SuppressWarnings(
"restriction")
85 public static Image
loadAsset(String file) throws Exception {
86 if (
cache.get(file) ==
null) {
88 if (f.getName().endsWith(
".fxml")) {
91 }
else if ((f ==
null || !f.exists()) && f.getName().endsWith(
".png")) {
92 WritableImage obj_img =
new WritableImage(30, 30);
93 byte alpha = (byte) 0;
94 for (
int cx = 0; cx < obj_img.getWidth(); cx++) {
95 for (
int cy = 0; cy < obj_img.getHeight(); cy++) {
96 int color = obj_img.getPixelReader().getArgb(cx, cy);
97 int mc = (alpha << 24) | 0x00ffffff;
98 int newColor = color & mc;
99 obj_img.getPixelWriter().setArgb(cx, cy, newColor);
103 cache.put(file, obj_img);
104 System.out.println(
"No image at " + file);
107 new RuntimeException().printStackTrace();
110 String fileName = imageFile.getName();
113 }
catch (Exception ignored) {
116 }
catch (Exception e) {
119 }
else if (f.getName().endsWith(
".png")) {
120 cache.put(file,
new Image(f.toURI().toString()));
125 return cache.get(file);
131 }
catch (Exception e) {
134 return new ImageView(
loadAsset(
"BowlerStudio.png"));
135 }
catch (Exception e1) {
137 throw new RuntimeException(e1);
156 System.err.println(
"Loading assets");
160 }
catch(Exception ex) {
163 for (String file : files) {
164 System.err.println(
"Loading asset file: "+file);
178 File[] files = folder.listFiles();
180 for (File f : files) {
181 if (f.isDirectory()) {
void except(Throwable e, String stacktraceFromCatch)
static void loadAllAssets()
static void setGitSource(String gitSource, String assetRepoBranch)
static HashMap< String, Image > cache
static FXMLLoader loadLayout(String file)
static File loadFile(String file)
static FXMLLoader loadLayout(String file, boolean refresh)
static String getGitSource()
static void setAssetRepoBranch(String assetRepoBranch)
static String getAssetRepoBranch()
static Image loadAsset(String file)
static ImageView loadIcon(String file)
static void deleteFolder(File folder)
static HashMap< String, FXMLLoader > loaders
static void writeImage(Image img, File file)
static String assetRepoBranch
static String getVersion()
static File createFile(String git, String fileName, String commitMessage)
static ArrayList< String > filesInGit(String remote, String branch, String extnetion)
static File fileFromGit(String remoteURI, String fileInRepo)