Browse Source

Huge changes in the layereditor, now (almost) works...

Max Nuding 12 years ago
parent
commit
ae61b1cbce
3 changed files with 148 additions and 50 deletions
  1. 41
    18
      Cube Control/cubeWorker.java
  2. 103
    28
      Cube Control/frame.java
  3. 4
    4
      Cube Control/makefile

+ 41
- 18
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
 
@@ -55,6 +56,9 @@ public class cubeWorker {
55 56
   cubeWorker() {
56 57
   animations.add(new Animation());
57 58
   animations.get(0).setName("Animation 1");
59
+  animations.get(0).add(0);
60
+  animations.get(0).get(0).setName("Frame 1");
61
+  framesRemaining--;
58 62
   }
59 63
 
60 64
   cubeWorker(ArrayList<Animation> anims) {
@@ -158,7 +162,7 @@ public class cubeWorker {
158 162
     framesRemaining--;
159 163
     int s = animations.get(anim).size();
160 164
     animations.get(anim).add(s);
161
-  animations.get(anim).get(s).setName("Frame " + (2016 - framesRemaining));
165
+  animations.get(anim).get(s).setName("Frame " + animations.get(anim).size());
162 166
     return s;
163 167
     }
164 168
 
@@ -217,7 +221,8 @@ public class cubeWorker {
217 221
     try {
218 222
       animations = AnimationUtility.readFile(path);
219 223
     } catch (Exception e) {
220
-      System.out.println(e.toString());
224
+	  System.out.println("Did not load!");
225
+      e.printStackTrace(System.out);
221 226
       return -1;
222 227
     }
223 228
     int size = 0;
@@ -281,7 +286,14 @@ class AnimationUtility {
281 286
   ArrayList<Animation> animations = new ArrayList<Animation>();
282 287
 
283 288
   do {
284
-    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);
285 297
   } while (sc.hasNextLine());
286 298
 
287 299
   return animations;
@@ -301,7 +313,7 @@ class AnimationUtility {
301 313
     try {
302 314
       output = new FileWriter(f);
303 315
       for (int i = 0; i < animations.size(); i++) {
304
-        writeAnimation(animations.get(i), output);
316
+        writeAnimation(animations.get(i), output, (i == (animations.size() - 1)));
305 317
       }
306 318
     } catch (Exception e) {
307 319
       lastError = e.toString();
@@ -328,22 +340,25 @@ class AnimationUtility {
328 340
   Animation anim = new Animation();
329 341
   AFrame f = null;
330 342
   int index = 0;
331
-  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();
332 349
   anim.setName(sc.nextLine());
333 350
   while (size > 0) {
334 351
     f = readFrame(sc, index);
335
-    anim.add(index);
336
-    anim.set(f, index);
352
+    anim.add(index, f);
337 353
     index++;
338 354
     size--;
339 355
   }
340
-
341 356
   return anim;
342 357
   }
343 358
 
344 359
   private static AFrame readFrame(Scanner sc, int index) {
345 360
   AFrame frame = new AFrame();
346
-  frame.setName("Frame " + index);
361
+  frame.setName(sc.nextLine());
347 362
   byte[] d = {};
348 363
   for (int i = 0; i < 8; i++) {
349 364
     byte[] data = hexConvert(sc.nextLine());
@@ -364,25 +379,24 @@ class AnimationUtility {
364 379
 
365 380
   private static byte[] hexConvert(String hex) {
366 381
   hex = hex.replaceAll("\\n", "");
367
-  int length = hex.length();
368
-  byte[] data = new byte[length / 2];
369
-  for (int i = 0; i < length; i += 2) {
370
-    data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
371
-  }
372
-  return data;
382
+  HexBinaryAdapter a = new HexBinaryAdapter();
383
+  return a.unmarshal(hex);
373 384
   }
374 385
 
375
-  private static void writeAnimation(Animation anim, FileWriter f) throws IOException {
386
+  private static void writeAnimation(Animation anim, FileWriter f, boolean last) throws IOException {
376 387
     f.write(anim.size() + "\n");
377 388
   f.write(anim.getName() + "\n");
378 389
     for (int i = 0; i < anim.size(); i++) {
379 390
       writeFrame(anim.get(i), f);
380 391
     }
381
-    f.write("\n");
392
+    if (!last) {
393
+		f.write("\n");
394
+	}
382 395
   }
383 396
 
384 397
   private static void writeFrame(AFrame fr, FileWriter f) throws IOException {
385
-    for (int i = 0; i < 8; i++) {
398
+    f.write(fr.getName() + "\n");
399
+	for (int i = 0; i < 8; i++) {
386 400
       writeLayer(fr.getLayer(i), f);
387 401
     }
388 402
     f.write(Integer.toString( (fr.getTime() & 0xff) + 0x100, 16).substring(1) + "\n");
@@ -483,6 +497,15 @@ class Animation {
483 497
     }
484 498
   }
485 499
 
500
+  void add(int i, AFrame f) {
501
+    try {
502
+      frames.add(i, f);
503
+      lastFrameIndex++;
504
+    } catch (IndexOutOfBoundsException e)  {
505
+      System.out.println(e.toString());
506
+    }
507
+  }
508
+
486 509
   int size() {
487 510
     return frames.size();
488 511
   }

+ 103
- 28
Cube Control/frame.java View File

@@ -57,6 +57,7 @@ public class frame extends JFrame implements ListSelectionListener {
57 57
   private JButton frameDown = new JButton();
58 58
   private JButton frameAdd = new JButton();
59 59
   private JButton frameRemove = new JButton();
60
+  private JButton frameRename = new JButton();
60 61
   private JButton frame = new JButton();
61 62
   private JList animList = new JList();
62 63
   private DefaultListModel animModel = new DefaultListModel();
@@ -65,6 +66,7 @@ public class frame extends JFrame implements ListSelectionListener {
65 66
   private JButton animDown = new JButton();
66 67
   private JButton animAdd = new JButton();
67 68
   private JButton animRemove = new JButton();
69
+  private JButton animRename = new JButton();
68 70
   private JTextField animPath = new JTextField();
69 71
   private JButton load = new JButton();
70 72
   private JButton save = new JButton();
@@ -86,47 +88,64 @@ public class frame extends JFrame implements ListSelectionListener {
86 88
     String[] Optionen = {"Yes", "No"};
87 89
     int Auswahl = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Save?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Optionen, Optionen[0]);
88 90
     if (Auswahl == JOptionPane.YES_OPTION) {
89
-       worker.saveState(animPath.getText());
90 91
        return 1;
91 92
     } else {
92 93
        return 0;
93 94
     }
94 95
   }
95 96
 
97
+  private String askString(String title, String text) {
98
+    return JOptionPane.showInputDialog(null, text, title, JOptionPane.QUESTION_MESSAGE);
99
+  }
100
+
96 101
   private void errorMessage(String s) {
97 102
   String[] Optionen = {"OK"};
98 103
   JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
99 104
   }
100 105
 
101 106
   public void valueChanged(ListSelectionEvent evt) {
102
-    if ((!evt.getValueIsAdjusting()) && (evt.getSource() != animList) && (evt.getSource() != frameList)) {
103
-     DefaultListModel model = (DefaultListModel)((JList)evt.getSource()).getModel();
107
+    if ((!evt.getValueIsAdjusting()) && ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
104 108
      int anim = animList.getSelectedIndex();
105 109
      int max;
106
-     if(evt.getSource() == animList){
110
+   if (anim == -1){
111
+        anim = 0;
112
+     }
113
+     if(evt.getSource() == frameList){
107 114
        max = worker.numOfAnimations();
108
-       System.out.println(max);
109
-
115
+     animModel.clear();
110 116
      } else {
111
-       max = worker.numOfFrames(animList.getSelectedIndex());
112
-
117
+       max = worker.numOfFrames(anim);
118
+     frameListModel.clear();
113 119
      }
114 120
 
115
-     if (anim == -1){
116
-        anim = 0;
117
-     }
118
-     model.clear();
119
-     for (int i = 0; i < max; i++) {
121
+   // if value changed in anim, rebuild frame, else other way round
122
+   for (int i = 0; i < max; i++) {
120 123
        if(evt.getSource() == animList){
121
-          model.add(i, worker.getAnimationName(i));
124
+      frameListModel.addElement(worker.getFrameName(anim, i));
125
+      frameList.setModel(frameListModel);
122 126
        } else {
123
-         model.add(i, worker.getFrameName(anim, i));
127
+      animModel.addElement(worker.getAnimationName(i));
128
+      animList.setModel(animModel);
124 129
        }
125
-    }
126
-    frameList.setModel(model);
130
+   }
127 131
     }
128 132
   }
129 133
 
134
+  private void save() {
135
+    if (fileSelected == false) {
136
+        JFileChooser fc = new JFileChooser();
137
+        int ret = fc.showSaveDialog(this);
138
+        if (ret == JFileChooser.APPROVE_OPTION) {
139
+          File file = fc.getSelectedFile();
140
+          fileSelected = true;
141
+          animPath.setText(file.getPath());
142
+          worker.saveState(animPath.getText());
143
+        }
144
+      } else {
145
+      worker.saveState(animPath.getText());
146
+      }
147
+  }
148
+
130 149
   public frame(String title) {
131 150
     // Frame-Initialisierung
132 151
     super(title);
@@ -143,7 +162,11 @@ public class frame extends JFrame implements ListSelectionListener {
143 162
     addWindowListener(new WindowAdapter() {
144 163
       public void windowClosing(WindowEvent evt) {
145 164
              if (worker.changedStateSinceSave()) {
146
-                  saveExitDialog();
165
+                  if (saveExitDialog() == 1) {
166
+            save();
167
+          } else {
168
+            return;
169
+          }
147 170
              }
148 171
              System.exit(0);
149 172
       }
@@ -276,6 +299,7 @@ public class frame extends JFrame implements ListSelectionListener {
276 299
     });
277 300
 
278 301
     frameDown.setBounds(544, 122, 107, 28);
302
+
279 303
     frameDown.setText("Move down");
280 304
     frameDown.setFont(new Font("Dialog", Font.PLAIN, 13));
281 305
     cp.add(frameDown);
@@ -286,6 +310,28 @@ public class frame extends JFrame implements ListSelectionListener {
286 310
     });
287 311
 
288 312
     frameAdd.setBounds(544, 46, 107, 28);
313
+  frameRename.setText("Rename");
314
+  frameRename.setFont(new Font("Dialog", Font.PLAIN, 13));
315
+  cp.add(frameRename);
316
+  frameRename.addActionListener(new ActionListener() {
317
+    public void actionPerformed(ActionEvent evt) {
318
+      int a = animList.getSelectedIndex();
319
+      if (a < 0) {
320
+        errorMessage("Select an animation!");
321
+        return;
322
+      }
323
+      int f = frameList.getSelectedIndex();
324
+      if (f < 0) {
325
+        errorMessage("Select a frame!");
326
+        return;
327
+      }
328
+      worker.setFrameName(askString("Rename", "Rename " + frameList.getSelectedValue() + "?"), a, f);
329
+      frameListModel.set(f, worker.getFrameName(a, f));
330
+      frameList.setModel(frameListModel);
331
+    }
332
+  });
333
+
334
+
289 335
     frameAdd.setText("Add");
290 336
     frameAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
291 337
     cp.add(frameAdd);
@@ -295,7 +341,11 @@ public class frame extends JFrame implements ListSelectionListener {
295 341
       }
296 342
     });
297 343
 
344
+
298 345
     frameRemove.setBounds(544, 84, 107, 28);
346
+
347
+    frameRemove.setBounds(544, 84, 107, 33);
348
+
299 349
     frameRemove.setText("Remove");
300 350
     frameRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
301 351
     cp.add(frameRemove);
@@ -319,6 +369,7 @@ public class frame extends JFrame implements ListSelectionListener {
319 369
     animList.setModel(animModel);
320 370
     //jList2Model.addElement("");
321 371
     cp.add(animScrollPane);
372
+
322 373
     animUp.setBounds(224, 264, 99, 25);
323 374
     animUp.setText("Move up");
324 375
     animUp.setFont(new Font("Dialog", Font.PLAIN, 13));
@@ -329,7 +380,7 @@ public class frame extends JFrame implements ListSelectionListener {
329 380
       }
330 381
     });
331 382
 
332
-    animDown.setBounds(224, 360, 99, 25);
383
+    animDown.setBounds(224, 342, 99, 25);
333 384
     animDown.setText("Move down");
334 385
     animDown.setFont(new Font("Dialog", Font.PLAIN, 13));
335 386
     cp.add(animDown);
@@ -339,7 +390,24 @@ public class frame extends JFrame implements ListSelectionListener {
339 390
       }
340 391
     });
341 392
 
342
-    animAdd.setBounds(224, 296, 99, 25);
393
+  animRename.setBounds(224, 368, 99, 25);
394
+  animRename.setText("Rename");
395
+  animRename.setFont(new Font("Dialog", Font.PLAIN, 13));
396
+  cp.add(animRename);
397
+  animRename.addActionListener(new ActionListener() {
398
+    public void actionPerformed(ActionEvent evt) {
399
+      int a = animList.getSelectedIndex();
400
+      if (a < 0) {
401
+        errorMessage("Select an animation!");
402
+        return;
403
+      }
404
+      worker.setAnimationName(askString("Rename", "Rename " + animList.getSelectedValue() + "?"), a);
405
+      animModel.set(a, worker.getAnimationName(a));
406
+      animList.setModel(animModel);
407
+    }
408
+  });
409
+
410
+    animAdd.setBounds(224, 290, 99, 25);
343 411
     animAdd.setText("Add");
344 412
     animAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
345 413
     cp.add(animAdd);
@@ -349,7 +417,7 @@ public class frame extends JFrame implements ListSelectionListener {
349 417
       }
350 418
     });
351 419
 
352
-    animRemove.setBounds(224, 328, 99, 25);
420
+    animRemove.setBounds(224, 316, 99, 25);
353 421
     animRemove.setText("Remove");
354 422
     animRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
355 423
     cp.add(animRemove);
@@ -418,12 +486,12 @@ public class frame extends JFrame implements ListSelectionListener {
418 486
     });
419 487
 
420 488
     jLabel4.setBounds(536, 208, 112, 20);
421
-    jLabel4.setText("Frames remaining:");
489
+    jLabel4.setText("Remaining:");
422 490
     jLabel4.setFont(new Font("Dialog", Font.PLAIN, 13));
423 491
     cp.add(jLabel4);
424 492
     frameRemaining.setBounds(536, 232, 113, 24);
425 493
     frameRemaining.setEditable(false);
426
-    frameRemaining.setText("2048");
494
+    frameRemaining.setText(String.valueOf(worker.framesRemaining()));
427 495
     frameRemaining.setFont(new Font("Dialog", Font.PLAIN, 13));
428 496
     cp.add(frameRemaining);
429 497
     animList.setFont(new Font("Dialog", Font.PLAIN, 13));
@@ -542,13 +610,12 @@ public class frame extends JFrame implements ListSelectionListener {
542 610
       errorMessage("Could not add animation!");
543 611
     } else {
544 612
     int n = worker.numOfAnimations() - 1;
545
-    if (n < 0) {
613
+    // would have 0 anims after successfully adding one...
614
+  /*if (n < 0) {
546 615
       n = 0;
547
-    }
616
+    }*/
548 617
     animModel.clear();
549
-    System.out.println(n);
550
-    //animModel.addElement(worker.getAnimationName(n));
551
-    for (int i = 0; i < n; i++) {
618
+    for (int i = 0; i < (n + 1); i++) {
552 619
         animModel.add(i, worker.getAnimationName(i));
553 620
     }
554 621
     animList.setModel(animModel);
@@ -572,6 +639,14 @@ public class frame extends JFrame implements ListSelectionListener {
572 639
     }
573 640
     animPath.setText(file.getPath());
574 641
     worker.loadState(animPath.getText());
642
+  animModel.clear();
643
+  for (int i = 0; i < worker.numOfAnimations(); i++) {
644
+    animModel.addElement(worker.getAnimationName(i));
645
+  }
646
+  animList.setModel(animModel);
647
+
648
+  frameListModel.clear();
649
+  frameList.setModel(frameListModel);
575 650
   }
576 651
   }
577 652
 

+ 4
- 4
Cube Control/makefile View File

@@ -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 AFrame.class Animation.class AnimationUtility.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 frame$$23.class frame$$24.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' 'AFrame.class' 'Animation.class' 'AnimationUtility.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' 'frame$$23.class' 'frame$$24.class' 'AFrame.class' 'Animation.class' 'AnimationUtility.class' 'LEDoff.png' 'LEDon.png'
12 12
 endif
13 13
 
14 14
 all: build clean

Loading…
Cancel
Save