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

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

mapcss: basic support for node shapes and some fine tuning

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