|
@@ -27,6 +27,10 @@
|
27
|
27
|
*/
|
28
|
28
|
|
29
|
29
|
import java.util.ArrayList;
|
|
30
|
+import java.util.Arrays;
|
|
31
|
+import java.io.FileWriter;
|
|
32
|
+import java.io.File;
|
|
33
|
+import java.io.IOException;
|
30
|
34
|
|
31
|
35
|
public class cubeWorker {
|
32
|
36
|
|
|
@@ -53,6 +57,10 @@ public class cubeWorker {
|
53
|
57
|
animations.add(new Animation());
|
54
|
58
|
}
|
55
|
59
|
|
|
60
|
+ cubeWorker(ArrayList<Animation> anims) {
|
|
61
|
+ animations = anims;
|
|
62
|
+ }
|
|
63
|
+
|
56
|
64
|
// --------------------
|
57
|
65
|
// Misc. Methods
|
58
|
66
|
// --------------------
|
|
@@ -236,8 +244,11 @@ public class cubeWorker {
|
236
|
244
|
// Saves the state of this object in an animation file
|
237
|
245
|
public int saveState(String path) {
|
238
|
246
|
changedState = false;
|
239
|
|
- System.out.println("Saving to " + path);
|
240
|
|
-
|
|
247
|
+ AnimationUtility.writeFile(path, animations);
|
|
248
|
+ if (AnimationUtility.getLastError() != null) {
|
|
249
|
+ System.out.println(AnimationUtility.getLastError());
|
|
250
|
+ return -1;
|
|
251
|
+ }
|
241
|
252
|
return 0;
|
242
|
253
|
}
|
243
|
254
|
|
|
@@ -272,6 +283,76 @@ public class cubeWorker {
|
272
|
283
|
|
273
|
284
|
}
|
274
|
285
|
|
|
286
|
+class AnimationUtility {
|
|
287
|
+ private static String lastError = null;
|
|
288
|
+
|
|
289
|
+ public static ArrayList<Animation> readFile(String path) {
|
|
290
|
+ return null;
|
|
291
|
+ }
|
|
292
|
+
|
|
293
|
+ public static void writeFile(String path, ArrayList<Animation> animations) {
|
|
294
|
+ File f = new File(path);
|
|
295
|
+ if (f.exists()) {
|
|
296
|
+ try {
|
|
297
|
+ f.delete();
|
|
298
|
+ } catch (Exception e) {
|
|
299
|
+ lastError = e.toString();
|
|
300
|
+ return;
|
|
301
|
+ }
|
|
302
|
+ }
|
|
303
|
+ FileWriter output = null;
|
|
304
|
+ try {
|
|
305
|
+ output = new FileWriter(f);
|
|
306
|
+ for (int i = 0; i < animations.size(); i++) {
|
|
307
|
+ writeAnimation(animations.get(i), output);
|
|
308
|
+ }
|
|
309
|
+ } catch (Exception e) {
|
|
310
|
+ lastError = e.toString();
|
|
311
|
+ return;
|
|
312
|
+ } finally {
|
|
313
|
+ if (output != null) {
|
|
314
|
+ try {
|
|
315
|
+ output.close();
|
|
316
|
+ } catch (Exception e) {
|
|
317
|
+ lastError = e.toString();
|
|
318
|
+ }
|
|
319
|
+ }
|
|
320
|
+ }
|
|
321
|
+
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ public static String getLastError() {
|
|
325
|
+ String tmp = lastError;
|
|
326
|
+ lastError = null;
|
|
327
|
+ return tmp;
|
|
328
|
+ }
|
|
329
|
+
|
|
330
|
+ private static void writeAnimation(Animation anim, FileWriter f) throws IOException {
|
|
331
|
+ f.write(anim.getName() + "\n");
|
|
332
|
+ for (int i = 0; i < anim.size(); i++) {
|
|
333
|
+ writeFrame(anim.get(i), f);
|
|
334
|
+ }
|
|
335
|
+ f.write("\n");
|
|
336
|
+ }
|
|
337
|
+
|
|
338
|
+ private static void writeFrame(Frame fr, FileWriter f) throws IOException {
|
|
339
|
+ for (int i = 0; i < 8; i++) {
|
|
340
|
+ writeLayer(fr.getLayer(i), f);
|
|
341
|
+ }
|
|
342
|
+ f.write(Integer.toString( (fr.getTime() & 0xff) + 0x100, 16).substring(1) + "\n");
|
|
343
|
+ }
|
|
344
|
+
|
|
345
|
+ private static void writeLayer(byte[] l, FileWriter f) throws IOException {
|
|
346
|
+ String hex = "";
|
|
347
|
+ for (int i = 0; i < l.length; i++) {
|
|
348
|
+ hex += Integer.toString( (l[i] & 0xff) + 0x100, 16).substring(1);
|
|
349
|
+ }
|
|
350
|
+ hex += "\n";
|
|
351
|
+
|
|
352
|
+ f.write(hex);
|
|
353
|
+ }
|
|
354
|
+}
|
|
355
|
+
|
275
|
356
|
class Frame {
|
276
|
357
|
private byte[] data = new byte[64];
|
277
|
358
|
private byte duration = 1;
|
|
@@ -300,6 +381,10 @@ class Frame {
|
300
|
381
|
byte getTime() {
|
301
|
382
|
return duration;
|
302
|
383
|
}
|
|
384
|
+
|
|
385
|
+ byte[] getLayer(int i) {
|
|
386
|
+ return Arrays.copyOfRange(data, (i * 8), (i * 8) + 8);
|
|
387
|
+ }
|
303
|
388
|
}
|
304
|
389
|
|
305
|
390
|
class Animation {
|