diff options
Diffstat (limited to 'test/src')
-rw-r--r-- | test/src/ch/asynk/gdx/board/test/Assets.java | 4 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/board/test/BoardScreen.java | 331 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/board/test/GameBoard.java | 142 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/board/test/GameHud.java | 156 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/board/test/GameScreen.java | 220 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/board/test/GdxBoardTest.java | 42 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/board/test/MenuScreen.java | 180 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/board/test/UiScreen.java | 210 |
8 files changed, 751 insertions, 534 deletions
diff --git a/test/src/ch/asynk/gdx/board/test/Assets.java b/test/src/ch/asynk/gdx/board/test/Assets.java index c3d76ab..112d2ae 100644 --- a/test/src/ch/asynk/gdx/board/test/Assets.java +++ b/test/src/ch/asynk/gdx/board/test/Assets.java @@ -58,7 +58,7 @@ public class Assets extends ch.asynk.gdx.tabletop.Assets unload(LOADING); } - public void loadGame() + public void loadApp() { load(MAP_00, Texture.class); load(CHESS, Texture.class); @@ -70,7 +70,7 @@ public class Assets extends ch.asynk.gdx.tabletop.Assets load(FONT_25, BitmapFont.class, params25); } - public void unloadGame() + public void unloadApp() { unload(MAP_00); unload(CHESS); diff --git a/test/src/ch/asynk/gdx/board/test/BoardScreen.java b/test/src/ch/asynk/gdx/board/test/BoardScreen.java new file mode 100644 index 0000000..3ef9dea --- /dev/null +++ b/test/src/ch/asynk/gdx/board/test/BoardScreen.java @@ -0,0 +1,331 @@ +package ch.asynk.gdx.tabletop.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.Texture; +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.tabletop.Camera; +import ch.asynk.gdx.tabletop.Board; +import ch.asynk.gdx.tabletop.board.BoardFactory; +import ch.asynk.gdx.tabletop.ui.Button; + +public class BoardScreen implements Screen +{ + private static final String DOM = "BoardScreen"; + 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 class MyBoard + { + private final Assets assets; + private final Texture sherman; + private final Vector2 v; + public Texture map; + public Board board; + public int dx; + public int dy; + public int w; + public int h; + public float r; + + public MyBoard(final Assets assets) + { + this.assets = assets; + this.sherman = assets.getTexture(assets.SHERMAN); + this.v = new Vector2(); + } + + public void draw(SpriteBatch 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); + batch.draw(sherman, v.x - (sherman.getWidth() / 2), v.y - (sherman.getHeight() / 2)); + } + + public void reset() + { + board.centerOf(0, 0, v); + } + + 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 setHEX_V() + { + map = assets.getTexture(assets.MAP_00); + r = 0; + dx = 0; + dy = 0; + w = map.getWidth(); + h = map.getHeight(); + board = BoardFactory.getBoard(BoardFactory.BoardType.HEX, 110, 50, 103, BoardFactory.BoardOrientation.VERTICAL); + } + + public void setHEX_H() + { + map = assets.getTexture(assets.MAP_00); + r = 90; + dx = - ( map.getWidth() - map.getHeight() ) / 2; + dy = - dx; + w = map.getHeight(); + h = map.getWidth(); + board = BoardFactory.getBoard(BoardFactory.BoardType.HEX, 110, 103, 50, BoardFactory.BoardOrientation.HORIZONTAL); + } + + public void setSQUARE() + { + map = assets.getTexture(assets.CHESS); + r = 0; + dx = 0; + dy = 0; + w = map.getWidth(); + h = map.getHeight(); + board = BoardFactory.getBoard(BoardFactory.BoardType.SQUARE, 83, 5, 5); + } + + public void setTRI_H() + { + map = assets.getTexture(assets.TRI); + r = 0; + dx = 0; + dy = 0; + w = map.getWidth(); + h = map.getHeight(); + board = BoardFactory.getBoard(BoardFactory.BoardType.TRIANGLE, 150, 109, 53, BoardFactory.BoardOrientation.HORIZONTAL); + } + + public void setTRI_V() + { + map = assets.getTexture(assets.TRI); + r = 90; + dx = - ( map.getWidth() - map.getHeight() ) / 2; + dy = - dx; + w = map.getHeight(); + h = map.getWidth(); + board = BoardFactory.getBoard(BoardFactory.BoardType.TRIANGLE, 150, 16, 110, BoardFactory.BoardOrientation.VERTICAL); + } + } + + private final GdxBoardTest app; + private final MyBoard board; + private final Camera camera; + private final SpriteBatch batch; + private final Button btn; + + 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 + { + HEX_V, HEX_H, SQUARE, TRI_H, TRI_V, DONE; + public State next() + { + switch(this) { + case HEX_V: + return HEX_H; + case HEX_H: + return SQUARE; + case SQUARE: + return TRI_H; + case TRI_H: + return TRI_V; + case TRI_V: + return DONE; + default: + return HEX_V; + } + } + } + private State state; + + public BoardScreen(final GdxBoardTest app) + { + this.app = app; + this.board = new MyBoard(app.assets); + this.batch = new SpriteBatch(); + this.camera = new Camera(10, board.w, board.h, 1.0f, 0.3f, false); + this.btn = new Button( + app.assets.getFont(app.assets.FONT_25), + app.assets.getNinePatch(app.assets.PATCH, 23, 23, 23 ,23), + 15); + this.btn.write("next"); + Gdx.input.setInputProcessor(getMultiplexer(this)); + setState(State.HEX_V); + this.inputBlocked = false; + this.inputDelay = 0f; + this.paused = false; + } + + @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(); + btn.draw(batch); + batch.end(); + } + + @Override public void resize(int width, int height) + { + GdxBoardTest.debug("BoardScrean", String.format("resize (%d,%d)",width, height)); + camera.updateViewport(width, height); + } + + @Override public void dispose() + { + GdxBoardTest.debug("BoardScrean", "dispose()"); + batch.dispose(); + } + + @Override public void show() + { + GdxBoardTest.debug("BoardScrean", "show()"); + paused = false; + } + + @Override public void hide() + { + GdxBoardTest.debug("BoardScrean", "hide()"); + paused = true; + } + + @Override public void pause() + { + GdxBoardTest.debug("pause() "); + paused = true; + } + + @Override public void resume() + { + GdxBoardTest.debug("resume() "); + resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + paused = false; + } + + private void zoom(float dz) + { + camera.zoom(dz); + resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + } + + private void nextState() + { + setState(this.state.next()); + } + + private void setState(State state) + { + switch (state) { + case HEX_V: + board.setHEX_V(); + break; + case HEX_H: + board.setHEX_H(); + break; + case SQUARE: + board.setSQUARE(); + break; + case TRI_H: + board.setTRI_H(); + break; + case TRI_V: + board.setTRI_V(); + break; + case DONE: + this.app.switchToMenu(); + return; + } + board.reset(); + this.camera.setDimension(board.w, board.h); + zoom(1); + this.state = state; + } + + private InputMultiplexer getMultiplexer(final BoardScreen screen) + { + final InputMultiplexer multiplexer = new InputMultiplexer(); + multiplexer.addProcessor(new InputAdapter() { + @Override public boolean scrolled(int amount) + { + 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 (btn.touch(hudTouch.x, hudTouch.y)) { + nextState(); + } else { + 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/test/src/ch/asynk/gdx/board/test/GameBoard.java b/test/src/ch/asynk/gdx/board/test/GameBoard.java deleted file mode 100644 index 801e947..0000000 --- a/test/src/ch/asynk/gdx/board/test/GameBoard.java +++ /dev/null @@ -1,142 +0,0 @@ -package ch.asynk.gdx.tabletop.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.tabletop.Touchable; -import ch.asynk.gdx.tabletop.Board; -import ch.asynk.gdx.tabletop.board.BoardFactory; - -public class GameBoard implements Disposable, Touchable -{ - private final Assets assets; - private Texture map; - private Texture sherman; - 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(); - this.sherman = assets.getTexture(assets.SHERMAN); - 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; - case TRI_H: - setTRI_H(); - break; - case TRI_V: - setTRI_V(); - break; - } - board.centerOf(0, 0, v); - } - - 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.getWidth(); - h = map.getHeight(); - this.board = BoardFactory.getBoard(BoardFactory.BoardType.SQUARE, 83, 5, 5); - } - - private void setTRI_H() - { - this.map = assets.getTexture(assets.TRI); - r = 0; - dx = 0; - dy = 0; - w = map.getWidth(); - h = map.getHeight(); - this.board = BoardFactory.getBoard(BoardFactory.BoardType.TRIANGLE, 150, 109, 53, BoardFactory.BoardOrientation.HORIZONTAL); - } - - private void setTRI_V() - { - this.map = assets.getTexture(assets.TRI); - r = 90; - dx = - ( this.map.getWidth() - this.map.getHeight() ) / 2; - dy = - dx; - w = map.getHeight(); - h = map.getWidth(); - this.board = BoardFactory.getBoard(BoardFactory.BoardType.TRIANGLE, 150, 16, 110, BoardFactory.BoardOrientation.VERTICAL); - } - - 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); - batch.draw(sherman, v.x - (sherman.getWidth() / 2), v.y - (sherman.getHeight() / 2)); - } -} diff --git a/test/src/ch/asynk/gdx/board/test/GameHud.java b/test/src/ch/asynk/gdx/board/test/GameHud.java deleted file mode 100644 index f401bf9..0000000 --- a/test/src/ch/asynk/gdx/board/test/GameHud.java +++ /dev/null @@ -1,156 +0,0 @@ -package ch.asynk.gdx.tabletop.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.g2d.NinePatch; -import com.badlogic.gdx.graphics.g2d.BitmapFont; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.utils.Disposable; - -import ch.asynk.gdx.tabletop.ui.Button; -import ch.asynk.gdx.tabletop.ui.Alignment; -import ch.asynk.gdx.tabletop.ui.Root; -import ch.asynk.gdx.tabletop.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)); - - NinePatch patch = assets.getNinePatch(assets.PATCH, 23, 23, 23 ,23); - BitmapFont font = assets.getFont(assets.FONT_25); - - this.root = new Root(2); - this.root.setPadding(30); - - this.hello = new Button(font, patch, 10, 15); - this.hello.write("Hello"); - this.root.add(this.hello); - - this.next = new Button(font, patch, 20); - 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; - default: - break; - } - } - - private void updateNext(int p, Alignment a) - { - this.next.setPosition(p, p); - this.next.setAlignment(a); - } - - public void draw(Batch batch) - { - switch (this.state) { - case UI: - drawButtons(batch); - drawCorners(batch); - break; - default: - 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); - 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.draw(batch); - } - - public void drawDebug(ShapeRenderer debugShapes) - { - root.drawDebug(debugShapes); - } -} diff --git a/test/src/ch/asynk/gdx/board/test/GameScreen.java b/test/src/ch/asynk/gdx/board/test/GameScreen.java deleted file mode 100644 index 7f038d6..0000000 --- a/test/src/ch/asynk/gdx/board/test/GameScreen.java +++ /dev/null @@ -1,220 +0,0 @@ -package ch.asynk.gdx.tabletop.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.tabletop.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, TRI_H, TRI_V; - public State next() - { - switch(this) { - case UI: - return HEX_V; - case HEX_V: - return HEX_H; - case HEX_H: - return SQUARE; - case SQUARE: - return TRI_H; - case TRI_H: - return TRI_V; - case TRI_V: - 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 && this.state == State.UI) { - 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) - { - 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()"); - paused = false; - } - - @Override public void hide() - { - GdxBoardTest.debug("GameScreen", "hide()"); - paused = true; - } - - @Override public void pause() - { - GdxBoardTest.debug("pause() "); - paused = true; - } - - @Override public void resume() - { - GdxBoardTest.debug("resume() "); - resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); - paused = false; - } - - 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/test/src/ch/asynk/gdx/board/test/GdxBoardTest.java b/test/src/ch/asynk/gdx/board/test/GdxBoardTest.java index 072b4d6..7e6bcd4 100644 --- a/test/src/ch/asynk/gdx/board/test/GdxBoardTest.java +++ b/test/src/ch/asynk/gdx/board/test/GdxBoardTest.java @@ -6,13 +6,16 @@ import com.badlogic.gdx.Screen; public class GdxBoardTest extends Game { - public static final String DOM = "GdxBoardTest"; + private static final String DOM = "GdxBoardTest"; private enum State { NONE, LOADING, - GAME, + MENU, + UI, + BOARD, + EXIT } private State state; @@ -28,7 +31,8 @@ public class GdxBoardTest extends Game @Override public void dispose() { - switchToNone(); + debug("dispose()"); + assets.clear(); assets.dispose(); } @@ -53,9 +57,8 @@ public class GdxBoardTest extends Game error("switch from and to " + state); return; } - switch(state) { - case LOADING: assets.unloadLoading(); break; - case GAME: assets.unloadGame(); break; + if (state == State.LOADING) { + assets.unloadLoading(); } if (state != State.NONE) { getScreen().dispose(); @@ -64,20 +67,31 @@ public class GdxBoardTest extends Game 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); + switchTo(new LoadingScreen(this, () -> assets.loadApp(), () -> switchToMenu()), State.LOADING); + } + + public void switchToMenu() + { + switchTo(new MenuScreen(this), State.MENU); + } + + public void switchToUi() + { + switchTo(new UiScreen(this), State.UI); + } + + public void switchToBoard() + { + switchTo(new BoardScreen(this), State.BOARD); } - public void switchToGame() + public void switchToExit() { - switchTo(new GameScreen(this), State.GAME); + Gdx.app.exit(); + switchTo(null, State.EXIT); } } diff --git a/test/src/ch/asynk/gdx/board/test/MenuScreen.java b/test/src/ch/asynk/gdx/board/test/MenuScreen.java new file mode 100644 index 0000000..892182a --- /dev/null +++ b/test/src/ch/asynk/gdx/board/test/MenuScreen.java @@ -0,0 +1,180 @@ +package ch.asynk.gdx.tabletop.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.g2d.Sprite; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.InputAdapter; +import com.badlogic.gdx.InputMultiplexer; +import com.badlogic.gdx.math.Vector3; + +import ch.asynk.gdx.tabletop.ui.Alignment; +import ch.asynk.gdx.tabletop.ui.Menu; +import ch.asynk.gdx.tabletop.ui.Root; + +public class MenuScreen implements Screen +{ + private static final String DOM = "MenuScreen"; + + private final GdxBoardTest app; + private final OrthographicCamera cam; + private final SpriteBatch batch; + private final Texture bg; + private final Sprite corner; + private final Root root; + private final Menu menu; + private boolean paused; + + private final float WORLD_RATIO = 0.5f; + private final int PADDING = 15; + + private final Vector3 touch = new Vector3(); + + public MenuScreen(final GdxBoardTest app) + { + this.app = app; + this.batch = new SpriteBatch(); + final Assets assets = app.assets; + this.bg = assets.getTexture(assets.MAP_00); + this.corner = new Sprite(assets.getTexture(assets.CORNER)); + + this.root = new Root(1); + this.menu = new Menu( + assets.getFont(assets.FONT_25), + assets.getNinePatch(assets.PATCH, 23, 23, 23 ,23), + "Menu", new String[]{"UI","Board","Exit"}); + this.menu.setAlignment(Alignment.MIDDLE_CENTER); + this.menu.setPaddings(5, 5); + this.menu.setSpacings(10, 5); + this.menu.setPadding(20); + this.menu.setLabelsOffset(10); + this.root.add(this.menu); + + this.cam = new OrthographicCamera(bg.getWidth() * WORLD_RATIO, bg.getHeight() * WORLD_RATIO); + this.cam.position.set(bg.getWidth() / 2f, bg.getHeight() / 2f, 0); + this.cam.update(); + + Gdx.input.setInputProcessor(getMultiplexer(this)); + this.paused = false; + } + + @Override public void render(float delta) + { + if (paused) return; + + Gdx.gl.glClearColor(1, 1, 1, 1); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + + cam.update(); + batch.setProjectionMatrix(cam.combined); + batch.begin(); + batch.draw(bg, 0, 0); + drawCorners(batch); + root.draw(batch); + batch.end(); + } + + private void drawCorners(SpriteBatch 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); + } + + @Override public void resize(int width, int height) + { + GdxBoardTest.debug(DOM, String.format("resize (%d,%d)",width, height)); + if (width >= height) { + cam.viewportWidth = bg.getWidth(); + cam.viewportHeight = bg.getHeight() / (float)width * (float)height; + } else { + cam.viewportHeight = bg.getHeight(); + cam.viewportWidth = bg.getWidth() / (float)height * (float)width; + } + cam.viewportWidth *= WORLD_RATIO; + cam.viewportHeight *= WORLD_RATIO; + cam.update(); + root.resize( + cam.position.x - (cam.viewportWidth / 2f) + PADDING, + cam.position.y - (cam.viewportHeight / 2f) + PADDING, + cam.viewportWidth - 2 * PADDING, + cam.viewportHeight - 2 * PADDING + ); + } + + @Override public void dispose() + { + GdxBoardTest.debug(DOM, "dispose()"); + batch.dispose(); + } + + @Override public void show() + { + GdxBoardTest.debug(DOM, "show()"); + paused = false; + } + + @Override public void hide() + { + GdxBoardTest.debug(DOM, "hide()"); + paused = true; + } + + @Override public void pause() + { + GdxBoardTest.debug(DOM, "pause() "); + paused = true; + } + + @Override public void resume() + { + GdxBoardTest.debug(DOM, "resume() "); + resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + paused = false; + } + + private InputMultiplexer getMultiplexer(final MenuScreen screen) + { + final InputMultiplexer multiplexer = new InputMultiplexer(); + multiplexer.addProcessor(new InputAdapter() { + @Override public boolean touchDown(int x, int y, int pointer, int button) + { + if (button == Input.Buttons.LEFT) { + touch.set(x, y, 0); + cam.unproject(touch); + if (root.touch(touch.x, touch.y)) { + switch(menu.touched()) { + case 0: + app.switchToUi(); + break; + case 1: + app.switchToBoard(); + break; + case 2: + app.switchToExit(); + break; + } + } + } + return true; + } + }); + + return multiplexer; + } +} diff --git a/test/src/ch/asynk/gdx/board/test/UiScreen.java b/test/src/ch/asynk/gdx/board/test/UiScreen.java new file mode 100644 index 0000000..f321391 --- /dev/null +++ b/test/src/ch/asynk/gdx/board/test/UiScreen.java @@ -0,0 +1,210 @@ +package ch.asynk.gdx.tabletop.test; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Screen; +import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.NinePatch; +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.InputAdapter; +import com.badlogic.gdx.InputMultiplexer; +import com.badlogic.gdx.math.Vector3; + +import ch.asynk.gdx.tabletop.ui.Alignment; +import ch.asynk.gdx.tabletop.ui.Button; +import ch.asynk.gdx.tabletop.ui.Root; + +public class UiScreen implements Screen +{ + private static final String DOM = "UiScreen"; + + private final GdxBoardTest app; + private final OrthographicCamera cam; + private final SpriteBatch batch; + private final Texture bg; + private final Root root; + private final Button hello; + private boolean paused; + + private final float WORLD_RATIO = 0.5f; + private final float PADDING = 15f; + + private final Vector3 touch = new Vector3(); + + public enum State + { + POSITIONS, DONE; + public State next() + { + switch(this) { + case POSITIONS: + return DONE; + default: + return POSITIONS; + } + } + } + private State state; + + public UiScreen(final GdxBoardTest app) + { + this.app = app; + this.batch = new SpriteBatch(); + this.bg = app.assets.getTexture(app.assets.MAP_00); + + final NinePatch patch = app.assets.getNinePatch(app.assets.PATCH, 23, 23, 23 ,23); + final BitmapFont font = app.assets.getFont(app.assets.FONT_25); + + this.root = new Root(1); + this.hello = new Button(font, patch, 10, 15); + this.hello.write("Hello"); + this.root.add(this.hello); + + this.cam = new OrthographicCamera(bg.getWidth() * WORLD_RATIO, bg.getHeight() * WORLD_RATIO); + this.cam.position.set(bg.getWidth() / 2f, bg.getHeight() / 2f, 0); + this.cam.update(); + + Gdx.input.setInputProcessor(getMultiplexer()); + this.paused = false; + setState(State.POSITIONS); + } + + @Override public void render(float delta) + { + if (paused) return; + + Gdx.gl.glClearColor(1, 1, 1, 1); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + + cam.update(); + batch.setProjectionMatrix(cam.combined); + batch.begin(); + draw(batch); + batch.end(); + } + + private void draw(Batch batch) + { + batch.draw(bg, 0, 0); + switch (state) { + case POSITIONS: + drawButtons(batch); + break; + } + } + + private void drawButtons(Batch batch) + { + hello.write("hello"); + hello.setAlignment(Alignment.TOP_LEFT); + hello.setLabelAlignment(Alignment.BOTTOM_RIGHT); + 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_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); + hello.write("next"); + drawHello(batch, Alignment.MIDDLE_CENTER, Alignment.MIDDLE_CENTER); + } + + private void drawHello(Batch batch, Alignment alignment1, Alignment alignment2) + { + hello.setAlignment(alignment1); + hello.setLabelAlignment(alignment2); + hello.draw(batch); + } + + @Override public void resize(int width, int height) + { + GdxBoardTest.debug(DOM, String.format("resize (%d,%d)",width, height)); + if (width >= height) { + cam.viewportWidth = bg.getWidth(); + cam.viewportHeight = bg.getHeight() / (float)width * (float)height; + } else { + cam.viewportHeight = bg.getHeight(); + cam.viewportWidth = bg.getWidth() / (float)height * (float)width; + } + cam.viewportWidth *= WORLD_RATIO; + cam.viewportHeight *= WORLD_RATIO; + cam.update(); + root.resize( + cam.position.x - (cam.viewportWidth / 2f) + PADDING, + cam.position.y - (cam.viewportHeight / 2f) + PADDING, + cam.viewportWidth - 2 * PADDING, + cam.viewportHeight - 2 * PADDING + ); + } + + @Override public void dispose() + { + GdxBoardTest.debug(DOM, "dispose()"); + batch.dispose(); + } + + @Override public void show() + { + GdxBoardTest.debug(DOM, "show()"); + paused = false; + } + + @Override public void hide() + { + GdxBoardTest.debug(DOM, "hide()"); + paused = true; + } + + @Override public void pause() + { + GdxBoardTest.debug(DOM, "pause() "); + paused = true; + } + + @Override public void resume() + { + GdxBoardTest.debug(DOM, "resume() "); + resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + paused = false; + } + + private void touch() + { + if (root.touch(touch.x, touch.y)) { + setState(state.next()); + } + } + + private InputMultiplexer getMultiplexer() + { + final InputMultiplexer multiplexer = new InputMultiplexer(); + multiplexer.addProcessor(new InputAdapter() { + @Override public boolean touchDown(int x, int y, int pointer, int button) + { + if (button == Input.Buttons.LEFT) { + touch.set(x, y, 0); + cam.unproject(touch); + touch(); + } + return true; + } + }); + + return multiplexer; + } + + private void setState(State state) + { + switch (state) { + case DONE: + app.switchToMenu(); + } + this.state = state; + } +} |