Jamba C++ API  4.1.0
IGUIParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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 
117  virtual std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const = 0;
118 
129  virtual std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const = 0;
130 
131 public:
138  template<typename T>
139  std::shared_ptr<ITGUIParameter<T>> cast();
140 
151  virtual std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) = 0;
152 };
153 
154 //------------------------------------------------------------------------
155 // ITGUIParameter<T>
156 //------------------------------------------------------------------------
165 template<typename T>
166 class ITGUIParameter : public IGUIParameter
167 {
168 public:
171  using ParamType = T;
172 
178  using ValueAccessor = std::function<void(T const &)>;
179 
180 public:
233  {
234  public:
240  virtual tresult setValue(ParamType const &iValue) = 0;
241 
251  virtual bool updateValue(ParamType const &iValue) = 0;
252 
256 
259  virtual tresult commit(ParamType const &iValue)
260  {
261  auto res = setValue(iValue);
262  if(res == kResultOk)
263  return commit();
264  else
265  return res;
266  };
267  };
268 
269 public:
281  virtual tresult accessValue(ValueAccessor const &iGetter) const = 0;
282 
292  virtual bool update(ParamType const &iValue) = 0;
293 
299  virtual tresult setValue(ParamType const &iValue) = 0;
300 
306  virtual std::unique_ptr<ITEditor> edit() = 0;
307 
311  virtual std::unique_ptr<ITEditor> edit(ParamType const &iValue)
312  {
313  auto editor = edit();
314  editor->setValue(iValue);
315  return editor;
316  }
317 };
318 
319 //------------------------------------------------------------------------
320 // DefaultEditorImpl
321 //------------------------------------------------------------------------
328 template<typename T>
329 class DefaultEditorImpl : public ITGUIParameter<T>::ITEditor
330 {
331 public:
332  // Constructor
333  explicit DefaultEditorImpl(ITGUIParameter<T> *iGUIParameter, T const &iDefaultValue) :
334  fGUIParameter{iGUIParameter},
335  fInitialValue{iDefaultValue}
336  {
337  }
338 
339  // Destructor
340  ~DefaultEditorImpl() override { rollback(); }
341 
342  // updateValue
343  bool updateValue(T const &iValue) override
344  {
345  if(fDoneEditing)
346  return false;
347  return fGUIParameter->update(iValue);
348  }
349 
350  // setValue
351  tresult setValue(T const &iValue) override
352  {
353  if(fDoneEditing)
354  return kResultFalse;
355  fGUIParameter->update(iValue);
356  return kResultOk;
357  }
358 
362 
363  // commit
364  tresult commit() override
365  {
366  if(fDoneEditing)
367  return kResultFalse;
368  fDoneEditing = true;
369  return kResultOk;
370  }
371 
372  // rollback
373  tresult rollback() override
374  {
375  auto res = setValue(fInitialValue);
376  fDoneEditing = true;
377  return res;
378  }
379 
380 private:
383  bool fDoneEditing{false};
384 };
385 
386 //------------------------------------------------------------------------
387 // IGUIParam
388 //------------------------------------------------------------------------
398 {
399 public:
401  IGUIParam(std::shared_ptr<IGUIParameter> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
402  fPtr{std::move(iPtr)}
403  {}
404 
407  inline bool exists() const { return (bool) fPtr; }
408 
411  inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
412 
415  inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
416 
419  inline std::string toUTF8String(int32 iPrecision) const { DCHECK_F(exists()); return fPtr->toUTF8String(iPrecision); }
420 
423  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
424 
427  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
428 
429 private:
430  std::shared_ptr<IGUIParameter> fPtr;
431 };
432 
433 
434 }
ITGUIParameter< T > * fGUIParameter
Definition: IGUIParameter.h:381
tresult commit() override
Commits all the changes applied to the parameter.
Definition: IGUIParameter.h:364
DefaultEditorImpl(ITGUIParameter< T > *iGUIParameter, T const &iDefaultValue)
Definition: IGUIParameter.h:333
IGUIParam(std::shared_ptr< IGUIParameter > iPtr=nullptr)
Constructor.
Definition: IGUIParameter.h:401
This is the base class of all GUI parameters.
Definition: IGUIParameter.h:52
tresult setValue(T const &iValue) override
Unconditionaly sets the value of the parameter to the value provided.
Definition: IGUIParameter.h:351
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:430
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:311
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:49
std::function< void(ParamValue const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:178
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:423
~DefaultEditorImpl() override
Definition: IGUIParameter.h:340
Definition: GUIState.h:36
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:343
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:427
ParamValue ParamType
The type of the param (alias)
Definition: IGUIParameter.h:171
tresult rollback() override
Rollback all the changes that were made to this parameter (since this editor was created).
Definition: IGUIParameter.h:373
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:419
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:259
Default implementation of the editor interface.
Definition: IGUIParameter.h:329
bool exists() const
Returns true if this wrapper actually refers to an actual parameter.
Definition: IGUIParameter.h:407
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:232
int32 getStepCount() const
When a parameter is a discrete parameter (which means its underlying backing type is an int32 with va...
Definition: IGUIParameter.h:415
ParamID getParamID() const
Each parameter has a unique ID returned by this method.
Definition: IGUIParameter.h:411
Wrapper instance returned by ParamAware::registerBaseParam() methods.
Definition: IGUIParameter.h:397
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
T fInitialValue
Definition: IGUIParameter.h:382