Skip to content

Commit d8fa9e2

Browse files
committed
update the timeline immages based on the current state
1 parent b155db9 commit d8fa9e2

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

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

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public void initialize() {
118118
initializing = true;
119119
if (selfInternal != null) {
120120
File parent = selfInternal.getAbsoluteFile().getParentFile();
121-
File imageCacheDir = new File(parent.getAbsolutePath() + delim() +"timeline");
122-
if(!imageCacheDir.exists())
121+
File imageCacheDir = new File(parent.getAbsolutePath() + delim() + "timeline");
122+
if (!imageCacheDir.exists())
123123
imageCacheDir.mkdir();
124124
File db = new File(selfInternal.getAbsoluteFile().getParent() + delim() + "CSGdatabase.json");
125125
try {
@@ -130,7 +130,7 @@ public void initialize() {
130130
CSGDatabase.clear();
131131
createTempFile.delete();
132132
} catch (IOException e) {
133-
// TODO Auto-generated catch block
133+
// Auto-generated catch block
134134
e.printStackTrace();
135135
}
136136
CSGDatabase.setDbFile(db);
@@ -249,8 +249,16 @@ public Thread regenerateFrom(ICaDoodleOpperation source) {
249249
setCurrentIndex(getCurrentIndex() + 1);
250250
setPercentInitialized(((double) getCurrentIndex()) / size);
251251
// com.neuronrobotics.sdk.common.Log.error("Regenerating "+currentIndex);
252-
ICaDoodleOpperation op = opperations.get(getCurrentIndex() - 1);
252+
int currentIndex2 = getCurrentIndex() - 1;
253+
ICaDoodleOpperation op = opperations.get(currentIndex2);
253254
List<CSG> process = op.process(getPreviouState());
255+
getTimelineImageFile(currentIndex2).delete();
256+
try {
257+
setTimelineImage(process, currentIndex2);
258+
} catch (IOException e) {
259+
// Auto-generated catch block
260+
e.printStackTrace();
261+
}
254262
storeResultInCache(op, process);
255263
setCurrentState(op, process);
256264
}
@@ -287,6 +295,14 @@ public Thread regenerateCurrent() {
287295

288296
ICaDoodleOpperation op = getCurrentOpperation();
289297
List<CSG> process = op.process(getPreviouState());
298+
int currentIndex2 = getCurrentIndex();
299+
getTimelineImageFile(currentIndex2).delete();
300+
try {
301+
setTimelineImage(process, currentIndex2);
302+
} catch (IOException e) {
303+
// Auto-generated catch block
304+
e.printStackTrace();
305+
}
290306
storeResultInCache(op, process);
291307
setCurrentState(op, process);
292308
fireSaveSuggestion();
@@ -304,7 +320,7 @@ private void process(ICaDoodleOpperation op) {
304320
try {
305321
setTimelineImage(process, currentIndex2);
306322
} catch (IOException e) {
307-
// TODO Auto-generated catch block
323+
// Auto-generated catch block
308324
e.printStackTrace();
309325
}
310326
storeResultInCache(op, process);
@@ -406,13 +422,32 @@ public static int applyToAllConstituantElements(boolean addRet, String targetNam
406422
return back.size();
407423
}
408424

425+
public File getTimelineImageFile(ICaDoodleOpperation test) {
426+
for (int i = 0; i < getOpperations().size(); i++) {
427+
ICaDoodleOpperation key = getOpperations().get(i);
428+
if (key == test) {
429+
File file = getTimelineImageFile(i);
430+
return file;
431+
}
432+
}
433+
throw new RuntimeException("File not found!");
434+
}
435+
436+
public File getTimelineImageFile(int i) {
437+
File parent = selfInternal.getAbsoluteFile().getParentFile();
438+
File file = new File(parent.getAbsolutePath() + delim() + "timeline" + delim() + (i + 1) + ".png");
439+
return file;
440+
}
441+
409442
private void pruneForward() {
410443
for (int i = getCurrentIndex(); i < getOpperations().size(); i++) {
411444
ICaDoodleOpperation key = getOpperations().get(i);
412445
List<CSG> back = cache.remove(key);
413-
VitaminBomManager boM = CaDoodleFile.getBillOfMaterials();
414446
if (back != null)
415447
back.clear();
448+
File imageCache = getTimelineImageFile(i);
449+
//System.err.println("Deleting " + imageCache.getAbsolutePath());
450+
imageCache.delete();
416451
}
417452
List<ICaDoodleOpperation> subList = (List<ICaDoodleOpperation>) getOpperations().subList(0, getCurrentIndex());
418453
ArrayList<ICaDoodleOpperation> newList = new ArrayList<ICaDoodleOpperation>();
@@ -431,10 +466,11 @@ private void storeResultInCache(ICaDoodleOpperation op, List<CSG> process) {
431466
names.add(c.getName());
432467
cachedCopy.add(cloneCSG(c).setStorage(new PropertyStorage()).syncProperties(c).setName(c.getName())
433468
.setRegenerate(c.getRegenerate()));
434-
//cachedCopy.add(c);
469+
// cachedCopy.add(c);
435470
}
436471
cache.put(op, cachedCopy);
437472
}
473+
438474
private CSG cloneCSG(CSG dyingCSG) {
439475
CSG csg = new CSG();
440476

@@ -467,6 +503,7 @@ private CSG cloneCSG(CSG dyingCSG) {
467503
csg.setColor(dyingCSG.getColor());
468504
return csg;
469505
}
506+
470507
public void back() {
471508
if (isBackAvailible())
472509
setCurrentIndex(getCurrentIndex() - 1);
@@ -648,27 +685,27 @@ public File save() throws IOException {
648685

649686
private void setTimelineImage(List<CSG> currentState, int currentIndex2) throws IOException {
650687
File parent = selfInternal.getAbsoluteFile().getParentFile();
688+
File imageCache = new File(parent.getAbsolutePath() + delim() + "timeline" + delim() + currentIndex2 + ".png");
651689
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())
690+
691+
if (imageCache.exists())
654692
return;
655-
WritableImage image2 = loadingImageFromUIThread( currentState);
693+
WritableImage image2 = loadingImageFromUIThread(currentState);
656694
if (image2 != null) {
657695
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image2, null);
658696
try {
659697
ImageIO.write(bufferedImage, "png", imageCache);
660-
System.err.println("Thumbnail saved successfully to " +
661-
imageCache.getAbsolutePath());
698+
System.err.println("Thumbnail saved successfully to " + imageCache.getAbsolutePath());
662699
} catch (IOException e) {
663700
// com.neuronrobotics.sdk.common.Log.error("Error saving image: " +
664701
// e.getMessage());
665702
e.printStackTrace();
666703
}
667704

668-
if(currentIndex2 == currentIndex) {
669-
Files.copy(imageCache,image );
705+
if (currentIndex2 == currentIndex) {
706+
Files.copy(imageCache, image);
670707
}
671-
708+
672709
}
673710
}
674711

@@ -682,7 +719,7 @@ public WritableImage loadImageFromFile() {
682719
img = SwingFXUtils.toFXImage(bufferedImage, null);
683720
}
684721
} else {
685-
loadingImageFromUIThread( getCurrentState());
722+
loadingImageFromUIThread(getCurrentState());
686723
}
687724
} catch (Exception e) {
688725
com.neuronrobotics.sdk.common.Log.error("Error loading image: " + e.getMessage());
@@ -691,7 +728,7 @@ public WritableImage loadImageFromFile() {
691728
return img;
692729
}
693730

694-
private javafx.scene.image.WritableImage loadingImageFromUIThread(List<CSG> currentState ) {
731+
private javafx.scene.image.WritableImage loadingImageFromUIThread(List<CSG> currentState) {
695732
ArrayList<javafx.scene.image.WritableImage> holder = new ArrayList<WritableImage>();
696733
try {
697734
BowlerKernel.runLater(() -> {
@@ -701,7 +738,7 @@ private javafx.scene.image.WritableImage loadingImageFromUIThread(List<CSG> curr
701738
ex.printStackTrace();
702739
return null;
703740
}
704-
while (holder.size()==0)
741+
while (holder.size() == 0)
705742
try {
706743
Thread.sleep(16);
707744
// com.neuronrobotics.sdk.common.Log.error("Waiting for image to write");

0 commit comments

Comments
 (0)