Audacity  3.0.3
Snap.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Snap.h
6 
7  Dominic Mazzoni
8 
9  Create one of these objects at the beginning of a click/drag.
10  Then, given a time corresponding to the current mouse cursor
11  position, it will tell you the closest place to snap to.
12 
13 **********************************************************************/
14 
15 #ifndef __AUDACITY_SNAP__
16 #define __AUDACITY_SNAP__
17 
18 #include <vector>
19 #include <wx/defs.h>
20 #include "widgets/NumericTextCtrl.h" // member variable
21 
22 class AudacityProject;
23 class Track;
24 class TrackList;
25 class ZoomInfo;
26 class wxDC;
27 
28 const int kPixelTolerance = 4;
29 
30 class SnapPoint
31 {
32 public:
33  explicit
34  SnapPoint(double t_ = 0.0, const Track *track_ = nullptr)
35  : t(t_), track(track_)
36  {
37  }
38 
39  double t;
40  const Track *track;
41 };
42 
43 using SnapPointArray = std::vector < SnapPoint > ;
44 
45 struct SnapResults {
46  double timeSnappedTime{ 0.0 };
47  double outTime{ 0.0 };
48  wxInt64 outCoord{ -1 };
49  bool snappedPoint{ false };
50  bool snappedTime{ false };
51 
52  bool Snapped() const { return snappedPoint || snappedTime; }
53 };
54 
55 class AUDACITY_DLL_API SnapManager
56 {
57 public:
59  SnapManager(const AudacityProject &project,
60  SnapPointArray candidates,
61  const ZoomInfo &zoomInfo,
62  bool noTimeSnap = false,
63  int pixelTolerance = kPixelTolerance);
64 
67  SnapManager(const AudacityProject &project,
68  const TrackList &tracks,
69  const ZoomInfo &zoomInfo,
70  SnapPointArray candidates = {},
71  bool noTimeSnap = false,
72  int pixelTolerance = kPixelTolerance);
73 
74  ~SnapManager();
75 
76  // The track may be NULL.
77  // Returns true if the output time is not the same as the input.
78  // Pass rightEdge=true if this is the right edge of a selection,
79  // and false if it's the left edge.
80  SnapResults Snap(Track *currentTrack,
81  double t,
82  bool rightEdge);
83 
84  static const TranslatableStrings &GetSnapLabels();
85 
86  // The two coordinates need not be ordered:
87  static void Draw( wxDC *dc, wxInt64 snap0, wxInt64 snap1 );
88 
89 private:
90 
91  void Reinit();
92  void CondListAdd(double t, const Track *track);
93  double Get(size_t index);
94  wxInt64 PixelDiff(double t, size_t index);
95  size_t Find(double t, size_t i0, size_t i1);
96  size_t Find(double t);
97  bool SnapToPoints(Track *currentTrack, double t, bool rightEdge, double *outT);
98 
99 private:
100 
105 
107  double mEpsilon{ 1 / 44100.0 };
110 
111  // Info for snap-to-time
113  bool mSnapToTime{ false };
114 
115  int mSnapTo{ 0 };
116  double mRate{ 0.0 };
118 };
119 
120 #endif
TranslatableStrings
std::vector< TranslatableString > TranslatableStrings
Definition: TranslatableString.h:295
SnapManager::mConverter
NumericConverter mConverter
Definition: Snap.h:112
ZoomInfo
Definition: ZoomInfo.h:47
TrackList
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:1280
SnapResults::snappedTime
bool snappedTime
Definition: Snap.h:50
SnapManager::mZoomInfo
const ZoomInfo * mZoomInfo
Definition: Snap.h:102
SnapResults
Definition: Snap.h:45
SnapResults::outTime
double outTime
Definition: Snap.h:47
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
SnapResults::timeSnappedTime
double timeSnappedTime
Definition: Snap.h:46
SnapResults::Snapped
bool Snapped() const
Definition: Snap.h:52
BasicUI::Get
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:26
kPixelTolerance
const int kPixelTolerance
Definition: Snap.h:28
SnapPoint::t
double t
Definition: Snap.h:39
SnapResults::outCoord
wxInt64 outCoord
Definition: Snap.h:48
SnapPoint::track
const Track * track
Definition: Snap.h:40
SnapManager::mNoTimeSnap
bool mNoTimeSnap
Definition: Snap.h:104
ActiveProjects::Find
wxString Find(const FilePath &path)
NumericConverter
NumericConverter provides the advanced formatting control used in the selection bar of Audacity.
Definition: NumericTextCtrl.h:48
Track
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:239
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:92
SnapManager
Definition: Snap.h:56
SnapPoint::SnapPoint
SnapPoint(double t_=0.0, const Track *track_=nullptr)
Definition: Snap.h:34
NumericTextCtrl.h
SnapManager::mSnapPoints
SnapPointArray mSnapPoints
Definition: Snap.h:109
SnapResults::snappedPoint
bool snappedPoint
Definition: Snap.h:49
SnapPoint
Definition: Snap.h:31
SnapManager::mPixelTolerance
int mPixelTolerance
Definition: Snap.h:103
SnapManager::mCandidates
SnapPointArray mCandidates
Definition: Snap.h:108
SnapPointArray
std::vector< SnapPoint > SnapPointArray
Definition: Snap.h:43
SnapManager::mProject
const AudacityProject * mProject
Definition: Snap.h:101