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