From: Brett L. <wak...@us...> - 2012-01-01 14:43:20
|
data/1856/MapImage.svg | 14 ++++++-- rails/ui/swing/GUIToken.java | 57 +++++++++++++++++++-------------- rails/ui/swing/TokenIcon.java | 15 ++------ rails/ui/swing/hexmap/GUIHex.java | 17 +-------- rails/ui/swing/hexmap/HexMapImage.java | 3 + 5 files changed, 54 insertions(+), 52 deletions(-) New commits: commit 1aa084787dd2f2aca84acd3fef125f904e672860 Author: Frederick Weld <fre...@gm...> Date: Sun Jan 1 10:36:37 2012 +0100 Added precise sizing and positioning of token labels Introduced a generic logic that provides for precise sizing/positioning for any label text length and zoom factor. Replaces prior heuristic that, among others, placed labels partly outside of the tokens. diff --git a/rails/ui/swing/GUIToken.java b/rails/ui/swing/GUIToken.java index 1e5d8ec..5a02f08 100644 --- a/rails/ui/swing/GUIToken.java +++ b/rails/ui/swing/GUIToken.java @@ -3,6 +3,7 @@ package rails.ui.swing; import java.awt.*; import java.awt.geom.Ellipse2D; +import java.awt.geom.Rectangle2D; import javax.swing.JPanel; @@ -21,9 +22,12 @@ public class GUIToken extends JPanel { public static final int DEFAULT_X_COORD = 1; public static final int DEFAULT_Y_COORD = 1; - private static final Font smallTokenFont = new Font("Helvetica", Font.BOLD, 8); - private static final Font tokenFont = new Font("Helvetica", Font.BOLD, 10); - private static final Font largeTokenFont = new Font("Helvetica", Font.BOLD, 12); + private static final Font tokenFontTemplate = new Font("Helvetica", Font.BOLD, 10); + /** + * defined by the ratio of margin to diameter + * (eg., 0.2 means that 20% of the diameter is used as margin) + */ + private static final double tokenTextMargin = 0.15; @Override public void paintComponent(Graphics g) { @@ -35,24 +39,17 @@ public class GUIToken extends JPanel { } public void drawToken(Graphics2D g2d) { - Color oldColor = g2d.getColor(); - Font oldFont = g2d.getFont(); - double tokenScale = diameter / DEFAULT_DIAMETER; + Color oldColor = g2d.getColor(); g2d.setColor(Color.BLACK); g2d.draw(circle); g2d.setColor(bgColor); g2d.fill(circle); - - Font font = getTokenFont(name.length()); - g2d.setFont(new Font("Helvetica", Font.BOLD, - (int) (font.getSize() * tokenScale))); - g2d.setColor(fgColor); - g2d.drawString(name, (int) (circle.x + (12 - 3*name.length()) * tokenScale), - (int) (circle.y + 14 * tokenScale)); - g2d.setColor(oldColor); - g2d.setFont(oldFont); + + drawTokenText(name, g2d, fgColor, + new Point((int)(circle.x + diameter/2),(int)(circle.y + diameter/2)), + diameter); } protected void clear(Graphics g) { @@ -110,13 +107,27 @@ public class GUIToken extends JPanel { return name; } - public static Font getTokenFont (int size) { - if (size <= 2) { - return largeTokenFont; - } else if (size <= 4) { - return tokenFont; - } else { - return smallTokenFont; - } + public static void drawTokenText (String text, Graphics g, Color c, Point tokenCenter, double tokenDiameter) { + + //first calculate font size + double allowedTextDiameter = tokenDiameter * (1 - tokenTextMargin); + Rectangle2D textBoundsInTemplate = g.getFontMetrics(tokenFontTemplate).getStringBounds(text, g); + double fontScalingX = allowedTextDiameter / textBoundsInTemplate.getWidth(); + double fontScalingY = allowedTextDiameter / textBoundsInTemplate.getHeight(); + double fontScaling = (fontScalingX < fontScalingY) ? fontScalingX : fontScalingY; + int fontSize = (int) Math.floor(fontScaling * tokenFontTemplate.getSize()); + + //draw text + Color oldColor = g.getColor(); + Font oldFont = g.getFont(); + g.setColor(c); + g.setFont(tokenFontTemplate.deriveFont((float)fontSize)); //float needed to indicate size (not style) + Rectangle2D textBounds = g.getFontMetrics().getStringBounds(text, g); + g.drawString(text, + tokenCenter.x - (int)textBounds.getCenterX(), + tokenCenter.y - (int)textBounds.getCenterY()); + g.setColor(oldColor); + g.setFont(oldFont); + } } diff --git a/rails/ui/swing/TokenIcon.java b/rails/ui/swing/TokenIcon.java index 0f8f1d3..448eb2b 100644 --- a/rails/ui/swing/TokenIcon.java +++ b/rails/ui/swing/TokenIcon.java @@ -2,9 +2,9 @@ package rails.ui.swing; import java.awt.Color; import java.awt.Component; -import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Point; import java.awt.geom.Ellipse2D; import javax.swing.Icon; @@ -33,23 +33,16 @@ class TokenIcon implements Icon { new Ellipse2D.Double(x, y, diameter, diameter); Graphics2D g2d = (Graphics2D) g; Color oldColour = g2d.getColor(); - Font oldFont = g2d.getFont(); - double tokenScale = diameter / DEFAULT_DIAMETER; g2d.setColor(bgColour); g2d.fill(circle); g2d.setColor(Color.BLACK); g2d.draw(circle); - g2d.setFont(new Font("Helvetica", Font.BOLD, (int) (8 * tokenScale))); - g2d.setColor(fgColour); - // g2d.drawString(name, 3, 14); - g2d.drawString(text, (int) (circle.x + 2 * tokenScale), - (int) (circle.y + 14 * tokenScale)); - g2d.setColor(oldColour); - g2d.setFont(oldFont); - + GUIToken.drawTokenText(text, g, fgColour, + new Point((int)(circle.x + diameter/2),(int)(circle.y + diameter/2)), + diameter); } public int getIconWidth() { diff --git a/rails/ui/swing/hexmap/GUIHex.java b/rails/ui/swing/hexmap/GUIHex.java index 91b06d2..e083c44 100644 --- a/rails/ui/swing/hexmap/GUIHex.java +++ b/rails/ui/swing/hexmap/GUIHex.java @@ -578,25 +578,12 @@ public class GUIHex implements ViewObject { diameter, diameter); token.drawToken(g2); + } private void drawHome (Graphics2D g2, PublicCompanyI co, Point origin) { - Font oldFont = g2.getFont(); - Color oldColor = g2.getColor(); - - double scale = 0.5 * zoomFactor; - String name = co.getName(); - - Font font = GUIToken.getTokenFont(name.length()); - g2.setFont(new Font("Helvetica", Font.BOLD, - (int) (font.getSize() * zoomFactor * 0.75))); - g2.setColor(Color.BLACK); - g2.drawString(name, (int) (origin.x + (-4 - 3*name.length()) * scale), - (int) (origin.y + 4 * scale)); - - g2.setColor(oldColor); - g2.setFont(oldFont); + GUIToken.drawTokenText(co.getName(), g2, Color.BLACK, origin, tokenDiameter); } private void drawBonusToken(Graphics2D g2, BonusToken bt, Point origin) { commit c873a449f712e7934e80a467069af09807e0dd05 Author: Frederick Weld <fre...@gm...> Date: Sun Jan 1 07:01:15 2012 +0100 Fixed 1856 background map's mini-map (Windsor area) diff --git a/data/1856/MapImage.svg b/data/1856/MapImage.svg index 01dda31..595254a 100644 --- a/data/1856/MapImage.svg +++ b/data/1856/MapImage.svg @@ -150,12 +150,12 @@ inkscape:cx="2556" inkscape:cy="1957.5" inkscape:document-units="px" - inkscape:current-layer="layer3" + inkscape:current-layer="layer6" showgrid="false" inkscape:object-nodes="false" inkscape:window-width="1737" inkscape:window-height="1047" - inkscape:window-x="1452" + inkscape:window-x="172" inkscape:window-y="-11" inkscape:window-maximized="1" borderlayer="true" @@ -1558,6 +1558,16 @@ d="m 1316.1025,687.05754 c 11.3544,-9.50442 24.1951,-17.25146 37.7619,-23.13985 6.2666,-1.10258 11.5539,-4.50634 15.843,-9.11459 5.1019,-4.93631 11.9991,-7.33276 17.1024,-12.28326 3.9779,-3.49579 7.8596,-7.15447 12.7095,-9.4214 6.6475,-3.43833 12.4263,-8.89027 20.0869,-9.98548 6.6565,-1.63694 13.7417,-0.0944 20.2432,-2.34671" id="path3981" inkscape:connector-curvature="0" /> + <path + style="fill:#ffeaea;fill-opacity:1;stroke:none" + d="m 777.21379,820.08532 c -0.51563,-0.18361 -4.45313,-1.69384 -8.75,-3.35606 -4.29688,-1.66222 -10.20313,-3.49521 -13.125,-4.0733 -6.3968,-1.26562 -7.98154,-2.16641 -12.99328,-7.38562 -5.83181,-6.07323 -6.47767,-9.05655 -3.1495,-14.54801 2.4301,-4.00964 5.77457,-5.97601 12.52268,-7.36267 7.54706,-1.55084 10.51302,-1.32889 15.72195,1.1765 2.47731,1.19154 6.53809,2.51521 9.02396,2.94149 l 4.51977,0.77505 -0.16654,15.96477 c -0.16114,15.44732 -0.20706,15.9686 -1.41654,16.08323 -0.6875,0.0652 -1.67188,-0.0318 -2.1875,-0.21538 z" + id="path4752" + inkscape:connector-curvature="0" /> + <path + style="fill:#ffeaea;fill-opacity:1;stroke:none" + d="" + id="path4754" + inkscape:connector-curvature="0" /> </g> <g inkscape:groupmode="layer" commit 2c822f0d3923720ff5f7d989e8c9a64667e261e4 Author: Frederick Weld <fre...@gm...> Date: Sat Dec 31 13:03:26 2011 +0100 Fixed the glitch of initially displaying map images in the wrong scale Glitch description: If the scale factor of the background map image differs from 1, the map is initially displayed in the wrong scale for a second before resizing eventually corrects this (especially applies to 1856/1889 background images as their scale is lower than 0.2). diff --git a/rails/ui/swing/hexmap/HexMapImage.java b/rails/ui/swing/hexmap/HexMapImage.java index 171642e..5ad44af 100644 --- a/rails/ui/swing/hexmap/HexMapImage.java +++ b/rails/ui/swing/hexmap/HexMapImage.java @@ -76,7 +76,8 @@ public final class HexMapImage extends JSVGCanvas { } addGVTTreeRendererListener (new GVTTreeRendererAdapter() { - public void gvtRenderingCompleted(GVTTreeRendererEvent e) { + //prepare: map scaling has to occur before displaying it for the first time + public void gvtRenderingPrepare(GVTTreeRendererEvent e) { if (!initialized) { // store the rendering Transform initialTransform = getRenderingTransform(); |