001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint;
003
004import java.awt.Color;
005import java.util.Objects;
006
007import org.openstreetmap.josm.Main;
008import org.openstreetmap.josm.data.osm.OsmPrimitive;
009import org.openstreetmap.josm.data.osm.Relation;
010import org.openstreetmap.josm.data.osm.Way;
011import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
012import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
013import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
014import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
015import org.openstreetmap.josm.tools.CheckParameterUtil;
016import org.openstreetmap.josm.tools.Utils;
017
018public class AreaElemStyle extends ElemStyle {
019
020    /**
021     * If fillImage == null, color is the fill-color, otherwise
022     * an arbitrary color value sampled from the fillImage
023     */
024    public Color color;
025    public MapImage fillImage;
026    public TextElement text;
027
028    protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement text) {
029        super(c, 1f);
030        CheckParameterUtil.ensureParameterNotNull(color);
031        this.color = color;
032        this.fillImage = fillImage;
033        this.text = text;
034    }
035
036    public static AreaElemStyle create(final Environment env) {
037        final Cascade c = env.mc.getCascade(env.layer);
038        MapImage fillImage = null;
039        Color color = null;
040
041        IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
042        if (iconRef != null) {
043            fillImage = new MapImage(iconRef.iconName, iconRef.source);
044
045            color = new Color(fillImage.getImage(false).getRGB(
046                    fillImage.getWidth() / 2, fillImage.getHeight() / 2)
047            );
048
049            fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
050            Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
051            if (pAlpha != null) {
052                fillImage.alpha = pAlpha;
053            }
054        } else {
055            color = c.get(FILL_COLOR, null, Color.class);
056            if (color != null) {
057                int alpha = color.getAlpha();
058                if (alpha == 255) {
059                    // Assume alpha value has not been specified by the user if
060                    // is set to fully opaque. Use default value in this case.
061                    // It is not an ideal solution, but a little tricky to get this
062                    // right, especially as named map colors can be changed in
063                    // the preference GUI and written to the preferences file.
064                    alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
065                }
066                Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
067                if (pAlpha != null) {
068                    alpha = pAlpha;
069                }
070                color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
071            }
072        }
073
074        TextElement text = null;
075        Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
076        if (textPos == null || "center".equals(textPos.val)) {
077            text = TextElement.create(env, PaintColors.AREA_TEXT.get(), true);
078        }
079
080        if (color != null)
081            return new AreaElemStyle(c, color, fillImage, text);
082        else
083            return null;
084    }
085
086    @Override
087    public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
088            boolean selected, boolean outermember, boolean member) {
089        Color myColor = color;
090        if (osm instanceof Way) {
091            if (color != null) {
092                if (selected) {
093                    myColor = paintSettings.getSelectedColor(color.getAlpha());
094                } else if (outermember) {
095                    myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
096                }
097            }
098            painter.drawArea((Way) osm, myColor, fillImage, painter.isInactiveMode() || osm.isDisabled(), text);
099        } else if (osm instanceof Relation) {
100            if (color != null && (selected || outermember)) {
101                myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
102            }
103            painter.drawArea((Relation) osm, myColor, fillImage, painter.isInactiveMode() || osm.isDisabled(), text);
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 (!Objects.equals(fillImage, other.fillImage))
116            return false;
117        if (!Objects.equals(color, other.color))
118            return false;
119        if (!Objects.equals(text, other.text))
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 + (text != null ? text.hashCode() : 0);
130        return hash;
131    }
132
133    @Override
134    public String toString() {
135        return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
136                " fillImage=[" + fillImage + "]}";
137    }
138}