Browse Source

Fixed load/save

Thomas Buck 13 years ago
parent
commit
2e5b635b4a
2 changed files with 45 additions and 17 deletions
  1. 37
    17
      Cube Control/cubeWorker.java
  2. 8
    0
      Cube Control/frame.java

+ 37
- 17
Cube Control/cubeWorker.java View File

32
 import java.io.FileWriter;
32
 import java.io.FileWriter;
33
 import java.io.File;
33
 import java.io.File;
34
 import java.io.IOException;
34
 import java.io.IOException;
35
+import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
35
 
36
 
36
 public class cubeWorker {
37
 public class cubeWorker {
37
 
38
 
220
     try {
221
     try {
221
       animations = AnimationUtility.readFile(path);
222
       animations = AnimationUtility.readFile(path);
222
     } catch (Exception e) {
223
     } catch (Exception e) {
223
-      System.out.println(e.toString());
224
+	  System.out.println("Did not load!");
225
+      e.printStackTrace(System.out);
224
       return -1;
226
       return -1;
225
     }
227
     }
226
     int size = 0;
228
     int size = 0;
284
   ArrayList<Animation> animations = new ArrayList<Animation>();
286
   ArrayList<Animation> animations = new ArrayList<Animation>();
285
 
287
 
286
   do {
288
   do {
287
-    animations.add(readAnimation(sc));
289
+	Animation tmp = readAnimation(sc);
290
+	if (tmp == null) {
291
+		return animations;
292
+	}
293
+	if (sc.hasNextLine()) {
294
+		sc.nextLine();
295
+	}
296
+    animations.add(tmp);
288
   } while (sc.hasNextLine());
297
   } while (sc.hasNextLine());
289
 
298
 
290
   return animations;
299
   return animations;
304
     try {
313
     try {
305
       output = new FileWriter(f);
314
       output = new FileWriter(f);
306
       for (int i = 0; i < animations.size(); i++) {
315
       for (int i = 0; i < animations.size(); i++) {
307
-        writeAnimation(animations.get(i), output);
316
+        writeAnimation(animations.get(i), output, (i == (animations.size() - 1)));
308
       }
317
       }
309
     } catch (Exception e) {
318
     } catch (Exception e) {
310
       lastError = e.toString();
319
       lastError = e.toString();
331
   Animation anim = new Animation();
340
   Animation anim = new Animation();
332
   AFrame f = null;
341
   AFrame f = null;
333
   int index = 0;
342
   int index = 0;
334
-  int size = sc.nextInt();
343
+  String tmpSize = sc.nextLine().replaceAll("\\n", "");
344
+  if (tmpSize.equals("")) {
345
+	  return null;
346
+  }
347
+  Integer tmpSizeAgain = new Integer(tmpSize);
348
+  int size = tmpSizeAgain.intValue();
335
   anim.setName(sc.nextLine());
349
   anim.setName(sc.nextLine());
336
   while (size > 0) {
350
   while (size > 0) {
337
     f = readFrame(sc, index);
351
     f = readFrame(sc, index);
338
-    anim.add(index);
339
-    anim.set(f, index);
352
+    anim.add(index, f);
340
     index++;
353
     index++;
341
     size--;
354
     size--;
342
   }
355
   }
343
-
344
   return anim;
356
   return anim;
345
   }
357
   }
346
 
358
 
347
   private static AFrame readFrame(Scanner sc, int index) {
359
   private static AFrame readFrame(Scanner sc, int index) {
348
   AFrame frame = new AFrame();
360
   AFrame frame = new AFrame();
349
-  frame.setName("Frame " + index);
361
+  frame.setName(sc.nextLine());
350
   byte[] d = {};
362
   byte[] d = {};
351
   for (int i = 0; i < 8; i++) {
363
   for (int i = 0; i < 8; i++) {
352
     byte[] data = hexConvert(sc.nextLine());
364
     byte[] data = hexConvert(sc.nextLine());
367
 
379
 
368
   private static byte[] hexConvert(String hex) {
380
   private static byte[] hexConvert(String hex) {
369
   hex = hex.replaceAll("\\n", "");
381
   hex = hex.replaceAll("\\n", "");
370
-  int length = hex.length();
371
-  byte[] data = new byte[length / 2];
372
-  for (int i = 0; i < length; i += 2) {
373
-    data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
374
-  }
375
-  return data;
382
+  HexBinaryAdapter a = new HexBinaryAdapter();
383
+  return a.unmarshal(hex);
376
   }
384
   }
377
 
385
 
378
-  private static void writeAnimation(Animation anim, FileWriter f) throws IOException {
386
+  private static void writeAnimation(Animation anim, FileWriter f, boolean last) throws IOException {
379
     f.write(anim.size() + "\n");
387
     f.write(anim.size() + "\n");
380
   f.write(anim.getName() + "\n");
388
   f.write(anim.getName() + "\n");
381
     for (int i = 0; i < anim.size(); i++) {
389
     for (int i = 0; i < anim.size(); i++) {
382
       writeFrame(anim.get(i), f);
390
       writeFrame(anim.get(i), f);
383
     }
391
     }
384
-    f.write("\n");
392
+    if (!last) {
393
+		f.write("\n");
394
+	}
385
   }
395
   }
386
 
396
 
387
   private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
397
   private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
388
-    for (int i = 0; i < 8; i++) {
398
+    f.write(fr.getName() + "\n");
399
+	for (int i = 0; i < 8; i++) {
389
       writeLayer(fr.getLayer(i), f);
400
       writeLayer(fr.getLayer(i), f);
390
     }
401
     }
391
     f.write(Integer.toString( (fr.getTime() & 0xff) + 0x100, 16).substring(1) + "\n");
402
     f.write(Integer.toString( (fr.getTime() & 0xff) + 0x100, 16).substring(1) + "\n");
485
     }
496
     }
486
   }
497
   }
487
 
498
 
499
+  void add(int i, AFrame f) {
500
+    try {
501
+      frames.add(i, f);
502
+      lastFrameIndex++;
503
+    } catch (IndexOutOfBoundsException e)  {
504
+      System.out.println(e.toString());
505
+    }
506
+  }
507
+
488
   int size() {
508
   int size() {
489
     return frames.size();
509
     return frames.size();
490
   }
510
   }

+ 8
- 0
Cube Control/frame.java View File

573
     }
573
     }
574
     animPath.setText(file.getPath());
574
     animPath.setText(file.getPath());
575
     worker.loadState(animPath.getText());
575
     worker.loadState(animPath.getText());
576
+	animModel.clear();
577
+	for (int i = 0; i < worker.numOfAnimations(); i++) {
578
+		animModel.addElement(worker.getAnimationName(i));
579
+	}
580
+	animList.setModel(animModel);
581
+
582
+	frameListModel.clear();
583
+	frameList.setModel(frameListModel);
576
   }
584
   }
577
   }
585
   }
578
 
586
 

Loading…
Cancel
Save