iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IVPresetManagerControls.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
19#include "IControl.h"
20
21#include "IPlugPluginBase.h"
22
23BEGIN_IPLUG_NAMESPACE
24BEGIN_IGRAPHICS_NAMESPACE
25
30 , public IVectorBase
31{
32public:
33 enum class ESubControl
34 {
35 LeftButton = 0,
36 RightButton,
37 PresetMenu
38 };
39
40 IVBakedPresetManagerControl(const IRECT& bounds, const char* label = "",
41 const IVStyle& style = DEFAULT_STYLE.WithDrawShadows(false).WithLabelText(DEFAULT_LABEL_TEXT.WithVAlign(EVAlign::Middle)))
42 : IContainerBase(bounds)
43 , IVectorBase(style)
44 {
45 AttachIControl(this, label);
46 mIgnoreMouse = true;
47 }
48
49 void Draw(IGraphics& g) override
50 {
51 DrawLabel(g);
52 }
53
54 void RestorePreset(IPluginBase* pluginBase, int presetIdx)
55 {
56 pluginBase->RestorePreset(presetIdx);
57
58 WDL_String str;
59 str.SetFormatted(32, "Preset %i: %s", presetIdx + 1, pluginBase->GetPresetName(presetIdx));
60 mPresetNameButton->SetLabelStr(str.Get());
61 }
62
63 void OnPopupMenuSelection(IPopupMenu* pSelectedMenu, int valIdx) override
64 {
65 if (pSelectedMenu)
66 {
67 IPopupMenu::Item* pItem = pSelectedMenu->GetChosenItem();
68
69 if (pItem)
70 {
71 IPluginBase* pluginBase = dynamic_cast<IPluginBase*>(GetDelegate());
72 RestorePreset(pluginBase, pSelectedMenu->GetChosenItemIdx());
73 }
74 }
75 }
76
77 void OnAttached() override
78 {
79 auto prevPresetFunc = [&](IControl* pCaller) {
80 IPluginBase* pluginBase = dynamic_cast<IPluginBase*>(pCaller->GetDelegate());
81
82 int presetIdx = pluginBase->GetCurrentPresetIdx();
83 int nPresets = pluginBase->NPresets();
84
85 presetIdx--;
86
87 if (presetIdx < 0)
88 presetIdx = nPresets - 1;
89
90 RestorePreset(pluginBase, presetIdx);
91 };
92
93 auto nextPresetFunc = [&](IControl* pCaller) {
94 IPluginBase* pluginBase = dynamic_cast<IPluginBase*>(pCaller->GetDelegate());
95
96 int presetIdx = pluginBase->GetCurrentPresetIdx();
97 int nPresets = pluginBase->NPresets();
98
99 presetIdx++;
100
101 if (presetIdx >= nPresets)
102 presetIdx = 0;
103
104 RestorePreset(pluginBase, presetIdx);
105 };
106
107 auto choosePresetFunc = [&](IControl* pCaller) {
108 mMenu.Clear();
109
110 IPluginBase* pluginBase = dynamic_cast<IPluginBase*>(pCaller->GetDelegate());
111
112 int currentPresetIdx = pluginBase->GetCurrentPresetIdx();
113 int nPresets = pluginBase->NPresets();
114
115 for (int i = 0; i < nPresets; i++)
116 {
117 const char* str = pluginBase->GetPresetName(i);
118 if (i == currentPresetIdx)
119 mMenu.AddItem(str, -1, IPopupMenu::Item::kChecked);
120 else
121 mMenu.AddItem(str);
122 }
123
124 pCaller->GetUI()->CreatePopupMenu(*this, mMenu, pCaller->GetRECT());
125 };
126
127 AddChildControl(new IVButtonControl(IRECT(), SplashClickActionFunc, "<", mStyle))
128 ->SetAnimationEndActionFunction(prevPresetFunc);
129 AddChildControl(new IVButtonControl(IRECT(), SplashClickActionFunc, ">", mStyle))
130 ->SetAnimationEndActionFunction(nextPresetFunc);
131 AddChildControl(mPresetNameButton = new IVButtonControl(IRECT(), SplashClickActionFunc, "Choose Preset...", mStyle))->SetAnimationEndActionFunction(choosePresetFunc);
132
133 OnResize();
134 }
135
136 void OnResize() override
137 {
138 MakeRects(mRECT);
139
140 ForAllChildrenFunc([&](int childIdx, IControl* pChild){
141 pChild->SetTargetAndDrawRECTs(GetSubControlBounds((ESubControl) childIdx));
142 });
143 }
144
145private:
146 IRECT GetSubControlBounds(ESubControl control)
147 {
148 auto sections = mWidgetBounds;
149
150 std::array<IRECT, 3> rects = {
151 sections.ReduceFromLeft(50),
152 sections.ReduceFromLeft(50),
153 sections
154 };
155
156 return rects[(int) control];
157 }
158
159
160 IPopupMenu mMenu;
161 IVButtonControl* mPresetNameButton = nullptr;
162};
163
168 , public IVectorBase
169{
170public:
171 enum class ESubControl
172 {
173 LeftButton = 0,
174 RightButton,
175 PresetMenu,
176 LoadButton
177 };
178
179 IVDiskPresetManagerControl(const IRECT& bounds, const char* presetPath, const char* fileExtension, bool showFileExtensions = true, const char* label = "", const IVStyle& style = DEFAULT_STYLE.WithDrawShadows(false).WithLabelText(DEFAULT_LABEL_TEXT.WithVAlign(EVAlign::Middle)))
180 : IDirBrowseControlBase(bounds, fileExtension, showFileExtensions)
181 , IVectorBase(style)
182 {
183 AttachIControl(this, label);
184 mIgnoreMouse = true;
185 AddPath(presetPath, "");
186 SetupMenu();
187 }
188
189 void Draw(IGraphics& g) override
190 {
191 DrawLabel(g);
192 }
193
194 void OnPopupMenuSelection(IPopupMenu* pSelectedMenu, int valIdx) override
195 {
196 if (pSelectedMenu)
197 {
198 IPopupMenu::Item* pItem = pSelectedMenu->GetChosenItem();
199
200 if (pItem)
201 {
202 mSelectedItemIndex = mItems.Find(pItem);
203 LoadPresetAtCurrentIndex();
204 }
205 }
206 }
207
208 void OnAttached() override
209 {
210 auto prevPresetFunc = [&](IControl* pCaller) {
211 if (NItems())
212 {
213 mSelectedItemIndex--;
214
215 if (mSelectedItemIndex < 0)
216 mSelectedItemIndex = NItems() - 1;
217
218 LoadPresetAtCurrentIndex();
219 }
220 };
221
222 auto nextPresetFunc = [&](IControl* pCaller) {
223 if (NItems())
224 {
225 mSelectedItemIndex++;
226
227 if (mSelectedItemIndex >= NItems())
228 mSelectedItemIndex = 0;
229
230 LoadPresetAtCurrentIndex();
231 }
232 };
233
234 auto loadPresetFunc = [&](IControl* pCaller) {
235 WDL_String fileName, path;
236 pCaller->GetUI()->PromptForFile(fileName, path, EFileAction::Open, mExtension.Get());
237
238 if (path.GetLength())
239 {
241 AddPath(path.Get(), "");
242 SetupMenu();
243 }
244
245 if (fileName.GetLength())
246 {
247 SetSelectedFile(fileName.Get());
248 LoadPresetAtCurrentIndex();
249 }
250 };
251
252 auto choosePresetFunc = [&](IControl* pCaller) {
254 pCaller->GetUI()->CreatePopupMenu(*this, mMainMenu, pCaller->GetRECT());
255 };
256
257 AddChildControl(new IVButtonControl(IRECT(), SplashClickActionFunc, "<", mStyle))->SetAnimationEndActionFunction(prevPresetFunc);
258 AddChildControl(new IVButtonControl(IRECT(), SplashClickActionFunc, ">", mStyle))->SetAnimationEndActionFunction(nextPresetFunc);
259 AddChildControl(new IVButtonControl(IRECT(), SplashClickActionFunc, "Load", mStyle))->SetAnimationEndActionFunction(loadPresetFunc);
260 AddChildControl(mPresetNameButton = new IVButtonControl(IRECT(), SplashClickActionFunc, "Choose Preset...", mStyle))->SetAnimationEndActionFunction(choosePresetFunc);
261
262 OnResize();
263 }
264
265 void OnResize() override
266 {
267 MakeRects(mRECT);
268
269 ForAllChildrenFunc([&](int childIdx, IControl* pChild){
270 pChild->SetTargetAndDrawRECTs(GetSubControlBounds((ESubControl) childIdx));
271 });
272 }
273
274 void LoadPresetAtCurrentIndex()
275 {
276 if (mSelectedItemIndex > -1 &&
277 mSelectedItemIndex < mItems.GetSize())
278 {
279 WDL_String fileName;
280 GetSelectedFile(fileName);
281 if (!mShowFileExtensions)
282 {
283 fileName.remove_fileext();
284 }
285 mPresetNameButton->SetLabelStr(fileName.get_filepart());
286
287 // If it's just a single folder, we can set the
288 // chosen item index to mSelectedItemIndex
289 // in order to pop up the menu at the
290 // correct location
291 if (!mMainMenu.HasSubMenus())
292 {
293 mMainMenu.SetChosenItemIdx(mSelectedItemIndex);
294 }
295 }
296 }
297
298private:
299 IRECT GetSubControlBounds(ESubControl control)
300 {
301 auto sections = mWidgetBounds;
302
303 std::array<IRECT, 4> rects = {
304 sections.ReduceFromLeft(50),
305 sections.ReduceFromLeft(50),
306 sections.ReduceFromRight(50),
307 sections,
308 };
309
310 return rects[(int) control];
311 }
312
313 IVButtonControl* mPresetNameButton = nullptr;
314};
315
316END_IGRAPHICS_NAMESPACE
317END_IPLUG_NAMESPACE
318
This file contains the base IControl implementation, along with some base classes for specific types ...
IContainerBase allows a control to nest sub controls and it clips the drawing of those subcontrols In...
Definition: IControl.h:606
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
IGEditorDelegate * GetDelegate()
Gets a pointer to the class implementing the IEditorDelegate interface that handles parameter changes...
Definition: IControl.h:449
IControl * SetAnimationEndActionFunction(IActionFunction actionFunc)
Set an Action Function to be called at the end of an animation.
Definition: IControl.h:211
void SetTargetAndDrawRECTs(const IRECT &bounds)
Set BOTH the draw rect and the target area, within the graphics context for this control.
Definition: IControl.h:327
An abstract IControl base class that you can inherit from in order to make a control that pops up a m...
Definition: IControl.h:1900
void ClearPathList()
Clear the menu.
Definition: IControl.cpp:1068
void CheckSelectedItem()
Check the currently selected menu item.
Definition: IControl.cpp:1111
void SetupMenu()
Call after adding one or more paths, to populate the menu.
Definition: IControl.cpp:1041
void GetSelectedFile(WDL_String &path) const
Get the full path to the file if something has been selected in the menu.
Definition: IControl.cpp:1098
void SetSelectedFile(const char *filePath)
Set the selected file based on a file path.
Definition: IControl.cpp:1076
void AddPath(const char *path, const char *displayText)
Used to add a path to scan for files.
Definition: IControl.cpp:1018
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
Base class that contains plug-in info and state manipulation methods.
int GetCurrentPresetIdx() const
Get the index of the current, active preset.
bool RestorePreset(int idx)
Restore a preset by index.
const char * GetPresetName(int idx) const
Get the name a preset.
int NPresets() const
Gets the number of factory presets.
A class to specify an item of a pop up menu.
A class for setting the contents of a pop up menu.
A "meta control" for a "preset manager" for "baked in" factory presets It adds several child buttons.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set.
void OnPopupMenuSelection(IPopupMenu *pSelectedMenu, int valIdx) override
Implement this method to handle popup menu selection after IGraphics::CreatePopupMenu/IControlPromptU...
void OnResize() override
Called when IControl is constructed or resized using SetRect().
A vector button/momentary switch control.
Definition: IControls.h:53
A "meta control" for a "preset manager" for disk-based preset files It adds several child buttons.
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
void OnPopupMenuSelection(IPopupMenu *pSelectedMenu, int valIdx) override
Implement this method to handle popup menu selection after IGraphics::CreatePopupMenu/IControlPromptU...
void OnResize() override
Called when IControl is constructed or resized using SetRect().
A base interface to be combined with IControl for vectorial controls "IVControls",...
Definition: IControl.h:757
IRECT MakeRects(const IRECT &parent, bool hasHandle=false)
Calculate the rectangles for the various areas, depending on the style.
Definition: IControl.h:1158
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:775
virtual void DrawLabel(IGraphics &g)
Draw the IVControl label text.
Definition: IControl.h:889
void SplashClickActionFunc(IControl *pCaller)
The splash click action function is used by IVControls to start SplashAnimationFunc.
Definition: IControl.cpp:47
Used to manage a rectangular area, independent of draw class/platform.
IRECT ReduceFromRight(float amount)
Reduce in width from the right edge by 'amount' and return the removed region.
IRECT ReduceFromLeft(float amount)
Reduce in width from the left edge by 'amount' and return the removed region.
A struct encapsulating a set of properties used to configure IVControls.