| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.mappaint;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.Utils.equal;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.BasicStroke;
|
|---|
| 7 | import java.awt.Color;
|
|---|
| 8 | import java.util.Arrays;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
|---|
| 16 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 17 |
|
|---|
| 18 | public class LineElemStyle extends ElemStyle {
|
|---|
| 19 |
|
|---|
| 20 | public static LineElemStyle createSimpleLineStyle(Color color, boolean isAreaEdge) {
|
|---|
| 21 | MultiCascade mc = new MultiCascade();
|
|---|
| 22 | Cascade c = mc.getOrCreateCascade("default");
|
|---|
| 23 | c.put("width", Keyword.DEFAULT);
|
|---|
| 24 | c.put("color", color != null ? color : PaintColors.UNTAGGED.get());
|
|---|
| 25 | if (isAreaEdge) {
|
|---|
| 26 | c.put("z-index", -3f);
|
|---|
| 27 | }
|
|---|
| 28 | return createLine(new Environment(null, mc, "default", null));
|
|---|
| 29 | }
|
|---|
| 30 | public static final LineElemStyle UNTAGGED_WAY = createSimpleLineStyle(null, false);
|
|---|
| 31 |
|
|---|
| 32 | private BasicStroke line;
|
|---|
| 33 | public Color color;
|
|---|
| 34 | public Color dashesBackground;
|
|---|
| 35 | public float realWidth; // the real width of this line in meter
|
|---|
| 36 |
|
|---|
| 37 | private BasicStroke dashesLine;
|
|---|
| 38 |
|
|---|
| 39 | protected LineElemStyle(Cascade c, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, float realWidth) {
|
|---|
| 40 | super(c, 0f);
|
|---|
| 41 | this.line = line;
|
|---|
| 42 | this.color = color;
|
|---|
| 43 | this.dashesLine = dashesLine;
|
|---|
| 44 | this.dashesBackground = dashesBackground;
|
|---|
| 45 | this.realWidth = realWidth;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | public static LineElemStyle createLine(Environment env) {
|
|---|
| 49 | return createImpl(env, false);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | public static LineElemStyle createCasing(Environment env) {
|
|---|
| 53 | LineElemStyle casing = createImpl(env, true);
|
|---|
| 54 | if (casing != null) {
|
|---|
| 55 | casing.z_index += -100;
|
|---|
| 56 | casing.isModifier = true;
|
|---|
| 57 | }
|
|---|
| 58 | return casing;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | private static LineElemStyle createImpl(Environment env, boolean casing) {
|
|---|
| 62 | Cascade c = env.mc.getCascade(env.layer);
|
|---|
| 63 | Cascade c_def = env.mc.getCascade("default");
|
|---|
| 64 |
|
|---|
| 65 | String prefix = casing ? "casing-" : "";
|
|---|
| 66 |
|
|---|
| 67 | Float width;
|
|---|
| 68 | if (casing) {
|
|---|
| 69 | Float widthOnDefault = getWidth(c_def, "width", null);
|
|---|
| 70 | Float widthLine = getWidth(c, "width", widthOnDefault);
|
|---|
| 71 | width = getWidth(c, "casing-width", widthLine);
|
|---|
| 72 | } else {
|
|---|
| 73 | Float widthOnDefault = getWidth(c_def, "width", null);
|
|---|
| 74 | width = getWidth(c, "width", widthOnDefault);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | if (width == null)
|
|---|
| 78 | return null;
|
|---|
| 79 |
|
|---|
| 80 | float realWidth = c.get(prefix + "real-width", 0f, Float.class);
|
|---|
| 81 | if (realWidth > 0 && MapPaintSettings.INSTANCE.isUseRealWidth()) {
|
|---|
| 82 |
|
|---|
| 83 | /* if we have a "width" tag, try use it */
|
|---|
| 84 | String widthTag = env.osm.get("width");
|
|---|
| 85 | if(widthTag == null) {
|
|---|
| 86 | widthTag = env.osm.get("est_width");
|
|---|
| 87 | }
|
|---|
| 88 | if(widthTag != null) {
|
|---|
| 89 | try {
|
|---|
| 90 | realWidth = Float.valueOf(widthTag);
|
|---|
| 91 | }
|
|---|
| 92 | catch(NumberFormatException nfe) {
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | Color color = c.get(prefix + "color", null, Color.class);
|
|---|
| 98 | if (!casing && color == null) {
|
|---|
| 99 | color = c.get("fill-color", null, Color.class);
|
|---|
| 100 | }
|
|---|
| 101 | if (color == null) {
|
|---|
| 102 | color = PaintColors.UNTAGGED.get();
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | int alpha = 255;
|
|---|
| 106 | Integer pAlpha = Utils.color_float2int(c.get("opacity", null, Float.class));
|
|---|
| 107 | if (pAlpha != null) {
|
|---|
| 108 | alpha = pAlpha;
|
|---|
| 109 | }
|
|---|
| 110 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
|---|
| 111 |
|
|---|
| 112 | float[] dashes = c.get(prefix + "dashes", null, float[].class);
|
|---|
| 113 | if (dashes != null) {
|
|---|
| 114 | boolean hasPositive = false;
|
|---|
| 115 | for (float f : dashes) {
|
|---|
| 116 | if (f > 0) {
|
|---|
| 117 | hasPositive = true;
|
|---|
| 118 | }
|
|---|
| 119 | if (f < 0) {
|
|---|
| 120 | dashes = null;
|
|---|
| 121 | break;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | if (!hasPositive || (dashes != null && dashes.length == 0)) {
|
|---|
| 125 | dashes = null;
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | float dashesOffset = c.get(prefix + "dashes-offset", 0f, Float.class);
|
|---|
| 129 | Color dashesBackground = c.get(prefix + "dashes-background-color", null, Color.class);
|
|---|
| 130 | if (dashesBackground != null) {
|
|---|
| 131 | pAlpha = Utils.color_float2int(c.get(prefix + "dashes-background-opacity", null, Float.class));
|
|---|
| 132 | if (pAlpha != null) {
|
|---|
| 133 | alpha = pAlpha;
|
|---|
| 134 | }
|
|---|
| 135 | dashesBackground = new Color(dashesBackground.getRed(), dashesBackground.getGreen(),
|
|---|
| 136 | dashesBackground.getBlue(), alpha);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | Integer cap = null;
|
|---|
| 140 | Keyword capKW = c.get(prefix + "linecap", null, Keyword.class);
|
|---|
| 141 | if (capKW != null) {
|
|---|
| 142 | if (equal(capKW.val, "none")) {
|
|---|
| 143 | cap = BasicStroke.CAP_BUTT;
|
|---|
| 144 | } else if (equal(capKW.val, "round")) {
|
|---|
| 145 | cap = BasicStroke.CAP_ROUND;
|
|---|
| 146 | } else if (equal(capKW.val, "square")) {
|
|---|
| 147 | cap = BasicStroke.CAP_SQUARE;
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 | if (cap == null) {
|
|---|
| 151 | cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | Integer join = null;
|
|---|
| 155 | Keyword joinKW = c.get(prefix + "linejoin", null, Keyword.class);
|
|---|
| 156 | if (joinKW != null) {
|
|---|
| 157 | if (equal(joinKW.val, "round")) {
|
|---|
| 158 | join = BasicStroke.JOIN_ROUND;
|
|---|
| 159 | } else if (equal(joinKW.val, "miter")) {
|
|---|
| 160 | join = BasicStroke.JOIN_MITER;
|
|---|
| 161 | } else if (equal(joinKW.val, "bevel")) {
|
|---|
| 162 | join = BasicStroke.JOIN_BEVEL;
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | if (join == null) {
|
|---|
| 166 | join = BasicStroke.JOIN_ROUND;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | float miterlimit = c.get(prefix + "miterlimit", 10f, Float.class);
|
|---|
| 170 | if (miterlimit < 1f) {
|
|---|
| 171 | miterlimit = 10f;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
|
|---|
| 175 | BasicStroke dashesLine = null;
|
|---|
| 176 |
|
|---|
| 177 | if (dashes != null && dashesBackground != null) {
|
|---|
| 178 | float[] dashes2 = new float[dashes.length];
|
|---|
| 179 | System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1);
|
|---|
| 180 | dashes2[0] = dashes[dashes.length-1];
|
|---|
| 181 | dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | return new LineElemStyle(c, line, color, dashesLine, dashesBackground, realWidth);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | @Override
|
|---|
| 188 | public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
|
|---|
| 189 | Way w = (Way)primitive;
|
|---|
| 190 | /* show direction arrows, if draw.segment.relevant_directions_only is not set,
|
|---|
| 191 | the way is tagged with a direction key
|
|---|
| 192 | (even if the tag is negated as in oneway=false) or the way is selected */
|
|---|
| 193 | boolean showOrientation = !isModifier && (selected || paintSettings.isShowDirectionArrow()) && !paintSettings.isUseRealWidth();
|
|---|
| 194 | boolean showOneway = !isModifier && !selected &&
|
|---|
| 195 | !paintSettings.isUseRealWidth() &&
|
|---|
| 196 | paintSettings.isShowOnewayArrow() && w.hasDirectionKeys();
|
|---|
| 197 | boolean onewayReversed = w.reversedDirection();
|
|---|
| 198 | /* head only takes over control if the option is true,
|
|---|
| 199 | the direction should be shown at all and not only because it's selected */
|
|---|
| 200 | boolean showOnlyHeadArrowOnly = showOrientation && !selected && paintSettings.isShowHeadArrowOnly();
|
|---|
| 201 | Node lastN;
|
|---|
| 202 |
|
|---|
| 203 | Color myDashedColor = dashesBackground;
|
|---|
| 204 | BasicStroke myLine = line, myDashLine = dashesLine;
|
|---|
| 205 | if (realWidth > 0 && paintSettings.isUseRealWidth() && !showOrientation) {
|
|---|
| 206 | float myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
|
|---|
| 207 | if (myWidth < line.getLineWidth()) {
|
|---|
| 208 | myWidth = line.getLineWidth();
|
|---|
| 209 | }
|
|---|
| 210 | myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(),
|
|---|
| 211 | line.getMiterLimit(), line.getDashArray(), line.getDashPhase());
|
|---|
| 212 | if (dashesLine != null) {
|
|---|
| 213 | myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(),
|
|---|
| 214 | dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase());
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | Color myColor = color;
|
|---|
| 219 | if(w.isHighlighted()) {
|
|---|
| 220 | myColor = paintSettings.getHighlightColor();
|
|---|
| 221 | } else if (selected) {
|
|---|
| 222 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
|---|
| 223 | } else if (member) {
|
|---|
| 224 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
|---|
| 225 | } else if(w.isDisabled()) {
|
|---|
| 226 | myColor = paintSettings.getInactiveColor();
|
|---|
| 227 | myDashedColor = paintSettings.getInactiveColor();
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, showOrientation,
|
|---|
| 231 | showOnlyHeadArrowOnly, showOneway, onewayReversed);
|
|---|
| 232 |
|
|---|
| 233 | if(paintSettings.isShowOrderNumber()) {
|
|---|
| 234 | int orderNumber = 0;
|
|---|
| 235 | lastN = null;
|
|---|
| 236 | for(Node n : w.getNodes()) {
|
|---|
| 237 | if(lastN != null) {
|
|---|
| 238 | orderNumber++;
|
|---|
| 239 | painter.drawOrderNumber(lastN, n, orderNumber, myColor);
|
|---|
| 240 | }
|
|---|
| 241 | lastN = n;
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | @Override
|
|---|
| 247 | public boolean isProperLineStyle() {
|
|---|
| 248 | return !isModifier;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | @Override
|
|---|
| 252 | public boolean equals(Object obj) {
|
|---|
| 253 | if (obj == null || getClass() != obj.getClass())
|
|---|
| 254 | return false;
|
|---|
| 255 | if (!super.equals(obj))
|
|---|
| 256 | return false;
|
|---|
| 257 | final LineElemStyle other = (LineElemStyle) obj;
|
|---|
| 258 | return equal(line, other.line) &&
|
|---|
| 259 | equal(color, other.color) &&
|
|---|
| 260 | equal(dashesLine, other.dashesLine) &&
|
|---|
| 261 | equal(dashesBackground, other.dashesBackground) &&
|
|---|
| 262 | realWidth == other.realWidth;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | @Override
|
|---|
| 266 | public int hashCode() {
|
|---|
| 267 | int hash = super.hashCode();
|
|---|
| 268 | hash = 29 * hash + line.hashCode();
|
|---|
| 269 | hash = 29 * hash + color.hashCode();
|
|---|
| 270 | hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0);
|
|---|
| 271 | hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0);
|
|---|
| 272 | hash = 29 * hash + Float.floatToIntBits(realWidth);
|
|---|
| 273 | return hash;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | @Override
|
|---|
| 277 | public String toString() {
|
|---|
| 278 | return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
|
|---|
| 279 | " realWidth=" + realWidth + " color=" + Utils.toString(color) +
|
|---|
| 280 | " dashed=" + Arrays.toString(line.getDashArray()) +
|
|---|
| 281 | (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) +
|
|---|
| 282 | " dashedColor=" + Utils.toString(dashesBackground) +
|
|---|
| 283 | " linejoin=" + linejoinToString(line.getLineJoin()) +
|
|---|
| 284 | " linecap=" + linecapToString(line.getEndCap()) +
|
|---|
| 285 | '}';
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | public String linejoinToString(int linejoin) {
|
|---|
| 289 | switch (linejoin) {
|
|---|
| 290 | case BasicStroke.JOIN_BEVEL: return "bevel";
|
|---|
| 291 | case BasicStroke.JOIN_ROUND: return "round";
|
|---|
| 292 | case BasicStroke.JOIN_MITER: return "miter";
|
|---|
| 293 | default: return null;
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 | public String linecapToString(int linecap) {
|
|---|
| 297 | switch (linecap) {
|
|---|
| 298 | case BasicStroke.CAP_BUTT: return "none";
|
|---|
| 299 | case BasicStroke.CAP_ROUND: return "round";
|
|---|
| 300 | case BasicStroke.CAP_SQUARE: return "square";
|
|---|
| 301 | default: return null;
|
|---|
| 302 | }
|
|---|
| 303 | }
|
|---|
| 304 | }
|
|---|