Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
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 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
28#include "ParamAwareViews.h"
29#include "IDialogHandler.h"
30
31namespace pongasoft::VST {
32
33namespace Debug { class ParamDisplay; }
34namespace GUI {
35
36using namespace Params;
37
38namespace Params {
39class GUIParamCxMgr;
40}
41
43{
44public:
45 // Constructor
46 explicit GUIState(Parameters const &iPluginParameters);
47
50 virtual tresult init(VstParametersSPtr iVstParameters,
51 IMessageProducer *iMessageProducer,
52 IDialogHandler *iDialogHandler);
53
54 // getPluginParameters
56
60 template<typename T>
62
66 inline bool existsVst(ParamID iParamID) const { return fVstParameters && fVstParameters->exists(iParamID); }
67
71 inline bool existsJmb(ParamID iParamID) const { return fJmbParams.find(iParamID) != fJmbParams.cend()
72 ||
73 fPluginParameters.getJmbParamDef(iParamID) != nullptr; }
74
79 std::shared_ptr<IGUIParameter> findParam(ParamID iParamID) const;
80
84 std::shared_ptr<GUIRawVstParameter> getRawVstParameter(ParamID iParamID) const
85 {
86 if(existsVst(iParamID))
87 return std::make_shared<GUIRawVstParameter>(iParamID,
89 fPluginParameters.getRawVstParamDef(iParamID));
90 else
91 return nullptr;
92 }
93
94 // getRawVstParamDef
95 std::shared_ptr<RawVstParamDef> getRawVstParamDef(ParamID iParamID) const
96 {
97 return fPluginParameters.getRawVstParamDef(iParamID);
98 }
99
100 // getGUIVstParameter
101 template<typename T>
102 std::shared_ptr<GUIVstParameter<T>> getGUIVstParameter(ParamID iParamID) const;
103
104 // getGUIVstParameter
105 template<typename T>
106 inline std::shared_ptr<GUIVstParameter<T>> getGUIVstParameter(VstParam<T> iParamDef) const {
107 return iParamDef ? getGUIVstParameter<T>(iParamDef->fParamID) : nullptr;
108 }
109
116 std::unique_ptr<FObjectCx> connect(ParamID iParamID, Parameters::IChangeListener *iChangeListener) const
117 {
118 auto ptr = findParam(iParamID);
119 if(ptr)
120 return ptr->connect(iChangeListener);
121 else
122 return nullptr;
123 }
124
131 std::unique_ptr<FObjectCx> connect(ParamID iParamID, Parameters::ChangeCallback iChangeCallback) const
132 {
133 auto ptr = findParam(iParamID);
134 if(ptr)
135 return ptr->connect(iChangeCallback);
136 else
137 return nullptr;
138 }
139
157 template<typename TView>
158 inline ParamAwareView<TView> *makeParamAware(TView *iView) {
159 return fParamAwareViews.makeParamAware(iView, this);
160 }
161
164 template<typename TView>
165 [[deprecated("Since 4.0.0 - Use makeParamAware instead")]]
167 return makeParamAware<TView>(iView, this);
168 }
169
173 std::shared_ptr<IGUIJmbParameter> getJmbParameter(ParamID iParamID) const;
174
179 virtual tresult readRTState(IBStreamer &iStreamer);
180
185 virtual tresult readGUIState(IBStreamer &iStreamer);
186
191 virtual tresult writeGUIState(IBStreamer &oStreamer) const;
192
197 std::unique_ptr<GUIParamCxMgr> createParamCxMgr();
198
202 tresult handleMessage(Message const &iMessage) { return fMessageHandler.handleMessage(iMessage); }
203
207 template<typename T>
208 tresult broadcast(JmbParam<T> const &iParamDef, T const &iMessage);
209
210 // getAllRegistrationOrder
211 std::vector<ParamID> const &getAllRegistrationOrder() const { return fAllRegistrationOrder; }
212
224 virtual tresult handleGUIStateUpgrade(int16 iDeprecatedVersion, int16 iVersion) const { return kResultTrue; }
225
228 bool showDialog(std::string iTemplateName) override;
229
232 bool dismissDialog() override;
233
234 // gives access for debug
236
237protected:
238 // the parameters
240
241 // raw vst parameters
243
244 // param aware views
246
247 // message producer (to send messages)
249
250 // handles messages (receive messages)
252
253 // handles show/dismiss dialog window
255
256 // contains all the (serializable) registered parameters (unique ID, will be checked on add)
257 std::map<ParamID, std::shared_ptr<IGUIJmbParameter>> fJmbParams{};
258
259 // order in which the parameters were registered
260 std::vector<ParamID> fAllRegistrationOrder{};
261
262protected:
263 // setParamNormalized
264 tresult setParamNormalized(NormalizedState const *iNormalizedState);
265
266 // add serializable parameter to the structures
267 void addJmbParam(std::shared_ptr<IGUIJmbParameter> iParameter);
268
269 // allocateMessage
270 IPtr<IMessage> allocateMessage() override;
271
272 // sendMessage
273 tresult sendMessage(IPtr<IMessage> iMessage) override;
274
275 virtual tresult readDeprecatedGUIState(uint16 iDeprecatedVersion,
276 IBStreamer &iStreamer,
277 NormalizedState::SaveOrder const &iLatestSaveOrder);
278
280 virtual tresult readGUIState(NormalizedState::SaveOrder const &iSaveOrder, IBStreamer &iStreamer);
281};
282
288template<typename TPluginParameters>
290{
291 // ensures that TPluginParameters is a subclass of Parameters
292 static_assert(std::is_convertible<TPluginParameters*, Parameters*>::value, "TPluginParameters must be a subclass of Parameters");
293
294public:
295 using PluginParameters = TPluginParameters;
296
297public:
298 explicit GUIPluginState(PluginParameters const &iPluginParameters) :
299 GUIState{iPluginParameters},
300 fParams{iPluginParameters}
301 { }
302
303public:
305};
306
307//------------------------------------------------------------------------
308// GUIState::add
309//------------------------------------------------------------------------
310template<typename T>
312{
313 auto guiParam = iParamDef->newGUIParam();
314 addJmbParam(guiParam);
315 return GUIJmbParam<T>(std::dynamic_pointer_cast<GUIJmbParameter<T>>(guiParam));
316}
317
318//------------------------------------------------------------------------
319// GUIState::broadcast
320//------------------------------------------------------------------------
321template<typename T>
322tresult GUIState::broadcast(JmbParam<T> const &iParamDef, const T &iMessage)
323{
324 if(!iParamDef->fShared)
325 {
326 DLOG_F(WARNING, "broadcast ignored: parameter [%d] is not marked shared", iParamDef->fParamID);
327 return kResultFalse;
328 }
329
330 tresult res = kResultOk;
331
332 auto message = allocateMessage();
333
334 if(message)
335 {
336 Message m{message.get()};
337
338 // sets the message ID
339 m.setMessageID(iParamDef->fParamID);
340
341 // serialize the content
342 if(iParamDef->writeToMessage(iMessage, m) == kResultOk)
343 res |= sendMessage(message);
344 else
345 res = kResultFalse;
346 }
347 else
348 res = kResultFalse;
349
350 return res;
351}
352
353//------------------------------------------------------------------------
354// GUIState::getGUIVstParameter
355//------------------------------------------------------------------------
356template<typename T>
357std::shared_ptr<GUIVstParameter<T>> GUIState::getGUIVstParameter(ParamID iParamID) const
358{
359 std::shared_ptr<GUIRawVstParameter> param = getRawVstParameter(iParamID);
360
361 if(!param)
362 {
363 DLOG_F(WARNING, "vst param [%d] not found", iParamID);
364 return nullptr;
365 }
366
367 auto res = param->asVstParameter<T>();
368
369 if(res)
370 return res;
371 else
372 {
373 DLOG_F(WARNING, "vst param [%d] is not of the requested type", iParamID);
374 return nullptr;
375 }
376}
377
378
379}
380}
381
382//------------------------------------------------------------------------
383// Implementation note: because of the circular dependency between
384// JmbParamDef and IGUIJmbParameter, this templated method is defined
385// here in GUIState.h since GUIState.cpp is the primary user of this
386// method.
387//------------------------------------------------------------------------
388namespace pongasoft::VST {
389//------------------------------------------------------------------------
390// JmbParamDef<T>::newGUIParam
391//------------------------------------------------------------------------
392template<typename T>
393std::shared_ptr<GUI::Params::IGUIJmbParameter> JmbParamDef<T>::newGUIParam()
394{
395 auto ptr = std::dynamic_pointer_cast<JmbParamDef<T>>(IParamDef::shared_from_this());
397}
398}
399
This helper class is used to display the parameters (vst/jmb) WARNING: this class is allocating memor...
Definition ParamDisplay.h:35
PluginParameters const & fParams
Definition GUIState.h:304
TPluginParameters PluginParameters
Definition GUIState.h:295
GUIPluginState(PluginParameters const &iPluginParameters)
Definition GUIState.h:298
bool dismissDialog() override
Dismisses the currently shown dialog.
Definition GUIState.cpp:343
Parameters const & getPluginParameters() const
Definition GUIState.h:55
std::shared_ptr< IGUIParameter > findParam(ParamID iParamID) const
Generic call which returns a param with the given id or nullptr if there isn't one.
Definition GUIState.cpp:36
std::shared_ptr< RawVstParamDef > getRawVstParamDef(ParamID iParamID) const
Definition GUIState.h:95
bool showDialog(std::string iTemplateName) override
This method is called with the name of the template to use for the dialog.
Definition GUIState.cpp:332
bool existsVst(ParamID iParamID) const
Definition GUIState.h:66
ParamAwareView< TView > * registerConnectionFor(TView *iView)
Definition GUIState.h:166
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:158
virtual tresult readGUIState(IBStreamer &iStreamer)
This method is called from the GUI controller setState method and reads the state previously saved by...
Definition GUIState.cpp:119
virtual tresult readRTState(IBStreamer &iStreamer)
This method is called from the GUI controller setComponentState method and reads the state coming fro...
Definition GUIState.cpp:102
std::shared_ptr< GUIVstParameter< T > > getGUIVstParameter(VstParam< T > iParamDef) const
Definition GUIState.h:106
std::unique_ptr< FObjectCx > connect(ParamID iParamID, Parameters::IChangeListener *iChangeListener) const
Connects the paramID to the listener.
Definition GUIState.h:116
virtual tresult readDeprecatedGUIState(uint16 iDeprecatedVersion, IBStreamer &iStreamer, NormalizedState::SaveOrder const &iLatestSaveOrder)
Definition GUIState.cpp:143
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition GUIState.h:211
std::unique_ptr< GUIParamCxMgr > createParamCxMgr()
The CustomView class automatically calls this method to get a handle of a ParamCxMgr used to register...
Definition GUIState.cpp:277
virtual tresult init(VstParametersSPtr iVstParameters, IMessageProducer *iMessageProducer, IDialogHandler *iDialogHandler)
Called by the GUIController.
Definition GUIState.cpp:241
virtual tresult writeGUIState(IBStreamer &oStreamer) const
This method is called from the GUI controller getState method and writes the state specific to the GU...
Definition GUIState.cpp:205
std::shared_ptr< IGUIJmbParameter > getJmbParameter(ParamID iParamID) const
Definition GUIState.cpp:285
IMessageProducer * fMessageProducer
Definition GUIState.h:248
tresult setParamNormalized(NormalizedState const *iNormalizedState)
Definition GUIState.cpp:86
ParamAwareViews fParamAwareViews
Definition GUIState.h:245
std::unique_ptr< FObjectCx > connect(ParamID iParamID, Parameters::ChangeCallback iChangeCallback) const
Connects the paramID to the callback.
Definition GUIState.h:131
Parameters const & fPluginParameters
Definition GUIState.h:239
IDialogHandler * fDialogHandler
Definition GUIState.h:254
std::shared_ptr< GUIRawVstParameter > getRawVstParameter(ParamID iParamID) const
Definition GUIState.h:84
VstParametersSPtr fVstParameters
Definition GUIState.h:242
std::vector< ParamID > fAllRegistrationOrder
Definition GUIState.h:260
MessageHandler fMessageHandler
Definition GUIState.h:251
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:311
std::shared_ptr< GUIVstParameter< T > > getGUIVstParameter(ParamID iParamID) const
Definition GUIState.h:357
GUIState(Parameters const &iPluginParameters)
Definition GUIState.cpp:28
tresult broadcast(JmbParam< T > const &iParamDef, T const &iMessage)
Broadcast a message without requiring the need to instantiate a GUIJmbParam.
Definition GUIState.h:322
IPtr< IMessage > allocateMessage() override
Allocates a message instance.
Definition GUIState.cpp:310
std::map< ParamID, std::shared_ptr< IGUIJmbParameter > > fJmbParams
Definition GUIState.h:257
void addJmbParam(std::shared_ptr< IGUIJmbParameter > iParameter)
Definition GUIState.cpp:49
bool existsJmb(ParamID iParamID) const
Definition GUIState.h:71
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:224
tresult sendMessage(IPtr< IMessage > iMessage) override
Sends the given message to the peer.
Definition GUIState.cpp:321
tresult handleMessage(Message const &iMessage)
Handle an incoming message => will forward to JmbParam marked shared by rtOwner.
Definition GUIState.h:202
Defines the interface to show or dismiss a modal/dialog window which is a window that captures all ev...
Definition IDialogHandler.h:47
This class manages the views that have been made "param aware".
Definition ParamAwareViews.h:88
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition GUIJmbParameter.h:497
This is the templated version providing serializer methods, very similar to the GUIVstParameter conce...
Definition GUIJmbParameter.h:88
Maintains the connections established between parameters and its listeners/callbacks.
Definition GUIParamCxMgr.h:33
This subclass allows for registering callbacks to any kind of view without having to inherit from it.
Definition ParamAware.h:563
Abstraction for allocating and sending a message.
Definition MessageProducer.h:35
std::shared_ptr< GUI::Params::IGUIJmbParameter > newGUIParam() override
Create a new IGUIJmbParameter of the proper subtype.
Definition GUIState.h:393
Simple implementation of IMessageHandler which will delegate the message handling based on MessageID.
Definition MessageHandler.h:41
Simple wrapper class with better api.
Definition Messaging.h:46
void setMessageID(MessageID messageID)
Definition Messaging.h:55
Used to communicate the state between the UI and the RT and read/write to stream.
Definition NormalizedState.h:39
Interface to implement to receive parameter changes.
Definition Parameters.h:45
This is the class which maintains all the registered parameters.
Definition Parameters.h:39
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition Parameters.h:56
Definition ParamDisplay.cpp:29
Definition GUIState.h:38
std::shared_ptr< VstParameters > VstParametersSPtr
Definition VstParameters.h:96
Definition DrawContext.cpp:25
std::shared_ptr< T > make_sfo(Args &&...iArgs)
The VST SDK uses the concept of FObject (which are self contained reference counted objects) but requ...
Definition Utils.h:86
Definition Clock.h:24
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition ParamDef.h:522
std::shared_ptr< VstParamDef< T > > VstParam
Definition ParamDef.h:509
Maintains the order used to save/restore the RT and GUI state.
Definition NormalizedState.h:45