source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java@ 3889

Last change on this file since 3889 was 3889, checked in by bastiK, 14 years ago

fixed #5673 - "oneway" arrows on ways should be painted in different style than way-direction arrows

  • Property svn:eol-style set to native
File size: 11.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import static org.openstreetmap.josm.tools.Utils.equal;
5
6import java.awt.BasicStroke;
7import java.awt.Color;
8import java.util.Arrays;
9
10import org.openstreetmap.josm.data.osm.Node;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.data.osm.Way;
13import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
14import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
15import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
16import org.openstreetmap.josm.tools.Utils;
17
18public 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.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.getCascade();
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 = new Float(Integer.parseInt(widthTag));
91 }
92 catch(NumberFormatException nfe) {
93 }
94 }
95 }
96
97 Color color = c.get(prefix + "color", null, Color.class);
98 if (color == null) {
99 color = c.get(prefix + "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.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 int cap;
140 String capStr = c.get(prefix + "linecap", null, String.class);
141 if (equal(capStr, "none")) {
142 cap = BasicStroke.CAP_BUTT;
143 } else if (equal(capStr, "round")) {
144 cap = BasicStroke.CAP_ROUND;
145 } else if (equal(capStr, "square")) {
146 cap = BasicStroke.CAP_SQUARE;
147 } else {
148 cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND;
149 }
150
151 int join;
152 String joinStr = c.get(prefix + "linejoin", null, String.class);
153 if (equal(joinStr, "round")) {
154 join = BasicStroke.JOIN_ROUND;
155 } else if (equal(joinStr, "miter")) {
156 join = BasicStroke.JOIN_MITER;
157 } else if (equal(joinStr, "bevel")) {
158 join = BasicStroke.JOIN_BEVEL;
159 } else {
160 join = BasicStroke.JOIN_ROUND;
161 }
162
163 float miterlimit = c.get(prefix + "miterlimit", 10f, Float.class);
164 if (miterlimit < 1f) {
165 miterlimit = 10f;
166 }
167
168 BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
169 BasicStroke dashesLine = null;
170
171 if (dashes != null && dashesBackground != null) {
172 float[] dashes2 = new float[dashes.length];
173 System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1);
174 dashes2[0] = dashes[dashes.length-1];
175 dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset);
176 }
177
178 TextElement text = null;
179 if (!casing) {
180 String textPos = c.get("text-position", null, String.class);
181 if (textPos == null || equal(textPos, "line")) {
182 text = TextElement.create(c, PaintColors.TEXT.get());
183 }
184 }
185
186 return new LineElemStyle(c, line, color, dashesLine, dashesBackground, text, realWidth);
187 }
188
189 @Override
190 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
191 Way w = (Way)primitive;
192 /* show direction arrows, if draw.segment.relevant_directions_only is not set,
193 the way is tagged with a direction key
194 (even if the tag is negated as in oneway=false) or the way is selected */
195 boolean showOrientation = !isModifier && selected && !paintSettings.isUseRealWidth();
196 boolean showOneway = !isModifier && !selected &&
197 !paintSettings.isUseRealWidth() &&
198 paintSettings.isShowDirectionArrow() && w.hasDirectionKeys();
199 boolean onewayReversed = w.reversedDirection();
200 /* head only takes over control if the option is true,
201 the direction should be shown at all and not only because it's selected */
202 boolean showOnlyHeadArrowOnly = showOrientation && !selected && paintSettings.isShowHeadArrowOnly();
203 Node lastN;
204
205 Color myDashedColor = dashesBackground;
206 BasicStroke myLine = line, myDashLine = dashesLine;
207 if (realWidth > 0 && paintSettings.isUseRealWidth() && !showOrientation) {
208 float myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
209 if (myWidth < line.getLineWidth()) {
210 myWidth = line.getLineWidth();
211 }
212 myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(),
213 line.getMiterLimit(), line.getDashArray(), line.getDashPhase());
214 if (dashesLine != null) {
215 myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(),
216 dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase());
217 }
218 }
219
220 Color myColor = color;
221 if(w.isHighlighted()) {
222 myColor = paintSettings.getHighlightColor();
223 } else if (selected) {
224 myColor = paintSettings.getSelectedColor(color.getAlpha());
225 } else if (member) {
226 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
227 } else if(w.isDisabled()) {
228 myColor = paintSettings.getInactiveColor();
229 myDashedColor = paintSettings.getInactiveColor();
230 }
231
232 painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, text, showOrientation,
233 showOnlyHeadArrowOnly, showOneway, onewayReversed);
234
235 if(paintSettings.isShowOrderNumber()) {
236 int orderNumber = 0;
237 lastN = null;
238 for(Node n : w.getNodes()) {
239 if(lastN != null) {
240 orderNumber++;
241 painter.drawOrderNumber(lastN, n, orderNumber);
242 }
243 lastN = n;
244 }
245 }
246 }
247
248 @Override
249 public boolean equals(Object obj) {
250 if (obj == null || getClass() != obj.getClass())
251 return false;
252 if (!super.equals(obj))
253 return false;
254 final LineElemStyle other = (LineElemStyle) obj;
255 return equal(line, other.line) &&
256 equal(color, other.color) &&
257 equal(dashesLine, other.dashesLine) &&
258 equal(dashesBackground, other.dashesBackground) &&
259 equal(text, other.text) &&
260 realWidth == other.realWidth;
261 }
262
263 @Override
264 public int hashCode() {
265 int hash = super.hashCode();
266 hash = 29 * hash + line.hashCode();
267 hash = 29 * hash + color.hashCode();
268 hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0);
269 hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0);
270 hash = 29 * hash + (text != null ? text.hashCode() : 0);
271 hash = 29 * hash + Float.floatToIntBits(realWidth);
272 return hash;
273 }
274
275 @Override
276 public String toString() {
277 return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
278 " realWidth=" + realWidth + " color=" + Utils.toString(color) +
279 " dashed=" + Arrays.toString(line.getDashArray()) +
280 (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) +
281 " dashedColor=" + Utils.toString(dashesBackground) + '}';
282 }
283}
Note: See TracBrowser for help on using the repository browser.