1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-21 23:00:06 +02:00

Keep the current note centered when holding control to zoom

This commit is contained in:
Pokechu22 2017-07-04 01:02:43 -07:00 committed by Paul Licameli
parent 776e4dfdab
commit b46abbec62
3 changed files with 22 additions and 13 deletions

View File

@ -911,22 +911,26 @@ void NoteTrack::VScroll(int start, int end)
SetBottomNote(mStartBottomNote + delta); SetBottomNote(mStartBottomNote + delta);
} }
// Zoom the note track, centering the pitch at centerY, void NoteTrack::Zoom(const wxRect &rect, int y, int amount, bool center)
// positive amounts zoom in; negative amounts zoom out
void NoteTrack::Zoom(const wxRect &rect, int centerY, int amount)
{ {
// Construct track rectangle to map pitch to screen coordinates // Construct track rectangle to map pitch to screen coordinates
// Only y and height are needed: // Only y and height are needed:
wxRect trackRect(0, rect.GetY(), 1, rect.GetHeight()); wxRect trackRect(0, rect.GetY(), 1, rect.GetHeight());
PrepareIPitchToY(trackRect); PrepareIPitchToY(trackRect);
int centerPitch = YToIPitch(centerY); int clickedPitch = YToIPitch(y);
// zoom out by changing the pitch height -- a small integer // zoom out by changing the pitch height -- a small integer
mPitchHeight += amount; mPitchHeight += amount;
if (mPitchHeight <= 0) mPitchHeight = 1; if (mPitchHeight <= 0) mPitchHeight = 1;
PrepareIPitchToY(trackRect); // update because mPitchHeight changed PrepareIPitchToY(trackRect); // update because mPitchHeight changed
int newCenterPitch = YToIPitch(rect.GetY() + rect.GetHeight() / 2); if (center) {
// center the pitch that the user clicked on int newCenterPitch = YToIPitch(rect.GetY() + rect.GetHeight() / 2);
SetBottomNote(mBottomNote + (centerPitch - newCenterPitch)); // center the pitch that the user clicked on
SetBottomNote(mBottomNote + (clickedPitch - newCenterPitch));
} else {
int newClickedPitch = YToIPitch(y);
// align to keep the pitch that the user clicked on in the same place
SetBottomNote(mBottomNote + (clickedPitch - newClickedPitch));
}
} }
@ -940,7 +944,7 @@ void NoteTrack::ZoomTo(const wxRect &rect, int start, int end)
int temp = topPitch; topPitch = botPitch; botPitch = temp; int temp = topPitch; topPitch = botPitch; botPitch = temp;
} }
if (topPitch == botPitch) { // can't divide by zero, do something else if (topPitch == botPitch) { // can't divide by zero, do something else
Zoom(rect, start, 1); Zoom(rect, start, 1, true);
return; return;
} }
int trialPitchHeight = trackRect.height / (topPitch - botPitch); int trialPitchHeight = trackRect.height / (topPitch - botPitch);
@ -949,7 +953,7 @@ void NoteTrack::ZoomTo(const wxRect &rect, int start, int end)
} else if (trialPitchHeight == 0) { } else if (trialPitchHeight == 0) {
trialPitchHeight = 1; trialPitchHeight = 1;
} }
Zoom(rect, (start + end) / 2, trialPitchHeight - mPitchHeight); Zoom(rect, (start + end) / 2, trialPitchHeight - mPitchHeight, true);
} }
int NoteTrack::YToIPitch(int y) int NoteTrack::YToIPitch(int y)

View File

@ -127,9 +127,14 @@ class AUDACITY_DLL_API NoteTrack final
int GetBottomNote() const { return mBottomNote; } int GetBottomNote() const { return mBottomNote; }
int GetPitchHeight() const { return mPitchHeight; } int GetPitchHeight() const { return mPitchHeight; }
void SetPitchHeight(int h) { mPitchHeight = h; } void SetPitchHeight(int h) { mPitchHeight = h; }
void ZoomOut(const wxRect &rect, int y) { Zoom(rect, y, -1); } /// Zooms out by one unit
void ZoomIn(const wxRect &rect, int y) { Zoom(rect, y, 1); } void ZoomOut(const wxRect &rect, int y) { Zoom(rect, y, -1, true); }
void Zoom(const wxRect &rect, int centerY, int amount); /// Zooms in by one unit
void ZoomIn(const wxRect &rect, int y) { Zoom(rect, y, 1, true); }
/// Zoom the note track around y.
/// Positive amounts zoom in; negative amounts zoom out.
/// If center is true, the result will be centered at y.
void Zoom(const wxRect &rect, int y, int amount, bool center);
void ZoomTo(const wxRect &rect, int start, int end); void ZoomTo(const wxRect &rect, int start, int end);
int GetNoteMargin() const { return (mPitchHeight + 1) / 2; } int GetNoteMargin() const { return (mPitchHeight + 1) / 2; }
int GetOctaveHeight() const { return mPitchHeight * 12 + 2; } int GetOctaveHeight() const { return mPitchHeight * 12 + 2; }

View File

@ -236,7 +236,7 @@ unsigned NoteTrackVRulerControls::HandleWheelRotation
const auto nt = static_cast<NoteTrack*>(pTrack.get()); const auto nt = static_cast<NoteTrack*>(pTrack.get());
if (event.CmdDown() && !event.ShiftDown()) { if (event.CmdDown() && !event.ShiftDown()) {
nt->Zoom(evt.rect, evt.event.m_y, (int) (steps)); nt->Zoom(evt.rect, evt.event.m_y, (int) (steps), false);
} else if (!event.CmdDown() && event.ShiftDown()) { } else if (!event.CmdDown() && event.ShiftDown()) {
// Scroll some fixed number of notes, independent of zoom level or track height: // Scroll some fixed number of notes, independent of zoom level or track height:
static const int movement = 6; // 6 semitones is half an octave static const int movement = 6; // 6 semitones is half an octave