Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
CustomView.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 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
21#include <vstgui4/vstgui/lib/cview.h>
22#include <map>
26#include "CustomViewCreator.h"
27#include "StateAware.h"
28#include "CustomViewLifecycle.h"
29
31
32using namespace VSTGUI;
33using namespace Params;
34
61class CustomView : public CView, public ParamAware, public ICustomViewLifecycle
62{
63public:
64 // Constructor
65 explicit CustomView(const CRect &iSize);
66
67 // Deleting for now... not sure there is an actual use for it
68 CustomView(const CustomView &c) = delete;
69
74 CColor const &getBackColor() const { return fBackColor; }
75
78 void setBackColor(CColor const &iColor);
79
82 void setCustomViewTag (TagID iTag) { fTag = iTag; }
83
108 TagID getCustomViewTag () const { return fTag; }
109
112 void setEditorMode(bool iEditorMode);
113
142 bool getEditorMode() const;
143
144#if EDITOR_MODE
147 virtual void onEditorModeChanged() {}
148#endif
149
154 void draw(CDrawContext *iContext) override;
155
159 virtual void drawBackColor(CDrawContext *iContext);
160
164 void drawStyleChanged();
165
170 void onParameterChange(ParamID iParamID) override;
171
178 inline void markDirty() { setDirty(true); }
179
187 void afterApplyAttributes() override
188 {
191 markDirty();
192 }
193
194public:
195 CLASS_METHODS_NOCOPY(CustomView, CControl)
196
197protected:
198 using CView::sizeToFit; // fixes overload hiding warning
199
203 void sizeToFit(CCoord iWidth, CCoord iHeight)
204 {
205 CRect vs(getViewSize());
206 vs.setWidth(iWidth);
207 vs.setHeight(iHeight);
208 setViewSize(vs, true);
209 setMouseableArea(vs);
210 }
211
215 bool sizeToFit(BitmapPtr iBitmap, int iFrameCount = 1)
216 {
217 if(iBitmap)
218 {
219 sizeToFit(iBitmap->getWidth(), iBitmap->getHeight() / iFrameCount);
220 return true;
221 }
222 return false;
223 }
224
225protected:
226 ParamID fTag;
227#if EDITOR_MODE
228 bool fEditorMode{false};
229#endif
231
232public:
239 class Creator : public TCustomViewCreator<CustomView>
240 {
241 public:
242 explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
243 TCustomViewCreator<CustomView>(iViewName, iDisplayName)
244 {
246#if EDITOR_MODE
248#endif
250 }
251 };
252};
253
284template<typename TView, typename TGUIState>
285class StateAwareView : public TView, public StateAware<TGUIState>
286{
287 // ensures that TView is a subclass of CView
288 static_assert(std::is_convertible<TView *, CView*>::value, "TView must be a subclass of CView");
289
290 // ensures that TGUIState is a subclass of GUIState
291 static_assert(std::is_convertible<TGUIState *, GUIState*>::value, "TGUIState must be a subclass of GUIState");
292
293public:
294 // Constructor
295 template<typename... Args>
296 explicit StateAwareView(const CRect &iSize, Args&& ...args) : TView(iSize, std::forward<Args>(args)...) {}
297
298protected:
302 void initState(GUIState *iGUIState) override
303 {
304 TView::initState(iGUIState);
306 }
307};
308
313template<typename TGUIState>
315
335template<typename TView>
336class CustomViewAdapter : public TView, public ParamAware, public ICustomViewLifecycle
337{
338 // ensures that TView is a subclass of CView
339 static_assert(std::is_convertible<TView *, CView*>::value, "TView must be a subclass of CView");
340
341public:
342 // Constructor
343 template<typename... Args>
344 explicit CustomViewAdapter(const CRect &iSize, Args&& ...args) : TView(iSize, std::forward<Args>(args)...), fTag{UNDEFINED_PARAM_ID} {}
345
347 inline void markDirty() { TView::setDirty(true); }
348
350 void setCustomViewTag (TagID iTag) { fTag = iTag; }
351
353 TagID getCustomViewTag () const { return fTag; }
354
356 void setEditorMode(bool iEditorMode)
357 {
358#if EDITOR_MODE
359 if(fEditorMode != iEditorMode)
360 {
361 fEditorMode = iEditorMode;
362 onEditorModeChanged();
363 }
364#endif
365
366 // when not in editor mode, this does nothing...
367 }
368
370 bool getEditorMode() const
371 {
372#if EDITOR_MODE
373 return fEditorMode;
374#else
375 return false;
376#endif
377 }
378
379#if EDITOR_MODE
381 virtual void onEditorModeChanged() {}
382#endif
383
385 void onParameterChange(ParamID iParamID) override { markDirty(); };
386
388 void afterApplyAttributes() override
389 {
392 markDirty();
393 }
394
395protected:
397#if EDITOR_MODE
398 bool fEditorMode{false};
399#endif
400
401public:
403
408 {
409 public:
410 explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
411 creator_super_type(iViewName, iDisplayName)
412 {
414#if EDITOR_MODE
416#endif
417 }
418 };
419};
420
428template<typename TView, typename TGUIState>
429class StateAwareCustomViewAdapter : public CustomViewAdapter<TView>, public StateAware<TGUIState>
430{
431 // ensures that TView is a subclass of CView
432 static_assert(std::is_convertible<TView *, CView*>::value, "TView must be a subclass of CView");
433
434 // ensures that TGUIState is a subclass of GUIState
435 static_assert(std::is_convertible<TGUIState *, GUIState*>::value, "TGUIState must be a subclass of GUIState");
436
437public:
438 // Constructor
439 template<typename... Args>
440 explicit StateAwareCustomViewAdapter(const CRect &iSize, Args&& ...args) :
441 CustomViewAdapter<TView>(iSize, std::forward<Args>(args)...) {}
442
443protected:
444 // initState - overridden to extract fParams
445 void initState(GUIState *iGUIState) override
446 {
447 ParamAware::initState(iGUIState);
449 }
450};
451
455template<typename TView, typename TGUIState>
456class [[deprecated("Since 4.0.0 - Use StateAwareView instead")]] PluginView : public StateAwareView<TView, TGUIState>
457{
458public:
459 explicit PluginView(const CRect &iSize) : StateAwareView<TView, TGUIState>(iSize) {}
460};
461
465template<typename TGUIState>
466class [[deprecated("Since 4.0.0 - Use StateAwareCustomView instead")]] PluginCustomView : public StateAwareCustomView<TGUIState>
467{
468public:
469 explicit PluginCustomView(const CRect &iSize) : StateAwareCustomView<TGUIState>(iSize) {}
470};
471
475template<typename TView, typename TGUIState>
476class [[deprecated("Since 4.0.0 - Use StateAwareCustomViewAdapter instead")]] PluginCustomViewAdapter : public StateAwareCustomViewAdapter<TView, TGUIState>
477{
478public:
479 template<typename... Args>
480 explicit PluginCustomViewAdapter(const CRect &iSize, Args &&... args) :
481 StateAwareCustomViewAdapter<TView, TGUIState>(iSize, std::forward<Args>(args)...)
482 {}
483};
484
485}
Definition GUIState.h:43
This class is inherited by classes who want to be aware of parameters and be notified when they chang...
Definition ParamAware.h:65
virtual void initState(GUIState *iGUIState)
Called during initialization.
Definition ParamAware.cpp:37
virtual void registerParameters()
Subclasses should override this method to register each parameter.
Definition ParamAware.h:498
void unregisterAll()
Unregisters all parameters.
Definition ParamAware.cpp:98
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition CustomView.h:242
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition CustomView.h:410
TagID getCustomViewTag() const
The tag associated to this custom view.
Definition CustomView.h:353
void onParameterChange(ParamID iParamID) override
Callback when a parameter changes.
Definition CustomView.h:385
void markDirty()
Marks this view dirty which will (at the appropriate time in the rendering lifecycle) trigger a call ...
Definition CustomView.h:347
void afterApplyAttributes() override
Handles the lifecycle behavior getting triggered once all the attributes have been set (which usually...
Definition CustomView.h:388
bool getEditorMode() const
A flag whose purpose is to render/log information during development when the flag is set to true.
Definition CustomView.h:370
void setCustomViewTag(TagID iTag)
Definition CustomView.h:350
CustomViewAdapter(const CRect &iSize, Args &&...args)
Definition CustomView.h:344
TCustomViewCreator< CustomViewAdapter > creator_super_type
Definition CustomView.h:402
void setEditorMode(bool iEditorMode)
Definition CustomView.h:356
Class you should inherit from if you want to write a custom view.
Definition CustomView.h:62
virtual void drawBackColor(CDrawContext *iContext)
Draws the back color (if not set to transparent).
Definition CustomView.cpp:51
TagID getCustomViewTag() const
The tag associated to this custom view.
Definition CustomView.h:108
void onParameterChange(ParamID iParamID) override
Callback when a parameter changes.
Definition CustomView.cpp:84
CustomView(const CRect &iSize)
Definition CustomView.cpp:30
void markDirty()
Marks this view dirty which will (at the appropriate time in the rendering lifecycle) trigger a call ...
Definition CustomView.h:178
void sizeToFit(CCoord iWidth, CCoord iHeight)
Convenient call to size to fit this view according to the and height provided.
Definition CustomView.h:203
void afterApplyAttributes() override
Handles the lifecycle behavior getting triggered once all the attributes have been set (which usually...
Definition CustomView.h:187
bool sizeToFit(BitmapPtr iBitmap, int iFrameCount=1)
Convenient call to size to fit this view to match the bitmap.
Definition CustomView.h:215
void draw(CDrawContext *iContext) override
The basic draw method which will erase the background with the back color.
Definition CustomView.cpp:41
ParamID fTag
Definition CustomView.h:226
bool getEditorMode() const
A flag whose purpose is to render/log information during development when the flag is set to true.
Definition CustomView.cpp:108
CColor fBackColor
Definition CustomView.h:230
CustomView(const CustomView &c)=delete
void setCustomViewTag(TagID iTag)
Definition CustomView.h:82
CColor const & getBackColor() const
The back color (background) for the view.
Definition CustomView.h:74
void setBackColor(CColor const &iColor)
Definition CustomView.cpp:63
void drawStyleChanged()
Called when the draw style is changed (simply marks the view dirty).
Definition CustomView.cpp:76
void setEditorMode(bool iEditorMode)
Definition CustomView.cpp:92
This interface defines some methods that are important in the lifecycle of a custom view.
Definition CustomViewLifecycle.h:29
PluginCustomViewAdapter(const CRect &iSize, Args &&... args)
Definition CustomView.h:480
PluginCustomView(const CRect &iSize)
Definition CustomView.h:469
PluginView(const CRect &iSize)
Definition CustomView.h:459
StateAwareCustomViewAdapter(const CRect &iSize, Args &&...args)
Definition CustomView.h:440
void initState(GUIState *iGUIState) override
Called during initialization.
Definition CustomView.h:445
This class is used to get access to the GUI state and parameters of the plugin with their actual type...
Definition StateAware.h:32
virtual void initState(GUIState *iGUIState)
This method is called by Jamba automatically to initialize the state.
Definition StateAware.h:42
Override from this class if you need to implement a (custom) view specific to a given plugin.
Definition CustomView.h:286
void initState(GUIState *iGUIState) override
Overriden to call both ParamAware::initState() and StateAware::initState().
Definition CustomView.h:302
StateAwareView(const CRect &iSize, Args &&...args)
Definition CustomView.h:296
Generic custom view creator base class.
Definition CustomViewCreator.h:158
void registerColorAttribute(std::string const &iName, typename ColorAttribute::Getter iGetter, typename ColorAttribute::Setter iSetter)
Definition CustomViewCreator.h:952
friend class TCustomViewCreator
Definition CustomViewCreator.h:1200
void registerBooleanAttribute(std::string const &iName, typename BooleanAttribute::Getter iGetter, typename BooleanAttribute::Setter iSetter)
Definition CustomViewCreator.h:1100
void registerTagAttribute(std::string const &iName, typename TagAttribute::Getter iGetter, typename TagAttribute::Setter iSetter)
Definition CustomViewCreator.h:1049
Definition CustomController.h:25
StateAwareView< CustomView, TGUIState > StateAwareCustomView
Shortcut alias when implementing a StateAwareView where the view is a CustomView.
Definition CustomView.h:314
CBitmap * BitmapPtr
Definition Types.h:50
ParamID TagID
Defining a type for tags.
Definition Types.h:58
constexpr ParamID UNDEFINED_PARAM_ID
Constant used throughout the code to test whether the ParamID represents a valid id or an undefined o...
Definition Types.h:48