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

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

changed drawing of highlighted, selected and disabled ways to adapt the alpha value of overlays. see #5292

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