From: Erik V. <ev...@us...> - 2009-11-06 20:24:06
|
Update of /cvsroot/rails/18xx/rails/ui/swing/hexmap In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv6277/rails/ui/swing/hexmap Modified Files: GUIHex.java HexMap.java Added Files: BarredHexSide.java GUIBar.java Log Message: Display bars to show impassable hex sides Index: GUIHex.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/GUIHex.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** GUIHex.java 2 Nov 2009 23:30:35 -0000 1.23 --- GUIHex.java 6 Nov 2009 20:23:53 -0000 1.24 *************** *** 48,51 **** --- 48,52 ---- protected List<TokenI> offStationTokens; + protected List<GUIBar> bars; protected GUIToken provisionalGUIToken = null; *************** *** 168,171 **** --- 169,186 ---- } + 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); + log.debug("--- Added bar "+bar.getName()+" from "+x1+","+y1+" to "+x2+","+y2); + } + public Rectangle getBounds() { return rectBound; *************** *** 348,351 **** --- 363,374 ---- } + 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) { Index: HexMap.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/hexmap/HexMap.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** HexMap.java 2 Nov 2009 23:30:36 -0000 1.18 --- HexMap.java 6 Nov 2009 20:23:53 -0000 1.19 *************** *** 37,40 **** --- 37,41 ---- 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; *************** *** 67,74 **** --- 68,88 ---- public void setupHexes() { setupHexesGUI(); + setupBars(); addMouseListener(this); addMouseMotionListener(this); } + public void setupBars() { + List<Integer> barSides; + for (GUIHex hex : hexes) { + barSides = hex.getHexModel().getImpassableSides(); + if (barSides != null) { + for (int k : barSides) { + if (k < 3) hex.addBar (k); + } + } + } + } + GUIHex getHexContainingPoint(Point point) { for (GUIHex hex : hexes) { *************** *** 104,107 **** --- 118,132 ---- } } + + // Paint the impassability bars latest + for (GUIHex hex : hexes) { + Rectangle hexrect = hex.getBounds(); + + if (g.hitClip(hexrect.x, hexrect.y, hexrect.width, + hexrect.height)) { + hex.paintBars(g); + } + } + } catch (NullPointerException ex) { // If we try to paint before something is loaded, just retry --- NEW FILE: BarredHexSide.java --- /* $Header: /cvsroot/rails/18xx/rails/ui/swing/hexmap/BarredHexSide.java,v 1.1 2009/11/06 20:23:53 evos Exp $*/ package rails.ui.swing.hexmap; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import org.apache.log4j.Logger; import rails.game.MapHex; import rails.ui.swing.GameUIManager; import rails.ui.swing.ImageLoader; /** * This class represents the GUI version of a tile. */ public class BarredHexSide { protected BufferedImage tileImage = null; protected int rotation = 0; protected double tileScale = GUIHex.NORMAL_SCALE; protected double baseRotation; protected MapHex hex = null; protected int hexSide; protected static ImageLoader imageLoader = GameUIManager.getImageLoader(); protected AffineTransform af = new AffineTransform(); public static final double DEG60 = Math.PI / 3; protected static Logger log = Logger.getLogger(BarredHexSide.class.getPackage().getName()); public BarredHexSide(MapHex hex, int hexSide) { this.hexSide = hexSide; this.hex = hex; if (hex.getTileOrientation() == MapHex.EW) { baseRotation = 0.5 * DEG60; } else { baseRotation = 0.0; } } public void setRotation(int rotation) { this.rotation = rotation % 6; } public int getRotation() { return rotation; } public void setScale(double scale) { tileScale = scale; } public void paintBar(Graphics2D g2, int x, int y) { double radians = baseRotation + rotation * DEG60; int xCenter = (int) Math.round(tileImage.getWidth() * 0.5 * tileScale); int yCenter = (int) Math.round(tileImage.getHeight() * 0.5 * tileScale); af = AffineTransform.getRotateInstance(radians, xCenter, yCenter); af.scale(tileScale, tileScale); RenderingHints rh = new RenderingHints(null); rh.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); rh.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); rh.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE); rh.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); AffineTransformOp aop = new AffineTransformOp(af, rh); g2.drawImage(tileImage, aop, x - xCenter, y - yCenter); } } --- NEW FILE: GUIBar.java --- /* $Header: /cvsroot/rails/18xx/rails/ui/swing/hexmap/GUIBar.java,v 1.1 2009/11/06 20:23:53 evos Exp $*/ package rails.ui.swing.hexmap; import java.awt.*; /** * This class draws a company's token. */ public class GUIBar { private Color color = Color.BLACK; private int x1, x2; private int y1, y2; private String name; private static final int STROKE_WIDTH = 5; public static final int DEFAULT_LENGTH = 64; public static final int DEFAULT_WIDTH = 12; public static final int DEFAULT_X_COORD = 1; public static final int DEFAULT_Y_COORD = 1; public GUIBar(String name, int x1, int y1, int x2, int y2) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.name = name; } public void drawBar(Graphics2D g2d) { Color oldColor = g2d.getColor(); Stroke oldStroke = g2d.getStroke(); g2d.setColor(color); g2d.setStroke(new BasicStroke(STROKE_WIDTH)); g2d.drawLine(x1, y1, x2, y2); g2d.setColor(oldColor); g2d.setStroke(oldStroke); } public Color getColor() { return color; } public String getName() { return name; } } |