BowlerKernel
AssetFactory.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.assets;
2 
3 import com.neuronrobotics.bowlerstudio.IssueReportingExceptionHandler;
4 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
5 
6 //import javafx.embed.swing.SwingFXUtils;
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;
12 
13 import javax.imageio.ImageIO;
14 
15 import org.eclipse.jgit.api.errors.RefNotFoundException;
16 
17 import java.io.BufferedOutputStream;
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.net.URL;
22 import java.nio.ByteBuffer;
23 import java.util.HashMap;
24 import java.util.List;
25 
26 public class AssetFactory {
27 
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<>();
32  private static String assetRepoBranch = "";
33 
34  private AssetFactory() {
35  }
36 
37  public static FXMLLoader loadLayout(String file, boolean refresh) throws Exception {
38  File fxmlFIle = loadFile(file);
39  URL fileURL = fxmlFIle.toURI().toURL();
40 
41  if (loaders.get(file) == null || refresh) {
42  loaders.put(file, new FXMLLoader(fileURL));
43  }
44 
45  loaders.get(file).setLocation(fileURL);
46  return loaders.get(file);
47  }
48 
49  public static FXMLLoader loadLayout(String file) throws Exception {
50  return loadLayout(file, false);
51  }
52 
53  public static File loadFile(String file) throws Exception {
54  return ScriptingEngine
55  .fileFromGit(
56  getGitSource(),// git repo, change this if you fork this demo
58  file// File from within the Git repo
59  );
60  }
61 
62  public static void writeImage(Image img, File file) {
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);
69  try {
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]);
76  }
77  out.flush();
78  out.close();
79  } catch(IOException e) {
80  e.printStackTrace();
81  }
82  }
83 
84  @SuppressWarnings("restriction")
85  public static Image loadAsset(String file) throws Exception {
86  if (cache.get(file) == null) {
87  File f = loadFile(file);
88  if (f.getName().endsWith(".fxml")) {
89  loadLayout(file);
90  return null;
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);
100  }
101  }
102 
103  cache.put(file, obj_img);
104  System.out.println("No image at " + file);
105 
106  try {
107  new RuntimeException().printStackTrace();
108  File imageFile = ScriptingEngine.createFile(getGitSource(), file, "create file");
109  try {
110  String fileName = imageFile.getName();
111  writeImage(obj_img, imageFile);
112 
113  } catch (Exception ignored) {
114  }
115  ScriptingEngine.createFile(getGitSource(), file, "saving new content");
116  } catch (Exception e) {
117  e.printStackTrace();
118  }
119  } else if (f.getName().endsWith(".png")) {
120  cache.put(file, new Image(f.toURI().toString()));
121  }else {
122  return null;
123  }
124  }
125  return cache.get(file);
126  }
127 
128  public static ImageView loadIcon(String file) {
129  try {
130  return new ImageView(loadAsset(file));
131  } catch (Exception e) {
132  e.printStackTrace();
133  try {
134  return new ImageView(loadAsset("BowlerStudio.png"));
135  } catch (Exception e1) {
137  throw new RuntimeException(e1);
138  }
139  }
140  }
141 
142  public static String getGitSource() throws Exception {
143  return gitSource;
144  }
145 
146  public static void setGitSource(String gitSource, String assetRepoBranch) throws Exception {
147  System.err.println("Assets from: " + gitSource + "#" + assetRepoBranch);
148  //new Exception().printStackTrace();
151  cache.clear();
152  loadAllAssets();
153  }
154 
155  public static void loadAllAssets() throws Exception {
156  System.err.println("Loading assets");
157  List<String> files;
158  try {
160  }catch(Exception ex) {
162  }
163  for (String file : files) {
164  System.err.println("Loading asset file: "+file);
165  loadAsset(file);
166  }
167  }
168 
169  public static String getAssetRepoBranch() {
170  return assetRepoBranch;
171  }
172 
173  public static void setAssetRepoBranch(String assetRepoBranch) {
175  }
176 
177  public static void deleteFolder(File folder) {
178  File[] files = folder.listFiles();
179  if (files != null) { //some JVMs return null for empty dirs
180  for (File f : files) {
181  if (f.isDirectory()) {
182  deleteFolder(f);
183  } else {
184  f.delete();
185  }
186  }
187  }
188  folder.delete();
189  }
190 }
static void setGitSource(String gitSource, String assetRepoBranch)
static FXMLLoader loadLayout(String file, boolean refresh)
static void setAssetRepoBranch(String assetRepoBranch)
static HashMap< String, FXMLLoader > loaders
static void writeImage(Image img, File file)
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)