iPlug2 - C++ Audio Plug-in Framework
IVPresetManagerControl.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 "IControl.h"
19 
20 #include "IPlugPluginBase.h"
21 
22 BEGIN_IPLUG_NAMESPACE
23 BEGIN_IGRAPHICS_NAMESPACE
24 
29 {
30 public:
31  IVBakedPresetManagerControl(const IRECT& bounds, const IVStyle& style = DEFAULT_STYLE)
32  : IControl(bounds)
33  , mStyle(style)
34  {
35  mIgnoreMouse = true;
36  }
37 
38  void Draw(IGraphics& g) override { /* NO-OP */ }
39 
40  void RestorePreset(IPluginBase* pluginBase, int presetIdx)
41  {
42  pluginBase->RestorePreset(presetIdx);
43 
44  WDL_String str;
45  str.SetFormatted(32, "Preset %i: %s", presetIdx + 1, pluginBase->GetPresetName(presetIdx));
46  mPresetNameButton->SetLabelStr(str.Get());
47  }
48 
49  void OnPopupMenuSelection(IPopupMenu* pSelectedMenu, int valIdx) override
50  {
51  if (pSelectedMenu)
52  {
53  IPopupMenu::Item* pItem = pSelectedMenu->GetChosenItem();
54 
55  if (pItem)
56  {
57  IPluginBase* pluginBase = dynamic_cast<IPluginBase*>(GetDelegate());
58  RestorePreset(pluginBase, pSelectedMenu->GetChosenItemIdx());
59  }
60  }
61  }
62 
63  void OnAttached() override
64  {
65  IRECT sections = mRECT.GetPadded(-5.f);
66 
67  auto prevPresetFunc = [&](IControl* pCaller) {
68  IPluginBase* pluginBase = dynamic_cast<IPluginBase*>(pCaller->GetDelegate());
69 
70  int presetIdx = pluginBase->GetCurrentPresetIdx();
71  int nPresets = pluginBase->NPresets();
72 
73  presetIdx--;
74 
75  if (presetIdx < 0)
76  presetIdx = nPresets - 1;
77 
78  RestorePreset(pluginBase, presetIdx);
79  };
80 
81  auto nextPresetFunc = [&](IControl* pCaller) {
82  IPluginBase* pluginBase = dynamic_cast<IPluginBase*>(pCaller->GetDelegate());
83 
84  int presetIdx = pluginBase->GetCurrentPresetIdx();
85  int nPresets = pluginBase->NPresets();
86 
87  presetIdx++;
88 
89  if (presetIdx >= nPresets)
90  presetIdx = 0;
91 
92  RestorePreset(pluginBase, presetIdx);
93  };
94 
95  auto choosePresetFunc = [&](IControl* pCaller) {
96  mMenu.Clear();
97 
98  IPluginBase* pluginBase = dynamic_cast<IPluginBase*>(pCaller->GetDelegate());
99 
100  int currentPresetIdx = pluginBase->GetCurrentPresetIdx();
101  int nPresets = pluginBase->NPresets();
102 
103  for (int i = 0; i < nPresets; i++) {
104  const char* str = pluginBase->GetPresetName(i);
105  if (i == currentPresetIdx)
106  mMenu.AddItem(str, -1, IPopupMenu::Item::kChecked);
107  else
108  mMenu.AddItem(str);
109  }
110 
111  pCaller->GetUI()->CreatePopupMenu(*this, mMenu, pCaller->GetRECT());
112  };
113 
114  GetUI()->AttachControl(new IVButtonControl(sections.ReduceFromLeft(50), SplashClickActionFunc, "<", mStyle))->SetAnimationEndActionFunction(prevPresetFunc);
115  GetUI()->AttachControl(new IVButtonControl(sections.ReduceFromLeft(50), SplashClickActionFunc, ">", mStyle))->SetAnimationEndActionFunction(nextPresetFunc);
116 // GetUI()->AttachControl(new IVButtonControl(sections.ReduceFromRight(100), SplashClickActionFunc, "Load", mStyle))->SetAnimationEndActionFunction(loadPresetFunc);
117  GetUI()->AttachControl(mPresetNameButton = new IVButtonControl(sections, SplashClickActionFunc, "Choose Preset...", mStyle))->SetAnimationEndActionFunction(choosePresetFunc);
118  }
119 
120 private:
121  IPopupMenu mMenu;
122  IVButtonControl* mPresetNameButton = nullptr;
123  IVStyle mStyle;
124 };
125 
130 {
131 public:
132  IVDiskPresetManagerControl(const IRECT& bounds, const char* presetPath, const char* fileExtension, bool showFileExtensions = true, const IVStyle& style = DEFAULT_STYLE)
133  : IDirBrowseControlBase(bounds, fileExtension, showFileExtensions)
134  , mStyle(style)
135  {
136  mIgnoreMouse = true;
137  AddPath(presetPath, "");
138  SetupMenu();
139  }
140 
141  void Draw(IGraphics& g) override { /* NO-OP */ }
142 
143  void OnPopupMenuSelection(IPopupMenu* pSelectedMenu, int valIdx) override
144  {
145  if (pSelectedMenu)
146  {
147  IPopupMenu::Item* pItem = pSelectedMenu->GetChosenItem();
148 
149  if (pItem)
150  {
151  mSelectedIndex = mItems.Find(pItem);
152  LoadPresetAtCurrentIndex();
153  }
154  }
155  }
156 
157  void OnAttached() override
158  {
159  IRECT sections = mRECT.GetPadded(-5.f);
160 
161  auto prevPresetFunc = [&](IControl* pCaller) {
162  mSelectedIndex--;
163 
164  if (mSelectedIndex < 0)
165  mSelectedIndex = NItems() - 1;
166 
167  LoadPresetAtCurrentIndex();
168  };
169 
170  auto nextPresetFunc = [&](IControl* pCaller) {
171  mSelectedIndex++;
172 
173  if (mSelectedIndex >= NItems())
174  mSelectedIndex = 0;
175 
176  LoadPresetAtCurrentIndex();
177  };
178 
179  auto loadPresetFunc = [&](IControl* pCaller) {
180  WDL_String fileName;
181  WDL_String path;
182  pCaller->GetUI()->PromptForFile(fileName, path, EFileAction::Open, mExtension.Get());
183 
184  if (fileName.GetLength())
185  mPresetNameButton->SetLabelStr(fileName.Get());
186  };
187 
188  auto choosePresetFunc = [&](IControl* pCaller) { pCaller->GetUI()->CreatePopupMenu(*this, mMainMenu, pCaller->GetRECT()); };
189 
190  GetUI()->AttachControl(new IVButtonControl(sections.ReduceFromLeft(50), SplashClickActionFunc, "<", mStyle))->SetAnimationEndActionFunction(prevPresetFunc);
191  GetUI()->AttachControl(new IVButtonControl(sections.ReduceFromLeft(50), SplashClickActionFunc, ">", mStyle))->SetAnimationEndActionFunction(nextPresetFunc);
192  GetUI()->AttachControl(new IVButtonControl(sections.ReduceFromRight(100), SplashClickActionFunc, "Load", mStyle))->SetAnimationEndActionFunction(loadPresetFunc);
193  GetUI()->AttachControl(mPresetNameButton = new IVButtonControl(sections, SplashClickActionFunc, "Choose Preset...", mStyle))->SetAnimationEndActionFunction(choosePresetFunc);
194  }
195 
196  void LoadPresetAtCurrentIndex()
197  {
198  if (mSelectedIndex > -1 && mSelectedIndex < mItems.GetSize())
199  {
200  IPopupMenu::Item* pItem = mItems.Get(mSelectedIndex);
201 
202  //if (PLUG()->LoadPresetFromVSTPreset(mFiles.Get(pItem->GetTag())->Get()))
203  //{
204  // PLUG()->ModifyCurrentPreset(PLUG()->GetPatchName());
205  // PLUG()->InformHostOfPresetChange();
206  mPresetNameButton->SetLabelStr(pItem->GetText());
207  //}
208  }
209  }
210 
211 private:
212  IVButtonControl* mPresetNameButton = nullptr;
213  IVStyle mStyle;
214 };
215 
216 END_IGRAPHICS_NAMESPACE
217 END_IPLUG_NAMESPACE
218 
219 
void Draw(IGraphics &g) override
Draw the control to the graphics context.
const char * GetPresetName(int idx) const
Get the name a preset.
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
IGEditorDelegate * GetDelegate()
Gets a pointer to the class implementing the IEditorDelegate interface that handles parameter changes...
Definition: IControl.h:439
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set...
int NPresets() const
Gets the number of factory presets.
A vector button/momentary switch control.
Definition: IControls.h:50
A class to specify an item of a pop up menu.
IControl * SetAnimationEndActionFunction(IActionFunction actionFunc)
Set an Action Function to be called at the end of an animation.
Definition: IControl.h:206
IRECT ReduceFromRight(float amount)
Reduce in width from the right edge by &#39;amount&#39; and return the removed region.
This file contains the base IControl implementation, along with some base classes for specific types ...
A "meta control" for a "preset manager" for "baked in" factory presets It adds several child buttons...
void OnPopupMenuSelection(IPopupMenu *pSelectedMenu, int valIdx) override
Implement this method to handle popup menu selection after IGraphics::CreatePopupMenu/IControl::Promp...
A "meta control" for a "preset manager" for disk-based preset files It adds several child buttons...
A class for setting the contents of a pop up menu.
Base class that contains plug-in info and state manipulation methods.
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
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.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
int GetCurrentPresetIdx() const
Get the index of the current, active preset.
IRECT ReduceFromLeft(float amount)
Reduce in width from the left edge by &#39;amount&#39; and return the removed region.
void SplashClickActionFunc(IControl *pCaller)
The splash click action function is used by IVControls to start SplashAnimationFunc.
Definition: IControl.cpp:47
bool RestorePreset(int idx)
Restore a preset by index.
void OnPopupMenuSelection(IPopupMenu *pSelectedMenu, int valIdx) override
Implement this method to handle popup menu selection after IGraphics::CreatePopupMenu/IControl::Promp...
An abstract IControl base class that you can inherit from in order to make a control that pops up a m...
Definition: IControl.h:1712
IGraphics * GetUI()
Definition: IControl.h:452
IControl * AttachControl(IControl *pControl, int ctrlTag=kNoTag, const char *group="")
Attach an IControl to the graphics context and add it to the top of the control stack.
Definition: IGraphics.cpp:298
IRECT GetPadded(float padding) const
Get a copy of this IRECT with each value padded by padding N.B.
Base class that contains plug-in info and state manipulation methods.
A struct encapsulating a set of properties used to configure IVControls.