Jamba C++ API 8.0.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:
57 virtual ~IGUIParameter() = default;
58
62 class Editor
63 {
64 public:
69 virtual tresult commit() = 0;
70
75 virtual tresult rollback() = 0;
76
81 virtual ~Editor() = default;
82 };
83
84public:
90 virtual ParamID getParamID() const = 0;
91
101 virtual int32 getStepCount() const = 0;
102
109 virtual std::string toUTF8String(int32 iPrecision) const = 0;
110
113 virtual tresult resetToDefault() = 0;
114
125 virtual std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const = 0;
126
137 virtual std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const = 0;
138
139public:
146 template<typename T>
147 std::shared_ptr<ITGUIParameter<T>> cast();
148
159 virtual std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) = 0;
160};
161
162//------------------------------------------------------------------------
163// ITGUIParameter<T>
164//------------------------------------------------------------------------
173template<typename T>
175{
176public:
179 using ParamType = T;
180
186 using ValueAccessor = std::function<void(T const &)>;
187
188public:
240 class ITEditor : public Editor
241 {
242 public:
248 virtual tresult setValue(ParamType const &iValue) = 0;
249
259 virtual bool updateValue(ParamType const &iValue) = 0;
260
263 using Editor::commit;
264
267 virtual tresult commit(ParamType const &iValue)
268 {
269 auto res = setValue(iValue);
270 if(res == kResultOk)
271 return commit();
272 else
273 return res;
274 }
275 };
276
277public:
289 virtual tresult accessValue(ValueAccessor const &iGetter) const = 0;
290
300 virtual bool update(ParamType const &iValue) = 0;
301
307 virtual tresult setValue(ParamType const &iValue) = 0;
308
314 virtual std::unique_ptr<ITEditor> edit() = 0;
315
319 virtual std::unique_ptr<ITEditor> edit(ParamType const &iValue)
320 {
321 auto editor = edit();
322 editor->setValue(iValue);
323 return editor;
324 }
325};
326
327//------------------------------------------------------------------------
328// DefaultEditorImpl
329//------------------------------------------------------------------------
336template<typename T>
338{
339public:
340 // Constructor
341 explicit DefaultEditorImpl(ITGUIParameter<T> *iGUIParameter, T const &iDefaultValue) :
342 fGUIParameter{iGUIParameter},
343 fInitialValue{iDefaultValue}
344 {
345 }
346
347 // Destructor
348 ~DefaultEditorImpl() override { rollback(); }
349
350 // updateValue
351 bool updateValue(T const &iValue) override
352 {
353 if(fDoneEditing)
354 return false;
355 return fGUIParameter->update(iValue);
356 }
357
358 // setValue
359 tresult setValue(T const &iValue) override
360 {
361 if(fDoneEditing)
362 return kResultFalse;
363 fGUIParameter->update(iValue);
364 return kResultOk;
365 }
366
369 using ITGUIParameter<T>::ITEditor::commit;
370
371 // commit
372 tresult commit() override
373 {
374 if(fDoneEditing)
375 return kResultFalse;
376 fDoneEditing = true;
377 return kResultOk;
378 }
379
380 // rollback
381 tresult rollback() override
382 {
383 auto res = setValue(fInitialValue);
384 fDoneEditing = true;
385 return res;
386 }
387
388private:
391 bool fDoneEditing{false};
392};
393
394//------------------------------------------------------------------------
395// IGUIParam
396//------------------------------------------------------------------------
406{
407public:
409 IGUIParam(std::shared_ptr<IGUIParameter> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
410 fPtr{std::move(iPtr)}
411 {}
412
415 bool exists() const { return (bool) fPtr; }
416
419 ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
420
423 int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
424
427 std::string toUTF8String(int32 iPrecision) const { DCHECK_F(exists()); return fPtr->toUTF8String(iPrecision); }
428
431 void resetToDefault() { DCHECK_F(exists()); fPtr->resetToDefault(); }
432
435 std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
436
439 std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
440
441private:
442 std::shared_ptr<IGUIParameter> fPtr;
443};
444
445
446}
~DefaultEditorImpl() override
Definition IGUIParameter.h:348
tresult commit() override
Commits all the changes applied to the parameter.
Definition IGUIParameter.h:372
T fInitialValue
Definition IGUIParameter.h:390
tresult rollback() override
Rollbacks all the changes that were made to this parameter (since this editor was created).
Definition IGUIParameter.h:381
ITGUIParameter< T > * fGUIParameter
Definition IGUIParameter.h:389
DefaultEditorImpl(ITGUIParameter< T > *iGUIParameter, T const &iDefaultValue)
Definition IGUIParameter.h:341
bool fDoneEditing
Definition IGUIParameter.h:391
tresult setValue(T const &iValue) override
Unconditionally sets the value of the parameter to the value provided.
Definition IGUIParameter.h:359
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:351
int32 getStepCount() const
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
Definition IGUIParameter.h:423
void resetToDefault()
Resets the parameter to its default value.
Definition IGUIParameter.h:431
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:427
ParamID getParamID() const
Each parameter has a unique ID returned by this method.
Definition IGUIParameter.h:419
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:439
bool exists() const
Returns true if this wrapper actually refers to an actual parameter.
Definition IGUIParameter.h:415
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:435
IGUIParam(std::shared_ptr< IGUIParameter > iPtr=nullptr)
Constructor.
Definition IGUIParameter.h:409
std::shared_ptr< IGUIParameter > fPtr
Definition IGUIParameter.h:442
Defines the basic and common API of all parameter editors (allow to commit/rollback).
Definition IGUIParameter.h:63
virtual tresult rollback()=0
Rollbacks 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 ~IGUIParameter()=default
Destructor (needed for polymorphic base type).
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:241
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:267
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:175
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:319
virtual tresult accessValue(ValueAccessor const &iGetter) const =0
API to access the underlying value.
T ParamType
Definition IGUIParameter.h:179
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(T const &)> ValueAccessor
Definition IGUIParameter.h:186
Interface to implement to receive parameter changes.
Definition Parameters.h:47
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition Parameters.h:61
Definition GUIState.h:40