Jamba C++ API  4.5.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 #include "IDialogHandler.h"
29 
30 namespace pongasoft::VST {
31 
32 namespace Debug { class ParamDisplay; }
33 namespace GUI {
34 
35 using namespace Params;
36 
37 namespace Params {
38 class GUIParamCxMgr;
39 }
40 
42 {
43 public:
44  // Constructor
45  explicit GUIState(Parameters const &iPluginParameters);
46 
49  virtual tresult init(VstParametersSPtr iVstParameters,
50  IMessageProducer *iMessageProducer,
51  IDialogHandler *iDialogHandler);
52 
53  // getPluginParameters
54  Parameters const &getPluginParameters() const { return fPluginParameters; }
55 
59  template<typename T>
60  GUIJmbParam<T> add(JmbParam<T> iParamDef);
61 
65  inline bool existsVst(ParamID iParamID) const { return fVstParameters && fVstParameters->exists(iParamID); }
66 
70  inline bool existsJmb(ParamID iParamID) const { return fJmbParams.find(iParamID) != fJmbParams.cend()
71  ||
72  fPluginParameters.getJmbParamDef(iParamID) != nullptr; }
73 
78  std::shared_ptr<IGUIParameter> findParam(ParamID iParamID) const;
79 
83  std::shared_ptr<GUIRawVstParameter> getRawVstParameter(ParamID iParamID) const
84  {
85  if(existsVst(iParamID))
86  return std::make_shared<GUIRawVstParameter>(iParamID,
87  fVstParameters,
88  fPluginParameters.getRawVstParamDef(iParamID));
89  else
90  return nullptr;
91  }
92 
93  // getRawVstParamDef
94  std::shared_ptr<RawVstParamDef> getRawVstParamDef(ParamID iParamID) const
95  {
96  return fPluginParameters.getRawVstParamDef(iParamID);
97  }
98 
99  // getGUIVstParameter
100  template<typename T>
101  std::shared_ptr<GUIVstParameter<T>> getGUIVstParameter(ParamID iParamID) const;
102 
103  // getGUIVstParameter
104  template<typename T>
105  inline std::shared_ptr<GUIVstParameter<T>> getGUIVstParameter(VstParam<T> iParamDef) const {
106  return iParamDef ? getGUIVstParameter<T>(iParamDef->fParamID) : nullptr;
107  }
108 
115  std::unique_ptr<FObjectCx> connect(ParamID iParamID, Parameters::IChangeListener *iChangeListener) const
116  {
117  auto ptr = findParam(iParamID);
118  if(ptr)
119  return ptr->connect(iChangeListener);
120  else
121  return nullptr;
122  }
123 
130  std::unique_ptr<FObjectCx> connect(ParamID iParamID, Parameters::ChangeCallback iChangeCallback) const
131  {
132  auto ptr = findParam(iParamID);
133  if(ptr)
134  return ptr->connect(iChangeCallback);
135  else
136  return nullptr;
137  }
138 
156  template<typename TView>
157  inline ParamAwareView<TView> *makeParamAware(TView *iView) {
158  return fParamAwareViews.makeParamAware(iView, this);
159  }
160 
163  template<typename TView>
164  [[deprecated("Since 4.0.0 - Use makeParamAware instead")]]
166  return makeParamAware<TView>(iView, this);
167  }
168 
172  std::shared_ptr<IGUIJmbParameter> getJmbParameter(ParamID iParamID) const;
173 
178  virtual tresult readRTState(IBStreamer &iStreamer);
179 
184  virtual tresult readGUIState(IBStreamer &iStreamer);
185 
190  virtual tresult writeGUIState(IBStreamer &oStreamer) const;
191 
196  std::unique_ptr<GUIParamCxMgr> createParamCxMgr();
197 
201  tresult handleMessage(Message const &iMessage) { return fMessageHandler.handleMessage(iMessage); }
202 
206  template<typename T>
207  tresult broadcast(JmbParam<T> const &iParamDef, T const &iMessage);
208 
209  // getAllRegistrationOrder
210  std::vector<ParamID> const &getAllRegistrationOrder() const { return fAllRegistrationOrder; }
211 
223  virtual tresult handleGUIStateUpgrade(int16 iDeprecatedVersion, int16 iVersion) const { return kResultTrue; }
224 
227  bool showDialog(std::string iTemplateName) override;
228 
231  bool dismissDialog() override;
232 
233  // gives access for debug
234  friend class Debug::ParamDisplay;
235 
236 protected:
237  // the parameters
239 
240  // raw vst parameters
241  VstParametersSPtr fVstParameters{};
242 
243  // param aware views
244  ParamAwareViews fParamAwareViews{};
245 
246  // message producer (to send messages)
247  IMessageProducer *fMessageProducer{};
248 
249  // handles messages (receive messages)
250  MessageHandler fMessageHandler{};
251 
252  // handles show/dismiss dialog window
253  IDialogHandler *fDialogHandler{};
254 
255  // contains all the (serializable) registered parameters (unique ID, will be checked on add)
256  std::map<ParamID, std::shared_ptr<IGUIJmbParameter>> fJmbParams{};
257 
258  // order in which the parameters were registered
259  std::vector<ParamID> fAllRegistrationOrder{};
260 
261 protected:
262  // setParamNormalized
263  tresult setParamNormalized(NormalizedState const *iNormalizedState);
264 
265  // add serializable parameter to the structures
266  void addJmbParam(std::shared_ptr<IGUIJmbParameter> iParameter);
267 
268  // allocateMessage
269  IPtr<IMessage> allocateMessage() override;
270 
271  // sendMessage
272  tresult sendMessage(IPtr<IMessage> iMessage) override;
273 
274  virtual tresult readDeprecatedGUIState(uint16 iDeprecatedVersion,
275  IBStreamer &iStreamer,
276  NormalizedState::SaveOrder const &iLatestSaveOrder);
277 
279  virtual tresult readGUIState(NormalizedState::SaveOrder const &iSaveOrder, IBStreamer &iStreamer);
280 };
281 
287 template<typename TPluginParameters>
288 class GUIPluginState : public GUIState
289 {
290  // ensures that TPluginParameters is a subclass of Parameters
291  static_assert(std::is_convertible<TPluginParameters*, Parameters*>::value, "TPluginParameters must be a subclass of Parameters");
292 
293 public:
294  using PluginParameters = TPluginParameters;
295 
296 public:
297  explicit GUIPluginState(PluginParameters const &iPluginParameters) :
298  GUIState{iPluginParameters},
299  fParams{iPluginParameters}
300  { }
301 
302 public:
304 };
305 
306 //------------------------------------------------------------------------
307 // GUIState::add
308 //------------------------------------------------------------------------
309 template<typename T>
311 {
312  auto guiParam = iParamDef->newGUIParam();
313  addJmbParam(guiParam);
314  return GUIJmbParam<T>(std::dynamic_pointer_cast<GUIJmbParameter<T>>(guiParam));
315 }
316 
317 //------------------------------------------------------------------------
318 // GUIState::broadcast
319 //------------------------------------------------------------------------
320 template<typename T>
321 tresult GUIState::broadcast(JmbParam<T> const &iParamDef, const T &iMessage)
322 {
323  if(!iParamDef->fShared)
324  {
325  DLOG_F(WARNING, "broadcast ignored: parameter [%d] is not marked shared", iParamDef->fParamID);
326  return kResultFalse;
327  }
328 
329  tresult res = kResultOk;
330 
331  auto message = allocateMessage();
332 
333  if(message)
334  {
335  Message m{message.get()};
336 
337  // sets the message ID
338  m.setMessageID(iParamDef->fParamID);
339 
340  // serialize the content
341  if(iParamDef->writeToMessage(iMessage, m) == kResultOk)
342  res |= sendMessage(message);
343  else
344  res = kResultFalse;
345  }
346  else
347  res = kResultFalse;
348 
349  return res;
350 }
351 
352 //------------------------------------------------------------------------
353 // GUIState::getGUIVstParameter
354 //------------------------------------------------------------------------
355 template<typename T>
356 std::shared_ptr<GUIVstParameter<T>> GUIState::getGUIVstParameter(ParamID iParamID) const
357 {
358  std::shared_ptr<GUIRawVstParameter> param = getRawVstParameter(iParamID);
359 
360  if(!param)
361  {
362  DLOG_F(WARNING, "vst param [%d] not found", iParamID);
363  return nullptr;
364  }
365 
366  auto res = param->asVstParameter<T>();
367 
368  if(res)
369  return res;
370  else
371  {
372  DLOG_F(WARNING, "vst param [%d] is not of the requested type", iParamID);
373  return nullptr;
374  }
375 }
376 
377 
378 }
379 }
380 
381 //------------------------------------------------------------------------
382 // Implementation note: because of the circular dependency between
383 // JmbParamDef and IGUIJmbParameter, this templated method is defined
384 // here in GUIState.h since GUIState.cpp is the primary user of this
385 // method.
386 //------------------------------------------------------------------------
387 namespace pongasoft::VST {
388 //------------------------------------------------------------------------
389 // JmbParamDef<T>::newGUIParam
390 //------------------------------------------------------------------------
391 template<typename T>
392 std::shared_ptr<GUI::Params::IGUIJmbParameter> JmbParamDef<T>::newGUIParam()
393 {
394  auto ptr = std::dynamic_pointer_cast<JmbParamDef<T>>(IParamDef::shared_from_this());
395  return VstUtils::make_sfo<GUI::Params::GUIJmbParameter<T>>(ptr);
396 }
397 }
398 
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:223
bool existsJmb(ParamID iParamID) const
Definition: GUIState.h:70
This is the class which maintains all the registered parameters.
Definition: Parameters.h:37
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition: GUIState.h:210
std::shared_ptr< GUIVstParameter< T > > getGUIVstParameter(VstParam< T > iParamDef) const
Definition: GUIState.h:105
Definition: GUIState.h:41
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:44
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:65
std::shared_ptr< GUIVstParameter< T > > getGUIVstParameter(ParamID iParamID) const
Definition: GUIState.h:356
Parameters const & getPluginParameters() const
Definition: GUIState.h:54
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:157
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIJmbParameter.h:495
void setMessageID(MessageID messageID)
Definition: Messaging.h:54
std::shared_ptr< GUIRawVstParameter > getRawVstParameter(ParamID iParamID) const
Definition: GUIState.h:83
ParamAwareView< TView > * registerConnectionFor(TView *iView)
Definition: GUIState.h:165
tresult broadcast(JmbParam< T > const &iParamDef, T const &iMessage)
Broadcast a message without requiring the need to instantiate a GUIJmbParam.
Definition: GUIState.h:321
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:288
Parameters const & fPluginParameters
Definition: GUIState.h:238
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:294
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:201
std::unique_ptr< FObjectCx > connect(ParamID iParamID, Parameters::ChangeCallback iChangeCallback) const
Connects the paramID to the callback.
Definition: GUIState.h:130
Maintains the connections established between parameters and its listeners/callbacks.
Definition: GUIParamCxMgr.h:31
GUIPluginState(PluginParameters const &iPluginParameters)
Definition: GUIState.h:297
std::unique_ptr< FObjectCx > connect(ParamID iParamID, Parameters::IChangeListener *iChangeListener) const
Connects the paramID to the listener.
Definition: GUIState.h:115
PluginParameters const & fParams
Definition: GUIState.h:303
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
Defines the interface to show or dismiss a modal/dialog window which is a window that captures all ev...
Definition: IDialogHandler.h:45
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
std::shared_ptr< RawVstParamDef > getRawVstParamDef(ParamID iParamID) const
Definition: GUIState.h:94
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:310