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

Last change on this file since 3871 was 3871, checked in by bastiK, 15 years ago

mapcss: font attributes & text alignment

  • 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 if (miterlimit < 1f) {
167 miterlimit = 10f;
168 }
169
170 BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
171 BasicStroke dashesLine = null;
172
173 if (dashes != null && dashesBackground != null) {
174 float[] dashes2 = new float[dashes.length];
175 System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1);
176 dashes2[0] = dashes[dashes.length-1];
177 dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset);
178 }
179
180 return new LineElemStyle(c, line, color, dashesLine, dashesBackground, realWidth);
181 }
182
183 @Override
184 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
185 Way w = (Way)primitive;
186 /* show direction arrows, if draw.segment.relevant_directions_only is not set,
187 the way is tagged with a direction key
188 (even if the tag is negated as in oneway=false) or the way is selected */
189 boolean showDirection = selected || ((!paintSettings.isUseRealWidth()) && (paintSettings.isShowDirectionArrow()
190 && (!paintSettings.isShowRelevantDirectionsOnly() || w.hasDirectionKeys())));
191 boolean reversedDirection = w.reversedDirection();
192 /* head only takes over control if the option is true,
193 the direction should be shown at all and not only because it's selected */
194 boolean showOnlyHeadArrowOnly = showDirection && !selected && paintSettings.isShowHeadArrowOnly();
195 Node lastN;
196
197 Color myDashedColor = dashesBackground;
198 BasicStroke myLine = line, myDashLine = dashesLine;
199 if (realWidth > 0 && paintSettings.isUseRealWidth() && !showDirection) {
200 float myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
201 if (myWidth < line.getLineWidth()) {
202 myWidth = line.getLineWidth();
203 }
204 myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(),
205 line.getMiterLimit(), line.getDashArray(), line.getDashPhase());
206 if (dashesLine != null) {
207 myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(),
208 dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase());
209 }
210 }
211
212 Color markColor = null;
213 if(w.isHighlighted()) {
214 markColor = paintSettings.getHighlightColor();
215 } else if (selected) {
216 markColor = paintSettings.getSelectedColor(color.getAlpha());
217 } else if (member) {
218 markColor = paintSettings.getRelationSelectedColor(color.getAlpha());
219 } else if(w.isDisabled()) {
220 markColor = paintSettings.getInactiveColor();
221 myDashedColor = paintSettings.getInactiveColor();
222 }
223
224 painter.drawWay(w, markColor != null ? markColor : color, myLine, myDashLine, myDashedColor, showDirection,
225 selected ? false : reversedDirection, showOnlyHeadArrowOnly);
226
227 if(paintSettings.isShowOrderNumber()) {
228 int orderNumber = 0;
229 lastN = null;
230 for(Node n : w.getNodes()) {
231 if(lastN != null) {
232 orderNumber++;
233 painter.drawOrderNumber(lastN, n, orderNumber);
234 }
235 lastN = n;
236 }
237 }
238 }
239
240 @Override
241 public boolean equals(Object obj) {
242 if (obj == null || getClass() != obj.getClass())
243 return false;
244 if (!super.equals(obj))
245 return false;
246 final LineElemStyle other = (LineElemStyle) obj;
247 return equal(line, other.line) &&
248 equal(color, other.color) &&
249 equal(dashesLine, other.dashesLine) &&
250 equal(dashesBackground, other.dashesBackground) &&
251 realWidth == other.realWidth;
252 }
253
254 @Override
255 public int hashCode() {
256 int hash = super.hashCode();
257 hash = 29 * hash + line.hashCode();
258 hash = 29 * hash + color.hashCode();
259 hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0);
260 hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0);
261 hash = 29 * hash + Float.floatToIntBits(realWidth);
262 return hash;
263 }
264
265 @Override
266 public String toString() {
267 return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
268 " realWidth=" + realWidth + " color=" + Utils.toString(color) +
269 " dashed=" + Arrays.toString(line.getDashArray()) +
270 (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) +
271 " dashedColor=" + Utils.toString(dashesBackground) + '}';
272 }
273}
Note: See TracBrowser for help on using the repository browser.