source: osm/applications/editors/josm/plugins/wms-turbo-challenge2/src/wmsturbochallenge/WMSRacer.java

Last change on this file was 36483, checked in by stoecker, 4 months ago

set eol-style, fix checkstyle issues, add ignores

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package wmsturbochallenge;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7
8import javax.swing.JMenu;
9import javax.swing.JMenuItem;
10
11import org.openstreetmap.josm.actions.JosmAction;
12import org.openstreetmap.josm.gui.MainApplication;
13import org.openstreetmap.josm.gui.MapFrame;
14import org.openstreetmap.josm.gui.layer.Layer;
15import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
16import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
17import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
18import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
19import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
20import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
21import org.openstreetmap.josm.plugins.Plugin;
22import org.openstreetmap.josm.plugins.PluginInformation;
23
24/**
25 * This is the main class for the game plugin.
26 * @author Andrzej Zaborowski
27 */
28public class WMSRacer extends Plugin implements LayerChangeListener, ActiveLayerChangeListener {
29 public WMSRacer(PluginInformation info) {
30 super(info);
31 driveAction.updateEnabledState();
32
33 JMenu toolsMenu = MainApplication.getMenu().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
96 groundLayer = null;
97 setEnabled(false);
98 }
99 }
100
101 protected DriveAction driveAction = new DriveAction();
102
103 @Override
104 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
105 if (oldFrame != null) {
106 MainApplication.getLayerManager().removeLayerChangeListener(this);
107 MainApplication.getLayerManager().removeActiveLayerChangeListener(this);
108 }
109
110 driveAction.frame = newFrame;
111 driveAction.updateEnabledState();
112
113 if (newFrame != null) {
114 MainApplication.getLayerManager().addLayerChangeListener(this);
115 MainApplication.getLayerManager().addActiveLayerChangeListener(this);
116 }
117 }
118
119 @Override
120 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
121 driveAction.currentLayer = MainApplication.getLayerManager().getActiveLayer();
122 driveAction.updateEnabledState();
123 }
124
125 @Override
126 public void layerAdded(LayerAddEvent e) {
127 driveAction.updateEnabledState();
128 }
129
130 @Override
131 public void layerRemoving(LayerRemoveEvent e) {
132 driveAction.updateEnabledState();
133 }
134
135 @Override
136 public void layerOrderChanged(LayerOrderChangeEvent e) {
137 // Do nothing
138 }
139}
Note: See TracBrowser for help on using the repository browser.