Jamba C++ API  6.3.0
GUIRawVstParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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 "IGUIParameter.h"
23 #include "VstParameters.h"
24 #include "GUIParamCx.h"
25 
26 #include <string>
27 
29 
30 template<typename T>
32 
39 class GUIRawVstParameter : public ITGUIParameter<ParamValue>
40 {
41 public:
42  using ParamType = ParamValue;
44 
45 public:
59  class Editor : public EditorType
60  {
61  public:
62  Editor(ParamID iParamID, VstParametersSPtr iVstParameters);
63 
64  ~Editor() override { rollback(); }
65 
66  // disabling copy
67  Editor(Editor const &) = delete;
68  Editor& operator=(Editor const &) = delete;
69 
73  tresult setValue(ParamValue const &iValue) override;
74 
79  bool updateValue(ParamValue const &iValue) override;
80 
81  /*
82  * Call when you are done with the modifications.
83  * This has no effect if rollback() has already been called
84  */
85  tresult commit() override;
86 
87  // importing superclass commit methods
88  using EditorType::commit;
89 
94  tresult rollback() override;
95 
96  private:
97  ParamID fParamID;
99 
100  ParamValue fInitialParamValue;
102  };
103 
104 public:
105  // Constructor
106  GUIRawVstParameter(ParamID iParamID,
107  VstParametersSPtr iVstParameters,
108  std::shared_ptr<RawVstParamDef> iParamDef);
109 
110  // Destructor
111  ~GUIRawVstParameter() = default;
112 // {
113 // DLOG_F(INFO, "RawParameter::~RawParameter(%d)", fParamID);
114 // }
115 
116  // getParamID
117  inline ParamID getParamID() const override
118  {
119  return fParamID;
120  }
121 
122  // accessValue
123  tresult accessValue(ValueAccessor const &iGetter) const override
124  {
125  iGetter(getValue());
126  return kResultOk;
127  }
128 
132  inline ParamValue getValue() const
133  {
134  return fVstParameters->getParamNormalized(fParamID);
135  }
136 
140  inline int32 getStepCount() const override
141  {
142  auto parameterInfo = fVstParameters->getParameterInfo(fParamID);
143  if(parameterInfo)
144  return parameterInfo->stepCount;
145  else
146  return 0;
147  }
148 
152  void toString(String128 oString)
153  {
154  fParamDef->toString(getValue(), oString);
155  }
156 
160  Steinberg::String toString()
161  {
162  String128 s;
163  toString(s);
164  return Steinberg::String(s);
165  }
166 
167  // toUTF8String
168  std::string toUTF8String(int32 iPrecision) const override
169  {
170  return fParamDef->toUTF8String(getValue(), iPrecision);
171  }
172 
178  bool update(ParamValue const &iValue) override
179  {
180  auto const previousValue = getValue();
181  if(previousValue != iValue)
182  {
183  setValue(iValue);
184  return true;
185  }
186  return false;
187  }
188 
193  inline tresult setValue(ParamValue const &iValue) override
194  {
195  Editor editor(fParamID, fVstParameters);
196  editor.setValue(iValue);
197  return editor.commit();
198  }
199 
202  tresult resetToDefault() override
203  {
204  return setValue(fParamDef->fDefaultValue);
205  }
206 
210  std::unique_ptr<EditorType> edit() override
211  {
212  return std::make_unique<Editor>(fParamID, fVstParameters);
213  }
214 
219 
223  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
224  {
225  return fVstParameters->connect(fParamID, iChangeListener);
226  }
227 
231  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
232  {
233  return fVstParameters->connect(fParamID, std::move(iChangeCallback));
234  }
235 
240  template<typename T>
241  std::shared_ptr<GUIVstParameter<T>> asVstParameter();
242 
250  std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override;
251 
252 private:
253  ParamID fParamID;
255  std::shared_ptr<RawVstParamDef> fParamDef;
256 };
257 
258 //-------------------------------------------------------------------------------
259 // GUIRawVstParam - wrapper to make writing the code much simpler and natural
260 //-------------------------------------------------------------------------------
265 class GUIRawVstParam : public Utils::Operators::Dereferenceable<GUIRawVstParam>
266 {
267 public:
268  using ParamType = ParamValue;
270  using Editor = std::unique_ptr<EditorType>;
271 
272 public:
273  // Constructor
274  GUIRawVstParam(std::shared_ptr<GUIRawVstParameter> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
275  fPtr{std::move(iPtr)}
276  {}
277 
279  GUIRawVstParam &operator=(GUIRawVstParam const &iOther) = default;
280 
281  // exists
282  inline bool exists() const { return (bool) fPtr; }
283 
284  // getParamID
285  inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
286 
290  inline ParamValue getValue() const { DCHECK_F(exists()); return fPtr->getValue(); }
291 
293  inline ParamValue value() const { DCHECK_F(exists()); return fPtr->getValue(); }
294 
300  bool update(ParamValue const &iValue) { DCHECK_F(exists()); return fPtr->update(iValue); }
301 
306  tresult setValue(ParamValue const &iValue) { DCHECK_F(exists()); return fPtr->setValue(iValue); }
307 
310  inline tresult resetToDefault() { DCHECK_F(exists()); return fPtr->resetToDefault(); }
311 
315  tresult copyValueFrom(GUIRawVstParam const &iParam) { DCHECK_F(exists()); return setValue(iParam.getValue()); }
316 
320  inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
321 
325  void toString(String128 oString) { fPtr->toString(oString); }
326 
330  Steinberg::String toString() { DCHECK_F(exists()); return fPtr->toString(); }
331 
335  Editor edit() { DCHECK_F(exists()); return fPtr->edit(); }
336 
342  Editor edit(ParamValue const &iValue) { DCHECK_F(exists()); return fPtr->edit(iValue); }
343 
345  inline ParamValue operator *() const { DCHECK_F(exists()); return fPtr->getValue(); }
346 
348  [[deprecated("Since 4.1.0 - use operator* or .value() instead (ex: if(*param) {...} or if(param.value()) {...}")]]
349  inline operator ParamValue() const { DCHECK_F(exists()); return fPtr->getValue(); } // NOLINT
350 
352  inline GUIRawVstParam &operator=(ParamValue const &iValue) { DCHECK_F(exists()); fPtr->setValue(iValue); return *this; }
353 
356  inline std::string toUTF8String(int32 iPrecision) const { DCHECK_F(exists()); return fPtr->toUTF8String(iPrecision); }
357 
361  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
362 
366  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
367 
368 private:
369  std::shared_ptr<GUIRawVstParameter> fPtr;
370 };
371 
372 //------------------------------------------------------------------------
373 // shortcut notations
374 //------------------------------------------------------------------------
375 using GUIRawVstParamEditor = std::unique_ptr<GUIRawVstParameter::EditorType>;
376 
377 }
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
This implementation will adapt this parameter to be interpreted as a discrete parameter.
Definition: GUIRawVstParameter.cpp:144
~Editor() override
Definition: GUIRawVstParameter.h:64
ParamValue value() const
Synonym to getValue()
Definition: GUIRawVstParameter.h:293
GUIRawVstParam & operator=(ParamValue const &iValue)
Allow to write param = 0.5.
Definition: GUIRawVstParameter.h:352
std::string toUTF8String(int32 iPrecision) const
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIRawVstParameter.h:356
GUIRawVstParam(std::shared_ptr< GUIRawVstParameter > iPtr=nullptr)
Definition: GUIRawVstParameter.h:274
ParamID fParamID
Definition: GUIRawVstParameter.h:97
bool exists() const
Definition: GUIRawVstParameter.h:282
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
Represents a gui parameter with its underlying backing type T (aka ParamType).
Definition: IGUIParameter.h:33
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIRawVstParameter.h:366
bool updateValue(ParamValue const &iValue) override
Change the value of the parameter.
Definition: GUIRawVstParameter.cpp:54
ParamValue operator *() const
allow writing *param to access the underlying value (or in other words, *param is the same param....
Definition: GUIRawVstParameter.h:345
std::unique_ptr< EditorType > Editor
Definition: GUIRawVstParameter.h:270
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition: GUIRawVstParameter.h:117
Steinberg::String toString()
Returns a string representation of this parameter.
Definition: GUIRawVstParameter.h:330
std::shared_ptr< RawVstParamDef > fParamDef
Definition: GUIRawVstParameter.h:255
GUIRawVstParameter(ParamID iParamID, VstParametersSPtr iVstParameters, std::shared_ptr< RawVstParamDef > iParamDef)
Definition: GUIRawVstParameter.cpp:97
Implements all the various equality and relational operators for the type T which is assumed to encap...
Definition: Operators.h:54
ParamValue fInitialParamValue
Definition: GUIRawVstParameter.h:100
std::function< void(ParamValue const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:182
ParamID fParamID
Definition: GUIRawVstParameter.h:253
tresult setValue(ParamValue const &iValue)
Sets the value of this parameter.
Definition: GUIRawVstParameter.h:306
Editor edit(ParamValue const &iValue)
Shortcut to create an editor and set the value to it.
Definition: GUIRawVstParameter.h:342
Editor(ParamID iParamID, VstParametersSPtr iVstParameters)
Definition: GUIRawVstParameter.cpp:26
bool fIsEditing
Definition: GUIRawVstParameter.h:101
ParamValue getValue() const
Definition: GUIRawVstParameter.h:132
Definition: GUIState.h:37
tresult rollback() override
Call this if you want to revert to the original value of the parameter (when the editor is created).
Definition: GUIRawVstParameter.cpp:82
tresult setValue(ParamValue const &iValue) override
Sets the value of this parameter.
Definition: GUIRawVstParameter.h:193
bool update(ParamValue const &iValue) override
Update the parameter with a value.
Definition: GUIRawVstParameter.h:178
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:98
bool update(ParamValue const &iValue)
Update the parameter with a value.
Definition: GUIRawVstParameter.h:300
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:254
GUIRawVstParam & operator=(GUIRawVstParam const &iOther)=default
Assignment operator: fMyParam = registerParam(...);
ITGUIParameter< ParamValue >::ITEditor EditorType
Definition: GUIRawVstParameter.h:43
tresult resetToDefault() override
Resets the parameter to its default value.
Definition: GUIRawVstParameter.h:202
ParamValue getValue() const
Definition: GUIRawVstParameter.h:290
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIRawVstParameter.h:152
This class wraps a GUIRawVstParameter to deal with any type T.
Definition: GUIRawVstParameter.h:31
GUIRawVstParameter::EditorType EditorType
Definition: GUIRawVstParameter.h:269
ParamID getParamID() const
Definition: GUIRawVstParameter.h:285
int32 getStepCount() const override
Definition: GUIRawVstParameter.h:140
std::unique_ptr< GUIRawVstParameter::EditorType > GUIRawVstParamEditor
Definition: GUIRawVstParameter.h:375
tresult commit() override
Definition: GUIRawVstParameter.cpp:68
std::shared_ptr< VstParameters > VstParametersSPtr
Definition: VstParameters.h:95
tresult resetToDefault()
Resets the param to its default value.
Definition: GUIRawVstParameter.h:310
Steinberg::String toString()
Returns a string representation of this parameter.
Definition: GUIRawVstParameter.h:160
ParamValue ParamType
Definition: GUIRawVstParameter.h:42
std::shared_ptr< GUIRawVstParameter > fPtr
Definition: GUIRawVstParameter.h:369
tresult accessValue(ValueAccessor const &iGetter) const override
API to access the underlying value.
Definition: GUIRawVstParameter.h:123
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIRawVstParameter.h:325
ParamValue ParamType
Definition: GUIRawVstParameter.h:268
tresult setValue(ParamValue const &iValue) override
Change the value of the parameter.
Definition: GUIRawVstParameter.cpp:39
std::shared_ptr< GUIVstParameter< T > > asVstParameter()
Converts to a typed parameter.
Definition: GUIVstParameter.h:428
tresult copyValueFrom(GUIRawVstParam const &iParam)
Shortcut to copy the value from another param to this one.
Definition: GUIRawVstParameter.h:315
Wrapper to edit a single parameter.
Definition: GUIRawVstParameter.h:59
Encapsulates a vst parameter and how to access it (read/write) as well as how to "connect" to it in o...
Definition: GUIRawVstParameter.h:39
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIRawVstParameter.h:361
std::string toUTF8String(int32 iPrecision) const override
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIRawVstParameter.h:168
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition: GUIRawVstParameter.h:223
std::unique_ptr< EditorType > edit() override
Definition: GUIRawVstParameter.h:210
Editor edit()
Definition: GUIRawVstParameter.h:335
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition: GUIRawVstParameter.h:231
int32 getStepCount() const
Definition: GUIRawVstParameter.h:320