View Single Post
Old 01-18-2010, 02:13 AM   #3
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 2,954
Default IDrawbarControl

Here's my organ drawbar control. It's a reversed vertical fader with 9 discrete steps (0..8). You can drag its handle/tip to move the drawbar, or you can click on the numbers (usually) displayed on the drawbar itself.

Code:
// Handles the GUI control for the drawbars. The value for len is the height
// of the drawbar, *not* including the tip/handle.

class IDrawbarControl: public IControl
{
public:
	IDrawbarControl(IPlugBase* pPlug, int x, int y, int len, int paramIdx, IBitmap* pBitmap): IControl(pPlug, &IRECT(x, y, x + pBitmap->W, y + pBitmap->H), paramIdx),
		mLen(len), mBitmap(*pBitmap) {}
	~IDrawbarControl() {}

	void OnMouseDown(int x, int y, IMouseMod* pMod)
	{
		if (pMod->R)
		{
			PromptUserInput();
			return;
		}

		y -= mRECT.T;
		if (y < int(mValue * (double)mLen))
		{
			mValue = mValue - floor((double)y / (double)mLen / 0.125) * 0.125;
			SetDirty();	
		}
	}

	void OnMouseDrag(int x, int y, int dX, int dY, IMouseMod* pMod)
	{
		mValue += (double)dY / (double)mLen;
		SetDirty();
	}

	bool Draw(IGraphics* pGraphics)
	{
		return pGraphics->DrawBitmap(&mBitmap, &mRECT, 0, int((1. - floor(mValue / 0.125 + 0.5) * 0.125) * (double)mLen), &mBlend);
	}

protected:
	int mLen;
	IBitmap mBitmap;
};
You should feed this control a single bitmap which contains the drawbar including the handle. You'll need to specify the length (height) of the drawbar (without the handle) when you attach it to the plug's GUI:

Code:
MyPlug::MyPlug(IPlugInstanceInfo instanceInfo): IPLUG_CTOR(kNumParams, 0, instanceInfo)
{
	...

	int width = 500, height = 426;
	IGraphics* pGraphics = MakeGraphics(this, width, height);

	// Add drawbar control
	IBitmap bitmap = pGraphics->LoadIBitmap(DRAWBAR_ID, DRAWBAR_FN);
	int x = 48, y = 86;
	int len = 62;
	pGraphics->AttachControl(new IDrawbarControl(this, x, y, len, kMyDrawbar,  &bitmap));

	AttachGraphics(pGraphics);

	...
}
I will also post my keyboard control, but not right now as I really need to get some work done.
Tale is offline   Reply With Quote