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

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

mapcss: dashes

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.awt.Color;
5import java.util.Arrays;
6
7import org.openstreetmap.josm.data.osm.Node;
8import org.openstreetmap.josm.data.osm.OsmPrimitive;
9import org.openstreetmap.josm.data.osm.Way;
10import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
11import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
12import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
13import org.openstreetmap.josm.tools.Utils;
14
15public 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, 0f, 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 private float dashesOffset;
27 public Color dashedColor;
28
29 protected LineElemStyle(Cascade c, float width, float realWidth, Color color, float[] dashed, float dashesOffset, Color dashedColor) {
30 super(c);
31 setWidth(width);
32 this.realWidth = realWidth;
33 this.color = color;
34 this.dashed = dashed;
35 this.dashesOffset = dashesOffset;
36 this.dashedColor = dashedColor;
37 }
38
39 public static LineElemStyle createLine(Cascade c) {
40 return createImpl(c, "");
41 }
42
43 public static LineElemStyle createCasing(Cascade c) {
44 LineElemStyle casing = createImpl(c, "casing-");
45 if (casing != null) {
46 casing.object_z_index = -1;
47 }
48 return casing;
49 }
50
51 private static LineElemStyle createImpl(Cascade c, String prefix) {
52 Float width = c.get(prefix + "width", null, Float.class);
53 if (width == null)
54 return null;
55
56 float realWidth = c.get(prefix + "real-width", 0f, Float.class);
57 Color color = c.get(prefix + "color", null, Color.class);
58 if (color == null) {
59 color = c.get(prefix + "fill-color", null, Color.class);
60 }
61 if (color == null) {
62 color = PaintColors.UNTAGGED.get();
63 }
64
65 int alpha = 255;
66 Integer pAlpha = color_float2int(c.get("opacity", null, float.class));
67 if (pAlpha != null) {
68 alpha = pAlpha;
69 }
70 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
71
72 float[] dashes = c.get(prefix + "dashes", null, float[].class);
73 if (dashes != null) {
74 boolean hasPositive = false;
75 for (float f : dashes) {
76 if (f > 0) {
77 hasPositive = true;
78 }
79 if (f < 0) {
80 dashes = null;
81 break;
82 }
83 }
84 if (!hasPositive) {
85 dashes = null;
86 }
87 }
88 float dashesOffset = c.get(prefix + "dashes-offset", 0f, Float.class);
89 Color dashesBackground = c.get(prefix + "dashes-background-color", null, Color.class);
90 if (dashesBackground != null) {
91 pAlpha = color_float2int(c.get(prefix + "dashes-background-opacity", null, Float.class));
92 if (pAlpha != null) {
93 alpha = pAlpha;
94 }
95 dashesBackground = new Color(dashesBackground.getRed(), dashesBackground.getGreen(),
96 dashesBackground.getBlue(), alpha);
97 }
98
99 return new LineElemStyle(c, width, realWidth, color, dashes, dashesOffset, dashesBackground);
100 }
101
102 @Override
103 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
104 Way w = (Way)primitive;
105 /* show direction arrows, if draw.segment.relevant_directions_only is not set,
106 the way is tagged with a direction key
107 (even if the tag is negated as in oneway=false) or the way is selected */
108 boolean showDirection = selected || ((!paintSettings.isUseRealWidth()) && (paintSettings.isShowDirectionArrow()
109 && (!paintSettings.isShowRelevantDirectionsOnly() || w.hasDirectionKeys())));
110 boolean reversedDirection = w.reversedDirection();
111 /* head only takes over control if the option is true,
112 the direction should be shown at all and not only because it's selected */
113 boolean showOnlyHeadArrowOnly = showDirection && !selected && paintSettings.isShowHeadArrowOnly();
114 Node lastN;
115
116 Color myDashedColor = dashedColor;
117 float myWidth = getWidth();
118
119 if (realWidth > 0 && paintSettings.isUseRealWidth() && !showDirection) {
120
121 /* if we have a "width" tag, try use it */
122 /* (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) */
123 String widthTag = w.get("width");
124 if(widthTag == null) {
125 widthTag = w.get("est_width");
126 }
127 if(widthTag != null) {
128 try {
129 realWidth = new Float(Integer.parseInt(widthTag));
130 }
131 catch(NumberFormatException nfe) {
132 }
133 }
134
135 myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
136 if (myWidth < getWidth()) {
137 myWidth = getWidth();
138 }
139 }
140
141 Color markColor = null;
142 if(w.isHighlighted()) {
143 markColor = paintSettings.getHighlightColor();
144 } else if (selected) {
145 markColor = paintSettings.getSelectedColor(color.getAlpha());
146 } else if (member) {
147 markColor = paintSettings.getRelationSelectedColor(color.getAlpha());
148 } else if(w.isDisabled()) {
149 markColor = paintSettings.getInactiveColor();
150 myDashedColor = paintSettings.getInactiveColor();
151 }
152
153 painter.drawWay(w, markColor != null ? markColor : color, myWidth, dashed,
154 dashesOffset, myDashedColor, showDirection,
155 selected ? false : reversedDirection, showOnlyHeadArrowOnly);
156
157 if(paintSettings.isShowOrderNumber()) {
158 int orderNumber = 0;
159 lastN = null;
160 for(Node n : w.getNodes()) {
161 if(lastN != null) {
162 orderNumber++;
163 painter.drawOrderNumber(lastN, n, orderNumber);
164 }
165 lastN = n;
166 }
167 }
168 }
169
170 public float getWidth() {
171 if (width == -1f)
172 return MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
173 return width;
174 }
175
176 public void setWidth(float width) {
177 this.width = width;
178 }
179
180 @Override
181 public boolean equals(Object obj) {
182 if (obj == null || getClass() != obj.getClass())
183 return false;
184 if (!super.equals(obj))
185 return false;
186 final LineElemStyle other = (LineElemStyle) obj;
187 return width == other.width &&
188 realWidth == other.realWidth &&
189 Utils.equal(color, other.color) &&
190 Arrays.equals(dashed, other.dashed) &&
191 dashesOffset == other.dashesOffset &&
192 Utils.equal(dashedColor, other.dashedColor);
193 }
194
195 @Override
196 public int hashCode() {
197 int hash = super.hashCode();
198 hash = 29 * hash + Float.floatToIntBits(width);
199 hash = 29 * hash + Float.floatToIntBits(realWidth);
200 hash = 29 * hash + color.hashCode();
201 hash = 29 * hash + Arrays.hashCode(dashed);
202 hash = 29 * hash + Float.floatToIntBits(dashesOffset);
203 hash = 29 * hash + (dashedColor != null ? dashedColor.hashCode() : 0);
204 return hash;
205 }
206
207 @Override
208 public String toString() {
209 return "LineElemStyle{" + super.toString() + "width=" + width +
210 " realWidth=" + realWidth + " color=" + Utils.toString(color) +
211 " dashed=" + Arrays.toString(dashed) +
212 (dashesOffset == 0f ? "" : " dashesOffses=" + dashesOffset) +
213 " dashedColor=" + Utils.toString(dashedColor) + '}';
214 }
215}
Note: See TracBrowser for help on using the repository browser.