Skip to content

Commit

Permalink
Add ability for UIKnob to have a mapping to "round" values in normali…
Browse files Browse the repository at this point in the history
…zed space. This allows to have special drag-modes that round values, depending on mouse button used.

Example in new Inner Pitch:

class InnerPitchKnob : UIImageKnob
{
public:
nothrow:
@nogc:

    this(UIContext context, KnobImage knobImage, Parameter parameter)
    {
        super(context, knobImage, parameter);
    }

    override double roundParamValue(double normalizedValue, MouseState mstate)
    {
        if (mstate.rightButtonDown)
        {
            float roundedPitch = cast(int)(0.5 + 48 * normalizedValue);
            roundedPitch /= 48;
            if (roundedPitch < 0)
                roundedPitch = 0;
            if (roundedPitch > 1)
                roundedPitch = 1;

            return roundedPitch; // special drag
        }
        else
        {
            return normalizedValue; // normal drag
        }
    }
}
  • Loading branch information
Guillaume Piolat committed Jan 22, 2024
1 parent e92fd08 commit c824c63
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions pbrwidgets/dplug/pbrwidgets/knob.d
Expand Up @@ -298,6 +298,17 @@ nothrow:
}
}

/// Override this to round the parameter normalized value to some particular values.
/// For example for a special drag movement with right-click or control-click.
/// Default does nothing.
/// Params:
/// normalizedValue The main parameter normalized value (0 to 1).
/// Returns: the mapped value in normalized space (0 to 1).
double roundParamValue(double normalizedValue, MouseState mstate)
{
return normalizedValue;
}

override Click onMouseClick(int x, int y, int button, bool isDoubleClick, MouseState mstate)
{
if (!containsPoint(x, y))
Expand All @@ -317,20 +328,21 @@ nothrow:
}

_normalizedValueWhileDragging = _param.getNormalized();
_displacementInHeightDebt = 0;
return Click.startDrag;
}

// Called when mouse drag this Element.
override void onMouseDrag(int x, int y, int dx, int dy, MouseState mstate)
{
float displacementInHeight = cast(float)(dy) / _position.height;
_displacementInHeightDebt += cast(float)(dy) / _position.height;

float modifier = 1.0f;
if (mstate.shiftPressed || mstate.ctrlPressed)
modifier *= 0.1f;

double oldParamValue = _normalizedValueWhileDragging;
double newParamValue = oldParamValue - displacementInHeight * modifier * _sensivity;
double newParamValue = oldParamValue - _displacementInHeightDebt * modifier * _sensivity;
if (mstate.altPressed)
newParamValue = _param.getNormalizedDefault();

Expand All @@ -356,6 +368,8 @@ nothrow:
if (newParamValue < 1)
_mousePosOnLast1Cross = -float.infinity;

newParamValue = roundParamValue(newParamValue, mstate);

if (newParamValue != oldParamValue)
{
if (auto fp = cast(FloatParameter)_param)
Expand All @@ -365,10 +379,12 @@ nothrow:
else
assert(false);
_normalizedValueWhileDragging = newParamValue;
_displacementInHeightDebt = 0;
}

}

// For lazy updates
// For lazy updates
override void onBeginDrag()
{
_param.beginParamEdit();
Expand Down Expand Up @@ -439,7 +455,11 @@ protected:

// Normalized value last set while dragging. Necessary for integer paramerers
// that may round that normalized value when setting the parameter.
float _normalizedValueWhileDragging;
float _normalizedValueWhileDragging;


// To support rounded mappings properly.
double _displacementInHeightDebt = 0.0;

/// Exists because public angle properties are given in a
/// different referential, where 0 is at the top
Expand Down

0 comments on commit c824c63

Please sign in to comment.