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 | TOP(.5),
|
---|
25 | CENTER(0),
|
---|
26 | BOTTOM(-.5);
|
---|
27 |
|
---|
28 | private final double alignmentOffset;
|
---|
29 |
|
---|
30 | LineImageAlignment(double alignmentOffset) {
|
---|
31 | this.alignmentOffset = alignmentOffset;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Gets the alignment offset.
|
---|
36 | * @return The offset relative to the image height compared to placing the image in the middle of the line.
|
---|
37 | */
|
---|
38 | public double getAlignmentOffset() {
|
---|
39 | return alignmentOffset;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public MapImage pattern;
|
---|
44 | public float offset;
|
---|
45 | public float spacing;
|
---|
46 | public float phase;
|
---|
47 | public LineImageAlignment align;
|
---|
48 |
|
---|
49 | private static final String[] REPEAT_IMAGE_KEYS = {REPEAT_IMAGE, REPEAT_IMAGE_WIDTH, REPEAT_IMAGE_HEIGHT, REPEAT_IMAGE_OPACITY,
|
---|
50 | null, null};
|
---|
51 |
|
---|
52 | public RepeatImageElement(Cascade c, MapImage pattern, float offset, float spacing, float phase, LineImageAlignment align) {
|
---|
53 | super(c, 2.9f);
|
---|
54 | CheckParameterUtil.ensureParameterNotNull(pattern);
|
---|
55 | CheckParameterUtil.ensureParameterNotNull(align);
|
---|
56 | this.pattern = pattern;
|
---|
57 | this.offset = offset;
|
---|
58 | this.spacing = spacing;
|
---|
59 | this.phase = phase;
|
---|
60 | this.align = align;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static RepeatImageElement create(Environment env) {
|
---|
64 | MapImage pattern = NodeElement.createIcon(env, REPEAT_IMAGE_KEYS);
|
---|
65 | if (pattern == null)
|
---|
66 | return null;
|
---|
67 | Cascade c = env.mc.getCascade(env.layer);
|
---|
68 | float offset = c.get(REPEAT_IMAGE_OFFSET, 0f, Float.class);
|
---|
69 | float spacing = c.get(REPEAT_IMAGE_SPACING, 0f, Float.class);
|
---|
70 | float phase = -c.get(REPEAT_IMAGE_PHASE, 0f, Float.class);
|
---|
71 |
|
---|
72 | LineImageAlignment align = LineImageAlignment.CENTER;
|
---|
73 | Keyword alignKW = c.get(REPEAT_IMAGE_ALIGN, Keyword.CENTER, Keyword.class);
|
---|
74 | if ("top".equals(alignKW.val)) {
|
---|
75 | align = LineImageAlignment.TOP;
|
---|
76 | } else if ("bottom".equals(alignKW.val)) {
|
---|
77 | align = LineImageAlignment.BOTTOM;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return new RepeatImageElement(c, pattern, offset, spacing, phase, align);
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
85 | boolean selected, boolean outermember, boolean member) {
|
---|
86 | if (primitive instanceof Way) {
|
---|
87 | Way w = (Way) primitive;
|
---|
88 | painter.drawRepeatImage(w, pattern, painter.isInactiveMode() || w.isDisabled(), offset, spacing, phase, align);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public boolean isProperLineStyle() {
|
---|
94 | return true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public boolean equals(Object obj) {
|
---|
99 | if (this == obj) return true;
|
---|
100 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
101 | if (!super.equals(obj)) return false;
|
---|
102 | RepeatImageElement that = (RepeatImageElement) obj;
|
---|
103 | return align == that.align &&
|
---|
104 | Float.compare(that.offset, offset) == 0 &&
|
---|
105 | Float.compare(that.spacing, spacing) == 0 &&
|
---|
106 | Float.compare(that.phase, phase) == 0 &&
|
---|
107 | Objects.equals(pattern, that.pattern);
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public int hashCode() {
|
---|
112 | return Objects.hash(super.hashCode(), pattern, offset, spacing, phase, align);
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public String toString() {
|
---|
117 | return "RepeatImageStyle{" + super.toString() + "pattern=[" + pattern +
|
---|
118 | "], offset=" + offset + ", spacing=" + spacing +
|
---|
119 | ", phase=" + (-phase) + ", align=" + align + '}';
|
---|
120 | }
|
---|
121 | }
|
---|