1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.image.BufferedImage;
|
---|
6 |
|
---|
7 | import javax.swing.ImageIcon;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
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.gui.mappaint.MapPaintStyles.IconReference;
|
---|
16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
18 |
|
---|
19 | public class AreaElemStyle extends ElemStyle
|
---|
20 | {
|
---|
21 | /**
|
---|
22 | * If fillImage == null, color is the fill-color, otherwise
|
---|
23 | * an arbitrary color value sampled from the fillImage
|
---|
24 | */
|
---|
25 | public Color color;
|
---|
26 | public BufferedImage fillImage;
|
---|
27 | public float fillImageAlpha;
|
---|
28 |
|
---|
29 | protected AreaElemStyle(Cascade c, Color color, BufferedImage fillImage, float fillImageAlpha) {
|
---|
30 | super(c);
|
---|
31 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
32 | this.color = color;
|
---|
33 | this.fillImage = fillImage;
|
---|
34 | this.fillImageAlpha = fillImageAlpha;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static AreaElemStyle create(Cascade c) {
|
---|
38 | BufferedImage fillImage = null;
|
---|
39 | Color color = null;
|
---|
40 | float fillImageAlpha = 1f;
|
---|
41 |
|
---|
42 | IconReference iconRef = c.get("fill-image", null, IconReference.class);
|
---|
43 | if (iconRef != null) {
|
---|
44 | ImageIcon icon = MapPaintStyles.getIcon(iconRef, false);
|
---|
45 | if (icon != null) {
|
---|
46 | if (!(icon.getImage() instanceof BufferedImage)) {
|
---|
47 | icon = MapPaintStyles.getIcon(iconRef, true);
|
---|
48 | }
|
---|
49 | if (!(icon.getImage() instanceof BufferedImage))
|
---|
50 | throw new RuntimeException();
|
---|
51 | fillImage = (BufferedImage) icon.getImage();
|
---|
52 |
|
---|
53 | color = new Color(fillImage.getRGB(fillImage.getWidth() / 2, fillImage.getHeight() / 2));
|
---|
54 |
|
---|
55 | fillImageAlpha = Utils.color_int2float(Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255)))));
|
---|
56 | Float pAlpha = c.get("fill-opacity", null, Float.class);
|
---|
57 | if (pAlpha != null) {
|
---|
58 | if (pAlpha < 0f || pAlpha > 1f) {
|
---|
59 | pAlpha= 1f;
|
---|
60 | }
|
---|
61 | fillImageAlpha = pAlpha;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | } else {
|
---|
65 | color = c.get("fill-color", null, Color.class);
|
---|
66 | if (color != null) {
|
---|
67 | int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
|
---|
68 | Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
|
---|
69 | if (pAlpha != null) {
|
---|
70 | alpha = pAlpha;
|
---|
71 | }
|
---|
72 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (color != null)
|
---|
77 | return new AreaElemStyle(c, color, fillImage, fillImageAlpha);
|
---|
78 | else
|
---|
79 | return null;
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
|
---|
84 | if (osm instanceof Way)
|
---|
85 | {
|
---|
86 | Color myColor = color;
|
---|
87 | if (color != null) {
|
---|
88 | if (osm.isSelected()) {
|
---|
89 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
90 | }
|
---|
91 | }
|
---|
92 | painter.drawArea((Way) osm, myColor, fillImage, fillImageAlpha,
|
---|
93 | painter.isShowNames() ? painter.getAreaName(osm) : null);
|
---|
94 | } else if (osm instanceof Relation)
|
---|
95 | {
|
---|
96 | Color myColor = color;
|
---|
97 | if (color != null) {
|
---|
98 | if (selected) {
|
---|
99 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
100 | }
|
---|
101 | }
|
---|
102 | painter.drawArea((Relation) osm, myColor, fillImage, fillImageAlpha,
|
---|
103 | painter.getAreaName(osm));
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public boolean equals(Object obj) {
|
---|
109 | if (obj == null || getClass() != obj.getClass())
|
---|
110 | return false;
|
---|
111 | if (!super.equals(obj))
|
---|
112 | return false;
|
---|
113 | AreaElemStyle other = (AreaElemStyle) obj;
|
---|
114 | // we should get the same image object due to caching
|
---|
115 | if (fillImage != other.fillImage)
|
---|
116 | return false;
|
---|
117 | if (!Utils.equal(color, other.color))
|
---|
118 | return false;
|
---|
119 | if (fillImageAlpha != other.fillImageAlpha)
|
---|
120 | return false;
|
---|
121 | return true;
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public int hashCode() {
|
---|
126 | int hash = 3;
|
---|
127 | hash = 61 * hash + color.hashCode();
|
---|
128 | hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
|
---|
129 | hash = 61 * hash + Float.floatToIntBits(fillImageAlpha);
|
---|
130 | return hash;
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public String toString() {
|
---|
135 | return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
|
---|
136 | " fillImageAlpha=" + fillImageAlpha + " fillImage=[" + fillImage + "]}";
|
---|
137 | }
|
---|
138 | }
|
---|