Jamba C++ API  6.3.0
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 (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 
19 #pragma once
20 
21 #include <pluginterfaces/vst/vsttypes.h>
22 #include <pluginterfaces/base/ftypes.h>
23 #include <memory>
26 
28 
29 using namespace Steinberg;
30 using namespace Steinberg::Vst;
31 
32 // forward declaration required for compilation
33 template<typename T> class ITGUIParameter;
34 
41 
42 //------------------------------------------------------------------------
43 // IGUIParameter
44 //------------------------------------------------------------------------
52 class IGUIParameter : public std::enable_shared_from_this<IGUIParameter>
53 {
54 public:
58  class Editor
59  {
60  public:
65  virtual tresult commit() = 0;
66 
71  virtual tresult rollback() = 0;
72 
77  virtual ~Editor() = default;
78  };
79 
80 public:
86  virtual ParamID getParamID() const = 0;
87 
97  virtual int32 getStepCount() const = 0;
98 
105  virtual std::string toUTF8String(int32 iPrecision) const = 0;
106 
109  virtual tresult resetToDefault() = 0;
110 
121  virtual std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const = 0;
122 
133  virtual std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const = 0;
134 
135 public:
142  template<typename T>
143  std::shared_ptr<ITGUIParameter<T>> cast();
144 
155  virtual std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) = 0;
156 };
157 
158 //------------------------------------------------------------------------
159 // ITGUIParameter<T>
160 //------------------------------------------------------------------------
169 template<typename T>
170 class ITGUIParameter : public IGUIParameter
171 {
172 public:
175  using ParamType = T;
176 
182  using ValueAccessor = std::function<void(T const &)>;
183 
184 public:
237  {
238  public:
244  virtual tresult setValue(ParamType const &iValue) = 0;
245 
255  virtual bool updateValue(ParamType const &iValue) = 0;
256 
260 
263  virtual tresult commit(ParamType const &iValue)
264  {
265  auto res = setValue(iValue);
266  if(res == kResultOk)
267  return commit();
268  else
269  return res;
270  };
271  };
272 
273 public:
285  virtual tresult accessValue(ValueAccessor const &iGetter) const = 0;
286 
296  virtual bool update(ParamType const &iValue) = 0;
297 
303  virtual tresult setValue(ParamType const &iValue) = 0;
304 
310  virtual std::unique_ptr<ITEditor> edit() = 0;
311 
315  virtual std::unique_ptr<ITEditor> edit(ParamType const &iValue)
316  {
317  auto editor = edit();
318  editor->setValue(iValue);
319  return editor;
320  }
321 };
322 
323 //------------------------------------------------------------------------
324 // DefaultEditorImpl
325 //------------------------------------------------------------------------
332 template<typename T>
333 class DefaultEditorImpl : public ITGUIParameter<T>::ITEditor
334 {
335 public:
336  // Constructor
337  explicit DefaultEditorImpl(ITGUIParameter<T> *iGUIParameter, T const &iDefaultValue) :
338  fGUIParameter{iGUIParameter},
339  fInitialValue{iDefaultValue}
340  {
341  }
342 
343  // Destructor
344  ~DefaultEditorImpl() override { rollback(); }
345 
346  // updateValue
347  bool updateValue(T const &iValue) override
348  {
349  if(fDoneEditing)
350  return false;
351  return fGUIParameter->update(iValue);
352  }
353 
354  // setValue
355  tresult setValue(T const &iValue) override
356  {
357  if(fDoneEditing)
358  return kResultFalse;
359  fGUIParameter->update(iValue);
360  return kResultOk;
361  }
362 
366 
367  // commit
368  tresult commit() override
369  {
370  if(fDoneEditing)
371  return kResultFalse;
372  fDoneEditing = true;
373  return kResultOk;
374  }
375 
376  // rollback
377  tresult rollback() override
378  {
379  auto res = setValue(fInitialValue);
380  fDoneEditing = true;
381  return res;
382  }
383 
384 private:
387  bool fDoneEditing{false};
388 };
389 
390 //------------------------------------------------------------------------
391 // IGUIParam
392 //------------------------------------------------------------------------
402 {
403 public:
405  IGUIParam(std::shared_ptr<IGUIParameter> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
406  fPtr{std::move(iPtr)}
407  {}
408 
411  inline bool exists() const { return (bool) fPtr; }
412 
415  inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
416 
419  inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
420 
423  inline std::string toUTF8String(int32 iPrecision) const { DCHECK_F(exists()); return fPtr->toUTF8String(iPrecision); }
424 
427  inline void resetToDefault() { DCHECK_F(exists()); fPtr->resetToDefault(); }
428 
431  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
432 
435  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
436 
437 private:
438  std::shared_ptr<IGUIParameter> fPtr;
439 };
440 
441 
442 }
ITGUIParameter< T > * fGUIParameter
Definition: IGUIParameter.h:385
tresult commit() override
Commits all the changes applied to the parameter.
Definition: IGUIParameter.h:368
DefaultEditorImpl(ITGUIParameter< T > *iGUIParameter, T const &iDefaultValue)
Definition: IGUIParameter.h:337
IGUIParam(std::shared_ptr< IGUIParameter > iPtr=nullptr)
Constructor.
Definition: IGUIParameter.h:405
This is the base class of all GUI parameters.
Definition: IGUIParameter.h:52
tresult setValue(T const &iValue) override
Unconditionally sets the value of the parameter to the value provided.
Definition: IGUIParameter.h:355
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::shared_ptr< IGUIParameter > fPtr
Definition: IGUIParameter.h:438
virtual tresult commit()=0
Commits all the changes applied to the parameter.
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:315
std::string toUTF8String(T const &iValue, Steinberg::int32 iPrecision)
This generic function will determine (at compilation time) whether T can be written to an ostream and...
Definition: Utils.h:57
std::function< void(ParamValue const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:182
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:431
~DefaultEditorImpl() override
Definition: IGUIParameter.h:344
Definition: GUIState.h:37
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:347
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:435
ParamValue ParamType
The type of the param (alias)
Definition: IGUIParameter.h:175
tresult rollback() override
Rollback all the changes that were made to this parameter (since this editor was created).
Definition: IGUIParameter.h:377
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:423
Defines the basic and common API of all parameter editors (allow to commit/rollback)
Definition: IGUIParameter.h:58
virtual tresult commit(ParamType const &iValue)
Shortcut method which calls setValue(ParamType const &) followed by commit().
Definition: IGUIParameter.h:263
Default implementation of the editor interface.
Definition: IGUIParameter.h:333
bool exists() const
Returns true if this wrapper actually refers to an actual parameter.
Definition: IGUIParameter.h:411
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:236
int32 getStepCount() const
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
Definition: IGUIParameter.h:419
ParamID getParamID() const
Each parameter has a unique ID returned by this method.
Definition: IGUIParameter.h:415
Wrapper instance returned by ParamAware::registerBaseParam() methods.
Definition: IGUIParameter.h:401
void resetToDefault()
Resets the parameter to its default value.
Definition: IGUIParameter.h:427
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
T fInitialValue
Definition: IGUIParameter.h:386