MenuState.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package model.gameState;
  2. import image.Images;
  3. import image.Images.ImageType;
  4. import java.awt.AlphaComposite;
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.GradientPaint;
  8. import java.awt.Graphics2D;
  9. import java.awt.Polygon;
  10. import java.awt.RenderingHints;
  11. import java.awt.Transparency;
  12. import java.awt.image.BufferedImage;
  13. import java.awt.image.VolatileImage;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import model.GameModel;
  17. import model.SongHandler;
  18. import model.objects.DifficultyButton;
  19. import model.objects.MenuButton;
  20. import audio.Song;
  21. import audio.SongInstance;
  22. import audio.sorting.SortALPHA;
  23. import audio.sorting.SortPLAYED;
  24. import control.GameStateManager;
  25. import control.button.ButtonEvent;
  26. import control.button.ButtonHandler;
  27. import control.joystick.Joystick;
  28. import control.joystick.JoystickEvent;
  29. public class MenuState extends GameState {
  30. private ArrayList<MenuButton> buttons;
  31. private ArrayList<DifficultyButton> buttons2;
  32. private int selected, oldselected;
  33. private List<Song> songs;
  34. private int animationcounter;
  35. private boolean subscreen, startanimation;
  36. private VolatileImage mainScreenBackground, subScreenBackground, subScreenForeground;
  37. int yPosDiffButton = 900;
  38. private int difSelect=0;
  39. Font textFont2 = new Font("OCR A Extended", Font.BOLD, 50);
  40. BufferedImage aanwijzers = Images.getImage(ImageType.aanwijzers);
  41. int index = 0;
  42. public MenuState(GameStateManager gsm, SongHandler sh) {
  43. super(gsm, sh);
  44. buttons = new ArrayList<MenuButton>();
  45. buttons2 = new ArrayList<DifficultyButton>();
  46. this.songs = sh.getSongs();
  47. startanimation = true;
  48. subscreen = false;
  49. buttons.add(new MenuButton(-600, 50,1.7, 0, Color.green, selectedToSong(selected-2) ));
  50. buttons.add(new MenuButton(-600, 150, 1.7, 10, Color.BLUE, selectedToSong(selected-1)));
  51. buttons.add(new MenuButton(-600, 250, 1.7, 20, Color.red, selectedToSong(selected)));
  52. buttons.add(new MenuButton(-600, 350, 1.7, 30, Color.yellow,selectedToSong(selected+1)));
  53. buttons.add(new MenuButton(-600, 450, 1.7, 30, Color.WHITE,selectedToSong(selected+2)));
  54. buttons.get(2).setSelected(true);
  55. generateMainScreenBackground();
  56. generateSubScreenBackground();
  57. }
  58. @Override
  59. public void init() {
  60. ButtonHandler.getButton(1).setColor(GameModel.colors[0]);
  61. ButtonHandler.getButton(2).setColor(GameModel.colors[2]);
  62. ButtonHandler.getButton(5).setColor(GameModel.colors[1]);
  63. ButtonHandler.getButton(6).setColor(GameModel.colors[4]);
  64. }
  65. @Override
  66. public void update(float factor) {
  67. if(startanimation){
  68. buttonInAnimation(animationcounter);
  69. if(animationcounter == 5){
  70. startanimation = false;
  71. }
  72. }else if(subscreen){
  73. nextScreen();
  74. }else{
  75. previousScreen();
  76. }
  77. for(MenuButton b:buttons){
  78. b.update();
  79. }
  80. if(selected != oldselected){
  81. for(int i = 0; i < buttons.size(); i++){
  82. buttons.get(i).setSong(selectedToSong(selected+(i-2)));
  83. }
  84. oldselected = selected;
  85. buttons2.clear();
  86. int instanceNr = 0;
  87. for(int i = sh.getCurrentSong().getSongs().size(); i>0; i--){
  88. if(sh.getCurrentSong().getSongs().size() == 0)
  89. continue;
  90. SongInstance si = sh.getCurrentSong().getSongs().get(i-1);
  91. buttons2.add(new DifficultyButton(yPosDiffButton-instanceNr,si.getDifficulty(), GameModel.colors[i-1]));
  92. instanceNr += 100;
  93. }
  94. }
  95. index++;
  96. }
  97. @Override
  98. public void draw(Graphics2D g2) {
  99. if(!subscreen) {
  100. g2.drawImage(mainScreenBackground, 0, 0, 1280,1024,null);
  101. for(MenuButton b:buttons){
  102. b.draw(g2);
  103. }
  104. }
  105. if(subscreen) {
  106. g2.drawImage(subScreenBackground,0,0,1280,1024,null);
  107. g2.drawImage(subScreenForeground,0,0,1280,1024,null);
  108. index%=25;
  109. int y = (index/5)*75;
  110. int x = (index%5)*75;
  111. g2.drawImage(aanwijzers.getSubimage(x, y, 75, 75), 825,900 - difSelect*100,75,75,null);
  112. }
  113. }
  114. @Override
  115. public void buttonPressed(ButtonEvent e) {
  116. if(subscreen){ //Screen for Song details
  117. if(e.getButton().getButtonID() == 2){
  118. subscreen = false;
  119. }
  120. if(e.getButton().getButtonID() == 1){
  121. sh.close();
  122. gsm.setState(control.GameStateManager.State.PLAY_STATE);
  123. }
  124. }else{ //Screen for selecting song
  125. if(e.getButton().getButtonID() == 1){
  126. subscreen = true;
  127. generateSubScreenForeground();
  128. }else if(e.getButton().getButtonID() == 2){
  129. subscreen = false;
  130. }else if(e.getButton().getButtonID() == 5){
  131. sh.sort(new SortALPHA());
  132. oldselected = 1;
  133. selected = 0;
  134. }else if(e.getButton().getButtonID() == 6){
  135. sh.sort(new SortPLAYED());
  136. oldselected = 1;
  137. selected = 0;
  138. }
  139. }
  140. }
  141. @Override
  142. public void buttonReleased(ButtonEvent e) {
  143. }
  144. @Override
  145. public void onJoystickMoved(JoystickEvent e) {
  146. if(subscreen){
  147. if(e.getJoystick().getPos() == Joystick.Position.DOWN){
  148. difSelect--;
  149. if(difSelect < 0){
  150. difSelect += buttons2.size();
  151. }
  152. }else if(e.getJoystick().getPos() == Joystick.Position.UP){
  153. difSelect++;
  154. if(difSelect > buttons2.size()-1){
  155. difSelect = 0;
  156. }
  157. }
  158. sh.set(sh.getCurrentSong().getSongs().get(difSelect));
  159. generateSubScreenForeground();
  160. }else{ //Screen for selecting song
  161. if(e.getJoystick().getPos() == Joystick.Position.DOWN){
  162. selected++;
  163. }else if(e.getJoystick().getPos() == Joystick.Position.UP){
  164. selected--;
  165. }
  166. sh.set(songs.indexOf(selectedToSong(selected)));
  167. sh.play();
  168. }
  169. }
  170. public void generateMainScreenBackground(){
  171. mainScreenBackground = Images.initVolatileImage(1280, 1024, Transparency.OPAQUE);
  172. Graphics2D g2 = mainScreenBackground.createGraphics();
  173. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  174. g2.setRenderingHints(rh);
  175. g2.setFont(textFont2);
  176. g2.setPaint(new GradientPaint(0, 0, new Color(0,0,1, 0.6f),1280,1024 ,new Color(0,0,1, 0.2f)));
  177. g2.fillRect(0, 0, 1280, 1024);
  178. g2.setPaint(new GradientPaint(0, 0, new Color(1,1,0, 0.6f),1280,1024 ,new Color(0,0,1, 0.2f)));
  179. Polygon triangle2 = new Polygon();
  180. triangle2.addPoint(0, 0);
  181. triangle2.addPoint(0, 1024/4+50);
  182. triangle2.addPoint(1280/2+50, 0);
  183. g2.fillPolygon(triangle2);
  184. g2.setColor(Color.ORANGE);
  185. Polygon triangle = new Polygon();
  186. triangle.addPoint(0, 0);
  187. triangle.addPoint(0, 1024/4);
  188. triangle.addPoint(1280/2, 0);
  189. g2.fillPolygon(triangle);
  190. g2.setColor(Color.BLACK);
  191. g2.drawString("Main Menu", 32, 60);
  192. g2.dispose();
  193. mainScreenBackground.createGraphics();
  194. }
  195. public void generateSubScreenBackground(){
  196. subScreenBackground = Images.initVolatileImage(1280, 1024, Transparency.OPAQUE);
  197. Graphics2D g2 = subScreenBackground.createGraphics();
  198. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  199. g2.setRenderingHints(rh);
  200. g2.setColor(Color.BLACK);
  201. GradientPaint gp3 = new GradientPaint(640, 1024/8,Color.BLUE,640,1024 ,Color.WHITE);
  202. g2.setPaint(gp3);
  203. g2.fillRect(0, 0, 1280, 1024);
  204. GradientPaint gp4 = new GradientPaint(0, 0,new Color(1,1,0,0.6f),1280,1024,new Color(1,1,1,0.2f));
  205. g2.setPaint(gp4);
  206. g2.fillRect(0,128,1280,25);
  207. g2.setColor(Color.ORANGE);
  208. g2.fillRect(0, 0, 1280, 1024/8);
  209. g2.setColor(Color.BLACK);
  210. g2.dispose();
  211. subScreenBackground.createGraphics();
  212. }
  213. public void generateSubScreenForeground(){
  214. subScreenForeground = Images.initVolatileImage(1280, 1024, Transparency.TRANSLUCENT);
  215. Graphics2D g2 = subScreenForeground.createGraphics();
  216. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  217. g2.setComposite(AlphaComposite.DstOut);
  218. g2.fillRect(0, 0, subScreenForeground.getWidth(), subScreenForeground.getHeight());
  219. g2.setComposite(AlphaComposite.SrcOver);
  220. g2.setRenderingHints(rh);
  221. g2.setColor(Color.BLACK);
  222. g2.setFont(textFont2);
  223. g2.drawString(selectedToSong(selected).getTitle(), 30, 60);
  224. g2.setColor(Color.WHITE);
  225. g2.drawString("Overall Highscore: " + "", 30, 200);
  226. g2.drawString("Daily Highscore: " + "", 30, 300);
  227. g2.drawString("Beats per Minute: " + selectedToSong(selected).getBPM(), 30, 400);
  228. for(DifficultyButton b : buttons2){
  229. b.draw(g2);
  230. }
  231. g2.dispose();
  232. subScreenForeground.createGraphics();
  233. }
  234. public void buttonInAnimation(int button){
  235. if(button >= 0 && button < buttons.size() ){
  236. buttons.get(button).setX(buttons.get(button).getX()+40);
  237. if(buttons.get(button).getX() >= 320){
  238. animationcounter++;
  239. }
  240. if(buttons.get(button).getX() >= -200){
  241. buttonInAnimation(button+1);
  242. }
  243. }
  244. }
  245. public void nextScreen(){
  246. if(buttons.get(0).getX() > -700){
  247. for(MenuButton b:buttons){
  248. b.setX(b.getX()-100);
  249. }
  250. }
  251. }
  252. public void previousScreen(){
  253. if(buttons.get(0).getX() < 300){
  254. for(MenuButton b:buttons){
  255. b.setX(b.getX()+100);
  256. }
  257. }
  258. }
  259. public Song selectedToSong(int s){
  260. s %= songs.size();
  261. if(s < 0){
  262. return songs.get(songs.size()+s);
  263. }else{
  264. return songs.get(s);
  265. }
  266. }
  267. }