1 | /*
|
---|
2 | * GPLv2 or 3, Copyright (c) 2010 Andrzej Zaborowski
|
---|
3 | *
|
---|
4 | * This is the main class for the game plugin.
|
---|
5 | */
|
---|
6 | package wmsturbochallenge;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
9 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
10 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
11 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 |
|
---|
14 | import javax.swing.JMenu;
|
---|
15 | import javax.swing.JMenuItem;
|
---|
16 |
|
---|
17 | import java.awt.event.ActionEvent;
|
---|
18 |
|
---|
19 | public class WMSRacer implements LayerChangeListener {
|
---|
20 | public WMSRacer() {
|
---|
21 | driveAction.updateEnabledState();
|
---|
22 |
|
---|
23 | JMenu toolsMenu = Main.main.menu.toolsMenu;
|
---|
24 | toolsMenu.addSeparator();
|
---|
25 | toolsMenu.add(new JMenuItem(driveAction));
|
---|
26 | }
|
---|
27 |
|
---|
28 | /* Rather than add an action or main menu entry we should add
|
---|
29 | * an entry in the new layer's context menus in layerAdded
|
---|
30 | * but there doesn't seem to be any way to do that :( */
|
---|
31 | protected class DriveAction extends JosmAction {
|
---|
32 | public MapFrame frame = null;
|
---|
33 | public Layer currentLayer = null;
|
---|
34 | protected Layer groundLayer = null;
|
---|
35 |
|
---|
36 | public DriveAction() {
|
---|
37 | super("Go driving", "wmsracer",
|
---|
38 | "Drive a race car on this layer",
|
---|
39 | null, true);
|
---|
40 | setEnabled(false);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void actionPerformed(ActionEvent ev) {
|
---|
44 | if (groundLayer == null ||
|
---|
45 | !groundLayer.isBackgroundLayer())
|
---|
46 | return;
|
---|
47 |
|
---|
48 | new GameWindow(groundLayer);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void updateEnabledState() {
|
---|
52 | if (frame == null) {
|
---|
53 | groundLayer = null;
|
---|
54 | setEnabled(false);
|
---|
55 | return;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (currentLayer != null &&
|
---|
59 | currentLayer.isBackgroundLayer()) {
|
---|
60 | groundLayer = currentLayer;
|
---|
61 | setEnabled(true);
|
---|
62 | return;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* TODO: should only iterate through visible layers?
|
---|
66 | * or only wms layers? or perhaps we should allow
|
---|
67 | * driving on data/gpx layers too, or the full layer
|
---|
68 | * stack (by calling mapView.paint() instead of
|
---|
69 | * layer.paint()? Nah.
|
---|
70 | * (Note that for GPX or Data layers we could do
|
---|
71 | * some clever rendering directly on our perspectivic
|
---|
72 | * pseudo-3d surface by defining a strange projection
|
---|
73 | * like that or rendering in "stripes" at different
|
---|
74 | * horizontal scanlines (lines equidistant from
|
---|
75 | * camera eye)) */
|
---|
76 | for (Layer l : frame.mapView.getAllLayers())
|
---|
77 | if (l.isBackgroundLayer()) {
|
---|
78 | groundLayer = l;
|
---|
79 | setEnabled(true);
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | groundLayer = null;
|
---|
84 | setEnabled(false);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected DriveAction driveAction = new DriveAction();
|
---|
89 |
|
---|
90 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
---|
91 | if (oldFrame != null)
|
---|
92 | oldFrame.mapView.removeLayerChangeListener(this);
|
---|
93 |
|
---|
94 | driveAction.frame = newFrame;
|
---|
95 | driveAction.updateEnabledState();
|
---|
96 |
|
---|
97 | if (newFrame != null)
|
---|
98 | newFrame.mapView.addLayerChangeListener(this);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* LayerChangeListener methods */
|
---|
102 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
103 | driveAction.currentLayer = newLayer;
|
---|
104 | driveAction.updateEnabledState();
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void layerAdded(Layer newLayer) {
|
---|
108 | driveAction.updateEnabledState();
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void layerRemoved(Layer oldLayer) {
|
---|
112 | driveAction.updateEnabledState();
|
---|
113 | }
|
---|
114 | }
|
---|