PDA

View Full Version : IPlug Spinner control


cc_
10-13-2009, 06:46 AM
As requested, here's my spinner control, a text control you change by dragging like a knob. It's all pretty obvious except for the altParameter - that can be used when you don't have a real plugin parameter, but want the control to display like a parameter - eg an enumerated field or an integer.

In IControl.h:


/* Spinner is text you can change like a knob. Usually it displays
* the value of the parameter it is linked to. If showParam == false
* it doesn't show the parameter, you can set what it displays just
* like a TextControl. If it isn't linked to a parameter you can
* specify a pointer to an IParam altParameter which it will use instead
* (having a parameter makes it much more useful because it can display
* enums, or integers or whatever).
* If altParameter is specified then it will be kept updated as the
* control changes - there's no locking, so this parameter should only
* be used from the gui code (but that's what you wanted or you would
* have used a real parameter)
*/
class ISpinnerControl : public ITextControl
{
public:

ISpinnerControl(IPlugBase* pPlug, IRECT* pR, int paramIdx,
IText* pText, IParam *altParam=NULL, bool showParam = true,
bool showParamLabel = true,
EDirection direction = kVertical,double gearing = DEFAULT_GEARING);
~ISpinnerControl() {}

virtual void OnMouseDrag(int x, int y, int dX, int dY, IMouseMod* pMod);
virtual void OnMouseWheel(int x, int y, IMouseMod* pMod, int d);
virtual bool Draw(IGraphics* pGraphics);

protected:
EDirection mDirection;
double mGearing;
bool mShowParam;
bool mShowParamLabel;
IParam *mAltParam;
};


In IControl.cpp:


ISpinnerControl::ISpinnerControl (IPlugBase* pPlug, IRECT* pR, int paramIdx,
IText* pText, IParam *altParam, bool showParam,
bool showParamLabel,
EDirection direction,double gearing)
: ITextControl(pPlug, pR, pText),
mAltParam(altParam),mShowParam(showParam),mShowPar amLabel(showParamLabel),
mDirection(direction), mGearing(gearing)
{
mParamIdx=paramIdx;
}

void ISpinnerControl::OnMouseDrag(int x, int y, int dX, int dY, IMouseMod* pMod)
{
double delta = (mDirection == kVertical) ?
(double) dY / (double) (mRECT.T - mRECT.B) / mGearing :
(double) dX / (double) (mRECT.R - mRECT.L) / mGearing;

mValue += delta;

SetDirty();
}

void ISpinnerControl::OnMouseWheel(int x, int y, IMouseMod* pMod, int d)
{
double step = 0.01;
if (mParamIdx >= 0) {
step = mPlug->GetParam(mParamIdx)->GetStep();
}
else if (mAltParam) {
step = mAltParam->GetStep();
}
// parameters of type double return a step of 0
if (step != 0.0) {
// 30/3/9: currently this doesn't work very well because
// the mouse wheel stuff in IPlug doesn't handle fractions
// as it should
mValue += d * step;
}
else {
if (pMod->C || pMod->S) {
mValue += 0.001 * d;
}
else {
mValue += 0.01 * d;
}
}
mValue = BOUNDED(mValue,0.0,1.0);
SetDirty();
}

bool ISpinnerControl::Draw(IGraphics* pGraphics)
{
if ( mAltParam ) {
mAltParam->SetNormalized(mValue);
}
// if it's linked to a paramter we can draw it using ICaptionControl
// otherwise just use the text stored ITextControl (which can
// be updated using SetTextFromPlug)

if (mShowParam) {
IParam* pParam= ( mParamIdx>=0 ) ? mPlug->GetParam(mParamIdx) :
mAltParam;
if ( pParam ) {
char cStr[32];
pParam->GetDisplayForHost(cStr);
mStr.Set(cStr);
if (mShowParamLabel) {
mStr.Append(" ");
mStr.Append(pParam->GetLabelForHost());
}
}
}

return ITextControl::Draw(pGraphics);
}



Hope it works, I had to modify it slightly because mine isn't in IControl, but that's were it makes sense to put it really.

Jeffos
10-13-2009, 12:41 PM
that's the most beautiful CC message I ever saw !
some feedback soon...

[edit] just in case... as it seems I'm not ready to do jokes in english, it means "Thanks a lot"
[edit2] the "fake param" idea also opens me some doors!

Astralp
12-20-2009, 01:46 PM
hi cc

This doesn't compile when I try to re-compile the IPlug lib:

IControl.cpp|477|error C2039: 'GetStep' : is not a member of 'IParam'

I assume that getstep() was a function you also added, I can't see it anywhere.

thanks for making this available, I'm new to IPlug so still finding my feet...

edit: I commented out the mouse wheel stuff and it has compiled so I'll go and play with it now.

Jeffos
12-21-2009, 01:00 AM
hi Astralp, and welcome!
cc just added a getter for the (already existing) attribute mStep in IParam.cpp / IParam.h:
double IParam::GetStep()
{
return mStep;
}

RRokkenAudio
12-21-2009, 01:39 AM
Now if i overwrite my icontrol with this, it will not mess anything else up right... AMAZING work... these should be added to iplug and updated imo

You are great!!!!!!!!

~Rob.

RRokkenAudio
12-21-2009, 02:22 AM
Still getting used to this, How would you set this up in the cpp file? I'm used to setting up controls and faders etc..
Still hard to put together the line of code(trying to figure out how to write it, based on the function/class)
~Rob.

cc_
12-22-2009, 02:36 AM
hi Astralp, and welcome!
cc just added a getter for the (already existing) attribute mStep in IParam.cpp / IParam.h:
double IParam::GetStep()
{
return mStep;
}

Thanks, I must have forgotten that step :)

Astralp
01-15-2010, 07:40 PM
Thanks, I couldn't actually get it to work, dragging did not change the value, however I've moved on to making my own controls and your code was very helpful in getting me started :)

I really like IPlug so far.

cc_
01-16-2010, 01:18 AM
That's strange, I'm not sure why it wouldn't work... check that you have the rectangle for the control is set correctly (if it has zero size the drag won't work) or maybe try setting a break point in the OnMouseDrag function to make sure it is getting called.

Jeffos
01-16-2010, 06:46 AM
yes, I confirm it works. cc's "observer" contrib is also very cool. again, thanks for that cc!!

RRokkenAudio
01-17-2010, 09:31 AM
Still having trouble figuring out how to use these darn things in the cpp file. What is the line to use?

~Rob.

Astralp
01-17-2010, 09:38 AM
hi CC

No the onmousedown isn't getting called, I forgot to mention that sorry.. I'll have another look at it when I get a chance, I've built some of my own controls now so I'm a bit more familiar with it all.

RRokkenAudio
11-13-2010, 09:38 AM
This would be cool if the mouse area was bigger. Right now you have to be right in the middle of the value to be able to drag it. Annoying.

Cool control though otherwise.

~Rob.