1 | // License: GPL. See LICENSE file for details.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui.layer.markerlayer;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Graphics;
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.awt.Rectangle;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.ActionListener;
|
---|
12 | import java.awt.event.MouseAdapter;
|
---|
13 | import java.awt.event.MouseEvent;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.Iterator;
|
---|
16 |
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.Timer;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
22 | import org.openstreetmap.josm.actions.mapmode.PlayHeadDragMode;
|
---|
23 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
26 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
27 | import org.openstreetmap.josm.gui.MapView;
|
---|
28 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
29 | import org.openstreetmap.josm.tools.AudioPlayer;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Singleton marker class to track position of audio.
|
---|
33 | *
|
---|
34 | * @author David Earl<david@frankieandshadow.com>
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class PlayHeadMarker extends Marker {
|
---|
38 |
|
---|
39 | private Timer timer = null;
|
---|
40 | private double animationInterval = 0.0; // seconds
|
---|
41 | // private Rectangle audioTracer = null;
|
---|
42 | // private Icon audioTracerIcon = null;
|
---|
43 | static private PlayHeadMarker playHead = null;
|
---|
44 | private MapMode oldMode = null;
|
---|
45 | private EastNorth oldEastNorth;
|
---|
46 | private boolean enabled;
|
---|
47 | private boolean wasPlaying = false;
|
---|
48 | private int dropTolerance; /* pixels */
|
---|
49 |
|
---|
50 | public static PlayHeadMarker create() {
|
---|
51 | if (playHead == null) {
|
---|
52 | try {
|
---|
53 | playHead = new PlayHeadMarker();
|
---|
54 | } catch (Exception ex) {
|
---|
55 | return null;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return playHead;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private PlayHeadMarker() {
|
---|
62 | super(new LatLon(0.0,0.0), "",
|
---|
63 | Main.pref.get("marker.audiotracericon", "audio-tracer"),
|
---|
64 | null, -1.0, 0.0);
|
---|
65 | enabled = Main.pref.getBoolean("marker.traceaudio", true);
|
---|
66 | if (! enabled) return;
|
---|
67 | dropTolerance = Main.pref.getInteger("marker.playHeadDropTolerance", 50);
|
---|
68 | Main.map.mapView.addMouseListener(new MouseAdapter() {
|
---|
69 | @Override public void mousePressed(MouseEvent ev) {
|
---|
70 | Point p = ev.getPoint();
|
---|
71 | if (ev.getButton() != MouseEvent.BUTTON1 || p == null)
|
---|
72 | return;
|
---|
73 | if (playHead.containsPoint(p)) {
|
---|
74 | /* when we get a click on the marker, we need to switch mode to avoid
|
---|
75 | * getting confused with other drag operations (like select) */
|
---|
76 | oldMode = Main.map.mapMode;
|
---|
77 | oldEastNorth = eastNorth;
|
---|
78 | PlayHeadDragMode playHeadDragMode = new PlayHeadDragMode(playHead);
|
---|
79 | Main.map.selectMapMode(playHeadDragMode);
|
---|
80 | playHeadDragMode.mousePressed(ev);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | });
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override public boolean containsPoint(Point p) {
|
---|
87 | Point screen = Main.map.mapView.getPoint(eastNorth);
|
---|
88 | Rectangle r = new Rectangle(screen.x, screen.y, symbol.getIconWidth(),
|
---|
89 | symbol.getIconHeight());
|
---|
90 | return r.contains(p);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * called back from drag mode to say when we started dragging for real
|
---|
95 | * (at least a short distance)
|
---|
96 | */
|
---|
97 | public void startDrag() {
|
---|
98 | if (timer != null)
|
---|
99 | timer.stop();
|
---|
100 | wasPlaying = AudioPlayer.playing();
|
---|
101 | if (wasPlaying) {
|
---|
102 | try { AudioPlayer.pause(); }
|
---|
103 | catch (Exception ex) { AudioPlayer.audioMalfunction(ex);}
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * reinstate the old map mode after swuitching temporarily to do a play head drag
|
---|
109 | */
|
---|
110 | private void endDrag(boolean reset) {
|
---|
111 | if (! wasPlaying || reset)
|
---|
112 | try { AudioPlayer.pause(); }
|
---|
113 | catch (Exception ex) { AudioPlayer.audioMalfunction(ex);}
|
---|
114 | if (reset)
|
---|
115 | eastNorth = oldEastNorth;
|
---|
116 | Main.map.selectMapMode(oldMode);
|
---|
117 | Main.map.mapView.repaint();
|
---|
118 | timer.start();
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * apply the new position resulting from a drag in progress
|
---|
123 | * @param en the new position in map terms
|
---|
124 | */
|
---|
125 | public void drag(EastNorth en) {
|
---|
126 | eastNorth = en;
|
---|
127 | Main.map.mapView.repaint();
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * reposition the play head at the point on the track nearest position given,
|
---|
132 | * providing we are within reasonable distance from the track; otherwise reset to the
|
---|
133 | * original position.
|
---|
134 | * @param en the position to start looking from
|
---|
135 | */
|
---|
136 | public void reposition(EastNorth en) {
|
---|
137 | // eastNorth = en;
|
---|
138 | WayPoint cw = null;
|
---|
139 | AudioMarker recent = AudioMarker.recentlyPlayedMarker();
|
---|
140 | if (recent != null && recent.parentLayer != null && recent.parentLayer.fromLayer != null) {
|
---|
141 | /* work out EastNorth equivalent of 50 (default) pixels tolerance */
|
---|
142 | Point p = Main.map.mapView.getPoint(en);
|
---|
143 | EastNorth enPlus25px = Main.map.mapView.getEastNorth(p.x+dropTolerance, p.y);
|
---|
144 | cw = recent.parentLayer.fromLayer.nearestPointOnTrack(en, enPlus25px.east() - en.east());
|
---|
145 | }
|
---|
146 |
|
---|
147 | AudioMarker ca = null;
|
---|
148 | /* Find the prior audio marker (there should always be one in the
|
---|
149 | * layer, even if it is only one at the start of the track) to
|
---|
150 | * offset the audio from */
|
---|
151 | if (cw != null) {
|
---|
152 | if (recent != null && recent.parentLayer != null) {
|
---|
153 | for (Marker m : recent.parentLayer.data) {
|
---|
154 | if (m instanceof AudioMarker) {
|
---|
155 | AudioMarker a = (AudioMarker) m;
|
---|
156 | if (a.time > cw.time)
|
---|
157 | break;
|
---|
158 | ca = a;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (ca == null) {
|
---|
165 | /* Not close enough to track, or no audio marker found for some other reason */
|
---|
166 | JOptionPane.showMessageDialog(Main.parent, tr("You need to drag the play head near to the GPX track whose associated sound track you were playing (after the first marker)."));
|
---|
167 | endDrag(true);
|
---|
168 | } else {
|
---|
169 | eastNorth = cw.eastNorth;
|
---|
170 | ca.play(cw.time - ca.time);
|
---|
171 | endDrag(false);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Synchronize the audio at the position where the play head was paused before
|
---|
177 | * dragging with the position on the track where it was dropped.
|
---|
178 | * If this is quite near an audio marker, we use that
|
---|
179 | * marker as the sync. location, otherwise we create a new marker at the
|
---|
180 | * trackpoint nearest the end point of the drag point to apply the
|
---|
181 | * sync to.
|
---|
182 | * @param en : the EastNorth end point of the drag
|
---|
183 | */
|
---|
184 | public void synchronize(EastNorth en) {
|
---|
185 | AudioMarker recent = AudioMarker.recentlyPlayedMarker();
|
---|
186 | if(recent == null)
|
---|
187 | return;
|
---|
188 | /* First, see if we dropped onto an existing audio marker in the layer being played */
|
---|
189 | Point startPoint = Main.map.mapView.getPoint(en);
|
---|
190 | AudioMarker ca = null;
|
---|
191 | if (recent.parentLayer != null) {
|
---|
192 | double closestAudioMarkerDistanceSquared = 1.0E100;
|
---|
193 | for (Marker m : recent.parentLayer.data) {
|
---|
194 | if (m instanceof AudioMarker) {
|
---|
195 | double distanceSquared = m.eastNorth.distanceSq(en);
|
---|
196 | if (distanceSquared < closestAudioMarkerDistanceSquared) {
|
---|
197 | ca = (AudioMarker) m;
|
---|
198 | closestAudioMarkerDistanceSquared = distanceSquared;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* We found the closest marker: did we actually hit it? */
|
---|
205 | if (ca != null && ! ca.containsPoint(startPoint)) ca = null;
|
---|
206 |
|
---|
207 | /* If we didn't hit an audio marker, we need to create one at the nearest point on the track */
|
---|
208 | if (ca == null) {
|
---|
209 | /* work out EastNorth equivalent of 50 (default) pixels tolerance */
|
---|
210 | Point p = Main.map.mapView.getPoint(en);
|
---|
211 | EastNorth enPlus25px = Main.map.mapView.getEastNorth(p.x+dropTolerance, p.y);
|
---|
212 | WayPoint cw = recent.parentLayer.fromLayer.nearestPointOnTrack(en, enPlus25px.east() - en.east());
|
---|
213 | if (cw == null) {
|
---|
214 | JOptionPane.showMessageDialog(Main.parent, tr("You need to SHIFT-drag the play head onto an audio marker or onto the track point where you want to synchronize."));
|
---|
215 | endDrag(true);
|
---|
216 | return;
|
---|
217 | }
|
---|
218 | ca = recent.parentLayer.addAudioMarker(cw.time, cw.eastNorth);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* Actually do the synchronization */
|
---|
222 | if(ca == null)
|
---|
223 | {
|
---|
224 | JOptionPane.showMessageDialog(Main.parent,tr("Unable to create new audio marker."));
|
---|
225 | endDrag(true);
|
---|
226 | }
|
---|
227 | else if (recent.parentLayer.synchronizeAudioMarkers(ca)) {
|
---|
228 | JOptionPane.showMessageDialog(Main.parent, tr("Audio synchronized at point {0}.", ca.text));
|
---|
229 | eastNorth = ca.eastNorth;
|
---|
230 | endDrag(false);
|
---|
231 | } else {
|
---|
232 | JOptionPane.showMessageDialog(Main.parent,tr("Unable to synchronize in layer being played."));
|
---|
233 | endDrag(true);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | public void paint(Graphics g, MapView mv /*, boolean mousePressed */) {
|
---|
238 | if (time < 0.0) return;
|
---|
239 | Point screen = mv.getPoint(eastNorth);
|
---|
240 | symbol.paintIcon(mv, g, screen.x, screen.y);
|
---|
241 | }
|
---|
242 |
|
---|
243 | public void animate() {
|
---|
244 | if (! enabled) return;
|
---|
245 | if (timer == null) {
|
---|
246 | animationInterval = Double.parseDouble(Main.pref.get("marker.audioanimationinterval", "1")); //milliseconds
|
---|
247 | timer = new Timer((int)(animationInterval * 1000.0), new ActionListener() {
|
---|
248 | public void actionPerformed(ActionEvent e) {
|
---|
249 | timerAction();
|
---|
250 | }
|
---|
251 | });
|
---|
252 | timer.setInitialDelay(0);
|
---|
253 | } else {
|
---|
254 | timer.stop();
|
---|
255 | }
|
---|
256 | timer.start();
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * callback for moving play head marker according to audio player position
|
---|
261 | */
|
---|
262 | public void timerAction() {
|
---|
263 | AudioMarker recentlyPlayedMarker = AudioMarker.recentlyPlayedMarker();
|
---|
264 | if (recentlyPlayedMarker == null)
|
---|
265 | return;
|
---|
266 | double audioTime = recentlyPlayedMarker.time +
|
---|
267 | AudioPlayer.position() -
|
---|
268 | recentlyPlayedMarker.offset -
|
---|
269 | recentlyPlayedMarker.syncOffset;
|
---|
270 | if (Math.abs(audioTime - time) < animationInterval)
|
---|
271 | return;
|
---|
272 | if (recentlyPlayedMarker.parentLayer == null) return;
|
---|
273 | GpxLayer trackLayer = recentlyPlayedMarker.parentLayer.fromLayer;
|
---|
274 | if (trackLayer == null)
|
---|
275 | return;
|
---|
276 | /* find the pair of track points for this position (adjusted by the syncOffset)
|
---|
277 | * and interpolate between them
|
---|
278 | */
|
---|
279 | WayPoint w1 = null;
|
---|
280 | WayPoint w2 = null;
|
---|
281 |
|
---|
282 | for (GpxTrack track : trackLayer.data.tracks) {
|
---|
283 | for (Collection<WayPoint> trackseg : track.trackSegs) {
|
---|
284 | for (Iterator<WayPoint> it = trackseg.iterator(); it.hasNext();) {
|
---|
285 | WayPoint w = it.next();
|
---|
286 | if (audioTime < w.time) {
|
---|
287 | w2 = w;
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | w1 = w;
|
---|
291 | }
|
---|
292 | if (w2 != null) break;
|
---|
293 | }
|
---|
294 | if (w2 != null) break;
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (w1 == null)
|
---|
298 | return;
|
---|
299 | eastNorth = w2 == null ?
|
---|
300 | w1.eastNorth :
|
---|
301 | w1.eastNorth.interpolate(w2.eastNorth,
|
---|
302 | (audioTime - w1.time)/(w2.time - w1.time));
|
---|
303 | time = audioTime;
|
---|
304 | Main.map.mapView.repaint();
|
---|
305 | }
|
---|
306 | }
|
---|