From: Erik V. <ev...@us...> - 2009-12-15 18:56:22
|
Update of /cvsroot/rails/18xx/rails/ui/swing/hexmap In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv4117/rails/ui/swing/hexmap Modified Files: GUIHex.java HexMap.java NSHexMap.java GUITile.java EWHexMap.java Log Message: Map made zoomable. Index: NSHexMap.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/NSHexMap.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** NSHexMap.java 8 Dec 2009 19:31:49 -0000 1.10 --- NSHexMap.java 15 Dec 2009 18:56:11 -0000 1.11 *************** *** 18,21 **** --- 18,23 ---- cx = 0; cy = -scale / 2; + scale = defaultScale = Scale.get(); + } *************** *** 24,29 **** hexes = new ArrayList<GUIHex>(); - scale = Scale.get(); - hexArray = mapManager.getHexes(); MapHex mh; --- 26,29 ---- *************** *** 49,56 **** } } preferredSize = ! new Dimension((hexArray.length + 1) * 3 * scale, (int) Math.round((hexArray[0].length + 1) * 2 ! * GUIHex.SQRT3 * scale)); } --- 49,79 ---- } } + 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; ! for (int i = 0; i < hexArray.length; i++) { ! for (int j = 0; j < hexArray[0].length; j++) { ! hex = h[i][j]; ! if (hex != null) { ! hex.scaleHex(cx + 3 * i * scale, ! cy + (2 * j + (i & 1)) * GUIHex.SQRT3 * scale, ! scale, zoomFactor); ! } ! } ! } ! setSize(); } Index: EWHexMap.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/EWHexMap.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** EWHexMap.java 8 Dec 2009 19:31:49 -0000 1.11 --- EWHexMap.java 15 Dec 2009 18:56:11 -0000 1.12 *************** *** 6,9 **** --- 6,10 ---- import rails.game.MapHex; + import rails.ui.swing.Scale; /** *************** *** 15,18 **** --- 16,20 ---- public EWHexMap() { + scale = defaultScale = 2 * Scale.get(); cx = scale / 2; cy = 0; *************** *** 21,24 **** --- 23,27 ---- @Override protected void setupHexesGUI() { + hexes = new ArrayList<GUIHex>(); *************** *** 46,56 **** } } preferredSize = new Dimension( (int) Math.round((hexArray.length + 1) * GUIHex.SQRT3 ! * scale), ! (int) Math.round((hexArray[0].length + 1) * 1.5 * scale)); } @Override public void paint(Graphics g) { --- 49,84 ---- } } + setSize(); + + } + protected void setSize() { preferredSize = new Dimension( (int) Math.round((hexArray.length + 1) * GUIHex.SQRT3 ! * scale * zoomFactor), ! (int) Math.round((hexArray[0].length + 1) * 1.5 * scale * zoomFactor)); } + + @Override + protected void scaleHexesGUI () { + + hexArray = mapManager.getHexes(); + GUIHex hex; + for (int i = 0; i < hexArray.length; i++) { + for (int j = 0; j < hexArray[0].length; j++) { + hex = h[i][j]; + if (hex != null) { + hex.scaleHex(cx + scale * ((GUIHex.SQRT3 * i) + (GUIHex.SQRT3 / 2 * (j & 1))), + cy + j * 1.5 * scale, + scale, zoomFactor); + + } + } + } + + setSize(); + } + @Override public void paint(Graphics g) { Index: HexMap.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/HexMap.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** HexMap.java 6 Nov 2009 20:23:53 -0000 1.19 --- HexMap.java 15 Dec 2009 18:56:11 -0000 1.20 *************** *** 13,17 **** import rails.game.*; import rails.game.action.*; ! import rails.ui.swing.*; /** --- 13,18 ---- import rails.game.*; import rails.game.action.*; ! import rails.ui.swing.GameUIManager; ! import rails.ui.swing.ORUIManager; /** *************** *** 30,33 **** --- 31,35 ---- // Abstract Methods protected abstract void setupHexesGUI(); + protected abstract void scaleHexesGUI(); // GUI hexes need to be recreated for each object, since scale varies. *************** *** 37,42 **** protected Map<String, GUIHex> hexesByName = new HashMap<String, GUIHex>(); protected ArrayList<GUIHex> hexes; ! protected List<GUIBar> bars = new ArrayList<GUIBar>(); ! protected int scale = 2 * Scale.get(); protected int cx; protected int cy; --- 39,46 ---- 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; *************** *** 130,138 **** } catch (NullPointerException ex) { ! // If we try to paint before something is loaded, just retry ! // later. } } @Override public Dimension getMinimumSize() { --- 134,165 ---- } catch (NullPointerException ex) { ! // If we try to paint before something is loaded, just retry later. } } + public void zoomIn () { + zoomStep++; + zoom(); + } + public void zoomOut() { + zoomStep--; + zoom(); + } + + protected void zoom() { + zoomFactor = GameUIManager.getImageLoader().getZoomFactor(zoomStep); + setScale(); + scaleHexesGUI(); + revalidate(); + } + + protected void setScale() { + scale = (int)(defaultScale * zoomFactor); + } + + public int getZoomStep () { + return zoomStep; + } + @Override public Dimension getMinimumSize() { Index: GUITile.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/GUITile.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** GUITile.java 4 Nov 2009 20:33:22 -0000 1.18 --- GUITile.java 15 Dec 2009 18:56:11 -0000 1.19 *************** *** 33,36 **** --- 33,37 ---- protected MapHex hex = null; + protected GUIHex guiHex = null; protected static ImageLoader imageLoader = GameUIManager.getImageLoader(); *************** *** 43,51 **** Logger.getLogger(GUITile.class.getPackage().getName()); ! public GUITile(int tileId, MapHex hex) { this.tileId = tileId; ! this.hex = hex; tile = GameManager.getInstance().getTileManager().getTile(tileId); - tileImage = imageLoader.getTile(tileId); if (hex.getTileOrientation() == MapHex.EW) { --- 44,52 ---- Logger.getLogger(GUITile.class.getPackage().getName()); ! public GUITile(int tileId, GUIHex guiHex) { ! this.guiHex = guiHex; this.tileId = tileId; ! this.hex = (MapHex)guiHex.getModel(); tile = GameManager.getInstance().getTileManager().getTile(tileId); if (hex.getTileOrientation() == MapHex.EW) { *************** *** 233,237 **** public void paintTile(Graphics2D g2, int x, int y) { ! if (tileImage != null) { double radians = baseRotation + rotation * DEG60; --- 234,242 ---- public void paintTile(Graphics2D g2, int x, int y) { ! ! int zoomStep = guiHex.getHexMap().getZoomStep(); ! tileImage = imageLoader.getTile(tileId, zoomStep); ! ! if (tileImage != null) { double radians = baseRotation + rotation * DEG60; Index: GUIHex.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/GUIHex.java,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -d -r1.27 -r1.28 *** GUIHex.java 13 Dec 2009 21:14:12 -0000 1.27 --- GUIHex.java 15 Dec 2009 18:56:11 -0000 1.28 *************** *** 26,29 **** --- 26,34 ---- public static double SELECTED_SCALE = 0.8; + public static int NORMAL_TOKEN_SIZE = 15; + + public static Color BAR_COLOUR = Color.BLUE; + public static int BAR_WIDTH = 5; + public static void setScale(double scale) { NORMAL_SCALE = scale; *************** *** 35,38 **** --- 40,47 ---- protected static final Color highlightColor = Color.red; protected Point center; + /** x and y coordinates on the map */ + protected int x, y; + protected double zoomFactor = 1.0; + protected int tokenDiameter = NORMAL_TOKEN_SIZE; protected HexMap hexMap; // Containing this hex *************** *** 49,53 **** protected List<TokenI> offStationTokens; ! protected List<GUIBar> bars; protected GUIToken provisionalGUIToken = null; --- 58,62 ---- protected List<TokenI> offStationTokens; ! protected List<Integer> barStartPoints; protected GUIToken provisionalGUIToken = null; *************** *** 84,89 **** public GUIHex(HexMap hexMap, double cx, double cy, int scale, ! double xCoord, double yCoord) { this.hexMap = hexMap; if (hexMap.getMapManager().getTileOrientation() == MapHex.EW) { --- 93,109 ---- public GUIHex(HexMap hexMap, double cx, double cy, int scale, ! int xCoord, int yCoord) { this.hexMap = hexMap; + this.x = xCoord; + this.y = yCoord; + + scaleHex (cx, cy, scale, 1.0); + + } + + public void scaleHex (double cx, double cy, int scale, double zoomFactor) { + + this.zoomFactor = zoomFactor; + tokenDiameter = (int)Math.round(NORMAL_TOKEN_SIZE * zoomFactor); if (hexMap.getMapManager().getTileOrientation() == MapHex.EW) { *************** *** 154,157 **** --- 174,181 ---- } + public HexMap getHexMap() { + return hexMap; + } + public void setHexModel(MapHex model) { this.model = model; *************** *** 160,164 **** currentTileId = model.getCurrentTile().getId(); currentTileOrientation = model.getCurrentTileRotation(); ! currentGUITile = new GUITile(currentTileId, model); currentGUITile.setRotation(currentTileOrientation); setToolTip(); --- 184,188 ---- currentTileId = model.getCurrentTile().getId(); currentTileOrientation = model.getCurrentTileRotation(); ! currentGUITile = new GUITile(currentTileId, this); currentGUITile.setRotation(currentTileOrientation); setToolTip(); *************** *** 170,182 **** public void addBar (int orientation) { orientation %= 6; ! if (bars == null) bars = new ArrayList<GUIBar>(); int offset = hexMap.getMapManager().getTileOrientation() == MapHex.EW ? 0 : 4; ! int x1 = (int)xVertex[(offset+5-orientation)%6]; ! int y1 = (int)yVertex[(offset+5-orientation)%6]; ! int x2 = (int)xVertex[(offset+6-orientation)%6]; ! int y2 = (int)yVertex[(offset+6-orientation)%6]; ! GUIBar bar = new GUIBar (model.getName()+":"+orientation, ! x1, y1, x2, y2); ! bars.add(bar); } --- 194,200 ---- public void addBar (int orientation) { orientation %= 6; ! if (barStartPoints == null) barStartPoints = new ArrayList<Integer>(2); int offset = hexMap.getMapManager().getTileOrientation() == MapHex.EW ? 0 : 4; ! barStartPoints.add((offset+5-orientation)%6); } *************** *** 331,339 **** if (company.isClosed()) continue; city = homes.get(company); ! // Only draw the company name if there isn't yet a token of that company if (city.getTokens() != null) { for (TokenI token : city.getTokens()) { ! if (token instanceof BaseToken && ((BaseToken)token).getCompany() == company) { continue homes; --- 349,357 ---- if (company.isClosed()) continue; city = homes.get(company); ! // Only draw the company name if there isn't yet a token of that company if (city.getTokens() != null) { for (TokenI token : city.getTokens()) { ! if (token instanceof BaseToken && ((BaseToken)token).getCompany() == company) { continue homes; *************** *** 341,345 **** } } ! p = getTokenOrigin (1, 0, getHexModel().getCities().size(), city.getNumber()-1); //log.debug("+++ Home of "+company.getName()+" hex"+getName()+" city"+city.getName() --- 359,363 ---- } } ! p = getTokenOrigin (1, 0, getHexModel().getCities().size(), city.getNumber()-1); //log.debug("+++ Home of "+company.getName()+" hex"+getName()+" city"+city.getName() *************** *** 383,393 **** public void paintBars(Graphics g) { ! if (bars == null) return; Graphics2D g2 = (Graphics2D) g; ! for (GUIBar bar : bars) { ! bar.drawBar(g2); } } private void paintStationTokens(Graphics2D g2) { if (getHexModel().getCities().size() > 1) { --- 401,427 ---- public void paintBars(Graphics g) { ! if (barStartPoints == null) return; Graphics2D g2 = (Graphics2D) g; ! for (int startPoint : barStartPoints) { ! drawBar(g2, ! (int)Math.round(xVertex[startPoint]), ! (int)Math.round(yVertex[startPoint]), ! (int)Math.round(xVertex[(startPoint+1)%6]), ! (int)Math.round(yVertex[(startPoint+1)%6])); } } + protected void drawBar(Graphics2D g2d, int x1, int y1, int x2, int y2) { + Color oldColor = g2d.getColor(); + Stroke oldStroke = g2d.getStroke(); + + g2d.setColor(BAR_COLOUR); + g2d.setStroke(new BasicStroke(BAR_WIDTH)); + g2d.drawLine(x1, y1, x2, y2); + + g2d.setColor(oldColor); + g2d.setStroke(oldStroke); + } + private void paintStationTokens(Graphics2D g2) { if (getHexModel().getCities().size() > 1) { *************** *** 402,406 **** PublicCompanyI co = ((BaseToken) tokens.get(i)).getCompany(); Point origin = getTokenOrigin(numTokens, i, 1, 0); ! drawBaseToken(g2, co, origin); } } --- 436,440 ---- PublicCompanyI co = ((BaseToken) tokens.get(i)).getCompany(); Point origin = getTokenOrigin(numTokens, i, 1, 0); ! drawBaseToken(g2, co, origin, tokenDiameter); } } *************** *** 420,424 **** origin = getTokenOrigin(numTokens, j, numStations, i); co = ((BaseToken) tokens.get(j)).getCompany(); ! drawBaseToken(g2, co, origin); } } --- 454,458 ---- origin = getTokenOrigin(numTokens, j, numStations, i); co = ((BaseToken) tokens.get(j)).getCompany(); ! drawBaseToken(g2, co, origin, tokenDiameter); } } *************** *** 446,450 **** PublicCompanyI co = ((BaseToken) token).getCompany(); ! drawBaseToken(g2, co, origin); } else if (token instanceof BonusToken) { --- 480,484 ---- PublicCompanyI co = ((BaseToken) token).getCompany(); ! drawBaseToken(g2, co, origin, tokenDiameter); } else if (token instanceof BonusToken) { *************** *** 456,478 **** } ! private void drawBaseToken(Graphics2D g2, PublicCompanyI co, Point origin) { ! Dimension size = new Dimension(40, 40); GUIToken token = new GUIToken(co.getFgColour(), co.getBgColour(), co.getName(), ! origin.x, origin.y, 15); ! token.setBounds(origin.x, origin.y, size.width, size.height); token.drawToken(g2); } ! private void drawHome (Graphics2D g2, PublicCompanyI co, Point origin) { Font oldFont = g2.getFont(); Color oldColor = g2.getColor(); ! double tokenScale = 15.0 / 21.0; String name = co.getName(); ! Font font = GUIToken.getTokenFont(name.length()); g2.setFont(new Font("Helvetica", Font.BOLD, --- 490,512 ---- } ! private void drawBaseToken(Graphics2D g2, PublicCompanyI co, Point center, int diameter) { GUIToken token = new GUIToken(co.getFgColour(), co.getBgColour(), co.getName(), ! center.x, center.y, diameter); ! token.setBounds(center.x-(int)(0.5*diameter), center.y-(int)(0.5*diameter), ! diameter, diameter); token.drawToken(g2); } ! private void drawHome (Graphics2D g2, PublicCompanyI co, Point origin) { Font oldFont = g2.getFont(); Color oldColor = g2.getColor(); ! double tokenScale = 15.0 / 21.0; String name = co.getName(); ! Font font = GUIToken.getTokenFont(name.length()); g2.setFont(new Font("Helvetica", Font.BOLD, *************** *** 481,485 **** g2.drawString(name, (int) (origin.x + (12 - 3*name.length()) * tokenScale), (int) (origin.y + 14 * tokenScale)); ! g2.setColor(oldColor); g2.setFont(oldFont); --- 515,519 ---- g2.drawString(name, (int) (origin.x + (12 - 3*name.length()) * tokenScale), (int) (origin.y + 14 * tokenScale)); ! g2.setColor(oldColor); g2.setFont(oldFont); *************** *** 505,509 **** private Point getTokenOrigin(int numTokens, int currentToken, int numStations, int stationNumber) { ! Point p = new Point(center.x - 8, center.y - 8); int cityNumber = stationNumber + 1; --- 539,543 ---- private Point getTokenOrigin(int numTokens, int currentToken, int numStations, int stationNumber) { ! Point p = new Point(center.x, center.y); int cityNumber = stationNumber + 1; *************** *** 516,520 **** int positionCode = station.getPosition(); if (positionCode != 0) { ! y = 14; double r = Math.toRadians(30 * (positionCode / 50)); xx = x * Math.cos(r) + y * Math.sin(r); --- 550,554 ---- int positionCode = station.getPosition(); if (positionCode != 0) { ! y = 16 * zoomFactor; double r = Math.toRadians(30 * (positionCode / 50)); xx = x * Math.cos(r) + y * Math.sin(r); *************** *** 527,543 **** switch (station.getBaseSlots()) { case 2: ! x += -8 + 16 * currentToken; break; case 3: if (currentToken < 2) { ! x += -8 + 16 * currentToken; ! y += 8; } else { ! y -= 8; } break; case 4: ! x += -8 + 16 * currentToken % 2; ! y += 8 - 16 * currentToken / 2; } --- 561,577 ---- switch (station.getBaseSlots()) { case 2: ! x += (-8 + 16 * currentToken) * zoomFactor; break; case 3: if (currentToken < 2) { ! x += (-8 + 16 * currentToken) * zoomFactor; ! y += 8 * zoomFactor; } else { ! y -= 8 * zoomFactor; } break; case 4: ! x += (-8 + 16 * currentToken % 2) * zoomFactor; ! y += (8 - 16 * currentToken / 2) * zoomFactor; } *************** *** 666,670 **** this.upgradeMustConnect = upgradeMustConnect; ! provisionalGUITile = new GUITile(tileId, model); /* Check if we can find a valid orientation of this tile */ if (provisionalGUITile.rotate(0, currentGUITile, upgradeMustConnect)) { --- 700,704 ---- this.upgradeMustConnect = upgradeMustConnect; ! provisionalGUITile = new GUITile(tileId, this); /* Check if we can find a valid orientation of this tile */ if (provisionalGUITile.rotate(0, currentGUITile, upgradeMustConnect)) { *************** *** 736,740 **** currentTileId = Integer.parseInt(elements[0]); currentTileOrientation = Integer.parseInt(elements[1]); ! currentGUITile = new GUITile(currentTileId, model); currentGUITile.setRotation(currentTileOrientation); currentTile = currentGUITile.getTile(); --- 770,774 ---- currentTileId = Integer.parseInt(elements[0]); currentTileOrientation = Integer.parseInt(elements[1]); ! currentGUITile = new GUITile(currentTileId, this); currentGUITile.setRotation(currentTileOrientation); currentTile = currentGUITile.getTile(); |