1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.styleelement;
|
---|
3 |
|
---|
4 | import java.util.Objects;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
7 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
8 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
9 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
10 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
11 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
12 | import org.openstreetmap.josm.gui.mappaint.styleelement.placement.PartiallyInsideAreaStrategy;
|
---|
13 | import org.openstreetmap.josm.gui.mappaint.styleelement.placement.PositionForAreaStrategy;
|
---|
14 | import org.openstreetmap.josm.gui.util.RotationAngle;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This class defines how an icon is rendered onto the area.
|
---|
18 | * @author Michael Zangl
|
---|
19 | * @since 11730
|
---|
20 | */
|
---|
21 | public class AreaIconElement extends StyleElement {
|
---|
22 | /**
|
---|
23 | * The icon that is displayed on the center of the area.
|
---|
24 | */
|
---|
25 | private final MapImage iconImage;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * The rotation of the {@link #iconImageAngle}
|
---|
29 | */
|
---|
30 | private final RotationAngle iconImageAngle;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * The position of the icon inside the area.
|
---|
34 | */
|
---|
35 | private final PositionForAreaStrategy iconPosition;
|
---|
36 |
|
---|
37 | protected AreaIconElement(Cascade c, MapImage iconImage, RotationAngle iconImageAngle, PositionForAreaStrategy iconPosition) {
|
---|
38 | super(c, 4.8f);
|
---|
39 | this.iconImage = Objects.requireNonNull(iconImage, "iconImage");
|
---|
40 | this.iconImageAngle = Objects.requireNonNull(iconImageAngle, "iconImageAngle");
|
---|
41 | this.iconPosition = Objects.requireNonNull(iconPosition, "iconPosition");
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
46 | boolean selected, boolean outermember, boolean member) {
|
---|
47 | if (painter.isShowIcons()) {
|
---|
48 | painter.drawAreaIcon(osm, iconImage, painter.isInactiveMode() || osm.isDisabled(), selected, member,
|
---|
49 | iconImageAngle.getRotationAngle(osm), iconPosition);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Create a new {@link AreaIconElement}
|
---|
55 | * @param env The current style definitions
|
---|
56 | * @return The area element or <code>null</code> if there is no icon.
|
---|
57 | */
|
---|
58 | public static AreaIconElement create(final Environment env) {
|
---|
59 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
60 | MapImage iconImage = NodeElement.createIcon(env);
|
---|
61 | if (iconImage != null) {
|
---|
62 | RotationAngle rotationAngle = NodeElement.createRotationAngle(env);
|
---|
63 | Keyword positionKeyword = c.get(AreaElement.ICON_POSITION, null, Keyword.class);
|
---|
64 | PositionForAreaStrategy position = PositionForAreaStrategy.forKeyword(positionKeyword, PartiallyInsideAreaStrategy.INSTANCE);
|
---|
65 |
|
---|
66 | return new AreaIconElement(c, iconImage, rotationAngle, position);
|
---|
67 | } else {
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public int hashCode() {
|
---|
74 | final int prime = 31;
|
---|
75 | int result = super.hashCode();
|
---|
76 | result = prime * result + ((iconImage == null) ? 0 : iconImage.hashCode());
|
---|
77 | result = prime * result + ((iconImageAngle == null) ? 0 : iconImageAngle.hashCode());
|
---|
78 | result = prime * result + ((iconPosition == null) ? 0 : iconPosition.hashCode());
|
---|
79 | return result;
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public boolean equals(Object obj) {
|
---|
84 | if (this == obj) {
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 | if (!super.equals(obj)) {
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 | if (getClass() != obj.getClass()) {
|
---|
91 | return false;
|
---|
92 | }
|
---|
93 | AreaIconElement other = (AreaIconElement) obj;
|
---|
94 | return Objects.equals(iconImage, other.iconImage) &&
|
---|
95 | Objects.equals(iconImageAngle, other.iconImageAngle) &&
|
---|
96 | Objects.equals(iconPosition, other.iconPosition);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public String toString() {
|
---|
101 | return "AreaIconElement{" + super.toString() + "iconImage=[" + iconImage + "] iconImageAngle=[" + iconImageAngle + "]}";
|
---|
102 | }
|
---|
103 | }
|
---|