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