|
@@ -28,6 +28,7 @@
|
28
|
28
|
|
29
|
29
|
import java.util.ArrayList;
|
30
|
30
|
import java.util.Arrays;
|
|
31
|
+import java.util.Scanner;
|
31
|
32
|
import java.io.FileWriter;
|
32
|
33
|
import java.io.File;
|
33
|
34
|
import java.io.IOException;
|
|
@@ -46,8 +47,6 @@ public class cubeWorker {
|
46
|
47
|
// --------------------
|
47
|
48
|
|
48
|
49
|
private ArrayList<Animation> animations = new ArrayList<Animation>();
|
49
|
|
- private int selectedAnimation = 0;
|
50
|
|
- private int selectedFrame = 0;
|
51
|
50
|
private int framesRemaining = 2016; // (128 * 1024) / 65 = 2016,...
|
52
|
51
|
private boolean changedState = false;
|
53
|
52
|
|
|
@@ -71,7 +70,7 @@ public class cubeWorker {
|
71
|
70
|
}
|
72
|
71
|
|
73
|
72
|
// Returns how many frames are in the current animation
|
74
|
|
- public int numOfFrames() {
|
|
73
|
+ public int numOfFrames(int selectedAnimation) {
|
75
|
74
|
return animations.get(selectedAnimation).size();
|
76
|
75
|
}
|
77
|
76
|
|
|
@@ -85,17 +84,6 @@ public class cubeWorker {
|
85
|
84
|
// Animation Specific
|
86
|
85
|
// --------------------
|
87
|
86
|
|
88
|
|
- // Selects an animation on wich the animation specific functions operate
|
89
|
|
- // Returns -1 if it does not exist, else its index
|
90
|
|
- public int selectAnimation(int index) {
|
91
|
|
- if (animations.size() <= index) {
|
92
|
|
- return -1;
|
93
|
|
- } else {
|
94
|
|
- selectedAnimation = index;
|
95
|
|
- return index;
|
96
|
|
- }
|
97
|
|
- }
|
98
|
|
-
|
99
|
87
|
// Adds a new Animation
|
100
|
88
|
// Returns id if ok, -1 if error or not enough space for
|
101
|
89
|
// another animation
|
|
@@ -111,22 +99,22 @@ public class cubeWorker {
|
111
|
99
|
}
|
112
|
100
|
|
113
|
101
|
// Removes an animation
|
114
|
|
- public void removeAnimation() {
|
|
102
|
+ public void removeAnimation(int selectedAnimation) {
|
115
|
103
|
changedState = true;
|
116
|
104
|
animations.remove(selectedAnimation);
|
117
|
105
|
selectedAnimation = 0;
|
118
|
106
|
}
|
119
|
107
|
|
120
|
|
- public String getAnimationName() {
|
|
108
|
+ public String getAnimationName(int selectedAnimation) {
|
121
|
109
|
return animations.get(selectedAnimation).getName();
|
122
|
110
|
}
|
123
|
111
|
|
124
|
|
- public void setAnimationName(String s) {
|
|
112
|
+ public void setAnimationName(String s, int selectedAnimation) {
|
125
|
113
|
changedState = true;
|
126
|
114
|
animations.get(selectedAnimation).setName(s);
|
127
|
115
|
}
|
128
|
116
|
|
129
|
|
- public void moveAnimation(int dir) {
|
|
117
|
+ public void moveAnimation(int dir, int selectedAnimation) {
|
130
|
118
|
changedState = true;
|
131
|
119
|
if (dir == UP){
|
132
|
120
|
//animation moved up
|
|
@@ -149,83 +137,70 @@ public class cubeWorker {
|
149
|
137
|
// Frame Specific
|
150
|
138
|
// --------------------
|
151
|
139
|
|
152
|
|
- // Selects an animation on wich the frame specific functions operate
|
153
|
|
- // Returns -1 if it does not exist, else its index
|
154
|
|
- public int selectFrame(int index) {
|
155
|
|
- if (animations.get(selectedAnimation).size() <= index) {
|
156
|
|
- return -1;
|
157
|
|
- } else {
|
158
|
|
- selectedFrame = index;
|
159
|
|
- return index;
|
160
|
|
- }
|
161
|
|
- }
|
162
|
|
-
|
163
|
|
- public String getFrameName() {
|
164
|
|
- return animations.get(selectedAnimation).get(selectedFrame).getName();
|
|
140
|
+ public String getFrameName(int anim, int frame) {
|
|
141
|
+ return animations.get(anim).get(frame).getName();
|
165
|
142
|
}
|
166
|
143
|
|
167
|
|
- public void setFrameName(String s) {
|
|
144
|
+ public void setFrameName(String s, int anim, int frame) {
|
168
|
145
|
changedState = true;
|
169
|
|
- animations.get(selectedAnimation).get(selectedFrame).setName(s);
|
|
146
|
+ animations.get(anim).get(frame).setName(s);
|
170
|
147
|
}
|
171
|
148
|
|
172
|
149
|
// Adds a Frame to the current animation.
|
173
|
150
|
// Returns id if okay, -1 if error
|
174
|
|
- public int addFrame() {
|
|
151
|
+ public int addFrame(int anim) {
|
175
|
152
|
changedState = true;
|
176
|
153
|
if (framesRemaining <= 0) {
|
177
|
154
|
return -1;
|
178
|
155
|
}
|
179
|
156
|
framesRemaining--;
|
180
|
|
- int s = animations.get(selectedAnimation).size();
|
181
|
|
- animations.get(selectedAnimation).add(s);
|
|
157
|
+ int s = animations.get(anim).size();
|
|
158
|
+ animations.get(anim).add(s);
|
|
159
|
+ animations.get(anim).get(s).setName("Frame " + (2016 - framesRemaining));
|
182
|
160
|
return s;
|
183
|
161
|
}
|
184
|
162
|
|
185
|
163
|
// Remove the frame
|
186
|
|
- public void removeFrame() {
|
|
164
|
+ public void removeFrame(int anim, int frame) {
|
187
|
165
|
changedState = true;
|
188
|
|
- animations.get(selectedAnimation).remove(selectedFrame);
|
189
|
|
- selectedFrame = 0;
|
|
166
|
+ animations.get(anim).remove(frame);
|
190
|
167
|
}
|
191
|
168
|
|
192
|
169
|
// Returns array with 64 bytes with led values
|
193
|
|
- public byte[] getFrame() {
|
194
|
|
- return animations.get(selectedAnimation).get(selectedFrame).getData();
|
|
170
|
+ public byte[] getFrame(int anim, int frame) {
|
|
171
|
+ return animations.get(anim).get(frame).getData();
|
195
|
172
|
}
|
196
|
173
|
|
197
|
|
- public void setFrame(byte[] data) {
|
|
174
|
+ public void setFrame(byte[] data, int anim, int frame) {
|
198
|
175
|
changedState = true;
|
199
|
|
- animations.get(selectedAnimation).get(selectedFrame).setData(data);
|
|
176
|
+ animations.get(anim).get(frame).setData(data);
|
200
|
177
|
}
|
201
|
178
|
|
202
|
179
|
// Frame duration in 1/24th of a second
|
203
|
|
- public byte getFrameTime() {
|
204
|
|
- return animations.get(selectedAnimation).get(selectedFrame).getTime();
|
|
180
|
+ public byte getFrameTime(int anim, int frame) {
|
|
181
|
+ return animations.get(anim).get(frame).getTime();
|
205
|
182
|
}
|
206
|
183
|
|
207
|
|
- public void setFrameTime(byte time) {
|
|
184
|
+ public void setFrameTime(byte time, int anim, int frame) {
|
208
|
185
|
changedState = true;
|
209
|
|
- animations.get(selectedAnimation).get(selectedFrame).setTime(time);
|
|
186
|
+ animations.get(anim).get(frame).setTime(time);
|
210
|
187
|
}
|
211
|
188
|
|
212
|
|
- public void moveFrame(int dir){
|
|
189
|
+ public void moveFrame(int dir, int anim, int frame){
|
213
|
190
|
changedState = true;
|
214
|
191
|
if (dir == UP){
|
215
|
192
|
// frame moved up
|
216
|
|
- if (selectedFrame > 0) {
|
217
|
|
- AFrame tmp = animations.get(selectedAnimation).get(selectedFrame);
|
218
|
|
- animations.get(selectedAnimation).set(animations.get(selectedAnimation).get(selectedFrame - 1), selectedFrame);
|
219
|
|
- animations.get(selectedAnimation).set(tmp, selectedFrame - 1);
|
220
|
|
- selectedFrame--;
|
221
|
|
- }
|
|
193
|
+ if (frame > 0) {
|
|
194
|
+ AFrame tmp = animations.get(anim).get(frame);
|
|
195
|
+ animations.get(anim).set(animations.get(anim).get(frame - 1), frame);
|
|
196
|
+ animations.get(anim).set(tmp, frame - 1);
|
|
197
|
+ }
|
222
|
198
|
} else if (dir == DOWN){
|
223
|
199
|
// frame moved down
|
224
|
|
- if (selectedFrame < (animations.get(selectedAnimation).size() - 1)) {
|
225
|
|
- AFrame tmp = animations.get(selectedAnimation).get(selectedFrame);
|
226
|
|
- animations.get(selectedAnimation).set(animations.get(selectedAnimation).get(selectedFrame + 1), selectedFrame);
|
227
|
|
- animations.get(selectedAnimation).set(tmp, selectedFrame + 1);
|
228
|
|
- selectedFrame++;
|
|
200
|
+ if (frame < (animations.get(anim).size() - 1)) {
|
|
201
|
+ AFrame tmp = animations.get(anim).get(frame);
|
|
202
|
+ animations.get(anim).set(animations.get(anim).get(frame + 1), frame);
|
|
203
|
+ animations.get(anim).set(tmp, frame + 1);
|
229
|
204
|
}
|
230
|
205
|
}
|
231
|
206
|
}
|
|
@@ -237,7 +212,20 @@ public class cubeWorker {
|
237
|
212
|
// Loads an animation file into this object
|
238
|
213
|
public int loadState(String path) {
|
239
|
214
|
changedState = false;
|
240
|
|
-
|
|
215
|
+ try {
|
|
216
|
+ animations = AnimationUtility.readFile(path);
|
|
217
|
+ } catch (Exception e) {
|
|
218
|
+ System.out.println(e.toString());
|
|
219
|
+ return -1;
|
|
220
|
+ }
|
|
221
|
+ int size = 0;
|
|
222
|
+ for (int i = 0; i < animations.size(); i++) {
|
|
223
|
+ size += animations.get(i).size();
|
|
224
|
+ }
|
|
225
|
+ framesRemaining = 2016 - size;
|
|
226
|
+ if (size > 2016) {
|
|
227
|
+ return -1;
|
|
228
|
+ }
|
241
|
229
|
return 0;
|
242
|
230
|
}
|
243
|
231
|
|
|
@@ -286,8 +274,15 @@ public class cubeWorker {
|
286
|
274
|
class AnimationUtility {
|
287
|
275
|
private static String lastError = null;
|
288
|
276
|
|
289
|
|
- public static ArrayList<Animation> readFile(String path) {
|
290
|
|
- return null;
|
|
277
|
+ public static ArrayList<Animation> readFile(String path) throws Exception {
|
|
278
|
+ Scanner sc = new Scanner(new File(path));
|
|
279
|
+ ArrayList<Animation> animations = new ArrayList<Animation>();
|
|
280
|
+
|
|
281
|
+ do {
|
|
282
|
+ animations.add(readAnimation(sc));
|
|
283
|
+ } while (sc.hasNextLine());
|
|
284
|
+
|
|
285
|
+ return animations;
|
291
|
286
|
}
|
292
|
287
|
|
293
|
288
|
public static void writeFile(String path, ArrayList<Animation> animations) {
|
|
@@ -327,8 +322,57 @@ class AnimationUtility {
|
327
|
322
|
return tmp;
|
328
|
323
|
}
|
329
|
324
|
|
|
325
|
+ private static Animation readAnimation(Scanner sc) {
|
|
326
|
+ Animation anim = new Animation();
|
|
327
|
+ AFrame f = null;
|
|
328
|
+ int index = 0;
|
|
329
|
+ int size = sc.nextInt();
|
|
330
|
+ anim.setName(sc.nextLine());
|
|
331
|
+ while (size > 0) {
|
|
332
|
+ f = readFrame(sc, index);
|
|
333
|
+ anim.add(index);
|
|
334
|
+ anim.set(f, index);
|
|
335
|
+ index++;
|
|
336
|
+ size--;
|
|
337
|
+ }
|
|
338
|
+
|
|
339
|
+ return anim;
|
|
340
|
+ }
|
|
341
|
+
|
|
342
|
+ private static AFrame readFrame(Scanner sc, int index) {
|
|
343
|
+ AFrame frame = new AFrame();
|
|
344
|
+ frame.setName("Frame " + index);
|
|
345
|
+ byte[] d = {};
|
|
346
|
+ for (int i = 0; i < 8; i++) {
|
|
347
|
+ byte[] data = hexConvert(sc.nextLine());
|
|
348
|
+ d = concat(data, d);
|
|
349
|
+ }
|
|
350
|
+ frame.setData(d);
|
|
351
|
+ d = hexConvert(sc.nextLine());
|
|
352
|
+ frame.setTime(d[0]);
|
|
353
|
+ return frame;
|
|
354
|
+ }
|
|
355
|
+
|
|
356
|
+ private static byte[] concat(byte[] a, byte[] b) {
|
|
357
|
+ byte[] c = new byte[a.length + b.length];
|
|
358
|
+ System.arraycopy(a, 0, c, 0, a.length);
|
|
359
|
+ System.arraycopy(b, 0, c, a.length, b.length);
|
|
360
|
+ return c;
|
|
361
|
+ }
|
|
362
|
+
|
|
363
|
+ private static byte[] hexConvert(String hex) {
|
|
364
|
+ hex = hex.replaceAll("\\n", "");
|
|
365
|
+ int length = hex.length();
|
|
366
|
+ byte[] data = new byte[length / 2];
|
|
367
|
+ for (int i = 0; i < length; i += 2) {
|
|
368
|
+ data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
|
|
369
|
+ }
|
|
370
|
+ return data;
|
|
371
|
+ }
|
|
372
|
+
|
330
|
373
|
private static void writeAnimation(Animation anim, FileWriter f) throws IOException {
|
331
|
|
- f.write(anim.getName() + "\n");
|
|
374
|
+ f.write(anim.size() + "\n");
|
|
375
|
+ f.write(anim.getName() + "\n");
|
332
|
376
|
for (int i = 0; i < anim.size(); i++) {
|
333
|
377
|
writeFrame(anim.get(i), f);
|
334
|
378
|
}
|
|
@@ -439,4 +483,4 @@ class Animation {
|
439
|
483
|
int size() {
|
440
|
484
|
return frames.size();
|
441
|
485
|
}
|
442
|
|
-}
|
|
486
|
+}
|