diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2018-10-02 09:59:03 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2018-10-02 09:59:03 +0200 |
commit | 9e6b127d1aad4dc9890b525b852ece2a3f9a2038 (patch) | |
tree | 83bdf033bbbcd5951ef6fecff1d9b944fedc8051 /test/src/ch/asynk/gdx/tabletop | |
parent | dd08d9539409966205441ecc90ef1054e73c26b5 (diff) | |
download | gdx-boardgame-9e6b127d1aad4dc9890b525b852ece2a3f9a2038.zip gdx-boardgame-9e6b127d1aad4dc9890b525b852ece2a3f9a2038.tar.gz |
test : fix path to respect package name
Diffstat (limited to 'test/src/ch/asynk/gdx/tabletop')
-rw-r--r-- | test/src/ch/asynk/gdx/tabletop/test/Assets.java | 84 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/tabletop/test/BoardScreen.java | 331 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/tabletop/test/DesktopLauncher.java | 13 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/tabletop/test/GdxBoardTest.java | 97 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/tabletop/test/LoadingScreen.java | 168 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/tabletop/test/MenuScreen.java | 180 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/tabletop/test/UiScreen.java | 209 |
7 files changed, 1082 insertions, 0 deletions
diff --git a/test/src/ch/asynk/gdx/tabletop/test/Assets.java b/test/src/ch/asynk/gdx/tabletop/test/Assets.java new file mode 100644 index 0000000..112d2ae --- /dev/null +++ b/test/src/ch/asynk/gdx/tabletop/test/Assets.java @@ -0,0 +1,84 @@ +package ch.asynk.gdx.tabletop.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.tabletop.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 TRI = "triangle.png"; + 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"; + public static final String SHERMAN = "sherman.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 loadApp() + { + load(MAP_00, Texture.class); + load(CHESS, Texture.class); + load(TRI, Texture.class); + load(CORNER, Texture.class); + load(PATCH, Texture.class); + load(SHERMAN, Texture.class); + load(FONT_20, BitmapFont.class, params20); + load(FONT_25, BitmapFont.class, params25); + } + + public void unloadApp() + { + unload(MAP_00); + unload(CHESS); + unload(TRI); + unload(CORNER); + unload(PATCH); + unload(SHERMAN); + unload(FONT_20); + unload(FONT_25); + } +} diff --git a/test/src/ch/asynk/gdx/tabletop/test/BoardScreen.java b/test/src/ch/asynk/gdx/tabletop/test/BoardScreen.java new file mode 100644 index 0000000..3ef9dea --- /dev/null +++ b/test/src/ch/asynk/gdx/tabletop/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/tabletop/test/DesktopLauncher.java b/test/src/ch/asynk/gdx/tabletop/test/DesktopLauncher.java new file mode 100644 index 0000000..73e3e17 --- /dev/null +++ b/test/src/ch/asynk/gdx/tabletop/test/DesktopLauncher.java @@ -0,0 +1,13 @@ +package ch.asynk.gdx.tabletop.test; + +import com.badlogic.gdx.backends.lwjgl.LwjglApplication; +import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; + +public class DesktopLauncher { + public static void main (String[] arg) { + LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); + new LwjglApplication(new GdxBoardTest(), config); + config.width=800; + config.height=600; + } +} diff --git a/test/src/ch/asynk/gdx/tabletop/test/GdxBoardTest.java b/test/src/ch/asynk/gdx/tabletop/test/GdxBoardTest.java new file mode 100644 index 0000000..7e6bcd4 --- /dev/null +++ b/test/src/ch/asynk/gdx/tabletop/test/GdxBoardTest.java @@ -0,0 +1,97 @@ +package ch.asynk.gdx.tabletop.test; + +import com.badlogic.gdx.Game; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Screen; + +public class GdxBoardTest extends Game +{ + private static final String DOM = "GdxBoardTest"; + + private enum State + { + NONE, + LOADING, + MENU, + UI, + BOARD, + EXIT + } + 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() + { + debug("dispose()"); + assets.clear(); + 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; + } + if (state == State.LOADING) { + assets.unloadLoading(); + } + if (state != State.NONE) { + getScreen().dispose(); + } + setScreen(nextScreen); + this.state = nextState; + } + + public void switchToLoading() + { + assets.loadLoading(); + assets.finishLoading(); + 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 switchToExit() + { + Gdx.app.exit(); + switchTo(null, State.EXIT); + } +} diff --git a/test/src/ch/asynk/gdx/tabletop/test/LoadingScreen.java b/test/src/ch/asynk/gdx/tabletop/test/LoadingScreen.java new file mode 100644 index 0000000..61dce4c --- /dev/null +++ b/test/src/ch/asynk/gdx/tabletop/test/LoadingScreen.java @@ -0,0 +1,168 @@ +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.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 static final String DOM = "LoadingScreen"; + + private final GdxBoardTest app; + private final SpriteBatch batch; + private final Color c; + private final OrthographicCamera camera; + + private final float BLINK_AFTER = 1f; + 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(DOM, "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("LoadingScreen", "pause() "); + } + + @Override public void resume() + { + GdxBoardTest.debug("LoadingScreen", "resume() "); + resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + paused = false; + } +} diff --git a/test/src/ch/asynk/gdx/tabletop/test/MenuScreen.java b/test/src/ch/asynk/gdx/tabletop/test/MenuScreen.java new file mode 100644 index 0000000..892182a --- /dev/null +++ b/test/src/ch/asynk/gdx/tabletop/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/tabletop/test/UiScreen.java b/test/src/ch/asynk/gdx/tabletop/test/UiScreen.java new file mode 100644 index 0000000..87eb220 --- /dev/null +++ b/test/src/ch/asynk/gdx/tabletop/test/UiScreen.java @@ -0,0 +1,209 @@ +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.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; + } +} |