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