1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Graphics2D;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.Rectangle;
|
---|
9 | import java.awt.Stroke;
|
---|
10 | import java.awt.geom.GeneralPath;
|
---|
11 | import java.awt.geom.Line2D;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
20 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * A visitor that paints a simple scheme of every primitive it visits to a
|
---|
24 | * previous set graphic environment.
|
---|
25 | *
|
---|
26 | * @author imi
|
---|
27 | */
|
---|
28 | public class SimplePaintVisitor implements Visitor {
|
---|
29 |
|
---|
30 | public final static Color darkerblue = new Color(0,0,96);
|
---|
31 | public final static Color darkblue = new Color(0,0,128);
|
---|
32 | public final static Color darkgreen = new Color(0,128,0);
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * The environment to paint to.
|
---|
36 | */
|
---|
37 | protected Graphics g;
|
---|
38 | /**
|
---|
39 | * MapView to get screen coordinates.
|
---|
40 | */
|
---|
41 | protected NavigatableComponent nc;
|
---|
42 |
|
---|
43 | public boolean inactive;
|
---|
44 |
|
---|
45 | protected static final double PHI = Math.toRadians(20);
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Preferences
|
---|
49 | */
|
---|
50 | protected Color inactiveColor;
|
---|
51 | protected Color selectedColor;
|
---|
52 | protected Color nodeColor;
|
---|
53 | protected Color dfltWayColor;
|
---|
54 | protected Color untaggedWayColor;
|
---|
55 | protected Color incompleteColor;
|
---|
56 | protected Color backgroundColor;
|
---|
57 | protected boolean showDirectionArrow;
|
---|
58 | protected boolean showOrderNumber;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Draw subsequent segments of same color as one Path
|
---|
62 | */
|
---|
63 | protected Color currentColor = null;
|
---|
64 | protected GeneralPath currentPath = new GeneralPath();
|
---|
65 |
|
---|
66 | public void visitAll(DataSet data) {
|
---|
67 |
|
---|
68 | inactiveColor = getPreferencesColor("inactive", Color.DARK_GRAY);
|
---|
69 | selectedColor = getPreferencesColor("selected", Color.WHITE);
|
---|
70 | nodeColor = getPreferencesColor("node", Color.RED);
|
---|
71 | dfltWayColor = getPreferencesColor("way", darkblue);
|
---|
72 | untaggedWayColor = getPreferencesColor("untagged way", darkgreen);
|
---|
73 | incompleteColor = getPreferencesColor("incomplete way", darkerblue);
|
---|
74 | backgroundColor = getPreferencesColor("background", Color.BLACK);
|
---|
75 | showDirectionArrow = Main.pref.getBoolean("draw.segment.direction");
|
---|
76 | showOrderNumber = Main.pref.getBoolean("draw.segment.order_number");
|
---|
77 |
|
---|
78 | // draw tagged ways first, then untagged ways. takes
|
---|
79 | // time to iterate through list twice, OTOH does not
|
---|
80 | // require changing the colour while painting...
|
---|
81 | for (final OsmPrimitive osm : data.ways)
|
---|
82 | if (!osm.deleted && !osm.selected && osm.tagged)
|
---|
83 | osm.visit(this);
|
---|
84 | displaySegments(null);
|
---|
85 |
|
---|
86 | for (final OsmPrimitive osm : data.ways)
|
---|
87 | if (!osm.deleted && !osm.selected && !osm.tagged)
|
---|
88 | osm.visit(this);
|
---|
89 | displaySegments(null);
|
---|
90 |
|
---|
91 | for (final OsmPrimitive osm : data.nodes)
|
---|
92 | if (!osm.deleted && !osm.selected)
|
---|
93 | osm.visit(this);
|
---|
94 |
|
---|
95 | for (final OsmPrimitive osm : data.getSelected())
|
---|
96 | if (!osm.deleted)
|
---|
97 | osm.visit(this);
|
---|
98 | displaySegments(null);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Draw a small rectangle.
|
---|
103 | * White if selected (as always) or red otherwise.
|
---|
104 | *
|
---|
105 | * @param n The node to draw.
|
---|
106 | */
|
---|
107 | public void visit(Node n) {
|
---|
108 | if (n.incomplete) return;
|
---|
109 |
|
---|
110 | Color color = null;
|
---|
111 | if (inactive)
|
---|
112 | color = inactiveColor;
|
---|
113 | else if (n.selected)
|
---|
114 | color = selectedColor;
|
---|
115 | else
|
---|
116 | color = nodeColor;
|
---|
117 | drawNode(n, color);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Draw a darkblue line for all segments.
|
---|
122 | * @param w The way to draw.
|
---|
123 | */
|
---|
124 | public void visit(Way w) {
|
---|
125 | if (w.incomplete) return;
|
---|
126 |
|
---|
127 | Color wayColor;
|
---|
128 |
|
---|
129 | if (inactive) {
|
---|
130 | wayColor = inactiveColor;
|
---|
131 | } else if (!w.tagged) {
|
---|
132 | wayColor = untaggedWayColor;
|
---|
133 | } else {
|
---|
134 | wayColor = dfltWayColor;
|
---|
135 | }
|
---|
136 |
|
---|
137 | int orderNumber = 0;
|
---|
138 | Node lastN = null;
|
---|
139 | for (Node n : w.nodes) {
|
---|
140 | if (lastN == null) {
|
---|
141 | lastN = n;
|
---|
142 | continue;
|
---|
143 | }
|
---|
144 | orderNumber++;
|
---|
145 | drawSegment(lastN, n, w.selected && !inactive ? selectedColor : wayColor, showDirectionArrow);
|
---|
146 | if (showOrderNumber)
|
---|
147 | drawOrderNumber(lastN, n, orderNumber);
|
---|
148 | lastN = n;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public void visit(Relation e) {
|
---|
153 | // relations are not (yet?) drawn.
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Draw an number of the order of the two consecutive nodes within the
|
---|
158 | * parents way
|
---|
159 | */
|
---|
160 | protected void drawOrderNumber(Node n1, Node n2, int orderNumber) {
|
---|
161 | int strlen = (""+orderNumber).length();
|
---|
162 | Point p1 = nc.getPoint(n1.eastNorth);
|
---|
163 | Point p2 = nc.getPoint(n2.eastNorth);
|
---|
164 | int x = (p1.x+p2.x)/2 - 4*strlen;
|
---|
165 | int y = (p1.y+p2.y)/2 + 4;
|
---|
166 |
|
---|
167 | Rectangle screen = g.getClipBounds();
|
---|
168 | if (screen.contains(x,y)) {
|
---|
169 | Color c = g.getColor();
|
---|
170 | g.setColor(backgroundColor);
|
---|
171 | g.fillRect(x-1, y-12, 8*strlen+1, 14);
|
---|
172 | g.setColor(c);
|
---|
173 | g.drawString(""+orderNumber, x, y);
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Draw the node as small rectangle with the given color.
|
---|
179 | *
|
---|
180 | * @param n The node to draw.
|
---|
181 | * @param color The color of the node.
|
---|
182 | */
|
---|
183 | public void drawNode(Node n, Color color) {
|
---|
184 | Point p = nc.getPoint(n.eastNorth);
|
---|
185 | g.setColor(color);
|
---|
186 | Rectangle screen = g.getClipBounds();
|
---|
187 |
|
---|
188 | if (screen.contains(p.x, p.y))
|
---|
189 | if (n.tagged) {
|
---|
190 | g.drawRect(p.x, p.y, 0, 0);
|
---|
191 | g.drawRect(p.x-2, p.y-2, 4, 4);
|
---|
192 | } else {
|
---|
193 | g.drawRect(p.x-1, p.y-1, 2, 2);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Draw a line with the given color.
|
---|
199 | */
|
---|
200 | protected void drawSegment(Node n1, Node n2, Color col, boolean showDirection) {
|
---|
201 |
|
---|
202 | if (col != currentColor) displaySegments(col);
|
---|
203 |
|
---|
204 | Point p1 = nc.getPoint(n1.eastNorth);
|
---|
205 | Point p2 = nc.getPoint(n2.eastNorth);
|
---|
206 |
|
---|
207 | Rectangle screen = g.getClipBounds();
|
---|
208 | Line2D line = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
|
---|
209 | if (screen.contains(p1.x, p1.y, p2.x, p2.y) || screen.intersectsLine(line))
|
---|
210 | {
|
---|
211 | currentPath.moveTo(p1.x, p1.y);
|
---|
212 | currentPath.lineTo(p2.x, p2.y);
|
---|
213 |
|
---|
214 | if (showDirection) {
|
---|
215 | double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
|
---|
216 | currentPath.lineTo((int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
|
---|
217 | currentPath.moveTo((int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
|
---|
218 | currentPath.lineTo(p2.x, p2.y);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | public static Color getPreferencesColor(String colName, Color def) {
|
---|
224 | String colStr = Main.pref.get("color."+colName);
|
---|
225 | if (colStr.equals("")) {
|
---|
226 | Main.pref.put("color."+colName, ColorHelper.color2html(def));
|
---|
227 | return def;
|
---|
228 | }
|
---|
229 | return ColorHelper.html2color(colStr);
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void setGraphics(Graphics g) {
|
---|
233 | this.g = g;
|
---|
234 | }
|
---|
235 |
|
---|
236 | public void setNavigatableComponent(NavigatableComponent nc) {
|
---|
237 | this.nc = nc;
|
---|
238 | }
|
---|
239 |
|
---|
240 | protected void displaySegments(Color newColor) {
|
---|
241 | if (currentPath != null) {
|
---|
242 | g.setColor(currentColor);
|
---|
243 | ((Graphics2D) g).draw(currentPath);
|
---|
244 | currentPath = new GeneralPath();
|
---|
245 | currentColor = newColor;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|