1 | package org.openstreetmap.josm.data.osm.visitor;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.Point;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.Main;
|
---|
8 | import org.openstreetmap.josm.data.osm.Node;
|
---|
9 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
10 | import org.openstreetmap.josm.data.osm.Way;
|
---|
11 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
12 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * A visitor that paint a simple scheme of every primitive it visits to a
|
---|
16 | * previous set graphic environment.
|
---|
17 | *
|
---|
18 | * @author imi
|
---|
19 | */
|
---|
20 | public class SimplePaintVisitor implements Visitor {
|
---|
21 |
|
---|
22 | public final static Color darkerblue = new Color(0,0,96);
|
---|
23 | public final static Color darkblue = new Color(0,0,128);
|
---|
24 | public final static Color darkgreen = new Color(0,128,0);
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The environment to paint to.
|
---|
28 | */
|
---|
29 | private final Graphics g;
|
---|
30 | /**
|
---|
31 | * MapView to get screen coordinates.
|
---|
32 | */
|
---|
33 | private final NavigatableComponent nc;
|
---|
34 | private static final double PHI = Math.toRadians(20);
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Construct the painter visitor.
|
---|
38 | * @param g The graphics to draw to.
|
---|
39 | * @param mv The view to get screen coordinates from.
|
---|
40 | */
|
---|
41 | public SimplePaintVisitor(Graphics g, NavigatableComponent mv) {
|
---|
42 | this.g = g;
|
---|
43 | this.nc = mv;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Draw a small rectangle.
|
---|
48 | * White if selected (as always) or red otherwise.
|
---|
49 | *
|
---|
50 | * @param n The node to draw.
|
---|
51 | */
|
---|
52 | public void visit(Node n) {
|
---|
53 | drawNode(n, n.selected ? getPreferencesColor("selected", Color.WHITE)
|
---|
54 | : getPreferencesColor("node", Color.RED));
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Draw just a line between the points.
|
---|
59 | * White if selected (as always) or green otherwise.
|
---|
60 | */
|
---|
61 | public void visit(Segment ls) {
|
---|
62 | drawSegment(ls, getPreferencesColor("segment", darkgreen));
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Draw a darkblue line for all segments.
|
---|
67 | * @param w The way to draw.
|
---|
68 | */
|
---|
69 | public void visit(Way w) {
|
---|
70 | // only to overwrite with blue
|
---|
71 | Color wayColor = getPreferencesColor("way", darkblue);
|
---|
72 | for (Segment ls : w.segments) {
|
---|
73 | if (ls.incomplete) {
|
---|
74 | wayColor = getPreferencesColor("incomplete way", darkerblue);
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | for (Segment ls : w.segments)
|
---|
80 | if (!ls.selected) // selected already in good color
|
---|
81 | drawSegment(ls, w.selected ? getPreferencesColor("selected", Color.WHITE) : wayColor);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Draw the node as small rectangle with the given color.
|
---|
86 | *
|
---|
87 | * @param n The node to draw.
|
---|
88 | * @param color The color of the node.
|
---|
89 | */
|
---|
90 | private void drawNode(Node n, Color color) {
|
---|
91 | Point p = nc.getPoint(n.eastNorth);
|
---|
92 | g.setColor(color);
|
---|
93 | g.drawRect(p.x-1, p.y-1, 2, 2);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Draw a line with the given color.
|
---|
98 | */
|
---|
99 | private void drawSegment(Segment ls, Color col) {
|
---|
100 | if (ls.incomplete)
|
---|
101 | return;
|
---|
102 | if (ls.selected)
|
---|
103 | col = getPreferencesColor("selected", Color.WHITE);
|
---|
104 | g.setColor(col);
|
---|
105 | Point p1 = nc.getPoint(ls.from.eastNorth);
|
---|
106 | Point p2 = nc.getPoint(ls.to.eastNorth);
|
---|
107 | g.drawLine(p1.x, p1.y, p2.x, p2.y);
|
---|
108 |
|
---|
109 | if (Main.pref.getBoolean("draw.segment.direction")) {
|
---|
110 | double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
|
---|
111 | g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
|
---|
112 | g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public static Color getPreferencesColor(String colName, Color def) {
|
---|
117 | String colStr = Main.pref.get("color."+colName);
|
---|
118 | if (colStr.equals(""))
|
---|
119 | return def;
|
---|
120 | return ColorHelper.html2color(colStr);
|
---|
121 | }
|
---|
122 | }
|
---|