package com.lucasdnd.ags.ui;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import com.lucasdnd.ags.system.GameSystem;
public class NewGamePanel extends Panel {
TextInput storeNameTextInput;
RadioButtonGroup difficultyRadioButtonGroup, mapSizeRadioButtonGroup;
TextButton startGameTextButton, cancelTextButton;
final int maxStoreNameLength = 22;
final float padding = 4f;
final float margin = 8f;
final float lineHeight = 32f;
public NewGamePanel(String title, float xPos, float yPos, float width, float height) throws SlickException {
super(title, xPos, yPos, width, height);
final float buttonWidth = 100f;
final float buttonHeight = 32f;
final float textInputWidth = 297f;
storeNameTextInput = new TextInput("Store name: ", maxStoreNameLength, xPos + padding + margin, yPos + lineHeight + margin, textInputWidth, buttonHeight);
startGameTextButton = new TextButton("Start", xPos + width * 0.25f, yPos + height - buttonHeight - padding * 4, buttonWidth, buttonHeight);
cancelTextButton = new TextButton("Cancel", xPos + width * 0.5f, yPos + height - buttonHeight - padding * 4, buttonWidth, buttonHeight);
difficultyRadioButtonGroup = new RadioButtonGroup("Difficulty", xPos + padding + margin, yPos + padding + lineHeight*2 + margin*2, new RadioButton[] {
new RadioButton("Easy ($ 2000)"),
new RadioButton("Normal ($ 1500)"),
new RadioButton("Hard ($ 1000)")
});
mapSizeRadioButtonGroup = new RadioButtonGroup("Map size", xPos + width / 2f + padding + margin*4, yPos + padding + lineHeight*2 + margin*2, new RadioButton[] {
new RadioButton("Small"),
new RadioButton("Medium", false),
new RadioButton("Large", false)
});
}
public void update (GameContainer container, StateBasedGame game, int delta, boolean leftMouseClicked, boolean leftMouseDown,
int mouseX, int mouseY) throws SlickException {
if (visible) {
storeNameTextInput.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY, container.getInput());
startGameTextButton.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
cancelTextButton.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
difficultyRadioButtonGroup.update(leftMouseClicked, mouseX, mouseY);
mapSizeRadioButtonGroup.update(leftMouseClicked, mouseX, mouseY);
// Enable the Start Game button if options were selected
startGameTextButton.setEnabled(
(difficultyRadioButtonGroup.getSelected() != null
&& mapSizeRadioButtonGroup.getSelected() != null)
&& storeNameTextInput.getText().length() > 0
);
// Check clicks
if (startGameTextButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
GameSystem gameSystem = ((GameSystem)game);
// Set the settings
gameSystem.setStoreName(storeNameTextInput.getText());
gameSystem.setDifficulty(difficultyRadioButtonGroup.getSelectedIndex());
gameSystem.setMapSize(mapSizeRadioButtonGroup.getSelectedIndex());
// Go!
gameSystem.addState(GameSystem.mainState);
GameSystem.mainState.init(container, gameSystem);
gameSystem.enterState(GameSystem.MAIN_STATE);
} else if (cancelTextButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
this.setVisible(false);
return;
}
}
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g, Font panelFont, Font shadowFont) throws SlickException {
if (visible) {
super.render(container, game, g, panelFont, shadowFont);
storeNameTextInput.render(container, game, g);
difficultyRadioButtonGroup.render(container, game, g);
mapSizeRadioButtonGroup.render(container, game, g);
startGameTextButton.render(container, game, g);
cancelTextButton.render(container, game, g);
}
}
@Override
public boolean gotLeftClicked(boolean leftMouseClicked, int mouseX, int mouseY) throws SlickException {
if (leftMouseClicked &&
mouseX >= xPos && mouseX <= xPos + width &&
mouseY >= yPos && mouseY <= yPos + height
) {
return true;
}
return false;
}
@Override
public boolean gotRightClicked(boolean rightMouseClicked, int mouseX, int mouseY) {
return false;
}
public TextInput getStoreNameTextInput() {
return storeNameTextInput;
}
}