Commit f0b38c37 authored by Haakon Gunleiksrud's avatar Haakon Gunleiksrud
Browse files

Merge branch '98-add-menustate' into 'dev'

Resolve "Add menustate"

Closes #98

See merge request tobiasio/progark-gruppe-3!102
parents e24b0bd2 1b9cc460
......@@ -16,7 +16,7 @@ import com.gameware.game.models.Player;
import com.gameware.game.states.GameStateManager;
import com.gameware.game.states.LoginState;
import com.gameware.game.states.MenuState;
import com.gameware.game.states.PlayStateComposite;
import com.gameware.game.states.PlayStateTemplate;
import java.io.IOException;
import java.lang.reflect.Constructor;
......@@ -44,7 +44,7 @@ public class GameWare extends ApplicationAdapter {
private Player player;
private List<Game> games = new ArrayList<>();
private Map<String, PlayStateComposite> gameIdToPlayState = new HashMap<>();
private Map<String, PlayStateTemplate> gameIdToPlayState = new HashMap<>();
//Singleton (lazy initialization)
private GameWare(){ }
......@@ -60,10 +60,6 @@ public class GameWare extends ApplicationAdapter {
@Override
public void create () {
if(instance == null){
instance = this;
}
gsm = GameStateManager.getInstance();
loadGameConfigs();
......@@ -121,11 +117,11 @@ public class GameWare extends ApplicationAdapter {
return games;
}
public Map<String, PlayStateComposite> getGameIdToPlayState(){
public Map<String, PlayStateTemplate> getGameIdToPlayState(){
return gameIdToPlayState;
}
public void updateGameMap(String id, PlayStateComposite state){
public void updateGameMap(String id, PlayStateTemplate state){
gameIdToPlayState.put(id, state);
}
......@@ -201,7 +197,7 @@ public class GameWare extends ApplicationAdapter {
try {
Class cl = Class.forName(className);
Constructor con = cl.getConstructor(GameStateManager.class);
PlayStateComposite state = (PlayStateComposite) con.newInstance(gsm);
PlayStateTemplate state = (PlayStateTemplate) con.newInstance(gsm);
gameIdToPlayState.put(id, state);
} catch (Exception e) {
e.printStackTrace();
......
......@@ -7,7 +7,7 @@ import com.gameware.game.sprites.Bubble;
import java.util.ArrayList;
public class BubbleWrapState extends PlayStateComposite {
public class BubbleWrapState extends PlayStateTemplate {
private Texture background;
private Texture unpopped;
private Texture popped1;
......
......@@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ColorRushState extends PlayStateComposite {
public class ColorRushState extends PlayStateTemplate {
private List<ColorRushTarget> activeTargets;
private List<ColorRushTarget> previousTargets;
......
......@@ -26,7 +26,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CreateJoinTournamentState extends State {
public class CreateJoinTournamentState extends MenuStateTemplate {
// Data
private List<Tournament> tournaments = new ArrayList<>();
......@@ -107,7 +107,7 @@ public class CreateJoinTournamentState extends State {
}
}
private void makeStage(){
protected void makeStage(){
Table rootTable = makeRootTable();
titleLabel.setFontScale(titleFontBigScale);
......@@ -250,7 +250,7 @@ public class CreateJoinTournamentState extends State {
}
private TextButton makeBackBtn(){
TextButton backBtn = new TextButton(backBtnText, skin);
TextButton backBtn = new TextButton(backBtnText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......
......@@ -27,7 +27,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CreateNewTournamentState extends State {
public class CreateNewTournamentState extends MenuStateTemplate {
// Data
private List<Game> games;
......@@ -57,7 +57,7 @@ public class CreateNewTournamentState extends State {
// Texts
private final String backBtnText = "Back";
private final String createBtnText = "Create";
private final String nextBtnText = "Next";
private final String nextBtnText = "Select games";
private final String noNameText = "No name given";
private final String noGamesSelectedText = "No games selected";
......@@ -115,16 +115,16 @@ public class CreateNewTournamentState extends State {
roundsPerGameSelectBox.setItems(1,2,3);
startDelaySelectBox.setItems(1,2,3);
nameDialog = makeDialog(nameLabelQuestion);
timePerRoundDialog = makeDialog(timePerRoundLabelQuestion);
maxPlayersDialog = makeDialog(maxPlayersLabelQuestion);
roundsPerGameDialog = makeDialog(roundsPerGameLabelQuestion);
startDelayDialog = makeDialog(startDelayLabelQuestion);
nameDialog = makeQuestionIconDialog(nameLabelQuestion);
timePerRoundDialog = makeQuestionIconDialog(timePerRoundLabelQuestion);
maxPlayersDialog = makeQuestionIconDialog(maxPlayersLabelQuestion);
roundsPerGameDialog = makeQuestionIconDialog(roundsPerGameLabelQuestion);
startDelayDialog = makeQuestionIconDialog(startDelayLabelQuestion);
makeStage();
}
private void makeStage(){
protected void makeStage(){
rootTable = makeRootTable();
titleLabel.setFontScale(titleFontBigScale);
......@@ -146,21 +146,21 @@ public class CreateNewTournamentState extends State {
innerTable.pad(spacingLittle);
innerTable.setBackground(backgroundTableBlueRounded);
innerTable.add(makeRow(nameLabel, nameDialog));
innerTable.add(makeTableWithLabelAndQuestionIcon(nameLabel, nameDialog));
nameInputField.setMaxLength(9);
innerTable.add(nameInputField).size(nameFieldWidth, inputFieldHeight);
innerTable.row();
removeKeyPadAtTouch();
innerTable.add(makeRow(timePerRoundLabel, timePerRoundDialog));
innerTable.add(makeTableWithLabelAndQuestionIcon(timePerRoundLabel, timePerRoundDialog));
innerTable.add(timePerRoundSelectBox);
innerTable.row();
innerTable.add(makeRow(maxPlayersLabel, maxPlayersDialog));
innerTable.add(makeTableWithLabelAndQuestionIcon(maxPlayersLabel, maxPlayersDialog));
innerTable.add(maxPlayersSelectBox);
innerTable.row();
innerTable.add(makeRow(roundsPerGameLabel, roundsPerGameDialog));
innerTable.add(makeTableWithLabelAndQuestionIcon(roundsPerGameLabel, roundsPerGameDialog));
innerTable.add(roundsPerGameSelectBox);
innerTable.row();
innerTable.add(makeRow(startDelayLabel, startDelayDialog));
innerTable.add(makeTableWithLabelAndQuestionIcon(startDelayLabel, startDelayDialog));
innerTable.add(startDelaySelectBox);
rootTable.add(innerTable).colspan(2);
rootTable.row();
......@@ -169,7 +169,7 @@ public class CreateNewTournamentState extends State {
rootTable.add(makeBackBtn()).expand().bottom().left();
TextButton nextBtn = makeNextBtn();
rootTable.add(nextBtn).size(buttonWidth, buttonHeight).expand().bottom().right();
rootTable.add(nextBtn).size(buttonWidth*1.2f, buttonHeight).expand().bottom().right();
}
private void makePageTwo(){
......@@ -186,11 +186,11 @@ public class CreateNewTournamentState extends State {
continue;
}
innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).screenshot)).width(imageWidthAndHeigh).height(imageWidthAndHeigh);
innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).getScreenshot())).width(imageWidthAndHeigh).height(imageWidthAndHeigh);
Table innerInnerTable = new Table();
innerInnerTable.defaults().space(spacingLittle);
innerInnerTable.add(makeRow(new Label(g.getName(), skin), makeDialog(new Label(g.getExplanation().replaceAll("\\\\n", "\n"), skin)))).spaceBottom(spacingLittle);
innerInnerTable.add(makeTableWithLabelAndQuestionIcon(new Label(g.getName(), skin), makeQuestionIconDialog(new Label(g.getExplanation().replaceAll("\\\\n", "\n"), skin)))).spaceBottom(spacingLittle);
innerInnerTable.row();
CheckBox gameCB = new CheckBox("",skin);
......@@ -235,7 +235,7 @@ public class CreateNewTournamentState extends State {
}
private TextButton makeCreateBtn() {
TextButton createBtn = new TextButton(createBtnText, skin);
TextButton createBtn = new TextButton(createBtnText, makeTextButtonStyle(greenColor));
createBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......@@ -274,7 +274,7 @@ public class CreateNewTournamentState extends State {
}
private TextButton makeBackBtn() {
TextButton backBtn = new TextButton(backBtnText, skin);
TextButton backBtn = new TextButton(backBtnText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......
......@@ -18,7 +18,7 @@ import com.gameware.game.models.Tournament;
import java.util.ArrayList;
import java.util.List;
public class FinishedTournamentState extends State {
public class FinishedTournamentState extends MenuStateTemplate {
// Data
private Tournament tournament;
......@@ -56,7 +56,7 @@ public class FinishedTournamentState extends State {
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = makeRootTable(backgroundFinTourn);
rootTable.defaults().spaceBottom(spacingMedium);
......@@ -135,7 +135,7 @@ public class FinishedTournamentState extends State {
}
private TextButton makeBackBtn(){
TextButton backBtn = new TextButton(backBtnText, skin);
TextButton backBtn = new TextButton(backBtnText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......
......@@ -12,7 +12,7 @@ import com.gameware.game.sprites.SlicingCircle;
import java.util.ArrayList;
import java.util.List;
public class FruitSlicerState extends PlayStateComposite {
public class FruitSlicerState extends PlayStateTemplate {
private int totalFruitsCut = 0;
private float startingEmitFrequency = 1f;
private float endingEmitFrequency = 0.2f;
......
package com.gameware.game.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
......@@ -17,7 +19,7 @@ import com.gameware.game.sprites.LoadingText;
import java.io.IOException;
import java.util.NoSuchElementException;
public class LoginState extends State {
public class LoginState extends MenuStateTemplate {
// Labels
private final Label titleLabel = new Label("GameWare", skin, "big");
......@@ -63,7 +65,7 @@ public class LoginState extends State {
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = super.makeRootTable();
// Add widgets
......@@ -196,14 +198,11 @@ public class LoginState extends State {
}
private TextButton makeBackBtn(){
TextButton backBtn = new TextButton(backText, skin);
TextButton backBtn = new TextButton(backText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
page --;
errorLabel.setText("");
stage.clear();
makeStage();
handleBackBtnClicked();
}
});
return backBtn;
......@@ -274,6 +273,13 @@ public class LoginState extends State {
}
}
private void handleBackBtnClicked(){
page --;
errorLabel.setText("");
stage.clear();
makeStage();
}
// Adds listener to stage that removes keyboard on touch
private void removeKeyPadAtTouch(){
stage.getRoot().addCaptureListener(new InputListener() {
......@@ -327,6 +333,13 @@ public class LoginState extends State {
this.loadingText.setLoading();
}
@Override
protected void handleInput() {
if (Gdx.input.isKeyJustPressed(Input.Keys.BACK) && page == 1){
handleBackBtnClicked();
}
}
@Override
public void dispose() {
stage.dispose();
......
......@@ -2,6 +2,7 @@ package com.gameware.game.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
......@@ -15,7 +16,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.gameware.game.GameWare;
import com.gameware.game.sprites.LoadingText;
public class MenuState extends State{
public class MenuState extends MenuStateTemplate{
// Labels
private final Label titleLabel = new Label("GameWare", skin, "big");
......@@ -44,14 +45,13 @@ public class MenuState extends State{
private boolean multiBtnClicked = false;
private LoadingText loadingText = new LoadingText();
public MenuState(GameStateManager gsm) {
super(gsm);
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = super.makeRootTable();
// Add widgets
......@@ -140,7 +140,7 @@ public class MenuState extends State{
Table table = new Table();
table.add(new Image(logOut)).width(logOutWidth).height(buttonHeight/3f).spaceRight(spacingLittle/2);
table.add(new Label(logOutBtnText, skin, "big")).spaceRight(spacingLittle/2);
Button btn = new Button(table, skin);
Button btn = new Button(table, makeTextButtonStyle(backColor));
btn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......
package com.gameware.game.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.gameware.game.GameWare;
public abstract class MenuStateTemplate extends State{
// Font
protected final float fontScale = 3f;
protected final float tinierTitleFontBigScale = 1.5f;
protected final float titleFontBigScale = 2.5f;
// Spacing
protected final float spacingLittle = Gdx.graphics.getHeight()/50;
protected final float spacingMedium = Gdx.graphics.getHeight()/15;
protected final float spacingLarge = spacingMedium*2;
// Padding
protected final int rootTablePadding = Gdx.graphics.getWidth()/15;
protected final int rootTablePaddingTop = Gdx.graphics.getWidth()/10;
protected final int padding = 50;
// Width & Height
protected final int buttonWidth = Gdx.graphics.getWidth()/3;
protected final int buttonHeight = Gdx.graphics.getHeight()/12;
protected final float questionMarkWidthAndHeight = Gdx.graphics.getWidth()/25;
protected final float imageWidthAndHeigh = Gdx.graphics.getWidth()/4;
// Textures
protected TextureRegionDrawable background = new TextureRegionDrawable(new TextureRegion(new Texture("state/bg1.jpg")));
protected TextureRegionDrawable backgroundScore = new TextureRegionDrawable(new TextureRegion(new Texture("state/bg_score.jpg")));
protected TextureRegionDrawable backgroundFinTourn = new TextureRegionDrawable(new TextureRegion(new Texture("state/bg1_finTourn.jpg")));
protected TextureRegionDrawable backgroundTableBlueRounded = new TextureRegionDrawable(new TextureRegion(new Texture("state/tableBGRounded.png")));
protected TextureRegionDrawable questionMark = new TextureRegionDrawable(new TextureRegion(new Texture("state/questionMarkIcon.png")));
protected Color backColor = Color.VIOLET;
protected Color greenColor = Color.GREEN;
// Sound Effects
protected Sound checkBoxSound;
public MenuStateTemplate(GameStateManager gsm){
super(gsm);
skin.getFont("font").getData().setScale(fontScale);
// Add sound effects
this.checkBoxSound = Gdx.audio.newSound(Gdx.files.internal("sfx/check_box.ogg"));
}
// Override Methods
@Override
protected void handleInput(){}
@Override
public void update(float dt){
if(firstTimeRunningUpdate) {
// First time running update change input processor. Or else it won't work when states are stacked in the gsm
Gdx.input.setInputProcessor(stage);
Gdx.input.setCatchBackKey(true);
firstTimeRunningUpdate = false;
}
this.handleInput();
stage.act(dt);
}
@Override
public void render(SpriteBatch sb){
stage.draw();
}
@Override
public void dispose(){
stage.dispose();
}
@Override
public void reset(){}
// Abstract methods
protected abstract void makeStage();
// UI methods:
// Makes default rootTable
protected Table makeRootTable(TextureRegionDrawable background){
Table rootTable = new Table();
rootTable.setFillParent(true);
rootTable.setBackground(background);
rootTable.pad(rootTablePadding);
rootTable.padTop(rootTablePaddingTop);
rootTable.defaults().space(spacingLittle);
return rootTable;
}
// Makes default rootTable
protected Table makeRootTable(){
return makeRootTable(background);
}
protected TextureRegionDrawable makeColorBackground(Color color){
Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565);
bgPixmap.setColor(color);
bgPixmap.fill();
return new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap)));
}
// Dialog methods
protected Table makeTableWithLabelAndQuestionIcon(Label label, final Dialog dialog){
Table table = new Table();
table.add(label).spaceRight(spacingLittle/2);
Image questionMarkImage = new Image(questionMark);
questionMarkImage.addListener(new ClickListener(){
@Override
public void clicked(InputEvent e, float x, float y){
if(GameWare.getInstance().isSoundEffectsOn()){ checkBoxSound.play(); }
dialog.show(stage);
}
});
table.add(questionMarkImage).width(questionMarkWidthAndHeight).height(questionMarkWidthAndHeight).top();
return table;
}
protected Dialog makeQuestionIconDialog(Label label){
Dialog dialog = new Dialog("", skin, "dialog") {
public void result(Object obj) { }
};
label.setAlignment(Align.center);
label.setWrap(true);
dialog.getContentTable().add(label).width(Gdx.graphics.getWidth()/1.5f).pad(dialog.getHeight()/5f).padBottom(0);
dialog.button("Okay", true); //sends "true" as the result
dialog.getButtonTable().pad(dialog.getHeight()/2f);
return dialog;
}
protected TextButton.TextButtonStyle makeTextButtonStyle(Color background){
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.up = skin.newDrawable("button", background);
textButtonStyle.down = skin.newDrawable("button-down", background);
textButtonStyle.font = skin.getFont("font-big");
textButtonStyle.fontColor = Color.WHITE;
return textButtonStyle;
}
}
......@@ -11,7 +11,8 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Scaling;
import com.gameware.game.GameWare;
public class OptionsState extends State {
public class OptionsState extends MenuStateTemplate {
// Labels
private final Label titleLabel = new Label("Options", skin, "big");
......@@ -27,11 +28,11 @@ public class OptionsState extends State {
public OptionsState(GameStateManager gsm) {
super(gsm);
stage.clear();
makeStage();
}
private void makeStage(){
protected void makeStage(){
// Make root table
Table rootTable = super.makeRootTable();
rootTable.defaults().spaceBottom(spacingMedium).expandY();
......@@ -98,7 +99,7 @@ public class OptionsState extends State {
}
private TextButton makeBackBtn(){
TextButton backBtn = new TextButton(backBtnText, skin);
TextButton backBtn = new TextButton(backBtnText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......
......@@ -4,7 +4,6 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.gameware.game.GameWare;
import com.gameware.game.sprites.ConfirmationBox;
import com.gameware.game.sprites.LoadingText;
......@@ -15,25 +14,33 @@ import java.util.ArrayList;
import java.util.List;
public class PauseState extends State {
private ShapeRenderer sr;
private List<PauseCircle> pauseCircles;
// Data
private PlayStateTemplate pausedGame;
// Textures
private Texture background;
private Texture pauseText;
private Texture dimmingTexture;
private Texture countdownNumberTexture;
private List<Texture> countdownNumbers;
// Variables
private Color originalFontColor;
private float countdownDurationLeft;
private boolean countdownStarted;
private boolean userExited;
private boolean needsConfirmation;
// Widgets