| 1 | package org.openstreetmap.josm.gui.mappaint;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.Color;
|
|---|
| 4 | import java.util.Collection;
|
|---|
| 5 |
|
|---|
| 6 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 7 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 8 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 9 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
|---|
| 10 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
|---|
| 12 | import org.openstreetmap.josm.tools.I18n;
|
|---|
| 13 |
|
|---|
| 14 | public class LineElemStyle extends ElemStyle implements Comparable<LineElemStyle> {
|
|---|
| 15 |
|
|---|
| 16 | public static final LineElemStyle UNTAGGED_WAY;
|
|---|
| 17 |
|
|---|
| 18 | static {
|
|---|
| 19 | UNTAGGED_WAY = new LineElemStyle();
|
|---|
| 20 | UNTAGGED_WAY.color = PaintColors.UNTAGGED.get();
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | private int width;
|
|---|
| 24 | public int realWidth; //the real width of this line in meter
|
|---|
| 25 | public Color color;
|
|---|
| 26 | private float[] dashed;
|
|---|
| 27 | public Color dashedColor;
|
|---|
| 28 |
|
|---|
| 29 | public boolean over;
|
|---|
| 30 | public enum WidthMode { ABSOLUTE, PERCENT, OFFSET }
|
|---|
| 31 | public WidthMode widthMode;
|
|---|
| 32 |
|
|---|
| 33 | public Collection<LineElemStyle> overlays;
|
|---|
| 34 |
|
|---|
| 35 | public LineElemStyle(LineElemStyle s, long maxScale, long minScale) {
|
|---|
| 36 | this.width = s.width;
|
|---|
| 37 | this.realWidth = s.realWidth;
|
|---|
| 38 | this.color = s.color;
|
|---|
| 39 | this.dashed = s.dashed;
|
|---|
| 40 | this.dashedColor = s.dashedColor;
|
|---|
| 41 | this.over = s.over;
|
|---|
| 42 | this.widthMode = s.widthMode;
|
|---|
| 43 |
|
|---|
| 44 | this.priority = s.priority;
|
|---|
| 45 | this.maxScale = maxScale;
|
|---|
| 46 | this.minScale = minScale;
|
|---|
| 47 | this.rules = s.rules;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | public LineElemStyle(LineElemStyle s, Collection<LineElemStyle> overlays) {
|
|---|
| 51 | this.width = s.width;
|
|---|
| 52 | this.realWidth = s.realWidth;
|
|---|
| 53 | this.color = s.color;
|
|---|
| 54 | this.dashed = s.dashed;
|
|---|
| 55 | this.dashedColor = s.dashedColor;
|
|---|
| 56 | this.over = s.over;
|
|---|
| 57 | this.widthMode = s.widthMode;
|
|---|
| 58 |
|
|---|
| 59 | this.priority = s.priority;
|
|---|
| 60 | this.maxScale = s.maxScale;
|
|---|
| 61 | this.minScale = s.minScale;
|
|---|
| 62 | this.rules = s.rules;
|
|---|
| 63 |
|
|---|
| 64 | this.overlays = overlays;
|
|---|
| 65 | this.code = s.code;
|
|---|
| 66 | for (LineElemStyle o : overlays) {
|
|---|
| 67 | this.code += o.code;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | public LineElemStyle() { init(); }
|
|---|
| 72 |
|
|---|
| 73 | public void init()
|
|---|
| 74 | {
|
|---|
| 75 | width = -1;
|
|---|
| 76 | realWidth = 0;
|
|---|
| 77 | dashed = new float[0];
|
|---|
| 78 | dashedColor = null;
|
|---|
| 79 | priority = 0;
|
|---|
| 80 | color = null;
|
|---|
| 81 | over = true; // only used for line modifications
|
|---|
| 82 | widthMode = WidthMode.ABSOLUTE;
|
|---|
| 83 | overlays = null;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | // get width for overlays
|
|---|
| 87 | public int getWidth(int ref)
|
|---|
| 88 | {
|
|---|
| 89 | int res;
|
|---|
| 90 | if(widthMode == WidthMode.ABSOLUTE) {
|
|---|
| 91 | res = width;
|
|---|
| 92 | } else if(widthMode == WidthMode.OFFSET) {
|
|---|
| 93 | res = ref + width;
|
|---|
| 94 | } else
|
|---|
| 95 | {
|
|---|
| 96 | if(width < 0) {
|
|---|
| 97 | res = 0;
|
|---|
| 98 | } else {
|
|---|
| 99 | res = ref*width/100;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | return res <= 0 ? 1 : res;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | public int compareTo(LineElemStyle s) {
|
|---|
| 106 | if(s.priority != priority)
|
|---|
| 107 | return s.priority > priority ? 1 : -1;
|
|---|
| 108 | if(!over && s.over)
|
|---|
| 109 | return -1;
|
|---|
| 110 | // we have no idea how to order other objects :-)
|
|---|
| 111 | return 0;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | public float[] getDashed() {
|
|---|
| 115 | return dashed;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | public void setDashed(float[] dashed) {
|
|---|
| 119 | if (dashed.length == 0) {
|
|---|
| 120 | this.dashed = dashed;
|
|---|
| 121 | return;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | boolean found = false;
|
|---|
| 125 | for (int i=0; i<dashed.length; i++) {
|
|---|
| 126 | if (dashed[i] > 0) {
|
|---|
| 127 | found = true;
|
|---|
| 128 | }
|
|---|
| 129 | if (dashed[i] < 0) {
|
|---|
| 130 | System.out.println(I18n.tr("Illegal dash pattern, values must be positive"));
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | if (found) {
|
|---|
| 134 | this.dashed = dashed;
|
|---|
| 135 | } else {
|
|---|
| 136 | System.out.println(I18n.tr("Illegal dash pattern, at least one value must be > 0"));
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | @Override
|
|---|
| 141 | public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
|
|---|
| 142 | Way w = (Way)primitive;
|
|---|
| 143 | /* show direction arrows, if draw.segment.relevant_directions_only is not set,
|
|---|
| 144 | the way is tagged with a direction key
|
|---|
| 145 | (even if the tag is negated as in oneway=false) or the way is selected */
|
|---|
| 146 | boolean showDirection = selected || ((!paintSettings.isUseRealWidth()) && (paintSettings.isShowDirectionArrow()
|
|---|
| 147 | && (!paintSettings.isShowRelevantDirectionsOnly() || w.hasDirectionKeys())));
|
|---|
| 148 | boolean reversedDirection = w.reversedDirection();
|
|---|
| 149 | /* head only takes over control if the option is true,
|
|---|
| 150 | the direction should be shown at all and not only because it's selected */
|
|---|
| 151 | boolean showOnlyHeadArrowOnly = showDirection && !selected && paintSettings.isShowHeadArrowOnly();
|
|---|
| 152 | Node lastN;
|
|---|
| 153 |
|
|---|
| 154 | Color myColor = color;
|
|---|
| 155 | Color myDashedColor = dashedColor;
|
|---|
| 156 | int myWidth = getWidth();
|
|---|
| 157 |
|
|---|
| 158 | if (realWidth > 0 && paintSettings.isUseRealWidth() && !showDirection) {
|
|---|
| 159 |
|
|---|
| 160 | /* if we have a "width" tag, try use it */
|
|---|
| 161 | /* (this might be slow and could be improved by caching the value in the Way, on the other hand only used if "real width" is enabled) */
|
|---|
| 162 | String widthTag = w.get("width");
|
|---|
| 163 | if(widthTag == null) {
|
|---|
| 164 | widthTag = w.get("est_width");
|
|---|
| 165 | }
|
|---|
| 166 | if(widthTag != null) {
|
|---|
| 167 | try {
|
|---|
| 168 | realWidth = Integer.parseInt(widthTag);
|
|---|
| 169 | }
|
|---|
| 170 | catch(NumberFormatException nfe) {
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
|
|---|
| 175 | if (myWidth < getWidth()) {
|
|---|
| 176 | myWidth = getWidth();
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if(w.isHighlighted()) {
|
|---|
| 181 | myColor = paintSettings.getHighlightColor();
|
|---|
| 182 | } else if (selected) {
|
|---|
| 183 | myColor = member ? paintSettings.getRelationSelectedColor() : paintSettings.getSelectedColor();
|
|---|
| 184 | } else if(w.isDisabled()) {
|
|---|
| 185 | myColor = paintSettings.getInactiveColor();
|
|---|
| 186 | myDashedColor = paintSettings.getInactiveColor();
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /* draw overlays under the way */
|
|---|
| 190 | if(overlays != null) {
|
|---|
| 191 | for(LineElemStyle s : overlays) {
|
|---|
| 192 | if(!s.over) {
|
|---|
| 193 | painter.drawWay(w, (s.color == null || selected) ? myColor: s.color, s.getWidth(myWidth),
|
|---|
| 194 | s.getDashed(),
|
|---|
| 195 | w.isDisabled() ? paintSettings.getInactiveColor() : s.dashedColor,
|
|---|
| 196 | false, false, false);
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /* draw the way */
|
|---|
| 202 | painter.drawWay(w, myColor, myWidth, dashed, myDashedColor, showDirection, selected ? false : reversedDirection, showOnlyHeadArrowOnly);
|
|---|
| 203 |
|
|---|
| 204 | /* draw overlays above the way */
|
|---|
| 205 | if(overlays != null) {
|
|---|
| 206 | for(LineElemStyle s : overlays) {
|
|---|
| 207 | if(s.over) {
|
|---|
| 208 | painter.drawWay(w, (s.color == null || selected) ? myColor : s.color, s.getWidth(myWidth),
|
|---|
| 209 | s.getDashed(), s.dashedColor, false, false, false);
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | if(paintSettings.isShowOrderNumber()) {
|
|---|
| 215 | int orderNumber = 0;
|
|---|
| 216 | lastN = null;
|
|---|
| 217 | for(Node n : w.getNodes()) {
|
|---|
| 218 | if(lastN != null) {
|
|---|
| 219 | orderNumber++;
|
|---|
| 220 | painter.drawOrderNumber(lastN, n, orderNumber);
|
|---|
| 221 | }
|
|---|
| 222 | lastN = n;
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | public int getWidth() {
|
|---|
| 228 | if (width == -1)
|
|---|
| 229 | return MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
|
|---|
| 230 | return width;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | public void setWidth(int width) {
|
|---|
| 234 | this.width = width;
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|