Jamba C++ API  4.1.0
GUIState.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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 #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 
209  // gives access for debug
210  friend class Debug::ParamDisplay;
211 
212 protected:
213  // the parameters
215 
216  // raw vst parameters
217  VstParametersSPtr fVstParameters{};
218 
219  // param aware views
220  ParamAwareViews fParamAwareViews{};
221 
222  // message producer (to send messages)
223  IMessageProducer *fMessageProducer{};
224 
225  // handles messages (receive messages)
226  MessageHandler fMessageHandler{};
227 
228  // contains all the (serializable) registered parameters (unique ID, will be checked on add)
229  std::map<ParamID, std::shared_ptr<IGUIJmbParameter>> fJmbParams{};
230 
231  // order in which the parameters were registered
232  std::vector<ParamID> fAllRegistrationOrder{};
233 
234 protected:
235  // setParamNormalized
236  tresult setParamNormalized(NormalizedState const *iNormalizedState);
237 
238  // add serializable parameter to the structures
239  void addJmbParam(std::shared_ptr<IGUIJmbParameter> iParameter);
240 
241  // allocateMessage
242  IPtr<IMessage> allocateMessage() override;
243 
244  // sendMessage
245  tresult sendMessage(IPtr<IMessage> iMessage) override;
246 };
247 
253 template<typename TPluginParameters>
254 class GUIPluginState : public GUIState
255 {
256 public:
257  using PluginParameters = TPluginParameters;
258 
259 public:
260  explicit GUIPluginState(PluginParameters const &iPluginParameters) :
261  GUIState{iPluginParameters},
262  fParams{iPluginParameters}
263  { }
264 
265 public:
267 };
268 
269 //------------------------------------------------------------------------
270 // GUIState::add
271 //------------------------------------------------------------------------
272 template<typename T>
274 {
275  auto guiParam = iParamDef->newGUIParam();
276  addJmbParam(guiParam);
277  return GUIJmbParam<T>(std::dynamic_pointer_cast<GUIJmbParameter<T>>(guiParam));
278 }
279 
280 //------------------------------------------------------------------------
281 // GUIState::broadcast
282 //------------------------------------------------------------------------
283 template<typename T>
284 tresult GUIState::broadcast(JmbParam<T> const &iParamDef, const T &iMessage)
285 {
286  if(!iParamDef->fShared)
287  {
288  DLOG_F(WARNING, "broadcast ignored: parameter [%d] is not marked shared", iParamDef->fParamID);
289  return kResultFalse;
290  }
291 
292  tresult res = kResultOk;
293 
294  auto message = allocateMessage();
295 
296  if(message)
297  {
298  Message m{message.get()};
299 
300  // sets the message ID
301  m.setMessageID(iParamDef->fParamID);
302 
303  // serialize the content
304  if(iParamDef->writeToMessage(iMessage, m) == kResultOk)
305  res |= sendMessage(message);
306  else
307  res = kResultFalse;
308  }
309  else
310  res = kResultFalse;
311 
312  return res;
313 }
314 
315 //------------------------------------------------------------------------
316 // GUIState::getGUIVstParameter
317 //------------------------------------------------------------------------
318 template<typename T>
319 std::shared_ptr<GUIVstParameter<T>> GUIState::getGUIVstParameter(ParamID iParamID) const
320 {
321  std::shared_ptr<GUIRawVstParameter> param = getRawVstParameter(iParamID);
322 
323  if(!param)
324  {
325  DLOG_F(WARNING, "vst param [%d] not found", iParamID);
326  return nullptr;
327  }
328 
329  auto res = param->asVstParameter<T>();
330 
331  if(res)
332  return res;
333  else
334  {
335  DLOG_F(WARNING, "vst param [%d] is not of the requested type", iParamID);
336  return nullptr;
337  }
338 }
339 
340 
341 }
342 }
343 
344 //------------------------------------------------------------------------
345 // Implementation note: because of the circular dependency between
346 // JmbParamDef and IGUIJmbParameter, this templated method is defined
347 // here in GUIState.h since GUIState.cpp is the primary user of this
348 // method.
349 //------------------------------------------------------------------------
350 namespace pongasoft::VST {
351 //------------------------------------------------------------------------
352 // JmbParamDef<T>::newGUIParam
353 //------------------------------------------------------------------------
354 template<typename T>
355 std::shared_ptr<GUI::Params::IGUIJmbParameter> JmbParamDef<T>::newGUIParam()
356 {
357  auto ptr = std::dynamic_pointer_cast<JmbParamDef<T>>(IParamDef::shared_from_this());
358  return VstUtils::make_sfo<GUI::Params::GUIJmbParameter<T>>(ptr);
359 }
360 }
361 
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
bool existsVst(ParamID iParamID) const
Definition: GUIState.h:62
std::shared_ptr< GUIVstParameter< T > > getGUIVstParameter(ParamID iParamID) const
Definition: GUIState.h:319
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:38
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:284
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:254
Parameters const & fPluginParameters
Definition: GUIState.h:214
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:257
std::shared_ptr< VstParamDef< T > > VstParam
Definition: ParamDef.h:456
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:260
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:266
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:273
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition: ParamDef.h:463
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:273