From 5227f2e1a980c4a064467e033d40703a273589a4 Mon Sep 17 00:00:00 2001 From: baconpaul Date: Mon, 14 Feb 2022 17:01:05 -0500 Subject: [PATCH] Fix switch drag/hover/ghost When you were dragging things whose value caused a rebuild it went badly with hover state. Fix it all and clean it all up. While at it, make the supplied data path work on windows too Closes #5801 --- src/common/SurgeStorage.cpp | 4 ++ src/surge-xt/gui/widgets/MultiSwitch.cpp | 48 +++++++++++++++++++++--- src/surge-xt/gui/widgets/MultiSwitch.h | 8 ++-- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/common/SurgeStorage.cpp b/src/common/SurgeStorage.cpp index e9d98f358b2..6dc1dbfda16 100644 --- a/src/common/SurgeStorage.cpp +++ b/src/common/SurgeStorage.cpp @@ -266,6 +266,10 @@ SurgeStorage::SurgeStorage(std::string suppliedDataPath) : otherscene_clients(0) datapath = sst::plugininfra::paths::bestLibrarySharedFolderPathFor(sxt, true); } } + else + { + datapath = suppliedDataPath; + } // Portable - first check for installPath\\SurgeXTUserData if (auto path{installPath / L"SurgeXTUserData"}; fs::is_directory(path)) diff --git a/src/surge-xt/gui/widgets/MultiSwitch.cpp b/src/surge-xt/gui/widgets/MultiSwitch.cpp index 8d7dc9492d3..26ed8d271ff 100644 --- a/src/surge-xt/gui/widgets/MultiSwitch.cpp +++ b/src/surge-xt/gui/widgets/MultiSwitch.cpp @@ -68,7 +68,7 @@ void MultiSwitch::paint(juce::Graphics &g) } } -int MultiSwitch::coordinateToSelection(int x, int y) +int MultiSwitch::coordinateToSelection(int x, int y) const { double coefX = (double)getWidth() / (double)columns; double coefY = (double)getHeight() / (double)rows; @@ -87,7 +87,7 @@ int MultiSwitch::coordinateToSelection(int x, int y) return 0; } -float MultiSwitch::coordinateToValue(int x, int y) +float MultiSwitch::coordinateToValue(int x, int y) const { if (rows * columns <= 1) { @@ -97,6 +97,31 @@ float MultiSwitch::coordinateToValue(int x, int y) return 1.f * coordinateToSelection(x, y) / (rows * columns - 1); } +juce::Point MultiSwitch::valueToCoordinate(float val) const +{ + if (rows * columns <= 1) + return getLocalBounds().getCentre().toFloat(); + + auto b = getLocalBounds(); + if (rows == 1) + { + auto y = b.getCentreY() * 1.f; + auto x = b.getWidth() * (getIntegerValue() + 0.5f) / (columns); + return {x, y}; + } + else if (columns == 1) + { + auto x = b.getCentreX() * 1.f; + auto y = b.getHeight() * (getIntegerValue() + 0.5f) / (rows); + return {x, y}; + } + else + { + jassertfalse; + return getLocalBounds().getCentre().toFloat(); + } +} + void MultiSwitch::mouseDown(const juce::MouseEvent &event) { if (forwardedMainFrameMouseDowns(event)) @@ -189,9 +214,14 @@ void MultiSwitch::mouseDrag(const juce::MouseEvent &event) } int sel = coordinateToSelection(event.x, event.y); - hoverSelection = sel; - setValue(limit_range((float)sel / (rows * columns - 1), 0.f, 1.f)); - notifyValueChanged(); + auto nv = limit_range((float)sel / (rows * columns - 1), 0.f, 1.f); + if (getIntegerValueFrom(nv) != getIntegerValue()) + { + hoverSelection = sel; + + setValue(limit_range((float)sel / (rows * columns - 1), 0.f, 1.f)); + notifyValueChanged(); + } } } @@ -210,9 +240,15 @@ void MultiSwitch::mouseEnter(const juce::MouseEvent &event) { startHover(event.p void MultiSwitch::startHover(const juce::Point &p) { - hoverSelection = coordinateToSelection(p.x, p.y); + if (everDragged && isMouseDown) // don't change hover state during a drag + { + hoverSelection = getIntegerValue(); + isHovered = true; + return; + } isHovered = true; + hoverSelection = coordinateToSelection(p.x, p.y); } void MultiSwitch::mouseExit(const juce::MouseEvent &event) { endHover(); } diff --git a/src/surge-xt/gui/widgets/MultiSwitch.h b/src/surge-xt/gui/widgets/MultiSwitch.h index de5ed9d463a..adff085b949 100644 --- a/src/surge-xt/gui/widgets/MultiSwitch.h +++ b/src/surge-xt/gui/widgets/MultiSwitch.h @@ -55,12 +55,14 @@ struct MultiSwitch : public juce::Component, { return (int)(frameOffset + ((v * (float)(rows * columns - 1) + 0.5f))); } - int coordinateToSelection(int x, int y); - float coordinateToValue(int x, int y); + int coordinateToSelection(int x, int y) const; + float coordinateToValue(int x, int y) const; + juce::Point valueToCoordinate(float v) const; float value{0}; float getValue() const override { return value; } int getIntegerValue() const { return (int)(value * (float)(rows * columns - 1) + 0.5f); } + int getIntegerValueFrom(float v) const { return (int)(v * (float)(rows * columns - 1) + 0.5f); } void setValue(float f) override { value = f; } bool draggable{false}; @@ -83,7 +85,7 @@ struct MultiSwitch : public juce::Component, void focusGained(juce::Component::FocusChangeType cause) override { // fixme - probably use the location of the current element - startHover(getBounds().getBottomLeft().toFloat()); + startHover(valueToCoordinate(getValue())); repaint(); } void focusLost(juce::Component::FocusChangeType cause) override