From 1918645d5387c36c3d6648013747f3a2fd0aead6 Mon Sep 17 00:00:00 2001 From: ericek111 Date: Wed, 25 Mar 2026 21:53:36 +0100 Subject: [PATCH] Cursors: optional snapping --- src/ui/Application.cpp | 20 ++++++++++---------- src/ui/Cursors.cpp | 6 ++++-- src/ui/Cursors.h | 1 + 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/ui/Application.cpp b/src/ui/Application.cpp index c8a4f52..52fb892 100644 --- a/src/ui/Application.cpp +++ b/src/ui/Application.cpp @@ -1112,17 +1112,17 @@ void Application::handleSpectrumInput(float posX, float posY, cursors_.hover = {true, freq, dB, bin}; } - // Left click: cursor A - if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) { - int peakBin = cursors_.findLocalPeak(spec, bin, 10); - double peakFreq = analyzer_.binToFreq(peakBin); - cursors_.setCursorA(peakFreq, spec[peakBin], peakBin); + // Left drag: cursor A + if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) { + int cBin = cursors_.snapToPeaks ? cursors_.findLocalPeak(spec, bin, 10) : bin; + double cFreq = analyzer_.binToFreq(cBin); + cursors_.setCursorA(cFreq, spec[cBin], cBin); } - // Right click: cursor B - if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) { - int peakBin = cursors_.findLocalPeak(spec, bin, 10); - double peakFreq = analyzer_.binToFreq(peakBin); - cursors_.setCursorB(peakFreq, spec[peakBin], peakBin); + // Right drag: cursor B + if (ImGui::IsMouseDown(ImGuiMouseButton_Right)) { + int cBin = cursors_.snapToPeaks ? cursors_.findLocalPeak(spec, bin, 10) : bin; + double cFreq = analyzer_.binToFreq(cBin); + cursors_.setCursorB(cFreq, spec[cBin], cBin); } { diff --git a/src/ui/Cursors.cpp b/src/ui/Cursors.cpp index 8aa0a91..a6d1787 100644 --- a/src/ui/Cursors.cpp +++ b/src/ui/Cursors.cpp @@ -83,7 +83,7 @@ void Cursors::draw(const SpectrumDisplay& specDisplay, float aDB = avgDBA(), bDB = avgDBB(); drawCursorMarker(cursorA, aDB, IM_COL32(255, 255, 0, 220)); - drawCursorMarker(cursorB, bDB, IM_COL32(0, 200, 255, 220)); + drawCursorMarker(cursorB, bDB, IM_COL32(100, 220, 255, 220)); // Draw labels at the top, touching the cursor's vertical line. // If the label would overflow the right edge, flip it to the left side. @@ -113,7 +113,7 @@ void Cursors::draw(const SpectrumDisplay& specDisplay, }; drawCursorLabel(cursorA, aDB, IM_COL32(255, 255, 0, 220), "A", 0); - drawCursorLabel(cursorB, bDB, IM_COL32(0, 200, 255, 220), "B", cursorA.active ? 1 : 0); + drawCursorLabel(cursorB, bDB, IM_COL32(100, 220, 255, 220), "B", cursorA.active ? 1 : 0); // Delta display (two lines, column-aligned on '=') if (showDelta && cursorA.active && cursorB.active) { @@ -156,6 +156,8 @@ void Cursors::drawPanel() { ImGui::Text("%s", dbuf); } + ImGui::Checkbox("Snap to peaks", &snapToPeaks); + // Averaging slider (logarithmic scale) ImGui::SetNextItemWidth(-1); ImGui::SliderInt("##avgcount", &avgCount, 1, 20000, avgCount == 1 ? "No avg" : "Avg: %d", diff --git a/src/ui/Cursors.h b/src/ui/Cursors.h index f3cd695..eb0f473 100644 --- a/src/ui/Cursors.h +++ b/src/ui/Cursors.h @@ -45,6 +45,7 @@ public: CursorInfo cursorA; CursorInfo cursorB; bool showDelta = true; + bool snapToPeaks = true; // Hover cursor (follows mouse, always active) CursorInfo hover;