Changeset 12476 in josm for trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java
- Timestamp:
- 2017-07-13T23:02:13+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java
r12381 r12476 4 4 import java.awt.Color; 5 5 import java.awt.Font; 6 import java.awt.geom.Point2D; 6 7 import java.util.Objects; 7 8 … … 15 16 import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.StaticLabelCompositionStrategy; 16 17 import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.TagLookupCompositionStrategy; 17 import org.openstreetmap.josm.gui.mappaint.styleelement.placement.PositionForAreaStrategy;18 18 import org.openstreetmap.josm.tools.CheckParameterUtil; 19 19 import org.openstreetmap.josm.tools.Utils; … … 39 39 public Font font; 40 40 /** 41 * The x offset of the text.42 */43 public int xOffset;44 /**45 * The y offset of the text.46 */47 public int yOffset;48 /**49 41 * The color to draw the text in, includes alpha. 50 42 */ … … 58 50 */ 59 51 public Color haloColor; 60 61 /**62 * The position strategy for this text label.63 */64 private final PositionForAreaStrategy labelPositionStrategy;65 52 66 53 /** … … 70 57 * If null, no label is rendered. 71 58 * @param font the font to be used. Must not be null. 72 * @param xOffset x offset73 * @param yOffset y offset74 59 * @param color the color to be used. Must not be null 75 60 * @param haloRadius halo radius 76 61 * @param haloColor halo color 77 * @param labelPositionStrategy The position in the area. 78 */ 79 protected TextLabel(LabelCompositionStrategy strategy, Font font, int xOffset, int yOffset, Color color, Float haloRadius, 80 Color haloColor, PositionForAreaStrategy labelPositionStrategy) { 62 */ 63 protected TextLabel(LabelCompositionStrategy strategy, Font font, Color color, Float haloRadius, 64 Color haloColor) { 81 65 this.labelCompositionStrategy = strategy; 82 66 this.font = Objects.requireNonNull(font, "font"); 83 this.xOffset = xOffset;84 this.yOffset = yOffset;85 67 this.color = Objects.requireNonNull(color, "color"); 86 68 this.haloRadius = haloRadius; 87 69 this.haloColor = haloColor; 88 this.labelPositionStrategy = Objects.requireNonNull(labelPositionStrategy, "labelPositionStrategy");89 70 } 90 71 … … 97 78 this.labelCompositionStrategy = other.labelCompositionStrategy; 98 79 this.font = other.font; 99 this.xOffset = other.xOffset;100 this.yOffset = other.yOffset;101 80 this.color = other.color; 102 81 this.haloColor = other.haloColor; 103 82 this.haloRadius = other.haloRadius; 104 this.labelPositionStrategy = other.labelPositionStrategy;105 }106 107 /**108 * Copy constructor that changes the position strategy.109 *110 * @param other the other element.111 * @param labelPositionStrategy the position112 */113 private TextLabel(TextLabel other, PositionForAreaStrategy labelPositionStrategy) {114 this.labelCompositionStrategy = other.labelCompositionStrategy;115 this.font = other.font;116 this.xOffset = other.xOffset;117 this.yOffset = other.yOffset;118 this.color = other.color;119 this.haloColor = other.haloColor;120 this.haloRadius = other.haloRadius;121 this.labelPositionStrategy = labelPositionStrategy;122 83 } 123 84 … … 176 137 Font font = StyleElement.getFont(c, s); 177 138 139 Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class); 140 float alpha = c.get(TEXT_OPACITY, 1f, Float.class); 141 color = Utils.alphaMultiply(color, alpha); 142 143 Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class); 144 if (haloRadius != null && haloRadius <= 0) { 145 haloRadius = null; 146 } 147 Color haloColor = null; 148 if (haloRadius != null) { 149 haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class); 150 float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class); 151 haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor); 152 } 153 154 return new TextLabel(strategy, font, color, haloRadius, haloColor); 155 } 156 157 /** 158 * Gets the text-offset property from a cascade 159 * @param c The cascade 160 * @return The text offset property 161 */ 162 public static Point2D getTextOffset(Cascade c) { 178 163 float xOffset = 0; 179 164 float yOffset = 0; … … 189 174 xOffset = c.get(TEXT_OFFSET_X, xOffset, Float.class); 190 175 yOffset = c.get(TEXT_OFFSET_Y, yOffset, Float.class); 191 192 Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class); 193 float alpha = c.get(TEXT_OPACITY, 1f, Float.class); 194 color = Utils.alphaMultiply(color, alpha); 195 196 Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class); 197 if (haloRadius != null && haloRadius <= 0) { 198 haloRadius = null; 199 } 200 Color haloColor = null; 201 if (haloRadius != null) { 202 haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class); 203 float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class); 204 haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor); 205 } 206 207 Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class); 208 PositionForAreaStrategy position = PositionForAreaStrategy.forKeyword(positionKeyword); 209 210 return new TextLabel(strategy, font, (int) xOffset, -(int) yOffset, color, haloRadius, haloColor, position); 176 return new Point2D.Double(xOffset, yOffset); 211 177 } 212 178 … … 223 189 } 224 190 225 /**226 * Gets the strategy that defines where to place the label.227 * @return The strategy. Never null.228 * @since 11722229 */230 public PositionForAreaStrategy getLabelPositionStrategy() {231 return labelPositionStrategy;232 }233 234 /**235 * Creates a new text label with a different position strategy236 * @param labelPositionStrategy The new position strategy to use237 * @return The new label238 */239 public TextLabel withPosition(PositionForAreaStrategy labelPositionStrategy) {240 return new TextLabel(this, labelPositionStrategy);241 }242 243 191 @Override 244 192 public String toString() { … … 249 197 StringBuilder sb = new StringBuilder(96); 250 198 sb.append("labelCompositionStrategy=").append(labelCompositionStrategy) 251 .append(" font=").append(font); 252 if (xOffset != 0) { 253 sb.append(" xOffset=").append(xOffset); 254 } 255 if (yOffset != 0) { 256 sb.append(" yOffset=").append(yOffset); 257 } 258 sb.append(" color=").append(Utils.toString(color)); 199 .append(" font=").append(font) 200 .append(" color=").append(Utils.toString(color)); 259 201 if (haloRadius != null) { 260 202 sb.append(" haloRadius=").append(haloRadius) … … 266 208 @Override 267 209 public int hashCode() { 268 return Objects.hash(labelCompositionStrategy, font, xOffset, yOffset,color, haloRadius, haloColor);210 return Objects.hash(labelCompositionStrategy, font, color, haloRadius, haloColor); 269 211 } 270 212 … … 274 216 if (obj == null || getClass() != obj.getClass()) return false; 275 217 TextLabel textLabel = (TextLabel) obj; 276 return xOffset == textLabel.xOffset && 277 yOffset == textLabel.yOffset && 278 Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) && 218 return Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) && 279 219 Objects.equals(font, textLabel.font) && 280 220 Objects.equals(color, textLabel.color) &&
Note:
See TracChangeset
for help on using the changeset viewer.