Added main menu for selecting songs

This commit is contained in:
jancoow
2015-05-31 21:05:32 +02:00
parent 39e521421a
commit b99c4aa67d
2 changed files with 168 additions and 49 deletions
+39 -49
View File
@@ -1,79 +1,50 @@
package model.gameState;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.util.ArrayList;
import model.objects.MenuButton;
import control.GameStateManager;
import control.button.Button;
import control.button.ButtonEvent;
import control.joystick.Joystick;
import control.joystick.JoystickEvent;
public class MenuState extends GameState {
public Polygon triangle;
private ArrayList<MenuButton> buttons;
private int selected;
int page;
int x,y;
public MenuState(GameStateManager gsm) {
public MenuState(GameStateManager gsm) {
super(gsm);
buttons = new ArrayList<MenuButton>();
buttons.add(new MenuButton(330,50,1.7,"Genre", 0, Color.green));
buttons.add(new MenuButton(330, 150, 1.7, "Most played", 10, new Color(0,0,255)));
buttons.add(new MenuButton(330, 250, 1.7, "Best played", 20, Color.red));
buttons.add(new MenuButton(330, 350, 1.7, "Last played", 30, Color.yellow));
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public void update() {
for(MenuButton b:buttons){
b.update();
}
}
@Override
public void draw(Graphics2D g2) {
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, 1280, 1024);
g2.setColor(Color.ORANGE);
triangle = new Polygon();
triangle.addPoint(0, 0);
triangle.addPoint(0, 1024/4);
triangle.addPoint(1280/2, 0);
g2.fillPolygon(triangle);
for(int i = 1; i <= 4; i++){
g2.fillRect(780, 1124 - 240*i, 500, 100);
}
Font textFont = new Font("OCR A Extended", Font.BOLD, 60);
g2.setFont(textFont);
g2.setColor(Color.BLACK);
if(page == 0){
g2.drawString("Main Menu", 50, 50);
}
if(page == 1){
g2.drawString("Most Played", 50, 50);
}
if(page == 2){
g2.drawString("Genre", 50, 50);
}
for(MenuButton b:buttons){
b.draw(g2);
}
}
@Override
public void buttonPressed(ButtonEvent e) {
switch(e.getButton().getButtonID()){
case 0:
gsm.next();
break;
}
}
@Override
public void buttonReleased(ButtonEvent e) {
@@ -81,8 +52,27 @@ public class MenuState extends GameState {
}
@Override
public void onJoystickMoved(JoystickEvent e) {
// TODO Auto-generated method stub
if(e.getJoystick().getPos() == Joystick.Position.DOWN){
selected++;
selected %= buttons.size();
for(int i = 0; i < buttons.size(); i++){
if(selected == i){
buttons.get(i).setSelected(true);
}else{
buttons.get(i).setSelected(false);
}
}
}else if(e.getJoystick().getPos() == Joystick.Position.UP){
selected--;
if(selected < 0) selected = buttons.size()-1;
for(int i = 0; i < buttons.size(); i++){
if(selected == i){
buttons.get(i).setSelected(true);
}else{
buttons.get(i).setSelected(false);
}
}
}
}
}
+129
View File
@@ -0,0 +1,129 @@
package model.objects;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.util.ArrayList;
public class MenuButton {
private ArrayList<GeneralPath> buttonparts;
private GeneralPath border, line;
private ArrayList<Color> colors;
private Paint gradient;
private int x, y, rounding;
private double scalefactor;
private String text;
private boolean selected;
private int fadecounter;
public MenuButton(int x, int y, double scale, String text, int rounding, Color color){
this.x = x;
this.y = y;
this.scalefactor = scale;
this.text = text;
this.rounding = rounding;
calculateButton();
line = arrayToGeneralpath(new int[][]{{-11,55},{-40,60},{-38,78},{-358,170-rounding*3},{-358,174-rounding*3},{-37,87},{-35,102},{-5,95}});
gradient = new LinearGradientPaint(
new Point2D.Double((-358+x)*scalefactor,(167+y)*scalefactor),
new Point2D.Double((-7+x)*scalefactor,(70+y)*scalefactor),
new float[]{0.0f, 1.0f},
new Color[]{new Color(color.getRed(), color.getGreen(), color.getBlue(), 10), color});
colors = new ArrayList<Color>();
// colors.add(new Color(47,153,21,255));
// colors.add(new Color(36,120,16,255));
// colors.add(new Color(48,154,22,255));
// colors.add(new Color(77,254,41,255));
// colors.add(new Color(61,201,32,255));
// colors.add(new Color(77,254,41,255));
colors.add(color.darker().darker());
colors.add(color.darker().darker().darker());
colors.add(color.darker().darker());
colors.add(color);
colors.add(color.darker());
colors.add(color);
}
public void calculateButton(){
buttonparts = new ArrayList<GeneralPath>();
buttonparts.add(arrayToGeneralpath(new int[][]{{449, 1}, {449, 68},{113, 103}, {106, 35}})); //background right
buttonparts.add(arrayToGeneralpath(new int[][]{{113, 103}, {106, 35},{70, 12+rounding}, {77, 88+rounding}})); //background middle
buttonparts.add(arrayToGeneralpath(new int[][]{{70, 12+rounding}, {77, 88+rounding},{4, 104+(int)(rounding*0.5)}, {-4, 32+(int)(rounding*0.5)}})); //background left
buttonparts.add(arrayToGeneralpath(new int[][]{{449, 15}, {449, 54},{118, 88}, {114, 48}})); //foreground right
buttonparts.add(arrayToGeneralpath(new int[][]{{118, 88}, {114, 48},{78, 25+rounding}, {82, 73+rounding}})); //foreground middle
buttonparts.add(arrayToGeneralpath(new int[][]{{78, 25+rounding}, {82, 73+rounding},{15, 88+(int)(rounding*0.5)}, {10, 43+(int)(rounding*0.5)}})); //foreground left
border = arrayToGeneralpath(new int[][]{{449,15}, {114,48},{78,25+rounding},{10,43+(int)(rounding*0.5)},{15,88+(int)(rounding*0.5)},{82,73+rounding},{118,88},{449,54}});
}
public GeneralPath arrayToGeneralpath(int block[][]){
GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, block.length);
polyline.moveTo ((block[0][0]+x)*scalefactor, (block[0][1]+y)*scalefactor);
for (int index = 1; index < block.length; index++) {
polyline.lineTo((block[index][0]+x)*scalefactor, (block[index][1]+y)*scalefactor);
};
polyline.closePath();
return polyline;
}
public void draw(Graphics2D g2d){
for(int i = 0; i < buttonparts.size(); i++){ //fill every button part
g2d.setColor(colors.get(i));
g2d.fill((buttonparts.get(i)));
}
if(selected){
g2d.setStroke(new BasicStroke(4));
g2d.setColor(Color.BLACK);
g2d.draw(border);
}
g2d.setPaint(gradient);
g2d.fill(line);
//draw text
g2d.setColor(Color.BLACK);
Font textFont = new Font("OCR A Extended", Font.BOLD, (int)(30*scalefactor));
g2d.setFont(textFont);
g2d.translate((x+160)*scalefactor, (y+74)*scalefactor);
g2d.rotate(-0.1);
g2d.drawString(text, 0, 0);
g2d.rotate(0.1);
g2d.translate(-(x+160)*scalefactor, -(y+74)*scalefactor);
}
public void update(){
if(selected && fadecounter < 5){
x -= 8;
y -=8;
scalefactor += 0.04;
fadecounter++;
calculateButton();
}else if(!selected && fadecounter >0){
x += 8;
y += 8;
fadecounter--;
scalefactor -= 0.04;
calculateButton();
}
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}