source: josm/trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java@ 5217

Last change on this file since 5217 was 5217, checked in by bastiK, 12 years ago

mapcss: change z-index handling

In contrast to previous default z-index of 1000 for nodes and -1000 for areas, the default z-index is
now always 0. An additional "major-z-index" is used to sort elemstyles by category, this property
can be adjusted in the style, so icons can be rendered below lines, if you want to.

  • Property svn:eol-style set to native
File size: 8.9 KB
RevLine 
[3824]1// License: GPL. Copyright 2007 by Immanuel Scholz and others
[2666]2package org.openstreetmap.josm.data.osm.visitor.paint;
[486]3
[2671]4import java.awt.Graphics2D;
[2706]5import java.awt.RenderingHints;
[1523]6import java.util.ArrayList;
[4327]7import java.util.Collection;
[2279]8import java.util.Collections;
[3836]9import java.util.List;
[486]10
11import org.openstreetmap.josm.Main;
[2450]12import org.openstreetmap.josm.data.Bounds;
13import org.openstreetmap.josm.data.osm.BBox;
[486]14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.data.osm.Way;
[4327]19import org.openstreetmap.josm.data.osm.WaySegment;
[2671]20import org.openstreetmap.josm.gui.NavigatableComponent;
[486]21import org.openstreetmap.josm.gui.mappaint.AreaElemStyle;
22import org.openstreetmap.josm.gui.mappaint.ElemStyle;
[3803]23import org.openstreetmap.josm.gui.mappaint.ElemStyles;
[486]24import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
[3836]25import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
26import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
[486]27
[4087]28/**
29 * <p>A map renderer which renders a map according to style rules in a set of style sheets.</p>
[5217]30 *
[4087]31 */
32public class StyledMapRenderer extends AbstractMapRenderer{
[2671]33
[3803]34 private ElemStyles styles;
[2675]35 private double circum;
[2667]36 private MapPainter painter;
[2675]37 private MapPaintSettings paintSettings;
[1263]38
[4040]39 private static int FLAG_NORMAL = 0;
40 private static int FLAG_DISABLED = 1;
[5217]41 private static int FLAG_MEMBER_OF_SELECTED = 2;
42 private static int FLAG_SELECTED = 4;
[4040]43
44 private static class StyleRecord implements Comparable<StyleRecord> {
45 final ElemStyle style;
46 final OsmPrimitive osm;
47 final int flags;
48
49 public StyleRecord(ElemStyle style, OsmPrimitive osm, int flags) {
50 this.style = style;
51 this.osm = osm;
52 this.flags = flags;
53 }
54
55 @Override
56 public int compareTo(StyleRecord other) {
57 if ((this.flags & FLAG_DISABLED) != 0 && (other.flags & FLAG_DISABLED) == 0)
58 return -1;
59 if ((this.flags & FLAG_DISABLED) == 0 && (other.flags & FLAG_DISABLED) != 0)
60 return 1;
[4087]61
[5217]62 int d0 = Float.compare(this.style.major_z_index, other.style.major_z_index);
63 if (d0 != 0)
64 return d0;
[4087]65
[5217]66 // selected on top of member of selected on top of unselected
67 // FLAG_DISABLED bit is the same at this point
68 if (this.flags > other.flags)
69 return 1;
70 if (this.flags < other.flags)
71 return -1;
72
73 int dz = Float.compare(this.style.z_index, other.style.z_index);
74 if (dz != 0)
75 return dz;
76
[4040]77 // simple node on top of icons and shapes
78 if (this.style == NodeElemStyle.SIMPLE_NODE_ELEMSTYLE && other.style != NodeElemStyle.SIMPLE_NODE_ELEMSTYLE)
79 return 1;
80 if (this.style != NodeElemStyle.SIMPLE_NODE_ELEMSTYLE && other.style == NodeElemStyle.SIMPLE_NODE_ELEMSTYLE)
81 return -1;
[4087]82
[4040]83 // newer primitives to the front
84 long id = this.osm.getUniqueId() - other.osm.getUniqueId();
85 if (id > 0)
86 return 1;
87 if (id < 0)
88 return -1;
[4087]89
[4040]90 return Float.compare(this.style.object_z_index, other.style.object_z_index);
91 }
92 }
[4087]93
[3836]94 private class StyleCollector {
[4005]95 private final boolean drawArea;
96 private final boolean drawMultipolygon;
97 private final boolean drawRestriction;
[2671]98
[4040]99 private final List<StyleRecord> styleElems;
[4005]100
[4040]101 public StyleCollector(boolean drawArea, boolean drawMultipolygon, boolean drawRestriction) {
[4005]102 this.drawArea = drawArea;
103 this.drawMultipolygon = drawMultipolygon;
104 this.drawRestriction = drawRestriction;
[4040]105 styleElems = new ArrayList<StyleRecord>();
[3836]106 }
[885]107
[4040]108 public void add(Node osm, int flags) {
[3836]109 StyleList sl = styles.get(osm, circum, nc);
110 for (ElemStyle s : sl) {
[4040]111 styleElems.add(new StyleRecord(s, osm, flags));
[4005]112 }
113 }
114
[4040]115 public void add(Way osm, int flags) {
[4005]116 StyleList sl = styles.get(osm, circum, nc);
117 for (ElemStyle s : sl) {
[4040]118 if (!(drawArea && (flags & FLAG_DISABLED) == 0) && s instanceof AreaElemStyle) {
[4005]119 continue;
120 }
[4040]121 styleElems.add(new StyleRecord(s, osm, flags));
[4005]122 }
123 }
124
[4040]125 public void add(Relation osm, int flags) {
[4005]126 StyleList sl = styles.get(osm, circum, nc);
127 for (ElemStyle s : sl) {
[4040]128 if (drawMultipolygon && drawArea && s instanceof AreaElemStyle && (flags & FLAG_DISABLED) == 0) {
129 styleElems.add(new StyleRecord(s, osm, flags));
[4005]130 } else if (drawRestriction && s instanceof NodeElemStyle) {
[4040]131 styleElems.add(new StyleRecord(s, osm, flags));
[3307]132 }
[1990]133 }
[2659]134 }
[1254]135
[3836]136 public void drawAll() {
[4040]137 Collections.sort(styleElems);
138 for (StyleRecord r : styleElems) {
139 r.style.paintPrimitive(
[4087]140 r.osm,
141 paintSettings,
142 painter,
143 (r.flags & FLAG_SELECTED) != 0,
[4040]144 (r.flags & FLAG_MEMBER_OF_SELECTED) != 0
145 );
[3824]146 }
[1990]147 }
[1190]148 }
149
[4087]150 /**
151 * {@inheritDoc}
152 */
153 public StyledMapRenderer(Graphics2D g, NavigatableComponent nc, boolean isInactiveMode) {
154 super(g, nc, isInactiveMode);
155 }
[1309]156
[4087]157 private void collectNodeStyles(DataSet data, StyleCollector sc, BBox bbox) {
[3836]158 for (final Node n: data.searchNodes(bbox)) {
159 if (n.isDrawable()) {
160 if (n.isDisabled()) {
[4040]161 sc.add(n, FLAG_DISABLED);
162 } else if (data.isSelected(n)) {
163 sc.add(n, FLAG_SELECTED);
[3836]164 } else if (n.isMemberOfSelected()) {
[4040]165 sc.add(n, FLAG_MEMBER_OF_SELECTED);
[3836]166 } else {
[4040]167 sc.add(n, FLAG_NORMAL);
[3322]168 }
169 }
[1169]170 }
[4087]171 }
172
173 private void collectWayStyles(DataSet data, StyleCollector sc, BBox bbox) {
[4005]174 for (final Way w : data.searchWays(bbox)) {
175 if (w.isDrawable()) {
176 if (w.isDisabled()) {
[4040]177 sc.add(w, FLAG_DISABLED);
178 } else if (data.isSelected(w)) {
179 sc.add(w, FLAG_SELECTED);
[4005]180 } else if (w.isMemberOfSelected()) {
[4040]181 sc.add(w, FLAG_MEMBER_OF_SELECTED);
[4005]182 } else {
[4040]183 sc.add(w, FLAG_NORMAL);
[4005]184 }
185 }
186 }
[4087]187 }
188
189 private void collectRelationStyles(DataSet data, StyleCollector sc, BBox bbox) {
[3836]190 for (Relation r: data.searchRelations(bbox)) {
191 if (r.isDrawable()) {
192 if (r.isDisabled()) {
[4040]193 sc.add(r, FLAG_DISABLED);
194 } else if (data.isSelected(r)) {
195 sc.add(r, FLAG_SELECTED);
[3836]196 } else {
[4040]197 sc.add(r, FLAG_NORMAL);
[3836]198 }
[1221]199 }
[1195]200 }
[4087]201 }
[808]202
[4087]203 @Override
204 public void render(final DataSet data, boolean renderVirtualNodes, Bounds bounds) {
[4317]205 //long start = System.currentTimeMillis();
[4087]206 BBox bbox = new BBox(bounds);
207
208 styles = MapPaintStyles.getStyles();
209
210 this.paintSettings = MapPaintSettings.INSTANCE;
211
212 circum = nc.getDist100Pixel();
213 boolean drawArea = circum <= Main.pref.getInteger("mappaint.fillareas", 10000000);
214 boolean drawMultipolygon = drawArea && Main.pref.getBoolean("mappaint.multipolygon", true);
215 styles.setDrawMultipolygon(drawMultipolygon);
216 boolean drawRestriction = Main.pref.getBoolean("mappaint.restriction", true);
217 boolean leftHandTraffic = Main.pref.getBoolean("mappaint.lefthandtraffic", false);
218
219 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
220 Main.pref.getBoolean("mappaint.use-antialiasing", true) ?
221 RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
222
[4327]223 Collection<WaySegment> hws = data.getHighlightedWaySegments();
[4087]224
[4327]225 this.painter = new MapPainter(paintSettings, g, isInactiveMode, nc, renderVirtualNodes, circum, leftHandTraffic, hws);
226
[4087]227 StyleCollector sc = new StyleCollector(drawArea, drawMultipolygon, drawRestriction);
228 collectNodeStyles(data, sc, bbox);
229 collectWayStyles(data, sc, bbox);
230 collectRelationStyles(data, sc, bbox);
[4317]231 //long phase1 = System.currentTimeMillis();
[4040]232 sc.drawAll();
233 sc = null;
[4327]234 painter.drawVirtualNodes(data.searchWays(bbox), data.getHighlightedVirtualNodes());
[4005]235
[4317]236 //long now = System.currentTimeMillis();
237 //System.err.println(String.format("PAINTING TOOK %d [PHASE1 took %d] (at scale %s)", now - start, phase1 - start, circum));
[1169]238 }
[486]239}
Note: See TracBrowser for help on using the repository browser.