Skip to content

Commit

Permalink
Fix properties panel range issue, simplify knob implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Feb 22, 2025
1 parent 1161128 commit 47eebd6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Source/Components/DraggableNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ struct DraggableListNumber final : public DraggableNumber {

void mouseDrag(MouseEvent const& e) override
{
if (editor || !targetFound)
if (editor || !targetFound || e.getDistanceFromDragStart() < 1)
return;

// Hide cursor and set unbounded mouse movement
Expand Down
14 changes: 12 additions & 2 deletions Source/Components/PropertiesPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -983,11 +983,21 @@ class PropertiesPanel : public Component {
draggableNumber->onInteraction = onInteractionFn;

draggableNumber->onValueChange = [this](double const newValue){
property = std::clamp(newValue, min, max);
if(min != 0.0f || max != 0.0f) {
property = std::clamp(newValue, min, max);
}
else {
property = newValue;
}
};

draggableNumber->onReturnKey = [this](double const newValue) {
property = std::clamp(newValue, min, max);
if(min != 0.0f || max != 0.0f) {
property = std::clamp(newValue, min, max);
}
else {
property = newValue;
}
};

property.addListener(this);
Expand Down
110 changes: 14 additions & 96 deletions Source/Objects/KnobObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,16 +597,10 @@ class KnobObject final : public ObjectBase {
}
case hash("range"): {
if (atoms.size() >= 2) {
auto const newMin = atoms[0].getFloat();
auto const newMax = atoms[1].getFloat();
// we have to use our min/max as by the time we get the "range" message, it has already changed knb->x_min & knb->x_max!
auto const oldMin = ::getValue<float>(min);
auto const oldMax = ::getValue<float>(max);

updateRange();
updateDoubleClickValue();

updateKnobPosFromMinMax(oldMin, oldMax, newMin, newMax);
knob.setValue(getValue());
updateLabel();
}
break;
}
Expand Down Expand Up @@ -1006,20 +1000,6 @@ class KnobObject final : public ObjectBase {
return 127.0f;
}

void setMinimum(float const value)
{
if (auto knb = ptr.get<t_fake_knob>()) {
knb->x_min = value;
}
}

void setMaximum(float const value)
{
if (auto knb = ptr.get<t_fake_knob>()) {
knb->x_max = value;
}
}

void mouseDoubleClick(MouseEvent const& e) override
{
knob.doubleClicked();
Expand All @@ -1043,55 +1023,7 @@ class KnobObject final : public ObjectBase {
knob.setNumberOfTicks(numTicks);
knob.repaint();
}

void updateKnobPosFromMin(float const oldMin, float const oldMax, float const newMin)
{
updateKnobPosFromMinMax(oldMin, oldMax, newMin, oldMax);
}

void updateKnobPosFromMax(float const oldMin, float const oldMax, float const newMax)
{
updateKnobPosFromMinMax(oldMin, oldMax, oldMin, newMax);
}

void updateKnobPosFromMinMax(float const oldMin, float const oldMax, float const newMin, float const newMax)
{
// map current value to new range
float knobVal = knob.getValue();
if (!std::isfinite(knobVal))
knobVal = newMin;
float exp = 0.0f;

if (auto knb = ptr.get<t_fake_knob>()) {
exp = knb->x_exp;
} else {
return;
}

// TODO: this is probably a bit broken right now?

// if exponential mode, map current position factor into exponential
if (exp != 0.0f) {
if (exp > 0.0f)
knobVal = pow(knobVal, exp);
else
knobVal = 1 - pow(1 - knobVal, -exp);
}

auto const currentVal = jmap(knobVal, 0.0f, 1.0f, oldMin, oldMax);
auto newValNormalised = newMin == newMax ? newMin : jmap(currentVal, newMin, newMax, 0.0f, 1.0f);

// if exponential mode, remove exponential mapping from position
if (exp != 0.0f) {
if (exp > 0.0f)
newValNormalised = pow(newValNormalised, 1 / exp);
else
newValNormalised = 1 - pow(1 - newValNormalised, -1 / exp);
}

knob.setValue(std::clamp(newValNormalised, 0.0f, 1.0f));
}


void updateColours()
{
bgCol = convertColour(Colour::fromString(secondaryColour.toString()));
Expand All @@ -1115,41 +1047,27 @@ class KnobObject final : public ObjectBase {
}

object->updateBounds();
knob.setValue(getValue());
updateLabel();
} else if (value.refersToSameSourceAs(min)) {
float oldMinVal, oldMaxVal, newMinVal = ::getValue<float>(min);
// set new min value and update knob
if (auto knb = ptr.get<t_fake_knob>()) {
oldMinVal = static_cast<float>(knb->x_min);
oldMaxVal = static_cast<float>(knb->x_max);
} else {
return;
pd->sendDirectMessage(knb.get(), "range", {::getValue<float>(min), knb->x_max});
}

// set new min value and update knob
setMinimum(newMinVal);
knob.setValue(getValue());
updateRange();
updateDoubleClickValue();
updateKnobPosFromMin(oldMinVal, oldMaxVal, newMinVal);

if (auto knb = ptr.get<t_fake_knob>())
knb->x_arcstart = clipArcStart(::getValue<float>(arcStart), std::min(newMinVal, oldMaxVal), std::max(newMinVal, oldMaxVal));

updateLabel();
} else if (value.refersToSameSourceAs(max)) {
float oldMinVal, oldMaxVal, newMaxVal = ::getValue<float>(max);
// set new min value and update knob
if (auto knb = ptr.get<t_fake_knob>()) {
oldMinVal = static_cast<float>(knb->x_min);
oldMaxVal = static_cast<float>(knb->x_max);
} else {
return;
pd->sendDirectMessage(knb.get(), "range", {knb->x_min, ::getValue<float>(max)});
}

// set new min value and update knob
setMaximum(newMaxVal);
knob.setValue(getValue());
updateRange();
updateDoubleClickValue();

updateKnobPosFromMax(oldMinVal, oldMaxVal, newMaxVal);
if (auto knb = ptr.get<t_fake_knob>())
knb->x_arcstart = clipArcStart(::getValue<float>(arcStart), std::min(oldMinVal, newMaxVal), std::max(oldMinVal, newMaxVal));
updateLabel();

} else if (value.refersToSameSourceAs(initialValue)) {
updateDoubleClickValue();
if (auto knb = ptr.get<t_fake_knob>())
Expand Down

0 comments on commit 47eebd6

Please sign in to comment.