1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.styleelement;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Rectangle;
|
---|
6 | import java.awt.geom.Point2D;
|
---|
7 | import java.util.Objects;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.INode;
|
---|
10 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.Node;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
14 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Text style attached to a style with a bounding box, like an icon or a symbol.
|
---|
23 | */
|
---|
24 | public class BoxTextElement extends StyleElement {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * MapCSS text-anchor-horizontal
|
---|
28 | */
|
---|
29 | public enum HorizontalTextAlignment {
|
---|
30 | /**
|
---|
31 | * Align to the left
|
---|
32 | */
|
---|
33 | LEFT,
|
---|
34 | /**
|
---|
35 | * Align in the center
|
---|
36 | */
|
---|
37 | CENTER,
|
---|
38 | /**
|
---|
39 | * Align to the right
|
---|
40 | */
|
---|
41 | RIGHT
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * MapCSS text-anchor-vertical
|
---|
46 | */
|
---|
47 | public enum VerticalTextAlignment {
|
---|
48 | /**
|
---|
49 | * Render above the box
|
---|
50 | */
|
---|
51 | ABOVE,
|
---|
52 | /**
|
---|
53 | * Align to the top of the box
|
---|
54 | */
|
---|
55 | TOP,
|
---|
56 | /**
|
---|
57 | * Render at the center of the box
|
---|
58 | */
|
---|
59 | CENTER,
|
---|
60 | /**
|
---|
61 | * Align to the bottom of the box
|
---|
62 | */
|
---|
63 | BOTTOM,
|
---|
64 | /**
|
---|
65 | * Render below the box
|
---|
66 | */
|
---|
67 | BELOW
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Something that provides us with a {@link BoxProviderResult}
|
---|
72 | * @since 10600 (functional interface)
|
---|
73 | */
|
---|
74 | @FunctionalInterface
|
---|
75 | public interface BoxProvider {
|
---|
76 | /**
|
---|
77 | * Compute and get the {@link BoxProviderResult}. The temporary flag is set if the result of the computation may change in the future.
|
---|
78 | * @return The result of the computation.
|
---|
79 | */
|
---|
80 | BoxProviderResult get();
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * A box rectangle with a flag if it is temporary.
|
---|
85 | */
|
---|
86 | public static class BoxProviderResult {
|
---|
87 | private final Rectangle box;
|
---|
88 | private final boolean temporary;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Create a new box provider result
|
---|
92 | * @param box The box
|
---|
93 | * @param temporary The temporary flag, will be returned by {@link #isTemporary()}
|
---|
94 | */
|
---|
95 | public BoxProviderResult(Rectangle box, boolean temporary) {
|
---|
96 | this.box = box;
|
---|
97 | this.temporary = temporary;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns the box.
|
---|
102 | * @return the box
|
---|
103 | */
|
---|
104 | public Rectangle getBox() {
|
---|
105 | return box;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Determines if the box can change in future calls of the {@link BoxProvider#get()} method
|
---|
110 | * @return {@code true} if the box can change in future calls of the {@code BoxProvider#get()} method
|
---|
111 | */
|
---|
112 | public boolean isTemporary() {
|
---|
113 | return temporary;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * A {@link BoxProvider} that always returns the same non-temporary rectangle
|
---|
119 | */
|
---|
120 | public static class SimpleBoxProvider implements BoxProvider {
|
---|
121 | private final Rectangle box;
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Constructs a new {@code SimpleBoxProvider}.
|
---|
125 | * @param box the box
|
---|
126 | */
|
---|
127 | public SimpleBoxProvider(Rectangle box) {
|
---|
128 | this.box = box;
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | public BoxProviderResult get() {
|
---|
133 | return new BoxProviderResult(box, false);
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public int hashCode() {
|
---|
138 | return Objects.hash(box);
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public boolean equals(Object obj) {
|
---|
143 | if (this == obj) return true;
|
---|
144 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
145 | SimpleBoxProvider that = (SimpleBoxProvider) obj;
|
---|
146 | return Objects.equals(box, that.box);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * The default style a simple node should use for it's text
|
---|
152 | */
|
---|
153 | public static final BoxTextElement SIMPLE_NODE_TEXT_ELEMSTYLE;
|
---|
154 | static {
|
---|
155 | MultiCascade mc = new MultiCascade();
|
---|
156 | Cascade c = mc.getOrCreateCascade("default");
|
---|
157 | c.put(TEXT, Keyword.AUTO);
|
---|
158 | Node n = new Node();
|
---|
159 | n.put("name", "dummy");
|
---|
160 | SIMPLE_NODE_TEXT_ELEMSTYLE = create(new Environment(n, mc, "default", null), NodeElement.SIMPLE_NODE_ELEMSTYLE.getBoxProvider());
|
---|
161 | if (SIMPLE_NODE_TEXT_ELEMSTYLE == null) throw new AssertionError();
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Caches the default text color from the preferences.
|
---|
166 | *
|
---|
167 | * FIXME: the cache isn't updated if the user changes the preference during a JOSM
|
---|
168 | * session. There should be preference listener updating this cache.
|
---|
169 | */
|
---|
170 | private static volatile Color defaultTextColorCache;
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * The text this element should display.
|
---|
174 | */
|
---|
175 | public TextLabel text;
|
---|
176 | /**
|
---|
177 | * The x offset of the text.
|
---|
178 | */
|
---|
179 | public int xOffset;
|
---|
180 | /**
|
---|
181 | * The y offset of the text. In screen space (inverted to user space)
|
---|
182 | */
|
---|
183 | public int yOffset;
|
---|
184 | /**
|
---|
185 | * The {@link HorizontalTextAlignment} for this text.
|
---|
186 | */
|
---|
187 | public HorizontalTextAlignment hAlign;
|
---|
188 | /**
|
---|
189 | * The {@link VerticalTextAlignment} for this text.
|
---|
190 | */
|
---|
191 | public VerticalTextAlignment vAlign;
|
---|
192 | protected BoxProvider boxProvider;
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Create a new {@link BoxTextElement}
|
---|
196 | * @param c The current cascade
|
---|
197 | * @param text The text to display
|
---|
198 | * @param boxProvider The box provider to use
|
---|
199 | * @param offsetX x offset, in screen space
|
---|
200 | * @param offsetY y offset, in screen space
|
---|
201 | * @param hAlign The {@link HorizontalTextAlignment}
|
---|
202 | * @param vAlign The {@link VerticalTextAlignment}
|
---|
203 | */
|
---|
204 | public BoxTextElement(Cascade c, TextLabel text, BoxProvider boxProvider,
|
---|
205 | int offsetX, int offsetY, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign) {
|
---|
206 | super(c, 5f);
|
---|
207 | xOffset = offsetX;
|
---|
208 | yOffset = offsetY;
|
---|
209 | CheckParameterUtil.ensureParameterNotNull(text);
|
---|
210 | CheckParameterUtil.ensureParameterNotNull(hAlign);
|
---|
211 | CheckParameterUtil.ensureParameterNotNull(vAlign);
|
---|
212 | this.text = text;
|
---|
213 | this.boxProvider = boxProvider;
|
---|
214 | this.hAlign = hAlign;
|
---|
215 | this.vAlign = vAlign;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Create a new {@link BoxTextElement} with a boxprovider and a box.
|
---|
220 | * @param env The MapCSS environment
|
---|
221 | * @param boxProvider The box provider.
|
---|
222 | * @return A new {@link BoxTextElement} or <code>null</code> if the creation failed.
|
---|
223 | */
|
---|
224 | public static BoxTextElement create(Environment env, BoxProvider boxProvider) {
|
---|
225 | initDefaultParameters();
|
---|
226 |
|
---|
227 | TextLabel text = TextLabel.create(env, defaultTextColorCache, false);
|
---|
228 | if (text == null) return null;
|
---|
229 | // Skip any primitives that don't have text to draw. (Styles are recreated for any tag change.)
|
---|
230 | // The concrete text to render is not cached in this object, but computed for each
|
---|
231 | // repaint. This way, one BoxTextElement object can be used by multiple primitives (to save memory).
|
---|
232 | if (text.labelCompositionStrategy.compose(env.osm) == null) return null;
|
---|
233 |
|
---|
234 | Cascade c = env.mc.getCascade(env.layer);
|
---|
235 |
|
---|
236 | HorizontalTextAlignment hAlign;
|
---|
237 | switch (c.get(TEXT_ANCHOR_HORIZONTAL, Keyword.RIGHT, Keyword.class).val) {
|
---|
238 | case "left":
|
---|
239 | hAlign = HorizontalTextAlignment.LEFT;
|
---|
240 | break;
|
---|
241 | case "center":
|
---|
242 | hAlign = HorizontalTextAlignment.CENTER;
|
---|
243 | break;
|
---|
244 | case "right":
|
---|
245 | default:
|
---|
246 | hAlign = HorizontalTextAlignment.RIGHT;
|
---|
247 | }
|
---|
248 | VerticalTextAlignment vAlign;
|
---|
249 | switch (c.get(TEXT_ANCHOR_VERTICAL, Keyword.BOTTOM, Keyword.class).val) {
|
---|
250 | case "above":
|
---|
251 | vAlign = VerticalTextAlignment.ABOVE;
|
---|
252 | break;
|
---|
253 | case "top":
|
---|
254 | vAlign = VerticalTextAlignment.TOP;
|
---|
255 | break;
|
---|
256 | case "center":
|
---|
257 | vAlign = VerticalTextAlignment.CENTER;
|
---|
258 | break;
|
---|
259 | case "below":
|
---|
260 | vAlign = VerticalTextAlignment.BELOW;
|
---|
261 | break;
|
---|
262 | case "bottom":
|
---|
263 | default:
|
---|
264 | vAlign = VerticalTextAlignment.BOTTOM;
|
---|
265 | }
|
---|
266 | Point2D offset = TextLabel.getTextOffset(c);
|
---|
267 |
|
---|
268 | return new BoxTextElement(c, text, boxProvider, (int) offset.getX(), (int) -offset.getY(), hAlign, vAlign);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Get the box in which the content should be drawn.
|
---|
273 | * @return The box.
|
---|
274 | */
|
---|
275 | public Rectangle getBox() {
|
---|
276 | return boxProvider.get().getBox();
|
---|
277 | }
|
---|
278 |
|
---|
279 | private static void initDefaultParameters() {
|
---|
280 | if (defaultTextColorCache != null) return;
|
---|
281 | defaultTextColorCache = PaintColors.TEXT.get();
|
---|
282 | }
|
---|
283 |
|
---|
284 | @Override
|
---|
285 | public void paintPrimitive(IPrimitive osm, MapPaintSettings settings, StyledMapRenderer painter,
|
---|
286 | boolean selected, boolean outermember, boolean member) {
|
---|
287 | if (osm instanceof INode) {
|
---|
288 | painter.drawBoxText((INode) osm, this);
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | @Override
|
---|
293 | public boolean equals(Object obj) {
|
---|
294 | if (this == obj) return true;
|
---|
295 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
296 | if (!super.equals(obj)) return false;
|
---|
297 | BoxTextElement that = (BoxTextElement) obj;
|
---|
298 | return hAlign == that.hAlign &&
|
---|
299 | vAlign == that.vAlign &&
|
---|
300 | xOffset == that.xOffset &&
|
---|
301 | yOffset == that.yOffset &&
|
---|
302 | Objects.equals(text, that.text) &&
|
---|
303 | Objects.equals(boxProvider, that.boxProvider);
|
---|
304 | }
|
---|
305 |
|
---|
306 | @Override
|
---|
307 | public int hashCode() {
|
---|
308 | return Objects.hash(super.hashCode(), text, boxProvider, hAlign, vAlign, xOffset, yOffset);
|
---|
309 | }
|
---|
310 |
|
---|
311 | @Override
|
---|
312 | public String toString() {
|
---|
313 | return "BoxTextElement{" + super.toString() + ' ' + text.toStringImpl()
|
---|
314 | + " box=" + getBox() + " hAlign=" + hAlign + " vAlign=" + vAlign + " xOffset=" + xOffset + " yOffset=" + yOffset + '}';
|
---|
315 | }
|
---|
316 | }
|
---|