diff options
Diffstat (limited to 'test/src/ch/asynk/gdx/boardgame')
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java | 168 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/Assets.java | 84 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java | 240 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/DesktopLauncher.java | 14 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/GdxBoardTest.java | 97 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/LoadingScreen.java | 168 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java | 86 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/UiScreen.java | 103 |
8 files changed, 960 insertions, 0 deletions
diff --git a/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java b/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java new file mode 100644 index 0000000..e564790 --- /dev/null +++ b/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java @@ -0,0 +1,168 @@ +package ch.asynk.gdx.boardgame.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.Camera; +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.boardgame.ui.Root; + +public abstract class AbstractScreen implements Screen +{ + protected final float WORLD_RATIO = 0.5f; + 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; + + protected final Vector2 dragPos = new Vector2(); + protected final Vector3 boardTouch = new Vector3(); + protected final Vector3 hudTouch = new Vector3(); + + protected final String dom; + protected final GdxBoardTest app; + protected final SpriteBatch batch; + protected final Texture bg; + protected final Root root; + protected Camera camera; + protected boolean inputBlocked; + protected float inputDelay; + protected boolean paused; + + protected abstract void feed(); + public AbstractScreen(final GdxBoardTest app, final String dom) + { + this.app = app; + this.dom = dom; + this.batch = new SpriteBatch(); + this.bg = app.assets.getTexture(app.assets.MAP_00); + this.root = new Root(1); + this.root.setPadding(15); + feed(); + Gdx.input.setInputProcessor(getMultiplexer()); + this.inputBlocked = false; + this.inputDelay = 0f; + this.paused = false; + } + + protected abstract void draw(SpriteBatch batch); + @Override public void render(float delta) + { + if (paused) return; + + Gdx.gl.glClearColor(1, 1, 1, 1); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + + camera.update(); + batch.setProjectionMatrix(camera.combined); + batch.begin(); + draw(batch); + batch.end(); + } + + @Override public void resize(int width, int height) + { + GdxBoardTest.debug(dom, String.format("resize (%d,%d)",width, height)); + if (width >= height) { + camera.viewportWidth = bg.getWidth(); + camera.viewportHeight = bg.getHeight() / (float)width * (float)height; + } else { + camera.viewportHeight = bg.getHeight(); + camera.viewportWidth = bg.getWidth() / (float)height * (float)width; + } + camera.viewportWidth *= WORLD_RATIO; + camera.viewportHeight *= WORLD_RATIO; + camera.update(); + root.resize( + camera.position.x - (camera.viewportWidth / 2f), + camera.position.y - (camera.viewportHeight / 2f), + camera.viewportWidth, + camera.viewportHeight + ); + } + + @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; + } + + protected abstract void onTouch(int x, int y); + protected abstract void onZoom(float dz); + protected abstract void onDragged(int dx, int dy); + private InputMultiplexer getMultiplexer() + { + final InputMultiplexer multiplexer = new InputMultiplexer(); + multiplexer.addProcessor(new InputAdapter() { + @Override public boolean scrolled(int amount) + { + onZoom(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); + onTouch(x, 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); + onDragged(dx, dy); + return true; + } + }); + multiplexer.addProcessor(new GestureDetector(new GestureAdapter() { + @Override public boolean zoom(float initialDistance, float distance) + { + if (initialDistance > distance) + onZoom(ZOOM_GESTURE_FACTOR); + else + onZoom(-ZOOM_GESTURE_FACTOR); + inputBlocked = true; + inputDelay = INPUT_DELAY; + return true; + } + })); + + return multiplexer; + } +} diff --git a/test/src/ch/asynk/gdx/boardgame/test/Assets.java b/test/src/ch/asynk/gdx/boardgame/test/Assets.java new file mode 100644 index 0000000..0c8ba5c --- /dev/null +++ b/test/src/ch/asynk/gdx/boardgame/test/Assets.java @@ -0,0 +1,84 @@ +package ch.asynk.gdx.boardgame.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.boardgame.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/boardgame/test/BoardScreen.java b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java new file mode 100644 index 0000000..876c2e8 --- /dev/null +++ b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java @@ -0,0 +1,240 @@ +package ch.asynk.gdx.boardgame.test; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.math.Vector2; + +import ch.asynk.gdx.boardgame.Camera; +import ch.asynk.gdx.boardgame.Board; +import ch.asynk.gdx.boardgame.board.BoardFactory; +import ch.asynk.gdx.boardgame.ui.Alignment; +import ch.asynk.gdx.boardgame.ui.Button; +import ch.asynk.gdx.boardgame.ui.Root; + +public class BoardScreen extends AbstractScreen +{ + + 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("BoardScreen", 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("BoardScreen", 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); + } + } + + 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; + + private Camera cam; + private MyBoard board; + private Button btn; + private Root root; + + public BoardScreen(final GdxBoardTest app) + { + super(app, ""); + } + + @Override protected void feed() + { + this.board = new MyBoard(app.assets); + this.camera = this.cam = 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.setAlignment(Alignment.BOTTOM_RIGHT); + this.btn.write("next"); + this.root = new Root(1); + this.root.add(btn); + this.root.setPadding(5); + setState(State.HEX_V); + } + + @Override public void draw(SpriteBatch batch) { } + @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); + + cam.applyBoardViewport(); + batch.setProjectionMatrix(cam.combined); + batch.begin(); + board.draw(batch); + batch.end(); + + cam.applyHudViewport(); + batch.setProjectionMatrix(cam.getHudMatrix()); + batch.begin(); + root.draw(batch); + batch.end(); + } + + @Override public void resize(int width, int height) + { + GdxBoardTest.debug("BoardScrean", String.format("resize (%d,%d)",width, height)); + cam.updateViewport(width, height); + root.resize(cam.getHud()); + } + + @Override protected void onDragged(int dx, int dy) + { + cam.translate(dx, dy); + } + + @Override protected void onTouch(int x, int y) + { + cam.unproject(x, y, boardTouch); + cam.unprojectHud(x, y, hudTouch); + if (btn.touch(hudTouch.x, hudTouch.y)) { + setState(state.next()); + } else { + board.touch(boardTouch.x, boardTouch.y); + } + } + + @Override protected void onZoom(float dz) + { + cam.zoom(dz); + resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + } + + 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: + cam.applyScreenViewport(); + this.app.switchToMenu(); + return; + } + board.reset(); + cam.setDimension(board.w, board.h); + onZoom(1); + this.state = state; + } +} diff --git a/test/src/ch/asynk/gdx/boardgame/test/DesktopLauncher.java b/test/src/ch/asynk/gdx/boardgame/test/DesktopLauncher.java new file mode 100644 index 0000000..2c9066f --- /dev/null +++ b/test/src/ch/asynk/gdx/boardgame/test/DesktopLauncher.java @@ -0,0 +1,14 @@ +package ch.asynk.gdx.boardgame.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.title = "gdx-boardgame Demo"; + config.width=800; + config.height=600; + } +} diff --git a/test/src/ch/asynk/gdx/boardgame/test/GdxBoardTest.java b/test/src/ch/asynk/gdx/boardgame/test/GdxBoardTest.java new file mode 100644 index 0000000..307f961 --- /dev/null +++ b/test/src/ch/asynk/gdx/boardgame/test/GdxBoardTest.java @@ -0,0 +1,97 @@ +package ch.asynk.gdx.boardgame.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/boardgame/test/LoadingScreen.java b/test/src/ch/asynk/gdx/boardgame/test/LoadingScreen.java new file mode 100644 index 0000000..69c7e11 --- /dev/null +++ b/test/src/ch/asynk/gdx/boardgame/test/LoadingScreen.java @@ -0,0 +1,168 @@ +package ch.asynk.gdx.boardgame.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/boardgame/test/MenuScreen.java b/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java new file mode 100644 index 0000000..be9ec72 --- /dev/null +++ b/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java @@ -0,0 +1,86 @@ +package ch.asynk.gdx.boardgame.test; + +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.graphics.OrthographicCamera; + +import ch.asynk.gdx.boardgame.ui.Alignment; +import ch.asynk.gdx.boardgame.ui.Menu; + +public class MenuScreen extends AbstractScreen +{ + private Sprite corner; + private Menu menu; + + public MenuScreen(final GdxBoardTest app) + { + super(app, "MenuScreen"); + } + + @Override protected void feed() + { + final Assets assets = app.assets; + this.corner = new Sprite(assets.getTexture(assets.CORNER)); + + 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.camera = new OrthographicCamera(bg.getWidth() * WORLD_RATIO, bg.getHeight() * WORLD_RATIO); + this.camera.position.set(bg.getWidth() / 2f, bg.getHeight() / 2f, 0); + this.camera.update(); + } + + @Override protected void draw(SpriteBatch batch) + { + batch.draw(bg, 0, 0); + drawCorners(batch); + root.draw(batch); + } + + private void drawCorners(SpriteBatch batch) + { + float right = root.getInnerX() + root.getInnerWidth() - corner.getWidth(); + float top = root.getInnerY() + root.getInnerHeight() - corner.getHeight(); + corner.setRotation(0); + corner.setPosition(root.getInnerX(), top); + corner.draw(batch); + corner.setRotation(90); + corner.setPosition(root.getInnerX(), root.getInnerY()); + corner.draw(batch); + corner.setRotation(180); + corner.setPosition(right, root.getInnerY()); + corner.draw(batch); + corner.setPosition(right, top); + corner.setRotation(270); + corner.draw(batch); + } + + @Override protected void onZoom(float dz) { } + @Override protected void onDragged(int dx, int dy) { } + @Override protected void onTouch(int x, int y) + { + hudTouch.set(x, y, 0); + camera.unproject(hudTouch); + if (root.touch(hudTouch.x, hudTouch.y)) { + switch(menu.touched()) { + case 0: + app.switchToUi(); + break; + case 1: + app.switchToBoard(); + break; + case 2: + app.switchToExit(); + break; + } + } + } +} diff --git a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java new file mode 100644 index 0000000..5292af8 --- /dev/null +++ b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java @@ -0,0 +1,103 @@ +package ch.asynk.gdx.boardgame.test; + +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.NinePatch; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.OrthographicCamera; + +import ch.asynk.gdx.boardgame.ui.Alignment; +import ch.asynk.gdx.boardgame.ui.Button; + +public class UiScreen extends AbstractScreen +{ + private Button hello; + + public UiScreen(final GdxBoardTest app) + { + super(app, "UiScreen"); + } + + public enum State + { + POSITIONS, DONE; + public State next() + { + switch(this) { + case POSITIONS: + return DONE; + default: + return POSITIONS; + } + } + } + private State state; + + @Override protected void feed() + { + final NinePatch patch = app.assets.getNinePatch(app.assets.PATCH, 23, 23, 23 ,23); + final BitmapFont font = app.assets.getFont(app.assets.FONT_25); + + this.hello = new Button(font, patch, 10, 15); + this.hello.write("Hello"); + this.root.add(this.hello); + + this.camera = new OrthographicCamera(bg.getWidth() * WORLD_RATIO, bg.getHeight() * WORLD_RATIO); + this.camera.position.set(bg.getWidth() / 2f, bg.getHeight() / 2f, 0); + this.camera.update(); + setState(State.POSITIONS); + } + + private void setState(State state) + { + switch (state) { + case DONE: + app.switchToMenu(); + } + this.state = state; + } + + @Override protected void draw(SpriteBatch batch) + { + batch.draw(bg, 0, 0); + switch (state) { + case POSITIONS: + drawButtons(batch); + break; + } + } + + private void drawButtons(SpriteBatch 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(SpriteBatch batch, Alignment alignment1, Alignment alignment2) + { + hello.setAlignment(alignment1); + hello.setLabelAlignment(alignment2); + hello.draw(batch); + } + + @Override protected void onZoom(float dz) { } + @Override protected void onDragged(int dx, int dy) { } + @Override protected void onTouch(int x, int y) + { + hudTouch.set(x, y, 0); + camera.unproject(hudTouch); + if (root.touch(hudTouch.x, hudTouch.y)) { + setState(state.next()); + } + } +} |