001 // License: GPL. For details, see LICENSE file. 002 package org.openstreetmap.josm.gui.mappaint; 003 004 import static org.openstreetmap.josm.tools.Utils.equal; 005 006 import java.awt.Color; 007 008 import org.openstreetmap.josm.Main; 009 import org.openstreetmap.josm.data.osm.OsmPrimitive; 010 import org.openstreetmap.josm.data.osm.Relation; 011 import org.openstreetmap.josm.data.osm.Way; 012 import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings; 013 import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter; 014 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors; 015 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 016 import org.openstreetmap.josm.tools.CheckParameterUtil; 017 import org.openstreetmap.josm.tools.Utils; 018 019 public class AreaElemStyle extends ElemStyle 020 { 021 /** 022 * If fillImage == null, color is the fill-color, otherwise 023 * an arbitrary color value sampled from the fillImage 024 */ 025 public Color color; 026 public MapImage fillImage; 027 public TextElement text; 028 029 protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement text) { 030 super(c, 1f); 031 CheckParameterUtil.ensureParameterNotNull(color); 032 this.color = color; 033 this.fillImage = fillImage; 034 this.text = text; 035 } 036 037 public static AreaElemStyle create(Cascade c) { 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 = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50)))); 059 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class)); 060 if (pAlpha != null) { 061 alpha = pAlpha; 062 } 063 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); 064 } 065 } 066 067 TextElement text = null; 068 Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class); 069 if (textPos == null || Utils.equal(textPos.val, "center")) { 070 text = TextElement.create(c, PaintColors.AREA_TEXT.get(), true); 071 } 072 073 if (color != null) 074 return new AreaElemStyle(c, color, fillImage, text); 075 else 076 return null; 077 } 078 079 @Override 080 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) { 081 if (osm instanceof Way) 082 { 083 Color myColor = color; 084 if (color != null) { 085 if (osm.isSelected()) { 086 myColor = paintSettings.getSelectedColor(color.getAlpha()); 087 } 088 } 089 painter.drawArea((Way) osm, myColor, fillImage, text); 090 } else if (osm instanceof Relation) 091 { 092 Color myColor = color; 093 if (color != null) { 094 if (selected) { 095 myColor = paintSettings.getRelationSelectedColor(color.getAlpha()); 096 } 097 } 098 painter.drawArea((Relation) osm, myColor, fillImage, text); 099 } 100 } 101 102 @Override 103 public boolean equals(Object obj) { 104 if (obj == null || getClass() != obj.getClass()) 105 return false; 106 if (!super.equals(obj)) 107 return false; 108 AreaElemStyle other = (AreaElemStyle) obj; 109 // we should get the same image object due to caching 110 if (!equal(fillImage, other.fillImage)) 111 return false; 112 if (!equal(color, other.color)) 113 return false; 114 if (!equal(text, other.text)) 115 return false; 116 return true; 117 } 118 119 @Override 120 public int hashCode() { 121 int hash = 3; 122 hash = 61 * hash + color.hashCode(); 123 hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0); 124 hash = 61 * hash + (text != null ? text.hashCode() : 0); 125 return hash; 126 } 127 128 @Override 129 public String toString() { 130 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) + 131 " fillImage=[" + fillImage + "]}"; 132 } 133 }