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

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

applied #6035 (patch by jcollie) - Add extra polygon shapes to MapCSS painter

  • Property svn:eol-style set to native
File size: 16.0 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.Font;
9import java.awt.Stroke;
10
11import javax.swing.GrayFilter;
12import javax.swing.ImageIcon;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
19import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
20import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
21import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
22import org.openstreetmap.josm.tools.CheckParameterUtil;
23import org.openstreetmap.josm.tools.Utils;
24
25/**
26 * applies for Nodes and turn restriction relations
27 */
28public class NodeElemStyle extends ElemStyle {
29
30 public ImageIcon icon;
31 public int iconAlpha;
32 public Symbol symbol;
33 public NodeTextElement text;
34
35 private ImageIcon disabledIcon;
36
37 public enum SymbolShape { SQUARE, CIRCLE, TRIANGLE, PENTAGON, HEXAGON, HEPTAGON, OCTAGON, NONAGON, DECAGON }
38 public enum HorizontalTextAlignment { LEFT, CENTER, RIGHT }
39 public enum VerticalTextAlignment { ABOVE, TOP, CENTER, BOTTOM, BELOW }
40
41 public static class Symbol {
42 public SymbolShape symbol;
43 public int size;
44 public Stroke stroke;
45 public Color strokeColor;
46 public Color fillColor;
47
48 public Symbol(SymbolShape symbol, int size, Stroke stroke, Color strokeColor, Color fillColor) {
49 if (stroke != null && strokeColor == null)
50 throw new IllegalArgumentException();
51 if (stroke == null && fillColor == null)
52 throw new IllegalArgumentException();
53 this.symbol = symbol;
54 this.size = size;
55 this.stroke = stroke;
56 this.strokeColor = strokeColor;
57 this.fillColor = fillColor;
58 }
59
60 @Override
61 public boolean equals(Object obj) {
62 if (obj == null || getClass() != obj.getClass())
63 return false;
64 final Symbol other = (Symbol) obj;
65 return symbol == other.symbol &&
66 size == other.size &&
67 equal(stroke, other.stroke) &&
68 equal(strokeColor, other.strokeColor) &&
69 equal(fillColor, other.fillColor);
70 }
71
72 @Override
73 public int hashCode() {
74 int hash = 7;
75 hash = 67 * hash + symbol.hashCode();
76 hash = 67 * hash + size;
77 hash = 67 * hash + (stroke != null ? stroke.hashCode() : 0);
78 hash = 67 * hash + (strokeColor != null ? strokeColor.hashCode() : 0);
79 hash = 67 * hash + (fillColor != null ? fillColor.hashCode() : 0);
80 return hash;
81 }
82
83 @Override
84 public String toString() {
85 return "symbol=" + symbol + " size=" + size +
86 (stroke != null ? (" stroke=" + stroke + " strokeColor=" + strokeColor) : "") +
87 (fillColor != null ? (" fillColor=" + fillColor) : "");
88 }
89 }
90
91 public static class NodeTextElement extends TextElement {
92 public HorizontalTextAlignment hAlign;
93 public VerticalTextAlignment vAlign;
94
95 public NodeTextElement(String textKey, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign, Font font, int xOffset, int yOffset, Color color) {
96 super(textKey, font, xOffset, yOffset, color);
97 CheckParameterUtil.ensureParameterNotNull(hAlign);
98 CheckParameterUtil.ensureParameterNotNull(vAlign);
99 this.hAlign = hAlign;
100 this.vAlign = vAlign;
101 }
102
103 @Override
104 public boolean equals(Object obj) {
105 if (!super.equals(obj))
106 return false;
107 if (obj == null || getClass() != obj.getClass())
108 return false;
109 final NodeTextElement other = (NodeTextElement) obj;
110 return hAlign == other.hAlign &&
111 vAlign == other.vAlign;
112 }
113
114 @Override
115 public int hashCode() {
116 int hash = super.hashCode();
117 hash = 97 * hash + hAlign.hashCode();
118 hash = 97 * hash + vAlign.hashCode();
119 return hash;
120 }
121
122 }
123
124 public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE;
125 static {
126 MultiCascade mc = new MultiCascade();
127 Cascade c = mc.getOrCreateCascade("default");
128 c.put("text", "auto");
129 SIMPLE_NODE_ELEMSTYLE = create(new Environment(null, mc, "default", null), true);
130 }
131
132 protected NodeElemStyle(Cascade c, ImageIcon icon, int iconAlpha, Symbol symbol, NodeTextElement text) {
133 super(c);
134 this.icon = icon;
135 this.iconAlpha = iconAlpha;
136 this.symbol = symbol;
137 this.text = text;
138 }
139
140 public static NodeElemStyle create(Environment env) {
141 return create(env, false);
142 }
143
144 private static NodeElemStyle create(Environment env, boolean allowOnlyText) {
145 Cascade c = env.mc.getCascade(env.layer);
146
147 IconReference iconRef = c.get("icon-image", null, IconReference.class);
148 ImageIcon icon = null;
149 int iconAlpha = 0;
150 Symbol symbol = null;
151
152 if (iconRef != null) {
153 icon = MapPaintStyles.getIcon(iconRef, false);
154 if (icon == null) {
155 icon = MapPaintStyles.getNoIcon_Icon(iconRef.source, false);
156 }
157 iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
158 Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
159 if (pAlpha != null) {
160 iconAlpha = pAlpha;
161 }
162 } else {
163 symbol = createSymbol(env);
164 }
165
166 if (icon == null && symbol == null && !allowOnlyText)
167 return null;
168
169 NodeTextElement text = null;
170 TextElement te = TextElement.create(c, PaintColors.TEXT.get());
171 if (te != null) {
172 HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT;
173 Keyword hAlignKW = c.get("text-anchor-horizontal", Keyword.RIGHT, Keyword.class);
174 if (equal(hAlignKW.val, "left")) {
175 hAlign = HorizontalTextAlignment.LEFT;
176 } else if (equal(hAlignKW.val, "center")) {
177 hAlign = HorizontalTextAlignment.CENTER;
178 } else if (equal(hAlignKW.val, "right")) {
179 hAlign = HorizontalTextAlignment.RIGHT;
180 }
181 VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM;
182 String vAlignStr = c.get("text-anchor-vertical", Keyword.BOTTOM, Keyword.class).val;
183 if (equal(vAlignStr, "above")) {
184 vAlign = VerticalTextAlignment.ABOVE;
185 } else if (equal(vAlignStr, "top")) {
186 vAlign = VerticalTextAlignment.TOP;
187 } else if (equal(vAlignStr, "center")) {
188 vAlign = VerticalTextAlignment.CENTER;
189 } else if (equal(vAlignStr, "bottom")) {
190 vAlign = VerticalTextAlignment.BOTTOM;
191 } else if (equal(vAlignStr, "below")) {
192 vAlign = VerticalTextAlignment.BELOW;
193 }
194 text = new NodeTextElement(te.textKey, hAlign, vAlign, te.font, te.xOffset, te.yOffset, te.color);
195 }
196
197 return new NodeElemStyle(c, icon, iconAlpha, symbol, text);
198 }
199
200 private static Symbol createSymbol(Environment env) {
201 Cascade c = env.mc.getCascade(env.layer);
202 Cascade c_def = env.mc.getCascade("default");
203
204 SymbolShape shape;
205 Keyword shapeKW = c.get("symbol-shape", null, Keyword.class);
206 if (shapeKW == null)
207 return null;
208 if (equal(shapeKW.val, "square")) {
209 shape = SymbolShape.SQUARE;
210 } else if (equal(shapeKW.val, "circle")) {
211 shape = SymbolShape.CIRCLE;
212 } else if (equal(shapeKW.val, "triangle")) {
213 shape = SymbolShape.TRIANGLE;
214 } else if (equal(shapeKW.val, "pentagon")) {
215 shape = SymbolShape.PENTAGON;
216 } else if (equal(shapeKW.val, "hexagon")) {
217 shape = SymbolShape.HEXAGON;
218 } else if (equal(shapeKW.val, "heptagon")) {
219 shape = SymbolShape.HEPTAGON;
220 } else if (equal(shapeKW.val, "octagon")) {
221 shape = SymbolShape.OCTAGON;
222 } else if (equal(shapeKW.val, "nonagon")) {
223 shape = SymbolShape.NONAGON;
224 } else if (equal(shapeKW.val, "decagon")) {
225 shape = SymbolShape.DECAGON;
226 } else
227 return null;
228
229 Float sizeOnDefault = c_def.get("symbol-size", null, Float.class);
230 if (sizeOnDefault != null && sizeOnDefault <= 0) {
231 sizeOnDefault = null;
232 }
233 Float size = getWidth(c, "symbol-size", sizeOnDefault);
234
235 if (size == null) {
236 size = 10f;
237 }
238
239 if (size <= 0)
240 return null;
241
242 Float strokeWidthOnDefault = getWidth(c_def, "symbol-stroke-width", null);
243 Float strokeWidth = getWidth(c, "symbol-stroke-width", strokeWidthOnDefault);
244
245 Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
246
247 if (strokeWidth == null && strokeColor != null) {
248 strokeWidth = 1f;
249 } else if (strokeWidth != null && strokeColor == null) {
250 strokeColor = Color.ORANGE;
251 }
252
253 Stroke stroke = null;
254 if (strokeColor != null) {
255 float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class);
256 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
257 strokeColor.getBlue(), Utils.color_float2int(strokeAlpha));
258 stroke = new BasicStroke(strokeWidth);
259 }
260
261 Color fillColor = c.get("symbol-fill-color", null, Color.class);
262 if (stroke == null && fillColor == null)
263 fillColor = Color.BLUE;
264
265 if (fillColor != null) {
266 float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class);
267 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
268 fillColor.getBlue(), Utils.color_float2int(fillAlpha));
269 }
270
271 return new Symbol(shape, Math.round(size), stroke, strokeColor, fillColor);
272 }
273
274 @Override
275 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings settings, MapPainter painter, boolean selected, boolean member) {
276 if (primitive instanceof Node) {
277 Node n = (Node) primitive;
278 if (icon != null && painter.isShowIcons()) {
279 painter.drawNodeIcon(n, (painter.isInactive() || n.isDisabled()) ? getDisabledIcon() : icon,
280 Utils.color_int2float(iconAlpha), selected, member, text);
281 } else if (symbol != null) {
282 Color fillColor = symbol.fillColor;
283 if (fillColor != null) {
284 if (n.isHighlighted()) {
285 fillColor = settings.getHighlightColor();
286 } else {
287 if (painter.isInactive() || n.isDisabled()) {
288 fillColor = settings.getInactiveColor();
289 } else if (selected) {
290 fillColor = settings.getSelectedColor(fillColor.getAlpha());
291 } else if (member) {
292 fillColor = settings.getRelationSelectedColor(fillColor.getAlpha());
293 }
294 }
295 }
296 Color strokeColor = symbol.strokeColor;
297 if (strokeColor != null) {
298 if (n.isHighlighted()) {
299 strokeColor = settings.getHighlightColor();
300 } else {
301 if (painter.isInactive() || n.isDisabled()) {
302 strokeColor = settings.getInactiveColor();
303 } else if (selected) {
304 strokeColor = settings.getSelectedColor(strokeColor.getAlpha());
305 } else if (member) {
306 strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha());
307 }
308 }
309 }
310 painter.drawNodeSymbol(n, symbol, fillColor, strokeColor, text);
311 } else {
312 if (n.isHighlighted()) {
313 painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), text);
314 } else {
315 Color color;
316 boolean isConnection = n.isConnectionNode();
317
318 if (painter.isInactive() || n.isDisabled()) {
319 color = settings.getInactiveColor();
320 } else if (selected) {
321 color = settings.getSelectedColor();
322 } else if (member) {
323 color = settings.getRelationSelectedColor();
324 } else if (isConnection) {
325 if (n.isTagged()) {
326 color = settings.getTaggedConnectionColor();
327 } else {
328 color = settings.getConnectionColor();
329 }
330 } else {
331 if (n.isTagged()) {
332 color = settings.getTaggedColor();
333 } else {
334 color = settings.getNodeColor();
335 }
336 }
337
338 final int size = Utils.max((selected ? settings.getSelectedNodeSize() : 0),
339 (n.isTagged() ? settings.getTaggedNodeSize() : 0),
340 (isConnection ? settings.getConnectionNodeSize() : 0),
341 settings.getUnselectedNodeSize());
342
343 final boolean fill = (selected && settings.isFillSelectedNode()) ||
344 (n.isTagged() && settings.isFillTaggedNode()) ||
345 (isConnection && settings.isFillConnectionNode()) ||
346 settings.isFillUnselectedNode();
347
348 painter.drawNode(n, color, size, fill, text);
349 }
350 }
351 } else if (primitive instanceof Relation && icon != null) {
352 painter.drawRestriction((Relation) primitive, this);
353 }
354 }
355
356 public ImageIcon getDisabledIcon() {
357 if (disabledIcon != null)
358 return disabledIcon;
359 if (icon == null)
360 return null;
361 return disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
362 }
363
364 @Override
365 public int hashCode() {
366 int hash = super.hashCode();
367 hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0);
368 hash = 17 * hash + iconAlpha;
369 hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
370 hash = 17 * hash + (text != null ? text.hashCode() : 0);
371 return hash;
372 }
373
374 @Override
375 public boolean equals(Object obj) {
376 if (obj == null || getClass() != obj.getClass())
377 return false;
378 if (!super.equals(obj))
379 return false;
380
381 final NodeElemStyle other = (NodeElemStyle) obj;
382 // we should get the same image object due to caching
383 if (icon != other.icon && (icon == null || other.icon == null || icon.getImage() != other.icon.getImage()))
384 return false;
385 if (this.iconAlpha != other.iconAlpha)
386 return false;
387 if (!equal(symbol, other.symbol))
388 return false;
389 if (!equal(text, other.text))
390 return false;
391 return true;
392 }
393
394
395 @Override
396 public String toString() {
397 return "NodeElemStyle{" + super.toString() +
398 (icon != null ? ("icon=" + icon + " iconAlpha=" + iconAlpha) : "") +
399 (symbol != null ? (" symbol=[" + symbol + "]") : "") + '}';
400 }
401
402}
Note: See TracBrowser for help on using the repository browser.