1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Cursor;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
12 | import org.openstreetmap.josm.gui.layer.markerlayer.PlayHeadMarker;
|
---|
13 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Singleton marker class to track position of audio.
|
---|
17 | *
|
---|
18 | * @author david.earl
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class PlayHeadDragMode extends MapMode {
|
---|
22 |
|
---|
23 | private boolean dragging;
|
---|
24 | private Point mousePos;
|
---|
25 | private Point mouseStart;
|
---|
26 | private final transient PlayHeadMarker playHeadMarker;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code PlayHeadDragMode}.
|
---|
30 | * @param m Audio marker
|
---|
31 | */
|
---|
32 | public PlayHeadDragMode(PlayHeadMarker m) {
|
---|
33 | super(tr("Drag play head"), "playheaddrag", tr("Drag play head"), (Shortcut) null,
|
---|
34 | Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
35 | playHeadMarker = m;
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override public void enterMode() {
|
---|
39 | super.enterMode();
|
---|
40 | Main.map.mapView.addMouseListener(this);
|
---|
41 | Main.map.mapView.addMouseMotionListener(this);
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override public void exitMode() {
|
---|
45 | super.exitMode();
|
---|
46 | Main.map.mapView.removeMouseListener(this);
|
---|
47 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override public void mousePressed(MouseEvent ev) {
|
---|
51 | mouseStart = mousePos = ev.getPoint();
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override public void mouseDragged(MouseEvent ev) {
|
---|
55 | if (mouseStart == null || mousePos == null) return;
|
---|
56 | if ((ev.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0) return;
|
---|
57 | Point p = ev.getPoint();
|
---|
58 | if (!dragging) {
|
---|
59 | if (p.distance(mouseStart) < 3) return;
|
---|
60 | playHeadMarker.startDrag();
|
---|
61 | dragging = true;
|
---|
62 | }
|
---|
63 | if (p.distance(mousePos) == 0) return;
|
---|
64 | playHeadMarker.drag(Main.map.mapView.getEastNorth(ev.getX(), ev.getY()));
|
---|
65 | mousePos = p;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override public void mouseReleased(MouseEvent ev) {
|
---|
69 | mouseStart = null;
|
---|
70 | if (ev.getButton() != MouseEvent.BUTTON1 || !dragging)
|
---|
71 | return;
|
---|
72 |
|
---|
73 | requestFocusInMapView();
|
---|
74 | updateKeyModifiers(ev);
|
---|
75 |
|
---|
76 | EastNorth en = Main.map.mapView.getEastNorth(ev.getX(), ev.getY());
|
---|
77 | if (!shift) {
|
---|
78 | playHeadMarker.reposition(en);
|
---|
79 | } else {
|
---|
80 | playHeadMarker.synchronize(en);
|
---|
81 | }
|
---|
82 | mousePos = null;
|
---|
83 | dragging = false;
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override public String getModeHelpText() {
|
---|
87 | return tr("Drag play head and release near track to play audio from there; SHIFT+release to synchronize audio at that point.");
|
---|
88 | }
|
---|
89 | }
|
---|