From 8c82551adc21d4e4e51c45b97b939c541e5451a4 Mon Sep 17 00:00:00 2001 From: drdev Date: Tue, 16 Dec 2014 05:20:15 +0000 Subject: [PATCH] Support laying out and drawing tile map --- .../planarconquest/CommandCenterScreen.java | 49 +++++++------ .../planarconquest/ConquestPlaneMap.java | 73 ++++++++++++++++++- 2 files changed, 99 insertions(+), 23 deletions(-) diff --git a/forge-gui-mobile/src/forge/screens/planarconquest/CommandCenterScreen.java b/forge-gui-mobile/src/forge/screens/planarconquest/CommandCenterScreen.java index a2c2901775e..eb053b1e3d0 100644 --- a/forge-gui-mobile/src/forge/screens/planarconquest/CommandCenterScreen.java +++ b/forge-gui-mobile/src/forge/screens/planarconquest/CommandCenterScreen.java @@ -32,6 +32,9 @@ import forge.planarconquest.ConquestPlane; import forge.planarconquest.ConquestPlane.Region; import forge.planarconquest.ConquestPlaneData; import forge.planarconquest.ConquestPlaneData.RegionData; +import forge.planarconquest.ConquestPlaneMap; +import forge.planarconquest.ConquestPlaneMap.HexagonTile; +import forge.planarconquest.ConquestPlaneMap.IPlaneMapRenderer; import forge.planarconquest.ConquestPreferences.CQPref; import forge.planarconquest.IVCommandCenter; import forge.screens.FScreen; @@ -43,7 +46,6 @@ import forge.toolbox.FDisplayObject; import forge.toolbox.FEvent; import forge.toolbox.FScrollPane; import forge.toolbox.FEvent.FEventHandler; -import forge.toolbox.FScrollPane.ScrollBounds; import forge.toolbox.FLabel; import forge.util.Utils; @@ -85,6 +87,7 @@ public class CommandCenterScreen extends FScreen implements IVCommandCenter { public void update() { model = FModel.getConquest().getModel(); setHeaderCaption(model.getName()); + map.revalidate(); updateCurrentDay(); } @@ -173,44 +176,46 @@ public class CommandCenterScreen extends FScreen implements IVCommandCenter { btnEndDay.setSize(width / 2, btnEndDay.getAutoSizeBounds().height); btnEndDay.setPosition((width - btnEndDay.getWidth()) / 2, height - btnEndDay.getHeight()); - //btnEditDeck.setBounds(0, height - VPrompt.HEIGHT, VPrompt.BTN_WIDTH, VPrompt.HEIGHT); - //btnPortal.setBounds(width - VPrompt.BTN_WIDTH, height - VPrompt.HEIGHT, VPrompt.BTN_WIDTH, VPrompt.HEIGHT); + //float numCommanders = commanderRow.panels.length; + //float commanderWidth = (width - (numCommanders + 3) * COMMANDER_GAP) / numCommanders; + //float commanderRowHeight = commanderWidth * FCardPanel.ASPECT_RATIO + 2 * COMMANDER_ROW_PADDING; + //commanderRow.setBounds(0, btnEndDay.getTop() - PLANE_NAME_FONT.getLineHeight() - 2 * FLabel.DEFAULT_INSETS - commanderRowHeight, width, commanderRowHeight); - float numCommanders = commanderRow.panels.length; - float commanderWidth = (width - (numCommanders + 3) * COMMANDER_GAP) / numCommanders; - float commanderRowHeight = commanderWidth * FCardPanel.ASPECT_RATIO + 2 * COMMANDER_ROW_PADDING; - commanderRow.setBounds(0, btnEndDay.getTop() - PLANE_NAME_FONT.getLineHeight() - 2 * FLabel.DEFAULT_INSETS - commanderRowHeight, width, commanderRowHeight); - - map.setBounds(0, startY, width, commanderRow.getTop() - startY); + map.setBounds(0, startY, width, btnEndDay.getTop() - startY); } - private class PlaneMap extends FScrollPane { + private class PlaneMap extends FScrollPane implements IPlaneMapRenderer { private float zoom = 1; private FImage walker = (FImage)PlaneswalkerAchievements.getTrophyImage("Ajani Goldmane"); + private Graphics g; + private ConquestPlaneMap mapData; public PlaneMap() { } @Override protected ScrollBounds layoutAndGetScrollBounds(float visibleWidth, float visibleHeight) { - return new ScrollBounds(visibleWidth, visibleHeight); + if (model == null) { + mapData = null; + return new ScrollBounds(visibleWidth, visibleHeight); + } + mapData = model.getCurrentPlaneData().getMap(); + mapData.setTileWidth(visibleWidth / 4 * zoom); + return new ScrollBounds(mapData.getWidth(), mapData.getHeight()); } - protected void drawBackground(Graphics g) { - float w = getWidth(); - float h = getHeight(); - float tileWidth = w / 4 * zoom; - float tileHeight = tileWidth * FSkinImage.HEXAGON_TILE.getHeight() / FSkinImage.HEXAGON_TILE.getWidth(); - - float x = (w - tileWidth) / 2; - float y = (h - tileHeight) / 2; - drawTile(g, x, y, tileWidth, tileHeight); + protected void drawBackground(Graphics g0) { + if (mapData == null) { return; } + g = g0; + mapData.draw(this, getScrollLeft(), getScrollTop(), getWidth(), getHeight()); + g = null; } - private void drawTile(Graphics g, float x, float y, float w, float h) { + @Override + public void draw(HexagonTile tile, float x, float y, float w, float h) { g.drawImage(FSkinImage.HEXAGON_TILE, x, y, w, h); - float pedestalWidth = w * 0.8f; + float pedestalWidth = w * 0.2f;//0.8f; float pedestalHeight = pedestalWidth * walker.getHeight() / walker.getWidth(); g.drawImage(walker, x + (w - pedestalWidth) / 2, y + h / 2 - pedestalHeight * 0.8f, pedestalWidth, pedestalHeight); } diff --git a/forge-gui/src/main/java/forge/planarconquest/ConquestPlaneMap.java b/forge-gui/src/main/java/forge/planarconquest/ConquestPlaneMap.java index 2c3097a5407..874d5391a2d 100644 --- a/forge-gui/src/main/java/forge/planarconquest/ConquestPlaneMap.java +++ b/forge-gui/src/main/java/forge/planarconquest/ConquestPlaneMap.java @@ -3,6 +3,7 @@ package forge.planarconquest; import java.util.ArrayList; import java.util.List; +import forge.assets.FSkinProp; import forge.util.Aggregates; public class ConquestPlaneMap { @@ -10,6 +11,7 @@ public class ConquestPlaneMap { private final int gridSize; private final HexagonTile[][] grid; private int tileCount; + private float tileWidth, tileHeight; public ConquestPlaneMap(ConquestPlane plane) { int size = (int)Math.round(Math.sqrt(plane.getCardPool().size() / 3)); //use card pool size to determine grid size @@ -21,6 +23,75 @@ public class ConquestPlaneMap { generateRandomGrid(); } + public void setTileWidth(float tileWidth0) { + if (tileWidth == tileWidth0) { return; } + tileWidth = tileWidth0; + tileHeight = tileWidth * (float)FSkinProp.IMG_HEXAGON_TILE.getHeight() / (float)FSkinProp.IMG_HEXAGON_TILE.getWidth(); + } + + public float getWidth() { + return tileWidth * 0.75f * (gridSize - 1) + tileWidth; + } + public float getHeight() { + return tileHeight * gridSize + tileHeight / 2; + } + + public HexagonTile getTileAtPoint(float x, float y) { + //convert pixel to axial coordinates + x = (x - tileWidth / 2) / tileWidth; + double t1 = y / (tileWidth / 2), t2 = Math.floor(x + t1); + int r = (int)Math.floor((Math.floor(t1 - x) + t2) / 3); + int q = (int)Math.floor((Math.floor(2 * x + 1) + t2) / 3) - r; + + //convert axial to offset coordinates + r += (q - (q & 1)) / 2; + + if (r < 0 || q < 0 || q >= gridSize || r >= gridSize) { + return null; + } + return grid[q][r]; + } + + public void draw(IPlaneMapRenderer renderer, float scrollLeft, float scrollTop, float visibleWidth, float visibleHeight) { + float dx = tileWidth * 0.75f; + int startQ = getIndexInRange((int)Math.floor((scrollLeft - tileWidth) / dx) + 1); + int startR = getIndexInRange((int)Math.floor((scrollTop - tileHeight * 1.5f) / tileHeight) + 1); + int endQ = getIndexInRange((int)Math.floor((scrollLeft + visibleWidth - tileWidth) / dx) + 2); + int endR = getIndexInRange((int)Math.floor((scrollTop + visibleHeight - tileHeight * 1.5f) / tileHeight) + 2); + + float y; + float x = startQ * dx - scrollLeft; + float startY = startR * tileHeight - scrollTop; + for (int q = startQ; q <= endQ; q++) { + y = startY; + if (q % 2 == 1) { + y += tileHeight / 2; //odd columns start lower + } + for (int r = startR; r <= endR; r++) { + HexagonTile tile = grid[q][r]; + if (tile != null) { + renderer.draw(tile, x, y, tileWidth, tileHeight); + } + y += tileHeight; + } + x += dx; + } + } + + private int getIndexInRange(int index) { + if (index < 0) { + return 0; + } + if (index >= gridSize) { + return gridSize - 1; + } + return index; + } + + public static interface IPlaneMapRenderer { + void draw(HexagonTile tile, float x, float y, float w, float h); + } + private void generateRandomGrid() { int startQ = gridSize / 2; //player will start at center of grid always int startR = startQ; @@ -96,7 +167,7 @@ public class ConquestPlaneMap { } private void addToList(int q, int r, List list) { - if (r < 0 || q < 0 || q >= grid.length || r >= grid[0].length) { + if (r < 0 || q < 0 || q >= gridSize || r >= gridSize) { return; } HexagonTile tile = grid[q][r];