| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 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 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Singleton marker class to track position of audio.
|
|---|
| 16 | *
|
|---|
| 17 | * @author david.earl
|
|---|
| 18 | *
|
|---|
| 19 | */
|
|---|
| 20 | public class PlayHeadDragMode extends MapMode {
|
|---|
| 21 |
|
|---|
| 22 | private boolean dragging = false;
|
|---|
| 23 | private Point mousePos = null;
|
|---|
| 24 | private Point mouseStart = null;
|
|---|
| 25 | private PlayHeadMarker playHeadMarker = null;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Constructs a new {@code PlayHeadDragMode}.
|
|---|
| 29 | * @param m Audio marker
|
|---|
| 30 | */
|
|---|
| 31 | public PlayHeadDragMode(PlayHeadMarker m) {
|
|---|
| 32 | super(tr("Drag play head"), "playheaddrag", tr("Drag play head"), null,
|
|---|
| 33 | Main.map, Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
|---|
| 34 | playHeadMarker = m;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | @Override public void enterMode() {
|
|---|
| 38 | super.enterMode();
|
|---|
| 39 | Main.map.mapView.addMouseListener(this);
|
|---|
| 40 | Main.map.mapView.addMouseMotionListener(this);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | @Override public void exitMode() {
|
|---|
| 44 | super.exitMode();
|
|---|
| 45 | Main.map.mapView.removeMouseListener(this);
|
|---|
| 46 | Main.map.mapView.removeMouseMotionListener(this);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | @Override public void mousePressed(MouseEvent ev) {
|
|---|
| 50 | mouseStart = mousePos = ev.getPoint();
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | @Override public void mouseDragged(MouseEvent ev) {
|
|---|
| 54 | if (mouseStart == null || mousePos == null) return;
|
|---|
| 55 | if ((ev.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0) return;
|
|---|
| 56 | Point p = ev.getPoint();
|
|---|
| 57 | if (p == null) return;
|
|---|
| 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 | Point p = ev.getPoint();
|
|---|
| 70 | mouseStart = null;
|
|---|
| 71 | if (ev.getButton() != MouseEvent.BUTTON1 || p == null || ! dragging)
|
|---|
| 72 | return;
|
|---|
| 73 |
|
|---|
| 74 | requestFocusInMapView();
|
|---|
| 75 | updateKeyModifiers(ev);
|
|---|
| 76 |
|
|---|
| 77 | EastNorth en = Main.map.mapView.getEastNorth(ev.getX(), ev.getY());
|
|---|
| 78 | if (! shift) {
|
|---|
| 79 | playHeadMarker.reposition(en);
|
|---|
| 80 | } else {
|
|---|
| 81 | playHeadMarker.synchronize(en);
|
|---|
| 82 | }
|
|---|
| 83 | mousePos = null;
|
|---|
| 84 | dragging = false;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | @Override public String getModeHelpText() {
|
|---|
| 88 | return tr("Drag play head and release near track to play audio from there; SHIFT+release to synchronize audio at that point.");
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|