1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import java.awt.BasicStroke;
|
---|
5 | import java.awt.Color;
|
---|
6 | import java.awt.Image;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 | import java.awt.Stroke;
|
---|
9 | import java.util.Objects;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.BoxProvider;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.SimpleBoxProvider;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
|
---|
21 | import org.openstreetmap.josm.gui.util.RotationAngle;
|
---|
22 | import org.openstreetmap.josm.tools.Utils;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * applies for Nodes and turn restriction relations
|
---|
26 | */
|
---|
27 | public class NodeElemStyle extends ElemStyle implements StyleKeys {
|
---|
28 | public final MapImage mapImage;
|
---|
29 | public final RotationAngle mapImageAngle;
|
---|
30 | public final Symbol symbol;
|
---|
31 |
|
---|
32 | private Image enabledNodeIcon;
|
---|
33 | private Image disabledNodeIcon;
|
---|
34 |
|
---|
35 | private boolean enabledNodeIconIsTemporary;
|
---|
36 | private boolean disabledNodeIconIsTemporary;
|
---|
37 |
|
---|
38 | public enum SymbolShape { SQUARE, CIRCLE, TRIANGLE, PENTAGON, HEXAGON, HEPTAGON, OCTAGON, NONAGON, DECAGON }
|
---|
39 |
|
---|
40 | public static class Symbol {
|
---|
41 | public SymbolShape symbol;
|
---|
42 | public int size;
|
---|
43 | public Stroke stroke;
|
---|
44 | public Color strokeColor;
|
---|
45 | public Color fillColor;
|
---|
46 |
|
---|
47 | public Symbol(SymbolShape symbol, int size, Stroke stroke, Color strokeColor, Color fillColor) {
|
---|
48 | if (stroke != null && strokeColor == null)
|
---|
49 | throw new IllegalArgumentException("Stroke given without color");
|
---|
50 | if (stroke == null && fillColor == null)
|
---|
51 | throw new IllegalArgumentException("Either a stroke or a fill color must be given");
|
---|
52 | this.symbol = symbol;
|
---|
53 | this.size = size;
|
---|
54 | this.stroke = stroke;
|
---|
55 | this.strokeColor = strokeColor;
|
---|
56 | this.fillColor = fillColor;
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public boolean equals(Object obj) {
|
---|
61 | if (obj == null || getClass() != obj.getClass())
|
---|
62 | return false;
|
---|
63 | final Symbol other = (Symbol) obj;
|
---|
64 | return symbol == other.symbol &&
|
---|
65 | size == other.size &&
|
---|
66 | Objects.equals(stroke, other.stroke) &&
|
---|
67 | Objects.equals(strokeColor, other.strokeColor) &&
|
---|
68 | Objects.equals(fillColor, other.fillColor);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public int hashCode() {
|
---|
73 | int hash = 7;
|
---|
74 | hash = 67 * hash + symbol.hashCode();
|
---|
75 | hash = 67 * hash + size;
|
---|
76 | hash = 67 * hash + (stroke != null ? stroke.hashCode() : 0);
|
---|
77 | hash = 67 * hash + (strokeColor != null ? strokeColor.hashCode() : 0);
|
---|
78 | hash = 67 * hash + (fillColor != null ? fillColor.hashCode() : 0);
|
---|
79 | return hash;
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public String toString() {
|
---|
84 | return "symbol=" + symbol + " size=" + size +
|
---|
85 | (stroke != null ? (" stroke=" + stroke + " strokeColor=" + strokeColor) : "") +
|
---|
86 | (fillColor != null ? (" fillColor=" + fillColor) : "");
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE;
|
---|
91 | public static final BoxProvider SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER;
|
---|
92 | static {
|
---|
93 | MultiCascade mc = new MultiCascade();
|
---|
94 | mc.getOrCreateCascade("default");
|
---|
95 | SIMPLE_NODE_ELEMSTYLE = create(new Environment(null, mc, "default", null), 4.1f, true);
|
---|
96 | if (SIMPLE_NODE_ELEMSTYLE == null) throw new AssertionError();
|
---|
97 | SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER = SIMPLE_NODE_ELEMSTYLE.getBoxProvider();
|
---|
98 | }
|
---|
99 |
|
---|
100 | public static final StyleList DEFAULT_NODE_STYLELIST = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE);
|
---|
101 | public static final StyleList DEFAULT_NODE_STYLELIST_TEXT = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE, BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE);
|
---|
102 |
|
---|
103 | protected NodeElemStyle(Cascade c, MapImage mapImage, Symbol symbol, float default_major_z_index, RotationAngle rotationAngle) {
|
---|
104 | super(c, default_major_z_index);
|
---|
105 | this.mapImage = mapImage;
|
---|
106 | this.symbol = symbol;
|
---|
107 | this.mapImageAngle = rotationAngle;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public static NodeElemStyle create(Environment env) {
|
---|
111 | return create(env, 4f, false);
|
---|
112 | }
|
---|
113 |
|
---|
114 | private static NodeElemStyle create(Environment env, float default_major_z_index, boolean allowDefault) {
|
---|
115 | Cascade c = env.mc.getCascade(env.layer);
|
---|
116 |
|
---|
117 | MapImage mapImage = createIcon(env, ICON_KEYS);
|
---|
118 | Symbol symbol = null;
|
---|
119 | if (mapImage == null) {
|
---|
120 | symbol = createSymbol(env);
|
---|
121 | }
|
---|
122 | final String rotationString = c.get("icon-rotation", null, String.class);
|
---|
123 | RotationAngle rotationAngle = null;
|
---|
124 | if ("way".equalsIgnoreCase(rotationString)) {
|
---|
125 | rotationAngle = RotationAngle.buildWayDirectionRotation();
|
---|
126 | } else if (rotationString != null) {
|
---|
127 | try {
|
---|
128 | rotationAngle = RotationAngle.buildStaticRotation(rotationString);
|
---|
129 | } catch (RuntimeException ignore) {
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | // optimization: if we neither have a symbol, nor a mapImage
|
---|
134 | // we don't have to check for the remaining style properties and we don't
|
---|
135 | // have to allocate a node element style.
|
---|
136 | if (!allowDefault && symbol == null && mapImage == null) return null;
|
---|
137 |
|
---|
138 | return new NodeElemStyle(c, mapImage, symbol, default_major_z_index, rotationAngle);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public static MapImage createIcon(final Environment env, final String[] keys) {
|
---|
142 | Cascade c = env.mc.getCascade(env.layer);
|
---|
143 |
|
---|
144 | final IconReference iconRef = c.get(keys[ICON_IMAGE_IDX], null, IconReference.class, true);
|
---|
145 | if (iconRef == null)
|
---|
146 | return null;
|
---|
147 |
|
---|
148 | Cascade c_def = env.mc.getCascade("default");
|
---|
149 |
|
---|
150 | Float widthOnDefault = c_def.get(keys[ICON_WIDTH_IDX], null, Float.class);
|
---|
151 | if (widthOnDefault != null && widthOnDefault <= 0) {
|
---|
152 | widthOnDefault = null;
|
---|
153 | }
|
---|
154 | Float widthF = getWidth(c, keys[ICON_WIDTH_IDX], widthOnDefault);
|
---|
155 |
|
---|
156 | Float heightOnDefault = c_def.get(keys[ICON_HEIGHT_IDX], null, Float.class);
|
---|
157 | if (heightOnDefault != null && heightOnDefault <= 0) {
|
---|
158 | heightOnDefault = null;
|
---|
159 | }
|
---|
160 | Float heightF = getWidth(c, keys[ICON_HEIGHT_IDX], heightOnDefault);
|
---|
161 |
|
---|
162 | int width = widthF == null ? -1 : Math.round(widthF);
|
---|
163 | int height = heightF == null ? -1 : Math.round(heightF);
|
---|
164 |
|
---|
165 | float offsetXF = 0f;
|
---|
166 | float offsetYF = 0f;
|
---|
167 | if (keys[ICON_OFFSET_X_IDX] != null) {
|
---|
168 | offsetXF = c.get(keys[ICON_OFFSET_X_IDX], 0f, Float.class);
|
---|
169 | offsetYF = c.get(keys[ICON_OFFSET_Y_IDX], 0f, Float.class);
|
---|
170 | }
|
---|
171 |
|
---|
172 | final MapImage mapImage = new MapImage(iconRef.iconName, iconRef.source);
|
---|
173 |
|
---|
174 | mapImage.width = width;
|
---|
175 | mapImage.height = height;
|
---|
176 | mapImage.offsetX = Math.round(offsetXF);
|
---|
177 | mapImage.offsetY = Math.round(offsetYF);
|
---|
178 |
|
---|
179 | mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
|
---|
180 | Integer pAlpha = Utils.color_float2int(c.get(keys[ICON_OPACITY_IDX], null, float.class));
|
---|
181 | if (pAlpha != null) {
|
---|
182 | mapImage.alpha = pAlpha;
|
---|
183 | }
|
---|
184 | return mapImage;
|
---|
185 | }
|
---|
186 |
|
---|
187 | private static Symbol createSymbol(Environment env) {
|
---|
188 | Cascade c = env.mc.getCascade(env.layer);
|
---|
189 | Cascade c_def = env.mc.getCascade("default");
|
---|
190 |
|
---|
191 | SymbolShape shape;
|
---|
192 | Keyword shapeKW = c.get("symbol-shape", null, Keyword.class);
|
---|
193 | if (shapeKW == null)
|
---|
194 | return null;
|
---|
195 | if ("square".equals(shapeKW.val)) {
|
---|
196 | shape = SymbolShape.SQUARE;
|
---|
197 | } else if ("circle".equals(shapeKW.val)) {
|
---|
198 | shape = SymbolShape.CIRCLE;
|
---|
199 | } else if ("triangle".equals(shapeKW.val)) {
|
---|
200 | shape = SymbolShape.TRIANGLE;
|
---|
201 | } else if ("pentagon".equals(shapeKW.val)) {
|
---|
202 | shape = SymbolShape.PENTAGON;
|
---|
203 | } else if ("hexagon".equals(shapeKW.val)) {
|
---|
204 | shape = SymbolShape.HEXAGON;
|
---|
205 | } else if ("heptagon".equals(shapeKW.val)) {
|
---|
206 | shape = SymbolShape.HEPTAGON;
|
---|
207 | } else if ("octagon".equals(shapeKW.val)) {
|
---|
208 | shape = SymbolShape.OCTAGON;
|
---|
209 | } else if ("nonagon".equals(shapeKW.val)) {
|
---|
210 | shape = SymbolShape.NONAGON;
|
---|
211 | } else if ("decagon".equals(shapeKW.val)) {
|
---|
212 | shape = SymbolShape.DECAGON;
|
---|
213 | } else
|
---|
214 | return null;
|
---|
215 |
|
---|
216 | Float sizeOnDefault = c_def.get("symbol-size", null, Float.class);
|
---|
217 | if (sizeOnDefault != null && sizeOnDefault <= 0) {
|
---|
218 | sizeOnDefault = null;
|
---|
219 | }
|
---|
220 | Float size = getWidth(c, "symbol-size", sizeOnDefault);
|
---|
221 |
|
---|
222 | if (size == null) {
|
---|
223 | size = 10f;
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (size <= 0)
|
---|
227 | return null;
|
---|
228 |
|
---|
229 | Float strokeWidthOnDefault = getWidth(c_def, "symbol-stroke-width", null);
|
---|
230 | Float strokeWidth = getWidth(c, "symbol-stroke-width", strokeWidthOnDefault);
|
---|
231 |
|
---|
232 | Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
|
---|
233 |
|
---|
234 | if (strokeWidth == null && strokeColor != null) {
|
---|
235 | strokeWidth = 1f;
|
---|
236 | } else if (strokeWidth != null && strokeColor == null) {
|
---|
237 | strokeColor = Color.ORANGE;
|
---|
238 | }
|
---|
239 |
|
---|
240 | Stroke stroke = null;
|
---|
241 | if (strokeColor != null) {
|
---|
242 | Integer strokeAlpha = Utils.color_float2int(c.get("symbol-stroke-opacity", null, Float.class));
|
---|
243 | if (strokeAlpha != null) {
|
---|
244 | strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
|
---|
245 | strokeColor.getBlue(), strokeAlpha);
|
---|
246 | }
|
---|
247 | stroke = new BasicStroke(strokeWidth);
|
---|
248 | }
|
---|
249 |
|
---|
250 | Color fillColor = c.get("symbol-fill-color", null, Color.class);
|
---|
251 | if (stroke == null && fillColor == null) {
|
---|
252 | fillColor = Color.BLUE;
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (fillColor != null) {
|
---|
256 | Integer fillAlpha = Utils.color_float2int(c.get("symbol-fill-opacity", null, Float.class));
|
---|
257 | if (fillAlpha != null) {
|
---|
258 | fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
|
---|
259 | fillColor.getBlue(), fillAlpha);
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | return new Symbol(shape, Math.round(size), stroke, strokeColor, fillColor);
|
---|
264 | }
|
---|
265 |
|
---|
266 | @Override
|
---|
267 | public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings settings, StyledMapRenderer painter,
|
---|
268 | boolean selected, boolean outermember, boolean member) {
|
---|
269 | if (primitive instanceof Node) {
|
---|
270 | Node n = (Node) primitive;
|
---|
271 | if (mapImage != null && painter.isShowIcons()) {
|
---|
272 | painter.drawNodeIcon(n, mapImage, painter.isInactiveMode() || n.isDisabled(), selected, member,
|
---|
273 | mapImageAngle == null ? 0.0 : mapImageAngle.getRotationAngle(primitive));
|
---|
274 | } else if (symbol != null) {
|
---|
275 | Color fillColor = symbol.fillColor;
|
---|
276 | if (fillColor != null) {
|
---|
277 | if (painter.isInactiveMode() || n.isDisabled()) {
|
---|
278 | fillColor = settings.getInactiveColor();
|
---|
279 | } else if (selected) {
|
---|
280 | fillColor = settings.getSelectedColor(fillColor.getAlpha());
|
---|
281 | } else if (member) {
|
---|
282 | fillColor = settings.getRelationSelectedColor(fillColor.getAlpha());
|
---|
283 | }
|
---|
284 | }
|
---|
285 | Color strokeColor = symbol.strokeColor;
|
---|
286 | if (strokeColor != null) {
|
---|
287 | if (painter.isInactiveMode() || n.isDisabled()) {
|
---|
288 | strokeColor = settings.getInactiveColor();
|
---|
289 | } else if (selected) {
|
---|
290 | strokeColor = settings.getSelectedColor(strokeColor.getAlpha());
|
---|
291 | } else if (member) {
|
---|
292 | strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha());
|
---|
293 | }
|
---|
294 | }
|
---|
295 | painter.drawNodeSymbol(n, symbol, fillColor, strokeColor);
|
---|
296 | } else {
|
---|
297 | Color color;
|
---|
298 | boolean isConnection = n.isConnectionNode();
|
---|
299 |
|
---|
300 | if (painter.isInactiveMode() || n.isDisabled()) {
|
---|
301 | color = settings.getInactiveColor();
|
---|
302 | } else if (selected) {
|
---|
303 | color = settings.getSelectedColor();
|
---|
304 | } else if (member) {
|
---|
305 | color = settings.getRelationSelectedColor();
|
---|
306 | } else if (isConnection) {
|
---|
307 | if (n.isTagged()) {
|
---|
308 | color = settings.getTaggedConnectionColor();
|
---|
309 | } else {
|
---|
310 | color = settings.getConnectionColor();
|
---|
311 | }
|
---|
312 | } else {
|
---|
313 | if (n.isTagged()) {
|
---|
314 | color = settings.getTaggedColor();
|
---|
315 | } else {
|
---|
316 | color = settings.getNodeColor();
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | final int size = Utils.max((selected ? settings.getSelectedNodeSize() : 0),
|
---|
321 | (n.isTagged() ? settings.getTaggedNodeSize() : 0),
|
---|
322 | (isConnection ? settings.getConnectionNodeSize() : 0),
|
---|
323 | settings.getUnselectedNodeSize());
|
---|
324 |
|
---|
325 | final boolean fill = (selected && settings.isFillSelectedNode()) ||
|
---|
326 | (n.isTagged() && settings.isFillTaggedNode()) ||
|
---|
327 | (isConnection && settings.isFillConnectionNode()) ||
|
---|
328 | settings.isFillUnselectedNode();
|
---|
329 |
|
---|
330 | painter.drawNode(n, color, size, fill);
|
---|
331 |
|
---|
332 | }
|
---|
333 | } else if (primitive instanceof Relation && mapImage != null) {
|
---|
334 | painter.drawRestriction((Relation) primitive, mapImage, painter.isInactiveMode() || primitive.isDisabled());
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | public BoxProvider getBoxProvider() {
|
---|
339 | if (mapImage != null)
|
---|
340 | return mapImage.getBoxProvider();
|
---|
341 | else if (symbol != null)
|
---|
342 | return new SimpleBoxProvider(new Rectangle(-symbol.size/2, -symbol.size/2, symbol.size, symbol.size));
|
---|
343 | else {
|
---|
344 | // This is only executed once, so no performance concerns.
|
---|
345 | // However, it would be better, if the settings could be changed at runtime.
|
---|
346 | int size = Utils.max(
|
---|
347 | Main.pref.getInteger("mappaint.node.selected-size", 5),
|
---|
348 | Main.pref.getInteger("mappaint.node.unselected-size", 3),
|
---|
349 | Main.pref.getInteger("mappaint.node.connection-size", 5),
|
---|
350 | Main.pref.getInteger("mappaint.node.tagged-size", 3)
|
---|
351 | );
|
---|
352 | return new SimpleBoxProvider(new Rectangle(-size/2, -size/2, size, size));
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | @Override
|
---|
357 | public int hashCode() {
|
---|
358 | int hash = super.hashCode();
|
---|
359 | hash = 17 * hash + (mapImage != null ? mapImage.hashCode() : 0);
|
---|
360 | hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
|
---|
361 | hash = 17 * hash + (mapImageAngle != null ? mapImageAngle.hashCode() : 0);
|
---|
362 | return hash;
|
---|
363 | }
|
---|
364 |
|
---|
365 | @Override
|
---|
366 | public boolean equals(Object obj) {
|
---|
367 | if (obj == null || getClass() != obj.getClass())
|
---|
368 | return false;
|
---|
369 | if (!super.equals(obj))
|
---|
370 | return false;
|
---|
371 |
|
---|
372 | final NodeElemStyle other = (NodeElemStyle) obj;
|
---|
373 | // we should get the same image object due to caching
|
---|
374 | if (!Objects.equals(mapImage, other.mapImage))
|
---|
375 | return false;
|
---|
376 | if (!Objects.equals(symbol, other.symbol))
|
---|
377 | return false;
|
---|
378 | if (!Objects.equals(mapImageAngle, other.mapImageAngle))
|
---|
379 | return false;
|
---|
380 | return true;
|
---|
381 | }
|
---|
382 |
|
---|
383 | @Override
|
---|
384 | public String toString() {
|
---|
385 | StringBuilder s = new StringBuilder("NodeElemStyle{");
|
---|
386 | s.append(super.toString());
|
---|
387 | if (mapImage != null) {
|
---|
388 | s.append(" icon=[" + mapImage + "]");
|
---|
389 | }
|
---|
390 | if (symbol != null) {
|
---|
391 | s.append(" symbol=[" + symbol + "]");
|
---|
392 | }
|
---|
393 | if (mapImageAngle != null) {
|
---|
394 | s.append(" mapImageAngle=[" + mapImageAngle + "]");
|
---|
395 | }
|
---|
396 | s.append('}');
|
---|
397 | return s.toString();
|
---|
398 | }
|
---|
399 | }
|
---|