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