Browse Source

Fixed some frame stuff.

Auto select animations and frames, consistent order for frame and
animation buttons.
Thomas Buck 12 years ago
parent
commit
287df82da2
2 changed files with 85 additions and 65 deletions
  1. 84
    64
      CubeControl/Frame.java
  2. 1
    1
      CubeFirmware/makefile

+ 84
- 64
CubeControl/Frame.java View File

@@ -33,14 +33,9 @@ import javax.vecmath.Point3d;
33 33
 import java.util.Arrays;
34 34
 
35 35
 public class Frame extends JFrame implements ListSelectionListener, ChangeListener{
36
-
37
-	private static final long serialVersionUID = 23421337L;
38
-	// Anfang Variablen
39 36
 	private GraphicsConfiguration gConfig;
40 37
 	private Canvas3D cubeCanvas;
41 38
 	public Led3D ledView;
42
-
43
-	// Anfang Attribute
44 39
 	private JButton editA = new JButton();
45 40
 	private JButton editB = new JButton();
46 41
 	private JButton editC = new JButton();
@@ -59,7 +54,7 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
59 54
 	private JButton frameRemove = new JButton();
60 55
 	private JButton frameRename = new JButton();
61 56
 	private JList animList = new JList();
62
-	private DefaultListModel animModel = new DefaultListModel();
57
+	private DefaultListModel animListModel = new DefaultListModel();
63 58
 	private JScrollPane animScrollPane = new JScrollPane(animList);
64 59
 	private JButton animUp = new JButton();
65 60
 	private JButton animDown = new JButton();
@@ -89,17 +84,13 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
89 84
 	private JPanel serialPanel = new JPanel();
90 85
 	private JPanel settingsPanel = new JPanel();
91 86
 	private JSlider durationSlider = new JSlider(1, 256 ,1); //min, max, value
92
-
93
-
94
-	// Ende Attribute
95
-	
87
+	private int lastSelectedFrame = 0;
88
+	private int lastSelectedAnim = 0;
96 89
 	public cubeWorker worker = new cubeWorker(this);
97 90
 	private boolean fileSelected = false;
98 91
 	private Frame thisFrame;
99 92
 	private static Frame recentFrame;
100 93
 
101
-	// Ende Variablen
102
-
103 94
 	private int saveExitDialog() {
104 95
 		String[] Optionen = { "Yes", "No" };
105 96
 		int Auswahl = JOptionPane.showOptionDialog(this,
@@ -174,6 +165,20 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
174 165
 			ledView.setData(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getData());
175 166
 		}
176 167
 	}
168
+
169
+	private void rebuildFrameList() {
170
+		// animList selection changed, update frameList
171
+		frameListModel.clear();
172
+
173
+		// System.out.println("Selected Animation: " + animList.getSelectedIndex());
174
+		int max = worker.getAnimation(animList.getSelectedIndex()).size();
175
+	
176
+		for (int i = 0; i < max; i++) {
177
+			frameListModel.addElement(worker.getAnimation(
178
+			animList.getSelectedIndex()).getFrame(i).getName());
179
+		}
180
+		frameList.setModel(frameListModel);
181
+	}
177 182
 	
178 183
 	/**
179 184
 	 * ListSelectionListener that updates Anim & Frame List
@@ -182,40 +187,55 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
182 187
 	 * @param evt List that generated the event. Has to be animList or frameList
183 188
 	 */
184 189
 	public void valueChanged(ListSelectionEvent evt) {
185
-		if ((!evt.getValueIsAdjusting())
186
-			&& ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
187
-			// If animList or framsList is the source, we act...
188
-	
189
-			if ((evt.getSource() == animList)
190
-				&& (animList.getSelectedIndex() != -1)) {
191
-				// animList selection changed, update frameList
192
-				frameListModel.clear();
193
-	
194
-				// System.out.println("Selected Animation: " + animList.getSelectedIndex());
195
-				int max = worker.getAnimation(animList.getSelectedIndex()).size();
196
-	
197
-				for (int i = 0; i < max; i++) {
198
-				frameListModel.addElement(worker.getAnimation(
199
-					animList.getSelectedIndex()).getFrame(i).getName());
190
+		if ((!evt.getValueIsAdjusting()) && ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
191
+			// Select an animation, if not already done
192
+			if (animList.getSelectedIndex() == -1) {
193
+				if ((animListModel.getSize() > lastSelectedAnim) && (lastSelectedAnim != -1)) {
194
+					animList.setSelectedIndex(lastSelectedAnim);
195
+				} else {
196
+					animList.setSelectedIndex(0);
200 197
 				}
201
-				frameList.setModel(frameListModel);
202 198
 			}
203
-	
204
-			// If both selections are valid, update Frame duration and set 3D
205
-			// data
199
+
200
+			// Rebuild the frame list, if possible & needed
201
+			if ((evt.getSource() == animList) && (animList.getSelectedIndex() != -1)) {
202
+				rebuildFrameList();
203
+			}
204
+
205
+			// Select a frame, if not already done
206
+			if (frameList.getSelectedIndex() == -1) {
207
+				if ((frameListModel.getSize() > lastSelectedFrame) && (lastSelectedFrame != -1)) {
208
+					frameList.setSelectedIndex(lastSelectedFrame);
209
+				} else {
210
+					frameList.setSelectedIndex(0);
211
+				}
212
+				lastSelectedFrame = frameList.getSelectedIndex();
213
+			}
214
+
215
+			// If possible, set all data for selected frame
206 216
 			if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
207 217
 				ledView.setData(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getData());
208 218
 				frameLengthText.setText(Integer.toString(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime()));
209
-			} else {
210
-				// clear Frame duration
211
-				frameLengthText.setText("");
219
+				durationSlider.setValue(Integer.parseInt(frameLengthText.getText()));
220
+			}
221
+
222
+			if (frameList.getSelectedIndex() != -1) {
223
+				lastSelectedAnim = animList.getSelectedIndex();
224
+			}
225
+			if (frameList.getSelectedIndex() != -1) {
226
+				lastSelectedFrame = frameList.getSelectedIndex();
227
+			}
228
+			if (lastSelectedAnim == -1) {
229
+				lastSelectedAnim = 0;
230
+			}
231
+			if (lastSelectedFrame == -1) {
232
+				lastSelectedFrame = 0;
212 233
 			}
213 234
 		}
214 235
 	}
215 236
 
216 237
 	public void stateChanged(ChangeEvent e){
217
-			frameLengthText.setText(durationSlider.getValue() + "");
218
-		
238
+		frameLengthText.setText(durationSlider.getValue() + "");
219 239
 	}
220 240
 	
221 241
 	private void save() {
@@ -258,7 +278,7 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
258 278
 
259 279
 		// Build Animation List
260 280
 		for (int i = 0; i < worker.size(); i++) {
261
-			animModel.addElement(worker.getAnimation(i).getName());
281
+			animListModel.addElement(worker.getAnimation(i).getName());
262 282
 		}
263 283
 
264 284
 		// Ask user to save before closing the window
@@ -323,7 +343,7 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
323 343
 		cp.add(frameListScrollPane);
324 344
 		// Add Animation List
325 345
 		animScrollPane.setBounds(18, 378, 187, 219);
326
-		animList.setModel(animModel);
346
+		animList.setModel(animListModel);
327 347
 		cp.add(animScrollPane);
328 348
 
329 349
 		// Add Move Frame Up Button
@@ -432,10 +452,10 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
432 452
 		animUp.addActionListener(new ActionListener() {
433 453
 			public void actionPerformed(ActionEvent evt) {
434 454
 				int i = animList.getSelectedIndex();
435
-				if ((i > 0) && (animModel.getSize() >= 2)) {
436
-					Object tmp = animModel.get(i);
437
-					animModel.set(i, animModel.get(i - 1));
438
-					animModel.set(i - 1, tmp);
455
+				if ((i > 0) && (animListModel.getSize() >= 2)) {
456
+					Object tmp = animListModel.get(i);
457
+					animListModel.set(i, animListModel.get(i - 1));
458
+					animListModel.set(i - 1, tmp);
439 459
 					animList.setSelectedIndex(i - 1);
440 460
 					worker.moveAnimationUp(i);
441 461
 				}
@@ -443,17 +463,17 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
443 463
 		});
444 464
 
445 465
 		// Add Move Animation Down Button
446
-		animDown.setBounds(211, 423, 110, 39);
466
+		animDown.setBounds(211, 558, 110, 39);
447 467
 		animDown.setText("Move down");
448 468
 		animDown.setFont(font);
449 469
 		cp.add(animDown);
450 470
 		animDown.addActionListener(new ActionListener() {
451 471
 			public void actionPerformed(ActionEvent evt) {
452 472
 				int i = animList.getSelectedIndex();
453
-				if ((i >= 0) && (animModel.getSize() >= 2) && (i < (animModel.getSize() - 1))) {
454
-					Object tmp = animModel.get(i);
455
-					animModel.set(i, animModel.get(i + 1));
456
-					animModel.set(i + 1, tmp);
473
+				if ((i >= 0) && (animListModel.getSize() >= 2) && (i < (animListModel.getSize() - 1))) {
474
+					Object tmp = animListModel.get(i);
475
+					animListModel.set(i, animListModel.get(i + 1));
476
+					animListModel.set(i + 1, tmp);
457 477
 					animList.setSelectedIndex(i + 1);
458 478
 					worker.moveAnimationDown(i);
459 479
 				}
@@ -461,7 +481,7 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
461 481
 		});
462 482
 
463 483
 		// Add Rename Animation Button
464
-		animRename.setBounds(211, 468, 110, 39);
484
+		animRename.setBounds(211, 513, 110, 39);
465 485
 		animRename.setText("Rename");
466 486
 		animRename.setFont(font);
467 487
 		cp.add(animRename);
@@ -473,13 +493,13 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
473 493
 					return;
474 494
 				}
475 495
 				worker.getAnimation(a).setName(askString("Rename", "Rename " + animList.getSelectedValue() + "?"));
476
-				animModel.set(a, worker.getAnimation(a).getName());
477
-				animList.setModel(animModel);
496
+				animListModel.set(a, worker.getAnimation(a).getName());
497
+				animList.setModel(animListModel);
478 498
 			}
479 499
 		});
480 500
 
481 501
 		// Add button for adding animations
482
-		animAdd.setBounds(211, 513, 110, 39);
502
+		animAdd.setBounds(211, 423, 110, 39);
483 503
 		animAdd.setText("Add");
484 504
 		animAdd.setFont(font);
485 505
 		cp.add(animAdd);
@@ -488,17 +508,17 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
488 508
 				if (worker.addAnimation() == -1) {
489 509
 					errorMessage("Could not add animation!");
490 510
 				} else {
491
-					animModel.clear();
511
+					animListModel.clear();
492 512
 					for (int i = 0; i < worker.size(); i++) {
493
-						animModel.add(i, worker.getAnimation(i).getName());
513
+						animListModel.add(i, worker.getAnimation(i).getName());
494 514
 					}
495
-					animList.setModel(animModel);
515
+					animList.setModel(animListModel);
496 516
 				}
497 517
 			}
498 518
 		});
499 519
 
500 520
 		// Add Animation remove button
501
-		animRemove.setBounds(211, 558, 110, 39);
521
+		animRemove.setBounds(211, 468, 110, 39);
502 522
 		animRemove.setText("Remove");
503 523
 		animRemove.setFont(font);
504 524
 		cp.add(animRemove);
@@ -508,8 +528,8 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
508 528
 					errorMessage("Select an animation.");
509 529
 				} else {
510 530
 					worker.removeAnimation(animList.getSelectedIndex());
511
-					animModel.removeElementAt(animList.getSelectedIndex());
512
-					animList.setModel(animModel);
531
+					animListModel.removeElementAt(animList.getSelectedIndex());
532
+					animList.setModel(animListModel);
513 533
 				}
