Skip to content

Commit

Permalink
Fix switch drag/hover/ghost
Browse files Browse the repository at this point in the history
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 surge-synthesizer#5801
  • Loading branch information
baconpaul committed Feb 14, 2022
1 parent a1f0cd5 commit 5227f2e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/common/SurgeStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
48 changes: 42 additions & 6 deletions src/surge-xt/gui/widgets/MultiSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -97,6 +97,31 @@ float MultiSwitch::coordinateToValue(int x, int y)
return 1.f * coordinateToSelection(x, y) / (rows * columns - 1);
}

juce::Point<float> 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))
Expand Down Expand Up @@ -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();
}
}
}

Expand All @@ -210,9 +240,15 @@ void MultiSwitch::mouseEnter(const juce::MouseEvent &event) { startHover(event.p

void MultiSwitch::startHover(const juce::Point<float> &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(); }
Expand Down
8 changes: 5 additions & 3 deletions src/surge-xt/gui/widgets/MultiSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> 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};
Expand All @@ -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
Expand Down

0 comments on commit 5227f2e

Please sign in to comment.