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 | public class RepeatImageElement extends StyleElement {
|
---|
16 |
|
---|
17 | public enum LineImageAlignment { TOP, CENTER, BOTTOM }
|
---|
18 |
|
---|
19 | public MapImage pattern;
|
---|
20 | public float offset;
|
---|
21 | public float spacing;
|
---|
22 | public float phase;
|
---|
23 | public LineImageAlignment align;
|
---|
24 |
|
---|
25 | private static final String[] REPEAT_IMAGE_KEYS = {REPEAT_IMAGE, REPEAT_IMAGE_WIDTH, REPEAT_IMAGE_HEIGHT, REPEAT_IMAGE_OPACITY,
|
---|
26 | null, null};
|
---|
27 |
|
---|
28 | public RepeatImageElement(Cascade c, MapImage pattern, float offset, float spacing, float phase, LineImageAlignment align) {
|
---|
29 | super(c, 2.9f);
|
---|
30 | CheckParameterUtil.ensureParameterNotNull(pattern);
|
---|
31 | CheckParameterUtil.ensureParameterNotNull(align);
|
---|
32 | this.pattern = pattern;
|
---|
33 | this.offset = offset;
|
---|
34 | this.spacing = spacing;
|
---|
35 | this.phase = phase;
|
---|
36 | this.align = align;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public static RepeatImageElement create(Environment env) {
|
---|
40 | MapImage pattern = NodeElement.createIcon(env, REPEAT_IMAGE_KEYS);
|
---|
41 | if (pattern == null)
|
---|
42 | return null;
|
---|
43 | Cascade c = env.mc.getCascade(env.layer);
|
---|
44 | float offset = c.get(REPEAT_IMAGE_OFFSET, 0f, Float.class);
|
---|
45 | float spacing = c.get(REPEAT_IMAGE_SPACING, 0f, Float.class);
|
---|
46 | float phase = -c.get(REPEAT_IMAGE_PHASE, 0f, Float.class);
|
---|
47 |
|
---|
48 | LineImageAlignment align = LineImageAlignment.CENTER;
|
---|
49 | Keyword alignKW = c.get(REPEAT_IMAGE_ALIGN, Keyword.CENTER, Keyword.class);
|
---|
50 | if ("top".equals(alignKW.val)) {
|
---|
51 | align = LineImageAlignment.TOP;
|
---|
52 | } else if ("bottom".equals(alignKW.val)) {
|
---|
53 | align = LineImageAlignment.BOTTOM;
|
---|
54 | }
|
---|
55 |
|
---|
56 | return new RepeatImageElement(c, pattern, offset, spacing, phase, align);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
61 | boolean selected, boolean outermember, boolean member) {
|
---|
62 | Way w = (Way) primitive;
|
---|
63 | painter.drawRepeatImage(w, pattern, painter.isInactiveMode() || w.isDisabled(), offset, spacing, phase, align);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public boolean isProperLineStyle() {
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public boolean equals(Object obj) {
|
---|
73 | if (this == obj) return true;
|
---|
74 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
75 | if (!super.equals(obj)) return false;
|
---|
76 | RepeatImageElement that = (RepeatImageElement) obj;
|
---|
77 | return Float.compare(that.offset, offset) == 0 &&
|
---|
78 | Float.compare(that.spacing, spacing) == 0 &&
|
---|
79 | Float.compare(that.phase, phase) == 0 &&
|
---|
80 | Objects.equals(pattern, that.pattern) &&
|
---|
81 | align == that.align;
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public int hashCode() {
|
---|
86 | return Objects.hash(super.hashCode(), pattern, offset, spacing, phase, align);
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public String toString() {
|
---|
91 | return "RepeatImageStyle{" + super.toString() + "pattern=[" + pattern +
|
---|
92 | "], offset=" + offset + ", spacing=" + spacing +
|
---|
93 | ", phase=" + (-phase) + ", align=" + align + '}';
|
---|
94 | }
|
---|
95 | }
|
---|