1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-11-04 08:04:06 +01:00

More factoring of TimeShiftHandle::Drag

This commit is contained in:
Paul Licameli
2018-09-26 14:50:56 -04:00
parent 37b3968dd6
commit aa3c3a8d5a
2 changed files with 81 additions and 68 deletions

View File

@@ -405,7 +405,7 @@ UIHandle::Result TimeShiftHandle::Click
mSlideUpDownOnly = event.CmdDown() && !multiToolModeActive;
mRect = rect;
mMouseClickX = event.m_x;
mClipMoveState.mMouseClickX = event.m_x;
mSnapManager = std::make_shared<SnapManager>(trackList,
&viewInfo,
&mClipMoveState.capturedClipArray,
@@ -422,6 +422,77 @@ UIHandle::Result TimeShiftHandle::Click
}
namespace {
double FindDesiredSlideAmount(
const ViewInfo &viewInfo, wxCoord xx, const wxMouseEvent &event,
SnapManager *pSnapManager,
bool slideUpDownOnly, bool snapPreferRightEdge,
ClipMoveState &state,
Track &capturedTrack, Track &track )
{
if (slideUpDownOnly)
return 0.0;
else {
double desiredSlideAmount =
viewInfo.PositionToTime(event.m_x) -
viewInfo.PositionToTime(state.mMouseClickX);
double clipLeft = 0, clipRight = 0;
if (track.GetKind() == Track::Wave) {
WaveTrack *const mtw = static_cast<WaveTrack*>(&track);
const double rate = mtw->GetRate();
// set it to a sample point
desiredSlideAmount = rint(desiredSlideAmount * rate) / rate;
}
// Adjust desiredSlideAmount using SnapManager
if (pSnapManager) {
if (state.capturedClip) {
clipLeft = state.capturedClip->GetStartTime()
+ desiredSlideAmount;
clipRight = state.capturedClip->GetEndTime()
+ desiredSlideAmount;
}
else {
clipLeft = capturedTrack.GetStartTime() + desiredSlideAmount;
clipRight = capturedTrack.GetEndTime() + desiredSlideAmount;
}
auto results =
pSnapManager->Snap(&capturedTrack, clipLeft, false);
auto newClipLeft = results.outTime;
results =
pSnapManager->Snap(&capturedTrack, clipRight, false);
auto newClipRight = results.outTime;
// Only one of them is allowed to snap
if (newClipLeft != clipLeft && newClipRight != clipRight) {
// Un-snap the un-preferred edge
if (snapPreferRightEdge)
newClipLeft = clipLeft;
else
newClipRight = clipRight;
}
// Take whichever one snapped (if any) and compute the NEW desiredSlideAmount
state.snapLeft = -1;
state.snapRight = -1;
if (newClipLeft != clipLeft) {
const double difference = (newClipLeft - clipLeft);
desiredSlideAmount += difference;
state.snapLeft =
viewInfo.TimeToPosition(newClipLeft, xx);
}
else if (newClipRight != clipRight) {
const double difference = (newClipRight - clipRight);
desiredSlideAmount += difference;
state.snapRight =
viewInfo.TimeToPosition(newClipRight, xx);
}
}
return desiredSlideAmount;
}
}
struct TemporaryClipRemover {
TemporaryClipRemover( ClipMoveState &clipMoveState )
: state( clipMoveState )
@@ -514,69 +585,10 @@ UIHandle::Result TimeShiftHandle::Drag
}
mClipMoveState.hSlideAmount = 0.0;
double desiredSlideAmount;
if (mSlideUpDownOnly) {
desiredSlideAmount = 0.0;
}
else {
desiredSlideAmount =
viewInfo.PositionToTime(event.m_x) -
viewInfo.PositionToTime(mMouseClickX);
double clipLeft = 0, clipRight = 0;
if (pTrack->GetKind() == Track::Wave) {
WaveTrack *const mtw = static_cast<WaveTrack*>(pTrack.get());
const double rate = mtw->GetRate();
// set it to a sample point
desiredSlideAmount = rint(desiredSlideAmount * rate) / rate;
}
// Adjust desiredSlideAmount using SnapManager
if (mSnapManager.get()) {
if (mClipMoveState.capturedClip) {
clipLeft = mClipMoveState.capturedClip->GetStartTime()
+ desiredSlideAmount;
clipRight = mClipMoveState.capturedClip->GetEndTime()
+ desiredSlideAmount;
}
else {
clipLeft = mCapturedTrack->GetStartTime() + desiredSlideAmount;
clipRight = mCapturedTrack->GetEndTime() + desiredSlideAmount;
}
auto results =
mSnapManager->Snap(mCapturedTrack.get(), clipLeft, false);
auto newClipLeft = results.outTime;
results =
mSnapManager->Snap(mCapturedTrack.get(), clipRight, false);
auto newClipRight = results.outTime;
// Only one of them is allowed to snap
if (newClipLeft != clipLeft && newClipRight != clipRight) {
// Un-snap the un-preferred edge
if (mSnapPreferRightEdge)
newClipLeft = clipLeft;
else
newClipRight = clipRight;
}
// Take whichever one snapped (if any) and compute the NEW desiredSlideAmount
mClipMoveState.snapLeft = -1;
mClipMoveState.snapRight = -1;
if (newClipLeft != clipLeft) {
const double difference = (newClipLeft - clipLeft);
desiredSlideAmount += difference;
mClipMoveState.snapLeft =
viewInfo.TimeToPosition(newClipLeft, mRect.x);
}
else if (newClipRight != clipRight) {
const double difference = (newClipRight - clipRight);
desiredSlideAmount += difference;
mClipMoveState.snapRight =
viewInfo.TimeToPosition(newClipRight, mRect.x);
}
}
}
double desiredSlideAmount =
FindDesiredSlideAmount( viewInfo, mRect.x, event, mSnapManager.get(),
mSlideUpDownOnly, mSnapPreferRightEdge, mClipMoveState,
*mCapturedTrack, *pTrack );
// Scroll during vertical drag.
// EnsureVisible(pTrack); //vvv Gale says this has problems on Linux, per bug 393 thread. Revert for 2.0.2.
@@ -682,7 +694,7 @@ UIHandle::Result TimeShiftHandle::Drag
}
// Make the offset permanent; start from a "clean slate"
if( ok ) {
mMouseClickX = event.m_x;
mClipMoveState.mMouseClickX = event.m_x;
if (mClipMoveState.capturedClipIsSelection) {
// Slide the selection, too
viewInfo.selectedRegion.move( slide );
@@ -697,7 +709,7 @@ UIHandle::Result TimeShiftHandle::Drag
mDidSlideVertically = true;
// Make the offset permanent; start from a "clean slate"
mMouseClickX = event.m_x;
mClipMoveState.mMouseClickX = event.m_x;
}
// Not done yet, check for horizontal movement.

View File

@@ -30,6 +30,8 @@ struct ClipMoveState {
TrackClipArray capturedClipArray {};
wxInt64 snapLeft { -1 }, snapRight { -1 };
int mMouseClickX{};
void clear()
{
capturedClip = nullptr;
@@ -38,6 +40,7 @@ struct ClipMoveState {
hSlideAmount = 0;
capturedClipArray.clear();
snapLeft = snapRight = -1;
mMouseClickX = 0;
}
};
@@ -108,8 +111,6 @@ private:
bool mSnapPreferRightEdge{};
int mMouseClickX{};
// Handles snapping the selection boundaries or track boundaries to
// line up with existing tracks or labels. mSnapLeft and mSnapRight
// are the horizontal index of pixels to display user feedback