Explorar el Código

Worker reads file, fixes in frame

Thomas Buck hace 13 años
padre
commit
84ae110989
Se han modificado 3 ficheros con 151 adiciones y 98 borrados
  1. 108
    64
      Cube Control/cubeWorker.java
  2. 39
    30
      Cube Control/frame.java
  3. 4
    4
      Cube Control/makefile

+ 108
- 64
Cube Control/cubeWorker.java Ver fichero

@@ -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
+}

+ 39
- 30
Cube Control/frame.java Ver fichero

@@ -32,7 +32,7 @@ import javax.vecmath.*;
32 32
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
33 33
  */
34 34
 
35
-public class frame extends JFrame {
35
+public class frame extends JFrame implements ListSelectionListener {
36 36
   // Anfang Variablen
37 37
   private GraphicsConfiguration gConfig = SimpleUniverse.getPreferredConfiguration();
38 38
   private Canvas3D cubeCanvas = new Canvas3D(gConfig);
@@ -96,6 +96,16 @@ public class frame extends JFrame {
96 96
   JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
97 97
   }
98 98
 
99
+  public void valueChanged(ListSelectionEvent evt) {
100
+    if (!evt.getValueIsAdjusting()) {
101
+	   int anim = animList.getSelectedIndex();
102
+	   for (int i = 0; i < worker.numOfFrames(anim); i++) {
103
+		   frameListModel.add(i, worker.getFrameName(anim, i));
104
+		}
105
+		frameList.setModel(frameListModel);
106
+    }
107
+  }
108
+
99 109
   public frame(String title) {
100 110
     // Frame-Initialisierung
101 111
     super(title);
@@ -106,10 +116,7 @@ public class frame extends JFrame {
106 116
     }
107 117
     
108 118
     for(int i = 0; i < worker.numOfAnimations(); i++){
109
-      animModel.addElement(worker.getAnimationName());
110
-    }
111
-    for(int i = 0; i < worker.numOfFrames(); i++){
112
-      frameListModel.add(i, worker.getFrameName());
119
+      animModel.addElement(worker.getAnimationName(i));
113 120
     }
114 121
 
115 122
     addWindowListener(new WindowAdapter() {
@@ -390,7 +397,7 @@ public class frame extends JFrame {
390 397
     animList.setFont(new Font("Dialog", Font.PLAIN, 13));
391 398
     frameList.setFont(new Font("Dialog", Font.PLAIN, 13));
392 399
     // Ende Komponenten
393
-    animList.addListSelectionListener(new MyListSelectionListener(animList, worker));
400
+    animList.addListSelectionListener(this);
394 401
     setResizable(false);
395 402
     setVisible(true);
396 403
   }
@@ -437,7 +444,7 @@ public class frame extends JFrame {
437 444
             frameListModel.set(i, frameListModel.get(i - 1));
438 445
             frameListModel.set(i - 1, tmp);
439 446
             frameList.setSelectedIndex(i - 1);
440
-            worker.moveFrame(worker.UP);
447
+            worker.moveFrame(worker.UP, animList.getSelectedIndex(), frameList.getSelectedIndex());
441 448
          }
442 449
   }
443 450
 
@@ -448,18 +455,26 @@ public class frame extends JFrame {
448 455
             frameListModel.set(i, frameListModel.get(i + 1));
449 456
             frameListModel.set(i + 1, tmp);
450 457
             frameList.setSelectedIndex(i + 1);
451
-            worker.moveFrame(worker.DOWN);
458
+            worker.moveFrame(worker.DOWN, animList.getSelectedIndex(), frameList.getSelectedIndex());
452 459
          }
453 460
   }
454 461
 
455 462
   public void frameAdd_ActionPerformed(ActionEvent evt) {
456
-         worker.addFrame();
463
+         worker.addFrame(animList.getSelectedIndex());
457 464
          frameRemaining.setText(Integer.toString(worker.framesRemaining()));
465
+		 int n = worker.numOfFrames(animList.getSelectedIndex()) - 1;
466
+		 if (n < 0) {
467
+			 n = 0;
468
+		 }
469
+		 frameListModel.add(n, worker.getFrameName(animList.getSelectedIndex(), n));
470
+		 frameList.setModel(frameListModel);
458 471
   }
459 472
 
460 473
   public void frameRemove_ActionPerformed(ActionEvent evt) {
461
-         worker.removeFrame();
474
+         worker.removeFrame(animList.getSelectedIndex(), frameList.getSelectedIndex());
462 475
          frameRemaining.setText(Integer.toString(worker.framesRemaining()));
476
+		 frameListModel.removeElementAt(frameList.getSelectedIndex());
477
+		 frameList.setModel(frameListModel);
463 478
   }
464 479
 
465 480
   public void animUp_ActionPerformed(ActionEvent evt) {
@@ -469,7 +484,7 @@ public class frame extends JFrame {
469 484
             animModel.set(i, animModel.get(i - 1));
470 485
             animModel.set(i - 1, tmp);
471 486
             animList.setSelectedIndex(i - 1);
472
-            worker.moveAnimation(worker.UP);
487
+            worker.moveAnimation(worker.UP, animList.getSelectedIndex());
473 488
          }
474 489
   }
475 490
 
@@ -480,19 +495,28 @@ public class frame extends JFrame {
480 495
             animModel.set(i, animModel.get(i + 1));
481 496
             animModel.set(i + 1, tmp);
482 497
             animList.setSelectedIndex(i + 1);
483
-            worker.moveAnimation(worker.DOWN);
498
+            worker.moveAnimation(worker.DOWN, animList.getSelectedIndex());
484 499
          }
485 500
   }
486 501
 
487 502
   public void animAdd_ActionPerformed(ActionEvent evt) {
488 503
     if(worker.addAnimation() == -1){
489
-      //show error Dialog
490
-    }
504
+      errorMessage("Could not add animation!");
505
+    } else {
506
+		int n = worker.numOfAnimations() - 1;
507
+		if (n < 0) {
508
+			n = 0;
509
+		}
510
+		animModel.add(n, worker.getAnimationName(n));
511
+		animList.setModel(animModel);
512
+	}
491 513
 
492 514
   }
493 515
 
494 516
   public void animRemove_ActionPerformed(ActionEvent evt) {
495
-     worker.removeAnimation();
517
+     worker.removeAnimation(animList.getSelectedIndex());
518
+	 animModel.removeElementAt(animList.getSelectedIndex());
519
+	 animList.setModel(animModel);
496 520
   }
497 521
 
498 522
   public void load_ActionPerformed(ActionEvent evt) {
@@ -566,18 +590,3 @@ public class frame extends JFrame {
566 590
   // Ende Methoden
567 591
 }
568 592
 
569
-class MyListSelectionListener implements ListSelectionListener {
570
-
571
-  JList alist;
572
-  cubeWorker worker;
573
-  MyListSelectionListener(JList animList, cubeWorker w){
574
-    alist = animList;
575
-    worker = w;
576
-  }
577
-  public void valueChanged(ListSelectionEvent evt) {
578
-    if (!evt.getValueIsAdjusting()) {
579
-       worker.selectAnimation(alist.getSelectedIndex());
580
-    }
581
-  }
582
-}
583
-

+ 4
- 4
Cube Control/makefile Ver fichero

@@ -1,14 +1,14 @@
1 1
 JAVAC = javac
2 2
 CC = gcc
3
-#TARGET = unix
4
-TARGET = win
3
+TARGET = unix
4
+#TARGET = win
5 5
 
6 6
 JAVAFILES = cubeWorker.java layerEditFrame.java frame.java
7 7
 
8 8
 ifeq ($(TARGET),win)
9
-CLASSES = cubeWorker.class layerEditFrame.class layerEditFrame$$1.class layerEditFrame$$2.class layerEditFrame$$3.class layerEditFrame$$4.class frame.class frame$$1.class frame$$2.class frame$$3.class frame$$4.class frame$$5.class frame$$6.class frame$$7.class frame$$8.class frame$$9.class frame$$10.class frame$$11.class frame$$12.class frame$$13.class frame$$14.class frame$$15.class frame$$16.class frame$$17.class frame$$18.class frame$$19.class frame$$20.class frame$$21.class frame$$22.class MyListSelectionListener.class AFrame.class Animation.class LEDoff.png LEDon.png
9
+CLASSES = cubeWorker.class layerEditFrame.class layerEditFrame$$1.class layerEditFrame$$2.class layerEditFrame$$3.class layerEditFrame$$4.class frame.class frame$$1.class frame$$2.class frame$$3.class frame$$4.class frame$$5.class frame$$6.class frame$$7.class frame$$8.class frame$$9.class frame$$10.class frame$$11.class frame$$12.class frame$$13.class frame$$14.class frame$$15.class frame$$16.class frame$$17.class frame$$18.class frame$$19.class frame$$20.class frame$$21.class frame$$22.class AFrame.class Animation.class AnimationUtility.class LEDoff.png LEDon.png
10 10
 else
11
-CLASSES = 'cubeWorker.class' 'layerEditFrame.class' 'layerEditFrame$$1.class' 'layerEditFrame$$2.class' 'layerEditFrame$$3.class' 'layerEditFrame$$4.class' 'frame.class' 'frame$$1.class' 'frame$$2.class' 'frame$$3.class' 'frame$$4.class' 'frame$$5.class' 'frame$$6.class' 'frame$$7.class' 'frame$$8.class' 'frame$$9.class' 'frame$$10.class' 'frame$$11.class' 'frame$$12.class' 'frame$$13.class' 'frame$$14.class' 'frame$$15.class' 'frame$$16.class' 'frame$$17.class' 'frame$$18.class' 'frame$$19.class' 'frame$$20.class' 'frame$$21.class' 'frame$$22.class' 'MyListSelectionListener.class' 'AFrame.class' 'Animation.class' 'LEDoff.png' 'LEDon.png'
11
+CLASSES = 'cubeWorker.class' 'layerEditFrame.class' 'layerEditFrame$$1.class' 'layerEditFrame$$2.class' 'layerEditFrame$$3.class' 'layerEditFrame$$4.class' 'frame.class' 'frame$$1.class' 'frame$$2.class' 'frame$$3.class' 'frame$$4.class' 'frame$$5.class' 'frame$$6.class' 'frame$$7.class' 'frame$$8.class' 'frame$$9.class' 'frame$$10.class' 'frame$$11.class' 'frame$$12.class' 'frame$$13.class' 'frame$$14.class' 'frame$$15.class' 'frame$$16.class' 'frame$$17.class' 'frame$$18.class' 'frame$$19.class' 'frame$$20.class' 'frame$$21.class' 'frame$$22.class' 'AFrame.class' 'Animation.class' 'AnimationUtility.class' 'LEDoff.png' 'LEDon.png'
12 12
 endif
13 13
 
14 14
 all: build clean

Loading…
Cancelar
Guardar