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