1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
5 |
|
---|
6 | import java.awt.BasicStroke;
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.util.Arrays;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
14 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
16 | import org.openstreetmap.josm.tools.Utils;
|
---|
17 |
|
---|
18 | public class LineElemStyle extends ElemStyle {
|
---|
19 |
|
---|
20 | public static LineElemStyle createSimpleLineStyle(Color color) {
|
---|
21 | MultiCascade mc = new MultiCascade();
|
---|
22 | Cascade c = mc.getOrCreateCascade("default");
|
---|
23 | c.put("width", -1f);
|
---|
24 | c.put("color", color != null ? color : PaintColors.UNTAGGED.get());
|
---|
25 | return createLine(new Environment(null, mc, "default", null));
|
---|
26 | }
|
---|
27 | public static final LineElemStyle UNTAGGED_WAY = createSimpleLineStyle(null);
|
---|
28 |
|
---|
29 | private BasicStroke line;
|
---|
30 | public Color color;
|
---|
31 | public Color dashesBackground;
|
---|
32 | public TextElement text;
|
---|
33 | public float realWidth; // the real width of this line in meter
|
---|
34 |
|
---|
35 | private BasicStroke dashesLine;
|
---|
36 |
|
---|
37 | protected LineElemStyle(Cascade c, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, TextElement text, float realWidth) {
|
---|
38 | super(c);
|
---|
39 | this.line = line;
|
---|
40 | this.color = color;
|
---|
41 | this.dashesLine = dashesLine;
|
---|
42 | this.dashesBackground = dashesBackground;
|
---|
43 | this.text = text;
|
---|
44 | this.realWidth = realWidth;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static LineElemStyle createLine(Environment env) {
|
---|
48 | return createImpl(env, false);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static LineElemStyle createCasing(Environment env) {
|
---|
52 | LineElemStyle casing = createImpl(env, true);
|
---|
53 | if (casing != null) {
|
---|
54 | casing.z_index = -100;
|
---|
55 | casing.isModifier = true;
|
---|
56 | }
|
---|
57 | return casing;
|
---|
58 | }
|
---|
59 |
|
---|
60 | private static LineElemStyle createImpl(Environment env, boolean casing) {
|
---|
61 | Cascade c = env.mc.getCascade(env.layer);
|
---|
62 | Cascade c_def = env.mc.getCascade("default");
|
---|
63 |
|
---|
64 | String prefix = casing ? "casing-" : "";
|
---|
65 |
|
---|
66 | Float width;
|
---|
67 | if (casing) {
|
---|
68 | Float widthOnDefault = getWidth(c_def, "width", null);
|
---|
69 | Float widthLine = getWidth(c, "width", widthOnDefault);
|
---|
70 | width = getWidth(c, "casing-width", widthLine);
|
---|
71 | } else {
|
---|
72 | Float widthOnDefault = getWidth(c_def, "width", null);
|
---|
73 | width = getWidth(c, "width", widthOnDefault);
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (width == null)
|
---|
77 | return null;
|
---|
78 |
|
---|
79 | float realWidth = c.get(prefix + "real-width", 0f, Float.class);
|
---|
80 | if (realWidth > 0 && MapPaintSettings.INSTANCE.isUseRealWidth()) {
|
---|
81 |
|
---|
82 | /* if we have a "width" tag, try use it */
|
---|
83 | String widthTag = env.osm.get("width");
|
---|
84 | if(widthTag == null) {
|
---|
85 | widthTag = env.osm.get("est_width");
|
---|
86 | }
|
---|
87 | if(widthTag != null) {
|
---|
88 | try {
|
---|
89 | realWidth = new Float(Integer.parseInt(widthTag));
|
---|
90 | }
|
---|
91 | catch(NumberFormatException nfe) {
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | Color color = c.get(prefix + "color", null, Color.class);
|
---|
97 | if (color == null) {
|
---|
98 | color = c.get(prefix + "fill-color", null, Color.class);
|
---|
99 | }
|
---|
100 | if (color == null) {
|
---|
101 | color = PaintColors.UNTAGGED.get();
|
---|
102 | }
|
---|
103 |
|
---|
104 | int alpha = 255;
|
---|
105 | Integer pAlpha = Utils.color_float2int(c.get("opacity", null, float.class));
|
---|
106 | if (pAlpha != null) {
|
---|
107 | alpha = pAlpha;
|
---|
108 | }
|
---|
109 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
---|
110 |
|
---|
111 | float[] dashes = c.get(prefix + "dashes", null, float[].class);
|
---|
112 | if (dashes != null) {
|
---|
113 | boolean hasPositive = false;
|
---|
114 | for (float f : dashes) {
|
---|
115 | if (f > 0) {
|
---|
116 | hasPositive = true;
|
---|
117 | }
|
---|
118 | if (f < 0) {
|
---|
119 | dashes = null;
|
---|
120 | break;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | if (!hasPositive || dashes.length == 0) {
|
---|
124 | dashes = null;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | float dashesOffset = c.get(prefix + "dashes-offset", 0f, Float.class);
|
---|
128 | Color dashesBackground = c.get(prefix + "dashes-background-color", null, Color.class);
|
---|
129 | if (dashesBackground != null) {
|
---|
130 | pAlpha = Utils.color_float2int(c.get(prefix + "dashes-background-opacity", null, Float.class));
|
---|
131 | if (pAlpha != null) {
|
---|
132 | alpha = pAlpha;
|
---|
133 | }
|
---|
134 | dashesBackground = new Color(dashesBackground.getRed(), dashesBackground.getGreen(),
|
---|
135 | dashesBackground.getBlue(), alpha);
|
---|
136 | }
|
---|
137 |
|
---|
138 | int cap;
|
---|
139 | String capStr = c.get(prefix + "linecap", null, String.class);
|
---|
140 | if (equal(capStr, "none")) {
|
---|
141 | cap = BasicStroke.CAP_BUTT;
|
---|
142 | } else if (equal(capStr, "round")) {
|
---|
143 | cap = BasicStroke.CAP_ROUND;
|
---|
144 | } else if (equal(capStr, "square")) {
|
---|
145 | cap = BasicStroke.CAP_SQUARE;
|
---|
146 | } else {
|
---|
147 | cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND;
|
---|
148 | }
|
---|
149 |
|
---|
150 | int join;
|
---|
151 | String joinStr = c.get(prefix + "linejoin", null, String.class);
|
---|
152 | if (equal(joinStr, "round")) {
|
---|
153 | join = BasicStroke.JOIN_ROUND;
|
---|
154 | } else if (equal(joinStr, "miter")) {
|
---|
155 | join = BasicStroke.JOIN_MITER;
|
---|
156 | } else if (equal(joinStr, "bevel")) {
|
---|
157 | join = BasicStroke.JOIN_BEVEL;
|
---|
158 | } else {
|
---|
159 | join = BasicStroke.JOIN_ROUND;
|
---|
160 | }
|
---|
161 |
|
---|
162 | float miterlimit = c.get(prefix + "miterlimit", 10f, Float.class);
|
---|
163 | if (miterlimit < 1f) {
|
---|
164 | miterlimit = 10f;
|
---|
165 | }
|
---|
166 |
|
---|
167 | BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
|
---|
168 | BasicStroke dashesLine = null;
|
---|
169 |
|
---|
170 | if (dashes != null && dashesBackground != null) {
|
---|
171 | float[] dashes2 = new float[dashes.length];
|
---|
172 | System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1);
|
---|
173 | dashes2[0] = dashes[dashes.length-1];
|
---|
174 | dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset);
|
---|
175 | }
|
---|
176 |
|
---|
177 | TextElement text = null;
|
---|
178 | if (!casing) {
|
---|
179 | String textPos = c.get("text-position", null, String.class);
|
---|
180 | if (textPos == null || equal(textPos, "line")) {
|
---|
181 | text = TextElement.create(c, PaintColors.TEXT.get());
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | return new LineElemStyle(c, line, color, dashesLine, dashesBackground, text, realWidth);
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
|
---|
190 | Way w = (Way)primitive;
|
---|
191 | /* show direction arrows, if draw.segment.relevant_directions_only is not set,
|
---|
192 | the way is tagged with a direction key
|
---|
193 | (even if the tag is negated as in oneway=false) or the way is selected */
|
---|
194 | boolean showOrientation = !isModifier && selected && !paintSettings.isUseRealWidth();
|
---|
195 | boolean showOneway = !isModifier && !selected &&
|
---|
196 | !paintSettings.isUseRealWidth() &&
|
---|
197 | paintSettings.isShowDirectionArrow() && w.hasDirectionKeys();
|
---|
198 | boolean onewayReversed = w.reversedDirection();
|
---|
199 | /* head only takes over control if the option is true,
|
---|
200 | the direction should be shown at all and not only because it's selected */
|
---|
201 | boolean showOnlyHeadArrowOnly = showOrientation && !selected && paintSettings.isShowHeadArrowOnly();
|
---|
202 | Node lastN;
|
---|
203 |
|
---|
204 | Color myDashedColor = dashesBackground;
|
---|
205 | BasicStroke myLine = line, myDashLine = dashesLine;
|
---|
206 | if (realWidth > 0 && paintSettings.isUseRealWidth() && !showOrientation) {
|
---|
207 | float myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
|
---|
208 | if (myWidth < line.getLineWidth()) {
|
---|
209 | myWidth = line.getLineWidth();
|
---|
210 | }
|
---|
211 | myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(),
|
---|
212 | line.getMiterLimit(), line.getDashArray(), line.getDashPhase());
|
---|
213 | if (dashesLine != null) {
|
---|
214 | myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(),
|
---|
215 | dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase());
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | Color myColor = color;
|
---|
220 | if(w.isHighlighted()) {
|
---|
221 | myColor = paintSettings.getHighlightColor();
|
---|
222 | } else if (selected) {
|
---|
223 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
224 | } else if (member) {
|
---|
225 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
226 | } else if(w.isDisabled()) {
|
---|
227 | myColor = paintSettings.getInactiveColor();
|
---|
228 | myDashedColor = paintSettings.getInactiveColor();
|
---|
229 | }
|
---|
230 |
|
---|
231 | painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, text, showOrientation,
|
---|
232 | showOnlyHeadArrowOnly, showOneway, onewayReversed);
|
---|
233 |
|
---|
234 | if(paintSettings.isShowOrderNumber()) {
|
---|
235 | int orderNumber = 0;
|
---|
236 | lastN = null;
|
---|
237 | for(Node n : w.getNodes()) {
|
---|
238 | if(lastN != null) {
|
---|
239 | orderNumber++;
|
---|
240 | painter.drawOrderNumber(lastN, n, orderNumber);
|
---|
241 | }
|
---|
242 | lastN = n;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | @Override
|
---|
248 | public boolean equals(Object obj) {
|
---|
249 | if (obj == null || getClass() != obj.getClass())
|
---|
250 | return false;
|
---|
251 | if (!super.equals(obj))
|
---|
252 | return false;
|
---|
253 | final LineElemStyle other = (LineElemStyle) obj;
|
---|
254 | return equal(line, other.line) &&
|
---|
255 | equal(color, other.color) &&
|
---|
256 | equal(dashesLine, other.dashesLine) &&
|
---|
257 | equal(dashesBackground, other.dashesBackground) &&
|
---|
258 | equal(text, other.text) &&
|
---|
259 | realWidth == other.realWidth;
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public int hashCode() {
|
---|
264 | int hash = super.hashCode();
|
---|
265 | hash = 29 * hash + line.hashCode();
|
---|
266 | hash = 29 * hash + color.hashCode();
|
---|
267 | hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0);
|
---|
268 | hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0);
|
---|
269 | hash = 29 * hash + (text != null ? text.hashCode() : 0);
|
---|
270 | hash = 29 * hash + Float.floatToIntBits(realWidth);
|
---|
271 | return hash;
|
---|
272 | }
|
---|
273 |
|
---|
274 | @Override
|
---|
275 | public String toString() {
|
---|
276 | return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
|
---|
277 | " realWidth=" + realWidth + " color=" + Utils.toString(color) +
|
---|
278 | " dashed=" + Arrays.toString(line.getDashArray()) +
|
---|
279 | (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) +
|
---|
280 | " dashedColor=" + Utils.toString(dashesBackground) + '}';
|
---|
281 | }
|
---|
282 | }
|
---|