Browse Source

Worked at movement

Thomas Buck 12 years ago
parent
commit
e7e0d91fa4
4 changed files with 156 additions and 10 deletions
  1. 41
    1
      CubeControl/AFrame.java
  2. 62
    2
      CubeControl/Animation.java
  3. 33
    7
      CubeControl/Frame.java
  4. 20
    0
      CubeControl/cubeWorker.java

+ 41
- 1
CubeControl/AFrame.java View File

32
  * @version 1.0
32
  * @version 1.0
33
  */
33
  */
34
 
34
 
35
-public class AFrame {
35
+public class AFrame implements Comparable<AFrame> {
36
 	private short[] data = new short[64];
36
 	private short[] data = new short[64];
37
 	private short duration = 1;
37
 	private short duration = 1;
38
 	private String name = "Frame";
38
 	private String name = "Frame";
39
+	private int order;
40
+	private static int orderIndex = 0;
41
+
42
+	/**
43
+	 * Compare this frame to another frame.
44
+	 * Compares the order in the frame list.
45
+	 * @return 0 if equal, -1 if this one comes first, 1 if last
46
+	 */
47
+	public int compareTo(AFrame compareFrame) {
48
+		if (getOrder() < compareFrame.getOrder()) {
49
+			return -1;
50
+		} else if (getOrder() == compareFrame.getOrder()) {
51
+			return 0;
52
+		} else {
53
+			return 1;
54
+		} 
55
+	}
56
+
57
+	/**
58
+	 * Get index of frame in list of frames.
59
+	 * @return index
60
+	 */
61
+	public int getOrder() {
62
+		return order;
63
+	}
64
+
65
+	/**
66
+	 * Set index of frame in list of frames.
67
+	 * @param newOrder new index
68
+	 */
69
+	public void setOrder(int newOrder) {
70
+		order = newOrder;
71
+	}
72
+
73
+	/**
74
+	 * Inserts frane at end of frame list.
75
+	 */
76
+	public AFrame() {
77
+		order = orderIndex++;
78
+	}
39
 
79
 
40
 	/**
80
 	/**
41
 	 * Gets the Name of this Frame
81
 	 * Gets the Name of this Frame

+ 62
- 2
CubeControl/Animation.java View File

22
  */
22
  */
23
 import java.util.ArrayList;
23
 import java.util.ArrayList;
24
 import java.util.List;
24
 import java.util.List;
25
+import java.util.Collections;
25
 
26
 
26
 /**
27
 /**
27
  * A collection of frames that represent an entire animation.
28
  * A collection of frames that represent an entire animation.
32
  * @version 1.0
33
  * @version 1.0
33
  */
34
  */
34
 
35
 
35
-public class Animation {
36
+public class Animation implements Comparable<Animation> {
36
 	List<AFrame> frames = new ArrayList<AFrame>();
37
 	List<AFrame> frames = new ArrayList<AFrame>();
37
 	private int lastFrameIndex = 0;
38
 	private int lastFrameIndex = 0;
38
 	private String name = "Animation";
39
 	private String name = "Animation";
40
+	private int order;
41
+	private static int orderIndex = 0;
42
+
43
+	/**
44
+	 * Compare this animation to another animation.
45
+	 * Compares the order in the animations list.
46
+	 * @return 0 if equal, -1 if this one comes first, 1 if last
47
+	 */
48
+	public int compareTo(Animation compareAnimation) {
49
+		if (getOrder() < compareAnimation.getOrder()) {
50
+			return -1;
51
+		} else if (getOrder() == compareAnimation.getOrder()) {
52
+			return 0;
53
+		} else {
54
+			return 1;
55
+		} 
56
+	}
57
+
58
+	/**
59
+	 * Get index of animation in list of animations.
60
+	 * @return index
61
+	 */
62
+	public int getOrder() {
63
+		return order;
64
+	}
65
+
66
+	/**
67
+	 * Set index of animation in list of animations.
68
+	 * @param newOrder new index
69
+	 */
70
+	public void setOrder(int newOrder) {
71
+		order = newOrder;
72
+	}
73
+
74
+	/**
75
+	 * Inserts animation at end of animation list.
76
+	 */
77
+	public Animation() {
78
+		order = orderIndex++;
79
+	}
39
 
80
 
40
 	/**
81
 	/**
41
 	 * Gets the name of this animation
82
 	 * Gets the name of this animation
130
 		}
171
 		}
131
 	}
172
 	}
132
 
173
 
174
+	private void sortFrameList() {
175
+		Collections.sort(frames);
176
+	}
177
+
178
+	// return true if damaged and fixed partially
179
+	private boolean checkFrameList() {
180
+		for (int i = 0; i < frames.size(); i++) {
181
+			if (frames.get(i).getOrder() != i) {
182
+				frames.get(i).setOrder(frames.size());
183
+				return true;
184
+			}
185
+		}
186
+		return false;
187
+	}
188
+
133
 	/**
189
 	/**
134
-	 * Return size of this animation, in number of frames
190
+	 * Return size of this animation, in number of frames.
191
+	 * Also fixes the order of the frame list, if needed.
135
 	 * 
192
 	 * 
136
 	 * @return number of frames
193
 	 * @return number of frames
137
 	 */
194
 	 */
138
 	public int size() {
195
 	public int size() {
196
+		while(checkFrameList()) {
197
+			sortFrameList();
198
+		}
139
 		return frames.size();
199
 		return frames.size();
140
 	}
200
 	}
141
 }
201
 }

+ 33
- 7
CubeControl/Frame.java View File

926
 	public static void main(String[] args) {
926
 	public static void main(String[] args) {
927
 		Frame f = new Frame("Cube Control");
927
 		Frame f = new Frame("Cube Control");
928
 		Led3D l = f.get3D();
928
 		Led3D l = f.get3D();
929
+		String lastCommand = null;
929
 		java.util.Scanner sc = new java.util.Scanner(System.in);
930
 		java.util.Scanner sc = new java.util.Scanner(System.in);
930
 		System.out.println("#### Cube Control Debug Console ####");
931
 		System.out.println("#### Cube Control Debug Console ####");
931
 		System.out.println("## Enter a Command ('h' for help) ##");
932
 		System.out.println("## Enter a Command ('h' for help) ##");
935
 			if (sc.hasNextLine()) {
936
 			if (sc.hasNextLine()) {
936
 				String s = sc.nextLine();
937
 				String s = sc.nextLine();
937
 
938
 
939
+				// Up arrow key
940
+				if (s.equals("\u001B[A")) {
941
+					if (lastCommand != null) {
942
+						System.out.println("Last command: " + lastCommand);
943
+						s = new String(lastCommand);
944
+					} else {
945
+						System.out.println("No last command!");
946
+					}
947
+				}
948
+
938
 				if (s.equals("p") || (s.equals("print")))
949
 				if (s.equals("p") || (s.equals("print")))
939
 					l.printTranslationData();
950
 					l.printTranslationData();
940
 
951
 
964
 				}
975
 				}
965
 
976
 
966
 				if (s.startsWith("port ")) {
977
 				if (s.startsWith("port ")) {
967
-					s = s.substring(5);
968
-					f.jComboBox1.addItem(s);
969
-					f.jComboBox1.setSelectedItem(s);
978
+					f.jComboBox1.addItem(s.substring(5));
979
+					f.jComboBox1.setSelectedItem(s.substring(5));
970
 				}
980
 				}
971
 
981
 
972
 				if (s.startsWith("send ")) {
982
 				if (s.startsWith("send ")) {
973
-					s = s.substring(5);
974
 					HelperUtility.openPort((String) f.jComboBox1
983
 					HelperUtility.openPort((String) f.jComboBox1
975
 							.getSelectedItem());
984
 							.getSelectedItem());
976
-					short[] dat = toShortArray(s);
985
+					short[] dat = toShortArray(s.substring(5));
977
 					HelperUtility.writeData(dat, dat.length);
986
 					HelperUtility.writeData(dat, dat.length);
978
 					HelperUtility.closePort();
987
 					HelperUtility.closePort();
979
 				}
988
 				}
980
 
989
 
981
 				if (s.startsWith("read ")) {
990
 				if (s.startsWith("read ")) {
982
-					s = s.substring(5);
983
-					int leng = Integer.parseInt(s);
991
+					int leng = Integer.parseInt(s.substring(5));
984
 					HelperUtility.openPort((String) f.jComboBox1
992
 					HelperUtility.openPort((String) f.jComboBox1
985
 							.getSelectedItem());
993
 							.getSelectedItem());
986
 					short[] data = HelperUtility.readData(leng);
994
 					short[] data = HelperUtility.readData(leng);
988
 					HelperUtility.closePort();
996
 					HelperUtility.closePort();
989
 				}
997
 				}
990
 
998
 
999
+				if (s.startsWith("frames ")) {
1000
+					int anim = Integer.parseInt(s.substring(7));
1001
+					if (anim >= f.worker.numOfAnimations()) {
1002
+						System.out.println("Animation does not exist! Max: " + (f.worker.numOfAnimations() - 1));
1003
+					} else {
1004
+						System.out.println("Animation: " + f.worker.getAnimationName(anim));
1005
+						for (int i = 0; i < f.worker.numOfFrames(anim); i++) {
1006
+							AFrame frame = f.worker.getAnimationList().get(anim).get(i);
1007
+							System.out.println("\tFrame " + frame.getOrder() + " (" + frame.getTime() + "): " + frame.getName());
1008
+						}
1009
+					}
1010
+				}
1011
+
991
 				if (s.equals("s") || s.equals("scan")) {
1012
 				if (s.equals("s") || s.equals("scan")) {
992
 					String[] sPorts = f.worker.getSerialPorts();
1013
 					String[] sPorts = f.worker.getSerialPorts();
993
 					f.jComboBox1.removeAllItems();
1014
 					f.jComboBox1.removeAllItems();
1015
 					System.out
1036
 					System.out
1016
 							.println("\t'reset'  / 'r'\t:\tReset rotation of cube");
1037
 							.println("\t'reset'  / 'r'\t:\tReset rotation of cube");
1017
 					System.out
1038
 					System.out
1039
+							.println("\t'anims'  / 'a'\t:\tPrint animation tree");
1040
+					System.out
1041
+							.println("\t'frames *anim*\t:\tPrint frame tree");
1042
+					System.out
1018
 							.println("\t'help'   / 'h'\t:\tShow this message");
1043
 							.println("\t'help'   / 'h'\t:\tShow this message");
1019
 					System.out
1044
 					System.out
1020
 							.println("\t'quit'   / 'q'\t:\tExit Cube Control");
1045
 							.println("\t'quit'   / 'q'\t:\tExit Cube Control");
1021
 				}
1046
 				}
1022
 
1047
 
1048
+				lastCommand = new String(s);
1023
 				System.out.print("$> ");
1049
 				System.out.print("$> ");
1024
 			}
1050
 			}
1025
 		} while (true);
1051
 		} while (true);

