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

Last change on this file since 3856 was 3856, checked in by bastiK, 13 years ago

improve mapcss support

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