/**
*
*/
package cz.cuni.mff.abacs.burglar.visual;
import com.aem.sticky.StickyListener;
import com.aem.sticky.button.Button;
import com.aem.sticky.button.CentralisedTextButton;
import com.aem.sticky.button.events.ButtonListener;
import com.aem.sticky.button.events.ClickListener;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
/**
* Game main menu implementation.
*
* @author abacs
*
*/
public class MenuState extends BasicGameState {
protected static String STR_SELECT_LEVEL = "Select Level";
protected static String STR_EXIT = "Exit";
protected static String SUBPATH_CLICK_SOUND = "click.ogg";
// -------------------------------------------------------------------------
protected float SCALE_STEP = 0.0001f;
protected float MIN_SCALE = 1.0000f;
protected float MAX_SCALE = 1.0500f;
// -------------------------------------------------------------------------
private final int _stateID;
protected StickyListener _buttonListener;
protected Button _levelSelectButton;
protected Button _exitButton;
protected GameContainer _container = null;
protected StateBasedGame _stateBasedGame = null;
// -------------------------------------------------------------------------
// constructors:
/**
*
*
* @param stateId
*/
public MenuState(int stateId) {
this._stateID = stateId;
}
// -------------------------------------------------------------------------
// BasigGameState:
@Override
public int getID() {
return this._stateID;
}
/**
* Initialize any needed data before the menu loop.
*
* @param container
* @param game
* @throws SlickExpression
*/
@Override
public void init(
GameContainer container,
StateBasedGame game
) throws SlickException {
this._stateBasedGame = game;
this._container = container;
Sound buttonSound = new Sound(VisualBurglar.PATH_SOUNDS + SUBPATH_CLICK_SOUND);
// buttons:
this._buttonListener = new StickyListener();
this.createExitButton(container, buttonSound);
this.createLevelSelectButton(container, buttonSound);
}
/**
* Adds listeners.
*/
@Override
public void enter(GameContainer container, StateBasedGame game) throws SlickException {
super.enter(container, game);
container.getInput().addListener(this._buttonListener);
System.out.println("Menu state entered");
}
/**
* Removes all listeners.
*/
@Override
public void leave(GameContainer container, StateBasedGame game) throws SlickException {
super.leave(container, game);
container.getInput().removeListener(_buttonListener);
System.out.println("Menu state left");
}
/**
* Called during the game to update the logic in our world,
* within this method we obtain the user input,
* calculate the world response to the input, do the calculation.
*
* @param container
* @param game
* @param delta
* @throws SlickExpression
*/
@Override
public void update(
GameContainer container,
StateBasedGame game,
int delta
) throws SlickException {
this._exitButton.update(container, delta);
this._levelSelectButton.update(container, delta);
}
/**
* Allows to draw the menu.
*
* @param container
* @param game
* @param delta
* @throws SlickExpression
*/
@Override
public void render(
GameContainer container,
StateBasedGame game,
Graphics graphics
) throws SlickException {
// Draw menu
this._exitButton.render(container, graphics);
this._levelSelectButton.render(container, graphics);
}
// -------------------------------------------------------------------------
// buttons:
protected void createLevelSelectButton(
final GameContainer container,
Sound buttonSound
) throws SlickException {
float x = container.getWidth() / 2;
float y = container.getHeight() / 2 - 25;
this._levelSelectButton =
new CentralisedTextButton(x, y, STR_SELECT_LEVEL, buttonSound);
this._buttonListener.add(this._levelSelectButton);
this._levelSelectButton.addListener(new ClickListener() {
@Override
public void onClick(Button arg0, float arg1, float arg2) {
// enters the new game state:
_stateBasedGame.getState(VisualBurglar.STATE_LEVEL_SELECT);
_stateBasedGame.enterState(VisualBurglar.STATE_LEVEL_SELECT);
}
/** Does nothing */
@Override
public void onDoubleClick(Button arg0, float arg1, float arg2) { }
/** Does nothing */
@Override
public void onRightClick(Button arg0, float arg1, float arg2) { }
});
this._levelSelectButton.addListener(new ButtonListener() {
@Override
public void onMouseEnter(Button arg0) { }
@Override
public void onMouseExit(Button arg0) { }
});
}
protected void createExitButton(
GameContainer container,
Sound buttonSound
) throws SlickException {
float x = container.getWidth() / 2;
float y = container.getHeight() / 2 + 25;
this._exitButton = new CentralisedTextButton(x, y, STR_EXIT, buttonSound);
this._buttonListener.add(this._exitButton);
this._exitButton.addListener(new ClickListener() {
@Override
public void onClick(Button arg0, float arg1, float arg2) {
_container.exit();
}
@Override
public void onDoubleClick(Button arg0, float arg1, float arg2) { }
@Override
public void onRightClick(Button arg0, float arg1, float arg2) { }
});
this._exitButton.addListener(new ButtonListener() {
@Override
public void onMouseEnter(Button arg0) { }
@Override
public void onMouseExit(Button arg0) { }
});
}
}