+ 20
- 0
CubeControl/cubeWorker.java View File

28
 import java.util.ArrayList;
28
 import java.util.ArrayList;
29
 import java.util.List;
29
 import java.util.List;
30
 import java.util.StringTokenizer;
30
 import java.util.StringTokenizer;
31
+import java.util.Collections;
31
 
32
 
32
 /**
33
 /**
33
  * This class holds all Data of the Application. Additionally it performs the
34
  * This class holds all Data of the Application. Additionally it performs the
98
 	// Misc. Methods
99
 	// Misc. Methods
99
 	// --------------------
100
 	// --------------------
100
 
101
 
102
+	private void sortAnimationList() {
103
+		Collections.sort(animations);
104
+	}
105
+
106
+	// return true if damaged and fixed partially
107
+	private boolean checkAnimationList() {
108
+		for (int i = 0; i < animations.size(); i++) {
109
+			if (animations.get(i).getOrder() != i) {
110
+				animations.get(i).setOrder(animations.size());
111
+				return true;
112
+			}
113
+		}
114
+		return false;
115
+	}
116
+
101
 	/**
117
 	/**
102
 	 * Get the number of animations in this worker.
118
 	 * Get the number of animations in this worker.
119
+	 * Also fixes the order of the animation list, if needed.
103
 	 * 
120
 	 * 
104
 	 * @return number of animations
121
 	 * @return number of animations
105
 	 */
122
 	 */
106
 	public int numOfAnimations() {
123
 	public int numOfAnimations() {
124
+		while(checkAnimationList()) {
125
+			sortAnimationList();
126
+		}
107
 		return animations.size();
127
 		return animations.size();
108
 	}
128
 	}
109
 
129
 

Loading…
Cancel
Save