source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java@ 5054

Last change on this file since 5054 was 5054, checked in by bastiK, 13 years ago

see #6797: load map images in background, in case they are loaded over a network. Show temporary image in the meantime.

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