Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Animation.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Animation.java
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Collections;
  26. /**
  27. * A collection of frames that represent an entire animation.
  28. *
  29. * @author Thomas Buck
  30. * @author Max Nuding
  31. * @author Felix Bäder
  32. * @version 1.0
  33. */
  34. public class Animation implements Comparable<Animation> {
  35. List<AFrame> frames = new ArrayList<AFrame>();
  36. private int lastFrameIndex = 0;
  37. private String name = "Animation";
  38. private int order;
  39. private static int orderIndex = 0;
  40. /**
  41. * Compare this animation to another animation.
  42. * Compares the order in the animations list.
  43. * @return 0 if equal, -1 if this one comes first, 1 if last
  44. */
  45. public int compareTo(Animation compareAnimation) {
  46. if (getOrder() < compareAnimation.getOrder()) {
  47. return -1;
  48. } else if (getOrder() == compareAnimation.getOrder()) {
  49. return 0;
  50. } else {
  51. return 1;
  52. }
  53. }
  54. /**
  55. * Get index of animation in list of animations.
  56. * @return index
  57. */
  58. public int getOrder() {
  59. return order;
  60. }
  61. /**
  62. * Set index of animation in list of animations.
  63. * @param newOrder new index
  64. */
  65. public void setOrder(int newOrder) {
  66. order = newOrder;
  67. }
  68. /**
  69. * Inserts animation at end of animation list.
  70. */
  71. public Animation() {
  72. order = orderIndex++;
  73. }
  74. /**
  75. * Gets the name of this animation
  76. *
  77. * @return name of this animation
  78. */
  79. public String getName() {
  80. return name;
  81. }
  82. /**
  83. * Sets the name of this animation
  84. *
  85. * @param s
  86. * new name
  87. */
  88. public void setName(String s) {
  89. name = s;
  90. }
  91. /**
  92. * Gets the specified frame in this animation
  93. *
  94. * @param i Index of frame you want
  95. * @return The selected frame
  96. */
  97. public AFrame get(int i) {
  98. try {
  99. return frames.get(i);
  100. } catch (IndexOutOfBoundsException e) {
  101. System.out.println(e.toString());
  102. return null;
  103. }
  104. }
  105. /**
  106. * Sets the selected Frame
  107. *
  108. * @param f the frame you want to place in this animation
  109. * @param i Index of the frame you want to override
  110. */
  111. public void set(AFrame f, int i) {
  112. if (lastFrameIndex <= i) {
  113. try {
  114. frames.set(i, f);
  115. } catch (IndexOutOfBoundsException e) {
  116. System.out.println(e.toString());
  117. }
  118. }
  119. }
  120. /**
  121. * Removes a frame. Subsequent frames shift to the left.
  122. *
  123. * @param i Index of frame you want to remove
  124. */
  125. public void remove(int i) {
  126. try {
  127. frames.remove(i);
  128. } catch (IndexOutOfBoundsException e) {
  129. System.out.println(e.toString());
  130. }
  131. }
  132. /**
  133. * Add a new (empty) frame at the specified position
  134. *
  135. * @param i Index you want to place the new frame in
  136. * @see Animation#size size()
  137. */
  138. public void add(int i) {
  139. try {
  140. frames.add(i, new AFrame());
  141. lastFrameIndex++;
  142. } catch (IndexOutOfBoundsException e) {
  143. System.out.println(e.toString());
  144. }
  145. }
  146. /**
  147. * Add a specified frame at the specified position
  148. *
  149. * @param i Index for new frame
  150. * @param f data for new frame
  151. */
  152. public void add(int i, AFrame f) {
  153. try {
  154. frames.add(i, f);
  155. lastFrameIndex++;
  156. } catch (IndexOutOfBoundsException e) {
  157. System.out.println(e.toString());
  158. }
  159. }
  160. private void sortFrameList() {
  161. Collections.sort(frames);
  162. }
  163. // return true if damaged and fixed partially
  164. private boolean checkFrameList() {
  165. for (int i = 0; i < frames.size(); i++) {
  166. if (frames.get(i).getOrder() != i) {
  167. frames.get(i).setOrder(frames.size());
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * Return size of this animation, in number of frames.
  175. * Also fixes the order of the frame list, if needed.
  176. *
  177. * @return number of frames
  178. */
  179. public int size() {
  180. while(checkFrameList()) {
  181. sortFrameList();
  182. }
  183. return frames.size();
  184. }
  185. }