1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-25 16:48:44 +02:00

Rewrite SnapManager to avoid using ZoomInfo::zoom

This commit is contained in:
Paul-Licameli 2015-04-20 00:04:04 -04:00 committed by Paul Licameli
parent 5418ce377b
commit fe36b636cb
4 changed files with 33 additions and 32 deletions

View File

@ -8,14 +8,14 @@
**********************************************************************/ **********************************************************************/
#include "Snap.h"
#include <algorithm>
#include <math.h> #include <math.h>
#include "LabelTrack.h" #include "LabelTrack.h"
#include "Prefs.h"
#include "Project.h" #include "Project.h"
#include "Snap.h"
#include "TrackPanel.h" #include "TrackPanel.h"
#include "WaveTrack.h"
#include "widgets/NumericTextCtrl.h" #include "widgets/NumericTextCtrl.h"
// Change this to "true" to snap to nearest and "false" to snap to previous // Change this to "true" to snap to nearest and "false" to snap to previous
@ -29,8 +29,10 @@ static int CompareSnapPoints(SnapPoint *s1, SnapPoint *s2)
} }
SnapManager::SnapManager(TrackList *tracks, TrackClipArray *exclusions, SnapManager::SnapManager(TrackList *tracks, TrackClipArray *exclusions,
double zoom, int pixelTolerance, bool noTimeSnap) const ZoomInfo &zoomInfo, int pixelTolerance, bool noTimeSnap)
: mConverter(NumericConverter::TIME) : mConverter(NumericConverter::TIME)
, mPixelTolerance(std::max(0, pixelTolerance))
, mZoomInfo(zoomInfo)
{ {
int i; int i;
@ -50,13 +52,7 @@ SnapManager::SnapManager(TrackList *tracks, TrackClipArray *exclusions,
} }
mSnapPoints = new SnapPointArray(CompareSnapPoints); mSnapPoints = new SnapPointArray(CompareSnapPoints);
if (zoom > 0 && pixelTolerance > 0)
mTolerance = pixelTolerance / zoom;
else {
// This shouldn't happen, but we don't want to crash if we get
// illegal values. The net effect of this is to never snap.
mTolerance = 0.0;
}
// Two time points closer than this are considered the same // Two time points closer than this are considered the same
mEpsilon = 1 / 44100.0; mEpsilon = 1 / 44100.0;
@ -135,9 +131,12 @@ double SnapManager::Get(int index)
} }
// Returns the difference in time between t and the point at a given index // Returns the difference in time between t and the point at a given index
double SnapManager::Diff(double t, int index) wxInt64 SnapManager::PixelDiff(double t, int index)
{ {
return fabs(t - Get(index)); return abs(
mZoomInfo.TimeToPosition(t, 0) -
mZoomInfo.TimeToPosition(Get(index), 0)
);
} }
// Find the index where this SnapPoint should go in // Find the index where this SnapPoint should go in
@ -168,7 +167,7 @@ int SnapManager::Find(double t)
next++; next++;
// Now return whichever one is closer to time t // Now return whichever one is closer to time t
if (next < len && Diff(t, next) < Diff(t, index)) if (next < len && PixelDiff(t, next) < PixelDiff(t, index))
return next; return next;
else else
return index; return index;
@ -189,7 +188,7 @@ bool SnapManager::SnapToPoints(Track *currentTrack,
int index = Find(t); int index = Find(t);
// If it's too far away, just give up now // If it's too far away, just give up now
if (Diff(t, index) >= mTolerance) if (PixelDiff(t, index) >= mPixelTolerance)
return false; return false;
// Otherwise, search left and right for all of the points // Otherwise, search left and right for all of the points
@ -198,10 +197,10 @@ bool SnapManager::SnapToPoints(Track *currentTrack,
int right = index; int right = index;
int i; int i;
while(left > 0 && Diff(t, left-1) < mTolerance) while(left > 0 && PixelDiff(t, left-1) < mPixelTolerance)
left--; left--;
while(right < len-1 && Diff(t, right+1) < mTolerance) while(right < len-1 && PixelDiff(t, right+1) < mPixelTolerance)
right++; right++;
if (left == index && right == index) { if (left == index && right == index) {

View File

@ -22,6 +22,7 @@
#include "widgets/NumericTextCtrl.h" #include "widgets/NumericTextCtrl.h"
class TrackClipArray; class TrackClipArray;
class ZoomInfo;
enum enum
{ {
@ -43,9 +44,9 @@ class SnapPoint {
WX_DEFINE_SORTED_ARRAY(SnapPoint *, SnapPointArray); WX_DEFINE_SORTED_ARRAY(SnapPoint *, SnapPointArray);
class SnapManager { class SnapManager {
public: public:
SnapManager(TrackList *tracks, TrackClipArray *exclusions, SnapManager(TrackList *tracks, TrackClipArray *exclusions,
double zoom, int pixelTolerance, bool noTimeSnap = false); const ZoomInfo &zoomInfo, int pixelTolerance, bool noTimeSnap = false);
~SnapManager(); ~SnapManager();
@ -54,34 +55,35 @@ class SnapManager {
// Pass rightEdge=true if this is the right edge of a selection, // Pass rightEdge=true if this is the right edge of a selection,
// and false if it's the left edge. // and false if it's the left edge.
bool Snap(Track *currentTrack, bool Snap(Track *currentTrack,
double t, double t,
bool rightEdge, bool rightEdge,
double *out_t, double *out_t,
bool *snappedPoint, bool *snappedPoint,
bool *snappedTime); bool *snappedTime);
static wxArrayString GetSnapLabels(); static wxArrayString GetSnapLabels();
static wxArrayString GetSnapValues(); static wxArrayString GetSnapValues();
static const wxString & GetSnapValue(int index); static const wxString & GetSnapValue(int index);
static int GetSnapIndex(const wxString & value); static int GetSnapIndex(const wxString & value);
private: private:
void CondListAdd(double t, Track *tr); void CondListAdd(double t, Track *tr);
double Get(int index); double Get(int index);
double Diff(double t, int index); wxInt64 PixelDiff(double t, int index);
int Find(double t, int i0, int i1); int Find(double t, int i0, int i1);
int Find(double t); int Find(double t);
bool SnapToPoints(Track *currentTrack, double t, bool rightEdge, bool SnapToPoints(Track *currentTrack, double t, bool rightEdge,
double *out_t); double *out_t);
double mEpsilon; double mEpsilon;
double mTolerance;
double mZoom;
SnapPointArray *mSnapPoints; SnapPointArray *mSnapPoints;
// Info for snap-to-time // Info for snap-to-time
NumericConverter mConverter; NumericConverter mConverter;
bool mSnapToTime; bool mSnapToTime;
const wxInt64 mPixelTolerance;
const ZoomInfo &mZoomInfo;
}; };
#endif #endif

View File

@ -2570,7 +2570,7 @@ void TrackPanel::SelectionHandleClick(wxMouseEvent & event,
delete mSnapManager; delete mSnapManager;
mSnapManager = new SnapManager(mTracks, NULL, mSnapManager = new SnapManager(mTracks, NULL,
mViewInfo->zoom, *mViewInfo,
4); // pixel tolerance 4); // pixel tolerance
mSnapLeft = -1; mSnapLeft = -1;
@ -4120,7 +4120,7 @@ void TrackPanel::StartSlide(wxMouseEvent & event)
delete mSnapManager; delete mSnapManager;
mSnapManager = new SnapManager(mTracks, mSnapManager = new SnapManager(mTracks,
&mCapturedClipArray, &mCapturedClipArray,
mViewInfo->zoom, *mViewInfo,
4, // pixel tolerance 4, // pixel tolerance
true); // don't snap to time true); // don't snap to time
mSnapLeft = -1; mSnapLeft = -1;

View File

@ -2204,7 +2204,7 @@ void AdornedRulerPanel::HandleSnapping()
delete mSnapManager; delete mSnapManager;
} }
mSnapManager = new SnapManager(mProject->GetTracks(), NULL, mSnapManager = new SnapManager(mProject->GetTracks(), NULL,
mViewInfo->zoom, *mViewInfo,
QUICK_PLAY_SNAP_PIXEL); QUICK_PLAY_SNAP_PIXEL);
bool snappedPoint, snappedTime; bool snappedPoint, snappedTime;
mIsSnapped = (mSnapManager->Snap(NULL, mQuickPlayPos, false, mIsSnapped = (mSnapManager->Snap(NULL, mQuickPlayPos, false,