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 using OnLoadFunc = std::function<void(const WDL_String& path)>;
172
173 enum class ESubControl
174 {
175 LeftButton = 0,
176 RightButton,
177 PresetMenu,
178 LoadButton
179 };
180
181 IVDiskPresetManagerControl(const IRECT& bounds,
182 const char* presetPath,
183 const char* fileExtension,
184 bool showFileExtensions = true,
185 const char* label = "",
186 const IVStyle& style = DEFAULT_STYLE.WithDrawShadows(false).WithLabelText(DEFAULT_LABEL_TEXT.WithVAlign(EVAlign::Middle)),
187 OnLoadFunc onLoadFunc = nullptr)
188 : IDirBrowseControlBase(bounds, fileExtension, showFileExtensions)
189 , IVectorBase(style)
190 , mOnLoadFunc(onLoadFunc)
191 {
192 AttachIControl(this, label);
193 mIgnoreMouse = true;
194 AddPath(presetPath, "");
195 SetupMenu();
196 }
197
198 void Draw(IGraphics& g) override
199 {
200 DrawLabel(g);
201 }
202
203 void OnPopupMenuSelection(IPopupMenu* pSelectedMenu, int valIdx) override
204 {
205 if (pSelectedMenu)
206 {
207 IPopupMenu::Item* pItem = pSelectedMenu->GetChosenItem();
208
209 if (pItem)
210 {
211 mSelectedItemIndex = mItems.Find(pItem);
212 LoadPresetAtCurrentIndex();
213 }
214 }
215 }
216
217 void OnAttached() override
218 {
219 auto prevPresetFunc = [&](IControl* pCaller) {
220 if (NItems())
221 {
222 mSelectedItemIndex--;
223
224 if (mSelectedItemIndex < 0)
225 mSelectedItemIndex = NItems() - 1;
226
227 LoadPresetAtCurrentIndex();
228 }
229 };
230
231 auto nextPresetFunc = [&](IControl* pCaller) {
232 if (NItems())
233 {
234 mSelectedItemIndex++;
235
236 if (mSelectedItemIndex >= NItems())
237 mSelectedItemIndex = 0;
238
239 LoadPresetAtCurrentIndex();
240 }
241 };
242
243 auto loadPresetFunc = [&](IControl* pCaller) {
244 WDL_String fileName, path;
245 pCaller->GetUI()->PromptForFile(fileName, path, EFileAction::Open, mExtension.Get());
246
247 if (path.GetLength())
248 {
250 AddPath(path.Get(), "");
251 SetupMenu();
252 }
253
254 if (fileName.GetLength())
255 {
256 SetSelectedFile(fileName.Get());
257 LoadPresetAtCurrentIndex();
258 }
259 };
260
261 auto choosePresetFunc = [&](IControl* pCaller) {
263 pCaller->GetUI()->CreatePopupMenu(*this, mMainMenu, pCaller->GetRECT());
264 };
265
266 AddChildControl(new IVButtonControl(IRECT(), SplashClickActionFunc, "<", mStyle))->SetAnimationEndActionFunction(prevPresetFunc);
267 AddChildControl(new IVButtonControl(IRECT(), SplashClickActionFunc, ">", mStyle))->SetAnimationEndActionFunction(nextPresetFunc);
268 AddChildControl(new IVButtonControl(IRECT(), SplashClickActionFunc, "Load", mStyle))->SetAnimationEndActionFunction(loadPresetFunc);
269 AddChildControl(mPresetNameButton = new IVButtonControl(IRECT(), SplashClickActionFunc, "Choose Preset...", mStyle))->SetAnimationEndActionFunction(choosePresetFunc);
270
271 OnResize();
272 }
273
274 void OnResize() override
275 {
276 MakeRects(mRECT);
277
278 ForAllChildrenFunc([&](int childIdx, IControl* pChild){
279 pChild->SetTargetAndDrawRECTs(GetSubControlBounds((ESubControl) childIdx));
280 });
281 }
282
283 void LoadPresetAtCurrentIndex()
284 {
285 if (mSelectedItemIndex > -1 &&
286 mSelectedItemIndex < mItems.GetSize())
287 {
288 WDL_String fileName;
289 GetSelectedFile(fileName);
290 if (!mShowFileExtensions)
291 {
292 fileName.remove_fileext();
293 }
294 mPresetNameButton->SetLabelStr(fileName.get_filepart());
295
296
297 if (mOnLoadFunc)
298 {
299 mOnLoadFunc(fileName);
300 }
301
302 // If it's just a single folder, we can set the
303 // chosen item index to mSelectedItemIndex
304 // in order to pop up the menu at the
305 // correct location
306 if (!mMainMenu.HasSubMenus())
307 {
308 mMainMenu.SetChosenItemIdx(mSelectedItemIndex);
309 }
310 }
311 }
312
313private:
314 IRECT GetSubControlBounds(ESubControl control)
315 {
316 auto sections = mWidgetBounds;
317
318 std::array<IRECT, 4> rects = {
319 sections.ReduceFromLeft(50),
320 sections.ReduceFromLeft(50),
321 sections.ReduceFromRight(50),
322 sections,
323 };
324
325 return rects[(int) control];
326 }
327
328 IVButtonControl* mPresetNameButton = nullptr;
329 OnLoadFunc mOnLoadFunc;
330};
331
332END_IGRAPHICS_NAMESPACE
333END_IPLUG_NAMESPACE
334
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:611
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:454
IControl * SetAnimationEndActionFunction(IActionFunction actionFunc)
Set an Action Function to be called at the end of an animation.
Definition: IControl.h:216
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
An abstract IControl base class that you can inherit from in order to make a control that pops up a m...
Definition: IControl.h:1905
void ClearPathList()
Clear the menu.
Definition: IControl.cpp:1079
void CheckSelectedItem()
Check the currently selected menu item.
Definition: IControl.cpp:1122
void SetupMenu()
Call after adding one or more paths, to populate the menu.
Definition: IControl.cpp:1052
void GetSelectedFile(WDL_String &path) const
Get the full path to the file if something has been selected in the menu.
Definition: IControl.cpp:1109
void SetSelectedFile(const char *filePath)
Set the selected file based on a file path.
Definition: IControl.cpp:1087
void AddPath(const char *path, const char *displayText)
Used to add a path to scan for files.
Definition: IControl.cpp:1029
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:54
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:762
IRECT MakeRects(const IRECT &parent, bool hasHandle=false)
Calculate the rectangles for the various areas, depending on the style.
Definition: IControl.h:1163
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:780
virtual void DrawLabel(IGraphics &g)
Draw the IVControl label text.
Definition: IControl.h:894
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.