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, 089 boolean selected, boolean outermember, boolean member) { 090 Color myColor = color; 091 if (osm instanceof Way) { 092 if (color != null) { 093 if (selected) { 094 myColor = paintSettings.getSelectedColor(color.getAlpha()); 095 } else if (outermember) { 096 myColor = paintSettings.getRelationSelectedColor(color.getAlpha()); 097 } 098 } 099 painter.drawArea((Way) osm, myColor, fillImage, text); 100 } else if (osm instanceof Relation) { 101 if (color != null && (selected || outermember)) { 102 myColor = paintSettings.getRelationSelectedColor(color.getAlpha()); 103 } 104 painter.drawArea((Relation) osm, myColor, fillImage, text); 105 } 106 } 107 108 @Override 109 public boolean equals(Object obj) { 110 if (obj == null || getClass() != obj.getClass()) 111 return false; 112 if (!super.equals(obj)) 113 return false; 114 AreaElemStyle other = (AreaElemStyle) obj; 115 // we should get the same image object due to caching 116 if (!Objects.equals(fillImage, other.fillImage)) 117 return false; 118 if (!Objects.equals(color, other.color)) 119 return false; 120 if (!Objects.equals(text, other.text)) 121 return false; 122 return true; 123 } 124 125 @Override 126 public int hashCode() { 127 int hash = 3; 128 hash = 61 * hash + color.hashCode(); 129 hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0); 130 hash = 61 * hash + (text != null ? text.hashCode() : 0); 131 return hash; 132 } 133 134 @Override 135 public String toString() { 136 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) + 137 " fillImage=[" + fillImage + "]}"; 138 } 139}