1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-04-30 15:49:41 +02:00

FreqWindow: Save cursor position as frequency, not pixel position

Fixes the cursor remaining on the same pixel but changing frequency,
when resizing the window horizontally with a cursor active (already
broken), or zooming/scrolling horizontally with a cursor active (only
possible in this branch).

Signed-off-by: nyanpasu64 <nyanpasu64@tuta.io>
This commit is contained in:
nyanpasu64 2021-07-11 02:36:55 -07:00 committed by Be
parent 65b7c100d6
commit 2e5a05016b
2 changed files with 33 additions and 10 deletions

View File

@ -956,6 +956,30 @@ void FrequencyPlotDialog::PlotMouseEvent(wxMouseEvent & event)
else
mFreqPlot->SetCursor(*mArrowCursor);
wxRect r = mPlotRect;
int width = r.width - 2;
if (
hNumberScale != NumberScale() &&
r.Contains(mMouseX, mMouseY) &&
(mMouseX!=0) &&
(mMouseX!=r.width-1)
) {
auto calcXPosFromMouseX = [&r, &width, &hNumberScale = this->hNumberScale](
int mouseX
) -> float {
float relativeMouseX = float(mouseX - (r.x + 1)) / float(width);
return hNumberScale.PositionToValue(relativeMouseX);
};
/// Frequency of the mouse pixel
mCursorXLeft = calcXPosFromMouseX(mMouseX);
/// Frequency at 1 pixel to the right
mCursorXRight = calcXPosFromMouseX(mMouseX + 1);
} else {
mCursorXLeft = NO_CURSOR;
mCursorXRight = NO_CURSOR;
}
mFreqPlot->Refresh(false);
}
}
@ -1023,18 +1047,11 @@ void FrequencyPlotDialog::PlotPaint(wxPaintEvent & event)
int width = r.width - 2;
// Find the peak nearest the cursor and plot it
if ( r.Contains(mMouseX, mMouseY) & (mMouseX!=0) & (mMouseX!=r.width-1) ) {
auto calcXPosFromMouseX = [&r, &width, &hNumberScale = this->hNumberScale](
int mouseX
) -> float {
float relativeMouseX = float(mouseX - (r.x + 1)) / float(width);
return hNumberScale.PositionToValue(relativeMouseX);
};
if ( mCursorXLeft != NO_CURSOR ) {
/// Frequency of the mouse pixel
float xPos = calcXPosFromMouseX(mMouseX);
float xPos = mCursorXLeft;
/// Frequency at 1 pixel to the right
float xPosNext = calcXPosFromMouseX(mMouseX + 1);
float xPosNext = mCursorXRight;
float peakAmplitude = 0;
float peakPos = mAnalyst->FindPeak(xPos, &peakAmplitude);

View File

@ -158,6 +158,12 @@ private:
int mMouseX;
int mMouseY;
static constexpr float NO_CURSOR = -1.f;
/// Frequency/period under the mouse cursor, if present.
float mCursorXLeft = NO_CURSOR;
/// Frequency/period 1 pixel to the right of the mouse cursor, if present.
float mCursorXRight = NO_CURSOR;
std::unique_ptr<SpectrumAnalyst> mAnalyst;
DECLARE_EVENT_TABLE()