Jamba C++ API  4.3.0
GUIState.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 (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 #pragma once
19 
27 #include "ParamAwareViews.h"
28 
29 namespace pongasoft::VST {
30 
31 namespace Debug { class ParamDisplay; }
32 namespace GUI {
33 
34 using namespace Params;
35 
36 namespace Params {
37 class GUIParamCxMgr;
38 }
39 
40 class GUIState : public IMessageProducer
41 {
42 public:
43  // Constructor
44  explicit GUIState(Parameters const &iPluginParameters);
45 
48  virtual tresult init(VstParametersSPtr iVstParameters, IMessageProducer *iMessageProducer);
49 
50  // getPluginParameters
51  Parameters const &getPluginParameters() const { return fPluginParameters; }
52 
56  template<typename T>
57  GUIJmbParam<T> add(JmbParam<T> iParamDef);
58 
62  inline bool existsVst(ParamID iParamID) const { return fVstParameters && fVstParameters->exists(iParamID); }
63 
67  inline bool existsJmb(ParamID iParamID) const { return fJmbParams.find(iParamID) != fJmbParams.cend()
68  ||
69  fPluginParameters.getJmbParamDef(iParamID) != nullptr; }
70 
75  std::shared_ptr<IGUIParameter> findParam(ParamID iParamID) const;
76 
80  std::shared_ptr<GUIRawVstParameter> getRawVstParameter(ParamID iParamID) const
81  {
82  if(existsVst(iParamID))
83  return std::make_shared<GUIRawVstParameter>(iParamID,
84  fVstParameters,
85  fPluginParameters.getRawVstParamDef(iParamID));
86  else
87  return nullptr;
88  }
89 
90  // getRawVstParamDef
91  std::shared_ptr<RawVstParamDef> getRawVstParamDef(ParamID iParamID) const
92  {
93  return fPluginParameters.getRawVstParamDef(iParamID);
94  }
95 
96  // getGUIVstParameter
97  template<typename T>
98  std::shared_ptr<GUIVstParameter<T>> getGUIVstParameter(ParamID iParamID) const;
99 
100  // getGUIVstParameter
101  template<typename T>
102  inline std::shared_ptr<GUIVstParameter<T>> getGUIVstParameter(VstParam<T> iParamDef) const {
103  return iParamDef ? getGUIVstParameter<T>(iParamDef->fParamID) : nullptr;
104  }
105 
112  std::unique_ptr<FObjectCx> connect(ParamID iParamID, Parameters::IChangeListener *iChangeListener) const
113  {
114  auto ptr = findParam(iParamID);
115  if(ptr)
116  return ptr->connect(iChangeListener);
117  else
118  return nullptr;
119  }
120 
127  std::unique_ptr<FObjectCx> connect(ParamID iParamID, Parameters::ChangeCallback iChangeCallback) const
128  {
129  auto ptr = findParam(iParamID);
130  if(ptr)
131  return ptr->connect(iChangeCallback);
132  else
133  return nullptr;
134  }
135 
153  template<typename TView>
154  inline ParamAwareView<TView> *makeParamAware(TView *iView) {
155  return fParamAwareViews.makeParamAware(iView, this);
156  }
157 
160  template<typename TView>
161  [[deprecated("Since 4.0.0 - Use makeParamAware instead")]]
163  return makeParamAware<TView>(iView, this);
164  }
165 
169  std::shared_ptr<IGUIJmbParameter> getJmbParameter(ParamID iParamID) const;
170 
175  virtual tresult readRTState(IBStreamer &iStreamer);
176 
181  virtual tresult readGUIState(IBStreamer &iStreamer);
182 
187  virtual tresult writeGUIState(IBStreamer &oStreamer) const;
188 
193  std::unique_ptr<GUIParamCxMgr> createParamCxMgr();
194 
198  tresult handleMessage(Message const &iMessage) { return fMessageHandler.handleMessage(iMessage); }
199 
203  template<typename T>
204  tresult broadcast(JmbParam<T> const &iParamDef, T const &iMessage);
205 
206  // getAllRegistrationOrder
207  std::vector<ParamID> const &getAllRegistrationOrder() const { return fAllRegistrationOrder; }
208 
220  virtual tresult handleGUIStateUpgrade(int16 iDeprecatedVersion, int16 iVersion) const { return kResultTrue; }
221 
222  // gives access for debug
223  friend class Debug::ParamDisplay;
224 
225 protected:
226  // the parameters
228 
229  // raw vst parameters
230  VstParametersSPtr fVstParameters{};
231 
232  // param aware views
233  ParamAwareViews fParamAwareViews{};
234 
235  // message producer (to send messages)
236  IMessageProducer *fMessageProducer{};
237 
238  // handles messages (receive messages)
239  MessageHandler fMessageHandler{};
240 
241  // contains all the (serializable) registered parameters (unique ID, will be checked on add)
242  std::map<ParamID, std::shared_ptr<IGUIJmbParameter>> fJmbParams{};
243 
244  // order in which the parameters were registered
245  std::vector<ParamID> fAllRegistrationOrder{};
246 
247 protected:
248  // setParamNormalized
249  tresult setParamNormalized(NormalizedState const *iNormalizedState);
250 
251  // add serializable parameter to the structures
252  void addJmbParam(std::shared_ptr<IGUIJmbParameter> iParameter);
253 
254  // allocateMessage
255  IPtr<IMessage> allocateMessage() override;
256 
257  // sendMessage
258  tresult sendMessage(IPtr<IMessage> iMessage) override;
259 
260  virtual tresult readDeprecatedGUIState(uint16 iDeprecatedVersion,
261  IBStreamer &iStreamer,
262  NormalizedState::SaveOrder const &iLatestSaveOrder);
263 
265  virtual tresult readGUIState(NormalizedState::SaveOrder const &iSaveOrder, IBStreamer &iStreamer);
266 };
267 
273 template<typename TPluginParameters>
274 class GUIPluginState : public GUIState
275 {
276  // ensures that TPluginParameters is a subclass of Parameters
277  static_assert(std::is_convertible<TPluginParameters*, Parameters*>::value, "TPluginParameters must be a subclass of Parameters");
278 
279 public:
280  using PluginParameters = TPluginParameters;
281 
282 public:
283  explicit GUIPluginState(PluginParameters const &iPluginParameters) :
284  GUIState{iPluginParameters},
285  fParams{iPluginParameters}
286  { }
287 
288 public:
290 };
291 
292 //------------------------------------------------------------------------
293 // GUIState::add
294 //------------------------------------------------------------------------
295 template<typename T>
297 {
298  auto guiParam = iParamDef->newGUIParam();
299  addJmbParam(guiParam);
300  return GUIJmbParam<T>(std::dynamic_pointer_cast<GUIJmbParameter<T>>(guiParam));
301 }
302 
303 //------------------------------------------------------------------------
304 // GUIState::broadcast
305 //------------------------------------------------------------------------
306 template<typename T>
307 tresult GUIState::broadcast(JmbParam<T> const &iParamDef, const T &iMessage)
308 {
309  if(!iParamDef->fShared)
310  {
311  DLOG_F(WARNING, "broadcast ignored: parameter [%d] is not marked shared", iParamDef->fParamID);
312  return kResultFalse;
313  }
314 
315  tresult res = kResultOk;
316 
317  auto message = allocateMessage();
318 
319  if(message)
320  {
321  Message m{message.get()};
322 
323  // sets the message ID
324  m.setMessageID(iParamDef->fParamID);
325 
326  // serialize the content
327  if(iParamDef->writeToMessage(iMessage, m) == kResultOk)
328  res |= sendMessage(message);
329  else
330  res = kResultFalse;
331  }
332  else
333  res = kResultFalse;
334 
335  return res;
336 }
337 
338 //------------------------------------------------------------------------
339 // GUIState::getGUIVstParameter
340 //------------------------------------------------------------------------
341 template<typename T>
342 std::shared_ptr<GUIVstParameter<T>> GUIState::getGUIVstParameter(ParamID iParamID) const
343 {
344  std::shared_ptr<GUIRawVstParameter> param = getRawVstParameter(iParamID);
345 
346  if(!param)
347  {
348  DLOG_F(WARNING, "vst param [%d] not found", iParamID);
349  return nullptr;
350  }
351 
352  auto res = param->asVstParameter<T>();
353 
354  if(res)
355  return res;
356  else
357  {
358  DLOG_F(WARNING, "vst param [%d] is not of the requested type", iParamID);
359  return nullptr;
360  }
361 }
362 
363 
364 }
365 }
366 
367 //------------------------------------------------------------------------
368 // Implementation note: because of the circular dependency between
369 // JmbParamDef and IGUIJmbParameter, this templated method is defined
370 // here in GUIState.h since GUIState.cpp is the primary user of this
371 // method.
372 //------------------------------------------------------------------------
373 namespace pongasoft::VST {
374 //------------------------------------------------------------------------
375 // JmbParamDef<T>::newGUIParam
376 //------------------------------------------------------------------------
377 template<typename T>
378 std::shared_ptr<GUI::Params::IGUIJmbParameter> JmbParamDef<T>::newGUIParam()
379 {
380  auto ptr = std::dynamic_pointer_cast<JmbParamDef<T>>(IParamDef::shared_from_this());
381  return VstUtils::make_sfo<GUI::Params::GUIJmbParameter<T>>(ptr);
382 }
383 }
384 
virtual tresult handleGUIStateUpgrade(int16 iDeprecatedVersion, int16 iVersion) const
When Jamba detects that a previously saved GUI state matches a deprecated version (as registered with...
Definition: GUIState.h:220
bool existsJmb(ParamID iParamID) const
Definition: GUIState.h:67
This is the class which maintains all the registered parameters.
Definition: Parameters.h:37
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition: GUIState.h:207
std::shared_ptr< GUIVstParameter< T > > getGUIVstParameter(VstParam< T > iParamDef) const
Definition: GUIState.h:102
Definition: GUIState.h:40
Abstraction for allocating and sending a message.
Definition: MessageProducer.h:33
This helper class is used to display the parameters (vst/jmb) WARNING: this class is allocating memor...
Definition: ParamDisplay.h:33
Simple wrapper class with better api.
Definition: Messaging.h:42
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition: Parameters.h:55
Maintains the order used to save/restore the RT and GUI state.
Definition: NormalizedState.h:43
bool existsVst(ParamID iParamID) const
Definition: GUIState.h:62
std::shared_ptr< GUIVstParameter< T > > getGUIVstParameter(ParamID iParamID) const
Definition: GUIState.h:342
Parameters const & getPluginParameters() const
Definition: GUIState.h:51
Used to communicate the state between the UI and the RT and read/write to stream.
Definition: NormalizedState.h:37
ParamAwareView< TView > * makeParamAware(TView *iView)
Allow for registering an arbitrary callback on an arbitrary view without having to inherit from the v...
Definition: GUIState.h:154
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIJmbParameter.h:489
void setMessageID(MessageID messageID)
Definition: Messaging.h:52
std::shared_ptr< GUIRawVstParameter > getRawVstParameter(ParamID iParamID) const
Definition: GUIState.h:80
ParamAwareView< TView > * registerConnectionFor(TView *iView)
Definition: GUIState.h:162
tresult broadcast(JmbParam< T > const &iParamDef, T const &iMessage)
Broadcast a message without requiring the need to instantiate a GUIJmbParam.
Definition: GUIState.h:307
This class manages the views that have been made "param aware".
Definition: ParamAwareViews.h:86
This is the templated version providing serializer methods, very similar to the GUIVstParameter conce...
Definition: GUIJmbParameter.h:86
Simple implementation of IMessageHandler which will delegate the message handling based on MessageID.
Definition: MessageHandler.h:39
Simple templated extension to expose the plugin parameters as its real type.
Definition: GUIState.h:274
Parameters const & fPluginParameters
Definition: GUIState.h:227
This subclass allows for registering callbacks to any kind of view without having to inherit from it.
Definition: ParamAware.h:561
TPluginParameters PluginParameters
Definition: GUIState.h:280
std::shared_ptr< VstParamDef< T > > VstParam
Definition: ParamDef.h:507
tresult handleMessage(Message const &iMessage)
Handle an incoming message => will forward to JmbParam marked shared by rtOwner.
Definition: GUIState.h:198
std::unique_ptr< FObjectCx > connect(ParamID iParamID, Parameters::ChangeCallback iChangeCallback) const
Connects the paramID to the callback.
Definition: GUIState.h:127
Maintains the connections established between parameters and its listeners/callbacks.
Definition: GUIParamCxMgr.h:31
GUIPluginState(PluginParameters const &iPluginParameters)
Definition: GUIState.h:283
std::unique_ptr< FObjectCx > connect(ParamID iParamID, Parameters::IChangeListener *iChangeListener) const
Connects the paramID to the listener.
Definition: GUIState.h:112
PluginParameters const & fParams
Definition: GUIState.h:289
std::shared_ptr< VstParameters > VstParametersSPtr
Definition: VstParameters.h:95
Definition: Clock.h:23
Base class for all non vst parameters (need to provide serialization/deserialization)
Definition: ParamDef.h:316
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition: ParamDef.h:514
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
std::shared_ptr< RawVstParamDef > getRawVstParamDef(ParamID iParamID) const
Definition: GUIState.h:91
GUIJmbParam< T > add(JmbParam< T > iParamDef)
This method is called for each parameter managed by the GUIState that is not a regular VST parameter.
Definition: GUIState.h:296