Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Frame.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * Frame.java
  3. *
  4. *
  5. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  6. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  7. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  8. *
  9. * This file is part of LED-Cube.
  10. *
  11. * LED-Cube is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * LED-Cube is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. import com.sun.j3d.utils.universe.*;
  25. import javax.media.j3d.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import javax.swing.*;
  29. import javax.swing.event.*;
  30. import java.io.File;
  31. import javax.vecmath.Point2d;
  32. import javax.vecmath.Point3d;
  33. import java.util.Arrays;
  34. public class Frame extends JFrame implements ListSelectionListener, ChangeListener{
  35. private GraphicsConfiguration gConfig;
  36. private Canvas3D cubeCanvas;
  37. public Led3D ledView;
  38. private Container cp;
  39. private JButton editA = new JButton();
  40. private JButton editB = new JButton();
  41. private JButton editC = new JButton();
  42. private JButton editD = new JButton();
  43. private JButton editE = new JButton();
  44. private JButton editF = new JButton();
  45. private JButton editG = new JButton();
  46. private JButton editH = new JButton();
  47. private JButton[] editButtons = { editA, editB, editC, editD, editE, editF, editG, editH };
  48. private JButton toggleLegsButton = new JButton();
  49. private JButton toggleLegsButtonFullscreen = new JButton();
  50. private DefaultListModel frameListModel = new DefaultListModel();
  51. public JList frameList = new JList();
  52. private JScrollPane frameListScrollPane = new JScrollPane(frameList);
  53. private JButton frameUp = new JButton();
  54. private JButton frameDown = new JButton();
  55. private JButton frameAdd = new JButton();
  56. private JButton frameRemove = new JButton();
  57. private JButton frameRename = new JButton();
  58. private JList animList = new JList();
  59. private DefaultListModel animListModel = new DefaultListModel();
  60. private JScrollPane animScrollPane = new JScrollPane(animList);
  61. private JButton animUp = new JButton();
  62. private JButton animDown = new JButton();
  63. private JButton animAdd = new JButton();
  64. private JButton animRemove = new JButton();
  65. private JButton animRename = new JButton();
  66. private JTextField animPath = new JTextField();
  67. private JButton load = new JButton();
  68. private JButton save = new JButton();
  69. private JButton saveAs = new JButton();
  70. public JComboBox serialPortSelector = new JComboBox();
  71. private JButton upload = new JButton();
  72. private JButton download = new JButton();
  73. private JLabel remainingLabel = new JLabel();
  74. private JTextField frameRemaining = new JTextField();
  75. private JLabel frameLengthLabel = new JLabel();
  76. private JTextField frameLengthText = new JTextField();
  77. private JButton frameDuration = new JButton();
  78. private JButton fullScreenButton = new JButton();
  79. private JButton playAnimation = new JButton();
  80. private JButton exitButton;
  81. private JButton playAnimationFullscreen = new JButton();
  82. private JPanel previewPanel = new JPanel();
  83. private JPanel framePanel = new JPanel();
  84. private JPanel animPanel = new JPanel();
  85. private JPanel filePanel = new JPanel();
  86. private JPanel serialPanel = new JPanel();
  87. private JPanel settingsPanel = new JPanel();
  88. private JSlider durationSlider = new JSlider(1, 256 ,1); //min, max, value
  89. private JLabel logoText = new JLabel();
  90. private int lastSelectedFrame = 0;
  91. private int lastSelectedAnim = 0;
  92. public cubeWorker worker = new cubeWorker(this);
  93. private boolean fileSelected = false;
  94. private boolean showLegs = true;
  95. private Frame thisFrame;
  96. private static Frame recentFrame;
  97. private void toggleLegs(){
  98. ledView.toggleLegs();
  99. if(showLegs) {
  100. toggleLegsButton.setText("Show legs");
  101. toggleLegsButtonFullscreen.setText("Show legs");
  102. } else {
  103. toggleLegsButton.setText("Hide legs");
  104. toggleLegsButtonFullscreen.setText("Hide legs");
  105. }
  106. showLegs = !showLegs;
  107. }
  108. private int saveExitDialog() {
  109. String[] Optionen = { "Yes", "No" };
  110. int Auswahl = JOptionPane.showOptionDialog(this,
  111. "Do you want to save your changes?", "Save?",
  112. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  113. Optionen, Optionen[0]);
  114. if (Auswahl == JOptionPane.YES_OPTION) {
  115. return 1;
  116. } else {
  117. return 0;
  118. }
  119. }
  120. /**
  121. * Ask the user to enter a string via this Frame.
  122. * @param title Title of the message box
  123. * @param text Text in the message box
  124. * @return String user entered
  125. */
  126. public String askString(String title, String text) {
  127. return JOptionPane.showInputDialog(null, text, title,
  128. JOptionPane.QUESTION_MESSAGE);
  129. }
  130. /**
  131. * Show an error message to the user via this Frame.
  132. * @param s Error Message
  133. */
  134. public void errorMessage(String s) {
  135. String[] Optionen = { "OK" };
  136. JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION,
  137. JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  138. }
  139. /**
  140. * Show an error message to the user via this Frame.
  141. * @param title Title of message box
  142. * @param msg Error message.
  143. */
  144. public void errorMessage(String title, String msg) {
  145. String[] Optionen = { "OK" };
  146. JOptionPane.showOptionDialog(this, msg, title, JOptionPane.YES_OPTION,
  147. JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  148. }
  149. /**
  150. * Show an error message to the user via the most recent Frame.
  151. * @param s Error Message
  152. */
  153. public static void errorMessageStat(String s) {
  154. recentFrame.errorMessage(s);
  155. }
  156. /**
  157. * Show an error message to the user via the most recent Frame.
  158. * @param title Title of message box
  159. * @param msg Error message.
  160. */
  161. public static void errorMessageStat(String title, String msg) {
  162. recentFrame.errorMessage(title, msg);
  163. }
  164. /**
  165. * Toggle a LED in the current selected anim & frame
  166. * @param x X Coordinate
  167. * @param y Y Coord.
  168. * @param z Z Coord.
  169. */
  170. public void toggleLED(int x, int y, int z) {
  171. if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
  172. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).toggleLED(x, y, z);
  173. ledView.setData(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getData());
  174. }
  175. }
  176. private void rebuildFrameList() {
  177. // animList selection changed, update frameList
  178. frameListModel.clear();
  179. // System.out.println("Selected Animation: " + animList.getSelectedIndex());
  180. int max = worker.getAnimation(animList.getSelectedIndex()).size();
  181. for (int i = 0; i < max; i++) {
  182. frameListModel.addElement(worker.getAnimation(
  183. animList.getSelectedIndex()).getFrame(i).getName());
  184. }
  185. frameList.setModel(frameListModel);
  186. }
  187. /**
  188. * ListSelectionListener that updates Anim & Frame List
  189. * and reloads the data for the 3d view
  190. *
  191. * @param evt List that generated the event. Has to be animList or frameList
  192. */
  193. public void valueChanged(ListSelectionEvent evt) {
  194. if ((!evt.getValueIsAdjusting()) && ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
  195. // Select an animation, if not already done
  196. if (animList.getSelectedIndex() == -1) {
  197. if ((animListModel.getSize() > lastSelectedAnim) && (lastSelectedAnim != -1)) {
  198. animList.setSelectedIndex(lastSelectedAnim);
  199. } else {
  200. animList.setSelectedIndex(0);
  201. }
  202. }
  203. // Rebuild the frame list, if possible & needed
  204. if ((evt.getSource() == animList) && (animList.getSelectedIndex() != -1)) {
  205. rebuildFrameList();
  206. }
  207. // Select a frame, if not already done
  208. if (frameList.getSelectedIndex() == -1) {
  209. if ((frameListModel.getSize() > lastSelectedFrame) && (lastSelectedFrame != -1)) {
  210. frameList.setSelectedIndex(lastSelectedFrame);
  211. } else {
  212. frameList.setSelectedIndex(0);
  213. }
  214. lastSelectedFrame = frameList.getSelectedIndex();
  215. }
  216. // If possible, set all data for selected frame
  217. if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
  218. ledView.setData(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getData());
  219. frameLengthText.setText(Integer.toString(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime()));
  220. durationSlider.setValue(Integer.parseInt(frameLengthText.getText()));
  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;
  233. }
  234. }
  235. }
  236. public void stateChanged(ChangeEvent e){
  237. frameLengthText.setText(durationSlider.getValue() + "");
  238. }
  239. private void save() {
  240. if (fileSelected == false) {
  241. JFileChooser fc = new JFileChooser();
  242. int ret = fc.showSaveDialog(this);
  243. if (ret == JFileChooser.APPROVE_OPTION) {
  244. File file = fc.getSelectedFile();
  245. fileSelected = true;
  246. animPath.setText(file.getPath());
  247. worker.saveState(animPath.getText());
  248. }
  249. } else {
  250. worker.saveState(animPath.getText());
  251. }
  252. }
  253. private void showLayerEditFrame(int num) {
  254. if (animList.getSelectedIndex() == -1) {
  255. errorMessage("Please select an animation.");
  256. } else if (frameList.getSelectedIndex() == -1) {
  257. errorMessage("Please select a Frame.");
  258. } else {
  259. // Show edit frame
  260. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), num, worker, thisFrame);
  261. }
  262. }
  263. private void playAnimation() {
  264. if (animList.getSelectedIndex() == -1) {
  265. errorMessage("Please select an animation.");
  266. } else if (frameList.getSelectedIndex() == -1) {
  267. errorMessage("Please select a Frame.");
  268. } else {
  269. for (int i = 0; i < frameList.getModel().getSize(); i++){
  270. frameList.setSelectedIndex(i);
  271. long time1 = (long) worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  272. long time = (long) (((time1+1) * 1000) / 24);
  273. try {
  274. Thread.sleep(time);
  275. } catch (Exception e) {
  276. System.out.println(e);
  277. }
  278. }
  279. }
  280. }
  281. public Frame(String title) {
  282. super(title);
  283. thisFrame = this;
  284. recentFrame = this;
  285. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  286. // Build serial port selection
  287. String[] sPorts = HelperUtility.getPorts();
  288. for (int i = 0; i < sPorts.length; i++) {
  289. serialPortSelector.addItem(sPorts[i]);
  290. }
  291. // Build Animation List
  292. for (int i = 0; i < worker.size(); i++) {
  293. animListModel.addElement(worker.getAnimation(i).getName());
  294. }
  295. // Ask user to save before closing the window
  296. addWindowListener(new WindowAdapter() {
  297. public void windowClosing(WindowEvent evt) {
  298. if (worker.changedStateSinceSave()) {
  299. if (saveExitDialog() == 1) {
  300. save();
  301. } else {
  302. return;
  303. }
  304. }
  305. System.exit(0);
  306. }
  307. });
  308. // Set window size & position
  309. int frameWidth = 672;
  310. int frameHeight = 646;
  311. setSize(frameWidth, frameHeight);
  312. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  313. int x = (d.width - frameWidth) / 2;
  314. int y = (d.height - frameHeight) / 2;
  315. setLocation(x, y);
  316. cp = getContentPane();
  317. cp.setLayout(null);
  318. // ----- 3D Stuff -----
  319. // --------------------
  320. gConfig = SimpleUniverse.getPreferredConfiguration();
  321. cubeCanvas = new Canvas3D(gConfig);
  322. cubeCanvas.setBounds(18, 31, 275, 275); // 3d view
  323. ledView = new Led3D(cubeCanvas, this);
  324. cp.add(cubeCanvas);
  325. // --------------------
  326. // Add Layer edit buttons
  327. for (int i = 0; i < 8; i++) {
  328. final String nameStart = "Layer ";
  329. editButtons[i].setText(nameStart + (char)('A' + i));
  330. editButtons[i].setBounds(299, 32 + (35 * i), 102, 29);
  331. cp.add(editButtons[i]);
  332. editButtons[i].addActionListener(new ActionListener() {
  333. public void actionPerformed(ActionEvent evt) {
  334. JButton tmp = (JButton)evt.getSource();
  335. String name = tmp.getText();
  336. name = name.substring(nameStart.length());
  337. // Name now is one of A - H
  338. char c = name.charAt(0);
  339. c -= 'A';
  340. showLayerEditFrame((int)c);
  341. }
  342. });
  343. }
  344. toggleLegsButton.setBounds(299, 312, 102, 34);
  345. toggleLegsButton.setText("Hide legs");
  346. cp.add(toggleLegsButton);
  347. toggleLegsButton.addActionListener(new ActionListener(){
  348. public void actionPerformed(ActionEvent evt){
  349. toggleLegs();
  350. }
  351. });
  352. // Add Frame List
  353. frameListScrollPane.setBounds(339, 379, 187, 218);
  354. frameList.setModel(frameListModel);
  355. cp.add(frameListScrollPane);
  356. // Add Animation List
  357. animScrollPane.setBounds(18, 378, 187, 219);
  358. animList.setModel(animListModel);
  359. cp.add(animScrollPane);
  360. // Add Move Frame Up Button
  361. frameUp.setBounds(532, 378, 110, 39);
  362. frameUp.setText("Move up");
  363. cp.add(frameUp);
  364. frameUp.addActionListener(new ActionListener() {
  365. public void actionPerformed(ActionEvent evt) {
  366. int i = frameList.getSelectedIndex();
  367. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  368. Object tmp = frameListModel.get(i);
  369. frameListModel.set(i, frameListModel.get(i - 1));
  370. frameListModel.set(i - 1, tmp);
  371. frameList.setSelectedIndex(i - 1);
  372. worker.getAnimation(animList.getSelectedIndex()).moveFrameUp(i);
  373. }
  374. }
  375. });
  376. // Add Move Frame Down Button
  377. frameDown.setBounds(532, 558, 110, 39);
  378. frameDown.setText("Move down");
  379. cp.add(frameDown);
  380. frameDown.addActionListener(new ActionListener() {
  381. public void actionPerformed(ActionEvent evt) {
  382. int i = frameList.getSelectedIndex();
  383. if ((i >= 0) && (frameListModel.getSize() >= 2) && (i < (frameListModel.getSize() - 1))) {
  384. Object tmp = frameListModel.get(i);
  385. frameListModel.set(i, frameListModel.get(i + 1));
  386. frameListModel.set(i + 1, tmp);
  387. frameList.setSelectedIndex(i + 1);
  388. worker.getAnimation(animList.getSelectedIndex()).moveFrameDown(i);
  389. }
  390. }
  391. });
  392. // Add 'Frame Add' Button
  393. frameAdd.setBounds(532, 423, 110, 39);
  394. frameAdd.setText("Add");
  395. cp.add(frameAdd);
  396. frameAdd.addActionListener(new ActionListener() {
  397. public void actionPerformed(ActionEvent evt) {
  398. if (animList.getSelectedIndex() == -1) {
  399. errorMessage("Please select an animation!");
  400. } else {
  401. int n = worker.getAnimation(animList.getSelectedIndex()).size();
  402. worker.getAnimation(animList.getSelectedIndex()).addFrame();
  403. frameRemaining.setText(Integer.toString(worker.memoryRemaining()));
  404. frameListModel.add(n, worker.getAnimation(animList.getSelectedIndex()).getFrame(n).getName());
  405. frameList.setModel(frameListModel);
  406. }
  407. }
  408. });
  409. // Add button for frame removal
  410. frameRemove.setBounds(532, 468, 110, 39);
  411. frameRemove.setText("Remove");
  412. cp.add(frameRemove);
  413. frameRemove.addActionListener(new ActionListener() {
  414. public void actionPerformed(ActionEvent evt) {
  415. if (animList.getSelectedIndex() == -1) {
  416. errorMessage("Select an animation.");
  417. } else if (frameList.getSelectedIndex() == -1) {
  418. errorMessage("Select a Frame.");
  419. } else {
  420. worker.getAnimation(animList.getSelectedIndex()).removeFrame(frameList.getSelectedIndex());
  421. frameRemaining.setText(Integer.toString(worker.memoryRemaining()));
  422. frameListModel.removeElementAt(frameList.getSelectedIndex());
  423. frameList.setModel(frameListModel);
  424. }
  425. }
  426. });
  427. // Add frame rename button
  428. frameRename.setBounds(532, 513, 110, 39);
  429. frameRename.setText("Rename");
  430. cp.add(frameRename);
  431. frameRename.addActionListener(new ActionListener() {
  432. public void actionPerformed(ActionEvent evt) {
  433. int a = animList.getSelectedIndex();
  434. if (a < 0) {
  435. errorMessage("Select an animation!");
  436. return;
  437. }
  438. int f = frameList.getSelectedIndex();
  439. if (f < 0) {
  440. errorMessage("Select a Frame!");
  441. return;
  442. }
  443. String newName = askString("Rename", "Rename " + frameList.getSelectedValue() + "?");
  444. if (!newName.equals("")) {
  445. worker.getAnimation(a).getFrame(f).setName(newName);
  446. }
  447. frameListModel.set(f, worker.getAnimation(a).getFrame(f).getName());
  448. frameList.setModel(frameListModel);
  449. }
  450. });
  451. // Add Move Animation Up Button
  452. animUp.setBounds(211, 378, 110, 39);
  453. animUp.setText("Move up");
  454. cp.add(animUp);
  455. animUp.addActionListener(new ActionListener() {
  456. public void actionPerformed(ActionEvent evt) {
  457. int i = animList.getSelectedIndex();
  458. if ((i > 0) && (animListModel.getSize() >= 2)) {
  459. Object tmp = animListModel.get(i);
  460. animListModel.set(i, animListModel.get(i - 1));
  461. animListModel.set(i - 1, tmp);
  462. animList.setSelectedIndex(i - 1);
  463. worker.moveAnimationUp(i);
  464. }
  465. }
  466. });
  467. // Add Move Animation Down Button
  468. animDown.setBounds(211, 558, 110, 39);
  469. animDown.setText("Move down");
  470. cp.add(animDown);
  471. animDown.addActionListener(new ActionListener() {
  472. public void actionPerformed(ActionEvent evt) {
  473. int i = animList.getSelectedIndex();
  474. if ((i >= 0) && (animListModel.getSize() >= 2) && (i < (animListModel.getSize() - 1))) {
  475. Object tmp = animListModel.get(i);
  476. animListModel.set(i, animListModel.get(i + 1));
  477. animListModel.set(i + 1, tmp);
  478. animList.setSelectedIndex(i + 1);
  479. worker.moveAnimationDown(i);
  480. }
  481. }
  482. });
  483. // Add Rename Animation Button
  484. animRename.setBounds(211, 513, 110, 39);
  485. animRename.setText("Rename");
  486. cp.add(animRename);
  487. animRename.addActionListener(new ActionListener() {
  488. public void actionPerformed(ActionEvent evt) {
  489. int a = animList.getSelectedIndex();
  490. if (a < 0) {
  491. errorMessage("Select an animation!");
  492. return;
  493. }
  494. String newName = askString("Rename", "Rename " + animList.getSelectedValue() + "?");
  495. if (!newName.equals("")) {
  496. worker.getAnimation(a).setName(newName);
  497. }
  498. animListModel.set(a, worker.getAnimation(a).getName());
  499. animList.setModel(animListModel);
  500. }
  501. });
  502. // Add button for adding animations
  503. animAdd.setBounds(211, 423, 110, 39);
  504. animAdd.setText("Add");
  505. cp.add(animAdd);
  506. animAdd.addActionListener(new ActionListener() {
  507. public void actionPerformed(ActionEvent evt) {
  508. if (worker.addAnimation() == -1) {
  509. errorMessage("Could not add animation!");
  510. } else {
  511. animListModel.clear();
  512. for (int i = 0; i < worker.size(); i++) {
  513. animListModel.add(i, worker.getAnimation(i).getName());
  514. }
  515. animList.setModel(animListModel);
  516. }
  517. }
  518. });
  519. // Add Animation remove button
  520. animRemove.setBounds(211, 468, 110, 39);
  521. animRemove.setText("Remove");
  522. cp.add(animRemove);
  523. animRemove.addActionListener(new ActionListener() {
  524. public void actionPerformed(ActionEvent evt) {
  525. if (animList.getSelectedIndex() == -1) {
  526. errorMessage("Select an animation.");
  527. } else {
  528. worker.removeAnimation(animList.getSelectedIndex());
  529. animListModel.removeElementAt(animList.getSelectedIndex());
  530. animList.setModel(animListModel);
  531. }
  532. }
  533. });
  534. fullScreenButton.setText("Fullscreen");
  535. fullScreenButton.setBounds(18, 312, 134, 35);
  536. cp.add(fullScreenButton);
  537. fullScreenButton.addActionListener(new ActionListener() {
  538. public void actionPerformed(ActionEvent evt) {
  539. ledView.enterFullscreen();
  540. dispose();
  541. setUndecorated(true);
  542. setVisible(true);
  543. setLocation(0,0);
  544. setSize(700, 700);
  545. int w = Toolkit.getDefaultToolkit().getScreenSize().width;
  546. int h = Toolkit.getDefaultToolkit().getScreenSize().height;
  547. setSize(w - 5, h - 30);
  548. playAnimationFullscreen.setVisible(true);
  549. toggleLegsButtonFullscreen.setVisible(true);
  550. cubeCanvas.setBounds(0, 0, w - 5, h - 80);
  551. }
  552. });
  553. exitButton = new JButton("Exit Fullscreen");
  554. exitButton.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-150, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  555. cp.add(exitButton);
  556. exitButton.addActionListener(new ActionListener() {
  557. public void actionPerformed(ActionEvent evt) {
  558. dispose();
  559. setUndecorated(false);
  560. setVisible(true);
  561. playAnimationFullscreen.setVisible(false);
  562. toggleLegsButtonFullscreen.setVisible(false);
  563. setLocation(0,0);
  564. setSize(672, 656);
  565. ledView.leaveFullscreen();
  566. cubeCanvas.setBounds(18, 31, 275, 275);
  567. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  568. int x = (d.width - getSize().width) / 2;
  569. int y = (d.height - getSize().height) / 2;
  570. setLocation(x, y);
  571. }
  572. });
  573. playAnimation.setText("Play");
  574. playAnimation.setBounds(159, 312, 134, 35);
  575. cp.add(playAnimation);
  576. playAnimation.addActionListener(new ActionListener() {
  577. public void actionPerformed(ActionEvent evt){
  578. playAnimation();
  579. }
  580. });
  581. playAnimationFullscreen.setText("Play");
  582. playAnimationFullscreen.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-310, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  583. playAnimationFullscreen.setVisible(false);
  584. cp.add(playAnimationFullscreen);
  585. playAnimationFullscreen.addActionListener(new ActionListener() {
  586. public void actionPerformed(ActionEvent evt){
  587. playAnimation();
  588. }
  589. });
  590. toggleLegsButtonFullscreen.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-470, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  591. toggleLegsButtonFullscreen.setText("Hide legs");
  592. toggleLegsButtonFullscreen.setVisible(false);
  593. cp.add(toggleLegsButtonFullscreen);
  594. toggleLegsButtonFullscreen.addActionListener(new ActionListener(){
  595. public void actionPerformed(ActionEvent evt){
  596. toggleLegs();
  597. }
  598. });
  599. frameDuration.setBounds(462, 129, 65, 20);
  600. frameDuration.setText("Save");
  601. cp.add(frameDuration);
  602. frameDuration.addActionListener(new ActionListener() {
  603. public void actionPerformed(ActionEvent evt) {
  604. if (frameLengthText.getText().equals("0")) {
  605. errorMessage("0 is not a valid value!");
  606. frameLengthText.setText("1");
  607. } else if (Integer.parseInt(frameLengthText.getText()) > 256) {
  608. errorMessage("Value too high. Max: 256");
  609. frameLengthText.setText("256");
  610. return;
  611. } else {
  612. if (animList.getSelectedIndex() == -1) {
  613. errorMessage("Please select an animation!");
  614. return;
  615. }
  616. if (frameList.getSelectedIndex() == -1) {
  617. errorMessage("Please select a Frame!");
  618. return;
  619. }
  620. durationSlider.setValue(Integer.parseInt(frameLengthText.getText()));
  621. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).setTime((short)(Integer.parseInt(frameLengthText.getText())));
  622. }
  623. }
  624. });
  625. animPath.setBounds(417, 281, 228, 20);
  626. animPath.setEditable(false);
  627. animPath.setText("Select an animation file...");
  628. cp.add(animPath);
  629. load.setBounds(417, 307, 72, 39);
  630. load.setText("Load");
  631. cp.add(load);
  632. load.addActionListener(new ActionListener() {
  633. public void actionPerformed(ActionEvent evt) {
  634. JFileChooser fc = new JFileChooser();
  635. int ret = fc.showOpenDialog(thisFrame);
  636. if (ret == JFileChooser.APPROVE_OPTION) {
  637. File file = fc.getSelectedFile();
  638. if (fileSelected == false) {
  639. fileSelected = true;
  640. }
  641. animPath.setText(file.getPath());
  642. worker.loadState(animPath.getText());
  643. animListModel.clear();
  644. for (int i = 0; i < worker.size(); i++) {
  645. animListModel.addElement(worker.getAnimation(i).getName());
  646. }
  647. animList.setModel(animListModel);
  648. animList.setSelectedIndex(0);
  649. rebuildFrameList();
  650. }
  651. }
  652. });
  653. save.setBounds(491, 307, 72, 39);
  654. save.setText("Save");
  655. cp.add(save);
  656. save.addActionListener(new ActionListener() {
  657. public void actionPerformed(ActionEvent evt) {
  658. if (fileSelected == false) {
  659. JFileChooser fc = new JFileChooser();
  660. int ret = fc.showSaveDialog(thisFrame);
  661. if (ret == JFileChooser.APPROVE_OPTION) {
  662. File file = fc.getSelectedFile();
  663. fileSelected = true;
  664. animPath.setText(file.getPath());
  665. worker.saveState(animPath.getText());
  666. }
  667. } else {
  668. worker.saveState(animPath.getText());
  669. }
  670. }
  671. });
  672. saveAs.setBounds(565, 307, 82, 39);
  673. saveAs.setText("Save As");
  674. cp.add(saveAs);
  675. saveAs.addActionListener(new ActionListener() {
  676. public void actionPerformed(ActionEvent evt) {
  677. JFileChooser fc;
  678. if (fileSelected == true) {
  679. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  680. } else {
  681. fc = new JFileChooser();
  682. }
  683. int ret = fc.showSaveDialog(thisFrame);
  684. if (ret == JFileChooser.APPROVE_OPTION) {
  685. File file = fc.getSelectedFile();
  686. if (fileSelected == false) {
  687. fileSelected = true;
  688. }
  689. animPath.setText(file.getPath());
  690. worker.saveState(animPath.getText());
  691. }
  692. }
  693. });
  694. serialPortSelector.setBounds(417, 182, 228, 25);
  695. cp.add(serialPortSelector);
  696. upload.setBounds(417, 212, 111, 39);
  697. upload.setText("Upload");
  698. cp.add(upload);
  699. upload.addActionListener(new ActionListener() {
  700. public void actionPerformed(ActionEvent evt) {
  701. // Error messages are generated by worker
  702. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  703. worker.cubeSendState((String) serialPortSelector.getSelectedItem());
  704. }
  705. }
  706. });
  707. download.setBounds(534, 212, 111, 39);
  708. download.setText("Download");
  709. cp.add(download);
  710. download.addActionListener(new ActionListener() {
  711. public void actionPerformed(ActionEvent evt) {
  712. // Error messages are generated by worker
  713. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  714. worker.cubeGetState((String) serialPortSelector.getSelectedItem());
  715. }
  716. }
  717. });
  718. frameLengthLabel.setBounds(420, 97, 130, 13);
  719. frameLengthLabel.setText("Time (1/24 sec)");
  720. cp.add(frameLengthLabel);
  721. frameLengthText.setBounds(419, 129, 40, 20);
  722. cp.add(frameLengthText);
  723. remainingLabel.setBounds(530, 113, 90, 13);
  724. remainingLabel.setText("Remaining:");
  725. cp.add(remainingLabel);
  726. durationSlider.setBounds(417, 108, 115, 23);
  727. durationSlider.addChangeListener(this);
  728. cp.add(durationSlider);
  729. frameRemaining.setBounds(530, 129, 49, 20);
  730. frameRemaining.setEditable(false);
  731. frameRemaining.setText(String.valueOf(worker.memoryRemaining()));
  732. cp.add(frameRemaining);
  733. previewPanel.setBounds(8, 12, 399, 342);
  734. previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
  735. cp.add(previewPanel);
  736. framePanel.setBounds(331, 360, 321, 246);
  737. framePanel.setBorder(BorderFactory.createTitledBorder("Frame"));
  738. cp.add(framePanel);
  739. animPanel.setBounds(8, 360, 321, 246);
  740. animPanel.setBorder(BorderFactory.createTitledBorder("Animation"));
  741. cp.add(animPanel);
  742. filePanel.setBounds(409, 262, 243, 92);
  743. filePanel.setBorder(BorderFactory.createTitledBorder("Animation File"));
  744. cp.add(filePanel);
  745. serialPanel.setBounds(409, 160, 243, 99);
  746. serialPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  747. cp.add(serialPanel);
  748. settingsPanel.setBounds(409, 75, 243, 82);
  749. settingsPanel.setBorder(BorderFactory.createTitledBorder("Frame duration"));
  750. cp.add(settingsPanel);
  751. logoText.setText("CubeControl");
  752. logoText.setBounds(415, 10, 243, 65);
  753. logoText.setFont(new Font("Default", Font.BOLD, 38));
  754. cp.add(logoText);
  755. // Ende Komponenten
  756. animList.addListSelectionListener(this);
  757. frameList.addListSelectionListener(this);
  758. setResizable(false);
  759. setVisible(true);
  760. valueChanged(new ListSelectionEvent(animList, 0, 0, false));
  761. }
  762. public Led3D get3D() {
  763. return ledView;
  764. }
  765. public static void main(String[] args) {
  766. Frame f = new Frame("Cube Control");
  767. Led3D l = f.get3D();
  768. String lastCommand = null;
  769. java.util.Scanner sc = new java.util.Scanner(System.in);
  770. System.out.println("#### Cube Control Debug Console ####");
  771. System.out.println("## Enter a Command ('h' for help) ##");
  772. System.out.print("$> ");
  773. do {
  774. if (sc.hasNextLine()) {
  775. String s = sc.nextLine();
  776. // Up arrow key
  777. if (s.equals("\u001B[A")) {
  778. if (lastCommand != null) {
  779. System.out.println("Last command: " + lastCommand);
  780. s = new String(lastCommand);
  781. } else {
  782. System.out.println("No last command!");
  783. }
  784. }
  785. if (s.equals("p") || (s.equals("print")))
  786. l.printTranslationData();
  787. if (s.equals("q") || s.equals("quit"))
  788. System.exit(0);
  789. if (s.equals("r") || s.equals("reset"))
  790. l.resetView();
  791. if (s.equals("l"))
  792. f.toggleLegs();
  793. if (s.equals("on") || s.equals("1")) {
  794. short[] d = new short[64];
  795. for (int i = 0; i < d.length; i++) {
  796. d[i] = 0xFF;
  797. }
  798. l.setData(d);
  799. System.out.println("All LEDs on now...");
  800. }
  801. if (s.equals("off") || s.equals("0")) {
  802. short[] d = new short[64];
  803. for (int i = 0; i < d.length; i++) {
  804. d[i] = 0x00;
  805. }
  806. l.setData(d);
  807. System.out.println("All LEDs off now...");
  808. }
  809. if (s.startsWith("port ")) {
  810. f.serialPortSelector.addItem(s.substring(5));
  811. f.serialPortSelector.setSelectedItem(s.substring(5));
  812. }
  813. if (s.startsWith("send ")) {
  814. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  815. short[] dat = toShortArray(s.substring(5));
  816. HelperUtility.writeData(dat, dat.length);
  817. HelperUtility.closePort();
  818. }
  819. if (s.startsWith("read ")) {
  820. int leng = Integer.parseInt(s.substring(5));
  821. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  822. short[] data = HelperUtility.readData(leng);
  823. System.out.println(shortToString(data));
  824. HelperUtility.closePort();
  825. }
  826. if (s.startsWith("frames ")) {
  827. int anim = Integer.parseInt(s.substring(7));
  828. if (anim >= f.worker.size()) {
  829. System.out.println("Animation does not exist! Max: " + (f.worker.size() - 1));
  830. } else {
  831. System.out.println("Animation: " + f.worker.getAnimation(anim).getName());
  832. for (int i = 0; i < f.worker.getAnimation(anim).size(); i++) {
  833. AFrame frame = f.worker.getAnimation(anim).getFrame(i);
  834. System.out.println("\tFrame (" + frame.getTime() + "): " + frame.getName());
  835. }
  836. }
  837. }
  838. if (s.equals("a") || s.equals("anims")) {
  839. for (int i = 0; i < f.worker.size(); i++) {
  840. Animation anim = f.worker.getAnimation(i);
  841. System.out.println("\tAnimation: " + anim.getName());
  842. }
  843. }
  844. if (s.equals("s") || s.equals("scan")) {
  845. String[] sPorts = HelperUtility.getPorts();
  846. f.serialPortSelector.removeAllItems();
  847. for (int i = 0; i < sPorts.length; i++) {
  848. System.out.println("Port " + i + ": " + sPorts[i]);
  849. f.serialPortSelector.addItem(sPorts[i]);
  850. }
  851. }
  852. if (s.equals("h") || (s.equals("help"))) {
  853. System.out.println("Commands:");
  854. System.out.println("\t'port *name*'\t:\tSet serial port to this");
  855. System.out.println("\t'send *datas*'\t:\tSend data to serial port");
  856. System.out.println("\t'read *leng*'\t:\tRead data from serial port");
  857. System.out.println("\t'scan' / 's'\t:\tScan for serial ports");
  858. System.out.println("\t'on' / '1'\t:\tToggle all LEDs on");
  859. System.out.println("\t'off'/ '0'\t:\tToggle all LEDs off");
  860. System.out.println("\t'print'/ 'p'\t:\tPrint 3D Translation Matrix Data");
  861. System.out.println("\t'reset'/ 'r'\t:\tReset rotation of cube");
  862. System.out.println("\t'anims'/ 'a'\t:\tPrint animation tree");
  863. System.out.println("\t'frames *anim*\t:\tPrint frame tree");
  864. System.out.println("\t'help' / 'h'\t:\tShow this message");
  865. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  866. }
  867. lastCommand = new String(s);
  868. System.out.print("$> ");
  869. }
  870. } while (true);
  871. }
  872. private static short[] toShortArray(String s) {
  873. char[] d = s.toCharArray();
  874. System.out.println("Length: " + d.length);
  875. short[] r = new short[d.length];
  876. for (int i = 0; i < d.length; i++) {
  877. r[i] = (short) d[i];
  878. }
  879. return r;
  880. }
  881. private static String shortToString(short[] d) {
  882. String s = "";
  883. for (int i = 0; i < d.length; i++) {
  884. s += String.valueOf((char) d[i]);
  885. }
  886. return s;
  887. }
  888. }