Jamba C++ API  4.4.0
ParamAware.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <vstgui/lib/cview.h>
21 
23 
24 #include <utility>
25 #include "GUIRawVstParameter.h"
26 #include "GUIJmbParameter.h"
27 #include "GUIVstParameter.h"
28 #include "GUIOptionalParam.h"
29 
30 namespace pongasoft::VST::GUI {
31 
32 class GUIState;
33 
34 namespace Params {
35 
36 class GUIParamCxMgr;
37 
64 {
65 public:
66  virtual ~ParamAware();
67 
68  //------------------------------------------------------------------------
69  // Base Param
70  //------------------------------------------------------------------------
71 
79  IGUIParam registerBaseParam(ParamID iParamID, bool iSubscribeToChanges = true);
80 
87  IGUIParam registerBaseCallback(ParamID iParamID,
88  Parameters::ChangeCallback iChangeCallback,
89  bool iInvokeCallback);
90 
97  IGUIParam registerBaseCallback(ParamID iParamID,
99  bool iInvokeCallback);
100 
101  //------------------------------------------------------------------------
102  // Optional Param
103  //------------------------------------------------------------------------
104 
113  template<typename T>
115  bool iSubscribeToChanges = true);
116 
125  template<typename T>
127  Parameters::ChangeCallback iChangeCallback,
128  bool iInvokeCallback = false);
129 
138  template<typename T>
141  bool iInvokeCallback = false);
142 
143  //------------------------------------------------------------------------
144  // Optional Discrete Param
145  //------------------------------------------------------------------------
146 
157  int32 iStepCount,
158  bool iSubscribeToChanges = true);
159 
170  int32 iStepCount,
171  Parameters::ChangeCallback iChangeCallback,
172  bool iInvokeCallback = false);
173 
184  int32 iStepCount,
186  bool iInvokeCallback = false);
187 
188  //------------------------------------------------------------------------
189  // Raw Vst Param / ParamID
190  //------------------------------------------------------------------------
191 
198  GUIRawVstParam registerRawVstParam(ParamID iParamID, bool iSubscribeToChanges = true);
199 
206  GUIRawVstParam registerRawVstCallback(ParamID iParamID,
207  Parameters::ChangeCallback iChangeCallback,
208  bool iInvokeCallback = false);
209 
216  GUIRawVstParam registerRawVstCallback(ParamID iParamID,
218  bool iInvokeCallback = false);
219 
220 
221  //------------------------------------------------------------------------
222  // Raw Vst Param / param definition (RawVstParam)
223  //------------------------------------------------------------------------
224 
229  inline GUIRawVstParam registerParam(RawVstParam const &iParamDef, bool iSubscribeToChanges = true) {
230  return registerRawVstParam(iParamDef->fParamID, iSubscribeToChanges);
231  }
232 
237  inline GUIRawVstParam registerCallback(RawVstParam const &iParamDef,
238  Parameters::ChangeCallback iChangeCallback,
239  bool iInvokeCallback = false) {
240  return registerRawVstCallback(iParamDef->fParamID, std::move(iChangeCallback), iInvokeCallback);
241  }
242 
247  inline GUIRawVstParam registerCallback(RawVstParam const &iParamDef,
249  bool iInvokeCallback = false) {
250  return registerRawVstCallback(iParamDef->fParamID, std::move(iChangeCallback), iInvokeCallback);
251  }
252 
253  //------------------------------------------------------------------------
254  // Shortcut Vst Param bool/Percent
255  //------------------------------------------------------------------------
256 
257  // shortcut for BooleanParameter (will be removed => not very useful)
258  [[deprecated("Since 4.0.0 - use registerVstParam<bool> directly")]]
259  GUIVstParam<bool> registerVstBooleanParam(ParamID iParamID, bool iSubscribeToChanges = true);
260 
261  // shortcut for PercentParameter (will be removed => not very useful)
262  [[deprecated("Since 4.0.0 - use registerVstParam<Percent> directly")]]
263  GUIVstParam<Percent> registerVstPercentParam(ParamID iParamID, bool iSubscribeToChanges = true);
264 
265  //------------------------------------------------------------------------
266  // Vst Param (Typed) / ParamID
267  //------------------------------------------------------------------------
268 
275  template<typename T>
276  GUIVstParam<T> registerVstParam(ParamID iParamID, bool iSubscribeToChanges = true);
277 
284  template<typename T>
285  GUIVstParam<T> registerVstCallback(ParamID iParamID,
286  Parameters::ChangeCallback iChangeCallback,
287  bool iInvokeCallback = false);
288 
295  template<typename T>
296  GUIVstParam<T> registerVstCallback(ParamID iParamID,
298  bool iInvokeCallback = false);
299 
300  //------------------------------------------------------------------------
301  // Vst Param (Typed) / param definition (VstParam<T>)
302  //------------------------------------------------------------------------
303 
310  template<typename T>
311  inline GUIVstParam<T> registerParam(VstParam<T> const &iParamDef, bool iSubscribeToChanges = true) {
312  return registerVstParam<T>(iParamDef->fParamID, iSubscribeToChanges);
313  }
314 
321  template<typename T>
322  inline GUIVstParam<T> registerCallback(VstParam<T> const &iParamDef,
323  Parameters::ChangeCallback iChangeCallback,
324  bool iInvokeCallback = false) {
325  return registerVstCallback<T>(iParamDef->fParamID, std::move(iChangeCallback), iInvokeCallback);
326  }
327 
341  template<typename T>
342  inline GUIVstParam<T> registerCallback(VstParam<T> const &iParamDef,
344  bool iInvokeCallback = false) {
345  return registerVstCallback<T>(iParamDef->fParamID, std::move(iChangeCallback), iInvokeCallback);
346  }
347 
348  //------------------------------------------------------------------------
349  // Jmb Param (Typed) / ParamID
350  //------------------------------------------------------------------------
351 
358  template<typename T>
359  GUIJmbParam<T> registerJmbParam(ParamID iParamID, bool iSubscribeToChanges = true);
360 
367  template<typename T>
368  GUIJmbParam<T> registerJmbCallback(ParamID iParamID,
369  Parameters::ChangeCallback iChangeCallback,
370  bool iInvokeCallback = false);
371 
378  template<typename T>
379  GUIJmbParam<T> registerJmbCallback(ParamID iParamID,
381  bool iInvokeCallback = false);
382 
383  //------------------------------------------------------------------------
384  // Jmb Param (Typed) / Param definition (JmbParam<T>)
385  //------------------------------------------------------------------------
386 
393  template<typename T>
394  inline GUIJmbParam<T> registerParam(JmbParam<T> const &iParamDef, bool iSubscribeToChanges = true) {
395  return registerJmbParam<T>(iParamDef->fParamID, iSubscribeToChanges);
396  }
397 
404  template<typename T>
405  inline GUIJmbParam<T> registerCallback(JmbParam<T> const &iParamDef,
406  Parameters::ChangeCallback iChangeCallback,
407  bool iInvokeCallback = false) {
408  return registerJmbCallback<T>(iParamDef->fParamID, std::move(iChangeCallback), iInvokeCallback);
409 
410  }
411 
424  template<typename T>
425  inline GUIJmbParam<T> registerCallback(JmbParam<T> const &iParamDef,
427  bool iInvokeCallback = false) {
428  return registerJmbCallback<T>(iParamDef->fParamID, std::move(iChangeCallback), iInvokeCallback);
429  }
430 
431  //------------------------------------------------------------------------
432  // Jmb Param (Typed) / Param (GUIJmbParam<T>)
433  //------------------------------------------------------------------------
434 
441  template<typename T>
442  inline GUIJmbParam<T> registerParam(GUIJmbParam<T> &iParam, bool iSubscribeToChanges = true) {
443  return registerJmbParam<T>(iParam.getParamID(), iSubscribeToChanges);
444  }
445 
452  template<typename T>
454  Parameters::ChangeCallback iChangeCallback,
455  bool iInvokeCallback = false) {
456  return registerJmbCallback<T>(iParam.getParamID(), std::move(iChangeCallback), iInvokeCallback);
457  }
458 
471  template<typename T>
474  bool iInvokeCallback = false) {
475  return registerJmbCallback<T>(iParam.getParamID(), std::move(iChangeCallback), iInvokeCallback);
476  }
477 
485  virtual void initState(GUIState *iGUIState);
486 
497  virtual void registerParameters()
498  {
499  // subclasses implements this method
500  }
501 
504  void unregisterAll();
505 
511  [[deprecated("Since 4.0.0 - If you need this kind of granularity, maintain your own connections")]]
512  bool unregisterParam(ParamID iParamID) { return false; }
513 
519  [[deprecated("Since 4.0.0 - If you need this kind of granularity, maintain your own connections")]]
520  GUIRawVstParam unregisterParam(GUIRawVstParam const &iParam) { return iParam; }
521 
527  template<typename T>
528  [[deprecated("Since 4.0.0 - If you need this kind of granularity, maintain your own connections")]]
529  GUIVstParam<T> unregisterParam(GUIVstParam<T> const &iParam) { return iParam; }
530 
536  template<typename T>
537  [[deprecated("Since 4.0.0 - If you need this kind of granularity, maintain your own connections")]]
538  GUIJmbParam<T> unregisterParam(GUIJmbParam<T> const &iParam) { return iParam; }
539 
543  void onParameterChange(ParamID iParamID) override {}
544 
547  void invokeAll();
548 
549 protected:
550  // Access to parameters
551  std::unique_ptr<GUIParamCxMgr> fParamCxMgr{};
552 };
553 
560 template<typename TView>
562 {
563  // ensures that TView is a subclass of CView
564  static_assert(std::is_convertible<TView *, VSTGUI::CView*>::value, "TView must be a subclass of CView");
565 
566 public:
567  // required because we implement other registerCallback methods
569 
570  // defining the change listener callback
571  using ChangeListener = std::function<void(TView *, ParamID)>;
572 
573  // Constructor
574  explicit ParamAwareView(TView *iView) : fView(iView), fListener{} {}
575 
584  {
585  fListener = std::move(iListener);
586  }
587 
602  template<typename T>
603  inline GUIVstParam<T> registerCallback(VstParam<T> const &iParamDef,
604  Parameters::ChangeCallback2<TView, GUIVstParam<T>> iChangeCallback,
605  bool iInvokeCallback = false)
606  {
607  auto callback = [view = this->fView, cb2 = std::move(iChangeCallback)] (GUIVstParam<T> &iParam) {
608  cb2(view, iParam);
609  };
610 
611  return ParamAware::registerCallback<T>(iParamDef, std::move(callback), iInvokeCallback);
612  }
613 
628  template<typename T>
630  Parameters::ChangeCallback2<TView, GUIJmbParam<T>> iChangeCallback,
631  bool iInvokeCallback = false)
632  {
633  auto callback = [view = this->fView, cb2 = std::move(iChangeCallback)] (GUIJmbParam<T> &iParam) {
634  cb2(view, iParam);
635  };
636 
637  return ParamAware::registerCallback<T>(iParam, std::move(callback), iInvokeCallback);
638  }
639 
643  void onParameterChange(ParamID iParamID) override
644  {
645  if(fListener)
646  fListener(fView, iParamID);
647  }
648 
649 protected:
650  TView *fView;
652 };
653 
654 }
655 }
TView * fView
Definition: ParamAware.h:650
GUIOptionalParam< T > registerOptionalParam(ParamID iParamID, bool iSubscribeToChanges=true)
Registers an optional parameter which handles Vst, Jmb or no parameter at all.
Definition: ParamAware.hpp:30
GUIVstParam< T > registerCallback(VstParam< T > const &iParamDef, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Convenient call to register a callback for the Vst param simply by using its description (accessible ...
Definition: ParamAware.h:322
This class is inherited by classes who want to be aware of parameters and be notified when they chang...
Definition: ParamAware.h:63
void onParameterChange(ParamID iParamID) override
This implementation simply delegates to the listener registered by registerListener().
Definition: ParamAware.h:643
Definition: GUIState.h:40
GUIJmbParam< T > registerCallback(JmbParam< T > const &iParamDef, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Convenient call to register a callback for the Jmb param simply by using its description (accessible ...
Definition: ParamAware.h:405
GUIRawVstParam registerCallback(RawVstParam const &iParamDef, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Registers a raw parameter (no conversion).
Definition: ParamAware.h:237
std::function< void(TView *, Param &)> ChangeCallback2
A callback which will be invoked for changes with the view and param as arguments.
Definition: Parameters.h:67
GUIVstParam< T > registerVstCallback(ParamID iParamID, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Register a callback for a Vst parameter simply given its id The callback will be invoked when the par...
Definition: ParamAware.hpp:88
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIRawVstParameter.h:265
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition: Parameters.h:55
GUIOptionalParam< T > registerOptionalCallback(ParamID iParamID, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Registers an optional parameter which handles Vst, Jmb or no parameter at all.
Definition: ParamAware.hpp:44
void registerListener(ChangeListener iListener)
Registers a listener invoked when a parameter changes.
Definition: ParamAware.h:583
std::shared_ptr< RawVstParamDef > RawVstParam
Definition: ParamDef.h:508
virtual void initState(GUIState *iGUIState)
Called during initialization.
Definition: ParamAware.cpp:36
GUIVstParam< T > registerParam(VstParam< T > const &iParamDef, bool iSubscribeToChanges=true)
Convenient call to register a Vst param simply by using its description (accessible in state-aware vi...
Definition: ParamAware.h:311
GUIVstParam< Percent > registerVstPercentParam(ParamID iParamID, bool iSubscribeToChanges=true)
Definition: ParamAware.cpp:89
virtual void registerParameters()
Subclasses should override this method to register each parameter.
Definition: ParamAware.h:497
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIVstParameter.h:278
GUIOptionalParam< int32 > registerOptionalDiscreteParam(ParamID iParamID, int32 iStepCount, bool iSubscribeToChanges=true)
Registers an optional discrete parameter which handles Vst, Jmb or no parameter at all.
Definition: ParamAware.cpp:115
GUIVstParam< T > registerCallback(VstParam< T > const &iParamDef, Parameters::ChangeCallback1< GUIVstParam< T >> iChangeCallback, bool iInvokeCallback=false)
Convenient call to register a callback for the Vst param simply by using its description (accessible ...
Definition: ParamAware.h:342
GUIJmbParam< T > registerCallback(GUIJmbParam< T > &iParam, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Convenient call to register a callback for the Jmb param simply by using the param from the state (ac...
Definition: ParamAware.h:453
GUIJmbParam< T > registerJmbCallback(ParamID iParamID, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Registers a callback for the Jmb param by its id and return the wrapper to the param.
Definition: ParamAware.hpp:128
Definition: DrawContext.cpp:24
GUIVstParam< T > unregisterParam(GUIVstParam< T > const &iParam)
Removes the registration of the provided param (closing the connection/stopping to listen)
Definition: ParamAware.h:529
GUIVstParam< bool > registerVstBooleanParam(ParamID iParamID, bool iSubscribeToChanges=true)
Definition: ParamAware.cpp:81
GUIRawVstParam registerRawVstCallback(ParamID iParamID, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Registers a raw parameter (no conversion) The callback will be invoked when the parameter changes.
Definition: ParamAware.cpp:55
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIJmbParameter.h:495
GUIVstParam< T > registerCallback(VstParam< T > const &iParamDef, Parameters::ChangeCallback2< TView, GUIVstParam< T >> iChangeCallback, bool iInvokeCallback=false)
Register a callback for the Vst parameter.
Definition: ParamAware.h:603
virtual ~ParamAware()
Definition: ParamAware.cpp:28
GUIVstParam< T > registerVstParam(ParamID iParamID, bool iSubscribeToChanges=true)
Register a Vst parameter simply given its id onParameterChange() will be called on changes (if iSubsc...
Definition: ParamAware.hpp:76
std::unique_ptr< GUIParamCxMgr > fParamCxMgr
Definition: ParamAware.h:551
std::function< void(TView *, ParamID)> ChangeListener
Definition: ParamAware.h:571
GUIJmbParam< T > unregisterParam(GUIJmbParam< T > const &iParam)
Removes the registration of the provided param (closing the connection/stopping to listen)
Definition: ParamAware.h:538
GUIRawVstParam unregisterParam(GUIRawVstParam const &iParam)
Removes the registration of the provided param (closing the connection/stopping to listen)
Definition: ParamAware.h:520
void unregisterAll()
Unregisters all parameters.
Definition: ParamAware.cpp:97
GUIJmbParam< T > registerParam(JmbParam< T > const &iParamDef, bool iSubscribeToChanges=true)
Convenient call to register a Jmb param simply by using its description (accessible in state-aware vi...
Definition: ParamAware.h:394
bool unregisterParam(ParamID iParamID)
Removes the registration of the provided param (closing the connection/stopping to listen)
Definition: ParamAware.h:512
GUIRawVstParam registerRawVstParam(ParamID iParamID, bool iSubscribeToChanges=true)
Registers a raw parameter (no conversion) onParameterChange() will be called on changes (if iSubscrib...
Definition: ParamAware.cpp:44
This subclass allows for registering callbacks to any kind of view without having to inherit from it.
Definition: ParamAware.h:561
std::function< void(Param &)> ChangeCallback1
A callback which will be invoked for changes with the param as an argument.
Definition: Parameters.h:61
std::shared_ptr< VstParamDef< T > > VstParam
Definition: ParamDef.h:507
IGUIParam registerBaseCallback(ParamID iParamID, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback)
Registers a callback for the "base" param for the most generic use case.
Definition: ParamAware.cpp:173
GUIRawVstParam registerCallback(RawVstParam const &iParamDef, Parameters::ChangeCallback1< GUIRawVstParam > iChangeCallback, bool iInvokeCallback=false)
Registers a raw parameter (no conversion).
Definition: ParamAware.h:247
GUIJmbParam< T > registerCallback(GUIJmbParam< T > &iParam, Parameters::ChangeCallback2< TView, GUIJmbParam< T >> iChangeCallback, bool iInvokeCallback=false)
Register a callback for the Jmb parameter.
Definition: ParamAware.h:629
ParamAwareView(TView *iView)
Definition: ParamAware.h:574
ParamID getParamID() const
Definition: GUIJmbParameter.h:514
void invokeAll()
Invoke all (currently) registered callbacks and onParameterChange() (if registered).
Definition: ParamAware.cpp:106
GUIJmbParam< T > registerCallback(JmbParam< T > const &iParamDef, Parameters::ChangeCallback1< GUIJmbParam< T >> iChangeCallback, bool iInvokeCallback=false)
Convenient call to register a callback for the Jmb param simply by using its description (accessible ...
Definition: ParamAware.h:425
GUIJmbParam< T > registerJmbParam(ParamID iParamID, bool iSubscribeToChanges=true)
Registers the Jmb param by its id and return the wrapper to the param.
Definition: ParamAware.hpp:116
void onParameterChange(ParamID iParamID) override
This is the callback that should be implemented to handle parameter changes.
Definition: ParamAware.h:543
GUIJmbParam< T > registerParam(GUIJmbParam< T > &iParam, bool iSubscribeToChanges=true)
Convenient call to register a Jmb param simply by using the param from the state (accessible in state...
Definition: ParamAware.h:442
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition: ParamDef.h:514
GUIOptionalParam< int32 > registerOptionalDiscreteCallback(ParamID iParamID, int32 iStepCount, Parameters::ChangeCallback iChangeCallback, bool iInvokeCallback=false)
Registers an optional discrete parameter which handles Vst, Jmb or no parameter at all.
Definition: ParamAware.cpp:130
GUIRawVstParam registerParam(RawVstParam const &iParamDef, bool iSubscribeToChanges=true)
Registers a raw parameter (no conversion).
Definition: ParamAware.h:229
Wrapper instance returned by ParamAware::registerBaseParam() methods.
Definition: IGUIParameter.h:401
GUIJmbParam< T > registerCallback(GUIJmbParam< T > &iParam, Parameters::ChangeCallback1< GUIJmbParam< T >> iChangeCallback, bool iInvokeCallback=false)
Convenient call to register a callback for the Jmb param simply by using the param from the state (ac...
Definition: ParamAware.h:472
ChangeListener fListener
Definition: ParamAware.h:651
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
Represents an optional parameter (Jmb, Vst or no param at all).
Definition: GUIOptionalParam.h:48
IGUIParam registerBaseParam(ParamID iParamID, bool iSubscribeToChanges=true)
Registers the "base" param for the most generic use case but as a result is fairly limited and mainly...
Definition: ParamAware.cpp:162