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

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

Experimental mapcss support. All *.java files in the gui/mappaint/mapcss/parser folder are generated from the javacc source file MapCSSParser.jj in the same folder. The generated code sums up to 2700 lines, there is no further build dependency.

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