Browse Source

Fixed load/save

Thomas Buck 12 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,6 +32,7 @@ import java.util.Scanner;
32 32
 import java.io.FileWriter;
33 33
 import java.io.File;
34 34
 import java.io.IOException;
35
+import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
35 36
 
36 37
 public class cubeWorker {
37 38
 
@@ -220,7 +221,8 @@ public class cubeWorker {
220 221
     try {
221 222
       animations = AnimationUtility.readFile(path);
222 223
     } catch (Exception e) {
223
-      System.out.println(e.toString());
224
+	  System.out.println("Did not load!");
225
+      e.printStackTrace(System.out);
224 226
       return -1;
225 227
     }
226 228
     int size = 0;
@@ -284,7 +286,14 @@ class AnimationUtility {
284 286
   ArrayList<Animation> animations = new ArrayList<Animation>();
285 287
 
286 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 297
   } while (sc.hasNextLine());
289 298
 
290 299
   return animations;
@@ -304,7 +313,7 @@ class AnimationUtility {
304 313
     try {
305 314
       output = new FileWriter(f);
306 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 318
     } catch (Exception e) {
310 319
       lastError = e.toString();
@@ -331,22 +340,25 @@ class AnimationUtility {
331 340
   Animation anim = new Animation();
332 341
   AFrame f = null;
333 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 349
   anim.setName(sc.nextLine());
336 350
   while (size > 0) {
337 351
     f = readFrame(sc, index);
338
-    anim.add(index);
339
-    anim.set(f, index);
352
+    anim.add(index, f);
340 353
     index++;
341 354
     size--;
342 355
   }
343
-
344 356
   return anim;
345 357
   }
346 358
 
347 359
   private static AFrame readFrame(Scanner sc, int index) {
348 360
   AFrame frame = new AFrame();
349
-  frame.setName("Frame " + index);
361
+  frame.setName(sc.nextLine());
350 362
   byte[] d = {};
351 363
   for (int i = 0; i < 8; i++) {
352 364
     byte[] data = hexConvert(sc.nextLine());
@@ -367,25 +379,24 @@ class AnimationUtility {
367 379
 
368 380
   private static byte[] hexConvert(String hex) {
369 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 387
     f.write(anim.size() + "\n");
380 388
   f.write(anim.getName() + "\n");
381 389
     for (int i = 0; i < anim.size(); i++) {
382 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 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 400
       writeLayer(fr.getLayer(i), f);
390 401
     }
391 402
     f.write(Integer.toString( (fr.getTime() & 0xff) + 0x100, 16).substring(1) + "\n");
@@ -485,6 +496,15 @@ class Animation {
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 508
   int size() {
489 509
     return frames.size();
490 510
   }

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

@@ -573,6 +573,14 @@ public class frame extends JFrame implements ListSelectionListener {
573 573
     }
574 574
     animPath.setText(file.getPath());
575 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