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