diff options
Diffstat (limited to 'test')
5 files changed, 88 insertions, 28 deletions
diff --git a/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java b/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java index 4c51780..b3963fb 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java @@ -135,7 +135,7 @@ public abstract class AbstractScreen implements Screen paused = false; } - protected abstract void onTouch(int x, int y); + protected abstract void onTouch(int x, int y, boolean down); protected abstract void onZoom(float dz); protected abstract void onDragged(int dx, int dy); private InputMultiplexer getMultiplexer() @@ -147,19 +147,31 @@ public abstract class AbstractScreen implements Screen onZoom(amount * ZOOM_SCROLL_FACTOR); return true; } + + @Override public boolean touchUp(int x, int y, int pointer, int button) + { + if (inputBlocked) return true; + if (button == Input.Buttons.LEFT) { + dragPos.set(x, y); + onTouch(x, y, false); + } + 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); + onTouch(x, y, true); } return true; } + @Override public boolean touchDragged(int x, int y, int pointer) { - int dx = (int) (dragPos.x - x); - int dy = (int) (dragPos.y - y); + int dx = (int) (x - dragPos.x); + int dy = (int) (y - dragPos.y); dragPos.set(x, y); onDragged(dx, dy); return true; diff --git a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java index 4013273..fc6fc30 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java @@ -1,7 +1,6 @@ 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.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.Texture; @@ -223,5 +222,5 @@ public class AnimationsScreen extends AbstractScreen @Override protected void onZoom(float dz) { } @Override protected void onDragged(int dx, int dy) { } - @Override protected void onTouch(int x, int y) { } + @Override protected void onTouch(int x, int y, boolean down) { } } diff --git a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java index 086133d..6d762fa 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java @@ -1,7 +1,6 @@ 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.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.Texture; @@ -29,6 +28,7 @@ public class BoardScreen extends AbstractScreen private final Piece panzer; private final Vector2 pos; private final Vector2 v; + private boolean dragging; public Texture map; public Board board; public TileStorage tileStorage; @@ -40,6 +40,7 @@ public class BoardScreen extends AbstractScreen public MyBoard(final Assets assets) { + this.dragging = false; this.assets = assets; this.pos = new Vector2(); this.v = new Vector2(); @@ -60,38 +61,75 @@ public class BoardScreen extends AbstractScreen public void reset() { - pos.set(0, 0); tilesToDraw.clear(); + v.set(0, 0); + pos.set(0, 0); + handleAdjacents(); board.centerOf(0, 0, v); panzer.centerOn(v.x, v.y); panzer.setRotation(Orientation.DEFAULT.r()); } - public boolean touch(float x, float y) + public boolean touch(float x, float y, boolean down) { board.toBoard(x, y, v); + if (!board.isOnMap((int)v.x, (int)v.y)) + return false; + if (down) { + Tile tile = getTile((int)v.x, (int)v.y); + if (!dragging && panzer.isOn(tile)) { + dragging = true; + clearAdjacents(); + } else { + touchInfo(x, y); + pos.set(v); + handleAdjacents(); + board.centerOf((int)v.x, (int)v.y, v); + panzer.centerOn(v.x, v.y); + panzer.setRotation(Orientation.fromR(panzer.getRotation()).left().r()); + GdxBoardTest.debug("BoardScreen", String.format(" => [%d;%d]", (int)v.x, (int)v.y)); + } + } else { + if (dragging) { + touchInfo(x, y); + handleAdjacents(); + panzer.dropOnBoard(board, v); + GdxBoardTest.debug("BoardScreen", String.format(" => [%d;%d]", (int)v.x, (int)v.y)); + dragging = false; + } + } + return true; + } + + private void touchInfo(float x, float y) + { GdxBoardTest.debug("BoardScreen", String.format("touchDown [%d;%d] => [%d;%d] => %d", (int)x, (int)y, (int)v.x, (int)v.y, board.genKey((int)v.x, (int)v.y))); float d0 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.TCHEBYCHEV); float d1 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.TAXICAB); float d2 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.EUCLIDEAN); - if (board.isOnMap((int)v.x, (int)v.y)) { - GdxBoardTest.debug("BoardScreen", String.format(" from [%d;%d] => %d :: %d :: %f", (int)pos.x, (int)pos.y, (int)d0, (int)d1, d2)); - pos.set(v); - handleAdjacents(); - board.centerOf((int)v.x, (int)v.y, v); - panzer.centerOn(v.x, v.y); - panzer.setRotation(Orientation.fromR(panzer.getRotation()).left().r()); - GdxBoardTest.debug("BoardScreen", String.format(" => [%d;%d]", (int)v.x, (int)v.y)); + GdxBoardTest.debug("BoardScreen", String.format(" from [%d;%d] => %d :: %d :: %f", (int)pos.x, (int)pos.y, (int)d0, (int)d1, d2)); + } + + public boolean drag(float dx, float dy) + { + if (dragging) { + panzer.translate(dx, dy); + return true; } - return true; + return false; } - private void handleAdjacents() + private void clearAdjacents() { for (Tile tile : board.getAdjacents()) { if (tile != null) tile.enableOverlay(12, false); } + } + + private void handleAdjacents() + { + clearAdjacents(); board.buildAdjacents((int)v.x, (int)v.y, this::getTile); for (Tile tile : board.getAdjacents()) { if (tile != null) { @@ -108,6 +146,7 @@ public class BoardScreen extends AbstractScreen private Tile getTile(int x, int y) { + if (!board.isOnMap(x, y)) return null; return tileStorage.getTile(x, y, board::genKey, this::buildTile); } @@ -206,10 +245,12 @@ public class BoardScreen extends AbstractScreen private final MyBoard board; private final Button btn; private final Root root; + private final Vector2 relative; public BoardScreen(final GdxBoardTest app) { super(app, ""); + this.relative = new Vector2(); 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( @@ -264,17 +305,25 @@ public class BoardScreen extends AbstractScreen @Override protected void onDragged(int dx, int dy) { - cam.translate(dx, dy); + cam.unprojectTranslation(dx, dy, relative); + if (!board.drag(relative.x, relative.y)) { + cam.translate(dx, dy); + } } - @Override protected void onTouch(int x, int y) + @Override protected void onTouch(int x, int y, boolean down) { - cam.unproject(x, y, boardTouch); - cam.unprojectHud(x, y, hudTouch); - if (btn.touch(hudTouch.x, hudTouch.y) != null) { - setState(state.next()); + if (down) { + cam.unproject(x, y, boardTouch); + cam.unprojectHud(x, y, hudTouch); + if (btn.touch(hudTouch.x, hudTouch.y) != null) { + setState(state.next()); + } else { + board.touch(boardTouch.x, boardTouch.y, true); + } } else { - board.touch(boardTouch.x, boardTouch.y); + cam.unproject(x, y, boardTouch); + board.touch(boardTouch.x, boardTouch.y, false); } } diff --git a/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java b/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java index b0a312a..fafb9e0 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java @@ -69,7 +69,7 @@ public class MenuScreen extends AbstractScreen @Override protected void onZoom(float dz) { } @Override protected void onDragged(int dx, int dy) { } - @Override protected void onTouch(int x, int y) + @Override protected void onTouch(int x, int y, boolean down) { hudTouch.set(x, y, 0); camera.unproject(hudTouch); diff --git a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java index b490b05..4120fac 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java @@ -200,7 +200,7 @@ public class UiScreen extends AbstractScreen } @Override protected void onZoom(float dz) { } - @Override protected void onTouch(int x, int y) + @Override protected void onTouch(int x, int y, boolean down) { hudTouch.set(x, y, 0); camera.unproject(hudTouch); |