diff --git a/frontend/android/assets/glassy/raw/BlueSquare.png b/frontend/android/assets/BlueSquare.png similarity index 100% rename from frontend/android/assets/glassy/raw/BlueSquare.png rename to frontend/android/assets/BlueSquare.png diff --git a/frontend/android/assets/glassy/raw/ColorRushBackground.jpg b/frontend/android/assets/ColorRushBackground.jpg similarity index 100% rename from frontend/android/assets/glassy/raw/ColorRushBackground.jpg rename to frontend/android/assets/ColorRushBackground.jpg diff --git a/frontend/android/assets/ConfirmationSprite.png b/frontend/android/assets/ConfirmationSprite.png new file mode 100644 index 0000000000000000000000000000000000000000..90edb606773c8507471d2e2fcd165adf9dd19929 Binary files /dev/null and b/frontend/android/assets/ConfirmationSprite.png differ diff --git a/frontend/android/assets/DimmingTexture.png b/frontend/android/assets/DimmingTexture.png new file mode 100644 index 0000000000000000000000000000000000000000..660a9ad25cc2fc6574570093cd25ca857ef67e71 Binary files /dev/null and b/frontend/android/assets/DimmingTexture.png differ diff --git a/frontend/android/assets/ExitButton.png b/frontend/android/assets/ExitButton.png new file mode 100644 index 0000000000000000000000000000000000000000..feb789f751ee76ad8d6e25bdc7e98609d4e09ee9 Binary files /dev/null and b/frontend/android/assets/ExitButton.png differ diff --git a/frontend/android/assets/glassy/raw/GreenSquare.png b/frontend/android/assets/GreenSquare.png similarity index 100% rename from frontend/android/assets/glassy/raw/GreenSquare.png rename to frontend/android/assets/GreenSquare.png diff --git a/frontend/android/assets/glassy/raw/GreySquare.png b/frontend/android/assets/GreySquare.png similarity index 100% rename from frontend/android/assets/glassy/raw/GreySquare.png rename to frontend/android/assets/GreySquare.png diff --git a/frontend/android/assets/glassy/raw/LoadingCircle.png b/frontend/android/assets/LoadingCircle.png similarity index 100% rename from frontend/android/assets/glassy/raw/LoadingCircle.png rename to frontend/android/assets/LoadingCircle.png diff --git a/frontend/android/assets/glassy/raw/PauseBackground.jpg b/frontend/android/assets/PauseBackground.jpg similarity index 100% rename from frontend/android/assets/glassy/raw/PauseBackground.jpg rename to frontend/android/assets/PauseBackground.jpg diff --git a/frontend/android/assets/glassy/raw/PauseButtonBlack.png b/frontend/android/assets/PauseButtonBlack.png similarity index 100% rename from frontend/android/assets/glassy/raw/PauseButtonBlack.png rename to frontend/android/assets/PauseButtonBlack.png diff --git a/frontend/android/assets/glassy/raw/PauseButtonWhite.png b/frontend/android/assets/PauseButtonWhite.png similarity index 100% rename from frontend/android/assets/glassy/raw/PauseButtonWhite.png rename to frontend/android/assets/PauseButtonWhite.png diff --git a/frontend/android/assets/PauseText.png b/frontend/android/assets/PauseText.png new file mode 100644 index 0000000000000000000000000000000000000000..7496fd7f6ca2aed81453db867919af7c512ae687 Binary files /dev/null and b/frontend/android/assets/PauseText.png differ diff --git a/frontend/android/assets/glassy/raw/RedSquare.png b/frontend/android/assets/RedSquare.png similarity index 100% rename from frontend/android/assets/glassy/raw/RedSquare.png rename to frontend/android/assets/RedSquare.png diff --git a/frontend/android/assets/ResumeButton.png b/frontend/android/assets/ResumeButton.png new file mode 100644 index 0000000000000000000000000000000000000000..eb15370fced7cb1222d15240f8afe28ee403587a Binary files /dev/null and b/frontend/android/assets/ResumeButton.png differ diff --git a/frontend/android/assets/glassy/raw/VioletSquare.png b/frontend/android/assets/VioletSquare.png similarity index 100% rename from frontend/android/assets/glassy/raw/VioletSquare.png rename to frontend/android/assets/VioletSquare.png diff --git a/frontend/android/assets/bubble_popped_1.png b/frontend/android/assets/bubble_popped_1.png new file mode 100644 index 0000000000000000000000000000000000000000..2c90959308dc3d16fe0e7b378600f5462b243a5c Binary files /dev/null and b/frontend/android/assets/bubble_popped_1.png differ diff --git a/frontend/android/assets/bubble_popped_2.png b/frontend/android/assets/bubble_popped_2.png new file mode 100644 index 0000000000000000000000000000000000000000..b5a0c9cb12500e4488c927e120bf22ad8badcaaf Binary files /dev/null and b/frontend/android/assets/bubble_popped_2.png differ diff --git a/frontend/android/assets/bubble_popped_3.png b/frontend/android/assets/bubble_popped_3.png new file mode 100644 index 0000000000000000000000000000000000000000..1078784397635ef1f5ca07c6553f2c2efd40d543 Binary files /dev/null and b/frontend/android/assets/bubble_popped_3.png differ diff --git a/frontend/android/assets/glassy/raw/bubble_unpopped_1.png b/frontend/android/assets/bubble_unpopped_1.png similarity index 100% rename from frontend/android/assets/glassy/raw/bubble_unpopped_1.png rename to frontend/android/assets/bubble_unpopped_1.png diff --git a/frontend/android/assets/glassy/raw/bubblewrap_background.jpg b/frontend/android/assets/bubblewrap_background.jpg similarity index 100% rename from frontend/android/assets/glassy/raw/bubblewrap_background.jpg rename to frontend/android/assets/bubblewrap_background.jpg diff --git a/frontend/android/assets/glassy/raw/button-disabled.png b/frontend/android/assets/button-disabled.png similarity index 100% rename from frontend/android/assets/glassy/raw/button-disabled.png rename to frontend/android/assets/button-disabled.png diff --git a/frontend/android/assets/glassy/raw/dialog_holo_light.9.png b/frontend/android/assets/dialog_holo_light.9.png similarity index 100% rename from frontend/android/assets/glassy/raw/dialog_holo_light.9.png rename to frontend/android/assets/dialog_holo_light.9.png diff --git a/frontend/android/assets/glassy/raw/PauseText.png b/frontend/android/assets/glassy/raw/PauseText.png deleted file mode 100644 index 32c2b42a05fa52302d22c006bcde72b22a6b55ff..0000000000000000000000000000000000000000 Binary files a/frontend/android/assets/glassy/raw/PauseText.png and /dev/null differ diff --git a/frontend/android/assets/glassy/raw/bubble_popped_1.png b/frontend/android/assets/glassy/raw/bubble_popped_1.png deleted file mode 100644 index 006f49f35147e2dec8b6c21f6ad62bfe95bfa571..0000000000000000000000000000000000000000 Binary files a/frontend/android/assets/glassy/raw/bubble_popped_1.png and /dev/null differ diff --git a/frontend/android/assets/glassy/raw/bubble_popped_2.png b/frontend/android/assets/glassy/raw/bubble_popped_2.png deleted file mode 100644 index 0110fa792498982b5a526c91dfd60b2fd9ea1056..0000000000000000000000000000000000000000 Binary files a/frontend/android/assets/glassy/raw/bubble_popped_2.png and /dev/null differ diff --git a/frontend/android/assets/glassy/raw/bubble_popped_3.png b/frontend/android/assets/glassy/raw/bubble_popped_3.png deleted file mode 100644 index 2a806cc4558d1a3bd2ab3839a3ad82f8c0948075..0000000000000000000000000000000000000000 Binary files a/frontend/android/assets/glassy/raw/bubble_popped_3.png and /dev/null differ diff --git a/frontend/android/assets/glassy/skin/glassy-ui.json b/frontend/android/assets/glassy/skin/glassy-ui.json index 13c0dbf6f5bf79adc12bde16db58ab37c2bed0d0..cff1e994597a2705fd1fae695d3085a91ac1bf05 100644 --- a/frontend/android/assets/glassy/skin/glassy-ui.json +++ b/frontend/android/assets/glassy/skin/glassy-ui.json @@ -172,7 +172,7 @@ com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle: { com.badlogic.gdx.scenes.scene2d.ui.SelectBox$SelectBoxStyle: { default: { font: font - fontColor: dark-cyan + fontColor: black background: select-box scrollStyle: scroll listStyle: plain diff --git a/frontend/core/src/com/gameware/game/GameWare.java b/frontend/core/src/com/gameware/game/GameWare.java index 2e55349b23b7cb08f049199dc450a0970675b421..5a4d03f667ebf14e1e9318770e012e21fab3f5ad 100644 --- a/frontend/core/src/com/gameware/game/GameWare.java +++ b/frontend/core/src/com/gameware/game/GameWare.java @@ -29,14 +29,14 @@ public class GameWare extends ApplicationAdapter { public static final int WIDTH = 480; public static final int HEIGHT = 800; - public static final String TITLE = "WackyWare!"; + public static final String TITLE = "GameWare!"; public static final String skinFilePath = "glassy/skin/glassy-ui.json"; - private static Music music; - private static Boolean musicOn = false; - private static Boolean soundEffects = true; + private Music music; + private Boolean musicOn = false; + private Boolean soundEffects = true; private Boolean includeFin = false; - //private static Boolean darkMode = false; - //private static String darkModeLabelString = "default"; + //private Boolean darkMode = false; + //private String darkModeLabelString = "default"; private Player player; private List<Game> games = new ArrayList<>(); @@ -70,12 +70,6 @@ public class GameWare extends ApplicationAdapter { toggleMusic(); gsm.push(new LoginState(gsm)); -// try{ -// List<Game> games = QueryIntermediate.getGames(); -// System.out.println(games); -// }catch(Exception e){ -// System.out.println(e); -// } } @Override @@ -123,12 +117,6 @@ public class GameWare extends ApplicationAdapter { } */ - @Override - public void dispose () { - batch.dispose(); - music.dispose(); - } - public void setPlayer(Player player){ this.player = player; } @@ -175,4 +163,10 @@ public class GameWare extends ApplicationAdapter { public Boolean getIncludeFin(){ return includeFin; } public void setIncludeFin(Boolean t){ includeFin = t; } + + @Override + public void dispose () { + batch.dispose(); + music.dispose(); + } } diff --git a/frontend/core/src/com/gameware/game/sprites/Bubble.java b/frontend/core/src/com/gameware/game/sprites/Bubble.java index b1f7884496c17c83762f8ebe16cc7a2d900429ad..406b950d7072e2fcdf0dd0334d5b38b488308061 100644 --- a/frontend/core/src/com/gameware/game/sprites/Bubble.java +++ b/frontend/core/src/com/gameware/game/sprites/Bubble.java @@ -3,69 +3,45 @@ package com.gameware.game.sprites; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.TextureRegion; -import com.badlogic.gdx.scenes.scene2d.Actor; -import com.badlogic.gdx.scenes.scene2d.InputEvent; -import com.badlogic.gdx.scenes.scene2d.Touchable; -import com.badlogic.gdx.scenes.scene2d.ui.Image; -import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; -import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.gameware.game.GameWare; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector3; import java.util.ArrayList; import java.util.Random; -public class Bubble extends Actor { +public class Bubble extends Sprite { private float width; private float height; - private int id; - private boolean justTouched = false; + private Vector3 position; private boolean textureChanged = false; - private int timesTouched; private ArrayList<Texture> poppedTextures; private Texture unpoppedTex; private Sound popSound; - // Wrapping Texture in Image because Image extends Widget extends Actor, so it can be used in a table - private Image bubble; + private Texture currentTexture; - public Bubble(float w, float h, int id, Texture unpoppedTex, Texture poppedTex1, Texture poppedTex2, Texture poppedTex3) { - this.width = w; - this.height = h; - this.id = id; - this.timesTouched = 0; + public Bubble(int x, int y, float width, float height, Texture unpoppedTex, Texture poppedTex1, Texture poppedTex2, Texture poppedTex3) { + this.position = new Vector3(x,y,0); + this.width = width; + this.height = height; this.popSound = Gdx.audio.newSound(Gdx.files.internal("sfx/pop.ogg")); - this.unpoppedTex = unpoppedTex; - - bubble = new Image(unpoppedTex); - // Add different textures for popped bubbles this.poppedTextures = new ArrayList<>(); - poppedTextures.add(poppedTex1); poppedTextures.add(poppedTex2); poppedTextures.add(poppedTex3); - bubble.addListener(new ClickListener() { - @Override - public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { - justTouched = true; - timesTouched++; - System.out.println("Bubble " + getId() + " has been popped " + getTimesTouched() + " time(s)."); - setTouchable(Touchable.disabled); - return true; - } - @Override - public void touchUp(InputEvent event, float x, float y, int pointer, int button) { - justTouched = false; - } - }); + this.currentTexture = unpoppedTex; + + //For the reset method. + this.unpoppedTex = unpoppedTex; } - public Image getTexture() { - return bubble; + public boolean isPressed(int x, int y){ + return x > this.position.x && x < (this.position.x + this.width) && (Gdx.graphics.getHeight() - y) > this.position.y && (Gdx.graphics.getHeight() - y) < (this.position.y + this.height); } public void changeToPoppedTexture() { @@ -74,22 +50,14 @@ public class Bubble extends Actor { } Random r = new Random(); - this.bubble.setDrawable(new TextureRegionDrawable(new TextureRegion(poppedTextures.get(r.nextInt(poppedTextures.size()))))); + this.currentTexture = poppedTextures.get(r.nextInt(poppedTextures.size())); textureChanged = true; } - public boolean isJustTouched() { - return justTouched; - } - public boolean isTextureChanged() { return textureChanged; } - public int getTimesTouched() { - return timesTouched; - } - public float getWidth() { return width; } @@ -98,19 +66,26 @@ public class Bubble extends Actor { return height; } - private int getId() { - return id; - } - public void reset() { - justTouched = false; textureChanged = false; - timesTouched = 0; - this.bubble.setDrawable(new TextureRegionDrawable(new TextureRegion(this.unpoppedTex))); + this.currentTexture = unpoppedTex; + } + + @Override + public void draw(SpriteBatch sb) { + Texture drawnTexture = this.currentTexture; + sb.begin(); + sb.draw(drawnTexture,this.position.x,this.position.y,this.width,this.height); + sb.end(); + } + + @Override + public void update(float dt) { } public void dispose() { popSound.dispose(); + // Textures are shared so they should not be disposed } } diff --git a/frontend/core/src/com/gameware/game/sprites/ColorRushButton.java b/frontend/core/src/com/gameware/game/sprites/ColorRushButton.java index 157126639005ae53d599aa622d3eddc014e6d476..5df7d58bed65cd28d86aec0cc3e906e1b8c54396 100644 --- a/frontend/core/src/com/gameware/game/sprites/ColorRushButton.java +++ b/frontend/core/src/com/gameware/game/sprites/ColorRushButton.java @@ -54,8 +54,6 @@ public class ColorRushButton extends Sprite{ @Override public void update(float dt) { - position.x = (int) Math.round(position.x); - position.y = (int) Math.round(position.y); } @Override diff --git a/frontend/core/src/com/gameware/game/sprites/ConfirmationBox.java b/frontend/core/src/com/gameware/game/sprites/ConfirmationBox.java new file mode 100644 index 0000000000000000000000000000000000000000..8d13ff817c522f978f000de415f9bf3ef1eea947 --- /dev/null +++ b/frontend/core/src/com/gameware/game/sprites/ConfirmationBox.java @@ -0,0 +1,67 @@ +package com.gameware.game.sprites; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector3; + +public class ConfirmationBox extends Sprite { + + private Texture boxTexture; + private Vector3 position; + private int width, height; + + public ConfirmationBox(int x, int y, int width, int height){ + this.position = new Vector3(x, y,0); + this.width = width; + this.height = height; + + this.boxTexture = new Texture(Gdx.files.internal("ConfirmationSprite.png")); + } + + @Override + public void reset() { + + } + + @Override + public void draw(SpriteBatch sb) { + sb.begin(); + sb.draw(this.boxTexture, this.position.x, this.position.y, this.width, this.height); + sb.end(); + } + + @Override + public void update(float dt) { + + } + + @Override + public void dispose() { + this.boxTexture.dispose(); + } + + public boolean yesPressed(int xTouch, int yTouch){ + return xTouch > this.position.x && xTouch < (this.position.x + this.width/2) && yTouch > this.position.y && yTouch < (this.position.y + this.height/2); + } + + public boolean noPressed(int xTouch, int yTouch){ + return xTouch > (this.position.x + this.width/2) && xTouch < (this.position.x + this.width) && yTouch > this.position.y && yTouch < (this.position.y + this.height/2); + } + + public int getX(){ + return (int) this.position.x; + } + + public int getY(){ + return (int) this.position.y; + } + + public int getWidth(){ + return this.width; + } + + public int getHeight(){ + return this.height; + } +} diff --git a/frontend/core/src/com/gameware/game/sprites/PauseButton.java b/frontend/core/src/com/gameware/game/sprites/PauseButton.java index 13936591e8af1866c3761c87afd3c57fa56d885f..d7ff0296b9bd97c32f0dd53aa007899b3a21d875 100644 --- a/frontend/core/src/com/gameware/game/sprites/PauseButton.java +++ b/frontend/core/src/com/gameware/game/sprites/PauseButton.java @@ -5,34 +5,38 @@ import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector3; +import javax.xml.soap.Text; + public class PauseButton extends Sprite { private int width; private int height; private Vector3 position; - private Texture mainTexture; + private Texture blackButtonTexture; + private Texture whiteButtonTexture; + private Texture currentActiveTexture; // Customizable constructor - public PauseButton(int x, int y, int width, int height, Texture pauseButtonTex){ + public PauseButton(int x, int y, int width, int height, Texture blackBtnTex, Texture whiteBtnTex){ this.position = new Vector3(x, y, 0); this.width = width; this.height = height; - this.mainTexture = pauseButtonTex; + this.blackButtonTexture = blackBtnTex; + this.whiteButtonTexture = whiteBtnTex; + + this.currentActiveTexture = blackButtonTexture; } // Constructor with default settings (black button) public PauseButton(){ - this(Gdx.graphics.getWidth() - Gdx.graphics.getWidth()/8,Gdx.graphics.getHeight() - Gdx.graphics.getHeight()/13,Gdx.graphics.getWidth()/10,Gdx.graphics.getWidth()/10, new Texture(Gdx.files.internal("glassy/raw/PauseButtonBlack.png"))); - } - - // Constructor with default settings but customizable texture - public PauseButton(Texture pauseButtonTex){ - this(Gdx.graphics.getWidth() - Gdx.graphics.getWidth()/8,Gdx.graphics.getHeight() - Gdx.graphics.getHeight()/13,Gdx.graphics.getWidth()/10,Gdx.graphics.getWidth()/10, pauseButtonTex); + this(Gdx.graphics.getWidth() - Gdx.graphics.getWidth()/8, + Gdx.graphics.getHeight() - Gdx.graphics.getHeight()/13, + Gdx.graphics.getWidth()/10,Gdx.graphics.getWidth()/10, + new Texture(Gdx.files.internal("PauseButtonBlack.png")), + new Texture(Gdx.files.internal("PauseButtonWhite.png"))); } - - @Override public void reset() { @@ -42,7 +46,7 @@ public class PauseButton extends Sprite { public void draw(SpriteBatch sb) { sb.begin(); - sb.draw(this.mainTexture, this.position.x, this.position.y, this.width, this.height); + sb.draw(this.currentActiveTexture, this.position.x, this.position.y, this.width, this.height); sb.end(); } @@ -53,10 +57,23 @@ public class PauseButton extends Sprite { @Override public void dispose() { - this.mainTexture.dispose(); + this.blackButtonTexture.dispose(); + this.whiteButtonTexture.dispose(); } public boolean isPressed(int x, int y){ return x > this.position.x && x < (this.position.x + this.width) && (Gdx.graphics.getHeight() - y) > this.position.y && (Gdx.graphics.getHeight() - y) < (this.position.y + this.height); } + + // The following two methods are used by the minigame-state to change the color of the pause button. + // This can be done during playtime if any future games has a dynamic background and want to + // alternate between using the two different colors on the pause button. + + public void setButtonWhite(){ + this.currentActiveTexture = this.whiteButtonTexture; + } + + public void setButtonBlack(){ + this.currentActiveTexture = this.blackButtonTexture; + } } diff --git a/frontend/core/src/com/gameware/game/sprites/PauseMenuButton.java b/frontend/core/src/com/gameware/game/sprites/PauseMenuButton.java new file mode 100644 index 0000000000000000000000000000000000000000..f116ad9d45c0a3099ea5a9893dfab636bbd1087f --- /dev/null +++ b/frontend/core/src/com/gameware/game/sprites/PauseMenuButton.java @@ -0,0 +1,44 @@ +package com.gameware.game.sprites; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector3; + +public class PauseMenuButton extends Sprite { + private Texture buttonTexture; + private Vector3 position; + private int width, height; + + public PauseMenuButton(Texture buttonTex, int x, int y, int width, int height){ + this.buttonTexture = buttonTex; + this.width = width; + this.height = height; + + this.position = new Vector3(x, y,0); + } + + @Override + public void reset() { + } + + @Override + public void draw(SpriteBatch sb) { + sb.begin(); + sb.draw(this.buttonTexture, this.position.x, this.position.y, this.width, this.height); + sb.end(); + } + + @Override + public void update(float dt) { + + } + + @Override + public void dispose() { + this.buttonTexture.dispose(); + } + + public boolean isPressed(int xTouch, int yTouch){ + return xTouch > this.position.x && xTouch < (this.position.x + this.width) && yTouch > this.position.y && yTouch < (this.position.y + this.height); + } +} diff --git a/frontend/core/src/com/gameware/game/states/BubbleWrapState.java b/frontend/core/src/com/gameware/game/states/BubbleWrapState.java index f7d7a0b0d60cf0ee42fc56d62dff33cd1a390b2b..c2893829c3f35841dc50ca7985fa4e6f46941577 100644 --- a/frontend/core/src/com/gameware/game/states/BubbleWrapState.java +++ b/frontend/core/src/com/gameware/game/states/BubbleWrapState.java @@ -2,171 +2,143 @@ package com.gameware.game.states; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.scenes.scene2d.ui.Container; import com.badlogic.gdx.scenes.scene2d.ui.Label; -import com.badlogic.gdx.scenes.scene2d.ui.Table; +import com.gameware.game.GameWare; import com.gameware.game.sprites.Bubble; -import com.gameware.game.sprites.PauseButton; import java.util.ArrayList; public class BubbleWrapState extends PlayStateTemplate { - private ArrayList<Bubble> bubbles; - private int poppedBubbles; private Texture background; - private float currentTime = 0f; - private float totalTime = totalGameTime; - private Label timeLabel; - private PauseButton pauseButton; private Texture unpopped; private Texture popped1; private Texture popped2; private Texture popped3; + private Label timeLabel; + private ArrayList<Bubble> bubbles = new ArrayList<>(); + private BitmapFont font; + + private int poppedBubbles; + private int score; + private final int bubbleLength = 170; + private final int bubblePadding = 10; + private final int bubbleCount = 54; + private float currentTime = 0f; + private float totalTime = totalGameTime; + public BubbleWrapState(GameStateManager gsm) { super(gsm); + super.setPauseButtonWhite(); + super.screenshot = new Texture(Gdx.files.internal("bubbleWrapPhotoEdit.png")); - unpopped = new Texture(Gdx.files.internal("glassy/raw/bubble_unpopped_1.png")); - popped1 = new Texture(Gdx.files.internal("glassy/raw/bubble_popped_1.png")); - popped2 = new Texture(Gdx.files.internal("glassy/raw/bubble_popped_2.png")); - popped3 = new Texture(Gdx.files.internal("glassy/raw/bubble_popped_3.png")); + //Overrides PlayStateTemplate's gametime, because this game should not be 30 seconds long. + this.totalTime = 20f; + + background = new Texture(Gdx.files.internal("bubblewrap_background.jpg")); + unpopped = new Texture(Gdx.files.internal("bubble_unpopped_1.png")); + popped1 = new Texture(Gdx.files.internal("bubble_popped_1.png")); + popped2 = new Texture(Gdx.files.internal("bubble_popped_2.png")); + popped3 = new Texture(Gdx.files.internal("bubble_popped_3.png")); this.poppedBubbles = 0; - this.background = new Texture(Gdx.files.internal("glassy/raw/bubblewrap_background.jpg")); - this.pauseButton = new PauseButton(new Texture(Gdx.files.internal("glassy/raw/PauseButtonWhite.png"))); + // Label that displays how much time is left this.timeLabel = new Label("Time left: ", skin); - timeLabel.setPosition(Gdx.graphics.getWidth()/8, Gdx.graphics.getHeight() - Gdx.graphics.getHeight()/10); - stage.addActor(timeLabel); + // Changes the label to a white color + font = new BitmapFont(); + font.getData().setScale(Gdx.graphics.getWidth() / GameWare.WIDTH * 2.5f); - // Creates the bubbles, the table to put them in and adds them to the stage as Actors - createBubbles(); - createBubbleWrapLayout(); - } + // Fill the table with bubbles + for(int i = 0; i<9; i++){ + for (int j = 0; j < this.bubbleCount/9; j++) { + bubbles.add(new Bubble(j*(bubbleLength + bubblePadding),i*(bubbleLength + bubblePadding),bubbleLength,bubbleLength,unpopped,popped1,popped2,popped3)); + } + } + + } @Override protected void handleInput() { if (Gdx.input.justTouched()) { - /* Checks if any bubble has been pressed and that it's not added - to the counter more than once */ + + /* Checks if any bubble has been pressed and that it's not added to the counter more than once */ for (Bubble bubble : bubbles) { - if (bubble.isJustTouched() && !bubble.isTextureChanged()) { + if (bubble.isPressed(Gdx.input.getX(),Gdx.input.getY()) && !bubble.isTextureChanged()) { bubble.changeToPoppedTexture(); poppedBubbles++; - System.out.println(poppedBubbles); } } + // Keeps score consistent + this.setScore(this.poppedBubbles); + // Checks if the pause button was pressed - if(this.pauseButton.isPressed(Gdx.input.getX(), Gdx.input.getY())){ - this.gsm.push(new PauseState(this.gsm)); - } + super.checkPause(); } } @Override public void update(float dt) { - Gdx.input.setInputProcessor(stage); this.handleInput(); this.currentTime += dt; timeLabel.setText("Time left: " + Math.round((totalTime - currentTime) * 100.0) / 100.0); - // Game should finish if the time has run out - if (this.currentTime >= this.totalTime) { - System.out.println("Score: " + poppedBubbles); + // Game should finish if the time has run out or all the bubbles are popped. + if ((this.currentTime >= this.totalTime) || poppedBubbles == 54) { handleFinishedGame(); } - // Game should finish if all the bubbles are popped - if (poppedBubbles == 48) { - System.out.println("All bubbles are popped! Score: " + poppedBubbles); - handleFinishedGame(); - } - stage.act(); } - // Stage uses a Batch to draw @Override public void render(SpriteBatch sb) { - stage.getBatch().begin(); - stage.getBatch().draw(this.background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); - stage.getBatch().end(); - stage.draw(); - - this.pauseButton.draw(sb); - - } - - private void createBubbles() { - bubbles = new ArrayList<>(); - int bubCount = 48; - for (int i = 0; i < bubCount; i++) { - bubbles.add(new Bubble(70, 70, i+1, this.unpopped, this.popped1, this.popped2, this.popped3)); - } - } - - private void createBubbleWrapLayout() { - Container<Table> tableContainer = new Container<>(); - float sw = Gdx.graphics.getWidth(); - float sh = Gdx.graphics.getHeight(); - - float cw = sw * 0.8f; - float ch = sh * 0.8f; - - tableContainer.setSize(cw, ch); - tableContainer.setPosition((sw - cw) / 2.0f, (sh - ch) / 2.0f); - tableContainer.fillX(); + sb.begin(); + sb.draw(this.background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + this.font.draw(sb, String.valueOf(Math.round((this.totalTime - this.currentTime) * 100) / 100.0), Gdx.graphics.getWidth()/40,Gdx.graphics.getHeight() - Gdx.graphics.getHeight()/40); + sb.end(); - Table table = new Table(); - table.setFillParent(true); + super.renderPauseButton(sb); - - // Fill the table with bubbles - for(int i = 0; i<8; i++){ - table.row(); - for (int j = i*6; j < (i+1)*6; j++) { - table.add(bubbles.get(j).getTexture()); - stage.addActor(bubbles.get(j)); - } + for (Bubble bubble : this.bubbles){ + bubble.draw(sb); } - tableContainer.setActor(table); - stage.addActor(tableContainer); } - - // TODO: Finish this method when know how to find player id and game id private void handleFinishedGame() { - System.out.println("Game finished, starting new state"); - this.setScore(this.poppedBubbles); + //Score is calculated. One point per bubble popped, pluss three points for each second of remaining time. + score = this.poppedBubbles + Math.round(this.totalTime - this.currentTime)*3; + this.setScore(score); this.gameDone(); return; } @Override public void dispose() { - stage.dispose(); this.unpopped.dispose(); this.popped1.dispose(); this.popped2.dispose(); this.popped3.dispose(); - this.pauseButton.dispose(); - background.dispose(); + super.pauseButton.dispose(); + for(Bubble bubble : bubbles){ bubble.dispose(); } + this.font.dispose(); + this.background.dispose(); } @Override public void reset() { this.poppedBubbles = 0; this.currentTime = 0f; - createBubbles(); - createBubbleWrapLayout(); + for(Bubble bubble : bubbles){ bubble.reset(); } } } diff --git a/frontend/core/src/com/gameware/game/states/ColorRushState.java b/frontend/core/src/com/gameware/game/states/ColorRushState.java index ea3b567a1c442e50a571900806104ca34f6e7812..103a3bbb64031f395f4690cf36aa37aaf72de360 100644 --- a/frontend/core/src/com/gameware/game/states/ColorRushState.java +++ b/frontend/core/src/com/gameware/game/states/ColorRushState.java @@ -7,7 +7,6 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.gameware.game.GameWare; import com.gameware.game.sprites.ColorRushButton; import com.gameware.game.sprites.ColorRushTarget; -import com.gameware.game.sprites.PauseButton; import java.util.ArrayList; import java.util.Collections; @@ -30,17 +29,12 @@ public class ColorRushState extends PlayStateTemplate { private boolean buttonsEnabled = true; private boolean disposeTargetLeft = true; - private PauseButton pauseButton; - public ColorRushState(GameStateManager gsm){ super(gsm); super.screenshot = new Texture(Gdx.files.internal("colorRushPhotoEdit.png")); // Creates the background - this.background = new Texture(Gdx.files.internal("glassy/raw/ColorRushBackground.jpg")); - - // Creates the pause button - this.pauseButton = new PauseButton(); + this.background = new Texture(Gdx.files.internal("ColorRushBackground.jpg")); // Creates the bitmapfont font = new BitmapFont(); @@ -50,12 +44,12 @@ public class ColorRushState extends PlayStateTemplate { // Creates the color textures this.colorTextures = new ArrayList<Texture>(); - this.colorTextures.add(new Texture(Gdx.files.internal("glassy/raw/RedSquare.png"))); - this.colorTextures.add(new Texture(Gdx.files.internal("glassy/raw/BlueSquare.png"))); - this.colorTextures.add(new Texture(Gdx.files.internal("glassy/raw/GreenSquare.png"))); - this.colorTextures.add(new Texture(Gdx.files.internal("glassy/raw/VioletSquare.png"))); + this.colorTextures.add(new Texture(Gdx.files.internal("RedSquare.png"))); + this.colorTextures.add(new Texture(Gdx.files.internal("BlueSquare.png"))); + this.colorTextures.add(new Texture(Gdx.files.internal("GreenSquare.png"))); + this.colorTextures.add(new Texture(Gdx.files.internal("VioletSquare.png"))); - this.disabledColorTexture = new Texture(Gdx.files.internal("glassy/raw/GreySquare.png")); + this.disabledColorTexture = new Texture(Gdx.files.internal("GreySquare.png")); // Randomizes the button arrangement Collections.shuffle(this.colorTextures); @@ -71,6 +65,7 @@ public class ColorRushState extends PlayStateTemplate { buttons.add(new ColorRushButton(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/8,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/8, this.colorTextures.get(3), this.disabledColorTexture, 3)); + // Parameters for the targets int targetWidth = Gdx.graphics.getWidth()/8; int targetHeight = Gdx.graphics.getWidth()/8; int targetX = Gdx.graphics.getWidth()/2 - targetWidth/2; @@ -105,10 +100,11 @@ public class ColorRushState extends PlayStateTemplate { } } - // Checks if the pause button was pressed - if(this.pauseButton.isPressed(Gdx.input.getX(), Gdx.input.getY())){ - this.gsm.push(new PauseState(this.gsm)); - } + // Keeps score consistent + this.setScore(this.targetsHit); + + //Pauses the game if the user pressed the pause button + super.checkPause(); } } @@ -179,7 +175,8 @@ public class ColorRushState extends PlayStateTemplate { sb.end(); - this.pauseButton.draw(sb); + // Renders pause button + super.renderPauseButton(sb); for (ColorRushButton button : this.buttons) { button.draw(sb); @@ -217,6 +214,7 @@ public class ColorRushState extends PlayStateTemplate { this.font.dispose(); this.background.dispose(); this.disabledColorTexture.dispose(); + super.pauseButton.dispose(); } @Override diff --git a/frontend/core/src/com/gameware/game/states/CreateJoinTournamentState.java b/frontend/core/src/com/gameware/game/states/CreateJoinTournamentState.java index 4470f7a41ee6811837a55c158081971f6b380fed..69617221d821bb453359cba4366ab70c1e8ecae7 100644 --- a/frontend/core/src/com/gameware/game/states/CreateJoinTournamentState.java +++ b/frontend/core/src/com/gameware/game/states/CreateJoinTournamentState.java @@ -3,11 +3,9 @@ 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.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.actions.Actions; +import com.badlogic.gdx.scenes.scene2d.actions.DelayAction; import com.badlogic.gdx.scenes.scene2d.ui.Dialog; import com.badlogic.gdx.scenes.scene2d.ui.CheckBox; import com.badlogic.gdx.scenes.scene2d.ui.Label; @@ -15,7 +13,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; 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.Scaling; import com.gameware.game.GameWare; import com.gameware.game.QueryIntermediate; import com.gameware.game.models.Round; @@ -27,70 +25,47 @@ import java.util.List; public class CreateJoinTournamentState extends State { - private Table outerTable; - private Table innerTable; - private ScrollPane tournamentsPane; - private Dialog dialog; - - private Label gameNameText = new Label("Multiplayer", skin, "big"); - private Label stateTitleText = new Label("Currently playing", skin, "big"); - private CheckBox includeCheckBox; - public Label tournamentFeedbackLabel = new Label("", skin); - - private String backBtnText = "Back"; - private String createBtnText = "Create new"; - private String joinBtnText = "Join new"; - private String noTournamentsText = "No tournaments"; - private String nameColText = "Name"; - private String statusColText = "Status"; - private String isPlayedTrueText = "Waiting"; - private String isPlayedFalseText = "Your turn"; - private String isFinishedText = "Finished"; - private String noAvailableTournamentsText = "No available tournaments"; - private String joinedText = "Joined "; - public String leftTournamentText = "Left "; - +// Data private List<Tournament> tournaments = new ArrayList<>(); - private Tournament tournamentTryingToLeave = null; - private int padding = 50; - private String includeFinTournamentsLabelText = "Include finished?"; - - private boolean includeFinishedTournaments = false; - private List<Tournament> finTournaments = new ArrayList<>(); - public class EnterClickListener extends ClickListener{ - private Tournament tournament; - private Round round; - - public EnterClickListener(Tournament tournament, Round round){ - this.tournament = tournament; - this.round = round; - } - - public void clicked(InputEvent event, float x, float y) { - handleEnterBtnClick(tournament, round); - }; - } - public class LeaveClickListener extends ClickListener{ - private Tournament tournament; - - public LeaveClickListener(Tournament tournament){ - this.tournament = tournament; - } +// Labels + private final Label titleLabel = new Label("Multiplayer", skin, "big"); + private final Label subHeadLabel = new Label("Currently playing", skin, "big"); + private final Label noTournamentsLabel = new Label("No tournaments", skin); + private final Label nameColLabel = new Label("Name", skin, "big"); + private final Label statusColLabel = new Label("Status", skin, "big"); + private final Label isFinishedLabel = new Label("Finished", skin); + private final Label includeFinTournamentsLabel = new Label("Include finished?", skin,"big"); + Label tournamentFeedbackLabel = new Label("", skin); + +// Texts + private final String backBtnText = "Back"; + private final String createBtnText = "Create new"; + private final String joinBtnText = "Join new"; + private final String isPlayedTrueText = "Waiting"; + private final String isPlayedFalseText = "Your turn"; + private final String noAvailableTournamentsText = "No available tournaments"; + private final String leaveDialogText = "Are you sure want to\nleave "; + private final String joinedText = "Joined "; + final String leftTournamentText = "Left "; + + +// Variables + private final int padding = 50; + private Tournament tournamentTryingToLeave = null; + private boolean includeFinishedTournaments = GameWare.getInstance().getIncludeFin(); + private final Color scrollPaneBGColor = Color.SKY; + private final float scrollPaneWidth = Gdx.graphics.getWidth()/1.15f; + private final float scrollPaneHeight = Gdx.graphics.getHeight()/4f; - public void clicked(InputEvent event, float x, float y) { - handleLeaveBtnClick(tournament); - }; - } + private Dialog dialog; + private CheckBox includeCheckBox; public CreateJoinTournamentState(GameStateManager gsm) { super(gsm); - - includeFinishedTournaments = GameWare.getInstance().getIncludeFin(); - try{ tournaments = QueryIntermediate.getTournamentsForPlayer(GameWare.getInstance().getPlayer().getId(),true); finTournaments = QueryIntermediate.getTournamentsForPlayer(GameWare.getInstance().getPlayer().getId(),false); @@ -102,48 +77,42 @@ public class CreateJoinTournamentState extends State { } private void makeStage(){ - outerTable = new Table(); - outerTable.setFillParent(true); - outerTable.setBackground(background); - - gameNameText.setFontScale(titleFontBigScale); - - outerTable.add(gameNameText).spaceBottom(spacingOnBottomInputFIeld); - outerTable.row(); - outerTable.add(stateTitleText).spaceBottom(spacingOnBottom); - outerTable.row(); - - innerTable = new Table(); - Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565); - bgPixmap.setColor(Color.SKY); - bgPixmap.fill(); - TextureRegionDrawable textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap))); - innerTable.setBackground(textureRegionDrawableBg); - - createTournamentList(); - - tournamentsPane = new ScrollPane(innerTable, skin); - outerTable.add(tournamentsPane).spaceBottom(spacingOnBottom); - outerTable.getCell(tournamentsPane).size(Gdx.graphics.getWidth()/1.1f, Gdx.graphics.getHeight()/4f); - outerTable.row(); - outerTable.add(tournamentFeedbackLabel).spaceBottom(spacingOnBottom);; - outerTable.row(); + Table rootTable = makeRootTable(); + + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).expandY().top(); + rootTable.row(); + + rootTable.add(subHeadLabel); + rootTable.row(); + + ScrollPane tournamentsPane = new ScrollPane(createTournamentList(), skin); + rootTable.add(tournamentsPane); + rootTable.getCell(tournamentsPane).size(scrollPaneWidth, scrollPaneHeight); + rootTable.row(); + + rootTable.add(tournamentFeedbackLabel); + rootTable.row(); + Table btnTable = new Table(); - btnTable.add(new Label(includeFinTournamentsLabelText,skin,"big")).spaceBottom(spacingOnBottom); + btnTable.add(includeFinTournamentsLabel).spaceBottom(spacingMedium); includeCheckBox = makeIncludeCheckbox(); - btnTable.add(includeCheckBox).spaceBottom(spacingOnBottom); - + btnTable.add(includeCheckBox).spaceBottom(spacingMedium); btnTable.row(); - btnTable.add(makeCreateBtn()).size(buttonWidth, buttonHeight).spaceBottom(spacingOnBottomInputFIeld).spaceRight(spacingOnBottom); - btnTable.add(makeJoinBtn()).size(buttonWidth, buttonHeight).spaceBottom(spacingOnBottomInputFIeld); - outerTable.add(btnTable); - stage.addActor(outerTable); - stage.addActor(makeBackBtn()); + btnTable.add(makeCreateBtn()).size(buttonWidth, buttonHeight).spaceRight(spacingLittle); + btnTable.add(makeJoinBtn()).size(buttonWidth, buttonHeight); + rootTable.add(btnTable); + rootTable.row(); + + rootTable.add(makeBackBtn()).expand().bottom().left(); + + stage.addActor(rootTable); makeDialog(); } +// Make widgets methods private void makeDialog(){ dialog = new Dialog("", skin, "dialog") { public void result(Object obj) { @@ -153,11 +122,11 @@ public class CreateJoinTournamentState extends State { QueryIntermediate.leaveTournament(tournamentTryingToLeave.get_id(), GameWare.getInstance().getPlayer().getId()); //Fjerner alt og legger til alt igjen, aka dårlig måte å gjøre på men fant ingen annen måte - outerTable.remove(); + stage.clear(); + setFeedbackLabelText(leftTournamentText+tournamentTryingToLeave.getName()); makeStage(); - tournamentFeedbackLabel.setText(leftTournamentText+tournamentTryingToLeave.getName()); } catch(Exception e){ - System.out.println(e); + e.printStackTrace(); } } else{ makeDialog(); @@ -166,60 +135,62 @@ public class CreateJoinTournamentState extends State { } }; dialog.text(""); - dialog.button("Yes", true).pad(padding); //sends "true" as the result - dialog.button("No", false).pad(padding); //sends "false" as the result + dialog.button("Yes", true).pad(padding*2); //sends "true" as the result + dialog.button("No", false).pad(padding*2); //sends "false" as the result } - private void createTournamentList(){ + private Table createTournamentList(){ + Table innerTable = new Table(); + innerTable.setBackground(makeColorBackground(scrollPaneBGColor)); + innerTable.defaults().space(spacingLittle); + innerTable.pad(spacingLittle).padRight(spacingLittle); + if(!tournaments.isEmpty()) { - innerTable.add(new Label(nameColText, skin)).spaceBottom(spacingOnBottom).padTop(spacingOnBottom); - innerTable.add(new Label(statusColText, skin)).spaceLeft(spacingOnBottom); - innerTable.row().spaceBottom(spacingOnBottom); + innerTable.add(nameColLabel).expand().top(); + innerTable.add(statusColLabel).expand().top(); + innerTable.row(); + for (Tournament t : tournaments) { Label tournamentLabel = new Label(t.getName(), skin); try{ Round r = QueryIntermediate.getRoundFromTournament(t.get_id(), GameWare.getInstance().getPlayer().getId(), t.getCurrentRound()); tournamentLabel.addListener(new EnterClickListener(t, r)); - innerTable.add(tournamentLabel).expandX().padLeft(spacingOnBottom); + innerTable.add(tournamentLabel); if(r.isPlayed()){ - innerTable.add(new Label(isPlayedTrueText, skin)).spaceLeft(spacingOnBottom); + innerTable.add(new Label(isPlayedTrueText, skin)); }else{ - innerTable.add(new Label(isPlayedFalseText, skin)).spaceLeft(spacingOnBottom); + innerTable.add(new Label(isPlayedFalseText, skin)); } }catch(Exception e){ System.out.println(e); - innerTable.add(new Label("N/A", skin)).spaceLeft(spacingOnBottom); + innerTable.add(new Label("N/A", skin)); } Label leaveBtn = new Label("X", skin, "error"); leaveBtn.setFontScale(4f); leaveBtn.addListener(new LeaveClickListener(t)); - innerTable.add(leaveBtn).padRight(spacingOnBottom).spaceLeft(spacingOnBottom); - innerTable.row().spaceBottom(spacingOnBottom); + innerTable.add(leaveBtn); + innerTable.row(); } //If the include checkbox is pressed, then the finished tournaments will render. if(includeFinishedTournaments){ for (Tournament t : finTournaments) { Label tournamentLabel = new Label(t.getName(), skin); - try{ - Round r = null; - tournamentLabel.addListener(new EnterClickListener(t, r)); - innerTable.add(tournamentLabel).expandX().padLeft(spacingOnBottom); - innerTable.add(new Label(isFinishedText,skin)).spaceLeft(spacingOnBottom); - }catch(Exception e){ - System.out.println(e); - innerTable.add(new Label("N/A", skin)).spaceLeft(spacingOnBottom); - } + tournamentLabel.addListener(new EnterClickListener(t, null)); + innerTable.add(tournamentLabel); + innerTable.add(isFinishedLabel); + Label leaveBtn = new Label("X", skin, "error"); leaveBtn.setFontScale(4f); leaveBtn.addListener(new LeaveClickListener(t)); - innerTable.add(leaveBtn).padRight(spacingOnBottom).spaceLeft(spacingOnBottom); - innerTable.row().spaceBottom(spacingOnBottom); + innerTable.add(leaveBtn); + innerTable.row(); } } }else{ - innerTable.add(new Label(noTournamentsText, skin)); + innerTable.add(noTournamentsLabel); } + return innerTable; } private TextButton makeCreateBtn(){ @@ -252,7 +223,6 @@ public class CreateJoinTournamentState extends State { handleBackBtnClick(); } }); - backBtn.setPosition(backBtnPlacementX, backBtnPlacementY); return backBtn; } @@ -267,57 +237,16 @@ public class CreateJoinTournamentState extends State { makeStage(); } }); - includeCB.getImage().setScale(3); + includeCB.getImage().setScaling(Scaling.fill); + includeCB.getImageCell().size(Gdx.graphics.getWidth()/16); includeCB.setChecked(includeFinishedTournaments); return includeCB; } - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleBackBtnClick(); - } - } - - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); - } - @Override - public void render(SpriteBatch sb) { - stage.draw(); - } - - @Override - public void dispose() { - stage.dispose(); - } - - @Override - public void reset() { - tournaments.clear(); - finTournaments.clear(); - includeCheckBox.setChecked(false); - try { - tournaments = QueryIntermediate.getTournamentsForPlayer(GameWare.getInstance().getPlayer().getId(),true); - finTournaments = QueryIntermediate.getTournamentsForPlayer(GameWare.getInstance().getPlayer().getId(),false); - } catch (IOException e) { - e.printStackTrace(); - } - makeStage(); - } - - - private void handleBackBtnClick(){ - gsm.set(new MenuState(gsm)); - } +// Handle click methods private void handleEnterBtnClick(Tournament t, Round r){ - System.out.println("Entered tournament with id = "+ t.get_id()); if(!t.isActive()){ gsm.set(new FinishedTournamentState(gsm,t)); } @@ -327,29 +256,89 @@ public class CreateJoinTournamentState extends State { } private void handleLeaveBtnClick(Tournament t){ - dialog.text("Do you want to leave\n"+t.getName()+"?").pad(padding); + dialog.text(leaveDialogText+t.getName()+"?").pad(padding); dialog.show(stage); tournamentTryingToLeave = t; } private void handleCreateBtnClick(){ gsm.set(new CreateNewTournamentState(gsm)); - }; + } private void handleJoinBtnClick(){ try { Tournament newT = QueryIntermediate.joinATournament(GameWare.getInstance().getPlayer().getId()); tournaments.add(newT); - tournamentFeedbackLabel.setText(joinedText+newT.getName()); + setFeedbackLabelText(joinedText+newT.getName()); //Fjerner alt og legger til alt igjen, aka dårlig måte å gjøre på men fant ingen annen måte - outerTable.remove(); + stage.clear(); makeStage(); }catch(Exception e){ - System.out.println(e); - tournamentFeedbackLabel.setText(noAvailableTournamentsText); + e.printStackTrace(); + setFeedbackLabelText(noAvailableTournamentsText); + } + } + + private void handleBackBtnClick(){ + gsm.set(new MenuState(gsm)); + } + + +// Click listeners + public class EnterClickListener extends ClickListener{ + private Tournament tournament; + private Round round; + + public EnterClickListener(Tournament tournament, Round round){ + this.tournament = tournament; + this.round = round; + } + + public void clicked(InputEvent event, float x, float y) { + handleEnterBtnClick(tournament, round); + }; + } + + public class LeaveClickListener extends ClickListener{ + private Tournament tournament; + + public LeaveClickListener(Tournament tournament){ + this.tournament = tournament; + } + + public void clicked(InputEvent event, float x, float y) { + handleLeaveBtnClick(tournament); + }; + } + + void setFeedbackLabelText(String text){ + tournamentFeedbackLabel.getColor().a = 1; + tournamentFeedbackLabel.addAction(Actions.sequence( + new DelayAction(6), + Actions.fadeOut(2))); + tournamentFeedbackLabel.setText(text); + } + + @Override + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleBackBtnClick(); } - }; + } + @Override + public void reset() { + tournaments.clear(); + finTournaments.clear(); + includeCheckBox.setChecked(false); + try { + tournaments = QueryIntermediate.getTournamentsForPlayer(GameWare.getInstance().getPlayer().getId(),true); + finTournaments = QueryIntermediate.getTournamentsForPlayer(GameWare.getInstance().getPlayer().getId(),false); + } catch (IOException e) { + e.printStackTrace(); + } + makeStage(); + } } diff --git a/frontend/core/src/com/gameware/game/states/CreateNewTournamentState.java b/frontend/core/src/com/gameware/game/states/CreateNewTournamentState.java index 15e91ba9e7c9faf38823402fc72b521c1949070d..2e0909a25aa3626728960cc53196557cfa356aed 100644 --- a/frontend/core/src/com/gameware/game/states/CreateNewTournamentState.java +++ b/frontend/core/src/com/gameware/game/states/CreateNewTournamentState.java @@ -2,17 +2,19 @@ package com.gameware.game.states; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.ui.CheckBox; +import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Label; +import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; import com.badlogic.gdx.scenes.scene2d.ui.SelectBox; -import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.TextField; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; -import com.badlogic.gdx.utils.Align; +import com.badlogic.gdx.utils.Scaling; import com.gameware.game.GameWare; import com.gameware.game.QueryIntermediate; import com.gameware.game.models.Game; @@ -24,87 +26,142 @@ import java.util.ArrayList; import java.util.List; public class CreateNewTournamentState extends State { - private Table table; - private Label title = new Label("Create new\ntournament", skin, "big"); - private String nameText = "Name"; - private TextField nameInputField; - private String timePerRoundText = "Hours to \n complete each round"; - private SelectBox timePerRoundSelectBox; - private String maxPlayersText = "Max number of players"; - private SelectBox maxPlayersSelectBox; - private String roundsPerGameText = "Round per game"; - private SelectBox roundsPerGameSelectBox; - private String startDelayText = "Hours waiting for more \n players before start"; - private SelectBox startDelaySelectBox; - private Label errorLabel = new Label("", skin, "error"); - private ArrayList<CheckBox> checkBoxes = new ArrayList<>(); - +// Data private List<Game> games; private Tournament tournament; + private ArrayList<CheckBox> checkBoxes = new ArrayList<>(); private List<String> chosenGames = new ArrayList<>(); private Round round; private Tournament tournamentFromDB; - private int inputFieldHeight = Gdx.graphics.getHeight()/15; - private int nameFieldWidth = Gdx.graphics.getWidth()/3; +// Lables + private final Label titleLabel = new Label("Create new\ntournament", skin, "big"); + private final Label nameLabel = new Label("Name", skin); + private final Label timePerRoundLabel = new Label("Hours each round", skin); + private final Label maxPlayersLabel = new Label("Max number \n of players", skin); + private final Label roundsPerGameLabel = new Label("Round per game", skin); + private final Label startDelayLabel = new Label("Hours before start", skin); + private final Label errorLabel = new Label("", skin, "error"); + private final Label subHeadLabel = new Label("Select games", skin, "big"); + +// Texts + private final String backBtnText = "Back"; + private final String createBtnText = "Create"; + private final String nextBtnText = "Next"; + + +// Input fields + private TextField nameInputField = new TextField("",skin); + private SelectBox timePerRoundSelectBox = new SelectBox(skin); + private SelectBox maxPlayersSelectBox = new SelectBox(skin); + private SelectBox roundsPerGameSelectBox = new SelectBox(skin); + private SelectBox startDelaySelectBox = new SelectBox(skin); + +// User inputs + private String nameUserInput = ""; + private int timePerRoundUserInput = 1; + private int maxPlayersUserInput = 2; + private int roundsPerGameUserInput = 1; + private int startDelayUserInput = 1; + +// Variables + private int page = 1; + private final int inputFieldHeight = Gdx.graphics.getHeight()/15; + private final int nameFieldWidth = Gdx.graphics.getWidth()/3; + private final Color scrollPaneBGColor = Color.SKY; + private final float checkBoxSize = Gdx.graphics.getWidth()/15; + private final float scrollPaneWidth = Gdx.graphics.getWidth()/1.3f; + private final float scrollPaneHeight = Gdx.graphics.getHeight()/2.5f; + + private Table rootTable; - private String cancelBtnText = "Cancel"; - private String createBtnText = "Create"; public CreateNewTournamentState(GameStateManager gsm) { super(gsm); - makeStage(); - } - - private void makeStage(){ try { games = GameWare.getInstance().getGames(); } catch (IOException e) { e.printStackTrace(); } - table = new Table(); - table.setFillParent(true); - table.setBackground(background); + timePerRoundSelectBox.setItems(1,2,3,4,5); + maxPlayersSelectBox.setItems(2,3,4,5); + roundsPerGameSelectBox.setItems(1,2,3); + startDelaySelectBox.setItems(1,2,3); + + makeStage(); + } + + private void makeStage(){ + rootTable = makeRootTable(); - title.setFontScale(titleFontBigScale); - table.add(title).colspan(2).spaceBottom(spacingOnBottomInputFIeld); - table.row(); + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).colspan(2).expandY().top(); + rootTable.row(); - table.add(makeLabel(nameText,skin)); - nameInputField = new TextField("",skin); + if(page == 1){ + makePageOne(); + }else if(page == 2){ + makePageTwo(); + } + stage.addActor(rootTable); + } + +// Make widgets methods + private void makePageOne(){ + Table innerTable = new Table(); + innerTable.defaults().space(spacingLittle); + + innerTable.add(nameLabel); nameInputField.setMaxLength(10); - table.add(nameInputField).size(nameFieldWidth, inputFieldHeight).spaceBottom(spacingOnBottom); - table.row(); + innerTable.add(nameInputField).size(nameFieldWidth, inputFieldHeight); + innerTable.row(); + removeKeyPadAtTouch(); - table.add(makeLabel(timePerRoundText,skin)); - timePerRoundSelectBox = new SelectBox(skin); - timePerRoundSelectBox.setItems(1,2,3,4,5); - table.add(timePerRoundSelectBox).spaceBottom(spacingOnBottom); - table.row(); + innerTable.add(timePerRoundLabel); + innerTable.add(timePerRoundSelectBox); + innerTable.row(); - table.add(makeLabel(maxPlayersText,skin)); - maxPlayersSelectBox = new SelectBox(skin); - maxPlayersSelectBox.setItems(2,3,4,5); - table.add(maxPlayersSelectBox).spaceBottom(spacingOnBottom); - table.row(); + innerTable.add(maxPlayersLabel); + innerTable.add(maxPlayersSelectBox); + innerTable.row(); - table.add(makeLabel(roundsPerGameText,skin)); - roundsPerGameSelectBox = new SelectBox(skin); - roundsPerGameSelectBox.setItems(1,2,3); - table.add(roundsPerGameSelectBox).spaceBottom(spacingOnBottom); - table.row(); + innerTable.add(roundsPerGameLabel); + innerTable.add(roundsPerGameSelectBox); + innerTable.row(); - table.add(makeLabel(startDelayText,skin)).spaceBottom(spacingOnBottom); - startDelaySelectBox = new SelectBox(skin); - startDelaySelectBox.setItems(1,2,3); - table.add(startDelaySelectBox).spaceBottom(spacingOnBottom); - table.row(); + innerTable.add(startDelayLabel); + innerTable.add(startDelaySelectBox); + innerTable.row(); + + innerTable.add(errorLabel).colspan(2); + rootTable.add(innerTable).colspan(2); + rootTable.row(); + + rootTable.add(makeBackBtn()).expand().bottom().left(); + + TextButton nextBtn = makeNextBtn(); + rootTable.add(nextBtn).size(buttonWidth, buttonHeight).expand().bottom().right(); + } + + private void makePageTwo(){ + rootTable.add(subHeadLabel).colspan(2); + rootTable.row(); + + Table innerTable = new Table(); + innerTable.pad(spacingLittle); + innerTable.defaults().space(spacingLittle); + innerTable.setBackground(makeColorBackground(scrollPaneBGColor)); for (final Game g : games){ - Label gameLabel = new Label(g.getName(),skin); - table.add(gameLabel).spaceBottom(spacingOnBottom); + innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).screenshot)); + + Table innerInnerTable = new Table(); + innerInnerTable.defaults().space(spacingLittle); + innerInnerTable.add(new Label(g.getName(),skin)); + innerInnerTable.row(); + CheckBox gameCB = new CheckBox("",skin); gameCB.addListener(new ClickListener(){ @Override @@ -117,26 +174,35 @@ public class CreateNewTournamentState extends State { } } }); - gameCB.getImage().setScale(2,2); + if(chosenGames.contains(g.getId())){ + gameCB.setChecked(true); + } + gameCB.getImage().setScaling(Scaling.fill); + gameCB.getImageCell().size(checkBoxSize); checkBoxes.add(gameCB); - table.add(gameCB).spaceBottom(spacingOnBottom); - table.row(); + innerInnerTable.add(gameCB); + + innerTable.add(innerInnerTable); + innerTable.row(); } - table.add(makeCreateBtn()).spaceBottom(spacingOnBottom); - table.add(makeCancelBtn()).spaceBottom(spacingOnBottom); - table.row(); - table.add(errorLabel).spaceBottom(spacingOnBottom); + ScrollPane scrollPane = new ScrollPane(innerTable, skin); + rootTable.add(scrollPane).colspan(2); + rootTable.getCell(scrollPane).size(scrollPaneWidth, scrollPaneHeight); + rootTable.row(); - stage.addActor(table); - } + rootTable.add(errorLabel).colspan(2); + rootTable.row(); + + rootTable.add(makeBackBtn()).expand().bottom().left(); - private Label makeLabel(String text, Skin skin){ - Label label = new Label(text,skin); - label.setAlignment(Align.center); - return label; + TextButton createBtn = makeCreateBtn(); + rootTable.add(createBtn).size(buttonWidth, buttonHeight).expand().bottom().right(); + + stage.addActor(rootTable); } + private TextButton makeCreateBtn() { TextButton createBtn = new TextButton(createBtnText, skin); createBtn.addListener(new ClickListener() { @@ -148,64 +214,57 @@ public class CreateNewTournamentState extends State { return createBtn; } - private TextButton makeCancelBtn() { - TextButton cancelBtn = new TextButton(cancelBtnText, skin); - cancelBtn.addListener(new ClickListener() { + private TextButton makeNextBtn() { + TextButton nextBtn = new TextButton(nextBtnText, skin); + nextBtn.addListener(new ClickListener() { @Override public void clicked(InputEvent e, float x, float y){ - handleCancelBtnClick(); - } - }); - return cancelBtn; - } + page ++; + nameUserInput = getTournamentName(); + timePerRoundUserInput = getTimePerRound(); + maxPlayersUserInput = getMaxPlayers(); + roundsPerGameUserInput = getRoundsPerGame(); + startDelayUserInput = getStartDelay(); - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleCancelBtnClick(); - } - } - - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); - } - - @Override - public void render(SpriteBatch sb) { - stage.draw(); + stage.clear(); + makeStage(); + } + }); + return nextBtn; } - @Override - public void dispose() { - stage.dispose(); + private TextButton makeBackBtn() { + TextButton backBtn = new TextButton(backBtnText, skin); + backBtn.addListener(new ClickListener() { + @Override + public void clicked(InputEvent e, float x, float y){ + handleBackBtnClick(); + } + }); + return backBtn; } - @Override - public void reset() { - errorLabel.setText(""); - nameInputField.setText(""); - timePerRoundSelectBox.setSelected(1); - maxPlayersSelectBox.setSelected(2); - roundsPerGameSelectBox.setSelected(1); - startDelaySelectBox.setSelected(1); - for(CheckBox cb : checkBoxes){ - cb.setChecked(false); +// Handle click methods + private void handleBackBtnClick() { + if(page == 1) { + gsm.set(new CreateJoinTournamentState(gsm)); + }else if (page == 2){ + page --; + + stage.clear(); + makeStage(); + + nameInputField.setText(nameUserInput); + timePerRoundSelectBox.setSelected(timePerRoundUserInput); + maxPlayersSelectBox.setSelected(maxPlayersUserInput); + roundsPerGameSelectBox.setSelected(roundsPerGameUserInput); + startDelaySelectBox.setSelected(startDelayUserInput); } - - } - - - private void handleCancelBtnClick() { - gsm.set(new CreateJoinTournamentState(gsm)); } private void handleCreateBtnClick(){ - if(chosenGames.size() == 0){ + if(chosenGames.isEmpty()){ errorLabel.setText("No games chosen."); return; } @@ -213,9 +272,8 @@ public class CreateNewTournamentState extends State { errorLabel.setText("No name given."); return; } - try { - tournament = new Tournament(GameWare.getInstance().getPlayer().getId(),chosenGames,getTournamentName(),getTimePerRound(),getMaxPlayers(),getRoundsPerGame(),1,chosenGames.size(),getStartDelay()); + tournament = new Tournament(GameWare.getInstance().getPlayer().getId(),chosenGames,nameUserInput, timePerRoundUserInput, maxPlayersUserInput, roundsPerGameUserInput,1,chosenGames.size(),startDelayUserInput); tournamentFromDB = QueryIntermediate.createNewTournament(tournament); round = QueryIntermediate.getRoundFromTournament(tournamentFromDB.get_id(), GameWare.getInstance().getPlayer().getId(), tournamentFromDB.getCurrentRound()); } catch (IOException e) { @@ -242,4 +300,41 @@ public class CreateNewTournamentState extends State { private int getStartDelay(){ return (int) startDelaySelectBox.getSelected(); } + private void removeKeyPadAtTouch(){ + stage.getRoot().addCaptureListener(new InputListener() { + public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { + if (!(event.getTarget() instanceof TextField)){ + Gdx.input.setOnscreenKeyboardVisible(false); + stage.unfocusAll(); + } + return false; + }}); + } + + @Override + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleBackBtnClick(); + } + } + + @Override + public void reset() { + errorLabel.setText(""); + + nameUserInput = ""; + timePerRoundUserInput = 1; + maxPlayersUserInput = 2; + roundsPerGameUserInput = 1; + startDelayUserInput = 1; + + timePerRoundSelectBox.setSelected(timePerRoundUserInput); + maxPlayersSelectBox.setSelected(maxPlayersUserInput); + roundsPerGameSelectBox.setSelected(roundsPerGameUserInput); + startDelaySelectBox.setSelected(startDelayUserInput); + + for(CheckBox cb : checkBoxes){ + cb.setChecked(false); + } + } } \ No newline at end of file diff --git a/frontend/core/src/com/gameware/game/states/FinishedTournamentState.java b/frontend/core/src/com/gameware/game/states/FinishedTournamentState.java index 864a90dfea098e8bfea75a9eb1f0e688f37ed60c..ccbd0686be59fd5559e4ef1a9d2b068e12b24128 100644 --- a/frontend/core/src/com/gameware/game/states/FinishedTournamentState.java +++ b/frontend/core/src/com/gameware/game/states/FinishedTournamentState.java @@ -3,22 +3,15 @@ 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.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.Label; -import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; 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.gameware.game.GameWare; import com.gameware.game.QueryIntermediate; import com.gameware.game.models.Point; -import com.gameware.game.models.Round; import com.gameware.game.models.Tournament; import java.util.ArrayList; @@ -26,88 +19,89 @@ import java.util.List; public class FinishedTournamentState extends State { - private Table table; - private Table hsTable; - private Table btnTable; - private ScrollPane scrollPane; +// Data private Tournament tournament; - private Dialog dialog; + private List<Point> points = new ArrayList<>(); - private int padding = 50; +// Labels + private Label tournamentNameLabel = new Label("N/A", skin,"big"); + private final Label finishedLabel = new Label("Final results", skin,"big"); + private final Label colOneLabel = new Label("Player", skin, "big"); + private final Label colTwoLabel = new Label("Total", skin, "big"); - private Label tournamentNameText; - private String backBtnText = "Back"; - private String leaveBtnText = "Leave"; - private String finishedText = "Final results"; - private String winnerName; +// Texts + private final String backBtnText = "Back"; + private final String leaveBtnText = "Leave"; + private final String winnerText = "Winner: "; + private final String leaveDialogText = "Are you sure want to\nleave "; - private String colOneText = "Player"; - private String colTwoText = "Total"; - private List<Point> points = new ArrayList<>(); +// Variables + private final int padding = 50; + private final Color scrollPaneBGColor = Color.GOLD; + + private Dialog dialog; - public class LeaveClickListener extends ClickListener{ - public void clicked(InputEvent event, float x, float y) { - handleLeaveBtnClick(); - }; - } public FinishedTournamentState(GameStateManager gsm, Tournament tournament) { super(gsm); this.tournament = tournament; - tournamentNameText = new Label(tournament.getName(), skin, "big"); try { + tournamentNameLabel.setText(tournament.getName()); points = QueryIntermediate.getTournamentPoints(tournament.get_id()); }catch(Exception e){ - System.out.println(e); + e.printStackTrace(); } makeStage(); } private void makeStage(){ - table = new Table(); - table.setFillParent(true); - table.setBackground(backgroundFinTourn); - tournamentNameText.setFontScale(tinierTitleFontBigScale); - - table.add(tournamentNameText).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - table.add(new Label(finishedText,skin,"big")).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - - hsTable = new Table(); - Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565); - bgPixmap.setColor(Color.GOLD); - bgPixmap.fill(); - TextureRegionDrawable textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap))); - hsTable.setBackground(textureRegionDrawableBg); - - hsTable.setFillParent(true); - hsTable.add(new Label(colOneText, skin)); - hsTable.add(new Label(colTwoText, skin)).spaceRight(spacingOnBottom); + Table rootTable = makeRootTable(backgroundFinTourn); + rootTable.defaults().spaceBottom(spacingMedium); + + tournamentNameLabel.setFontScale(tinierTitleFontBigScale); + rootTable.add(tournamentNameLabel); + rootTable.row(); + + rootTable.add(finishedLabel); + rootTable.row(); + + rootTable.add(makeHSTable()); + rootTable.row(); + + String winnerName = points.get(0).getName(); + rootTable.add(new Label(winnerText+winnerName,skin,"big")).spaceBottom(spacingLittle); + rootTable.row(); + + Table btnTable = new Table(); + btnTable.add(makeBackBtn()).spaceRight(spacingLittle); + TextButton leaveBtn = new TextButton(leaveBtnText, skin); + leaveBtn.addListener(new LeaveClickListener()); + btnTable.add(leaveBtn); + rootTable.add(btnTable); + + stage.addActor(rootTable); + + makeDialog(); + } + +// Make widgets methods + private Table makeHSTable(){ + Table hsTable = new Table(); + hsTable.pad(padding); + hsTable.defaults().space(spacingLittle); + hsTable.setBackground(makeColorBackground(scrollPaneBGColor)); + + hsTable.add(colOneLabel); + hsTable.add(colTwoLabel); hsTable.row(); + for(Point point : points){ hsTable.add(new Label(point.getName(), skin)); hsTable.add(new Label(point.getTotalPoints()+"", skin)); hsTable.row(); } - scrollPane = new ScrollPane(hsTable, skin); - table.add(scrollPane).spaceBottom(spacingOnBottomInputFIeld).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - winnerName = points.get(0).getName(); - table.add(new Label("Winner: "+winnerName,skin,"big")).spaceBottom(spacingOnBottom); - table.row(); - - btnTable = new Table(); - TextButton leaveBtn = new TextButton(leaveBtnText, skin); - leaveBtn.addListener(new LeaveClickListener()); - btnTable.add(leaveBtn).spaceBottom(spacingOnBottomInputFIeld).spaceRight(spacingOnBottom); - btnTable.add(makeBackBtn()); - table.add(btnTable); - - stage.addActor(table); - - makeDialog(); + return hsTable; } private void makeDialog(){ @@ -117,7 +111,7 @@ public class FinishedTournamentState extends State { try { QueryIntermediate.leaveTournament(tournament.get_id(), GameWare.getInstance().getPlayer().getId()); CreateJoinTournamentState state = new CreateJoinTournamentState(gsm); - state.tournamentFeedbackLabel.setText(state.leftTournamentText+tournament.getName()); + state.setFeedbackLabelText(state.leftTournamentText+tournament.getName()); gsm.set(state); } catch(Exception e){ System.out.println(e); @@ -128,7 +122,7 @@ public class FinishedTournamentState extends State { } }; - dialog.text("Do you want to leave\n"+tournament.getName()+"?").pad(padding); + dialog.text(leaveDialogText+tournament.getName()+"?").pad(padding); dialog.button("Yes", true).pad(padding); //sends "true" as the result dialog.button("No", false).pad(padding); //sends "false" as the result } @@ -144,39 +138,25 @@ public class FinishedTournamentState extends State { return backBtn; } - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleBackBtnClick(); - } - } - - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); +// Handle click methods + private void handleBackBtnClick(){ + gsm.set(new CreateJoinTournamentState(gsm)); } - @Override - public void render(SpriteBatch sb) { - stage.draw(); + private void handleLeaveBtnClick(){ + dialog.show(stage); } - @Override - public void dispose() { - stage.dispose(); + public class LeaveClickListener extends ClickListener{ + public void clicked(InputEvent event, float x, float y) { + handleLeaveBtnClick(); + }; } @Override - public void reset() { } - - private void handleBackBtnClick(){ - gsm.set(new CreateJoinTournamentState(gsm)); - } - - private void handleLeaveBtnClick(){ - dialog.show(stage); + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleBackBtnClick(); + } } } diff --git a/frontend/core/src/com/gameware/game/states/LoginState.java b/frontend/core/src/com/gameware/game/states/LoginState.java index d4721acf8fbed35086a2a77a738598afab5f6ac5..2c2418a1547aa5a936baf4a5fda589aabd847f1d 100644 --- a/frontend/core/src/com/gameware/game/states/LoginState.java +++ b/frontend/core/src/com/gameware/game/states/LoginState.java @@ -4,6 +4,8 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; +import com.badlogic.gdx.scenes.scene2d.actions.Actions; +import com.badlogic.gdx.scenes.scene2d.actions.DelayAction; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; @@ -18,32 +20,64 @@ import java.util.NoSuchElementException; public class LoginState extends State { - private Table table; - - private Label stateNameLabel = new Label("GameWare", skin, "big"); - private Label secondStateNameLabel = new Label("Log in / Create User", skin, "big"); +// Labels + private final Label titleLabel = new Label("GameWare", skin, "big"); + private final Label subHeadLabel = new Label("Log in / Create User", skin, "big"); + private Label errorLabel = new Label("", skin, "error"); +// Input fields private String usernameInputText = "Username"; private TextField usernameInputField; private String passwordInputText = "Password"; private TextField passwordInputField; - private char passwordCharacter = '*'; + private final char passwordCharacter = '*'; + private final int inputFieldWidth = Gdx.graphics.getWidth()/2; + private final int inputFieldHeight = Gdx.graphics.getHeight()/15; - private int inputFieldWidth = Gdx.graphics.getWidth()/2; - private int inputFieldHeight = Gdx.graphics.getHeight()/15; - private String loginBtnText = "Log in"; - private String signUpBtnText = "Sign Up"; +// Button texts + private final String loginBtnText = "Log in"; + private final String signUpBtnText = "Sign Up"; + +// Feedback texts + private final String wrongLoginText = "User not found"; + private final String takenUsernameText = "Username already taken"; + private final String ioExceptionText = "Something went wrong with query"; + +// Feedback label animation + private final int animationDelay = 2; + private final int animationDuration = 1; - private Label errorLabel = new Label("", skin, "error"); - private String wrongLoginText = "User not found"; - private String takenUsernameText = "Username already taken"; - private String ioExceptionText = "Something went wrong with query"; public LoginState(GameStateManager gsm) { super(gsm); makeStage(); } + private void makeStage(){ + Table rootTable = super.makeRootTable(); + +// Add widgets + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).expandY(); + rootTable.row(); + rootTable.add(subHeadLabel).expandY(); + rootTable.row(); + rootTable.add(makeUserInputField()).size(inputFieldWidth, inputFieldHeight); + rootTable.row(); + rootTable.add(makePasswordInputField()).size(inputFieldWidth, inputFieldHeight); + rootTable.row(); + rootTable.add(errorLabel); + rootTable.row(); + rootTable.add(makeLoginBtn()).size(buttonWidth, buttonHeight); + rootTable.row(); + rootTable.add(makeSignUpBtn()).size(buttonWidth, buttonHeight).padBottom(spacingLarge); + + removeKeyPadAtTouch(); + + stage.addActor(rootTable); + } + +// Make widgets methods private TextField makeUserInputField( ){ usernameInputField = new TextField(usernameInputText, skin); usernameInputField.addListener(new ClickListener() { @@ -78,7 +112,6 @@ public class LoginState extends State { private TextButton makeLoginBtn( ){ TextButton loginBtn = new TextButton(loginBtnText, skin); - loginBtn.addListener(new ClickListener() { @Override public void clicked(InputEvent e, float x, float y){ @@ -99,32 +132,73 @@ public class LoginState extends State { return signUpBtn; } - private void makeStage(){ - table = new Table(); - table.setFillParent(true); - table.setBackground(background); - - stateNameLabel.setFontScale(titleFontBigScale); - - table.add(stateNameLabel).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - table.add(secondStateNameLabel).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - table.add(makeUserInputField()).size(inputFieldWidth, inputFieldHeight).spaceBottom(spacingOnBottom); - table.row(); - table.add(makePasswordInputField()).size(inputFieldWidth, inputFieldHeight).spaceBottom(spacingOnBottom); - table.row(); - table.add(errorLabel).spaceBottom(spacingOnBottom); - table.row(); - table.add(makeLoginBtn()).size(buttonWidth, buttonHeight).spaceBottom(spacingOnBottom); - table.row(); - table.add(makeSignUpBtn()).size(buttonWidth, buttonHeight); - removeKeyPadAtTouch(); +// Handle click methods + private void handleLoginBtnClick(){ + String username = usernameInputField.getText(); + String password = passwordInputField.getText(); + try { + Player player = QueryIntermediate.loginPlayer(username, password); + +// Removes keyboard + Gdx.input.setOnscreenKeyboardVisible(false); + stage.unfocusAll(); + + GameWare.getInstance().setPlayer(player); + gsm.set(new MenuState(gsm)); + }catch(Exception e) { + e.printStackTrace(); + +// Add animation + errorLabel.getColor().a = 1; + errorLabel.addAction(Actions.sequence( + new DelayAction(animationDelay), + Actions.fadeOut(animationDuration))); + +// Different feedback text depending on which exception + if (e instanceof NoSuchElementException) { + errorLabel.setText(wrongLoginText); + } else if (e instanceof IOException) { + errorLabel.setText(ioExceptionText); + } else { + errorLabel.setText(e.getMessage()); + } + } + } + + private void handleSignUpBtnClick() { + String username = usernameInputField.getText(); + String password = passwordInputField.getText(); + try { + Player player = QueryIntermediate.createNewPlayer(username, password); + +// Removes keyboard + Gdx.input.setOnscreenKeyboardVisible(false); + stage.unfocusAll(); - stage.addActor(table); + GameWare.getInstance().setPlayer(player); + gsm.set(new MenuState(gsm)); + } catch (Exception e) { + e.printStackTrace(); + +// Add animation + errorLabel.getColor().a = 1; + errorLabel.addAction(Actions.sequence( + new DelayAction(animationDelay), + Actions.fadeOut(animationDuration))); + +// Different feedback text depending on which exception + if (e instanceof NoSuchElementException) { + errorLabel.setText(takenUsernameText); + } else if (e instanceof IOException) { + errorLabel.setText(takenUsernameText); + } else { + errorLabel.setText(e.getMessage()); + } + } } +// Adds listener to stage that removes keyboard on touch private void removeKeyPadAtTouch(){ stage.getRoot().addCaptureListener(new InputListener() { public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { @@ -136,8 +210,6 @@ public class LoginState extends State { }}); } - @Override - protected void handleInput() {} @Override public void update(float dt) { @@ -159,48 +231,4 @@ public class LoginState extends State { usernameInputField.setText(usernameInputText); passwordInputField.setText(passwordInputText); } - - private void handleLoginBtnClick(){ - String username = usernameInputField.getText(); - String password = passwordInputField.getText(); - Player player; - try { - player = QueryIntermediate.loginPlayer(username, password); - Gdx.input.setOnscreenKeyboardVisible(false); - stage.unfocusAll(); - GameWare.getInstance().setPlayer(player); - gsm.set(new MenuState(gsm)); - }catch(NoSuchElementException e){ - System.out.println(e); - errorLabel.setText(wrongLoginText); - }catch(IOException e){ - System.out.println(e); - errorLabel.setText(ioExceptionText); - } catch(Exception e){ - System.out.println(e); - errorLabel.setText(e.getMessage()); - } - } - - private void handleSignUpBtnClick(){ - String username = usernameInputField.getText(); - String password = passwordInputField.getText(); - Player player; - try { - player = QueryIntermediate.createNewPlayer(username, password); - Gdx.input.setOnscreenKeyboardVisible(false); - stage.unfocusAll(); - GameWare.getInstance().setPlayer(player); - gsm.set(new MenuState(gsm)); - }catch(NoSuchElementException e){ - System.out.println(e); - errorLabel.setText(takenUsernameText); - } catch(IOException e){ - System.out.println(e); - errorLabel.setText(takenUsernameText); - } catch(Exception e){ - System.out.println(e); - errorLabel.setText(e.getMessage()); - } - } } diff --git a/frontend/core/src/com/gameware/game/states/MenuState.java b/frontend/core/src/com/gameware/game/states/MenuState.java index ea9fa85f47a6b42fb24a39686900101de7dcc9e7..3bde38674d86979851c19e77d44b706268d71d6a 100644 --- a/frontend/core/src/com/gameware/game/states/MenuState.java +++ b/frontend/core/src/com/gameware/game/states/MenuState.java @@ -2,7 +2,6 @@ package com.gameware.game.states; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Table; @@ -12,45 +11,49 @@ import com.gameware.game.GameWare; public class MenuState extends State{ - private Table table; +// Labels + private final Label titleLabel = new Label("GameWare", skin, "big"); + private final Label subHeadLabel = new Label("Welcome "+ GameWare.getInstance().getPlayer().getName()+"!", skin, "big"); - private Label gameNameText = new Label("GameWare", skin, "big"); - private Label stateTitleText = new Label(GameWare.getInstance().getPlayer().getName(), skin, "big"); - - private String singlePlayerBtnText = "Single player"; - private String multiPlayerBtnText = "Multi player"; - private String highScoreBtnText = "High Scores"; - private String optionBtnText = "Options"; - private String logOutBtnText = "Log out"; +// Button texts + private final String singlePlayerBtnText = "Single player"; + private final String multiPlayerBtnText = "Multi player"; + private final String highScoreBtnText = "High Scores"; + private final String optionBtnText = "Options"; + private final String logOutBtnText = "Log out"; public MenuState(GameStateManager gsm) { super(gsm); makeStage(); } + private void makeStage(){ - table = new Table(); - table.setFillParent(true); - table.setBackground(background); - gameNameText.setFontScale(titleFontBigScale); - - table.add(gameNameText).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - table.add(stateTitleText).spaceBottom(spacingOnBottom); - table.row(); - table.add(makeSinglePlayerBtn()).size(buttonWidth*1.3f, buttonHeight).spaceBottom(spacingOnBottom); - table.row(); - table.add(makeMultiPlayerBtn()).size(buttonWidth*1.3f, buttonHeight).spaceBottom(spacingOnBottomInputFIeld); - table.row(); + Table rootTable = super.makeRootTable(); + +// Add widgets + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).expandY().top(); + rootTable.row(); + rootTable.add(subHeadLabel).expandY(); + rootTable.row(); + rootTable.add(makeSinglePlayerBtn()).size(buttonWidth*1.3f, buttonHeight); + rootTable.row(); + rootTable.add(makeMultiPlayerBtn()).size(buttonWidth*1.3f, buttonHeight).spaceBottom(spacingMedium); + rootTable.row(); + Table innerTable = new Table(); - innerTable.add(makeHighScoreBtn()).size(buttonWidth, buttonHeight).spaceBottom(spacingOnBottomInputFIeld).spaceRight(spacingOnBottom); - innerTable.add(makeOptionsBtn()).size(buttonWidth, buttonHeight).spaceBottom(spacingOnBottomInputFIeld); - table.add(innerTable); + innerTable.add(makeHighScoreBtn()).size(buttonWidth, buttonHeight).spaceBottom(spacingMedium).spaceRight(spacingLittle); + innerTable.add(makeOptionsBtn()).size(buttonWidth, buttonHeight).spaceBottom(spacingMedium); + rootTable.add(innerTable).padBottom(spacingLarge); + + rootTable.row(); + rootTable.add(makeLogOutBtn()).expandX().bottom().left(); - stage.addActor(table); - stage.addActor(makeLogOutBtn()); + stage.addActor(rootTable); } +// Make widgets methods private TextButton makeSinglePlayerBtn(){ TextButton singlePlayerBtn = new TextButton(singlePlayerBtnText, skin); singlePlayerBtn.addListener(new ClickListener() { @@ -71,15 +74,6 @@ public class MenuState extends State{ return multiPlayerBtn; } - private TextButton makeOptionsBtn(){ - TextButton optionsBtn = new TextButton(optionBtnText, skin); - optionsBtn.addListener(new ClickListener() { - @Override - public void clicked(InputEvent e, float x, float y){ handleOptionsBtnClick(); } - }); - return optionsBtn; - } - private TextButton makeHighScoreBtn(){ TextButton highScoreBtn = new TextButton(highScoreBtnText, skin); highScoreBtn.addListener(new ClickListener() { @@ -91,54 +85,43 @@ public class MenuState extends State{ return highScoreBtn; } + private TextButton makeOptionsBtn(){ + TextButton optionsBtn = new TextButton(optionBtnText, skin); + optionsBtn.addListener(new ClickListener() { + @Override + public void clicked(InputEvent e, float x, float y){ handleOptionsBtnClick(); } + }); + return optionsBtn; + } + private TextButton makeLogOutBtn(){ TextButton logOutBtn = new TextButton(logOutBtnText, skin); logOutBtn.addListener(new ClickListener() { @Override public void clicked(InputEvent e, float x, float y){ handleLogOutBtnClick(); } }); - logOutBtn.setPosition(backBtnPlacementX, backBtnPlacementY); return logOutBtn; } - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleLogOutBtnClick(); - } - } - - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); - } - - @Override - public void render(SpriteBatch sb) { stage.draw(); } - - @Override - public void dispose() { stage.dispose(); } - - @Override - public void reset() { } - - private void handleOptionsBtnClick(){ - gsm.set(new OptionsState(gsm)); - } - - private void handleMultiBtnClick(){ - gsm.set(new CreateJoinTournamentState(gsm)); - } +// Handle click methods private void handleSingleBtnClick() { gsm.set(new SinglePlayerSelectGameState(gsm)); } + private void handleMultiBtnClick(){ gsm.set(new CreateJoinTournamentState(gsm)); } + private void handleHighscoreBtnClick() { gsm.set(new ViewHighScoreState(gsm)); } + private void handleOptionsBtnClick(){ gsm.set(new OptionsState(gsm)); } + private void handleLogOutBtnClick(){ gsm.set(new LoginState(gsm)); GameWare.getInstance().setPlayer(null); } + + @Override + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleLogOutBtnClick(); + } + } } diff --git a/frontend/core/src/com/gameware/game/states/OptionsState.java b/frontend/core/src/com/gameware/game/states/OptionsState.java index 501cded67b1a89fef142dea8ff41c887b8642864..c2f0ce629c4a16a146cf9cc806009a179cd00ef0 100644 --- a/frontend/core/src/com/gameware/game/states/OptionsState.java +++ b/frontend/core/src/com/gameware/game/states/OptionsState.java @@ -2,29 +2,26 @@ package com.gameware.game.states; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.ui.CheckBox; import com.badlogic.gdx.scenes.scene2d.ui.Label; -import com.badlogic.gdx.scenes.scene2d.ui.Skin; 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.utils.Scaling; import com.gameware.game.GameWare; -import com.gameware.game.models.Game; public class OptionsState extends State { - private Table table; +// Labels + private final Label titleLabel = new Label("Options", skin, "big"); + private final Label musicToggleLabel = new Label("Music on/off", skin); + private final Label soundEffectToggleLabel = new Label("Sound effects on/off", skin); +// private final Label darkModeToggleLabel = new Label("Dark mode on/off", skin); - private int spacingOnBottom = Gdx.graphics.getHeight()/25; - private Label stateNameLabel = new Label("Options", skin, "big"); +// Texts + private final String backBtnText = "Back"; - private String backBtnText = "Back"; - private String musicToggleLabelText = "Music on/off"; - private String soundEffectToggleLabelText = "Sound effects on/off"; - //private String darkModeToggleLabelText = "Dark mode on/off"; public OptionsState(GameStateManager gsm) { super(gsm); @@ -32,95 +29,46 @@ public class OptionsState extends State { makeStage(); } - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleBackBtnClick(); - } - } - - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); - } - - @Override - public void render(SpriteBatch sb) { - stage.draw(); - } - - @Override - public void dispose() { - stage.dispose(); - } - - @Override - public void reset() { - - /*if(GameWare.getInstance().getDarkMode()){ - GameWare.getInstance().toggleDarkMode(); - } - */ - - if(!GameWare.getInstance().getMusicOn()){ - GameWare.getInstance().toggleMusic(); - } - if(!GameWare.getInstance().getSoundEffects()){ - GameWare.getInstance().toggleSoundEffects(); - } - } - private void makeStage(){ - table = new Table(); - table.setFillParent(true); - table.setBackground(background); +// Make root table + Table rootTable = super.makeRootTable(); + rootTable.defaults().spaceBottom(spacingMedium).expandY(); - stateNameLabel.setFontScale(titleFontBigScale); - table.add(stateNameLabel).spaceBottom(spacingOnBottomInputFIeld); - table.row(); +// Add title + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).top(); + rootTable.row(); - table.row(); - table.add(makeLabel(musicToggleLabelText,skin)).spaceBottom(spacingOnBottom); - table.add(makeMusicCheckBox(skin)).spaceBottom(spacingOnBottom); +// Make inner Table + adds labels and checkboxes + Table innerTable = new Table(); + innerTable.defaults().spaceBottom(spacingMedium); + innerTable.columnDefaults(0).spaceRight(spacingMedium); - table.row(); - table.add(makeLabel(soundEffectToggleLabelText,skin)).spaceBottom(spacingOnBottom).padRight(50); - table.add(makeSoundEffectCheckBox(skin)).spaceBottom(spacingOnBottom); + innerTable.add(musicToggleLabel); + innerTable.add(makeMusicCheckBox()); + innerTable.row(); - //table.row(); - //table.add(makeLabel(darkModeToggleLabelText,skin)).spaceBottom(spacingOnBottom); - //table.add(makeDarkModeCheckBox(skin)).spaceBottom(spacingOnBottom); + innerTable.add(soundEffectToggleLabel); + innerTable.add(makeSoundEffectCheckBox()); + //innerTable.row(); - table.row(); + //innerTable.add(makeLabel(darkModeToggleLabelText,skin)).spaceBottom(spacingLittle); + //innerTable.add(makeDarkModeCheckBox(skin)).spaceBottom(spacingLittle); - stage.addActor(table); - stage.addActor(makeBackBtn()); - } + rootTable.add(innerTable); + rootTable.row(); - private TextButton makeBackBtn(){ - TextButton backBtn = new TextButton(backBtnText, skin); - backBtn.addListener(new ClickListener() { - @Override - public void clicked(InputEvent e, float x, float y){ - handleBackBtnClick(); - } - }); - backBtn.setPosition(backBtnPlacementX, backBtnPlacementY); - return backBtn; - } - private Label makeLabel(String text, Skin skin){ - Label label = new Label(text,skin); - return label; + rootTable.add(makeBackBtn()).expandX().bottom().left(); + + stage.addActor(rootTable); } - private CheckBox makeMusicCheckBox(Skin skin){ +// Make widgets methods + private CheckBox makeMusicCheckBox(){ CheckBox musicToggle = new CheckBox("",skin); musicToggle.getImage().setScaling(Scaling.fill); - musicToggle.getImageCell().size(Gdx.graphics.getWidth()/10); + musicToggle.getImageCell().size(Gdx.graphics.getWidth()/14); if((GameWare.getInstance().getMusicOn() && !musicToggle.isChecked()) || (!GameWare.getInstance().getMusicOn() && musicToggle.isChecked())){ musicToggle.toggle(); } @@ -131,10 +79,10 @@ public class OptionsState extends State { return musicToggle; } - private CheckBox makeSoundEffectCheckBox(Skin skin){ + private CheckBox makeSoundEffectCheckBox(){ CheckBox soundEffectToggle = new CheckBox("",skin); soundEffectToggle.getImage().setScaling(Scaling.fill); - soundEffectToggle.getImageCell().size(Gdx.graphics.getWidth()/10); + soundEffectToggle.getImageCell().size(Gdx.graphics.getWidth()/14); soundEffectToggle.addListener(new ClickListener() { @Override public void clicked(InputEvent e, float x, float y){ GameWare.getInstance().toggleSoundEffects(); } @@ -163,7 +111,44 @@ public class OptionsState extends State { } */ -private void handleBackBtnClick(){ + private TextButton makeBackBtn(){ + TextButton backBtn = new TextButton(backBtnText, skin); + backBtn.addListener(new ClickListener() { + @Override + public void clicked(InputEvent e, float x, float y){ + handleBackBtnClick(); + } + }); + return backBtn; + } + + +// Handle click method + private void handleBackBtnClick(){ gsm.set(new MenuState(gsm)); } + + + @Override + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleBackBtnClick(); + } + } + + @Override + public void reset() { + + /*if(GameWare.getInstance().getDarkMode()){ + GameWare.getInstance().toggleDarkMode(); + } + */ + + if(!GameWare.getInstance().getMusicOn()){ + GameWare.getInstance().toggleMusic(); + } + if(!GameWare.getInstance().getSoundEffects()){ + GameWare.getInstance().toggleSoundEffects(); + } + } } diff --git a/frontend/core/src/com/gameware/game/states/PauseState.java b/frontend/core/src/com/gameware/game/states/PauseState.java index 4ef33b946354ba3d903c1fbd72accfc39fa053e4..1e787044956b07dc7b8c64243b3b50bcc2ddaf07 100644 --- a/frontend/core/src/com/gameware/game/states/PauseState.java +++ b/frontend/core/src/com/gameware/game/states/PauseState.java @@ -1,34 +1,50 @@ package com.gameware.game.states; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.gameware.game.sprites.LoadingCircle; +import com.gameware.game.sprites.ConfirmationBox; import com.gameware.game.sprites.PauseCircle; +import com.gameware.game.sprites.PauseMenuButton; import java.util.ArrayList; import java.util.List; -import sun.awt.image.GifImageDecoder; - public class PauseState extends State { private ShapeRenderer sr; private List<PauseCircle> pauseCircles; private Texture background; private Texture pauseText; - - public PauseState(GameStateManager gsm) { + private Texture dimmingTexture; + private ConfirmationBox confirmationBox; + private PauseMenuButton resumeButton; + private PauseMenuButton exitButton; + private boolean needsConfirmation; + private PlayStateTemplate pausedGame; + + public PauseState(GameStateManager gsm, PlayStateTemplate pausedGame) { super(gsm); - this.background = new Texture(Gdx.files.internal("glassy/raw/PauseBackground.jpg")); - this.pauseText = new Texture(Gdx.files.internal("glassy/raw/PauseText.png")); + this.background = new Texture(Gdx.files.internal("PauseBackground.jpg")); + this.pauseText = new Texture(Gdx.files.internal("PauseText.png")); + this.dimmingTexture = new Texture(Gdx.files.internal("DimmingTexture.png")); this.pauseCircles = new ArrayList<PauseCircle>(); + this.pausedGame = pausedGame; + + int confirmationBoxWidth = Gdx.graphics.getWidth()*7/8; + int confirmationBoxHeight = confirmationBoxWidth/2; + this.confirmationBox = new ConfirmationBox(Gdx.graphics.getWidth()/2 - confirmationBoxWidth/2, Gdx.graphics.getHeight()/2 - confirmationBoxHeight/2, confirmationBoxWidth, confirmationBoxHeight); + this.needsConfirmation = false; + + int buttonWidth = Gdx.graphics.getWidth()/3; + int buttonHeight = buttonWidth/2; + this.resumeButton = new PauseMenuButton(new Texture(Gdx.files.internal("ResumeButton.png")), buttonWidth/3, Gdx.graphics.getHeight()/7, buttonWidth, buttonHeight); + this.exitButton = new PauseMenuButton(new Texture(Gdx.files.internal("ExitButton.png")), buttonWidth + buttonWidth*2/3, Gdx.graphics.getHeight()/7, buttonWidth, buttonHeight); + + for(int i = 0; i<25; i++){ this.pauseCircles.add(new PauseCircle()); } @@ -37,7 +53,33 @@ public class PauseState extends State { @Override protected void handleInput() { if(Gdx.input.justTouched()){ - this.gsm.pop(); + int touchX = Gdx.input.getX(); + int touchY = Gdx.graphics.getHeight() - Gdx.input.getY(); + + if(this.needsConfirmation) { + // User doesn't want to exit after all + if(this.confirmationBox.noPressed(touchX, touchY)) { + this.needsConfirmation = false; + } + + // User confirms the exit, posts the current score + if(this.confirmationBox.yesPressed(touchX, touchY)) { + this.gsm.pop(); + this.pausedGame.gameDone(); + } + } + + else { + // Resumes the game + if(this.resumeButton.isPressed(touchX, touchY)){ + this.gsm.pop(); + } + + // First step of exitting; user now needs to confirm the exit via the popup + if(this.exitButton.isPressed(touchX, touchY)){ + this.needsConfirmation = true; + } + } } } @@ -66,15 +108,31 @@ public class PauseState extends State { } - int scrollWidth = Gdx.graphics.getWidth()/2; - int scrollHeight = Gdx.graphics.getWidth()/4; + // Draws the two bu + this.resumeButton.draw(sb); + this.exitButton.draw(sb); + + int textWidth = Gdx.graphics.getWidth()/2; + int textHeight = textWidth/15*4; sb.begin(); - // Pause scroll - sb.draw(this.pauseText, Gdx.graphics.getWidth()/2 - scrollWidth/2 , Gdx.graphics.getHeight()*3/4, scrollWidth, scrollHeight); + // Pause text + sb.draw(this.pauseText, Gdx.graphics.getWidth()/2 - textWidth/2 , Gdx.graphics.getHeight()*7/10, textWidth, textHeight); + + // Dimming layer that dims everything except the confirmation box when the + // user needs to confirm exit + if(this.needsConfirmation) { + sb.draw(this.dimmingTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + } sb.end(); + + // Needs confirmation if the user has pressed the exit button + if(this.needsConfirmation){ + this.confirmationBox.draw(sb); + } + } @Override @@ -85,6 +143,10 @@ public class PauseState extends State { this.background.dispose(); this.pauseText.dispose(); + this.resumeButton.dispose(); + this.exitButton.dispose(); + this.confirmationBox.dispose(); + this.dimmingTexture.dispose(); } @Override diff --git a/frontend/core/src/com/gameware/game/states/PlayStateTemplate.java b/frontend/core/src/com/gameware/game/states/PlayStateTemplate.java index 83bae43491a5be273acd8d720140af244029f703..1c215a7fd43d3f6f0eb784f6e220343b0cb70c71 100644 --- a/frontend/core/src/com/gameware/game/states/PlayStateTemplate.java +++ b/frontend/core/src/com/gameware/game/states/PlayStateTemplate.java @@ -1,12 +1,15 @@ package com.gameware.game.states; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.gameware.game.GameWare; import com.gameware.game.QueryIntermediate; import com.gameware.game.models.Round; import com.gameware.game.models.RoundCheck; import com.gameware.game.models.Tournament; +import com.gameware.game.sprites.PauseButton; import java.io.IOException; @@ -16,11 +19,19 @@ public abstract class PlayStateTemplate extends State { private Round round = null; private Tournament tournament = null; private Round updatedRound = null; + protected PauseButton pauseButton; protected float totalGameTime = 30f; protected Texture screenshot = null; public PlayStateTemplate(GameStateManager gsm){ super(gsm); + + // Default pause button (black color) + this.pauseButton = new PauseButton(); + } + + public void renderPauseButton(SpriteBatch sb){ + this.pauseButton.draw(sb); } public void setRound(Round r){ @@ -40,24 +51,18 @@ public abstract class PlayStateTemplate extends State { break; } } + RoundCheck rc = null; try { this.postScore(id); - }catch(Exception e){ - System.out.println(e); - } - try { + //Add new instance of the game into the HashMap in GameWare GameWare.getInstance().updateGameMap(id, this.getClass().getConstructor(GameStateManager.class).newInstance(gsm)); - }catch(Exception e){ - e.printStackTrace(); - } - RoundCheck rc = null; - try{ rc = QueryIntermediate.doRoundCheck(tournament.get_id()); }catch(Exception e){ e.printStackTrace(); } + if(round == null) { //Singleplayer gsm.set(new ScoreState(gsm, this.score, new SinglePlayerSelectGameState(gsm))); @@ -91,5 +96,22 @@ public abstract class PlayStateTemplate extends State { updatedRound = QueryIntermediate.putRoundScore(round.get_id(),tournament.get_id(), this.score); } } + + // Checks if the pause button was pressed, and pauses the minigame accordingly + public void checkPause(){ + if(this.pauseButton.isPressed(Gdx.input.getX(), Gdx.input.getY())){ + this.gsm.push(new PauseState(this.gsm, this)); + } + } + + // Changes the color of the pause button to white + public void setPauseButtonWhite(){ + this.pauseButton.setButtonWhite(); + } + + // Changes the color of the pause button back to black if it was previously changed to white + public void setPauseButtonBlack(){ + this.pauseButton.setButtonBlack(); + } } diff --git a/frontend/core/src/com/gameware/game/states/ScoreState.java b/frontend/core/src/com/gameware/game/states/ScoreState.java index 571f2aa387d1a1c7a481a7eb399059718896b1f5..f52184d4999a96ebc56ced0d73d77db74718e0c9 100644 --- a/frontend/core/src/com/gameware/game/states/ScoreState.java +++ b/frontend/core/src/com/gameware/game/states/ScoreState.java @@ -1,48 +1,70 @@ package com.gameware.game.states; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.scenes.scene2d.actions.Actions; +import com.badlogic.gdx.scenes.scene2d.actions.DelayAction; +import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; +import com.badlogic.gdx.scenes.scene2d.ui.Container; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Table; -import com.gameware.game.models.Round; -import com.gameware.game.models.Tournament; +import com.badlogic.gdx.utils.Align; public class ScoreState extends State { - private int score; +// Data private State nextState; + +// Labels + private final Label titleLabel = new Label("Your score:", skin, "big"); + private final Label continueLabel = new Label("Touch to continue", skin); + private Label scoreLabel = new Label("N/A", skin, "big"); + +// Variables private float currentTime = 0f; - private Table table; - private Label titleLabel = new Label("Your score:", skin, "big"); - private Label continueLabel = new Label("Touch to continue", skin); public ScoreState(GameStateManager gsm, int score, State nextState){ super(gsm); - this.score = score; + this.nextState = nextState; + scoreLabel.setText(score+""); + continueLabel.getColor().a = 0; makeStage(); } private void makeStage(){ - table = new Table(); - table.setFillParent(true); - table.setBackground(backgroundScore); - titleLabel.setFontScale(titleFontBigScale); - - table.add(titleLabel).spaceBottom(spacingOnBottom); - table.row(); - Label scoreLabel = new Label(score+"", skin, "big"); - scoreLabel.setFontScale(titleFontBigScale); - table.add(scoreLabel).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - table.add(continueLabel); + Table rootTable = makeRootTable(backgroundScore); - stage.addActor(table); + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).spaceBottom(spacingLarge); + rootTable.row(); + Container scoreContainer = new Container<Label>(scoreLabel); + scoreContainer.setTransform(true); + scoreContainer.align(Align.center); + rootTable.add(scoreContainer).center().spaceBottom(spacingLarge); + rootTable.row(); + rootTable.add(continueLabel); + + stage.addActor(rootTable); + +// Adds animations + scoreContainer.addAction(Actions.sequence( + new DelayAction(1f), + Actions.parallel( + Actions.moveBy(-(titleFontBigScale*35), -(titleFontBigScale*50),2), + Actions.scaleTo(titleFontBigScale*2, titleFontBigScale*2, 2)))); + + continueLabel.addAction(Actions.sequence( + new DelayAction(2), + Actions.forever( + new SequenceAction( + Actions.fadeIn(2), + Actions.fadeOut(2))))); } + private void handleTouch(){ gsm.set(nextState); } @@ -62,19 +84,4 @@ public class ScoreState extends State { } stage.act(dt); } - - @Override - public void render(SpriteBatch sb) { - stage.draw(); - } - - @Override - public void dispose() { - stage.dispose(); - } - - @Override - public void reset() { - - } } diff --git a/frontend/core/src/com/gameware/game/states/SinglePlayerSelectGameState.java b/frontend/core/src/com/gameware/game/states/SinglePlayerSelectGameState.java index 908a6662fa914105d5f9c9e9766ae7d3a8e4bf08..08b9db871f358c4c91c75e33bf43a320d88d4921 100644 --- a/frontend/core/src/com/gameware/game/states/SinglePlayerSelectGameState.java +++ b/frontend/core/src/com/gameware/game/states/SinglePlayerSelectGameState.java @@ -3,10 +3,6 @@ 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.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.Image; import com.badlogic.gdx.scenes.scene2d.ui.Label; @@ -14,9 +10,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; 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.gameware.game.GameWare; -import com.gameware.game.QueryIntermediate; import com.gameware.game.models.Game; import java.io.IOException; @@ -26,15 +20,24 @@ import java.util.Map; public class SinglePlayerSelectGameState extends State { - private Table table; - private Table innerTable; - private ScrollPane scrollPane; +// Data private List<Game> games = new ArrayList<>(); private final String backBtnText = "Back"; + private final String playBtnText = "Play!"; + +// Labels + private final Label titleLabel = new Label("Singleplayer", skin, "big"); + private final Label subHeadLabel = new Label("Select game", skin, "big"); + private final Label noGamesLabel = new Label("Something went wrong with getting the games", skin, "error"); + +// Variables + private final float scrollPaneWidth = Gdx.graphics.getWidth()/1.15f; + private final float scrollPaneHeight = Gdx.graphics.getHeight()/2.5f; + private final int padding = 50; + private final Color scrollPaneBGColor = Color.SKY; + private final float imageWidthAndHeigh = Gdx.graphics.getWidth()/4; - private Label stateNameLabel = new Label("Singleplayer", skin, "big"); - private Label secondStateNameLabel = new Label("Select game", skin, "big"); protected SinglePlayerSelectGameState(GameStateManager gsm) { super(gsm); @@ -47,75 +50,53 @@ public class SinglePlayerSelectGameState extends State { makeStage(); } - private void makeStage(){ - table = new Table(); - table.setFillParent(true); - table.setBackground(background); - stateNameLabel.setFontScale(titleFontBigScale); - - table.add(stateNameLabel).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - table.add(secondStateNameLabel).spaceBottom(spacingOnBottom); - table.row(); - - innerTable = new Table(); - - Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565); - bgPixmap.setColor(Color.SKY); - bgPixmap.fill(); - TextureRegionDrawable textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap))); - innerTable.setBackground(textureRegionDrawableBg); - - for (Game g : games){ - innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).screenshot)).pad(spacingOnBottom); - Table innerInnerTable = new Table(); - innerInnerTable.add(new Label(g.getName(),skin)).spaceBottom(spacingOnBottom); - innerInnerTable.row(); - TextButton gameBtn = new TextButton("Play!", skin); - gameBtn.addListener(new SinglePlayerSelectGameState.MyClickListener(g)); - innerInnerTable.add(gameBtn); - innerTable.add(innerInnerTable); - innerTable.row(); - } + Table rootTable = makeRootTable(); - scrollPane = new ScrollPane(innerTable, skin); - table.add(scrollPane).spaceBottom(spacingOnBottom); - table.getCell(scrollPane).size(Gdx.graphics.getWidth()/1.3f, Gdx.graphics.getHeight()/3f); - table.row(); + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).expandY().top(); + rootTable.row(); - stage.addActor(table); - stage.addActor(makeBackBtn()); - } + rootTable.add(subHeadLabel); + rootTable.row(); - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleBackBtnClick(); - } - } + ScrollPane scrollPane = new ScrollPane(makeInnerTable(), skin); + rootTable.add(scrollPane); + rootTable.getCell(scrollPane).size(scrollPaneWidth, scrollPaneHeight); + rootTable.row(); - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(); - } + rootTable.add(makeBackBtn()).expand().bottom().left(); - @Override - public void render(SpriteBatch sb) { - stage.draw(); + stage.addActor(rootTable); } - @Override - public void dispose() { - stage.dispose(); - } - @Override - public void reset() { +// Make widgets methods + private Table makeInnerTable(){ + Table innerTable = new Table(); + innerTable.setBackground(makeColorBackground(scrollPaneBGColor)); + innerTable.columnDefaults(0).spaceRight(spacingLittle); + + if(games.size()==0){ +// If the try failed, and no games found + innerTable.add(noGamesLabel).pad(padding); + }else { + for (Game g : games) { + innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).screenshot)).width(imageWidthAndHeigh).height(imageWidthAndHeigh).pad(spacingLittle); + Table innerInnerTable = new Table(); + innerInnerTable.add(new Label(g.getName(), skin)).spaceBottom(spacingLittle); + innerInnerTable.row(); + + TextButton gameBtn = new TextButton(playBtnText, skin); + gameBtn.addListener(new SinglePlayerSelectGameState.MyClickListener(g)); + innerInnerTable.add(gameBtn); + + innerTable.add(innerInnerTable); + innerTable.row(); + } + } + return innerTable; } private TextButton makeBackBtn(){ @@ -124,24 +105,19 @@ public class SinglePlayerSelectGameState extends State { @Override public void clicked(InputEvent e, float x, float y){ handleBackBtnClick(); } }); - backBtn.setPosition(backBtnPlacementX, backBtnPlacementY); return backBtn; } - private void handleBackBtnClick(){ - gsm.set(new MenuState(gsm)); - } - private void handleGameBtnClick(State state){ gsm.set(state); } - private static List<Game> getGames() throws IOException { - List<Game> games = QueryIntermediate.getGames(); - return games; + private void handleBackBtnClick(){ + gsm.set(new MenuState(gsm)); } + public class MyClickListener extends ClickListener{ private Game game; @@ -156,4 +132,11 @@ public class SinglePlayerSelectGameState extends State { handleGameBtnClick(s); }; } + + @Override + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleBackBtnClick(); + } + } } diff --git a/frontend/core/src/com/gameware/game/states/State.java b/frontend/core/src/com/gameware/game/states/State.java index f75015d12f4118b228eb2e7ba7974977e0291bae..1fa0a95d444068402950a5a351fd8e580121b991 100644 --- a/frontend/core/src/com/gameware/game/states/State.java +++ b/frontend/core/src/com/gameware/game/states/State.java @@ -9,46 +9,106 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Skin; +import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.gameware.game.GameWare; public abstract class State { - protected OrthographicCamera cam; + +// Data protected GameStateManager gsm; - protected Stage stage; - protected Skin skin; - protected static final float fontScale = 3f; - protected static final float tinierTitleFontBigScale = 1.5f; - protected static final float titleFontBigScale = 2.5f; - protected static final float spacingOnBottom = Gdx.graphics.getHeight()/50; - protected static final float spacingOnBottomInputFIeld = Gdx.graphics.getHeight()/15; - protected static final int buttonWidth = Gdx.graphics.getWidth()/3; - protected static final int buttonHeight = Gdx.graphics.getHeight()/12; - protected static final int backBtnPlacementX = Gdx.graphics.getWidth()/18; - protected static final int backBtnPlacementY = Gdx.graphics.getHeight()/25; + protected Stage stage = new Stage(); + protected final OrthographicCamera cam = new OrthographicCamera(); + protected final Skin skin = new Skin(Gdx.files.internal(GameWare.skinFilePath)); + +// 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; + +// Textures protected TextureRegionDrawable background = new TextureRegionDrawable(new TextureRegion(new Texture("bg1.jpg"))); - protected TextureRegionDrawable backgroundLighter = new TextureRegionDrawable(new TextureRegion(new Texture("bg1_lighter.jpg"))); +// protected TextureRegionDrawable backgroundLighter = new TextureRegionDrawable(new TextureRegion(new Texture("bg1_lighter.jpg"))); protected TextureRegionDrawable backgroundScore = new TextureRegionDrawable(new TextureRegion(new Texture("bg_score.jpg"))); // protected TextureRegionDrawable buttonDisabledDrawable = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("glassy/raw/button-disabled.png")))); protected TextureRegionDrawable backgroundFinTourn = new TextureRegionDrawable(new TextureRegion(new Texture("bg1_finTourn.jpg"))); +// Variables + private boolean firstTimeRunningUpdate = true; protected State(GameStateManager gsm){ this.gsm = gsm; - cam = new OrthographicCamera(); cam.setToOrtho(false, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2); - - stage = new Stage(); - Gdx.input.setInputProcessor(stage); - skin = new Skin(Gdx.files.internal(GameWare.skinFilePath)); skin.getFont("font").getData().setScale(fontScale); + Gdx.input.setInputProcessor(stage); } - protected abstract void handleInput(); - public abstract void update(float dt); - public abstract void render(SpriteBatch sb); - public abstract void dispose(); - public abstract void reset(); +// State methods + protected void handleInput(){ } + + public void update(float dt){ + if(firstTimeRunningUpdate) { + Gdx.input.setInputProcessor(stage); + Gdx.input.setCatchBackKey(true); + firstTimeRunningUpdate = false; + } + this.handleInput(); + stage.act(dt); + } + + public void render(SpriteBatch sb){ + stage.draw(); + } + + public void dispose(){ + stage.dispose(); + } + + public void reset(){} + + +// Other methods: + +// Makes default rootTable + protected Table makeRootTable(){ + Table rootTable = new Table(); + rootTable.setFillParent(true); + rootTable.setBackground(background); + rootTable.pad(rootTablePadding); + rootTable.padTop(rootTablePaddingTop); + rootTable.defaults().space(spacingLittle); + return 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; + } + + 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))); + } } diff --git a/frontend/core/src/com/gameware/game/states/TournamentHighScoreState.java b/frontend/core/src/com/gameware/game/states/TournamentHighScoreState.java index bdb7ee2bf0f8990bd2b660bbe03abae78dedf60d..5938479204f6d34826a093e3735da45eb7fec12f 100644 --- a/frontend/core/src/com/gameware/game/states/TournamentHighScoreState.java +++ b/frontend/core/src/com/gameware/game/states/TournamentHighScoreState.java @@ -3,17 +3,11 @@ 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.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.Label; -import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; 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.gameware.game.QueryIntermediate; import com.gameware.game.models.Point; import com.gameware.game.models.Round; @@ -24,70 +18,77 @@ import java.util.List; public class TournamentHighScoreState extends State{ - private Table table; - private Table innerTable; - private ScrollPane scrollPane; +// Data private Tournament tournament; private Round round; - - private Label stateNameText; - private Label tournamentNameText; - private String tournamentPointsText = "Tournament Points"; - private String colOneText = "Player"; - private String colTwoText = "Total"; - private String colThreeText = "Last Round"; - private String backBtnText = "Back"; private List<Point> points = new ArrayList<>(); +// Labels + private Label titleLabel = new Label("N/A", skin, "big"); + private final Label subHeadLabel = new Label("Tournament Points", skin, "big"); + private final Label colOneLabel = new Label("Player", skin, "big"); + private final Label colTwoLabel = new Label("Total", skin, "big"); + private final Label colThreeLabel = new Label("Last Round", skin, "big"); + +// Texts + private final String backBtnText = "Back"; + +// Variables + private final Color scrollPaneBGColor = Color.SKY; + private final int padding = 50; + + public TournamentHighScoreState(GameStateManager gsm, Tournament tournament, Round r){ super(gsm); this.tournament = tournament; this.round = r; - tournamentNameText = new Label(tournament.getName(), skin, "big"); - stateNameText = new Label(tournamentPointsText, skin, "big"); try { + titleLabel.setText(tournament.getName()); points = QueryIntermediate.getTournamentPoints(tournament.get_id()); }catch(Exception e){ - System.out.println(e); + e.printStackTrace(); } makeStage(); } private void makeStage(){ - table = new Table(); - table.setFillParent(true); - table.setBackground(background); - stateNameText.setFontScale(tinierTitleFontBigScale); - tournamentNameText.setFontScale(tinierTitleFontBigScale); - - table.add(tournamentNameText); - table.row(); - table.add(stateNameText).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - - innerTable = new Table(); - Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565); - bgPixmap.setColor(Color.SKY); - bgPixmap.fill(); - TextureRegionDrawable textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap))); - innerTable.setBackground(textureRegionDrawableBg); - - innerTable.setFillParent(true); - innerTable.add(new Label(colOneText, skin)); - innerTable.add(new Label(colTwoText, skin)).spaceRight(spacingOnBottom); - innerTable.add(new Label(colThreeText, skin)); + Table rootTable = makeRootTable(); + + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel); + rootTable.row(); + + subHeadLabel.setFontScale(tinierTitleFontBigScale); + rootTable.add(subHeadLabel).expandY().top(); + rootTable.row(); + + rootTable.add(makeHighScoreTable()); + rootTable.row(); + + rootTable.add(makeBackBtn()).expand().bottom().left(); + + stage.addActor(rootTable); + } + +// Make widgets methods + private Table makeHighScoreTable(){ + Table innerTable = new Table(); + innerTable.pad(padding); + innerTable.defaults().space(spacingLittle); + innerTable.setBackground(makeColorBackground(scrollPaneBGColor)); + + innerTable.add(colOneLabel).expandX(); + innerTable.add(colTwoLabel).expandX(); + innerTable.add(colThreeLabel).expandX(); innerTable.row(); + for(Point point : points){ innerTable.add(new Label(point.getName(), skin)); innerTable.add(new Label(point.getTotalPoints()+"", skin)); innerTable.add(new Label(point.getLatestPoints()+"", skin)); innerTable.row(); } - scrollPane = new ScrollPane(innerTable, skin); - table.add(scrollPane).spaceBottom(spacingOnBottomInputFIeld).spaceBottom(spacingOnBottomInputFIeld); - - stage.addActor(table); - stage.addActor(makeBackBtn()); + return innerTable; } private TextButton makeBackBtn(){ @@ -98,10 +99,10 @@ public class TournamentHighScoreState extends State{ handleBackBtnClick(); } }); - backBtn.setPosition(backBtnPlacementX, backBtnPlacementY); return backBtn; } +// Handle click methods private void handleBackBtnClick(){ gsm.set(new ViewTournamentState(gsm, tournament, round)); } @@ -113,25 +114,4 @@ public class TournamentHighScoreState extends State{ handleBackBtnClick(); } } - - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); - } - - @Override - public void render(SpriteBatch sb) { - stage.draw(); - } - - @Override - public void dispose() { - stage.dispose(); - } - - @Override - public void reset() { } } diff --git a/frontend/core/src/com/gameware/game/states/ViewHighScoreForGameState.java b/frontend/core/src/com/gameware/game/states/ViewHighScoreForGameState.java index ae36a7bfc04bc0efd431f40b7a6f91498d06cfa5..73f501cc8f6d7b19b05b867ec5b6c10020b8db92 100644 --- a/frontend/core/src/com/gameware/game/states/ViewHighScoreForGameState.java +++ b/frontend/core/src/com/gameware/game/states/ViewHighScoreForGameState.java @@ -3,17 +3,12 @@ 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.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.Label; import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; 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.gameware.game.GameWare; import com.gameware.game.QueryIntermediate; import com.gameware.game.models.Game; @@ -21,85 +16,86 @@ import com.gameware.game.models.Highscore; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; -import static com.gameware.game.models.Highscore.hsValueComparator; - public class ViewHighScoreForGameState extends State { - private Table outerTable; - private Table innerTable; - private ScrollPane hsPane; - private Game game; - private String playerId; - private double playerHS; - private static List<Highscore> highscores = new ArrayList<>(); + +// Data + private String playerId = GameWare.getInstance().getPlayer().getId(); + private List<Highscore> highscores = new ArrayList<>(); + +// Labels + private final Label titleLabel = new Label("Global High Scores", skin, "big"); + private final Label noHSLabel = new Label("No high scores recorded.", skin); + private final Label placeLabel = new Label("Place", skin); + private final Label nameLabel = new Label("Name", skin); + private final Label scoreLabel = new Label("Score", skin); + private final Label playerLabel = new Label("Your score: ", skin); + + private Label gameNameLabel = new Label("N/A", skin, "big"); + private Label playerScoreLabel = new Label("N/A",skin); + +// Texts + private final String backBtnText = "Back"; + +// Variables private Boolean isNoHS = true; - private Label gameNameText; - private Label stateNameText; - private ScrollPane scrollPane; - - private String backBtnText = "Back"; - private String playerLabelText = "Your score: "; - private String noHSLabelText = ""; - private String globalHighScoresText = "Global High Scores"; - private String colOneText = "Place"; - private String colTwoText = "Name"; - private String colThreeText = "Score"; + private final int numerOfPlacesShown = 5; + private final int padding = 50; + private final float scrollPaneWidth = Gdx.graphics.getWidth()/1.15f; + private final float scrollPaneHeight = Gdx.graphics.getWidth()/2.5f; + protected ViewHighScoreForGameState(GameStateManager gsm, Game game) { super(gsm); - this.game = game; - stage.clear(); - highscores.clear(); - - playerId = GameWare.getInstance().getPlayer().getId(); + gameNameLabel.setText(game.getName()); try{ - highscores = getHighscores(game.getId()); + highscores = QueryIntermediate.getHighscoresForGame(game.getId()); isNoHS = false; - } catch(NoSuchElementException e){ + + double playerHS = QueryIntermediate.getHighscoreForPlayerAndGame(playerId,game.getId()).getValue(); + playerScoreLabel.setText(String.valueOf(playerHS)); + } catch(NoSuchElementException | IOException e){ e.printStackTrace(); - isNoHS = true; } - gameNameText = new Label(game.getName(), skin, "big"); - stateNameText = new Label(globalHighScoresText, skin, "big"); - makeStage(); } - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleBackBtnClick(); - } - } + private void makeStage() { + Table rootTable = super.makeRootTable(); + rootTable.defaults().expandY(); - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); - } + titleLabel.setFontScale(tinierTitleFontBigScale); + rootTable.add(titleLabel); + rootTable.row(); - @Override - public void render(SpriteBatch sb) { - stage.draw(); - } + gameNameLabel.setFontScale(tinierTitleFontBigScale); + rootTable.add(gameNameLabel).top(); + rootTable.row(); - @Override - public void dispose() { - stage.dispose(); - } + ScrollPane hsPane = new ScrollPane(createHighScoreList(), skin); + rootTable.add(hsPane); + rootTable.getCell(hsPane).size(scrollPaneWidth, scrollPaneHeight); - @Override - public void reset() { + rootTable.row(); + Table secondInnerTable = new Table(); + secondInnerTable.setBackground(makeColorBackground(Color.SKY)); + secondInnerTable.add(playerLabel).spaceTop(spacingLittle).pad(padding); + secondInnerTable.add(playerScoreLabel).spaceTop(spacingLittle).pad(padding); + rootTable.add(secondInnerTable); + rootTable.row(); + + rootTable.add(makeBackBtn()).expandX().bottom().left(); + + stage.addActor(rootTable); } + +// Make widgets methods private TextButton makeBackBtn(){ TextButton backBtn = new TextButton(backBtnText, skin); backBtn.addListener(new ClickListener() { @@ -108,109 +104,50 @@ public class ViewHighScoreForGameState extends State { handleBackBtnClick(); } }); - backBtn.setPosition(backBtnPlacementX, backBtnPlacementY); return backBtn; } - private void handleBackBtnClick() { - gsm.set(new ViewHighScoreState(gsm)); - } - - private static List<Highscore> getHighscores(String gameId) { - try { - return QueryIntermediate.getHighscoresForGame(gameId); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - private void makeStage() { - outerTable = new Table(); - outerTable.setFillParent(true); - - - outerTable.setBackground(background); - stateNameText.setFontScale(tinierTitleFontBigScale); - gameNameText.setFontScale(tinierTitleFontBigScale); - - outerTable.add(gameNameText); - outerTable.row(); - outerTable.add(stateNameText).spaceBottom(spacingOnBottomInputFIeld); - outerTable.row(); - - innerTable = new Table(); - Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565); - bgPixmap.setColor(Color.SKY); - bgPixmap.fill(); - TextureRegionDrawable textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap))); - innerTable.setBackground(textureRegionDrawableBg); - - innerTable.setFillParent(true); + private Table createHighScoreList() { + Table innerTable = new Table(); + innerTable.setBackground(makeColorBackground(Color.SKY)); + innerTable.defaults().spaceRight(spacingLittle); if(isNoHS){ - noHSLabelText = "No high scores recorded."; - } - else{ - noHSLabelText = ""; - createHighScoreList(); - - } - - Label noHSLabel = new Label(noHSLabelText,skin); + innerTable.add(noHSLabel).pad(padding); + } else { + innerTable.add(placeLabel).top(); + innerTable.add(nameLabel).top(); + innerTable.add(scoreLabel).top(); + innerTable.row(); - hsPane = new ScrollPane(innerTable, skin); - outerTable.add(noHSLabel); - outerTable.row(); - outerTable.add(hsPane).spaceBottom(spacingOnBottomInputFIeld); - outerTable.getCell(hsPane).size(Gdx.graphics.getWidth()/1.1f, Gdx.graphics.getWidth()/2.5f); + int length = Math.min(highscores.size(), numerOfPlacesShown); - outerTable.row(); - Table secondInnerTable = new Table(); - Pixmap secondBgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565); - secondBgPixmap.setColor(Color.SKY); - secondBgPixmap.fill(); - TextureRegionDrawable secondTextureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(secondBgPixmap))); - secondInnerTable.setBackground(secondTextureRegionDrawableBg); - - Label playerLabel = new Label(playerLabelText,skin); - Label playerScoreLabel = new Label(String.valueOf(playerHS),skin); - secondInnerTable.add(playerLabel).spaceTop(spacingOnBottom).pad(50); - secondInnerTable.add(playerScoreLabel).spaceTop(spacingOnBottom).pad(50); - outerTable.add(secondInnerTable); - - stage.addActor(outerTable); - stage.addActor(makeBackBtn()); - } + for (int i = 0; i < length; i++){ + Highscore hs = highscores.get(i); - private void createHighScoreList() { + Label placementLabel = new Label(String.valueOf(i+1)+". ",skin); + Label nameLabel = new Label(hs.getName()+" ",skin); + Label scoreLabel = new Label(String.valueOf(hs.getValue()),skin); - try{ - playerHS = QueryIntermediate.getHighscoreForPlayerAndGame(playerId,game.getId()).getValue(); - } - catch (NoSuchElementException | IOException e){ - e.printStackTrace(); + innerTable.add(placementLabel); + innerTable.add(nameLabel); + innerTable.add(scoreLabel); + innerTable.row(); + } } + return innerTable; + } - innerTable.add(new Label(colOneText, skin)); - innerTable.add(new Label(colTwoText, skin)).spaceRight(spacingOnBottom); - innerTable.add(new Label(colThreeText, skin)); - innerTable.row(); - - int length = Math.min(highscores.size(),5); - - for (int i = 0; i < length; i++){ - Highscore hs = highscores.get(i); - Label placementLabel = new Label(String.valueOf(i+1)+". ",skin); - Label nameLabel = new Label(hs.getName()+" ",skin); - Label scoreLabel = new Label(String.valueOf(hs.getValue()),skin); +// Handle click method + private void handleBackBtnClick() { + gsm.set(new ViewHighScoreState(gsm)); + } - innerTable.add(placementLabel); - innerTable.add(nameLabel); - innerTable.add(scoreLabel); - innerTable.row(); + @Override + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleBackBtnClick(); } } - } \ No newline at end of file diff --git a/frontend/core/src/com/gameware/game/states/ViewHighScoreState.java b/frontend/core/src/com/gameware/game/states/ViewHighScoreState.java index 05f25908c2670975c6e1b25dbff647062a58d67e..91c133dc7773e381f3061e13cbeece44d0d89d0d 100644 --- a/frontend/core/src/com/gameware/game/states/ViewHighScoreState.java +++ b/frontend/core/src/com/gameware/game/states/ViewHighScoreState.java @@ -3,11 +3,9 @@ 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.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 java.util.ArrayList; import java.util.List; import com.badlogic.gdx.scenes.scene2d.ui.Image; @@ -16,23 +14,31 @@ import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; 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.gameware.game.GameWare; import com.gameware.game.models.Game; import java.io.IOException; public class ViewHighScoreState extends State { - private Table table; - private Table innerTable; - private ScrollPane scrollPane; - private List<Game> games; +// Data + private List<Game> games = new ArrayList<>(); + +// Labels + private final Label titleLabel = new Label("High Scores", skin, "big"); + private final Label subHeadLabel = new Label("Select game", skin, "big"); + private final Label noGamesLabel = new Label("Something went wrong with getting the games", skin, "error"); - private String backBtnText = "Back"; - private String BtnText = "View High Score!"; +// Texts + private final String backBtnText = "Back"; + private final String BtnText = "View High Score!"; + +// Values + private final float scrollPaneWidth = Gdx.graphics.getWidth()/1.15f; + private final float scrollPaneHeight = Gdx.graphics.getHeight()/2.5f; + private final Color scrollPaneBGColor = Color.SKY; + private final float imageWidthAndHeigh = Gdx.graphics.getWidth()/4; + private final int padding = 50; - private Label stateNameLabel = new Label("High Scores", skin, "big"); - private Label secondStateNameLabel = new Label("Select game", skin, "big"); public ViewHighScoreState(GameStateManager gsm) { super(gsm); @@ -48,74 +54,52 @@ public class ViewHighScoreState extends State { } private void makeStage(){ - table = new Table(); - table.setFillParent(true); - - table.setBackground(background); - stateNameLabel.setFontScale(titleFontBigScale); - - table.add(stateNameLabel).spaceBottom(spacingOnBottomInputFIeld); - table.row(); - table.add(secondStateNameLabel).spaceBottom(spacingOnBottom); - table.row(); - - innerTable = new Table(); - - Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565); - bgPixmap.setColor(Color.SKY); - bgPixmap.fill(); - TextureRegionDrawable textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap))); - innerTable.setBackground(textureRegionDrawableBg); - - for (Game g : games){ - innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).screenshot)).pad(spacingOnBottom); - Table innerInnerTable = new Table(); - innerInnerTable.add(new Label(g.getName(),skin)).spaceBottom(spacingOnBottom); - innerInnerTable.row(); - TextButton gameBtn = new TextButton(BtnText, skin); - gameBtn.addListener(new ViewHighScoreState.MyClickListener(g)); - innerInnerTable.add(gameBtn); - innerTable.add(innerInnerTable); - innerTable.row(); - } + Table rootTable = super.makeRootTable(); - scrollPane = new ScrollPane(innerTable, skin); - table.add(scrollPane).spaceBottom(spacingOnBottom); - table.getCell(scrollPane).size(Gdx.graphics.getWidth()/1.2f, Gdx.graphics.getHeight()/3f); - table.row(); + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).expandY().top(); + rootTable.row(); - stage.addActor(table); - stage.addActor(makeBackBtn()); - } + rootTable.add(subHeadLabel); + rootTable.row(); - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleBackBtnClick(); - } - } + ScrollPane scrollPane = new ScrollPane(makeInnerTable(), skin); + rootTable.add(scrollPane).spaceBottom(spacingLittle); + rootTable.getCell(scrollPane).size(scrollPaneWidth, scrollPaneHeight); + rootTable.row(); - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); - } + rootTable.add(makeBackBtn()).expandY().bottom().left(); - @Override - public void render(SpriteBatch sb) { - stage.draw(); - } + stage.addActor(rootTable); - @Override - public void dispose() { - stage.dispose(); } - @Override - public void reset() { +// Make widgets methods + private Table makeInnerTable(){ + Table innerTable = new Table(); + innerTable.setBackground(makeColorBackground(scrollPaneBGColor)); + + if(games.size()==0){ +// If the try failed, and no games found + innerTable.add(noGamesLabel).pad(padding); + }else { + for (Game g : games) { + innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).screenshot)).width(imageWidthAndHeigh).height(imageWidthAndHeigh).pad(spacingLittle); + + Table innerInnerTable = new Table(); + innerInnerTable.add(new Label(g.getName(), skin)).spaceBottom(spacingLittle); + innerInnerTable.row(); + + TextButton gameBtn = new TextButton(BtnText, skin); + gameBtn.addListener(new ViewHighScoreState.MyClickListener(g)); + innerInnerTable.add(gameBtn); + + innerTable.add(innerInnerTable); + innerTable.row(); + } + } + return innerTable; } private TextButton makeBackBtn(){ @@ -124,10 +108,11 @@ public class ViewHighScoreState extends State { @Override public void clicked(InputEvent e, float x, float y){ handleBackBtnClick(); } }); - backBtn.setPosition(backBtnPlacementX, backBtnPlacementY); return backBtn; } + +// Handle click methods private void handleBackBtnClick(){ gsm.set(new MenuState(gsm)); } @@ -136,6 +121,8 @@ public class ViewHighScoreState extends State { gsm.set(new ViewHighScoreForGameState(gsm, game)); } + +// Listener public class MyClickListener extends ClickListener{ private Game game; @@ -145,7 +132,14 @@ public class ViewHighScoreState extends State { public void clicked(InputEvent event, float x, float y) { handleGameBtnClick(game); + } + } - }; + + @Override + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleBackBtnClick(); + } } } \ No newline at end of file diff --git a/frontend/core/src/com/gameware/game/states/ViewTournamentState.java b/frontend/core/src/com/gameware/game/states/ViewTournamentState.java index 7921084c889773ffd6a249a2e6da22067ad07713..0f22b493a0a284466069b5744fda712fc7b5bf86 100644 --- a/frontend/core/src/com/gameware/game/states/ViewTournamentState.java +++ b/frontend/core/src/com/gameware/game/states/ViewTournamentState.java @@ -3,10 +3,10 @@ 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.scenes.scene2d.InputEvent; -import com.badlogic.gdx.scenes.scene2d.Touchable; +import com.badlogic.gdx.scenes.scene2d.actions.Actions; +import com.badlogic.gdx.scenes.scene2d.actions.DelayAction; import com.badlogic.gdx.scenes.scene2d.ui.Dialog; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Label; @@ -20,71 +20,79 @@ import com.gameware.game.models.Tournament; public class ViewTournamentState extends State { - private Table table; +// Data private Tournament tournament; private Round round; - private Dialog dialog; - private int padding = 50; +// Labels + private Label disabledPlayBtnFeedback = new Label("Waiting for other players to finish", skin, "big"); + private Label titleLabel = new Label("N/A", skin, "big"); - private Label stateNameText; - private String backBtnText = "Back"; - private String leaveBtnText = "Leave"; - private String playBtnText = "Play Round"; - private String viewPointsBtnText = "View Points"; - private String nrPlayersText = "Players in tournament:"; - private String roundsLeftText = "Rounds left:"; +// Texts + private final String backBtnText = "Back"; + private final String leaveBtnText = "Leave"; + private final String playBtnText = "Play Round"; + private final String viewPointsBtnText = "View Points"; + private final String nrPlayersText = "Players in tournament:"; + private final String roundsLeftText = "Rounds left:"; + private final String leaveDialogText = "Are you sure want to\nleave "; - public class LeaveClickListener extends ClickListener{ +// Variables + private final int padding = 50; + private final float playBtnWidth = buttonWidth*1.4f; + private final float playBtnHeight = buttonHeight*1.2f; + + private Dialog dialog; - public void clicked(InputEvent event, float x, float y) { - handleLeaveBtnClick(); - }; - } public ViewTournamentState(GameStateManager gsm, Tournament tournament, Round r) { super(gsm); this.tournament = tournament; this.round = r; - stateNameText = new Label(tournament.getName(), skin, "big"); + titleLabel.setText(tournament.getName()); makeStage(); } private void makeStage(){ - table = new Table(); - table.setFillParent(true); - table.setBackground(background); - stateNameText.setFontScale(tinierTitleFontBigScale); + Table rootTable = makeRootTable(); + + titleLabel.setFontScale(titleFontBigScale); + rootTable.add(titleLabel).expandY().top(); + rootTable.row(); - table.add(stateNameText).spaceBottom(spacingOnBottomInputFIeld); - table.row(); + disabledPlayBtnFeedback.getColor().a = 0; + rootTable.add(disabledPlayBtnFeedback); + rootTable.row(); - table.add(makePlayBtn()).size(buttonWidth*1.4f, buttonHeight*1.2f).spaceBottom(spacingOnBottom); + rootTable.add(makePlayBtn()).size(playBtnWidth, playBtnHeight); + rootTable.row(); - table.row(); PlayStateTemplate state = GameWare.getInstance().getGameIdToPlayState().get(round.getGameId()); - table.add(new Image(state.screenshot)).spaceBottom(spacingOnBottomInputFIeld); + rootTable.add(new Image(state.screenshot)).spaceBottom(spacingMedium); + rootTable.row(); - table.row(); - table.add(new Label(nrPlayersText+" "+tournament.getPlayers().size(), skin)); - table.row(); - table.add(new Label(roundsLeftText+" "+((tournament.getRoundsPerGame()*tournament.getGames().size())-tournament.getCurrentRound()+1), skin)).spaceBottom(spacingOnBottomInputFIeld); - table.row(); + rootTable.add(new Label(nrPlayersText+" "+tournament.getPlayers().size(), skin)); + rootTable.row(); + + rootTable.add(new Label(roundsLeftText+" "+((tournament.getRoundsPerGame()*tournament.getGames().size())-tournament.getCurrentRound()+1), skin)); + rootTable.row(); Table innerTable = new Table(); TextButton leaveBtn = new TextButton(leaveBtnText, skin); leaveBtn.addListener(new LeaveClickListener()); - innerTable.add(leaveBtn).size(buttonWidth, buttonHeight).spaceBottom(spacingOnBottomInputFIeld).spaceRight(spacingOnBottom); - innerTable.add(makeViewPointsBtn()).size(buttonWidth, buttonHeight).spaceBottom(spacingOnBottomInputFIeld); - table.add(innerTable); + innerTable.add(leaveBtn).size(buttonWidth, buttonHeight).spaceRight(spacingLittle); + innerTable.add(makeViewPointsBtn()).size(buttonWidth, buttonHeight); + rootTable.add(innerTable); + rootTable.row(); - stage.addActor(table); - stage.addActor(makeBackBtn()); + rootTable.add(makeBackBtn()).expand().bottom().left(); + stage.addActor(rootTable); makeDialog(); } +// Make widgets methods private void makeDialog(){ dialog = new Dialog("", skin, "dialog") { public void result(Object obj) { @@ -92,7 +100,7 @@ public class ViewTournamentState extends State { try { QueryIntermediate.leaveTournament(tournament.get_id(), GameWare.getInstance().getPlayer().getId()); CreateJoinTournamentState state = new CreateJoinTournamentState(gsm); - state.tournamentFeedbackLabel.setText(state.leftTournamentText+tournament.getName()); + state.setFeedbackLabelText(state.leftTournamentText+tournament.getName()); gsm.set(state); } catch(Exception e){ System.out.println(e); @@ -103,7 +111,7 @@ public class ViewTournamentState extends State { } }; - dialog.text("Do you want to leave\n"+tournament.getName()+"?").pad(padding); + dialog.text(leaveDialogText+tournament.getName()+"?").pad(padding); dialog.button("Yes", true).pad(padding); //sends "true" as the result dialog.button("No", false).pad(padding); //sends "false" as the result } @@ -116,7 +124,6 @@ public class ViewTournamentState extends State { handleBackBtnClick(); } }); - backBtn.setPosition(backBtnPlacementX, backBtnPlacementY); return backBtn; } @@ -125,14 +132,20 @@ public class ViewTournamentState extends State { if(round.isPlayed()){ TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); textButtonStyle.up = skin.newDrawable("button", Color.GRAY); - textButtonStyle.down = skin.newDrawable("button", Color.DARK_GRAY); - textButtonStyle.checked = skin.newDrawable("button", Color.DARK_GRAY); - textButtonStyle.over = skin.newDrawable("button", Color.LIGHT_GRAY); + textButtonStyle.down = skin.newDrawable("button", Color.GRAY); textButtonStyle.font = skin.getFont("font-big"); textButtonStyle.fontColor = Color.LIGHT_GRAY; playBtn = new TextButton(playBtnText, textButtonStyle); - playBtn.setTouchable(Touchable.disabled); + playBtn.addListener(new ClickListener() { + @Override + public void clicked(InputEvent e, float x, float y){ + disabledPlayBtnFeedback.getColor().a = 1; + disabledPlayBtnFeedback.addAction(Actions.sequence( + new DelayAction(2), + Actions.fadeOut(1))); + } + }); } else{ playBtn = new TextButton(playBtnText, skin); @@ -157,34 +170,8 @@ public class ViewTournamentState extends State { return viewPoints; } - @Override - protected void handleInput() { - if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ - handleBackBtnClick(); - } - } - - @Override - public void update(float dt) { - Gdx.input.setInputProcessor(stage); - Gdx.input.setCatchBackKey(true); - handleInput(); - stage.act(dt); - } - - @Override - public void render(SpriteBatch sb) { - stage.draw(); - } - - @Override - public void dispose() { - stage.dispose(); - } - - @Override - public void reset() { } +// Handle click methods private void handleBackBtnClick(){ gsm.set(new CreateJoinTournamentState(gsm)); } @@ -203,4 +190,20 @@ public class ViewTournamentState extends State { private void handleLeaveBtnClick(){ dialog.show(stage); } + + +// Listener + public class LeaveClickListener extends ClickListener{ + + public void clicked(InputEvent event, float x, float y) { + handleLeaveBtnClick(); + }; + } + + @Override + protected void handleInput() { + if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)){ + handleBackBtnClick(); + } + } }