From: Stefan F. <ste...@us...> - 2010-06-24 21:48:16
|
Update of /cvsroot/rails/18xx/rails/ui/swing/hexmap In directory sfp-cvsdas-4.v30.ch3.sourceforge.com:/tmp/cvs-serv25799/rails/ui/swing/hexmap Modified Files: EWHexMap.java GUIHex.java NSHexMap.java HexMap.java Log Message: Refactoring of HexMap classes to remove zoom problems Added autoscrolling support Added gamespecific config settings Index: NSHexMap.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/NSHexMap.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** NSHexMap.java 14 Mar 2010 09:14:04 -0000 1.14 --- NSHexMap.java 24 Jun 2010 21:48:08 -0000 1.15 *************** *** 3,10 **** import java.awt.*; - import java.util.ArrayList; - - import rails.game.MapHex; - import rails.ui.swing.Scale; /** --- 3,6 ---- *************** *** 16,129 **** public NSHexMap() { ! cx = 0; ! cy = -scale / 2; ! scale = defaultScale = Scale.get(); ! ! } ! ! @Override ! protected void setupHexesGUI() { ! hexes = new ArrayList<GUIHex>(); ! ! hexArray = mapManager.getHexes(); ! MapHex mh; ! int ii, jj; ! h = new GUIHex[hexArray.length][hexArray[0].length]; ! for (int i = minX; i < hexArray.length; i++) { ! ii = i - minX + 1; ! for (int j = minY; j < hexArray[0].length; j++) { ! jj = j - minY + 1; ! mh = hexArray[i][j]; ! if (mh != null) { ! GUIHex hex = ! new GUIHex(this, Math.round(cx + 3 * ii ! * scale), ! (int) Math.round(cy + (2 * jj + (ii & 1)) ! * GUIHex.SQRT3 * scale), ! scale, ii, jj); ! ! hex.setHexModel(mh); ! hex.originalTileId = hex.currentTileId; ! hexesByName.put(mh.getName(), hex); ! ! h[i][j] = hex; ! hexes.add(hex); ! } ! } ! } ! setSize(); } ! protected void setSize() { ! preferredSize = ! new Dimension( ! (int) Math.round((hexArray.length + 1) * 3 * scale * zoomFactor), ! (int) Math.round((hexArray[0].length + 1) * 2 ! * GUIHex.SQRT3 * scale * zoomFactor)); } ! ! @Override ! protected void scaleHexesGUI() { ! ! hexArray = mapManager.getHexes(); ! GUIHex hex; ! int ii, jj; ! ! for (int i = 0; i < hexArray.length; i++) { ! ii = i - minX + 1; ! for (int j = 0; j < hexArray[0].length; j++) { ! jj = j - minY + 1; ! hex = h[i][j]; ! if (hex != null) { ! hex.scaleHex(cx + 3 * ii * scale, ! cy + (2 * jj + (ii & 1)) * GUIHex.SQRT3 * scale, ! scale, zoomFactor); ! } ! } ! } ! setSize(); } ! @Override ! public void paint(Graphics g) { ! ! super.paint(g); ! Graphics2D g2 = (Graphics2D) g; ! String label; ! int ii, jj; ! ! boolean lettersGoHorizontal = mapManager.lettersGoHorizontal(); ! int xOffset = mapManager.letterAHasEvenNumbers() ? 1 : 0; ! int xLeft = cx + 10; ! int xRight = (cx + 5 + 3 * scale * hexArray.length); ! int yTop = 20; ! int yBottom = (int)(cy + 20 + 2 * hexArray[0].length * GUIHex.SQRT3 * scale); ! ! for (int i = 1; i < hexArray.length; i++) { ! ii = i - minX + 1; ! label = lettersGoHorizontal ! ? String.valueOf((char)('@'+i)) ! : String.valueOf(i); ! g2.drawString(label, ! (cx - 30 -3*label.length() + 3 * scale * (ii + xOffset)), ! yTop); ! g2.drawString(label, ! (cx - 30 -3*label.length() + 3 * scale * (ii + xOffset)), ! yBottom); ! } ! ! for (int j = 1; j < 2 * hexArray[0].length; j++) { ! jj = j - minY + 1; ! label = lettersGoHorizontal ! ? String.valueOf(j) ! : String.valueOf((char)('@'+j)); ! g2.drawString(label, ! xLeft, ! (int)(cy + 56 + jj * GUIHex.SQRT3 * scale)); ! g2.drawString(label, ! xRight, ! (int)(cy + 56 + jj * GUIHex.SQRT3 * scale)); ! } } } --- 12,41 ---- public NSHexMap() { ! // tile x-reference in NS is 1/3 of the baseline ! tileXOffset = -0.333; ! // tile y-reference in NS is baseline ! tileYOffset = -0.5; ! // coordinate margins ! coordinateXMargin = coordinatePeakMargin; ! coordinateYMargin = coordinateFlatMargin; } ! protected double calcXCoordinates(int col, double offset) { ! double colAdj = col - minCol + peakMargin + offset; ! // log.debug("x-Coordinate for col= " + col + " -> colAdj = " + colAdj); ! return Math.round(scale * 3 * colAdj); } ! ! protected double calcYCoordinates(int row, double offset) { ! double rowAdj = (row - minRow)/2.0 + flatMargin + offset; ! // log.debug("y-Coordinate for row= " + row + " -> rowAdj = " + rowAdj); ! return Math.round(scale * 2 * GUIHex.SQRT3 * rowAdj); } ! protected void setSize() { ! preferredSize = new Dimension( (int) calcXCoordinates(maxCol, peakMargin), ! (int) calcYCoordinates(maxRow, flatMargin)); } + } Index: EWHexMap.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/EWHexMap.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** EWHexMap.java 21 Mar 2010 12:55:33 -0000 1.15 --- EWHexMap.java 24 Jun 2010 21:48:08 -0000 1.16 *************** *** 3,10 **** import java.awt.*; - import java.util.ArrayList; - - import rails.game.MapHex; - import rails.ui.swing.Scale; /** --- 3,6 ---- *************** *** 16,134 **** public EWHexMap() { ! scale = defaultScale = 2 * Scale.get(); ! //cx = scale / 2; ! cx = -scale/2; ! cy = scale/2; ! } ! ! @Override ! protected void setupHexesGUI() { ! ! hexes = new ArrayList<GUIHex>(); ! ! hexArray = mapManager.getHexes(); ! MapHex mh; ! int ii, jj; ! h = new GUIHex[hexArray.length][hexArray[0].length]; ! for (int i = minX; i < hexArray.length; i++) { ! ii = i - minX + 1; ! for (int j = minY; j < hexArray[0].length; j++) { ! jj = j - minY + 1; ! mh = hexArray[i][j]; ! if (mh != null) { ! GUIHex hex = ! new GUIHex( ! this, ! (cx + scale ! * ((GUIHex.SQRT3 * ii) + (GUIHex.SQRT3 / 2 * (j & 1)))), ! (cy + jj * 1.5 * scale), scale, ii, jj); ! ! hex.setHexModel(mh); ! hex.originalTileId = hex.currentTileId; ! hexesByName.put(mh.getName(), hex); ! ! h[i][j] = hex; ! hexes.add(hex); ! } ! } ! } ! setSize(); ! } ! protected void setSize() { ! preferredSize = ! new Dimension( ! (int) Math.round((maxX-minX + 3) * GUIHex.SQRT3 ! * scale * zoomFactor), ! (int) Math.round((maxY-minY + 3) * 1.5 * scale * zoomFactor)); } ! ! @Override ! protected void scaleHexesGUI () { ! ! hexArray = mapManager.getHexes(); ! GUIHex hex; ! int ii, jj; ! for (int i = minX; i < hexArray.length; i++) { ! ii = i - minX + 1; ! for (int j = minY; j < hexArray[0].length; j++) { ! jj = j - minY + 1; ! hex = h[i][j]; ! if (hex != null) { ! hex.scaleHex(cx + scale * ((GUIHex.SQRT3 * ii) + (GUIHex.SQRT3 / 2 * (j & 1))), ! cy + jj * 1.5 * scale, ! scale, zoomFactor); ! ! } ! } ! } ! ! setSize(); } ! @Override ! public void paint(Graphics g) { ! ! super.paint(g); ! Graphics2D g2 = (Graphics2D) g; ! String label; ! int ii, jj; ! ! boolean lettersGoHorizontal = mapManager.lettersGoHorizontal(); ! int xOffset = (mapManager.letterAHasEvenNumbers() ? 0 : 1) + (minCol % 2); ! int xLeft = cx + scale; ! int xRight = (int)(cx + scale * (3 + GUIHex.SQRT3/2 * (maxCol-minCol+1))); ! int yTop = cy; ! int yBottom = (int)(cy + scale + (maxRow-minRow+1) * 1.5 * scale); ! ! for (int i = minCol; i <= maxCol; i++) { ! ii = i - minCol + 1; ! label = lettersGoHorizontal ! ? String.valueOf((char)('@'+i)) ! : String.valueOf(i); ! g2.drawString(label, ! (int) (cx + (26-3*label.length()) + scale * (GUIHex.SQRT3/2 * (ii + xOffset))), ! yTop); ! g2.drawString(label, ! (int) (cx + (26-3*label.length()) + scale * (GUIHex.SQRT3/2 * (ii + xOffset))), ! yBottom); ! } ! ! for (int j = minRow; j <= maxRow; j++) { ! jj = j - minRow + 1; ! label = lettersGoHorizontal ! ? String.valueOf(j) ! : String.valueOf((char)('@'+j)); ! g2.drawString(label, ! xLeft, ! (int)(cy - 10 + jj * 1.5 * scale)); ! g2.drawString(label, ! xRight, ! (int)(cy - 10 + jj * 1.5 * scale)); ! } ! ! } --- 12,43 ---- public EWHexMap() { ! // tile x-reference in EW is left side ! tileXOffset = -0.5; ! // tile y-reference in EW is 1/3 of the baseline ! tileYOffset = 0.333; ! // coordinate margins ! coordinateXMargin = coordinateFlatMargin; ! coordinateYMargin = coordinatePeakMargin; } ! ! @Override ! protected double calcXCoordinates(int col, double offset) { ! double colAdj = (col - minCol)/2.0 + flatMargin + offset; ! // log.debug("x-Coordinate for col= " + col + " -> colAdj = " + colAdj); ! return Math.round(scale * 2 * GUIHex.SQRT3 * colAdj); } ! @Override ! protected double calcYCoordinates(int row, double offset) { ! double rowAdj = row - minRow + peakMargin+ offset; ! // log.debug("y-Coordinate for row= " + row + " -> rowAdj = " + rowAdj); ! return Math.round(scale * 3 * rowAdj); } ! @Override ! protected void setSize() { ! preferredSize = new Dimension( (int) calcXCoordinates(maxCol, flatMargin), ! (int) calcYCoordinates(maxRow, peakMargin)); } Index: HexMap.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/HexMap.java,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** HexMap.java 15 May 2010 22:44:46 -0000 1.26 --- HexMap.java 24 Jun 2010 21:48:08 -0000 1.27 *************** *** 16,19 **** --- 16,20 ---- import rails.ui.swing.GameUIManager; import rails.ui.swing.ORUIManager; + import rails.ui.swing.Scale; import rails.util.*; *************** *** 31,38 **** protected MapManager mapManager; - // Abstract Methods - protected abstract void setupHexesGUI(); - protected abstract void scaleHexesGUI(); - // GUI hexes need to be recreated for each object, since scale varies. protected GUIHex[][] h; --- 32,35 ---- *************** *** 41,50 **** protected Map<String, GUIHex> hexesByName = new HashMap<String, GUIHex>(); protected ArrayList<GUIHex> hexes; - protected int defaultScale; protected int scale; ! protected int zoomStep = 10; ! protected double zoomFactor = 1.0; ! protected int cx; ! protected int cy; protected GUIHex selectedHex = null; protected Dimension preferredSize; --- 38,48 ---- protected Map<String, GUIHex> hexesByName = new HashMap<String, GUIHex>(); protected ArrayList<GUIHex> hexes; protected int scale; ! protected int zoomStep = 10; // can be overwritten in config ! protected double zoomFactor = 1; // defined dynamically if zoomStep changed ! protected double peakMargin = 1.0; ! protected double flatMargin = 0.80; ! protected double coordinatePeakMargin = 0.80; ! protected double coordinateFlatMargin = 0.60; protected GUIHex selectedHex = null; protected Dimension preferredSize; *************** *** 76,80 **** protected int strokeCap = BasicStroke.CAP_ROUND; protected int strokeJoin = BasicStroke.JOIN_BEVEL; ! static { try { --- 74,89 ---- protected int strokeCap = BasicStroke.CAP_ROUND; protected int strokeJoin = BasicStroke.JOIN_BEVEL; ! ! // Abstract Methods, implemented depending on the map type (EW or NS) ! protected abstract double calcXCoordinates(int col, double offset); ! protected abstract double calcYCoordinates(int row, double offset); ! protected abstract void setSize(); ! ! // ("Abstract") Variables to be initialized by map type subclasses ! protected double tileXOffset; ! protected double tileYOffset; ! protected double coordinateXMargin; ! protected double coordinateYMargin; ! static { try { *************** *** 93,98 **** --- 102,109 ---- public void init(ORUIManager orUIManager, MapManager mapManager) { + this.orUIManager = orUIManager; this.mapManager = mapManager; + minX = mapManager.getMinX(); minY = mapManager.getMinY(); *************** *** 103,111 **** maxRow = mapManager.getMaxRow(); maxCol = mapManager.getMaxCol(); ! setupHexes(); trainColors = new Color[]{colour1, colour2, colour3, colour4}; } public void setupHexes() { setupHexesGUI(); --- 114,232 ---- maxRow = mapManager.getMaxRow(); maxCol = mapManager.getMaxCol(); ! log.debug("HexMap init: minX="+ minX + ",minY=" + minY + ",maxX=" +maxX + ",maxY=" + maxY); ! log.debug("HexMap init: minCol="+ minCol + ",minRow=" + minRow + ",maxCol=" +maxCol + ",maxRow=" + maxRow); + setScale(); + setupHexes(); + + initializeSettings(); + } + + /** + * defines settings from the config files + */ + private void initializeSettings() { trainColors = new Color[]{colour1, colour2, colour3, colour4}; + + // define zoomStep from config + String zoomStepSetting = Config.getGameSpecific("map.zoomstep"); + if (Util.hasValue(zoomStepSetting)) { + try { + int newZoomStep = Integer.parseInt(zoomStepSetting); + if (zoomStep != newZoomStep) { + zoomStep = newZoomStep; + zoom(); + } + } catch (NumberFormatException e) { + // otherwise keep default defined above + } + } + } + + protected void setupHexesGUI() { + + hexes = new ArrayList<GUIHex>(); + + hexArray = mapManager.getHexes(); + MapHex mh; + + h = new GUIHex[hexArray.length][hexArray[0].length]; + for (int i = minX; i < hexArray.length; i++) { + for (int j = minY; j < hexArray[0].length; j++) { + mh = hexArray[i][j]; + if (mh != null) { + GUIHex hex = new GUIHex(this, calcXCoordinates(mh.getColumn(), tileXOffset), + calcYCoordinates(mh.getRow(), tileYOffset), + scale, i-minX+1, j-minY+1); + hex.setHexModel(mh); + hex.originalTileId = hex.currentTileId; + hexesByName.put(mh.getName(), hex); + h[i][j] = hex; + hexes.add(hex); + } + } + } + setSize(); } + + protected void scaleHexesGUI () { + hexArray = mapManager.getHexes(); + GUIHex hex; + for (int i = minX; i < hexArray.length; i++) { + for (int j = minY; j < hexArray[0].length; j++) { + hex = h[i][j]; + MapHex mh = hexArray[i][j]; + if (hex != null) { + hex.scaleHex(calcXCoordinates(mh.getColumn(), tileXOffset), calcYCoordinates(mh.getRow(), tileYOffset), + scale, zoomFactor); + } + } + } + setSize(); + } + + protected void drawLabel(Graphics2D g2, int index, int xCoordinate, int yCoordinate, boolean letter) { + String label = letter + ? String.valueOf((char)('@'+index)) + : String.valueOf(index); + xCoordinate -= 4.0*label.length(); + yCoordinate += 4.0; + g2.drawString(label, + xCoordinate, + yCoordinate); + + // log.debug("Draw Label " + label + " for " + index + " at x = " + xCoordinate + ", y = " + yCoordinate); + } + + @Override + public void paint(Graphics g) { + + super.paint(g); + Graphics2D g2 = (Graphics2D) g; + + boolean lettersGoHorizontal = mapManager.lettersGoHorizontal(); + int xLeft = (int)calcXCoordinates(minCol, - coordinateXMargin); + int xRight = (int)calcXCoordinates(maxCol, coordinateXMargin); + + int yTop = (int)calcYCoordinates(minRow, - coordinateYMargin); + int yBottom = (int)calcYCoordinates(maxRow, coordinateYMargin); + + for (int iCol = minCol; iCol <= maxCol; iCol++) { + int xCoordinate = (int)(calcXCoordinates(iCol, 0)); + drawLabel(g2, iCol, xCoordinate, yTop, lettersGoHorizontal); + drawLabel(g2, iCol, xCoordinate, yBottom, lettersGoHorizontal); + } + + for (int iRow = minRow; iRow <= maxRow; iRow++) { + int yCoordinate = (int)(calcYCoordinates(iRow, 0)); + drawLabel(g2, iRow, xLeft, yCoordinate, !lettersGoHorizontal); + drawLabel(g2, iRow, xRight, yCoordinate, !lettersGoHorizontal); + } + + + } + + public void setupHexes() { setupHexesGUI(); *************** *** 201,208 **** scaleHexesGUI(); revalidate(); } protected void setScale() { ! scale = (int)(defaultScale * zoomFactor); } --- 322,330 ---- scaleHexesGUI(); revalidate(); + // orUIManager.getMapPanel().resizeMapPanel(); } protected void setScale() { ! scale = (int)(Scale.get() * zoomFactor); } Index: GUIHex.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/GUIHex.java,v retrieving revision 1.44 retrieving revision 1.45 diff -C2 -d -r1.44 -r1.45 *** GUIHex.java 20 Apr 2010 22:07:09 -0000 1.44 --- GUIHex.java 24 Jun 2010 21:48:08 -0000 1.45 *************** *** 82,86 **** double[] xVertex = new double[6]; double[] yVertex = new double[6]; ! double len; GeneralPath hexagon; Rectangle rectBound; --- 82,86 ---- double[] xVertex = new double[6]; double[] yVertex = new double[6]; ! // double len; GeneralPath hexagon; Rectangle rectBound; *************** *** 114,128 **** if (hexMap.getMapManager().getTileOrientation() == MapHex.EW) { ! len = scale; ! xVertex[0] = cx + SQRT3 / 2 * scale; ! yVertex[0] = cy + 0.5 * scale; ! xVertex[1] = cx + SQRT3 * scale; yVertex[1] = cy; ! xVertex[2] = cx + SQRT3 * scale; ! yVertex[2] = cy - 1 * scale; ! xVertex[3] = cx + SQRT3 / 2 * scale; ! yVertex[3] = cy - 1.5 * scale; xVertex[4] = cx; ! yVertex[4] = cy - 1 * scale; xVertex[5] = cx; yVertex[5] = cy; --- 114,128 ---- if (hexMap.getMapManager().getTileOrientation() == MapHex.EW) { ! // len = scale; ! xVertex[0] = cx + SQRT3 * scale; ! yVertex[0] = cy + scale; ! xVertex[1] = cx + 2 * SQRT3 * scale; yVertex[1] = cy; ! xVertex[2] = cx + 2 * SQRT3 * scale; ! yVertex[2] = cy - 2 * scale; ! xVertex[3] = cx + SQRT3 * scale; ! yVertex[3] = cy - 3 * scale; xVertex[4] = cx; ! yVertex[4] = cy - 2 * scale; xVertex[5] = cx; yVertex[5] = cy; *************** *** 130,134 **** baseRotation = 30; // degrees } else { ! len = scale / 3.0; xVertex[0] = cx; yVertex[0] = cy; --- 130,134 ---- baseRotation = 30; // degrees } else { ! // len = scale / 3.0; xVertex[0] = cx; yVertex[0] = cy; |