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

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

MapCSS: an identifier literal at the right side of a declaration is now parsed as Keyword and not as String. This means 'color: "blue";' does not work any longer, but you have to write 'color: blue;'. This is useful for future extensions.

  • Property svn:eol-style set to native
File size: 11.2 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 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 return createLine(new Environment(null, mc, "default", null));
26 }
27 public static final LineElemStyle UNTAGGED_WAY = createSimpleLineStyle(null);
28
29 private BasicStroke line;
30 public Color color;
31 public Color dashesBackground;
32 public TextElement text;
33 public float realWidth; // the real width of this line in meter
34
35 private BasicStroke dashesLine;
36
37 protected LineElemStyle(Cascade c, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, TextElement text, float realWidth) {
38 super(c);
39 this.line = line;
40 this.color = color;
41 this.dashesLine = dashesLine;
42 this.dashesBackground = dashesBackground;
43 this.text = text;
44 this.realWidth = realWidth;
45 }
46
47 public static LineElemStyle createLine(Environment env) {
48 return createImpl(env, false);
49 }
50
51 public static LineElemStyle createCasing(Environment env) {
52 LineElemStyle casing = createImpl(env, true);
53 if (casing != null) {
54 casing.z_index = -100;
55 casing.isModifier = true;
56 }
57 return casing;
58 }
59
60 private static LineElemStyle createImpl(Environment env, boolean casing) {
61 Cascade c = env.mc.getCascade(env.layer);
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 (!casing && color == null) {
98 color = c.get("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 Integer cap = null;
139 Keyword capKW = c.get(prefix + "linecap", null, Keyword.class);
140 if (capKW != null) {
141 if (equal(capKW.val, "none")) {
142 cap = BasicStroke.CAP_BUTT;
143 } else if (equal(capKW.val, "round")) {
144 cap = BasicStroke.CAP_ROUND;
145 } else if (equal(capKW.val, "square")) {
146 cap = BasicStroke.CAP_SQUARE;
147 }
148 }
149 if (cap == null) {
150 cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND;
151 }
152
153 Integer join = null;
154 Keyword joinKW = c.get(prefix + "linejoin", null, Keyword.class);
155 if (joinKW != null) {
156 if (equal(joinKW.val, "round")) {
157 join = BasicStroke.JOIN_ROUND;
158 } else if (equal(joinKW.val, "miter")) {
159 join = BasicStroke.JOIN_MITER;
160 } else if (equal(joinKW.val, "bevel")) {
161 join = BasicStroke.JOIN_BEVEL;
162 }
163 }
164 if (join == null) {
165 join = BasicStroke.JOIN_ROUND;
166 }
167
168 float miterlimit = c.get(prefix + "miterlimit", 10f, Float.class);
169 if (miterlimit < 1f) {
170 miterlimit = 10f;
171 }
172
173 BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
174 BasicStroke dashesLine = null;
175
176 if (dashes != null && dashesBackground != null) {
177 float[] dashes2 = new float[dashes.length];
178 System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1);
179 dashes2[0] = dashes[dashes.length-1];
180 dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset);
181 }
182
183 TextElement text = null;
184 if (!casing) {
185 Keyword textPos = c.get("text-position", null, Keyword.class);
186 if (textPos == null || equal(textPos.val, "line")) {
187 text = TextElement.create(c, PaintColors.TEXT.get());
188 }
189 }
190
191 return new LineElemStyle(c, line, color, dashesLine, dashesBackground, text, realWidth);
192 }
193
194 @Override
195 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
196 Way w = (Way)primitive;
197 /* show direction arrows, if draw.segment.relevant_directions_only is not set,
198 the way is tagged with a direction key
199 (even if the tag is negated as in oneway=false) or the way is selected */
200 boolean showOrientation = !isModifier && selected && !paintSettings.isUseRealWidth();
201 boolean showOneway = !isModifier && !selected &&
202 !paintSettings.isUseRealWidth() &&
203 paintSettings.isShowDirectionArrow() && w.hasDirectionKeys();
204 boolean onewayReversed = w.reversedDirection();
205 /* head only takes over control if the option is true,
206 the direction should be shown at all and not only because it's selected */
207 boolean showOnlyHeadArrowOnly = showOrientation && !selected && paintSettings.isShowHeadArrowOnly();
208 Node lastN;
209
210 Color myDashedColor = dashesBackground;
211 BasicStroke myLine = line, myDashLine = dashesLine;
212 if (realWidth > 0 && paintSettings.isUseRealWidth() && !showOrientation) {
213 float myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
214 if (myWidth < line.getLineWidth()) {
215 myWidth = line.getLineWidth();
216 }
217 myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(),
218 line.getMiterLimit(), line.getDashArray(), line.getDashPhase());
219 if (dashesLine != null) {
220 myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(),
221 dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase());
222 }
223 }
224
225 Color myColor = color;
226 if(w.isHighlighted()) {
227 myColor = paintSettings.getHighlightColor();
228 } else if (selected) {
229 myColor = paintSettings.getSelectedColor(color.getAlpha());
230 } else if (member) {
231 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
232 } else if(w.isDisabled()) {
233 myColor = paintSettings.getInactiveColor();
234 myDashedColor = paintSettings.getInactiveColor();
235 }
236
237 painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, text, showOrientation,
238 showOnlyHeadArrowOnly, showOneway, onewayReversed);
239
240 if(paintSettings.isShowOrderNumber()) {
241 int orderNumber = 0;
242 lastN = null;
243 for(Node n : w.getNodes()) {
244 if(lastN != null) {
245 orderNumber++;
246 painter.drawOrderNumber(lastN, n, orderNumber, myColor);
247 }
248 lastN = n;
249 }
250 }
251 }
252
253 @Override
254 public boolean equals(Object obj) {
255 if (obj == null || getClass() != obj.getClass())
256 return false;
257 if (!super.equals(obj))
258 return false;
259 final LineElemStyle other = (LineElemStyle) obj;
260 return equal(line, other.line) &&
261 equal(color, other.color) &&
262 equal(dashesLine, other.dashesLine) &&
263 equal(dashesBackground, other.dashesBackground) &&
264 equal(text, other.text) &&
265 realWidth == other.realWidth;
266 }
267
268 @Override
269 public int hashCode() {
270 int hash = super.hashCode();
271 hash = 29 * hash + line.hashCode();
272 hash = 29 * hash + color.hashCode();
273 hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0);
274 hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0);
275 hash = 29 * hash + (text != null ? text.hashCode() : 0);
276 hash = 29 * hash + Float.floatToIntBits(realWidth);
277 return hash;
278 }
279
280 @Override
281 public String toString() {
282 return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
283 " realWidth=" + realWidth + " color=" + Utils.toString(color) +
284 " dashed=" + Arrays.toString(line.getDashArray()) +
285 (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) +
286 " dashedColor=" + Utils.toString(dashesBackground) + '}';
287 }
288}
Note: See TracBrowser for help on using the repository browser.