Jamba  3.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 
26 #include "ViewCxMgr.h"
27 
28 namespace pongasoft {
29 namespace VST {
30 namespace Debug { class ParamDisplay; }
31 namespace GUI {
32 
33 using namespace Params;
34 
35 namespace Params {
36 class GUIParamCxMgr;
37 }
38 
39 class GUIState : public IMessageProducer
40 {
41 public:
42  // Constructor
43  explicit GUIState(Parameters const &iPluginParameters);
44 
47  virtual tresult init(VstParametersSPtr iVstParameters, IMessageProducer *iMessageProducer);
48 
49  // getPluginParameters
50  Parameters const &getPluginParameters() const { return fPluginParameters; }
51 
55  template<typename T>
56  GUIJmbParam<T> add(JmbParam<T> iParamDef);
57 
61  inline bool existsVst(ParamID iParamID) const { return fVstParameters && fVstParameters->exists(iParamID); }
62 
66  inline bool existsJmb(ParamID iParamID) const { return fJmbParams.find(iParamID) != fJmbParams.cend(); }
67 
71  std::unique_ptr<GUIRawVstParameter> getRawVstParameter(ParamID iParamID) const
72  {
73  if(existsVst(iParamID))
74  return std::make_unique<GUIRawVstParameter>(iParamID, fVstParameters);
75  else
76  return nullptr;
77  }
78 
79  // getRawVstParamDef
80  std::shared_ptr<RawVstParamDef> getRawVstParamDef(ParamID iParamID) const
81  {
82  return fPluginParameters.getRawVstParamDef(iParamID);
83  }
84 
85  // getGUIVstParameter
86  template<typename T>
87  std::unique_ptr<GUIVstParameter<T>> getGUIVstParameter(ParamID iParamID) const;
88 
89  // getGUIVstParameter
90  template<typename T>
91  inline std::unique_ptr<GUIVstParameter<T>> getGUIVstParameter(VstParam<T> iParamDef) const {
92  return iParamDef ? getGUIVstParameter<T>(iParamDef->fParamID) : nullptr;
93  }
94 
101  std::unique_ptr<FObjectCx> connect(ParamID iParamID, Parameters::IChangeListener *iChangeListener) const
102  {
103  return fVstParameters ? fVstParameters->connect(iParamID, iChangeListener) : nullptr;
104  }
105 
112  std::unique_ptr<FObjectCx> connect(ParamID iParamID, Parameters::ChangeCallback iChangeCallback) const
113  {
114  return fVstParameters ? fVstParameters->connect(iParamID, std::move(iChangeCallback)) : nullptr;
115  }
116 
134  template<typename TView>
136  return fViewCxMgr.registerConnectionFor(iView, this);
137  }
138 
142  IGUIJmbParameter *getJmbParameter(ParamID iParamID) const;
143 
148  virtual tresult readRTState(IBStreamer &iStreamer);
149 
154  virtual tresult readGUIState(IBStreamer &iStreamer);
155 
160  virtual tresult writeGUIState(IBStreamer &oStreamer) const;
161 
166  std::unique_ptr<GUIParamCxMgr> createParamCxMgr();
167 
171  tresult handleMessage(Message const &iMessage) { return fMessageHandler.handleMessage(iMessage); }
172 
176  template<typename T>
177  tresult broadcast(JmbParam<T> const &iParamDef, T const &iMessage);
178 
179  // getAllRegistrationOrder
180  std::vector<ParamID> const &getAllRegistrationOrder() const { return fAllRegistrationOrder; }
181 
182  // gives access for debug
183  friend class Debug::ParamDisplay;
184 
185 protected:
186  // the parameters
188 
189  // raw vst parameters
190  VstParametersSPtr fVstParameters{};
191 
192  // view connection mgr
193  ViewCxMgr fViewCxMgr{};
194 
195  // message producer (to send messages)
196  IMessageProducer *fMessageProducer{};
197 
198  // handles messages (receive messages)
199  MessageHandler fMessageHandler{};
200 
201  // contains all the (serializable) registered parameters (unique ID, will be checked on add)
202  std::map<ParamID, std::unique_ptr<IGUIJmbParameter>> fJmbParams{};
203 
204  // order in which the parameters were registered
205  std::vector<ParamID> fAllRegistrationOrder{};
206 
207 protected:
208  // setParamNormalized
209  tresult setParamNormalized(NormalizedState const *iNormalizedState);
210 
211  // add serializable parameter to the structures
212  void addJmbParam(std::unique_ptr<IGUIJmbParameter> iParameter);
213 
214  // allocateMessage
215  IPtr<IMessage> allocateMessage() override;
216 
217  // sendMessage
218  tresult sendMessage(IPtr<IMessage> iMessage) override;
219 };
220 
226 template<typename TPluginParameters>
227 class GUIPluginState : public GUIState
228 {
229 public:
230  using PluginParameters = TPluginParameters;
231 
232 public:
233  explicit GUIPluginState(PluginParameters const &iPluginParameters) :
234  GUIState{iPluginParameters},
235  fParams{iPluginParameters}
236  { }
237 
238 public:
240 };
241 
242 //------------------------------------------------------------------------
243 // GUIState::add
244 //------------------------------------------------------------------------
245 template<typename T>
247 {
248  auto rawPtr = new GUIJmbParameter<T>(iParamDef);
249  std::unique_ptr<IGUIJmbParameter> guiParam{rawPtr};
250  addJmbParam(std::move(guiParam));
251  if(iParamDef->fShared && iParamDef->fSerializer)
252  {
253  switch(iParamDef->fOwner)
254  {
256  fMessageHandler.registerHandler(iParamDef->fParamID, rawPtr);
257  break;
258 
260  rawPtr->setMessageProducer(this);
261  break;
262 
263  default:
264  // not reached
265  DLOG_F(ERROR, "not reached");
266  break;
267  }
268  }
269  return rawPtr;
270 }
271 
272 //------------------------------------------------------------------------
273 // GUIState::broadcast
274 //------------------------------------------------------------------------
275 template<typename T>
276 tresult GUIState::broadcast(JmbParam<T> const &iParamDef, const T &iMessage)
277 {
278  if(!iParamDef->fShared)
279  {
280  DLOG_F(WARNING, "broadcast ignored: parameter [%d] is not marked shared", iParamDef->fParamID);
281  return kResultFalse;
282  }
283 
284  tresult res = kResultOk;
285 
286  auto message = allocateMessage();
287 
288  if(message)
289  {
290  Message m{message.get()};
291 
292  // sets the message ID
293  m.setMessageID(iParamDef->fParamID);
294 
295  // serialize the content
296  if(iParamDef->writeToMessage(iMessage, m) == kResultOk)
297  res |= sendMessage(message);
298  else
299  res = kResultFalse;
300  }
301  else
302  res = kResultFalse;
303 
304  return res;
305 }
306 
307 //------------------------------------------------------------------------
308 // GUIState::getGUIVstParameter
309 //------------------------------------------------------------------------
310 template<typename T>
311 std::unique_ptr<GUIVstParameter<T>> GUIState::getGUIVstParameter(ParamID iParamID) const
312 {
313  auto param = getRawVstParameter(iParamID);
314 
315  if(!param)
316  {
317  DLOG_F(WARNING, "vst param [%d] not found", iParamID);
318  return nullptr;
319  }
320 
321  auto rawParamDef = getRawVstParamDef(iParamID);
322 
323  auto paramDef = std::dynamic_pointer_cast<VstParamDef<T>>(rawParamDef);
324 
325  if(paramDef)
326  {
327  return std::make_unique<GUIVstParameter<T>>(std::move(param), paramDef);
328  }
329  else
330  {
331  DLOG_F(WARNING, "vst param [%d] is not of the requested type", iParamID);
332  return nullptr;
333  }
334 }
335 
336 
337 }
338 }
339 }
340 
Definition: Parameters.h:39
Parameters const & getPluginParameters() const
Definition: GUIState.h:50
std::shared_ptr< VstParamDef< T > > VstParam
Definition: ParamDef.h:358
Definition: GUIState.h:39
Definition: GUIJmbParameter.h:40
Definition: MessageProducer.h:33
Definition: ParamDisplay.h:33
Definition: Messaging.h:43
tresult handleMessage(Message const &iMessage)
Definition: GUIState.h:171
Definition: Clock.h:22
void setMessageID(MessageID messageID)
Definition: Messaging.h:53
std::unique_ptr< FObjectCx > connect(ParamID iParamID, Parameters::IChangeListener *iChangeListener) const
Definition: GUIState.h:101
bool existsJmb(ParamID iParamID) const
Definition: GUIState.h:66
GUIJmbParam< T > add(JmbParam< T > iParamDef)
Definition: GUIState.h:246
std::unique_ptr< GUIRawVstParameter > getRawVstParameter(ParamID iParamID) const
Definition: GUIState.h:71
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition: ParamDef.h:365
std::shared_ptr< RawVstParamDef > getRawVstParamDef(ParamID iParamID) const
Definition: GUIState.h:80
Definition: NormalizedState.h:38
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition: GUIState.h:180
Definition: GUIParamCxAware.h:289
std::unique_ptr< GUIVstParameter< T > > getGUIVstParameter(ParamID iParamID) const
Definition: GUIState.h:311
TPluginParameters PluginParameters
Definition: GUIState.h:230
Definition: GUIJmbParameter.h:237
ViewGUIParamCxAware< TView > * registerConnectionFor(TView *iView)
Definition: GUIState.h:135
Definition: GUIJmbParameter.h:86
std::function< void()> ChangeCallback
Definition: Parameters.h:57
bool existsVst(ParamID iParamID) const
Definition: GUIState.h:61
Definition: MessageHandler.h:39
Definition: GUIState.h:227
tresult broadcast(JmbParam< T > const &iParamDef, T const &iMessage)
Definition: GUIState.h:276
Parameters const & fPluginParameters
Definition: GUIState.h:187
Definition: GUIParamCxMgr.h:29
std::unique_ptr< GUIVstParameter< T > > getGUIVstParameter(VstParam< T > iParamDef) const
Definition: GUIState.h:91
Definition: ViewCxMgr.h:88
Definition: ParamDef.h:129
std::shared_ptr< VstParameters > VstParametersSPtr
Definition: VstParameters.h:95
std::unique_ptr< FObjectCx > connect(ParamID iParamID, Parameters::ChangeCallback iChangeCallback) const
Definition: GUIState.h:112
GUIPluginState(PluginParameters const &iPluginParameters)
Definition: GUIState.h:233
PluginParameters const & fParams
Definition: GUIState.h:239