Changeset 4319 in josm


Ignore:
Timestamp:
2011-08-18T13:01:55+02:00 (13 years ago)
Author:
bastiK
Message:

mappaint: split NodeElemStyle into icon/symbol part and text part

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/coor/QuadTiling.java

    r4191 r4319  
    44public class QuadTiling
    55{
    6     public static int NR_LEVELS = 24;
    7     public static double WORLD_PARTS = (1 << NR_LEVELS);
     6    public static final int NR_LEVELS = 24;
     7    public static final double WORLD_PARTS = (1 << NR_LEVELS);
    88
    9     public static int TILES_PER_LEVEL_SHIFT = 2; // Has to be 2. Other parts of QuadBuckets code rely on it
    10     public static int TILES_PER_LEVEL = 1<<TILES_PER_LEVEL_SHIFT;
    11     static public int X_PARTS = 360;
    12     static public int X_BIAS = -180;
     9    public static final int TILES_PER_LEVEL_SHIFT = 2; // Has to be 2. Other parts of QuadBuckets code rely on it
     10    public static final int TILES_PER_LEVEL = 1<<TILES_PER_LEVEL_SHIFT;
     11    static public final int X_PARTS = 360;
     12    static public final int X_BIAS = -180;
    1313
    14     static public int Y_PARTS = 180;
    15     static public int Y_BIAS = -90;
     14    static public final int Y_PARTS = 180;
     15    static public final int Y_BIAS = -90;
    1616
    1717    public static LatLon tile2LatLon(long quad)
  • trunk/src/org/openstreetmap/josm/data/osm/BBox.java

    r3719 r4319  
    136136    /**
    137137     * Tests, weather two BBoxes intersect as an area.
    138      * I.e. weather there exists a point that lies in both of them.
     138     * I.e. whether there exists a point that lies in both of them.
    139139     */
    140140    public boolean intersects(BBox b) {
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java

    r4317 r4319  
    3838import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.PolyData;
    3939import org.openstreetmap.josm.gui.NavigatableComponent;
     40import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle;
     41import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.HorizontalTextAlignment;
     42import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.VerticalTextAlignment;
    4043import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
    41 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.HorizontalTextAlignment;
    42 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.NodeTextElement;
    4344import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.Symbol;
    44 import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.VerticalTextAlignment;
    4545import org.openstreetmap.josm.gui.mappaint.TextElement;
    4646import org.openstreetmap.josm.tools.ImageProvider;
     
    496496    }
    497497
    498     public void drawNodeIcon(Node n, ImageIcon icon, float iconAlpha, boolean selected, boolean member, NodeTextElement text) {
     498    public void drawNodeIcon(Node n, ImageIcon icon, float iconAlpha, boolean selected, boolean member) {
    499499        Point p = nc.getPoint(n);
    500500        if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
     
    506506        icon.paintIcon ( nc, g, p.x-w/2, p.y-h/2 );
    507507        g.setPaintMode();
    508         drawNodeText(n, text, p, w/2, h/2);
    509508        if (selected || member)
    510509        {
     
    529528    }
    530529
    531     public void drawNodeSymbol(Node n, Symbol s, Color fillColor, Color strokeColor, NodeTextElement text) {
     530    public void drawNodeSymbol(Node n, Symbol s, Color fillColor, Color strokeColor) {
    532531        Point p = nc.getPoint(n);
    533532        if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
     
    604603            g.setStroke(new BasicStroke());
    605604        }
    606         drawNodeText(n, text, p, radius, radius);
    607605    }
    608606
     
    613611     * @param color The color of the node.
    614612     */
    615     public void drawNode(Node n, Color color, int size, boolean fill, NodeTextElement text) {
     613    public void drawNode(Node n, Color color, int size, boolean fill) {
    616614        if (size > 1) {
    617615            Point p = nc.getPoint(n);
     
    629627                g.drawRect(p.x - radius, p.y - radius, size, size);
    630628            }
    631 
    632             drawNodeText(n, text, p, radius, radius + 4);
    633         }
    634     }
    635 
    636     private void drawNodeText(Node n, NodeTextElement text, Point p, int w_half, int h_half) {
    637         if (!isShowNames() || text == null)
     629        }
     630    }
     631
     632    public void drawBoxText(Node n, BoxTextElemStyle bs) {
     633        if (!isShowNames() || bs == null)
    638634            return;
    639 
    640         /*
    641          * abort if we can't compose the label to be rendered
    642          */
    643         if (text.labelCompositionStrategy == null) return;
     635       
     636        Point p = nc.getPoint((Node) n);
     637        TextElement text = bs.text;
    644638        String s = text.labelCompositionStrategy.compose(n);
    645639        if (s == null) return;
     
    661655         *
    662656         */
    663         if (text.hAlign == HorizontalTextAlignment.RIGHT) {
    664             x += w_half + 2;
     657        if (bs.hAlign == HorizontalTextAlignment.RIGHT) {
     658            x += bs.box.x + bs.box.width + 2;
    665659        } else {
    666660            FontRenderContext frc = g.getFontRenderContext();
    667661            Rectangle2D bounds = text.font.getStringBounds(s, frc);
    668662            int textWidth = (int) bounds.getWidth();
    669             if (text.hAlign == HorizontalTextAlignment.CENTER) {
     663            if (bs.hAlign == HorizontalTextAlignment.CENTER) {
    670664                x -= textWidth / 2;
    671             } else if (text.hAlign == HorizontalTextAlignment.LEFT) {
    672                 x -= w_half + 4 + textWidth;
     665            } else if (bs.hAlign == HorizontalTextAlignment.LEFT) {
     666                x -= - bs.box.x + 4 + textWidth;
    673667            } else throw new AssertionError();
    674668        }
    675669
    676         if (text.vAlign == VerticalTextAlignment.BOTTOM) {
    677             y += h_half - 2;
     670        if (bs.vAlign == VerticalTextAlignment.BOTTOM) {
     671            y += bs.box.y + bs.box.height;
    678672        } else {
    679673            FontRenderContext frc = g.getFontRenderContext();
    680674            LineMetrics metrics = text.font.getLineMetrics(s, frc);
    681             if (text.vAlign == VerticalTextAlignment.ABOVE) {
    682                 y -= h_half + metrics.getDescent();
    683             } else if (text.vAlign == VerticalTextAlignment.TOP) {
    684                 y -= h_half - metrics.getAscent();
    685             } else if (text.vAlign == VerticalTextAlignment.CENTER) {
     675            if (bs.vAlign == VerticalTextAlignment.ABOVE) {
     676                y -= - bs.box.y + metrics.getDescent();
     677            } else if (bs.vAlign == VerticalTextAlignment.TOP) {
     678                y -= - bs.box.y - metrics.getAscent();
     679            } else if (bs.vAlign == VerticalTextAlignment.CENTER) {
    686680                y += (metrics.getAscent() - metrics.getDescent()) / 2;
    687             } else if (text.vAlign == VerticalTextAlignment.BELOW) {
    688                 y += h_half + metrics.getAscent() + 2;
     681            } else if (bs.vAlign == VerticalTextAlignment.BELOW) {
     682                y += bs.box.y + bs.box.height + metrics.getAscent() + 2;
    689683            } else throw new AssertionError();
    690684        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java

    r4318 r4319  
    7979            if (!hasNonModifier) {
    8080                p.a = new StyleList(p.a, NodeElemStyle.SIMPLE_NODE_ELEMSTYLE);
     81                if (BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE.text.labelCompositionStrategy.compose(osm) != null) {
     82                    p.a = new StyleList(p.a, BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE);
     83                }
    8184            }
    8285        } else if (osm instanceof Way && isDefaultLines()) {
     
    301304                addIfNotNull(sl, LineTextElemStyle.create(env));
    302305            } else if (osm instanceof Node) {
    303                 addIfNotNull(sl, NodeElemStyle.create(env));
     306                NodeElemStyle nodeStyle = NodeElemStyle.create(env);
     307                if (nodeStyle != null) {
     308                    sl.add(nodeStyle);
     309                    addIfNotNull(sl, BoxTextElemStyle.create(env, nodeStyle.getBox()));
     310                }
    304311            } else if (osm instanceof Relation) {
    305312                if (((Relation)osm).isMultipolygon()) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r4318 r4319  
    66import java.awt.BasicStroke;
    77import java.awt.Color;
    8 import java.awt.Dimension;
    9 import java.awt.Image;
     8import java.awt.Rectangle;
    109import java.awt.Stroke;
    1110
     
    1918import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
    2019import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
    21 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    2220import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    23 import org.openstreetmap.josm.tools.CheckParameterUtil;
    2421import org.openstreetmap.josm.tools.Pair;
    2522import org.openstreetmap.josm.tools.Utils;
     
    3229    public int iconAlpha;
    3330    public Symbol symbol;
    34     public NodeTextElement text;
    3531
    3632    private ImageIcon disabledIcon;
    3733
    3834    public enum SymbolShape { SQUARE, CIRCLE, TRIANGLE, PENTAGON, HEXAGON, HEPTAGON, OCTAGON, NONAGON, DECAGON }
    39     public enum HorizontalTextAlignment { LEFT, CENTER, RIGHT }
    40     public enum VerticalTextAlignment { ABOVE, TOP, CENTER, BOTTOM, BELOW }
    4135
    4236    public static class Symbol {
     
    9084    }
    9185
    92     public static class NodeTextElement extends TextElement {
    93         public HorizontalTextAlignment hAlign;
    94         public VerticalTextAlignment vAlign;
    95 
    96         public NodeTextElement(TextElement text, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign) {
    97             super(text);
    98             CheckParameterUtil.ensureParameterNotNull(hAlign);
    99             CheckParameterUtil.ensureParameterNotNull(vAlign);
    100             this.hAlign = hAlign;
    101             this.vAlign = vAlign;
    102         }
    103 
    104         @Override
    105         public boolean equals(Object obj) {
    106             if (!super.equals(obj))
    107                 return false;
    108             if (obj == null || getClass() != obj.getClass())
    109                 return false;
    110             final NodeTextElement other = (NodeTextElement) obj;
    111             return hAlign == other.hAlign &&
    112                     vAlign == other.vAlign;
    113         }
    114 
    115         @Override
    116         public int hashCode() {
    117             int hash = super.hashCode();
    118             hash = 97 * hash + hAlign.hashCode();
    119             hash = 97 * hash + vAlign.hashCode();
    120             return hash;
    121         }
    122 
    123         @Override
    124         public String toString() {
    125             return "NodeTextElement{" + toStringImpl() + '}';
    126         }
    127 
    128         @Override
    129         protected String toStringImpl() {
    130             return super.toStringImpl() + " hAlign=" + hAlign + " vAlign=" + vAlign;
    131         }
    132     }
    133 
    13486    public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE;
    13587    static {
    13688        MultiCascade mc = new MultiCascade();
    13789        Cascade c = mc.getOrCreateCascade("default");
    138         c.put("text", Keyword.AUTO);
    13990        SIMPLE_NODE_ELEMSTYLE = create(new Environment(null, mc, "default", null), true);
    140     }
    141 
    142     protected NodeElemStyle(Cascade c, ImageIcon icon, Integer iconAlpha, Symbol symbol, NodeTextElement text) {
     91        if (SIMPLE_NODE_ELEMSTYLE == null) throw new AssertionError();
     92    }
     93
     94    protected NodeElemStyle(Cascade c, ImageIcon icon, Integer iconAlpha, Symbol symbol) {
    14395        super(c, 1000f);
    14496        this.icon = icon;
    14597        this.iconAlpha = iconAlpha == null ? 0 : iconAlpha;
    14698        this.symbol = symbol;
    147         this.text = text;
    14899    }
    149100
     
    152103    }
    153104
    154     /*
    155      * Caches the default text color from the preferences.
    156      *
    157      * FIXME: the cache isn't updated if the user changes the preference during a JOSM
    158      * session. There should be preference listener updating this cache.
    159      */
    160     static private Color DEFAULT_TEXT_COLOR = null;
    161     static private void initDefaultParameters() {
    162         if (DEFAULT_TEXT_COLOR != null) return;
    163         DEFAULT_TEXT_COLOR = PaintColors.TEXT.get();
    164     }
    165 
    166105    private static NodeElemStyle create(Environment env, boolean allowDefault) {
    167         initDefaultParameters();
    168106        Cascade c = env.mc.getCascade(env.layer);
    169107
     
    173111            symbol = createSymbol(env);
    174112        }
    175 
    176         NodeTextElement text = null;
    177         TextElement te = TextElement.create(c, DEFAULT_TEXT_COLOR, symbol == null && icon == null && allowDefault);
    178         // optimization: if we neither have a symbol, nor an icon, nor a text element
     113       
     114        // optimization: if we neither have a symbol, nor an icon
    179115        // we don't have to check for the remaining style properties and we don't
    180116        // have to allocate a node element style.
    181         if (symbol == null && icon == null && te == null) return null;
    182 
    183         if (te != null) {
    184             HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT;
    185             Keyword hAlignKW = c.get("text-anchor-horizontal", Keyword.RIGHT, Keyword.class);
    186             if (equal(hAlignKW.val, "left")) {
    187                 hAlign = HorizontalTextAlignment.LEFT;
    188             } else if (equal(hAlignKW.val, "center")) {
    189                 hAlign = HorizontalTextAlignment.CENTER;
    190             } else if (equal(hAlignKW.val, "right")) {
    191                 hAlign = HorizontalTextAlignment.RIGHT;
    192             }
    193             VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM;
    194             String vAlignStr = c.get("text-anchor-vertical", Keyword.BOTTOM, Keyword.class).val;
    195             if (equal(vAlignStr, "above")) {
    196                 vAlign = VerticalTextAlignment.ABOVE;
    197             } else if (equal(vAlignStr, "top")) {
    198                 vAlign = VerticalTextAlignment.TOP;
    199             } else if (equal(vAlignStr, "center")) {
    200                 vAlign = VerticalTextAlignment.CENTER;
    201             } else if (equal(vAlignStr, "bottom")) {
    202                 vAlign = VerticalTextAlignment.BOTTOM;
    203             } else if (equal(vAlignStr, "below")) {
    204                 vAlign = VerticalTextAlignment.BELOW;
    205             }
    206             text = new NodeTextElement(te, hAlign, vAlign);
    207         }
     117        if (!allowDefault && symbol == null && icon == null) return null;
    208118
    209119        return new NodeElemStyle(c,
    210120                icon == null ? null : icon.a,
    211121                icon == null ? null : icon.b,
    212                 symbol,
    213                 text);
     122                symbol);
    214123    }
    215124
     
    330239            if (icon != null && painter.isShowIcons()) {
    331240                painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? getDisabledIcon() : icon,
    332                         Utils.color_int2float(iconAlpha), selected, member, text);
     241                        Utils.color_int2float(iconAlpha), selected, member);
    333242            } else if (symbol != null) {
    334243                Color fillColor = symbol.fillColor;
     
    360269                    }
    361270                }
    362                 painter.drawNodeSymbol(n, symbol, fillColor, strokeColor, text);
     271                painter.drawNodeSymbol(n, symbol, fillColor, strokeColor);
    363272            } else {
    364273                if (n.isHighlighted()) {
    365                     painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), text);
     274                    painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode());
    366275                } else {
    367276                    Color color;
     
    398307                            settings.isFillUnselectedNode();
    399308
    400                     painter.drawNode(n, color, size, fill, text);
     309                    painter.drawNode(n, color, size, fill);
    401310                }
    402311            }
     
    412321            return null;
    413322        return disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
     323    }
     324   
     325    public Rectangle getBox() {
     326        if (icon != null) {
     327            int w = icon.getIconWidth(), h=icon.getIconHeight();
     328            return new Rectangle(-w/2, -h/2, w, h);
     329        } else if (symbol != null) {
     330            return new Rectangle(-symbol.size/2, -symbol.size/2, symbol.size, symbol.size);
     331        } else {
     332            // This is only executed once, so no performance concerns.
     333            // However, it would be better, if the settings could be changed at runtime.
     334            int size = Utils.max(
     335                    Main.pref.getInteger("mappaint.node.selected-size", 5),
     336                    Main.pref.getInteger("mappaint.node.unselected-size", 3),
     337                    Main.pref.getInteger("mappaint.node.connection-size", 5),
     338                    Main.pref.getInteger("mappaint.node.tagged-size", 3)
     339            );
     340            return new Rectangle(-size/2, -size/2, size, size);
     341        }
    414342    }
    415343
     
    420348        hash = 17 * hash + iconAlpha;
    421349        hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
    422         hash = 17 * hash + (text != null ? text.hashCode() : 0);
    423350        return hash;
    424351    }
     
    438365            return false;
    439366        if (!equal(symbol, other.symbol))
    440             return false;
    441         if (!equal(text, other.text))
    442367            return false;
    443368        return true;
     
    455380            s.append(" symbol=[" + symbol + "]");
    456381        }
    457         if (text != null) {
    458             s.append(" text=[" + text.toStringImpl() + "]");
    459         }
    460382        s.append('}');
    461383        return s.toString();
Note: See TracChangeset for help on using the changeset viewer.