Create Custom iPlug2 Control

Anonymous

How do I make my own control?

iPlug2GPT logo
iPlug2GPT

Creating your own control in iPlug2 involves subclassing the IControl class and overriding specific methods to define your control's behavior and appearance. Here's a step-by-step guide on how to do this:

1. Include the Necessary Header

First, include the IControl.h header in your source file where you intend to define the custom control.

cpp
1#include "IControl.h"

2. Define Your Custom Control Class

Create a class that inherits from IControl. Override the Draw method to define how your control should be rendered. You can also override other event handling methods like OnMouseDown, OnMouseUp, OnMouseMove, etc., to define how your control responds to user interactions.

Here's an example of a simple custom control that draws a colored rectangle:

cpp
1class MyCustomControl : public IControl { 2public: 3 MyCustomControl(const IRECT& bounds, const IColor& color) 4 : IControl(bounds), mColor(color) {} 5 6 void Draw(IGraphics& g) override { 7 g.FillRect(mColor, mRECT); 8 } 9 10private: 11 IColor mColor; 12};

In this example, MyCustomControl takes a rectangle (IRECT) defining its bounds and a color (IColor) for the fill. The Draw method uses these to draw a simple rectangle.

3. Add Your Control to the Plugin UI

In your plugin's layout initialization function, create an instance of your custom control and add it to the graphics context. This is typically done in the constructor of your plugin class or a dedicated layout initialization method.

cpp
1mLayoutFunc = [&](IGraphics* pGraphics) { 2 // ... other UI setup ... 3 4 // Add your custom control 5 pGraphics->AttachControl(new MyCustomControl(bounds, COLOR_RED)); 6};

Ensure that bounds is an IRECT object defining where on the UI your control should appear.

4. Compile and Test

Compile your plugin and test the custom control in your plugin's UI. Make sure it behaves as expected and renders correctly.

Customizing Further

  • You can make your control interactive by overriding methods like OnMouseDown, OnMouseDrag, OnMouseUp, etc.
  • Use SetActionFunction to define actions that should occur when the control's value changes (e.g., modifying a parameter).
  • To make the control reflect a parameter's state, use SetParamIdx to link it to a specific parameter.

This is a basic template. Depending on what you want your control to do (display text, handle user input, animate, etc.), you might need to override additional methods or add more member variables. Remember, the key is to define how your control looks and behaves in the overridden methods of the IControl class.