1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.util.Arrays;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.Node;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.Way;
|
---|
10 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
11 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
13 | import org.openstreetmap.josm.tools.Utils;
|
---|
14 |
|
---|
15 | public class LineElemStyle extends ElemStyle {
|
---|
16 |
|
---|
17 | public static LineElemStyle createSimpleLineStyle(Color color) {
|
---|
18 | return new LineElemStyle(Cascade.EMPTY_CASCADE, -1f, 0f, color != null ? color : PaintColors.UNTAGGED.get(), null, null);
|
---|
19 | }
|
---|
20 | public static final LineElemStyle UNTAGGED_WAY = createSimpleLineStyle(null);
|
---|
21 |
|
---|
22 | private float width;
|
---|
23 | public float realWidth; // the real width of this line in meter
|
---|
24 | public Color color;
|
---|
25 | private float[] dashed;
|
---|
26 | public Color dashedColor;
|
---|
27 |
|
---|
28 | protected LineElemStyle(Cascade c, float width, float realWidth, Color color, float[] dashed, Color dashedColor) {
|
---|
29 | super(c);
|
---|
30 | setWidth(width);
|
---|
31 | this.realWidth = realWidth;
|
---|
32 | this.color = color;
|
---|
33 | this.dashed = dashed;
|
---|
34 | this.dashedColor = dashedColor;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static LineElemStyle createLine(Cascade c) {
|
---|
38 | return createImpl(c, "");
|
---|
39 | }
|
---|
40 |
|
---|
41 | public static LineElemStyle createCasing(Cascade c) {
|
---|
42 | return createImpl(c, "casing-");
|
---|
43 | }
|
---|
44 |
|
---|
45 | private static LineElemStyle createImpl(Cascade c, String prefix) {
|
---|
46 | Float width = c.get(prefix + "width", null, Float.class);
|
---|
47 | if (width == null)
|
---|
48 | return null;
|
---|
49 |
|
---|
50 | float realWidth = c.get(prefix + "real-width", 0f, Float.class);
|
---|
51 | Color color = c.get(prefix + "color", null, Color.class);
|
---|
52 | if (color == null) {
|
---|
53 | color = c.get(prefix + "fill-color", null, Color.class);
|
---|
54 | }
|
---|
55 | if (color == null) {
|
---|
56 | color = PaintColors.UNTAGGED_WAY.get();
|
---|
57 | }
|
---|
58 | float[] dashes = c.get(prefix + "dashes", null, float[].class);
|
---|
59 | Color dashesBackground = c.get(prefix + "dashes-background-color", null, Color.class);
|
---|
60 |
|
---|
61 | return new LineElemStyle(c, width, realWidth, color, dashes, dashesBackground);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
|
---|
66 | Way w = (Way)primitive;
|
---|
67 | /* show direction arrows, if draw.segment.relevant_directions_only is not set,
|
---|
68 | the way is tagged with a direction key
|
---|
69 | (even if the tag is negated as in oneway=false) or the way is selected */
|
---|
70 | boolean showDirection = selected || ((!paintSettings.isUseRealWidth()) && (paintSettings.isShowDirectionArrow()
|
---|
71 | && (!paintSettings.isShowRelevantDirectionsOnly() || w.hasDirectionKeys())));
|
---|
72 | boolean reversedDirection = w.reversedDirection();
|
---|
73 | /* head only takes over control if the option is true,
|
---|
74 | the direction should be shown at all and not only because it's selected */
|
---|
75 | boolean showOnlyHeadArrowOnly = showDirection && !selected && paintSettings.isShowHeadArrowOnly();
|
---|
76 | Node lastN;
|
---|
77 |
|
---|
78 | Color myDashedColor = dashedColor;
|
---|
79 | float myWidth = getWidth();
|
---|
80 |
|
---|
81 | if (realWidth > 0 && paintSettings.isUseRealWidth() && !showDirection) {
|
---|
82 |
|
---|
83 | /* if we have a "width" tag, try use it */
|
---|
84 | /* (this might be slow and could be improved by caching the value in the Way, on the other hand only used if "real width" is enabled) */
|
---|
85 | String widthTag = w.get("width");
|
---|
86 | if(widthTag == null) {
|
---|
87 | widthTag = w.get("est_width");
|
---|
88 | }
|
---|
89 | if(widthTag != null) {
|
---|
90 | try {
|
---|
91 | realWidth = new Float(Integer.parseInt(widthTag));
|
---|
92 | }
|
---|
93 | catch(NumberFormatException nfe) {
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
|
---|
98 | if (myWidth < getWidth()) {
|
---|
99 | myWidth = getWidth();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | Color markColor = null;
|
---|
104 | if(w.isHighlighted()) {
|
---|
105 | markColor = paintSettings.getHighlightColor();
|
---|
106 | } else if (selected) {
|
---|
107 | markColor = paintSettings.getSelectedColor();
|
---|
108 | } else if (member) {
|
---|
109 | markColor = paintSettings.getRelationSelectedColor();
|
---|
110 | } else if(w.isDisabled()) {
|
---|
111 | markColor = paintSettings.getInactiveColor();
|
---|
112 | myDashedColor = paintSettings.getInactiveColor();
|
---|
113 | }
|
---|
114 |
|
---|
115 | painter.drawWay(w, markColor != null ? markColor : color, myWidth, dashed, myDashedColor, showDirection,
|
---|
116 | selected ? false : reversedDirection, showOnlyHeadArrowOnly);
|
---|
117 |
|
---|
118 | if(paintSettings.isShowOrderNumber()) {
|
---|
119 | int orderNumber = 0;
|
---|
120 | lastN = null;
|
---|
121 | for(Node n : w.getNodes()) {
|
---|
122 | if(lastN != null) {
|
---|
123 | orderNumber++;
|
---|
124 | painter.drawOrderNumber(lastN, n, orderNumber);
|
---|
125 | }
|
---|
126 | lastN = n;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public float getWidth() {
|
---|
132 | if (width == -1f)
|
---|
133 | return MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
|
---|
134 | return width;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void setWidth(float width) {
|
---|
138 | this.width = width;
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public boolean equals(Object obj) {
|
---|
143 | if (obj == null || getClass() != obj.getClass())
|
---|
144 | return false;
|
---|
145 | if (!super.equals(obj))
|
---|
146 | return false;
|
---|
147 | final LineElemStyle other = (LineElemStyle) obj;
|
---|
148 | return width == other.width &&
|
---|
149 | realWidth == other.realWidth &&
|
---|
150 | Utils.equal(color, other.color) &&
|
---|
151 | Arrays.equals(dashed, other.dashed) &&
|
---|
152 | Utils.equal(dashedColor, other.dashedColor);
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | public int hashCode() {
|
---|
157 | int hash = super.hashCode();
|
---|
158 | hash = 29 * hash + Float.floatToIntBits(width);
|
---|
159 | hash = 29 * hash + Float.floatToIntBits(realWidth);
|
---|
160 | hash = 29 * hash + color.hashCode();
|
---|
161 | hash = 29 * hash + Arrays.hashCode(dashed);
|
---|
162 | hash = 29 * hash + (dashedColor != null ? dashedColor.hashCode() : 0);
|
---|
163 | return hash;
|
---|
164 | }
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public String toString() {
|
---|
168 | return "LineElemStyle{" + super.toString() + "width=" + width + " realWidth=" + realWidth + " color=" + color + " dashed=" + Arrays.toString(dashed) + " dashedColor=" + dashedColor + '}';
|
---|
169 | }
|
---|
170 | }
|
---|