Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
GUIVstParameter.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 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include "IGUIParameter.h"
22#include "GUIRawVstParameter.h"
26
28
32template<typename T>
34{
35public:
36 using ParamType = T;
38
39public:
53 class Editor : public EditorType
54 {
55 public:
56 inline explicit Editor(GUIRawVstParamEditor iRawEditor,
57 std::shared_ptr<IParamConverter<T>> iConverter) :
58 fRawEditor{std::move(iRawEditor)},
59 fConverter{std::move(iConverter)}
60 {
61 }
62
63 ~Editor() override { rollback(); }
64
65 // disabling copy
66 Editor(Editor const &) = delete;
67 Editor& operator=(Editor const &) = delete;
68
72 tresult setValue(ParamType const &iValue) override
73 {
74 return fRawEditor->setValue(fConverter->normalize(iValue));
75 }
76
81 bool updateValue(ParamType const &iValue) override
82 {
83 return fRawEditor->updateValue(fConverter->normalize(iValue));
84 }
85
86 /*
87 * Call when you are done with the modifications.
88 * This has no effect if rollback() has already been called
89 */
90 tresult commit() override
91 {
92 return fRawEditor->commit();
93 }
94
95 // importing superclass commit methods
96 using EditorType::commit;
97
102 tresult rollback() override
103 {
104 return fRawEditor->rollback();
105 }
106
107 private:
109 std::shared_ptr<IParamConverter<T>> fConverter;
110 };
111
112public:
113 // Constructor
114 GUIVstParameter(std::shared_ptr<GUIRawVstParameter> iRawParameter,
115 std::shared_ptr<IParamConverter<T>> iConverter) :
116 fRawParameter{std::move(iRawParameter)},
117 fConverter{std::move(iConverter)}
118 {
119 DCHECK_NOTNULL_F(fRawParameter.get());
120 DCHECK_NOTNULL_F(fConverter.get());
121 // DLOG_F(INFO, "VSTParameter::VSTParameter(%d)", fRawParameter->getParamID());
122 }
123
124 // Destructor
126 {
127 // DLOG_F(INFO, "VSTParameter::~VSTParameter(%d)", fRawParameter->getParamID());
128 }
129
130 // getParamID
131 ParamID getParamID() const override
132 {
133 return fRawParameter->getParamID();
134 }
135
136 // accessValue
137 tresult accessValue(typename ITGUIParameter<T>::ValueAccessor const &iGetter) const override
138 {
139 iGetter(getValue());
140 return kResultOk;
141 }
142
147 {
148 return fConverter->denormalize(fRawParameter->getValue());
149 }
150
154 ParamValue getNormalizedValue() const
155 {
156 return fRawParameter->getValue();
157 }
158
164 bool update(ParamType const &iValue) override
165 {
166 // Implementation note: because ParamType may not define `operator!=` we use the normalized value instead
167 auto currentNormalizedValue = getNormalizedValue();
168 auto newNormalizedValued = fConverter->normalize(iValue);
169 if(currentNormalizedValue != newNormalizedValued)
170 {
171 if(setNormalizedValue(newNormalizedValued) == kResultOk)
172 return true;
173 }
174 return false;
175 }
176
181 tresult setValue(ParamType const &iValue) override
182 {
183 return fRawParameter->setValue(fConverter->normalize(iValue));
184 }
185
190 tresult setNormalizedValue(ParamValue const &iNormalizedValue)
191 {
192 return fRawParameter->setValue(iNormalizedValue);
193 }
194
197 tresult resetToDefault() override
198 {
199 return fRawParameter->resetToDefault();
200 }
201
205 inline int32 getStepCount() const override { return fConverter->getStepCount(); }
206
210 void toString(String128 oString)
211 {
212 fRawParameter->toString(oString);
213 }
214
218 Steinberg::String toString()
219 {
220 return fRawParameter->toString();
221 }
222
223 // toUTF8String
224 std::string toUTF8String(int32 iPrecision) const override
225 {
226 return fConverter->toString(getValue(), iPrecision);
227 }
228
232 std::unique_ptr<EditorType> edit() override
233 {
234 return std::make_unique<Editor>(fRawParameter->edit(), fConverter);
235 }
236
240 using ITGUIParameter<T>::edit;
241
245 std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
246 {
247 return fRawParameter->connect(iChangeListener);
248 }
249
253 std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
254 {
255 return fRawParameter->connect(std::move(iChangeCallback));
256 }
257
258 // asDiscreteParameter
259 std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override
260 {
261 return fRawParameter->asDiscreteParameter(iStepCount);
262 }
263
264private:
265 std::shared_ptr<GUIRawVstParameter> fRawParameter;
266 std::shared_ptr<IParamConverter<T>> fConverter;
267};
268
269//------------------------------------------------------------------------
270// GUIVstParam - wrapper to make writing the code much simpler and natural
271//------------------------------------------------------------------------
278template<typename T>
280{
281public:
282 using ParamType = T;
284 using Editor = std::unique_ptr<EditorType>;
285
286public:
291 class Value {
292 public:
293 constexpr T const *operator ->() const { return &fValue; }
294 friend class GUIVstParam<T>;
295 private:
296 explicit Value(T const &iValue) : fValue{iValue} {}
298 };
299
300public:
301 // Constructor
302 GUIVstParam(std::shared_ptr<GUIVstParameter<T>> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
303 fPtr{std::move(iPtr)}
304 {}
305
307 GUIVstParam<T> &operator=(GUIVstParam<T> const &iOther) = default;
308
309 // exists
310 inline bool exists() const { return (bool) fPtr; }
311
312 // getParamID
313 inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
314
318 inline T getValue() const { DCHECK_F(exists()); return fPtr->getValue(); }
319
321 inline T value() const { DCHECK_F(exists()); return fPtr->getValue(); }
322
326 inline ParamValue getNormalizedValue() const { DCHECK_F(exists()); return fPtr->getNormalizedValue(); }
327
333 bool update(T const &iValue) { DCHECK_F(exists()); return fPtr->update(iValue); }
334
339 tresult setValue(T const &iValue) { DCHECK_F(exists()); return fPtr->setValue(iValue); }
340
345 tresult setNormalizedValue(ParamValue const &iNormalizedValue) { DCHECK_F(exists()); return fPtr->setNormalizedValue(iNormalizedValue); }
346
349 inline tresult resetToDefault() { DCHECK_F(exists()); return fPtr->resetToDefault(); }
350
355 template<typename V>
356 tresult copyValueFrom(GUIVstParam<V> const &iParam) { DCHECK_F(exists()); return setNormalizedValue(iParam.getNormalizedValue()); }
357
361 tresult copyValueFrom(GUIRawVstParam const &iParam) { DCHECK_F(exists()); return setNormalizedValue(iParam.getValue()); }
362
366 inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
367
371 void toString(String128 oString) { DCHECK_F(exists()); fPtr->toString(oString); }
372
376 Steinberg::String toString() { DCHECK_F(exists()); return fPtr->toString(); }
377
380 inline std::string toUTF8String(int32 iPrecision) const { DCHECK_F(exists()); return fPtr->toUTF8String(iPrecision); }
381
385 Editor edit() { DCHECK_F(exists()); return fPtr->edit(); }
386
392 Editor edit(T const &iValue) { DCHECK_F(exists()); return fPtr->edit(iValue); }
393
395 constexpr T operator *() const { DCHECK_F(exists()); return fPtr->getValue(); }
396
398 constexpr Value operator ->() const { DCHECK_F(exists()); return Value{fPtr->getValue()}; }
399
401 [[deprecated("Since 4.1.0 - use operator* or .value() instead (ex: if(*param) {...} or if(param.value()) {...}")]]
402 inline operator T() const { DCHECK_F(exists()); return fPtr->getValue(); } // NOLINT
403
405 inline GUIVstParam<T> &operator=(T const &iValue) { DCHECK_F(exists()); fPtr->setValue(iValue); return *this; }
406
408// friend constexpr bool operator==(GUIVstParam<T> const &lhs, GUIVstParam<T> const &rhs) { return lhs.fPtr->getNormalizedValue() == rhs.fPtr->getNormalizedValue(); }
409
413 inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
414
418 inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
419
420private:
421 std::shared_ptr<GUIVstParameter<T>> fPtr;
422};
423
424
425//------------------------------------------------------------------------
426// GUIRawVstParameter::asVstParameter
427//------------------------------------------------------------------------
428template<typename T>
429std::shared_ptr<GUIVstParameter<T>> GUIRawVstParameter::asVstParameter()
430{
431 auto vstParamDef = std::dynamic_pointer_cast<VstParamDef<T>>(fParamDef);
432 if(vstParamDef && vstParamDef->fConverter)
433 return std::make_shared<GUIVstParameter<T>>(std::dynamic_pointer_cast<GUIRawVstParameter>(shared_from_this()),
434 vstParamDef->fConverter);
435 else
436 return nullptr;
437}
438
439//------------------------------------------------------------------------
440// shortcut notations
441//------------------------------------------------------------------------
442template<typename T>
443using GUIVstParamEditor = std::unique_ptr<typename GUIVstParameter<T>::EditorType>;
444
447
448}
Implements all the various equality and relational operators for the type T which is assumed to encap...
Definition Operators.h:56
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition GUIRawVstParameter.h:267
ParamValue getValue() const
Definition GUIRawVstParameter.h:291
std::shared_ptr< RawVstParamDef > fParamDef
Definition GUIRawVstParameter.h:256
std::shared_ptr< GUIVstParameter< T > > asVstParameter()
Converts to a typed parameter.
Definition GUIVstParameter.h:429
constexpr T const * operator->() const
Definition GUIVstParameter.h:293
Value(T const &iValue)
Definition GUIVstParameter.h:296
T fValue
Definition GUIVstParameter.h:297
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition GUIVstParameter.h:280
bool update(T const &iValue)
Update the parameter with a value.
Definition GUIVstParameter.h:333
tresult copyValueFrom(GUIVstParam< V > const &iParam)
Shortcut to copy the value from another param to this one.
Definition GUIVstParameter.h:356
tresult setValue(T const &iValue)
Sets the value of this parameter.
Definition GUIVstParameter.h:339
typename GUIVstParameter< T >::ITEditor EditorType
Definition GUIVstParameter.h:283
int32 getStepCount() const
Definition GUIVstParameter.h:366
std::string toUTF8String(int32 iPrecision) const
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition GUIVstParameter.h:380
Editor edit(T const &iValue)
Shortcut to create an editor and set the value to it.
Definition GUIVstParameter.h:392
T getValue() const
Definition GUIVstParameter.h:318
constexpr T operator*() const
allow writing *param to access the underlying value (or in other words, *param is the same param....
Definition GUIVstParameter.h:395
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition GUIVstParameter.h:371
ParamID getParamID() const
Definition GUIVstParameter.h:313
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition GUIVstParameter.h:418
GUIVstParam< T > & operator=(T const &iValue)
Allow to write param = 3.0.
Definition GUIVstParameter.h:405
bool exists() const
Definition GUIVstParameter.h:310
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Allow to write param1 == param2.
Definition GUIVstParameter.h:413
tresult copyValueFrom(GUIRawVstParam const &iParam)
Shortcut to copy the value from another param to this one (raw value).
Definition GUIVstParameter.h:361
ParamValue getNormalizedValue() const
Definition GUIVstParameter.h:326
T value() const
Synonym to getValue().
Definition GUIVstParameter.h:321
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Sets the value of this parameter as a normalized value.
Definition GUIVstParameter.h:345
T ParamType
Definition GUIVstParameter.h:282
constexpr Value operator->() const
allow writing param->x to access the underlying value when T is a struct or class
Definition GUIVstParameter.h:398
tresult resetToDefault()
Resets the param to its default value.
Definition GUIVstParameter.h:349
std::unique_ptr< EditorType > Editor
Definition GUIVstParameter.h:284
GUIVstParam(std::shared_ptr< GUIVstParameter< T > > iPtr=nullptr)
Definition GUIVstParameter.h:302
GUIVstParam< T > & operator=(GUIVstParam< T > const &iOther)=default
Assignment operator: fMyParam = registerParam(...);.
Editor edit()
Definition GUIVstParameter.h:385
std::shared_ptr< GUIVstParameter< bool > > fPtr
Definition GUIVstParameter.h:421
Steinberg::String toString()
Returns a string representation of this parameter.
Definition GUIVstParameter.h:376
tresult commit() override
Definition GUIVstParameter.h:90
bool updateValue(ParamType const &iValue) override
Change the value of the parameter.
Definition GUIVstParameter.h:81
tresult rollback() override
Call this if you want to revert to the original value of the parameter (when the editor is created).
Definition GUIVstParameter.h:102
GUIRawVstParamEditor fRawEditor
Definition GUIVstParameter.h:108
tresult setValue(ParamType const &iValue) override
Change the value of the parameter.
Definition GUIVstParameter.h:72
~Editor() override
Definition GUIVstParameter.h:63
Editor(GUIRawVstParamEditor iRawEditor, std::shared_ptr< IParamConverter< T > > iConverter)
Definition GUIVstParameter.h:56
std::shared_ptr< IParamConverter< T > > fConverter
Definition GUIVstParameter.h:109
This class wraps a GUIRawVstParameter to deal with any type T.
Definition GUIVstParameter.h:34
std::string toUTF8String(int32 iPrecision) const override
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition GUIVstParameter.h:224
int32 getStepCount() const override
Definition GUIVstParameter.h:205
std::shared_ptr< GUIRawVstParameter > fRawParameter
Definition GUIVstParameter.h:265
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition GUIVstParameter.h:131
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
Converts this parameter into a discrete parameter.
Definition GUIVstParameter.h:259
tresult resetToDefault() override
Resets the parameter to its default value.
Definition GUIVstParameter.h:197
~GUIVstParameter()
Definition GUIVstParameter.h:125
tresult setValue(ParamType const &iValue) override
Sets the value of this parameter.
Definition GUIVstParameter.h:181
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition GUIVstParameter.h:210
std::unique_ptr< EditorType > edit() override
Definition GUIVstParameter.h:232
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition GUIVstParameter.h:253
GUIVstParameter(std::shared_ptr< GUIRawVstParameter > iRawParameter, std::shared_ptr< IParamConverter< T > > iConverter)
Definition GUIVstParameter.h:114
ParamType getValue() const
Definition GUIVstParameter.h:146
bool update(ParamType const &iValue) override
Update the parameter with a value.
Definition GUIVstParameter.h:164
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition GUIVstParameter.h:245
ParamValue getNormalizedValue() const
Definition GUIVstParameter.h:154
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Sets the value of this parameter as a normalized value.
Definition GUIVstParameter.h:190
T ParamType
Definition GUIVstParameter.h:36
typename ITGUIParameter< T >::ITEditor EditorType
Definition GUIVstParameter.h:37
tresult accessValue(typename ITGUIParameter< T >::ValueAccessor const &iGetter) const override
Definition GUIVstParameter.h:137
std::shared_ptr< IParamConverter< T > > fConverter
Definition GUIVstParameter.h:266
Steinberg::String toString()
Returns a string representation of this parameter.
Definition GUIVstParameter.h:218
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition IGUIParameter.h:238
Represents a gui parameter with its underlying backing type T (aka ParamType).
Definition IGUIParameter.h:172
std::function< void(T const &)> ValueAccessor
API to access the value of the param.
Definition IGUIParameter.h:183
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition ParamConverters.h:55
Interface to implement to receive parameter changes.
Definition Parameters.h:45
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition Parameters.h:56
Definition GUIState.h:38
GUIVstParam< bool > GUIVstBooleanParam
Definition GUIVstParameter.h:445
std::unique_ptr< typename GUIVstParameter< T >::EditorType > GUIVstParamEditor
Definition GUIVstParameter.h:443
GUIVstParam< Percent > GUIVstPercentParam
Definition GUIVstParameter.h:446
std::unique_ptr< GUIRawVstParameter::EditorType > GUIRawVstParamEditor
Definition GUIRawVstParameter.h:376