1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-09-19 09:30:52 +02:00

Condense SeekLeftOrRight code further.

This commit is contained in:
James Crook 2018-03-14 13:05:16 +00:00
parent 7c4ca093f3
commit dab9ad812f
2 changed files with 213 additions and 262 deletions

View File

@ -3129,22 +3129,22 @@ void AudacityProject::OnSkipEnd(const CommandContext &WXUNUSED(context) )
void AudacityProject::OnSeekLeftShort(const CommandContext &WXUNUSED(context) ) void AudacityProject::OnSeekLeftShort(const CommandContext &WXUNUSED(context) )
{ {
OnCursorLeft( CURSOR_MOVE ); SeekLeftOrRight( DIRECTION_LEFT, CURSOR_MOVE );
} }
void AudacityProject::OnSeekRightShort(const CommandContext &WXUNUSED(context) ) void AudacityProject::OnSeekRightShort(const CommandContext &WXUNUSED(context) )
{ {
OnCursorRight( CURSOR_MOVE ); SeekLeftOrRight( DIRECTION_RIGHT, CURSOR_MOVE );
} }
void AudacityProject::OnSeekLeftLong(const CommandContext &WXUNUSED(context) ) void AudacityProject::OnSeekLeftLong(const CommandContext &WXUNUSED(context) )
{ {
OnCursorLeft( SELECTION_EXTEND ); SeekLeftOrRight( DIRECTION_LEFT, SELECTION_EXTEND );
} }
void AudacityProject::OnSeekRightLong(const CommandContext &WXUNUSED(context) ) void AudacityProject::OnSeekRightLong(const CommandContext &WXUNUSED(context) )
{ {
OnCursorRight( SELECTION_EXTEND ); SeekLeftOrRight( DIRECTION_RIGHT, SELECTION_EXTEND );
} }
void AudacityProject::OnSelToStart(const CommandContext &WXUNUSED(context) ) void AudacityProject::OnSelToStart(const CommandContext &WXUNUSED(context) )
@ -3532,19 +3532,33 @@ void AudacityProject::HandleListSelection(Track *t, bool shift, bool ctrl,
ModifyState(true); ModifyState(true);
} }
// If this returns true, then there was a key up, and nothing more to do,
void AudacityProject::OnCursorLeft(const CommandContext &context) // after this function has completed.
// (at most this function just does a ModifyState for the keyup)
bool AudacityProject::OnlyHandleKeyUp( const CommandContext &context )
{ {
auto evt = context.pEvt; auto evt = context.pEvt;
bool bKeyUp = (evt) && evt->GetEventType() == wxEVT_KEY_UP; bool bKeyUp = (evt) && evt->GetEventType() == wxEVT_KEY_UP;
OnCursorLeft( CURSOR_MOVE, bKeyUp );
if( IsAudioActive() )
return bKeyUp;
if( !bKeyUp )
return false;
ModifyState(false);
return true;
}
void AudacityProject::OnCursorLeft(const CommandContext &context)
{
if( !OnlyHandleKeyUp( context ) )
SeekLeftOrRight( DIRECTION_LEFT, CURSOR_MOVE);
} }
void AudacityProject::OnCursorRight(const CommandContext &context) void AudacityProject::OnCursorRight(const CommandContext &context)
{ {
auto evt = context.pEvt; if( !OnlyHandleKeyUp( context ) )
bool bKeyUp = (evt) && evt->GetEventType() == wxEVT_KEY_UP; SeekLeftOrRight( DIRECTION_RIGHT, CURSOR_MOVE);
OnCursorRight( CURSOR_MOVE, bKeyUp );
} }
void AudacityProject::OnCursorShortJumpLeft(const CommandContext &WXUNUSED(context) ) void AudacityProject::OnCursorShortJumpLeft(const CommandContext &WXUNUSED(context) )
@ -3569,40 +3583,36 @@ void AudacityProject::OnCursorLongJumpRight(const CommandContext &WXUNUSED(conte
void AudacityProject::OnSelSetExtendLeft(const CommandContext &WXUNUSED(context) ) void AudacityProject::OnSelSetExtendLeft(const CommandContext &WXUNUSED(context) )
{ {
OnBoundaryMove( true, false); OnBoundaryMove( DIRECTION_LEFT);
} }
void AudacityProject::OnSelSetExtendRight(const CommandContext &WXUNUSED(context) ) void AudacityProject::OnSelSetExtendRight(const CommandContext &WXUNUSED(context) )
{ {
OnBoundaryMove( false, false); OnBoundaryMove( DIRECTION_RIGHT);
} }
void AudacityProject::OnSelExtendLeft(const CommandContext &context) void AudacityProject::OnSelExtendLeft(const CommandContext &context)
{ {
auto evt = context.pEvt; if( !OnlyHandleKeyUp( context ) )
bool bKeyUp = (evt) && evt->GetEventType() == wxEVT_KEY_UP; SeekLeftOrRight( DIRECTION_LEFT, SELECTION_EXTEND );
OnCursorLeft( SELECTION_EXTEND, bKeyUp );
} }
void AudacityProject::OnSelExtendRight(const CommandContext &context) void AudacityProject::OnSelExtendRight(const CommandContext &context)
{ {
auto evt = context.pEvt; if( !OnlyHandleKeyUp( context ) )
bool bKeyUp = (evt) && evt->GetEventType() == wxEVT_KEY_UP; SeekLeftOrRight( DIRECTION_RIGHT, SELECTION_EXTEND );
OnCursorRight( SELECTION_EXTEND, bKeyUp );
} }
void AudacityProject::OnSelContractLeft(const CommandContext &context) void AudacityProject::OnSelContractLeft(const CommandContext &context)
{ {
auto evt = context.pEvt; if( !OnlyHandleKeyUp( context ) )
bool bKeyUp = (evt) && evt->GetEventType() == wxEVT_KEY_UP; SeekLeftOrRight( DIRECTION_LEFT, SELECTION_CONTRACT );
OnCursorRight( SELECTION_CONTRACT, bKeyUp );
} }
void AudacityProject::OnSelContractRight(const CommandContext &context) void AudacityProject::OnSelContractRight(const CommandContext &context)
{ {
auto evt = context.pEvt; if( !OnlyHandleKeyUp( context ) )
bool bKeyUp = (evt) && evt->GetEventType() == wxEVT_KEY_UP; SeekLeftOrRight( DIRECTION_RIGHT, SELECTION_CONTRACT );
OnCursorLeft( SELECTION_CONTRACT, bKeyUp );
} }
#include "tracks/ui/TimeShiftHandle.h" #include "tracks/ui/TimeShiftHandle.h"
@ -9014,165 +9024,193 @@ void AudacityProject::OnFullScreen(const CommandContext &WXUNUSED(context) )
mCommandManager.Check(wxT("FullScreenOnOff"), bChecked); mCommandManager.Check(wxT("FullScreenOnOff"), bChecked);
} }
void AudacityProject::OnCursorLeft(SelectionOperation operation, bool keyup)
{
// PRL: What I found and preserved, strange though it be:
// During playback: jump depends on preferences and is independent of the zoom
// and does not vary if the key is held
// Else: jump depends on the zoom and gets bigger if the key is held
SeekLeftOrRight(-1.0, operation, keyup);
}
void AudacityProject::OnCursorRight(SelectionOperation operation, bool keyup)
{
// PRL: What I found and preserved, strange though it be:
// During playback: jump depends on preferences and is independent of the zoom
// and does not vary if the key is held
// Else: jump depends on the zoom and gets bigger if the key is held
SeekLeftOrRight(1.0, operation, keyup);
}
// Handle small cursor and play head movements // Handle small cursor and play head movements
void AudacityProject::SeekLeftOrRight void AudacityProject::SeekLeftOrRight
(double direction, SelectionOperation operation, bool keyup) (double direction, SelectionOperation operation)
{ {
if (IsAudioActive()) // PRL: What I found and preserved, strange though it be:
{ // During playback: jump depends on preferences and is independent of the zoom
if (keyup) // and does not vary if the key is held
return; // Else: jump depends on the zoom and gets bigger if the key is held
if (operation == CURSOR_MOVE) if( IsAudioActive() )
SeekAudio(mSeekShort * direction); {
else if (operation == SELECTION_EXTEND) if( operation == CURSOR_MOVE )
SeekAudio(mSeekLong * direction); SeekWhenAudioActive(mSeekShort * direction);
else if( operation == SELECTION_EXTEND )
SeekWhenAudioActive(mSeekLong * direction);
// Note: no action for CURSOR_CONTRACT
return;
}
return; // If the last adjustment was very recent, we are
} // holding the key down and should move faster.
const wxLongLong curtime = ::wxGetLocalTimeMillis();
enum { MIN_INTERVAL = 50 };
const bool fast = (curtime - mLastSelectionAdjustment < MIN_INTERVAL);
if (keyup) mLastSelectionAdjustment = curtime;
{
ModifyState(false);
return;
}
// If the last adjustment was very recent, we are // How much faster should the cursor move if shift is down?
// holding the key down and should move faster. enum { LARGER_MULTIPLIER = 4 };
const wxLongLong curtime = ::wxGetLocalTimeMillis(); const double seekStep = (fast ? LARGER_MULTIPLIER : 1.0) * direction;
enum { MIN_INTERVAL = 50 };
const bool fast = (curtime - mLastSelectionAdjustment < MIN_INTERVAL);
mLastSelectionAdjustment = curtime; SeekWhenAudioInactive( seekStep, TIME_UNIT_PIXELS, operation);
// How much faster should the cursor move if shift is down?
enum { LARGER_MULTIPLIER = 4 };
const double seekStep = (fast ? LARGER_MULTIPLIER : 1.0) * direction;
int snapToTime = GetSnapTo();
SeekQuiet(seekStep, TIME_UNIT_PIXELS, snapToTime, operation);
} }
void AudacityProject::SeekAudio(double seekStep) void AudacityProject::SeekWhenAudioActive(double seekStep)
{ {
#ifdef EXPERIMENTAL_IMPROVED_SEEKING #ifdef EXPERIMENTAL_IMPROVED_SEEKING
if (gAudioIO->GetLastPlaybackTime() < mLastSelectionAdjustment) { if (gAudioIO->GetLastPlaybackTime() < mLastSelectionAdjustment) {
// Allow time for the last seek to output a buffer before // Allow time for the last seek to output a buffer before
// discarding samples again // discarding samples again
// Do not advance mLastSelectionAdjustment // Do not advance mLastSelectionAdjustment
return; return;
} }
#endif #endif
mLastSelectionAdjustment = ::wxGetLocalTimeMillis(); mLastSelectionAdjustment = ::wxGetLocalTimeMillis();
gAudioIO->SeekStream(seekStep); gAudioIO->SeekStream(seekStep);
} }
void AudacityProject::SeekQuiet
(double seekStep, TimeUnit timeUnit, int snapToTime, void AudacityProject::OnBoundaryMove(int step)
SelectionOperation operation)
{ {
const double t0 = mViewInfo.selectedRegion.t0(); // step is negative, then is moving left. step positive, moving right.
const double t1 = mViewInfo.selectedRegion.t1(); // Move the left/right selection boundary, to expand the selection
const double end = mTracks->GetEndTime();
if (operation == SELECTION_CONTRACT) // If the last adjustment was very recent, we are
{ // holding the key down and should move faster.
// Contract selection wxLongLong curtime = ::wxGetLocalTimeMillis();
// Reduce and constrain (counter-intuitive) int pixels = step;
if (seekStep < 0) if( curtime - mLastSelectionAdjustment < 50 )
{ {
mViewInfo.selectedRegion.setT1( pixels *= 4;
std::max(t0, OffsetTime(t1, seekStep, timeUnit, snapToTime))); }
mLastSelectionAdjustment = curtime;
// Make sure it's visible. // we used to have a parameter boundaryContract to say if expanding or contracting.
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t1()); // it is no longer needed.
} bool bMoveT0 = (step < 0 );// ^ boundaryContract ;
else
{
mViewInfo.selectedRegion.setT0(
std::min(t1, OffsetTime(t0, seekStep, timeUnit, snapToTime)));
// Make sure NEW position is in view. if( IsAudioActive() )
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t0()); {
} double indicator = gAudioIO->GetStreamTime();
GetTrackPanel()->Refresh(false); if( bMoveT0 )
} mViewInfo.selectedRegion.setT0(indicator, false);
else if (operation == SELECTION_EXTEND) else
{ mViewInfo.selectedRegion.setT1(indicator);
// Extend selection
// Expand and constrain
if (seekStep < 0)
{
mViewInfo.selectedRegion.setT0(
std::max(0.0, OffsetTime(t0, seekStep, timeUnit, snapToTime)));
// Make sure it's visible. ModifyState(false);
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t0()); GetTrackPanel()->Refresh(false);
} return;
else }
{
mViewInfo.selectedRegion.setT1(
std::min(end, OffsetTime(t1, seekStep, timeUnit, snapToTime)));
// Make sure NEW position is in view. const double t0 = mViewInfo.selectedRegion.t0();
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t1()); const double t1 = mViewInfo.selectedRegion.t1();
} const double end = mTracks->GetEndTime();
GetTrackPanel()->Refresh(false);
}
else if (operation == CURSOR_MOVE)
{
// Move the cursor
// Already in cursor mode?
if (mViewInfo.selectedRegion.isPoint())
{
// Move and constrain
mViewInfo.selectedRegion.setT0(
std::max(0.0,
std::min(end,
OffsetTime(t0, seekStep, timeUnit, snapToTime))),
false // do not swap selection boundaries
);
mViewInfo.selectedRegion.collapseToT0();
// Move the visual cursor, avoiding an unnecessary complete redraw double newT = mViewInfo.OffsetTimeByPixels( bMoveT0 ? t0 : t1, pixels);
GetTrackPanel()->DrawOverlays(false); // constrain to be in the track limits.
GetRulerPanel()->DrawOverlays(false); newT = std::max( 0.0, newT );
newT = std::min( newT, end);
// optionally constrain to be a contraction, i.e. so t0/t1 do not cross over
//if( boundaryContract )
// newT = bMoveT0 ? std::min( t1, newT ) : std::max( t0, newT );
// This updates the selection shown on the selection bar, and the play region // Actually move
TP_DisplaySelection(); if( bMoveT0 )
} mViewInfo.selectedRegion.setT0( newT );
else else
{ mViewInfo.selectedRegion.setT1( newT );
// Transition to cursor mode.
if (seekStep < 0)
mViewInfo.selectedRegion.collapseToT0();
else
mViewInfo.selectedRegion.collapseToT1();
GetTrackPanel()->Refresh(false);
}
// Make sure NEW position is in view // Ensure it is visible, and refresh.
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t1()); GetTrackPanel()->ScrollIntoView(newT);
} GetTrackPanel()->Refresh(false);
ModifyState(false);
}
void AudacityProject::SeekWhenAudioInactive
(double seekStep, TimeUnit timeUnit,
SelectionOperation operation)
{
if( operation == CURSOR_MOVE )
{
MoveWhenAudioInactive( seekStep, timeUnit);
return;
}
int snapToTime = GetSnapTo();
const double t0 = mViewInfo.selectedRegion.t0();
const double t1 = mViewInfo.selectedRegion.t1();
const double end = mTracks->GetEndTime();
// Is it t0 or t1 moving?
bool bMoveT0 = ( operation == SELECTION_CONTRACT ) ^ ( seekStep < 0 );
// newT is where we want to move to
double newT = OffsetTime( bMoveT0 ? t0 : t1, seekStep, timeUnit, snapToTime);
// constrain to be in the track limits.
newT = std::max( 0.0, newT );
newT = std::min( newT, end);
// optionally constrain to be a contraction, i.e. so t0/t1 do not cross over
if( operation == SELECTION_CONTRACT )
newT = bMoveT0 ? std::min( t1, newT ) : std::max( t0, newT );
// Actually move
if( bMoveT0 )
mViewInfo.selectedRegion.setT0( newT );
else
mViewInfo.selectedRegion.setT1( newT );
// Ensure it is visible, and refresh.
GetTrackPanel()->ScrollIntoView(newT);
GetTrackPanel()->Refresh(false);
}
// Moving a cursor, and collapsed selection.
void AudacityProject::MoveWhenAudioInactive
(double seekStep, TimeUnit timeUnit)
{
// If TIME_UNIT_SECONDS, snap-to will be off.
int snapToTime = GetSnapTo();
const double t0 = mViewInfo.selectedRegion.t0();
const double t1 = mViewInfo.selectedRegion.t1();
const double end = mTracks->GetEndTime();
// Move the cursor
// Already in cursor mode?
if( mViewInfo.selectedRegion.isPoint() )
{
double newT = OffsetTime(t0, seekStep, timeUnit, snapToTime);
// constrain.
newT = std::max(0.0, newT);
newT = std::min(newT, end);
// Move
mViewInfo.selectedRegion.setT0(
newT,
false); // do not swap selection boundaries
mViewInfo.selectedRegion.collapseToT0();
// Move the visual cursor, avoiding an unnecessary complete redraw
GetTrackPanel()->DrawOverlays(false);
GetRulerPanel()->DrawOverlays(false);
// This updates the selection shown on the selection bar, and the play region
TP_DisplaySelection();
} else
{
// Transition to cursor mode.
if( seekStep < 0 )
mViewInfo.selectedRegion.collapseToT0();
else
mViewInfo.selectedRegion.collapseToT1();
GetTrackPanel()->Refresh(false);
}
// Make sure NEW position is in view
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t1());
return;
} }
double AudacityProject::OffsetTime double AudacityProject::OffsetTime
@ -9211,111 +9249,17 @@ double AudacityProject::GridMove(double t, int minPix)
return result; return result;
} }
void AudacityProject::OnBoundaryMove(bool left, bool boundaryContract)
{
// Move the left/right selection boundary, to either expand or contract the selection
// left=true: operate on left boundary; left=false: operate on right boundary
// boundaryContract=true: contract region; boundaryContract=false: expand region.
// If the last adjustment was very recent, we are
// holding the key down and should move faster.
wxLongLong curtime = ::wxGetLocalTimeMillis();
int pixels = 1;
if( curtime - mLastSelectionAdjustment < 50 )
{
pixels = 4;
}
mLastSelectionAdjustment = curtime;
if (IsAudioActive())
{
double indicator = gAudioIO->GetStreamTime();
if (left)
mViewInfo.selectedRegion.setT0(indicator, false);
else
mViewInfo.selectedRegion.setT1(indicator);
ModifyState(false);
GetTrackPanel()->Refresh(false);
}
else
{
// BOUNDARY MOVEMENT
// Contract selection from the right to the left
if( boundaryContract )
{
if (left) {
// Reduce and constrain left boundary (counter-intuitive)
// Move the left boundary by at most the desired number of pixels,
// but not past the right
mViewInfo.selectedRegion.setT0(
std::min(mViewInfo.selectedRegion.t1(),
mViewInfo.OffsetTimeByPixels(
mViewInfo.selectedRegion.t0(),
pixels)));
// Make sure it's visible
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t0());
}
else
{
// Reduce and constrain right boundary (counter-intuitive)
// Move the right boundary by at most the desired number of pixels,
// but not past the left
mViewInfo.selectedRegion.setT1(
std::max(mViewInfo.selectedRegion.t0(),
mViewInfo.OffsetTimeByPixels(
mViewInfo.selectedRegion.t1(),
-pixels)));
// Make sure it's visible
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t1());
}
}
// BOUNDARY MOVEMENT
// Extend selection toward the left
else
{
if (left) {
// Expand and constrain left boundary
mViewInfo.selectedRegion.setT0(
std::max(0.0,
mViewInfo.OffsetTimeByPixels(
mViewInfo.selectedRegion.t0(),
-pixels)));
// Make sure it's visible
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t0());
}
else
{
// Expand and constrain right boundary
const double end = mTracks->GetEndTime();
mViewInfo.selectedRegion.setT1(
std::min(end,
mViewInfo.OffsetTimeByPixels(
mViewInfo.selectedRegion.t1(),
pixels)));
// Make sure it's visible
GetTrackPanel()->ScrollIntoView(mViewInfo.selectedRegion.t1());
}
}
GetTrackPanel()->Refresh( false );
ModifyState(false);
}
}
// Move the cursor forward or backward, while paused or while playing. // Move the cursor forward or backward, while paused or while playing.
void AudacityProject::OnCursorMove(double seekStep) void AudacityProject::OnCursorMove(double seekStep)
{ {
if (IsAudioActive()) { if (IsAudioActive()) {
SeekAudio(seekStep); SeekWhenAudioActive(seekStep);
} }
else else
{ {
mLastSelectionAdjustment = ::wxGetLocalTimeMillis(); mLastSelectionAdjustment = ::wxGetLocalTimeMillis();
SeekQuiet(seekStep, TIME_UNIT_SECONDS, SNAP_OFF, CURSOR_MOVE); MoveWhenAudioInactive(seekStep, TIME_UNIT_SECONDS);
} }
ModifyState(false); ModifyState(false);

View File

@ -550,25 +550,32 @@ enum SelectionOperation {
CURSOR_MOVE CURSOR_MOVE
}; };
enum CursorDirection {
DIRECTION_LEFT = -1,
DIRECTION_RIGHT = +1
};
enum TimeUnit { enum TimeUnit {
TIME_UNIT_SECONDS, TIME_UNIT_SECONDS,
TIME_UNIT_PIXELS TIME_UNIT_PIXELS
}; };
void OnCursorLeft(SelectionOperation operation, bool keyup = false); bool OnlyHandleKeyUp( const CommandContext &context );
void OnCursorRight(SelectionOperation operation, bool keyup = false);
void OnCursorMove(double seekStep); void OnCursorMove(double seekStep);
void OnBoundaryMove(bool left, bool boundaryContract); void OnBoundaryMove(int step);
// Handle small cursor and play head movements // Handle small cursor and play head movements
void SeekLeftOrRight void SeekLeftOrRight
(double direction, SelectionOperation operation, bool keyup); (double direction, SelectionOperation operation);
void SeekAudio(double seekStep); void SeekWhenAudioActive(double seekStep);
void SeekWhenAudioInactive
void SeekQuiet (double seekStep, TimeUnit timeUnit,
(double seekStep, TimeUnit timeUnit, int snapToTime,
SelectionOperation operation); SelectionOperation operation);
void MoveWhenAudioInactive
(double seekStep, TimeUnit timeUnit);
double OffsetTime(double t, double offset, TimeUnit timeUnit, int snapToTime); double OffsetTime(double t, double offset, TimeUnit timeUnit, int snapToTime);