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 fillImage.getImage(); 045 046 color = new Color(fillImage.getImage().getRGB( 047 fillImage.getWidth() / 2, fillImage.getHeight() / 2) 048 ); 049 050 fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255)))); 051 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class)); 052 if (pAlpha != null) { 053 fillImage.alpha = pAlpha; 054 } 055 } else { 056 color = c.get(FILL_COLOR, null, Color.class); 057 if (color != null) { 058 int alpha = color.getAlpha(); 059 if (alpha == 255) { 060 // Assume alpha value has not been specified by the user if 061 // is set to fully opaque. Use default value in this case. 062 // It is not an ideal solution, but a little tricky to get this 063 // right, especially as named map colors can be changed in 064 // the preference GUI and written to the preferences file. 065 alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50)))); 066 } 067 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class)); 068 if (pAlpha != null) { 069 alpha = pAlpha; 070 } 071 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); 072 } 073 } 074 075 TextElement text = null; 076 Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class); 077 if (textPos == null || "center".equals(textPos.val)) { 078 text = TextElement.create(env, PaintColors.AREA_TEXT.get(), true); 079 } 080 081 if (color != null) 082 return new AreaElemStyle(c, color, fillImage, text); 083 else 084 return null; 085 } 086 087 @Override 088 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter, boolean selected, boolean member) { 089 if (osm instanceof Way) { 090 Color myColor = color; 091 if (color != null && osm.isSelected()) { 092 myColor = paintSettings.getSelectedColor(color.getAlpha()); 093 } 094 painter.drawArea((Way) osm, myColor, fillImage, text); 095 } else if (osm instanceof Relation) { 096 Color myColor = color; 097 if (color != null && selected) { 098 myColor = paintSettings.getRelationSelectedColor(color.getAlpha()); 099 } 100 painter.drawArea((Relation) osm, myColor, fillImage, text); 101 } 102 } 103 104 @Override 105 public boolean equals(Object obj) { 106 if (obj == null || getClass() != obj.getClass()) 107 return false; 108 if (!super.equals(obj)) 109 return false; 110 AreaElemStyle other = (AreaElemStyle) obj; 111 // we should get the same image object due to caching 112 if (!Objects.equals(fillImage, other.fillImage)) 113 return false; 114 if (!Objects.equals(color, other.color)) 115 return false; 116 if (!Objects.equals(text, other.text)) 117 return false; 118 return true; 119 } 120 121 @Override 122 public int hashCode() { 123 int hash = 3; 124 hash = 61 * hash + color.hashCode(); 125 hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0); 126 hash = 61 * hash + (text != null ? text.hashCode() : 0); 127 return hash; 128 } 129 130 @Override 131 public String toString() { 132 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) + 133 " fillImage=[" + fillImage + "]}"; 134 } 135}