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