Skip to content

Commit b155db9

Browse files
committed
Adding timeline images
1 parent 9938edd commit b155db9

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

src/main/java/com/neuronrobotics/bowlerstudio/scripting/cadoodle/CaDoodleFile.java

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import javax.imageio.ImageIO;
1919
import javafx.scene.image.WritableImage;
2020
import org.apache.commons.io.FileUtils;
21+
import org.python.google.common.io.Files;
2122

2223
import com.google.gson.Gson;
2324
import com.google.gson.GsonBuilder;
@@ -116,6 +117,10 @@ public void initialize() {
116117
fireInitializationStart();
117118
initializing = true;
118119
if (selfInternal != null) {
120+
File parent = selfInternal.getAbsoluteFile().getParentFile();
121+
File imageCacheDir = new File(parent.getAbsolutePath() + delim() +"timeline");
122+
if(!imageCacheDir.exists())
123+
imageCacheDir.mkdir();
119124
File db = new File(selfInternal.getAbsoluteFile().getParent() + delim() + "CSGdatabase.json");
120125
try {
121126
// set a temp file for the database to clear
@@ -295,8 +300,15 @@ public Thread regenerateCurrent() {
295300

296301
private void process(ICaDoodleOpperation op) {
297302
List<CSG> process = op.process(getCurrentState());
303+
int currentIndex2 = getCurrentIndex();
304+
try {
305+
setTimelineImage(process, currentIndex2);
306+
} catch (IOException e) {
307+
// TODO Auto-generated catch block
308+
e.printStackTrace();
309+
}
298310
storeResultInCache(op, process);
299-
setCurrentIndex(getCurrentIndex() + 1);
311+
setCurrentIndex(currentIndex2 + 1);
300312
setCurrentState(op, process);
301313
}
302314

@@ -623,32 +635,43 @@ public File save() throws IOException {
623635

624636
synchronized (selfInternal) {
625637
String contents = toJson();
638+
List<CSG> currentState = getCurrentState();
639+
int currentIndex2 = getCurrentIndex();
626640
FileUtils.write(selfInternal, contents, StandardCharsets.UTF_8, false);
627-
File parent = selfInternal.getAbsoluteFile().getParentFile();
628-
File image = new File(parent.getAbsolutePath() + delim() + "snapshot.png");
629-
setImage(null);
630-
loadingImageFromUIThread();
631-
632-
WritableImage image2 = getImage();
633-
if (image2 != null) {
634-
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image2, null);
635-
try {
636-
ImageIO.write(bufferedImage, "png", image);
637-
// com.neuronrobotics.sdk.common.Log.error("Thumbnail saved successfully to " +
638-
// image.getAbsolutePath());
639-
} catch (IOException e) {
640-
// com.neuronrobotics.sdk.common.Log.error("Error saving image: " +
641-
// e.getMessage());
642-
e.printStackTrace();
643-
}
644-
}
641+
setTimelineImage(currentState, currentIndex2);
645642
if (bom != null)
646643
bom.save();
647644
}
648645

649646
return getSelf();
650647
}
651648

649+
private void setTimelineImage(List<CSG> currentState, int currentIndex2) throws IOException {
650+
File parent = selfInternal.getAbsoluteFile().getParentFile();
651+
File image = new File(parent.getAbsolutePath() + delim() + "snapshot.png");
652+
File imageCache = new File(parent.getAbsolutePath() + delim() +"timeline"+ delim() + currentIndex2+".png");
653+
if(imageCache.exists())
654+
return;
655+
WritableImage image2 = loadingImageFromUIThread( currentState);
656+
if (image2 != null) {
657+
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image2, null);
658+
try {
659+
ImageIO.write(bufferedImage, "png", imageCache);
660+
System.err.println("Thumbnail saved successfully to " +
661+
imageCache.getAbsolutePath());
662+
} catch (IOException e) {
663+
// com.neuronrobotics.sdk.common.Log.error("Error saving image: " +
664+
// e.getMessage());
665+
e.printStackTrace();
666+
}
667+
668+
if(currentIndex2 == currentIndex) {
669+
Files.copy(imageCache,image );
670+
}
671+
672+
}
673+
}
674+
652675
public WritableImage loadImageFromFile() {
653676
try {
654677
File parent = selfInternal.getAbsoluteFile().getParentFile();
@@ -659,7 +682,7 @@ public WritableImage loadImageFromFile() {
659682
img = SwingFXUtils.toFXImage(bufferedImage, null);
660683
}
661684
} else {
662-
loadingImageFromUIThread();
685+
loadingImageFromUIThread( getCurrentState());
663686
}
664687
} catch (Exception e) {
665688
com.neuronrobotics.sdk.common.Log.error("Error loading image: " + e.getMessage());
@@ -668,14 +691,17 @@ public WritableImage loadImageFromFile() {
668691
return img;
669692
}
670693

671-
private void loadingImageFromUIThread() {
694+
private javafx.scene.image.WritableImage loadingImageFromUIThread(List<CSG> currentState ) {
695+
ArrayList<javafx.scene.image.WritableImage> holder = new ArrayList<WritableImage>();
672696
try {
673-
BowlerKernel.runLater(() -> setImage(ThumbnailImage.get(getCurrentState())));
697+
BowlerKernel.runLater(() -> {
698+
holder.add(ThumbnailImage.get(currentState));
699+
});
674700
} catch (Throwable ex) {
675701
ex.printStackTrace();
676-
return;
702+
return null;
677703
}
678-
while (getImage() == null)
704+
while (holder.size()==0)
679705
try {
680706
Thread.sleep(16);
681707
// com.neuronrobotics.sdk.common.Log.error("Waiting for image to write");
@@ -684,6 +710,7 @@ private void loadingImageFromUIThread() {
684710
e.printStackTrace();
685711
break;
686712
}
713+
return holder.get(0);
687714
}
688715

689716
public static CaDoodleFile fromJsonString(String content) throws Exception {

0 commit comments

Comments
 (0)