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

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

mapcss: support for opacity & icon-image

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