Add fully-functional pause menu, move skin creation

This commit is contained in:
Lonami Exo 2017-01-27 19:36:17 +01:00
parent 2d6c66a701
commit b6b7590cf4
7 changed files with 200 additions and 35 deletions

BIN
android/assets/ui/home.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
android/assets/ui/share.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -1,17 +1,37 @@
package io.github.lonamiwebs.klooni;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import io.github.lonamiwebs.klooni.screens.MainMenuScreen;
public class Klooni extends Game {
public static Skin skin;
public Skin skin;
@Override
public void create() {
//skin = new Skin(Gdx.files.internal("skin/craftacular-ui.json"));
// TODO Better way to have this skin somewhere
// Gotta create that darn .json!
skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
skin.add("button_up", new NinePatch(new Texture(
Gdx.files.internal("ui/button_up.png")), 28, 28, 28, 28));
skin.add("button_down", new NinePatch(new Texture(
Gdx.files.internal("ui/button_down.png")), 28, 28, 28, 28));
skin.add("play_texture", new Texture(Gdx.files.internal("ui/play.png")));
skin.add("star_texture", new Texture(Gdx.files.internal("ui/star.png")));
skin.add("stats_texture", new Texture(Gdx.files.internal("ui/stats.png")));
skin.add("palette_texture", new Texture(Gdx.files.internal("ui/palette.png")));
skin.add("home_texture", new Texture(Gdx.files.internal("ui/home.png")));
skin.add("replay_texture", new Texture(Gdx.files.internal("ui/replay.png")));
skin.add("share_texture", new Texture(Gdx.files.internal("ui/share.png")));
setScreen(new MainMenuScreen(this));
}
@ -23,5 +43,6 @@ public class Klooni extends Game {
@Override
public void dispose() {
super.dispose();
skin.dispose();
}
}

View file

@ -1,12 +1,12 @@
package io.github.lonamiwebs.klooni.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.game.Board;
@ -30,22 +30,21 @@ public class GameScreen implements Screen, InputProcessor {
private final Color clearColor;
private int score;
private final PauseMenuStage pauseMenu;
public GameScreen(Klooni aGame) {
game = aGame;
score = 0;
clearColor = new Color(0.9f, 0.9f, 0.7f, 1f);
batch = new SpriteBatch();
pauseMenu = new PauseMenuStage(game);
layout = new GameLayout();
scorer = new Scorer(layout, 10);
board = new Board(layout, 10);
holder = new PieceHolder(layout, 3);
// Fill some random pieces
for (int i = 0; i < 10; i++) {
board.putPiece(Piece.random(), MathUtils.random(10), MathUtils.random(10));
}
}
boolean isGameOver() {
@ -77,6 +76,11 @@ public class GameScreen implements Screen, InputProcessor {
holder.draw(batch, board.cellPatch);
batch.end();
if (pauseMenu.isShown() || pauseMenu.isHiding()) {
pauseMenu.act(delta);
pauseMenu.draw();
}
}
@Override
@ -101,7 +105,7 @@ public class GameScreen implements Screen, InputProcessor {
@Override
public void dispose() {
pauseMenu.dispose();
}
//endregion
@ -115,6 +119,9 @@ public class GameScreen implements Screen, InputProcessor {
@Override
public boolean keyUp(int keycode) {
if (keycode == Input.Keys.P) // Pause
pauseMenu.show();
return false;
}
@ -137,7 +144,7 @@ public class GameScreen implements Screen, InputProcessor {
// After the piece was put, check if it's game over
if (isGameOver()) {
clearColor.set(0.4f, 0.1f, 0.1f, 1f);
pauseMenu.show();
}
return true;
} else {

View file

@ -4,13 +4,10 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
@ -19,7 +16,6 @@ import io.github.lonamiwebs.klooni.Klooni;
public class MainMenuScreen implements Screen {
private Klooni game;
Skin skin;
Stage stage;
SpriteBatch batch;
@ -28,27 +24,16 @@ public class MainMenuScreen implements Screen {
batch = new SpriteBatch();
stage = new Stage();
skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
Table table = new Table();
table.setFillParent(true);
stage.addActor(table);
skin.add("button_up", new NinePatch(new Texture(
Gdx.files.internal("ui/button_up.png")), 28, 28, 28, 28));
skin.add("button_down", new NinePatch(new Texture(
Gdx.files.internal("ui/button_down.png")), 28, 28, 28, 28));
skin.add("play_texture", new Texture(Gdx.files.internal("ui/play.png")));
skin.add("star_texture", new Texture(Gdx.files.internal("ui/star.png")));
skin.add("stats_texture", new Texture(Gdx.files.internal("ui/stats.png")));
skin.add("palette_texture", new Texture(Gdx.files.internal("ui/palette.png")));
// Play button
ImageButton.ImageButtonStyle playStyle = new ImageButton.ImageButtonStyle(
skin.newDrawable("button_up", Color.GREEN), skin.newDrawable("button_down", Color.GREEN),
null, skin.getDrawable("play_texture"), null, null);
game.skin.newDrawable("button_up", Color.GREEN),
game.skin.newDrawable("button_down", Color.GREEN),
null, game.skin.getDrawable("play_texture"), null, null);
final ImageButton playButton = new ImageButton(playStyle);
table.add(playButton).colspan(3).fill().space(16);
@ -64,24 +49,27 @@ public class MainMenuScreen implements Screen {
// Star button (on GitHub)
ImageButton.ImageButtonStyle starStyle = new ImageButton.ImageButtonStyle(
skin.newDrawable("button_up", Color.YELLOW), skin.newDrawable("button_down", Color.YELLOW),
null, skin.getDrawable("star_texture"), null, null);
game.skin.newDrawable("button_up", Color.YELLOW),
game.skin.newDrawable("button_down", Color.YELLOW),
null, game.skin.getDrawable("star_texture"), null, null);
final ImageButton starButton = new ImageButton(starStyle);
table.add(starButton).space(16);
// Stats button (high scores)
ImageButton.ImageButtonStyle statsStyle = new ImageButton.ImageButtonStyle(
skin.newDrawable("button_up", Color.BLUE), skin.newDrawable("button_down", Color.BLUE),
null, skin.getDrawable("stats_texture"), null, null);
game.skin.newDrawable("button_up", Color.BLUE),
game.skin.newDrawable("button_down", Color.BLUE),
null, game.skin.getDrawable("stats_texture"), null, null);
final ImageButton statsButton = new ImageButton(statsStyle);
table.add(statsButton).space(16);
// Palette button (buy colors)
ImageButton.ImageButtonStyle paletteStyle = new ImageButton.ImageButtonStyle(
skin.newDrawable("button_up", Color.FIREBRICK), skin.newDrawable("button_down", Color.FIREBRICK),
null, skin.getDrawable("palette_texture"), null, null);
game.skin.newDrawable("button_up", Color.FIREBRICK),
game.skin.newDrawable("button_down", Color.FIREBRICK),
null, game.skin.getDrawable("palette_texture"), null, null);
final ImageButton paletteButton = new ImageButton(paletteStyle);
table.add(paletteButton).space(16);
@ -125,6 +113,5 @@ public class MainMenuScreen implements Screen {
@Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}

View file

@ -0,0 +1,150 @@
package io.github.lonamiwebs.klooni.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.RunnableAction;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import io.github.lonamiwebs.klooni.Klooni;
public class PauseMenuStage extends Stage {
private InputProcessor lastInputProcessor;
private boolean shown;
private boolean hiding;
public PauseMenuStage(final Klooni game) {
Table table = new Table();
table.setFillParent(true);
addActor(table);
// Home screen button
ImageButton.ImageButtonStyle homeStyle = new ImageButton.ImageButtonStyle(
game.skin.newDrawable("button_up", Color.FIREBRICK),
game.skin.newDrawable("button_down", Color.FIREBRICK),
null, game.skin.getDrawable("home_texture"), null, null);
final ImageButton homeButton = new ImageButton(homeStyle);
table.add(homeButton).space(16);
homeButton.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
game.setScreen(new MainMenuScreen(game));
dispose();
}
});
// Replay button
ImageButton.ImageButtonStyle replayStyle = new ImageButton.ImageButtonStyle(
game.skin.newDrawable("button_up", Color.GREEN),
game.skin.newDrawable("button_down", Color.GREEN),
null, game.skin.getDrawable("replay_texture"), null, null);
final ImageButton replayButton = new ImageButton(replayStyle);
table.add(replayButton).space(16);
replayButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new GameScreen(game));
dispose();
}
});
table.row();
// Palette button (buy colors)
ImageButton.ImageButtonStyle paletteStyle = new ImageButton.ImageButtonStyle(
game.skin.newDrawable("button_up", Color.YELLOW),
game.skin.newDrawable("button_down", Color.YELLOW),
null, game.skin.getDrawable("palette_texture"), null, null);
final ImageButton paletteButton = new ImageButton(paletteStyle);
table.add(paletteButton).space(16);
// Continue playing OR share (if game over) button
// TODO Enable both actions for this button
ImageButton.ImageButtonStyle playStyle = new ImageButton.ImageButtonStyle(
game.skin.newDrawable("button_up", Color.BLUE),
game.skin.newDrawable("button_down", Color.BLUE),
null, game.skin.getDrawable("play_texture"), null, null);
final ImageButton playButton = new ImageButton(playStyle);
table.add(playButton).space(16);
playButton.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
hide();
}
});
}
public void show() {
lastInputProcessor = Gdx.input.getInputProcessor();
Gdx.input.setInputProcessor(this);
shown = true;
hiding = false;
addAction(Actions.moveTo(0, Gdx.graphics.getHeight()));
addAction(Actions.moveTo(0, 0, 0.75f, Interpolation.swingOut));
}
public void hide() {
shown = false;
hiding = true;
Gdx.input.setInputProcessor(lastInputProcessor);
addAction(Actions.sequence(
Actions.moveTo(0, Gdx.graphics.getHeight(), 0.5f, Interpolation.swingIn),
new RunnableAction() {
@Override
public void run() {
hiding = false;
}
}
));
}
public boolean isShown() {
return shown;
}
public boolean isHiding() {
return hiding;
}
@Override
public void draw() {
// Draw an overlay rectangle with not all the opacity
if (shown) {
ShapeRenderer shapeRenderer = new ShapeRenderer(20);
Gdx.gl.glEnable(GL20.GL_BLEND);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(1f, 1f, 1f, 0.3f);
shapeRenderer.rect(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
shapeRenderer.end();
}
super.draw();
}
@Override
public boolean keyUp(int keyCode) {
if (keyCode == Input.Keys.P) // Pause
hide();
return super.keyUp(keyCode);
}
}