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.Way;
|
---|
8 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
9 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
10 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
11 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
12 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
13 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Style element that displays a repeated image pattern along a way.
|
---|
17 | */
|
---|
18 | public class RepeatImageElement extends StyleElement {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * The side on which the image should be aligned to the line.
|
---|
22 | */
|
---|
23 | public enum LineImageAlignment {
|
---|
24 | /**
|
---|
25 | * Align it to the top side of the line
|
---|
26 | */
|
---|
27 | TOP(.5),
|
---|
28 | /**
|
---|
29 | * Align it to the center of the line
|
---|
30 | */
|
---|
31 | CENTER(0),
|
---|
32 | /**
|
---|
33 | * Align it to the bottom of the line
|
---|
34 | */
|
---|
35 | BOTTOM(-.5);
|
---|
36 |
|
---|
37 | private final double alignmentOffset;
|
---|
38 |
|
---|
39 | LineImageAlignment(double alignmentOffset) {
|
---|
40 | this.alignmentOffset = alignmentOffset;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Gets the alignment offset.
|
---|
45 | * @return The offset relative to the image height compared to placing the image in the middle of the line.
|
---|
46 | */
|
---|
47 | public double getAlignmentOffset() {
|
---|
48 | return alignmentOffset;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * The image to draw on the line repeatedly
|
---|
54 | */
|
---|
55 | public MapImage pattern;
|
---|
56 | /**
|
---|
57 | * The offset to the side of the way
|
---|
58 | */
|
---|
59 | public float offset;
|
---|
60 | /**
|
---|
61 | * The space between the images
|
---|
62 | */
|
---|
63 | public float spacing;
|
---|
64 | /**
|
---|
65 | * The offset of the first image along the way
|
---|
66 | */
|
---|
67 | public float phase;
|
---|
68 | /**
|
---|
69 | * The alignment of the image
|
---|
70 | */
|
---|
71 | public LineImageAlignment align;
|
---|
72 |
|
---|
73 | private static final String[] REPEAT_IMAGE_KEYS = {REPEAT_IMAGE, REPEAT_IMAGE_WIDTH, REPEAT_IMAGE_HEIGHT, REPEAT_IMAGE_OPACITY,
|
---|
74 | null, null};
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Create a new image element
|
---|
78 | * @param c The cascade
|
---|
79 | * @param pattern The image to draw on the line repeatedly
|
---|
80 | * @param offset The offset to the side of the way
|
---|
81 | * @param spacing The space between the images
|
---|
82 | * @param phase The offset of the first image along the way
|
---|
83 | * @param align The alignment of the image
|
---|
84 | */
|
---|
85 | public RepeatImageElement(Cascade c, MapImage pattern, float offset, float spacing, float phase, LineImageAlignment align) {
|
---|
86 | super(c, 2.9f);
|
---|
87 | CheckParameterUtil.ensureParameterNotNull(pattern);
|
---|
88 | CheckParameterUtil.ensureParameterNotNull(align);
|
---|
89 | this.pattern = pattern;
|
---|
90 | this.offset = offset;
|
---|
91 | this.spacing = spacing;
|
---|
92 | this.phase = phase;
|
---|
93 | this.align = align;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Create a RepeatImageElement from the given environment
|
---|
98 | * @param env The environment
|
---|
99 | * @return The image style element or <code>null</code> if none should be painted
|
---|
100 | */
|
---|
101 | public static RepeatImageElement create(Environment env) {
|
---|
102 | MapImage pattern = NodeElement.createIcon(env, REPEAT_IMAGE_KEYS);
|
---|
103 | if (pattern == null)
|
---|
104 | return null;
|
---|
105 | Cascade c = env.mc.getCascade(env.layer);
|
---|
106 | float offset = c.get(REPEAT_IMAGE_OFFSET, 0f, Float.class);
|
---|
107 | float spacing = c.get(REPEAT_IMAGE_SPACING, 0f, Float.class);
|
---|
108 | float phase = -c.get(REPEAT_IMAGE_PHASE, 0f, Float.class);
|
---|
109 |
|
---|
110 | LineImageAlignment align = LineImageAlignment.CENTER;
|
---|
111 | Keyword alignKW = c.get(REPEAT_IMAGE_ALIGN, Keyword.CENTER, Keyword.class);
|
---|
112 | if ("top".equals(alignKW.val)) {
|
---|
113 | align = LineImageAlignment.TOP;
|
---|
114 | } else if ("bottom".equals(alignKW.val)) {
|
---|
115 | align = LineImageAlignment.BOTTOM;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return new RepeatImageElement(c, pattern, offset, spacing, phase, align);
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
123 | boolean selected, boolean outermember, boolean member) {
|
---|
124 | if (primitive instanceof Way) {
|
---|
125 | Way w = (Way) primitive;
|
---|
126 | painter.drawRepeatImage(w, pattern, painter.isInactiveMode() || w.isDisabled(), offset, spacing, phase, align);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public boolean isProperLineStyle() {
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public boolean equals(Object obj) {
|
---|
137 | if (this == obj) return true;
|
---|
138 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
139 | if (!super.equals(obj)) return false;
|
---|
140 | RepeatImageElement that = (RepeatImageElement) obj;
|
---|
141 | return align == that.align &&
|
---|
142 | Float.compare(that.offset, offset) == 0 &&
|
---|
143 | Float.compare(that.spacing, spacing) == 0 &&
|
---|
144 | Float.compare(that.phase, phase) == 0 &&
|
---|
145 | Objects.equals(pattern, that.pattern);
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public int hashCode() {
|
---|
150 | return Objects.hash(super.hashCode(), pattern, offset, spacing, phase, align);
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | public String toString() {
|
---|
155 | return "RepeatImageStyle{" + super.toString() + "pattern=[" + pattern +
|
---|
156 | "], offset=" + offset + ", spacing=" + spacing +
|
---|
157 | ", phase=" + (-phase) + ", align=" + align + '}';
|
---|
158 | }
|
---|
159 | }
|
---|