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