[3824] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[2666] | 2 | package org.openstreetmap.josm.data.osm.visitor.paint;
|
---|
[486] | 3 |
|
---|
[2671] | 4 | import java.awt.Graphics2D;
|
---|
[2706] | 5 | import java.awt.RenderingHints;
|
---|
[1523] | 6 | import java.util.ArrayList;
|
---|
[4327] | 7 | import java.util.Collection;
|
---|
[2279] | 8 | import java.util.Collections;
|
---|
[3836] | 9 | import java.util.List;
|
---|
[486] | 10 |
|
---|
| 11 | import org.openstreetmap.josm.Main;
|
---|
[2450] | 12 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
[486] | 14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[4327] | 19 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
[2671] | 20 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
[486] | 21 | import org.openstreetmap.josm.gui.mappaint.AreaElemStyle;
|
---|
| 22 | import org.openstreetmap.josm.gui.mappaint.ElemStyle;
|
---|
[3803] | 23 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
[486] | 24 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
[3836] | 25 | import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
|
---|
| 26 | import 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 | */
|
---|
| 32 | public 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 | }
|
---|