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.

Animation.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 {
  35. AFrame frames[] = new AFrame[1];
  36. private String name = "Animation";
  37. /**
  38. * Create an empty frame in this new animation.
  39. */
  40. public Animation() {
  41. frames[0] = new AFrame();
  42. }
  43. /**
  44. * Gets the specified frame in this animation
  45. *
  46. * @param i Index of frame you want
  47. * @return The selected frame
  48. */
  49. public AFrame getFrame(int i) {
  50. if (i >= frames.length) {
  51. return null;
  52. } else {
  53. return frames[i];
  54. }
  55. }
  56. /**
  57. * Move an frame up.
  58. * @param i the animation you want to move
  59. */
  60. public void moveFrameUp(int i) {
  61. if (i > 0) {
  62. AFrame tmp = frames[i];
  63. frames[i] = frames[i - 1];
  64. frames[i - 1] = tmp;
  65. }
  66. }
  67. /**
  68. * Move an frame down.
  69. * @param i the animation you want to move
  70. */
  71. public void moveFrameDown(int i) {
  72. if (i < (frames.length - 1)) {
  73. AFrame tmp = frames[i];
  74. frames[i] = frames[i + 1];
  75. frames[i + 1] = tmp;
  76. }
  77. }
  78. /**
  79. * Sets the selected Frame
  80. *
  81. * @param f the frame you want to place in this animation
  82. * @param i Index of the frame you want to override
  83. */
  84. public void setFrame(AFrame f, int i) {
  85. if (i < frames.length) {
  86. frames[i] = f;
  87. } else {
  88. addFrame(f);
  89. }
  90. }
  91. /**
  92. * Add a new (empty) frame at the end
  93. */
  94. public void addFrame() {
  95. extendArray();
  96. frames[frames.length - 1] = new AFrame();
  97. }
  98. /**
  99. * Add a specified frame at the end
  100. *
  101. * @param f data for new frame
  102. */
  103. public void addFrame(AFrame f) {
  104. addFrame();
  105. frames[frames.length - 1] = f;
  106. }
  107. /**
  108. * Return size of this animation, in number of frames.
  109. *
  110. * @return number of frames
  111. */
  112. public int size() {
  113. return frames.length;
  114. }
  115. /**
  116. * Removes a frame. Subsequent frames shift to the left.
  117. *
  118. * @param i Index of frame you want to remove
  119. */
  120. public void removeFrame(int i) {
  121. shiftOver(i);
  122. shrinkArray();
  123. }
  124. /**
  125. * Gets the name of this animation
  126. *
  127. * @return name of this animation
  128. */
  129. public String getName() {
  130. return name;
  131. }
  132. /**
  133. * Sets the name of this animation
  134. *
  135. * @param s
  136. * new name
  137. */
  138. public void setName(String s) {
  139. name = s;
  140. }
  141. private void extendArray() {
  142. AFrame newArray[] = new AFrame[frames.length + 1];
  143. for (int i = 0; i < frames.length; i++) {
  144. newArray[i] = frames[i];
  145. }
  146. frames = newArray;
  147. }
  148. private void shrinkArray() {
  149. AFrame newArray[] = new AFrame[frames.length - 1];
  150. for (int i = 0; i < newArray.length; i++) {
  151. newArray[i] = frames[i];
  152. }
  153. frames = newArray;
  154. }
  155. /* private void shiftSpace(int newFree) {
  156. AFrame temp = null;
  157. for (int i = newFree; i < (frames.length - 1); i++) {
  158. // Max i: vorletztes element
  159. AFrame tmp = frames[i];
  160. frames[i] = temp;
  161. temp = frames[i + 1];
  162. frames[i + 1] = tmp;
  163. }
  164. } */
  165. private void shiftOver(int toForget) {
  166. for (int i = (toForget + 1); i < frames.length; i++) {
  167. frames[i - 1] = frames[i];
  168. }
  169. }
  170. }