package com.lucasdnd.ags.ui;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
public class Menu extends UIComponent {
private ArrayList<Button> buttons; // List of Buttons this Menu has
private float separation; // Amount of pixels separating each Button
private int orientation; // Horizontal or Vertical
public static final int VERTICAL = 0; // Defines the orientation
public static final int HORIZONTAL = 1; // Defines the orientation
// Animation
private float alpha = 1f;
private float xOrigin = 0f;
private float xMovementStep = 0f;
public Menu(ArrayList<Button> buttons, float xPos, float yPos, float separation, int orientation) {
// Basic stuff
this.buttons = buttons;
this.xPos = xPos;
this.yPos = yPos;
this.separation = separation;
this.orientation = orientation;
// Organizes the x and y positions of each Button
organizeButtons();
// Animation
xOrigin = xPos - 15f;
xMovementStep = (xPos - xOrigin) / 60f;
}
/**
* The first Button is placed on the Menu's x and y. From there, it places the next ones a separation of pixels
* away from the previous one. The direction to which this loop will move is defined by the orientation attribute.
*/
public void organizeButtons() { // TODO: use the correct loop when using ArrayLists
if(orientation == VERTICAL) {
for(int i = 0; i < buttons.size(); i++) {
buttons.get(i).setxPos(xPos);
}
for(int i = 0; i < buttons.size(); i++) {
if(i == 0)
buttons.get(i).setyPos(yPos + (i * (separation + buttons.get(i).getHeight())));
else
buttons.get(i).setyPos(yPos + (i * (separation + buttons.get(i - 1).getHeight())));
}
} else {
for(int i = 0; i < buttons.size(); i++) {
buttons.get(i).setyPos(yPos);
}
for(int i = 0; i < buttons.size(); i++) {
if(i == 0)
buttons.get(i).setxPos(xPos + (i * (separation + buttons.get(i).getWidth())));
else
buttons.get(i).setxPos(xPos + (i * (separation + buttons.get(i - 1).getWidth())));
}
}
}
public void update(GameContainer container, StateBasedGame game, int delta, boolean leftMouseClicked, boolean leftMouseDown, int mouseX, int mouseY) throws SlickException {
if(visible) {
if(alpha < 1f) {
alpha += 1/60f;
xPos += xMovementStep;
organizeButtons();
}
for(Button b : buttons) {
b.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
}
} else {
alpha = 0f;
xPos = xOrigin;
organizeButtons();
}
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
if(visible) {
for(Button b : buttons) {
if (b.getImages() != null && b.getImages().length > 0) {
for(org.newdawn.slick.Image i : b.getImages()) {
i.setAlpha(alpha);
}
}
b.render(container, game, g);
}
}
}
}