iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IGraphicsFaustUI.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
18#include <array>
19#include <functional>
20#include <memory>
21
22#include "IControls.h"
24#include "IVMeterControl.h"
25#include "ILEDControl.h"
26
27#include "IPlugFaust.h"
28
29BEGIN_IPLUG_NAMESPACE
30BEGIN_IGRAPHICS_NAMESPACE
31
46{
47public:
48 using Item = FaustUIItem;
49 using EType = FaustUIItem::EType;
50
56 using CreateControlFunc = std::function<IControl*(const Item& item, int paramIdx, const IVStyle& style)>;
57
59 using ControlBoundsFunc = std::function<IRECT(const Item& item, const IRECT& cell)>;
60
63 {
64 public:
65 ButtonControl(const IRECT& bounds, int paramIdx, const char* label, const IVStyle& style)
66 : IVButtonControl(bounds, nullptr, label, style, true, false)
67 {
68 SetParamIdx(paramIdx);
69 }
70
71 void OnMouseDown(float x, float y, const IMouseMod& mod) override
72 {
73 SetValue(1.);
74 SetDirty(true);
75 }
76
77 void OnMouseUp(float x, float y, const IMouseMod& mod) override
78 {
79 SetValue(0.);
80 SetDirty(true);
81 }
82 };
83
86 {
87 public:
88 ValueDisplayControl(const IRECT& bounds, const char* label, const IVStyle& style, double min, double max, const char* unit)
89 : IControl(bounds)
90 , IVectorBase(style)
91 , mMin(min)
92 , mMax(max)
93 , mUnit(unit)
94 {
95 mIgnoreMouse = true;
96 AttachIControl(this, label);
97 }
98
99 void Draw(IGraphics& g) override
100 {
101 DrawLabel(g);
102 WDL_String str;
103 str.SetFormatted(64, "%.2f %s", mMin + GetValue() * (mMax - mMin), mUnit.Get());
104 g.DrawText(mStyle.valueText, str.Get(), mWidgetBounds, &mBlend);
105 }
106
107 void OnResize() override
108 {
109 SetTargetRECT(MakeRects(mRECT));
110 SetDirty(false);
111 }
112
113 private:
114 double mMin, mMax;
115 WDL_String mUnit;
116 };
117
119 class BoxControl;
120
123 IGraphicsFaustUI(IPlugFaust& faust, const IVStyle& style = DEFAULT_STYLE)
124 : mFaust(faust)
125 , mConfig(std::make_shared<Config>())
126 {
127 mConfig->style = style;
128
129 for (int t = 0; t < kNumTypes; t++)
130 mConfig->createFuncs[t] = DefaultCreateControl;
131
132 mConfig->boundsFunc = DefaultControlBounds;
133 }
134
138 inline IControl* CreateControl(const IRECT& bounds);
139
141 void SetCreateControlFunc(EType type, CreateControlFunc func) { mConfig->createFuncs[static_cast<int>(type)] = func; }
142
144 void SetControlBoundsFunc(ControlBoundsFunc func) { mConfig->boundsFunc = func; }
145
147 void SetPadding(float padding) { mConfig->padding = padding; }
148
150 void SetTabBarHeight(float height) { mConfig->tabBarHeight = height; }
151
153 static IControl* DefaultCreateControl(const Item& item, int paramIdx, const IVStyle& style)
154 {
155 const char* label = item.label.c_str();
156 IControl* pControl = nullptr;
157
158 switch (item.type)
159 {
160 case EType::Button:
161 pControl = new ButtonControl(IRECT(), paramIdx, label, style);
162 break;
163 case EType::CheckButton:
164 pControl = new IVToggleControl(IRECT(), paramIdx, label, style, "Off", "On");
165 break;
166 case EType::HSlider:
167 case EType::VSlider:
168 {
169 const EDirection dir = item.type == EType::HSlider ? EDirection::Horizontal : EDirection::Vertical;
170
171 if (item.menu)
172 pControl = new IVMenuButtonControl(IRECT(), paramIdx, label, style);
173 else if (item.radio)
174 pControl = new IVRadioButtonControl(IRECT(), paramIdx, {}, label, style, EVShape::Ellipse, dir);
175 else if (item.knob)
176 pControl = new IVKnobControl(IRECT(), paramIdx, label, style, true);
177 else
178 pControl = new IVSliderControl(IRECT(), paramIdx, label, style, true, dir);
179 break;
180 }
181 case EType::NumEntry:
182 if (item.menu)
183 pControl = new IVMenuButtonControl(IRECT(), paramIdx, label, style);
184 else if (item.radio)
185 pControl = new IVRadioButtonControl(IRECT(), paramIdx, {}, label, style);
186 else if (item.knob)
187 pControl = new IVKnobControl(IRECT(), paramIdx, label, style, true);
188 else
189 pControl = new IVNumberBoxControl(IRECT(), paramIdx, nullptr, label, style);
190 break;
191 case EType::HBargraph:
192 case EType::VBargraph:
193 if (item.led)
194 pControl = new ILEDControl(IRECT());
195 else if (item.numerical)
196 pControl = new ValueDisplayControl(IRECT(), label, style, item.min, item.max, item.unit.c_str());
197 else
198 pControl = new IVMeterControl<1>(IRECT(), label, style, item.type == EType::HBargraph ? EDirection::Horizontal : EDirection::Vertical);
199 break;
200 default:
201 break;
202 }
203
204 if (pControl && item.tooltip.size())
205 pControl->SetTooltip(item.tooltip.c_str());
206
207 return pControl;
208 }
209
211 static IRECT DefaultControlBounds(const Item& item, const IRECT& cell)
212 {
213 auto constrain = [&cell](float maxW, float maxH) {
214 return cell.GetCentredInside(std::min(cell.W(), maxW), std::min(cell.H(), maxH));
215 };
216
217 switch (item.type)
218 {
219 case EType::Button:
220 case EType::CheckButton:
221 return constrain(150.f, 60.f);
222 case EType::HSlider:
223 if (item.knob) return constrain(100.f, 130.f);
224 if (item.menu) return constrain(150.f, 60.f);
225 return constrain(300.f, item.radio ? 100.f : 70.f);
226 case EType::VSlider:
227 if (item.knob) return constrain(100.f, 130.f);
228 if (item.menu) return constrain(150.f, 60.f);
229 return constrain(item.radio ? 150.f : 70.f, 300.f);
230 case EType::NumEntry:
231 if (item.knob) return constrain(100.f, 130.f);
232 return constrain(150.f, item.radio ? 150.f : 60.f);
233 case EType::HBargraph:
234 if (item.led) return constrain(30.f, 30.f);
235 return constrain(300.f, item.numerical ? 50.f : 60.f);
236 case EType::VBargraph:
237 if (item.led) return constrain(30.f, 30.f);
238 return constrain(item.numerical ? 150.f : 50.f, 300.f);
239 default:
240 return cell;
241 }
242 }
243
244private:
245 static constexpr int kNumTypes = static_cast<int>(EType::Soundfile) + 1;
246
247 struct Config
248 {
249 IVStyle style;
250 std::array<CreateControlFunc, kNumTypes> createFuncs;
251 ControlBoundsFunc boundsFunc;
252 float padding = 5.f;
253 float tabBarHeight = 25.f;
254 };
255
257 struct Context
258 {
259 Item root;
260 std::vector<int> plugParamIdx; // Faust parameter index -> IPlug parameter index
261 std::shared_ptr<const Config> config;
262
263 int GetPlugParamIdx(const Item& item) const
264 {
265 return (item.paramIdx > kNoParameter && item.paramIdx < static_cast<int>(plugParamIdx.size())) ? plugParamIdx[item.paramIdx] : kNoParameter;
266 }
267 };
268
269 static bool IsVisible(const Item& item)
270 {
271 return !item.hidden && item.type != EType::Soundfile;
272 }
273
274 static inline IControl* CreateItemControl(const std::shared_ptr<const Context>& pContext, const Item& item, const IRECT& bounds = IRECT());
275
276 IPlugFaust& mFaust;
277 std::shared_ptr<Config> mConfig;
278};
279
281{
282public:
283 BoxControl(const std::shared_ptr<const Context>& pContext, const Item& item, const IRECT& bounds)
284 : IContainerBase(bounds)
285 , IVectorBase(pContext->config->style)
286 , mContext(pContext)
287 , mItem(item)
288 {
289 mIgnoreMouse = true;
290 AttachIControl(this, item.label.c_str());
291 }
292
294 void HideLabel() { mShowLabel = false; }
295
296 void Draw(IGraphics& g) override
297 {
298 if (!HasLabel())
299 return;
300
301 DrawLabel(g);
302
303 if (mStyle.drawFrame)
304 g.DrawRoundRect(GetColor(kFR), mWidgetBounds, GetRoundedCornerRadius(mWidgetBounds), &mBlend, mStyle.frameThickness);
305 }
306
307 void OnAttached() override
308 {
309 for (const auto& child : mItem.children)
310 {
311 if (!IsVisible(child))
312 continue;
313
314 if (IControl* pControl = CreateItemControl(mContext, child))
315 {
316 AddChildControl(pControl, child.ctrlTag);
317 mChildItems.push_back(&child);
318 }
319 }
320
321 OnResize();
322 }
323
324 void OnResize() override
325 {
326 if (!GetUI()) // not attached yet
327 return;
328
329 SetTargetRECT(HasLabel() ? MakeRects(mRECT) : mRECT);
330
331 const int nChildren = NChildren();
332
333 if (!nChildren)
334 return;
335
336 const float padding = mContext->config->padding;
337 const IRECT area = (HasLabel() ? mWidgetBounds : mRECT).GetPadded(-padding);
338 const EDirection dir = mItem.type == EType::HBox ? EDirection::Horizontal : EDirection::Vertical;
339
340 // Each child gets space in proportion to the number of widgets it lays out in this direction
341 float totalWeight = 0.f;
342
343 for (const Item* pChildItem : mChildItems)
344 totalWeight += Weight(*pChildItem, dir);
345
346 float start = 0.f;
347
348 for (int i = 0; i < nChildren; i++)
349 {
350 const Item& childItem = *mChildItems[i];
351 const float end = start + Weight(childItem, dir) / totalWeight;
352 const IRECT cell = (dir == EDirection::Horizontal ? area.FracRectHorizontal(end - start).GetHShifted(area.W() * start)
353 : area.FracRectVertical(end - start, true).GetVShifted(area.H() * start)).GetPadded(-padding);
354 GetChild(i)->SetTargetAndDrawRECTs(childItem.IsGroup() ? cell : mContext->config->boundsFunc(childItem, cell));
355 start = end;
356 }
357 }
358
360 static float Weight(const Item& item, EDirection dir)
361 {
362 if (!item.IsGroup())
363 return 1.f;
364
365 const bool sum = (item.type == EType::HBox && dir == EDirection::Horizontal) || (item.type == EType::VBox && dir == EDirection::Vertical);
366 float weight = 0.f;
367
368 for (const auto& child : item.children)
369 {
370 if (!IsVisible(child))
371 continue;
372
373 const float childWeight = Weight(child, dir);
374 weight = sum ? weight + childWeight : std::max(weight, childWeight);
375 }
376
377 return std::max(weight, 1.f);
378 }
379
380private:
381 bool HasLabel() const { return mShowLabel && !mItem.label.empty(); }
382
383 std::shared_ptr<const Context> mContext;
384 const Item& mItem;
385 bool mShowLabel = true;
386 std::vector<const Item*> mChildItems;
387};
388
389IControl* IGraphicsFaustUI::CreateItemControl(const std::shared_ptr<const Context>& pContext, const Item& item, const IRECT& bounds)
390{
391 const Config& config = *pContext->config;
392
393 switch (item.type)
394 {
395 case EType::HBox:
396 case EType::VBox:
397 return new BoxControl(pContext, item, bounds);
398 case EType::TabBox:
399 {
400 std::vector<const Item*> visibleChildren;
401
402 for (const auto& child : item.children)
403 {
404 if (IsVisible(child))
405 visibleChildren.push_back(&child);
406 }
407
408 // A tab bar needs at least two tabs
409 if (visibleChildren.size() == 1)
410 return CreateItemControl(pContext, *visibleChildren[0], bounds);
411
412 PageMap pages;
413
414 for (const auto& child : item.children)
415 {
416 if (!IsVisible(child))
417 continue;
418
419 const Item* pChild = &child;
420
421 auto attachFunc = [pContext, pChild](IVTabPage* pPage, const IRECT& bounds) {
422 if (IControl* pControl = CreateItemControl(pContext, *pChild))
423 {
424 if (auto* pBox = pControl->As<BoxControl>())
425 pBox->HideLabel();
426
427 pPage->AddChildControl(pControl, pChild->ctrlTag);
428 }
429 };
430
431 auto resizeFunc = [pContext, pChild](IContainerBase* pPage, const IRECT& bounds) {
432 if (pPage->NChildren() == 1)
433 {
434 const IRECT cell = bounds.GetPadded(-pContext->config->padding);
435 pPage->GetChild(0)->SetTargetAndDrawRECTs(pChild->IsGroup() ? cell : pContext->config->boundsFunc(*pChild, cell));
436 }
437 };
438
439 pages.push_back({child.label.c_str(), new IVTabPage(attachFunc, resizeFunc, config.style, 0.)});
440 }
441
442 if (pages.empty())
443 return nullptr;
444
445 return new IVTabbedPagesControl(bounds, pages, item.label.c_str(), config.style, config.tabBarHeight, 1.f);
446 }
447 default:
448 {
449 const CreateControlFunc& createFunc = config.createFuncs[static_cast<int>(item.type)];
450 return createFunc ? createFunc(item, pContext->GetPlugParamIdx(item), config.style) : nullptr;
451 }
452 }
453}
454
456{
457 auto pContext = std::make_shared<Context>();
458 pContext->root = mFaust.GetUIRoot();
459 pContext->config = std::make_shared<const Config>(*mConfig);
460
461 for (int p = 0; p < mFaust.NParams(); p++)
462 pContext->plugParamIdx.push_back(mFaust.GetPlugParamIdx(p));
463
464 const Item& root = pContext->root;
465
466 if (root.children.empty())
467 return nullptr;
468
469 return CreateItemControl(pContext, root, bounds);
470}
471
472END_IGRAPHICS_NAMESPACE
473END_IPLUG_NAMESPACE
A collection of IControls for common UI widgets, such as knobs, sliders, switches.
std::vector< std::pair< const char *, IVTabPage * > > PageMap
Ordered list of (tab name, page) pairs.
IContainerBase allows a control to nest sub controls and it clips the drawing of those subcontrols In...
Definition: IControl.h:662
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
IGraphics * GetUI()
Definition: IControl.h:472
void SetTargetRECT(const IRECT &bounds)
Set the rectangular mouse tracking target area, within the graphics context for this control.
Definition: IControl.h:328
virtual void SetParamIdx(int paramIdx, int valIdx=0)
Set the index of a parameter that the control is linked to If you are calling this "manually" to reus...
Definition: IControl.cpp:115
IControl * SetTooltip(const char *str)
Set a tooltip for the control.
Definition: IControl.h:221
virtual void SetValue(double value, int valIdx=0)
Set one of the control's values.
Definition: IControl.cpp:147
double GetValue(int valIdx=0) const
Get the control's value.
Definition: IControl.cpp:153
void SetTargetAndDrawRECTs(const IRECT &bounds)
Set BOTH the draw rect and the target area, within the graphics context for this control.
Definition: IControl.h:332
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:198
static float Weight(const Item &item, EDirection dir)
The relative amount of space an item needs in a direction: widgets count as 1, boxes sum their childr...
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set.
void HideLabel()
Don't show the label, e.g.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
void OnResize() override
Called when IControl is constructed or resized using SetRect().
A momentary button, for Faust's button()
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
void OnMouseUp(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse up event on this control.
Displays a bargraph's value as text, for [style:numerical].
void OnResize() override
Called when IControl is constructed or resized using SetRect().
void Draw(IGraphics &g) override
Draw the control to the graphics context.
Builds an IGraphics user interface from the UI description of an IPlugFaust DSP, like Faust's own gen...
static IControl * DefaultCreateControl(const Item &item, int paramIdx, const IVStyle &style)
The default control for each widget type.
void SetTabBarHeight(float height)
Set the height of the tab bar for tab boxes.
IControl * CreateControl(const IRECT &bounds)
Create a control containing the whole Faust UI.
void SetControlBoundsFunc(ControlBoundsFunc func)
Replace the function that sizes the controls within their layout cells.
void SetPadding(float padding)
Set the padding between controls and around boxes.
IGraphicsFaustUI(IPlugFaust &faust, const IVStyle &style=DEFAULT_STYLE)
void SetCreateControlFunc(EType type, CreateControlFunc func)
Replace the function that creates the controls for a widget type.
std::function< IRECT(const Item &item, const IRECT &cell)> ControlBoundsFunc
Returns the bounds of a widget's control, given the cell that the layout allocated for it.
std::function< IControl *(const Item &item, int paramIdx, const IVStyle &style)> CreateControlFunc
Creates the control for a widget.
static IRECT DefaultControlBounds(const Item &item, const IRECT &cell)
Default control sizing: stops controls from stretching too much in large cells.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:87
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:732
virtual void DrawRoundRect(const IColor &color, const IRECT &bounds, float cornerRadius=5.f, const IBlend *pBlend=0, float thickness=1.f)
Draw a rounded rectangle to the graphics context.
Definition: IGraphics.cpp:2864
Glowing LED control.
Definition: ILEDControl.h:28
IPlugFaust is the base class for hosting a Faust DSP inside an iPlug2 plug-in.
Definition: IPlugFaust.h:106
int NParams() const
Definition: IPlugFaust.h:148
int GetPlugParamIdx(int faustParamIdx) const
Definition: IPlugFaust.cpp:265
const FaustUIItem & GetUIRoot() const
Definition: IPlugFaust.h:192
A vector button/momentary switch control.
Definition: IControls.h:54
A vector knob control drawn using graphics primitives.
Definition: IControls.h:263
A vector button that pops up a menu.
Definition: IControls.h:239
Vectorial multi-channel capable meter control, linear or log response.
A "meta control" for a number box with an Inc/Dec button It adds several child buttons if buttons = t...
A vector "radio buttons" switch control.
Definition: IControls.h:203
A vector slider control.
Definition: IControls.h:320
A control used as the base class for a tabbed page of subcontrols.
A control to manage tabbed pages of sub controls Basic usage example:
A vector toggle control.
Definition: IControls.h:92
A base interface to be combined with IControl for vectorial controls "IVControls",...
Definition: IControl.h:813
IRECT MakeRects(const IRECT &parent, bool hasHandle=false)
Calculate the rectangles for the various areas, depending on the style.
Definition: IControl.h:1215
float GetRoundedCornerRadius(const IRECT &bounds) const
Get the radius of rounded corners for a rectangle, based on the style roundness factor.
Definition: IControl.h:910
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:831
virtual void DrawLabel(IGraphics &g)
Draw the IVControl label text.
Definition: IControl.h:946
const IColor & GetColor(EVColor color) const
Get value of a specific EVColor in the IVControl.
Definition: IControl.h:857
A node in the description of a Faust DSP's user interface, as declared by its buildUserInterface() me...
Definition: IPlugFaust.h:68
Used to manage mouse modifiers i.e.
Used to manage a rectangular area, independent of draw class/platform.
IRECT GetCentredInside(const IRECT &sr) const
Get a rectangle the size of sr but with the same center point as this rectangle.
IRECT FracRectVertical(float frac, bool fromTop=false) const
Returns a new IRECT with a height that is multiplied by frac.
float W() const
IRECT FracRectHorizontal(float frac, bool rhs=false) const
Returns a new IRECT with a width that is multiplied by frac.
IRECT GetVShifted(float y) const
Get a copy of this rectangle translated on the Y axis.
float H() const
IRECT GetHShifted(float x) const
Get a copy of this rectangle translated on the X axis.
IRECT GetPadded(float padding) const
Get a copy of this IRECT with each value padded by padding N.B.
A struct encapsulating a set of properties used to configure IVControls.