514 534
 			}
515 535
 		});
@@ -602,7 +622,7 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
602 622
 		});
603 623
 	
604 624
 		frameDuration.setBounds(462, 129, 55, 20);
605
-		frameDuration.setText("OK");
625
+		frameDuration.setText("Save");
606 626
 		frameDuration.setFont(font);
607 627
 		cp.add(frameDuration);
608 628
 		frameDuration.addActionListener(new ActionListener() {
@@ -628,8 +648,6 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
628 648
 				}
629 649
 			}
630 650
 		});
631
-
632
-		
633 651
 	
634 652
 		animPath.setBounds(417, 281, 228, 20);
635 653
 		animPath.setEditable(false);
@@ -652,11 +670,11 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
652 670
 					}
653 671
 					animPath.setText(file.getPath());
654 672
 					worker.loadState(animPath.getText());
655
-					animModel.clear();
673
+					animListModel.clear();
656 674
 					for (int i = 0; i < worker.size(); i++) {
657
-						animModel.addElement(worker.getAnimation(i).getName());
675
+						animListModel.addElement(worker.getAnimation(i).getName());
658 676
 					}
659
-					animList.setModel(animModel);
677
+					animList.setModel(animListModel);
660 678
 					frameListModel.clear();
661 679
 					frameList.setModel(frameListModel);
662 680
 				}
@@ -743,7 +761,7 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
743 761
 		frameLengthLabel.setFont(font);
744 762
 		cp.add(frameLengthLabel);
745 763
 	
746
-		frameLengthText.setBounds(419, 129, 36, 20);
764
+		frameLengthText.setBounds(419, 129, 40, 20);
747 765
 		frameLengthText.setFont(font);
748 766
 		cp.add(frameLengthText);
749 767
 
@@ -794,6 +812,8 @@ public class Frame extends JFrame implements ListSelectionListener, ChangeListen
794 812
 		frameList.addListSelectionListener(this);
795 813
 		setResizable(false);
796 814
 		setVisible(true);
815
+
816
+		valueChanged(new ListSelectionEvent(animList, 0, 0, false));
797 817
 	}
798 818
 	
799 819
 	public Led3D get3D() {

+ 1
- 1
CubeFirmware/makefile View File

@@ -261,7 +261,7 @@ ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
261 261
 
262 262
 
263 263
 # Default target.
264
-all: begin gccversion sizebefore build sizeafter finished end clean_list
264
+all: begin gccversion sizebefore build sizeafter finished end
265 265
 
266 266
 build: elf hex eep lss sym
267 267
 

Loading…
Cancel
Save