summaryrefslogtreecommitdiffstats
path: root/desktop
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2018-09-13 19:02:27 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2018-09-13 19:02:27 +0200
commitad423ba369941ebd3f9cced4030291ac67c4b3c6 (patch)
tree73c397867866f80b2ba7f50d7733a0513e46c51b /desktop
parent7eb1beae9fd79a07783bd850a5d739a9813797dc (diff)
downloadgdx-boardgame-ad423ba369941ebd3f9cced4030291ac67c4b3c6.zip
gdx-boardgame-ad423ba369941ebd3f9cced4030291ac67c4b3c6.tar.gz
namespace desktop -> test, move classes into desktop dir
Diffstat (limited to 'desktop')
-rw-r--r--desktop/build.gradle2
-rw-r--r--desktop/src/ch/asynk/zproject/desktop/Assets.java78
-rw-r--r--desktop/src/ch/asynk/zproject/desktop/DesktopLauncher.java5
-rw-r--r--desktop/src/ch/asynk/zproject/desktop/GameBoard.java110
-rw-r--r--desktop/src/ch/asynk/zproject/desktop/GameHud.java155
-rw-r--r--desktop/src/ch/asynk/zproject/desktop/GameScreen.java215
-rw-r--r--desktop/src/ch/asynk/zproject/desktop/GdxBoardTest.java83
-rw-r--r--desktop/src/ch/asynk/zproject/desktop/LoadingScreen.java166
8 files changed, 810 insertions, 4 deletions
diff --git a/desktop/build.gradle b/desktop/build.gradle
index 8c4bc7f..8dde89f 100644
--- a/desktop/build.gradle
+++ b/desktop/build.gradle
@@ -5,7 +5,7 @@ apply plugin: "java"
sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = [ "src/" ]
-project.ext.mainClassName = "ch.asynk.gdx.board.desktop.DesktopLauncher"
+project.ext.mainClassName = "ch.asynk.gdx.board.test.DesktopLauncher"
project.ext.assetsDir = new File("build/assets");
task run(dependsOn: classes, type: JavaExec) {
diff --git a/desktop/src/ch/asynk/zproject/desktop/Assets.java b/desktop/src/ch/asynk/zproject/desktop/Assets.java
new file mode 100644
index 0000000..e89d2a9
--- /dev/null
+++ b/desktop/src/ch/asynk/zproject/desktop/Assets.java
@@ -0,0 +1,78 @@
+package ch.asynk.gdx.board.test;
+
+import com.badlogic.gdx.assets.loaders.FileHandleResolver;
+import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
+import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.g2d.BitmapFont;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas;
+import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
+import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader;
+import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
+import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader.FreeTypeFontLoaderParameter;
+
+public class Assets extends ch.asynk.gdx.board.engine.Assets
+{
+ public static final String LOADING = "loading.atlas";
+
+ public static final String CORNER = "corner.png";
+ public static final String MAP_00 = "map_00.png";
+ public static final String CHESS = "chess.jpg";
+ public static final String FONT = "veteran-typewriter.ttf";
+ public static final String FONT_20 = "size20.ttf";
+ public static final String FONT_25 = "size25.ttf";
+ public static final String PATCH = "ui-patch.png";
+
+ private final FreeTypeFontLoaderParameter params20;
+ private final FreeTypeFontLoaderParameter params25;
+
+ public Assets()
+ {
+ super();
+ final FileHandleResolver resolver = new InternalFileHandleResolver();
+ setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
+ setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
+ params20 = new FreeTypeFontLoaderParameter();
+ params20.fontFileName = FONT;
+ params20.fontParameters.size = 20;
+ params25 = new FreeTypeFontLoaderParameter();
+ params25.fontFileName = FONT;
+ params25.fontParameters.size = 25;
+ }
+
+ @Override public void dispose()
+ {
+ GdxBoardTest.debug("diagnostics:\n" + getDiagnostics() );
+ clear();
+ super.dispose();
+ }
+
+ public void loadLoading()
+ {
+ load(LOADING, TextureAtlas.class);
+ }
+
+ public void unloadLoading()
+ {
+ unload(LOADING);
+ }
+
+ public void loadGame()
+ {
+ load(MAP_00, Texture.class);
+ load(CHESS, Texture.class);
+ load(CORNER, Texture.class);
+ load(PATCH, Texture.class);
+ load(FONT_20, BitmapFont.class, params20);
+ load(FONT_25, BitmapFont.class, params25);
+ }
+
+ public void unloadGame()
+ {
+ unload(MAP_00);
+ unload(CHESS);
+ unload(CORNER);
+ unload(PATCH);
+ unload(FONT_20);
+ unload(FONT_25);
+ }
+}
diff --git a/desktop/src/ch/asynk/zproject/desktop/DesktopLauncher.java b/desktop/src/ch/asynk/zproject/desktop/DesktopLauncher.java
index fbdd77c..0e14b17 100644
--- a/desktop/src/ch/asynk/zproject/desktop/DesktopLauncher.java
+++ b/desktop/src/ch/asynk/zproject/desktop/DesktopLauncher.java
@@ -1,13 +1,12 @@
-package ch.asynk.gdx.board.desktop;
+package ch.asynk.gdx.board.test;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
-import ch.asynk.gdx.board.ZProject;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
- new LwjglApplication(new ZProject(), config);
+ new LwjglApplication(new GdxBoardTest(), config);
config.width=800;
config.height=600;
}
diff --git a/desktop/src/ch/asynk/zproject/desktop/GameBoard.java b/desktop/src/ch/asynk/zproject/desktop/GameBoard.java
new file mode 100644
index 0000000..a2343db
--- /dev/null
+++ b/desktop/src/ch/asynk/zproject/desktop/GameBoard.java
@@ -0,0 +1,110 @@
+package ch.asynk.gdx.board.test;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.utils.Disposable;
+import com.badlogic.gdx.math.Vector2;
+
+import ch.asynk.gdx.board.engine.Touchable;
+import ch.asynk.gdx.board.engine.Board;
+import ch.asynk.gdx.board.engine.board.BoardFactory;
+
+public class GameBoard implements Disposable, Touchable
+{
+ private final Assets assets;
+ private Texture map;
+ private Board board;
+
+ private int dx;
+ private int dy;
+ private int w;
+ private int h;
+ private float r;
+
+ private Vector2 v;
+
+ public GameBoard(final Assets assets)
+ {
+ this.assets = assets;
+ this.v = new Vector2();
+ setState(GameScreen.State.UI);
+ }
+
+ @Override public void dispose()
+ {
+ map.dispose();
+ }
+
+ @Override public boolean touch(float x, float y)
+ {
+ board.toBoard(x, y, v);
+ GdxBoardTest.debug("BoardGame", String.format("touchDown [%d;%d] => [%d;%d]", (int)x, (int)y, (int)v.x, (int)v.y));
+ board.centerOf((int)v.x, (int)v.y, v);
+ GdxBoardTest.debug("BoardGame", String.format(" => [%d;%d]", (int)v.x, (int)v.y));
+ return true;
+ }
+
+ public void setState(GameScreen.State state)
+ {
+ switch (state) {
+ case UI:
+ case HEX_V:
+ setHEX_V();
+ break;
+ case HEX_H:
+ setHEX_H();
+ break;
+ case SQUARE:
+ setSQUARE();
+ break;
+ }
+ }
+
+ private void setHEX_V()
+ {
+ this.map = assets.getTexture(assets.MAP_00);
+ r = 0;
+ dx = 0;
+ dy = 0;
+ w = this.map.getWidth();
+ h = this.map.getHeight();
+ this.board = BoardFactory.getBoard(BoardFactory.BoardType.HEX, 110, 50, 103, BoardFactory.BoardOrientation.VERTICAL);
+ }
+
+ private void setHEX_H()
+ {
+ this.map = assets.getTexture(assets.MAP_00);
+ r = 90;
+ dx = - ( this.map.getWidth() - this.map.getHeight() ) / 2;
+ dy = - dx;
+ w = this.map.getHeight();
+ h = this.map.getWidth();
+ this.board = BoardFactory.getBoard(BoardFactory.BoardType.HEX, 110, 103, 50, BoardFactory.BoardOrientation.HORIZONTAL);
+ }
+
+ private void setSQUARE()
+ {
+ this.map = assets.getTexture(assets.CHESS);
+ r = 0;
+ dx = 0;
+ dy = 0;
+ w = map.getHeight();
+ h = map.getWidth();
+ this.board = BoardFactory.getBoard(BoardFactory.BoardType.SQUARE, 83, 5, 5);
+ }
+
+ public int getWidth()
+ {
+ return w;
+ }
+
+ public int getHeight()
+ {
+ return h;
+ }
+
+ public void draw(Batch batch)
+ {
+ batch.draw(map, dx, dy, map.getWidth()/2, map.getHeight()/2, map.getWidth(), map.getHeight(), 1, 1, r, 0, 0, map.getWidth(), map.getHeight(), false, false);
+ }
+}
diff --git a/desktop/src/ch/asynk/zproject/desktop/GameHud.java b/desktop/src/ch/asynk/zproject/desktop/GameHud.java
new file mode 100644
index 0000000..d2c8649
--- /dev/null
+++ b/desktop/src/ch/asynk/zproject/desktop/GameHud.java
@@ -0,0 +1,155 @@
+package ch.asynk.gdx.board.test;
+
+import java.util.function.Supplier;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.Sprite;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
+import com.badlogic.gdx.utils.Disposable;
+
+import ch.asynk.gdx.board.engine.ui.Button;
+import ch.asynk.gdx.board.engine.ui.Alignment;
+import ch.asynk.gdx.board.engine.ui.Root;
+import ch.asynk.gdx.board.engine.Touchable;
+
+public class GameHud implements Disposable, Touchable
+{
+ private final Sprite corner;
+ private final Root root;
+ private final Button hello;
+ private final Button next;
+ private GameScreen.State state;
+ private final Supplier<GameScreen.State> nextState;
+
+ public GameHud(final Assets assets, GameScreen.State state, Supplier<GameScreen.State> nextState)
+ {
+ this.state = state;
+ this.nextState = nextState;
+ this.corner = new Sprite(assets.getTexture(assets.CORNER));
+
+ this.root = new Root(2);
+ this.root.setPadding(30);
+
+ this.hello = new Button(assets.getFont(assets.FONT_25), assets.getNinePatch(assets.PATCH, 23, 23, 23 ,23), 10, 15);
+ this.hello.write("Hello");
+ this.root.add(this.hello);
+
+ this.next = new Button(assets.getFont(assets.FONT_25), assets.getNinePatch(assets.PATCH, 23, 23, 23 ,23), 20, 0);
+ this.next.write("NEXT");
+ this.next.setPosition(50, 50);
+ this.next.setAlignment(Alignment.MIDDLE_CENTER);
+ this.next.setLabelAlignment(Alignment.MIDDLE_CENTER);
+ this.root.add(this.next);
+ }
+
+ @Override public void dispose()
+ {
+ corner.getTexture().dispose();
+ }
+
+ @Override public boolean touch(float x, float y)
+ {
+ if (root.touch(x, y)) {
+ GdxBoardTest.debug("GameHud", String.format("touchDown : %f %f", x, y));
+ if (root.touched() == this.next)
+ onNext();
+ return true;
+ }
+ return false;
+ }
+
+ public void resize(float width, float height)
+ {
+ this.root.resize(width, height);
+ }
+
+ public void onNext()
+ {
+ this.state = nextState.get();
+ switch (this.state) {
+ case UI:
+ updateNext(50, Alignment.MIDDLE_CENTER);
+ this.root.add(this.hello);
+ break;
+ case HEX_V:
+ updateNext(0, Alignment.BOTTOM_RIGHT);
+ this.root.remove(this.hello);
+ break;
+ case HEX_H:
+ break;
+ }
+ }
+
+ private void updateNext(int p, Alignment a)
+ {
+ this.next.setPosition(p, p);
+ this.next.setAlignment(a);
+ this.next.update();
+ }
+
+ public void draw(Batch batch)
+ {
+ switch (this.state) {
+ case UI:
+ drawButtons(batch);
+ drawCorners(batch);
+ break;
+ case HEX_V:
+ case HEX_H:
+ drawRoot(batch);
+ break;
+ }
+ }
+
+ private void drawRoot(Batch batch)
+ {
+ root.draw(batch);
+ }
+
+ private void drawCorners(Batch batch)
+ {
+ float right = root.getX() + root.getWidth() - corner.getWidth();
+ float top = root.getY() + root.getHeight() - corner.getHeight();
+ corner.setRotation(0);
+ corner.setPosition(root.getX(), top);
+ corner.draw(batch);
+ corner.setRotation(90);
+ corner.setPosition(root.getX(), root.getY());
+ corner.draw(batch);
+ corner.setRotation(180);
+ corner.setPosition(right, root.getY());
+ corner.draw(batch);
+ corner.setPosition(right, top);
+ corner.setRotation(270);
+ corner.draw(batch);
+ }
+
+ private void drawButtons(Batch batch)
+ {
+ hello.setAlignment(Alignment.TOP_LEFT);
+ hello.setLabelAlignment(Alignment.BOTTOM_RIGHT);
+ hello.update();
+ root.draw(batch);
+ drawHello(batch, Alignment.TOP_CENTER, Alignment.BOTTOM_CENTER);
+ drawHello(batch, Alignment.TOP_RIGHT, Alignment.BOTTOM_LEFT);
+ drawHello(batch, Alignment.MIDDLE_LEFT, Alignment.MIDDLE_RIGHT);
+ // drawHello(batch, Alignment.MIDDLE_CENTER, Alignment.MIDDLE_CENTER);
+ drawHello(batch, Alignment.MIDDLE_RIGHT, Alignment.MIDDLE_LEFT);
+ drawHello(batch, Alignment.BOTTOM_LEFT, Alignment.TOP_RIGHT);
+ drawHello(batch, Alignment.BOTTOM_CENTER, Alignment.TOP_CENTER);
+ drawHello(batch, Alignment.BOTTOM_RIGHT, Alignment.TOP_LEFT);
+ }
+
+ private void drawHello(Batch batch, Alignment alignment1, Alignment alignment2)
+ {
+ hello.setAlignment(alignment1);
+ hello.setLabelAlignment(alignment2);
+ hello.update();
+ hello.draw(batch);
+ }
+
+ public void drawDebug(ShapeRenderer debugShapes)
+ {
+ root.drawDebug(debugShapes);
+ }
+}
diff --git a/desktop/src/ch/asynk/zproject/desktop/GameScreen.java b/desktop/src/ch/asynk/zproject/desktop/GameScreen.java
new file mode 100644
index 0000000..253affa
--- /dev/null
+++ b/desktop/src/ch/asynk/zproject/desktop/GameScreen.java
@@ -0,0 +1,215 @@
+package ch.asynk.gdx.board.test;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.graphics.GL20;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
+import com.badlogic.gdx.Input;
+import com.badlogic.gdx.InputAdapter;
+import com.badlogic.gdx.InputMultiplexer;
+import com.badlogic.gdx.input.GestureDetector;
+import com.badlogic.gdx.input.GestureDetector.GestureAdapter;
+import com.badlogic.gdx.math.Vector2;
+import com.badlogic.gdx.math.Vector3;
+
+import ch.asynk.gdx.board.engine.Camera;
+
+public class GameScreen implements Screen
+{
+ private static final float INPUT_DELAY = 0.1f; // filter out touches after gesture
+ private static final float ZOOM_SCROLL_FACTOR = .1f;
+ private static final float ZOOM_GESTURE_FACTOR = .01f;
+
+ private static final boolean DEBUG = true;
+
+ private final GdxBoardTest app;
+ private final GameHud hud;
+ private final GameBoard board;
+ private final Camera camera;
+ private final SpriteBatch batch;
+ private ShapeRenderer debugShapes = null;
+
+ private final Vector2 dragPos = new Vector2();
+ private final Vector3 boardTouch = new Vector3();
+ private final Vector3 hudTouch = new Vector3();
+
+ private boolean paused;
+ private float inputDelay;
+ private boolean inputBlocked;
+
+ public enum State
+ {
+ UI, HEX_V, HEX_H, SQUARE;
+ public State next()
+ {
+ switch(this) {
+ case UI:
+ return HEX_V;
+ case HEX_V:
+ return HEX_H;
+ case HEX_H:
+ return SQUARE;
+ case SQUARE:
+ return UI;
+ default:
+ return UI;
+ }
+ }
+ }
+ private State state;
+
+ public GameScreen(final GdxBoardTest app)
+ {
+ this.app = app;
+ this.hud = new GameHud(app.assets, State.UI, () -> nextState());
+ this.board = new GameBoard(app.assets);
+ this.batch = new SpriteBatch();
+ this.camera = new Camera(10, board.getWidth(), board.getHeight(), 1.0f, 0.3f, false);
+ Gdx.input.setInputProcessor(getMultiplexer(this));
+ this.paused = false;
+ this.inputDelay = 0f;
+ this.inputBlocked = false;
+ this.state = State.UI;
+ if (DEBUG) this.debugShapes = new ShapeRenderer();
+ }
+
+ public State nextState()
+ {
+ this.state = this.state.next();
+ this.board.setState(this.state);
+ this.camera.setDimension(board.getWidth(), board.getHeight());
+ zoom(1);
+ return this.state;
+ }
+
+ @Override public void render(float delta)
+ {
+ if (paused) return;
+
+ if (inputBlocked) {
+ inputDelay -= delta;
+ if (inputDelay <= 0f)
+ inputBlocked = false;
+ }
+
+ Gdx.gl.glClearColor(0, 0, 0, 1);
+ Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
+
+ camera.applyMapViewport();
+ batch.setProjectionMatrix(camera.combined);
+ batch.begin();
+ board.draw(batch);
+ batch.end();
+
+ camera.applyHudViewport();
+ batch.setProjectionMatrix(camera.getHudMatrix());
+ batch.begin();
+ hud.draw(batch);
+ batch.end();
+
+ if (DEBUG) {
+ Gdx.gl.glEnable(GL20.GL_BLEND);
+ debugShapes.setAutoShapeType(true);
+ debugShapes.setProjectionMatrix(camera.getHudMatrix());
+ debugShapes.begin();
+ hud.drawDebug(debugShapes);
+ debugShapes.end();
+ }
+
+ }
+
+ @Override public void resize(int width, int height)
+ {
+ if (paused) return;
+ GdxBoardTest.debug("GameScreen", String.format("resize (%d,%d)",width, height));
+ camera.updateViewport(width, height);
+ hud.resize(camera.getHud().width, camera.getHud().height);
+ }
+
+ @Override public void dispose()
+ {
+ GdxBoardTest.debug("GameScreen", "dispose()");
+ batch.dispose();
+ if (debugShapes != null) debugShapes.dispose();
+ hud.dispose();
+ board.dispose();
+ }
+
+ @Override public void show()
+ {
+ GdxBoardTest.debug("GameScreen", "show()");
+ }
+
+ @Override public void hide()
+ {
+ GdxBoardTest.debug("GameScreen", "hide()");
+ }
+
+ @Override public void pause()
+ {
+ GdxBoardTest.debug("pause() ");
+ paused = true;
+ }
+
+ @Override public void resume()
+ {
+ GdxBoardTest.debug("resume() ");
+ paused = false;
+ resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
+ }
+
+ private void zoom(float dz)
+ {
+ camera.zoom(dz);
+ resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
+ }
+
+ private InputMultiplexer getMultiplexer(final GameScreen screen)
+ {
+ final InputMultiplexer multiplexer = new InputMultiplexer();
+ multiplexer.addProcessor(new InputAdapter() {
+ @Override public boolean scrolled(int amount)
+ {
+ screen.zoom(amount * ZOOM_SCROLL_FACTOR);
+ return true;
+ }
+ @Override public boolean touchDown(int x, int y, int pointer, int button)
+ {
+ if (inputBlocked) return true;
+ if (button == Input.Buttons.LEFT) {
+ dragPos.set(x, y);
+ camera.unproject(x, y, boardTouch);
+ camera.unprojectHud(x, y, hudTouch);
+ if (!hud.touch(hudTouch.x, hudTouch.y)) {
+ if (state != State.UI)
+ board.touch(boardTouch.x, boardTouch.y);
+ }
+ }
+ return true;
+ }
+ @Override public boolean touchDragged(int x, int y, int pointer)
+ {
+ int dx = (int) (dragPos.x - x);
+ int dy = (int) (dragPos.y - y);
+ dragPos.set(x, y);
+ camera.translate(dx, dy);
+ return true;
+ }
+ });
+ multiplexer.addProcessor(new GestureDetector(new GestureAdapter() {
+ @Override public boolean zoom(float initialDistance, float distance)
+ {
+ if (initialDistance > distance)
+ screen.zoom(ZOOM_GESTURE_FACTOR);
+ else
+ screen.zoom(-ZOOM_GESTURE_FACTOR);
+ inputBlocked = true;
+ inputDelay = INPUT_DELAY;
+ return true;
+ }
+ }));
+
+ return multiplexer;
+ }
+}
diff --git a/desktop/src/ch/asynk/zproject/desktop/GdxBoardTest.java b/desktop/src/ch/asynk/zproject/desktop/GdxBoardTest.java
new file mode 100644
index 0000000..03726f7
--- /dev/null
+++ b/desktop/src/ch/asynk/zproject/desktop/GdxBoardTest.java
@@ -0,0 +1,83 @@
+package ch.asynk.gdx.board.test;
+
+import com.badlogic.gdx.Game;
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Screen;
+
+public class GdxBoardTest extends Game
+{
+ public static final String DOM = "GdxBoardTest";
+
+ private enum State
+ {
+ NONE,
+ LOADING,
+ GAME,
+ }
+ private State state;
+
+ public final Assets assets = new Assets();
+
+ @Override public void create()
+ {
+ this.state = State.NONE;
+ Gdx.app.setLogLevel(Gdx.app.LOG_DEBUG);
+ debug(String.format("create() [%d;%d] %f", Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Gdx.graphics.getDensity()));
+ switchToLoading();
+ }
+
+ @Override public void dispose()
+ {
+ switchToNone();
+ assets.dispose();
+ }
+
+ public static void error(String msg)
+ {
+ Gdx.app.error(DOM, msg);
+ }
+
+ public static void debug(String msg)
+ {
+ Gdx.app.debug(DOM, msg);
+ }
+
+ public static void debug(String from, String msg)
+ {
+ Gdx.app.debug(DOM, String.format("%s : %s", from, msg));
+ }
+
+ private void switchTo(Screen nextScreen, State nextState)
+ {
+ if (state == nextState) {
+ error("switch from and to " + state);
+ return;
+ }
+ switch(state) {
+ case LOADING: assets.unloadLoading(); break;
+ case GAME: assets.unloadGame(); break;
+ }
+ if (state != State.NONE) {
+ getScreen().dispose();
+ }
+ setScreen(nextScreen);
+ this.state = nextState;
+ }
+
+ public void switchToNone()
+ {
+ switchTo(null, State.NONE);
+ }
+
+ public void switchToLoading()
+ {
+ assets.loadLoading();
+ assets.finishLoading();
+ switchTo(new LoadingScreen(this, () -> assets.loadGame(), () -> switchToGame()), State.LOADING);
+ }
+
+ public void switchToGame()
+ {
+ switchTo(new GameScreen(this), State.GAME);
+ }
+}
diff --git a/desktop/src/ch/asynk/zproject/desktop/LoadingScreen.java b/desktop/src/ch/asynk/zproject/desktop/LoadingScreen.java
new file mode 100644
index 0000000..ee6ef2e
--- /dev/null
+++ b/desktop/src/ch/asynk/zproject/desktop/LoadingScreen.java
@@ -0,0 +1,166 @@
+package ch.asynk.gdx.board.test;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Screen;
+import com.badlogic.gdx.graphics.GL20;
+import com.badlogic.gdx.graphics.Color;
+import com.badlogic.gdx.graphics.OrthographicCamera;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
+
+public class LoadingScreen implements Screen
+{
+ private final GdxBoardTest app;
+ private final SpriteBatch batch;
+ private final Color c;
+ private final OrthographicCamera camera;
+
+ private final float BLINK_AFTER = 2f;
+ private final float ALPHA_FACTOR = 1.5f;
+
+ private boolean paused;
+ private TextureAtlas atlas;
+ private AtlasRegion bar;
+ private AtlasRegion border;
+ private AtlasRegion loading;
+ private AtlasRegion loaded;
+
+ private int x, y;
+ private int count;
+ private float percent;
+ private float alpha;
+ private boolean incr;
+ private boolean isLoaded;
+
+ public interface LoadAction {
+ void call();
+ }
+ private LoadAction onLoaded;
+
+ public LoadingScreen(final GdxBoardTest app, LoadAction startLoading, LoadAction onLoaded)
+ {
+ this.app = app;
+ this.onLoaded = onLoaded;
+ this.batch = new SpriteBatch();
+ this.c = batch.getColor();
+ this.camera = new OrthographicCamera();
+ int w = Gdx.graphics.getWidth();
+ int h = Gdx.graphics.getHeight();
+ this.camera.setToOrtho(false, w, h);
+ this.camera.update();
+ this.paused = false;
+ atlas = app.assets.getAtlas(app.assets.LOADING);
+ bar = atlas.findRegion("bar");
+ border = atlas.findRegion("border");
+ loading = atlas.findRegion("loading");
+ loaded = atlas.findRegion("loaded");
+ computeCoords(w, h);
+ percent = 0f;
+ alpha = 1f;
+ incr = false;
+ isLoaded = false;
+ startLoading.call();
+ }
+
+ private void computeCoords(int width, int height)
+ {
+ x = (width - border.getRegionWidth()) / 2;
+ y = (height - border.getRegionHeight()) / 2;
+ }
+
+ @Override public void render(float delta)
+ {
+ if (paused) return;
+
+ if (!isLoaded) {
+ if (app.assets.update()) {
+ GdxBoardTest.debug("LoadingScreen", "assets loaded");
+ isLoaded = true;
+ percent = 1f;
+ } else {
+ percent = app.assets.getProgress();
+ }
+ }
+
+ if (!isLoaded && percent >= 1f) {
+ count = 0;
+ alpha = 1f;
+ incr = false;
+ }
+
+ delta *= ALPHA_FACTOR;
+ if (incr) {
+ alpha += delta;
+ if (alpha >= 1f ) {
+ alpha = 1f;
+ incr = false;
+ if (isLoaded) count += 1;
+ if (count >= BLINK_AFTER) {
+ onLoaded.call();
+ return;
+ }
+ }
+ } else {
+ alpha -= delta;
+ if (alpha <= 0f ) {
+ alpha = 0f;
+ incr = true;
+ }
+ }
+
+ Gdx.gl.glClearColor(0, 0, 0, 1);
+ Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
+
+ batch.setProjectionMatrix(camera.combined);
+ batch.begin();
+ batch.setColor(c.r, c.g, c.b, 1f);
+ batch.draw(border, x, y);
+ batch.draw(bar, x + 4, y + 4, percent * bar.getRegionWidth(), bar.getRegionHeight());
+ batch.setColor(c.r, c.g, c.b, alpha);
+ if (!isLoaded) {
+ batch.draw(loading, x, y + border.getRegionHeight() + 3);
+ } else {
+ batch.draw(loaded, x + border.getRegionWidth() - loaded.getRegionWidth(), y - loaded.getRegionHeight() - 3);
+ }
+ batch.end();
+ }
+
+ @Override public void resize(int width, int height)
+ {
+ if (paused) return;
+ GdxBoardTest.debug("LoadingScreen", String.format("resize (%d,%d)",width, height));
+ this.camera.setToOrtho(false, width, height);
+ this.camera.update();
+ computeCoords(width, height);
+ }
+
+ @Override public void dispose()
+ {
+ GdxBoardTest.debug("LoadingScreen", "dispose()");
+ batch.dispose();
+ }
+
+ @Override public void show()
+ {
+ GdxBoardTest.debug("LoadingScreen", "show()");
+ }
+
+ @Override public void hide()
+ {
+ GdxBoardTest.debug("LoadingScreen", "hide()");
+ }
+
+ @Override public void pause()
+ {
+ paused = true;
+ GdxBoardTest.debug("pause() ");
+ }
+
+ @Override public void resume()
+ {
+ GdxBoardTest.debug("resume() ");
+ resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
+ paused = false;
+ }
+}