MenuState.java 8.9 KB

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