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