Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
IGUIParameter.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019-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
20#pragma once
21
22#include <pluginterfaces/vst/vsttypes.h>
23#include <pluginterfaces/base/ftypes.h>
24#include <memory>
27
29
30using namespace Steinberg;
31using namespace Steinberg::Vst;
32
33// forward declaration required for compilation
34template<typename T> class ITGUIParameter;
35
41using GUIDiscreteParameter = ITGUIParameter<int32>;
42
43//------------------------------------------------------------------------
44// IGUIParameter
45//------------------------------------------------------------------------
53class IGUIParameter : public std::enable_shared_from_this<IGUIParameter>
54{
55public:
59 class Editor
60 {
61 public:
66 virtual tresult commit() = 0;
67
72 virtual tresult rollback() = 0;
73
78 virtual ~Editor() = default;
79 };
80
81public:
87 virtual ParamID getParamID() const = 0;
88
98 virtual int32 getStepCount() const = 0;
99
106 virtual std::string toUTF8String(int32 iPrecision) const = 0;
107
110 virtual tresult resetToDefault() = 0;
111
122 virtual std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const = 0;
123
134 virtual std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const = 0;
135
136public:
143 template<typename T>
144 std::shared_ptr<ITGUIParameter<T>> cast();
145
156 virtual std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) = 0;
157};
158
159//------------------------------------------------------------------------
160// ITGUIParameter<T>
161//------------------------------------------------------------------------
170template<typename T>
172{
173public:
176 using ParamType = T;
177
183 using ValueAccessor = std::function<void(T const &)>;
184
185public:
238 {
239 public:
245 virtual tresult setValue(ParamType const &iValue) = 0;
246
256 virtual bool updateValue(ParamType const &iValue) = 0;
257
261
264 virtual tresult commit(ParamType const &iValue)
265 {
266 auto res = setValue(iValue);
267 if(res == kResultOk)
268 return commit();
269 else
270 return res;
271 };
272 };
273
274public:
286 virtual tresult accessValue(ValueAccessor const &iGetter) const = 0;
287
297 virtual bool update(ParamType const &iValue) = 0;
298
304 virtual tresult setValue(ParamType const &iValue) = 0;
305
311 virtual std::unique_ptr<ITEditor> edit() = 0;
312
316 virtual std::unique_ptr<ITEditor> edit(ParamType const &iValue)
317 {
318 auto editor = edit();
319 editor->setValue(iValue);
320 return editor;
321 }
322};
323
324//------------------------------------------------------------------------
325// DefaultEditorImpl
326//------------------------------------------------------------------------
333template<typename T>
334class DefaultEditorImpl : public ITGUIParameter<T>::ITEditor
335{
336public:
337 // Constructor
338 explicit DefaultEditorImpl(ITGUIParameter<T> *iGUIParameter, T const &iDefaultValue) :
339 fGUIParameter{iGUIParameter},
340 fInitialValue{iDefaultValue}
341 {
342 }
343
344 // Destructor
345 ~DefaultEditorImpl() override { rollback(); }
346
347 // updateValue
348 bool updateValue(T const &iValue) override
349 {
350 if(fDoneEditing)
351 return false;
352 return fGUIParameter->update(iValue);
353 }
354
355 // setValue
356 tresult setValue(T const &iValue) override
357 {
358 if(fDoneEditing)
359 return kResultFalse;
360 fGUIParameter->update(iValue);
361 return kResultOk;
362 }
363
366 using ITGUIParameter<T>::ITEditor::commit;
367
368 // commit
369 tresult commit() override
370 {
371 if(fDoneEditing)
372 return kResultFalse;
373 fDoneEditing = true;
374 return kResultOk;
375 }
376
377 // rollback
378 tresult rollback() override
379 {
380 auto res = setValue(fInitialValue);
381 fDoneEditing = true;
382 return res;
383 }
384
385private:
388 bool fDoneEditing{false};
389};
390
391//------------------------------------------------------------------------
392// IGUIParam
393//------------------------------------------------------------------------
403{
404public:
406 IGUIParam(std::shared_ptr<IGUIParameter> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
407 fPtr{std::move(iPtr)}
408 {}
409
412 inline bool exists() const { return (bool) fPtr; }
413
416 inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
417
420 inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
421
424 inline std::string toUTF8String(int32 iPrecision) const { DCHECK_F(exists()); return fPtr->toUTF8String(iPrecision); }
425
428 inline void resetToDefault() { DCHECK_F(exists()); fPtr->resetToDefault(); }
429
432 inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
433
436 std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
437
438private:
439 std::shared_ptr<IGUIParameter> fPtr;
440};
441
442
443}
~DefaultEditorImpl() override
Definition IGUIParameter.h:345
tresult commit() override
Commits all the changes applied to the parameter.
Definition IGUIParameter.h:369
T fInitialValue
Definition IGUIParameter.h:387
tresult rollback() override
Rollback all the changes that were made to this parameter (since this editor was created).
Definition IGUIParameter.h:378
ITGUIParameter< T > * fGUIParameter
Definition IGUIParameter.h:386
DefaultEditorImpl(ITGUIParameter< T > *iGUIParameter, T const &iDefaultValue)
Definition IGUIParameter.h:338
bool fDoneEditing
Definition IGUIParameter.h:388
tresult setValue(T const &iValue) override
Unconditionally sets the value of the parameter to the value provided.
Definition IGUIParameter.h:356
bool updateValue(T const &iValue) override
First check if the value provided (iValue) is different from the current value and if that is the cas...
Definition IGUIParameter.h:348
int32 getStepCount() const
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
Definition IGUIParameter.h:420
void resetToDefault()
Resets the parameter to its default value.
Definition IGUIParameter.h:428
std::string toUTF8String(int32 iPrecision) const
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition IGUIParameter.h:424
ParamID getParamID() const
Each parameter has a unique ID returned by this method.
Definition IGUIParameter.h:416
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Creates a connection between this parameter and the callback: whenever the parameter changes,...
Definition IGUIParameter.h:436
bool exists() const
Returns true if this wrapper actually refers to an actual parameter.
Definition IGUIParameter.h:412
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Creates a connection between this parameter and the change listener: whenever the parameter changes,...
Definition IGUIParameter.h:432
IGUIParam(std::shared_ptr< IGUIParameter > iPtr=nullptr)
Constructor.
Definition IGUIParameter.h:406
std::shared_ptr< IGUIParameter > fPtr
Definition IGUIParameter.h:439
Defines the basic and common API of all parameter editors (allow to commit/rollback).
Definition IGUIParameter.h:60
virtual tresult rollback()=0
Rollback all the changes that were made to this parameter (since this editor was created).
virtual tresult commit()=0
Commits all the changes applied to the parameter.
virtual ~Editor()=default
Technically the destructor must only call rollback(), but due to the fact that it is a virtual method...
A discrete parameter is defined by a parameter whose underlying backing type is an int32 and whose nu...
Definition IGUIParameter.h:54
virtual std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const =0
Creates a connection between this parameter and the change listener: whenever the parameter changes,...
virtual std::string toUTF8String(int32 iPrecision) const =0
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
virtual std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount)=0
Converts this parameter into a discrete parameter.
virtual tresult resetToDefault()=0
Resets the parameter to its default value.
virtual ParamID getParamID() const =0
Each parameter has a unique ID returned by this method.
virtual std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const =0
Creates a connection between this parameter and the callback: whenever the parameter changes,...
virtual int32 getStepCount() const =0
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
std::shared_ptr< ITGUIParameter< T > > cast()
Downcasts this parameter into a typed version.
Definition IGUIParameter.hpp:30
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition IGUIParameter.h:238
virtual bool updateValue(ParamType const &iValue)=0
First check if the value provided (iValue) is different from the current value and if that is the cas...
virtual tresult commit()=0
Importing other commit method from superclass.
virtual tresult commit(ParamType const &iValue)
Shortcut method which calls setValue(ParamType const &) followed by commit().
Definition IGUIParameter.h:264
virtual tresult setValue(ParamType const &iValue)=0
Unconditionally sets the value of the parameter to the value provided.
Represents a gui parameter with its underlying backing type T (aka ParamType).
Definition IGUIParameter.h:172
virtual std::unique_ptr< ITEditor > edit(ParamType const &iValue)
Shortcut api which creates an editor followed by ITEditor::setValue(ParamType const &) to set the par...
Definition IGUIParameter.h:316
virtual tresult accessValue(ValueAccessor const &iGetter) const =0
API to access the underlying value.
virtual std::unique_ptr< ITEditor > edit()=0
Creates an editor to modify the parameter in a transactional fashion.
virtual bool update(ParamType const &iValue)=0
First check if the value provided (iValue) is different from the current value and if that is the cas...
virtual tresult setValue(ParamType const &iValue)=0
Unconditionally sets the value of the parameter to the value provided.
std::function< void(ParamValue const &)> ValueAccessor
Definition IGUIParameter.h:183
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