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

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

some renaming (to make future commits easier to read)

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.awt.Color;
5import java.util.Collection;
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.I18n;
14
15public class LineElemStyle extends ElemStyle implements Comparable<LineElemStyle> {
16
17 public static final LineElemStyle UNTAGGED_WAY;
18
19 static {
20 UNTAGGED_WAY = new LineElemStyle();
21 }
22
23 private int width;
24 public int realWidth; //the real width of this line in meter
25 public Color color;
26 private float[] dashed;
27 public Color dashedColor;
28
29 public boolean over;
30 public enum WidthMode { ABSOLUTE, PERCENT, OFFSET }
31 public WidthMode widthMode;
32
33 public Collection<LineElemStyle> overlays;
34
35 public LineElemStyle(LineElemStyle s, long maxScale, long minScale) {
36 this.width = s.width;
37 this.realWidth = s.realWidth;
38 this.color = s.color;
39 this.dashed = s.dashed;
40 this.dashedColor = s.dashedColor;
41 this.over = s.over;
42 this.widthMode = s.widthMode;
43
44 this.priority = s.priority;
45 this.maxScale = maxScale;
46 this.minScale = minScale;
47 this.conditions = s.conditions;
48 }
49
50 public LineElemStyle(LineElemStyle s, Collection<LineElemStyle> overlays) {
51 this.width = s.width;
52 this.realWidth = s.realWidth;
53 this.color = s.color;
54 this.dashed = s.dashed;
55 this.dashedColor = s.dashedColor;
56 this.over = s.over;
57 this.widthMode = s.widthMode;
58
59 this.priority = s.priority;
60 this.maxScale = s.maxScale;
61 this.minScale = s.minScale;
62 this.conditions = s.conditions;
63
64 this.overlays = overlays;
65 this.code = s.code;
66 for (LineElemStyle o : overlays) {
67 this.code += o.code;
68 }
69 }
70
71 public LineElemStyle() { init(); }
72
73 public void init()
74 {
75 width = -1;
76 realWidth = 0;
77 dashed = new float[0];
78 dashedColor = null;
79 priority = 0;
80 color = PaintColors.UNTAGGED.get();
81 over = true; // only used for line modifications
82 widthMode = WidthMode.ABSOLUTE;
83 overlays = null;
84 }
85
86 // get width for overlays
87 public int getWidth(int ref)
88 {
89 int res;
90 if(widthMode == WidthMode.ABSOLUTE) {
91 res = width;
92 } else if(widthMode == WidthMode.OFFSET) {
93 res = ref + width;
94 } else
95 {
96 if(width < 0) {
97 res = 0;
98 } else {
99 res = ref*width/100;
100 }
101 }
102 return res <= 0 ? 1 : res;
103 }
104
105 public int compareTo(LineElemStyle s) {
106 if(s.priority != priority)
107 return s.priority > priority ? 1 : -1;
108 if(!over && s.over)
109 return -1;
110 // we have no idea how to order other objects :-)
111 return 0;
112 }
113
114 public float[] getDashed() {
115 return dashed;
116 }
117
118 public void setDashed(float[] dashed) {
119 if (dashed.length == 0) {
120 this.dashed = dashed;
121 return;
122 }
123
124 boolean found = false;
125 for (int i=0; i<dashed.length; i++) {
126 if (dashed[i] > 0) {
127 found = true;
128 }
129 if (dashed[i] < 0) {
130 System.out.println(I18n.tr("Illegal dash pattern, values must be positive"));
131 }
132 }
133 if (found) {
134 this.dashed = dashed;
135 } else {
136 System.out.println(I18n.tr("Illegal dash pattern, at least one value must be > 0"));
137 }
138 }
139
140 @Override
141 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
142 Way w = (Way)primitive;
143 /* show direction arrows, if draw.segment.relevant_directions_only is not set,
144 the way is tagged with a direction key
145 (even if the tag is negated as in oneway=false) or the way is selected */
146 boolean showDirection = selected || ((!paintSettings.isUseRealWidth()) && (paintSettings.isShowDirectionArrow()
147 && (!paintSettings.isShowRelevantDirectionsOnly() || w.hasDirectionKeys())));
148 boolean reversedDirection = w.reversedDirection();
149 /* head only takes over control if the option is true,
150 the direction should be shown at all and not only because it's selected */
151 boolean showOnlyHeadArrowOnly = showDirection && !selected && paintSettings.isShowHeadArrowOnly();
152 Node lastN;
153
154 Color myDashedColor = dashedColor;
155 int myWidth = getWidth();
156
157 if (realWidth > 0 && paintSettings.isUseRealWidth() && !showDirection) {
158
159 /* if we have a "width" tag, try use it */
160 /* (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) */
161 String widthTag = w.get("width");
162 if(widthTag == null) {
163 widthTag = w.get("est_width");
164 }
165 if(widthTag != null) {
166 try {
167 realWidth = Integer.parseInt(widthTag);
168 }
169 catch(NumberFormatException nfe) {
170 }
171 }
172
173 myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
174 if (myWidth < getWidth()) {
175 myWidth = getWidth();
176 }
177 }
178
179 Color markColor = null;
180 if(w.isHighlighted()) {
181 markColor = paintSettings.getHighlightColor();
182 } else if (selected) {
183 markColor = paintSettings.getSelectedColor();
184 } else if (member) {
185 markColor = paintSettings.getRelationSelectedColor();
186 } else if(w.isDisabled()) {
187 markColor = paintSettings.getInactiveColor();
188 myDashedColor = paintSettings.getInactiveColor();
189 }
190
191 /* draw overlays under the way */
192 if(overlays != null) {
193 for(LineElemStyle s : overlays) {
194 if(!s.over) {
195 painter.drawWay(w,
196 markColor != null ?
197 (s.color != null ? new Color(markColor.getRed(), markColor.getGreen(), markColor.getBlue(), s.color.getAlpha()) : markColor) :
198 s.color,
199 s.getWidth(myWidth),
200 s.getDashed(),
201 w.isDisabled() ? paintSettings.getInactiveColor() : s.dashedColor,
202 false, false, false);
203 }
204 }
205 }
206
207 /* draw the way */
208 painter.drawWay(w, markColor != null ? markColor : color, myWidth, dashed, myDashedColor, showDirection,
209 selected ? false : reversedDirection, showOnlyHeadArrowOnly);
210
211 /* draw overlays above the way */
212 if(overlays != null) {
213 for(LineElemStyle s : overlays) {
214 if(s.over) {
215 painter.drawWay(w,
216 markColor != null ?
217 (s.color != null ? new Color(markColor.getRed(), markColor.getGreen(), markColor.getBlue(), s.color.getAlpha()) : markColor) :
218 s.color,
219 s.getWidth(myWidth),
220 s.getDashed(),
221 s.dashedColor,
222 false, false, false);
223 }
224 }
225 }
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 public int getWidth() {
241 if (width == -1)
242 return MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
243 return width;
244 }
245
246 public void setWidth(int width) {
247 this.width = width;
248 }
249}
Note: See TracBrowser for help on using the repository browser.