Jamba C++ API 8.0.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>
24#include "CustomViewCreator.h"
25#include "StateAware.h"
26#include "CustomViewLifecycle.h"
27#include "../Concepts.h"
28
30
31using namespace VSTGUI;
32using namespace Params;
33
60class CustomView : public CView, public ParamAware, public ICustomViewLifecycle
61{
62public:
63 // Constructor
64 explicit CustomView(const CRect &iSize);
65
66 // Deleting for now... not sure there is an actual use for it
67 CustomView(const CustomView &c) = delete;
68
73 CColor const &getBackColor() const { return fBackColor; }
74
77 void setBackColor(CColor const &iColor);
78
81 void setCustomViewTag (TagID iTag) { fTag = iTag; }
82
107 TagID getCustomViewTag () const { return fTag; }
108
111 void setEditorMode(bool iEditorMode);
112
141 bool getEditorMode() const;
142
143#if EDITOR_MODE
146 virtual void onEditorModeChanged() {}
147#endif
148
153 void draw(CDrawContext *iContext) override;
154
158 virtual void drawBackColor(CDrawContext *iContext);
159
163 void drawStyleChanged();
164
169 void onParameterChange(ParamID iParamID) override;
170
177 void markDirty() { setDirty(true); }
178
186 void afterApplyAttributes() override
187 {
190 markDirty();
191 }
192
193public:
194 CLASS_METHODS_NOCOPY(CustomView, CControl)
195
196protected:
197 using CView::sizeToFit; // fixes overload hiding warning
198
202 void sizeToFit(CCoord iWidth, CCoord iHeight)
203 {
204 CRect vs(getViewSize());
205 vs.setWidth(iWidth);
206 vs.setHeight(iHeight);
207 setViewSize(vs, true);
208 setMouseableArea(vs);
209 }
210
214 bool sizeToFit(BitmapPtr iBitmap, int iFrameCount = 1)
215 {
216 if(iBitmap)
217 {
218 sizeToFit(iBitmap->getWidth(), iBitmap->getHeight() / iFrameCount);
219 return true;
220 }
221 return false;
222 }
223
224protected:
225 ParamID fTag;
226#if EDITOR_MODE
227 bool fEditorMode{false};
228#endif
230
231public:
238 class Creator : public TCustomViewCreator<CustomView>
239 {
240 public:
241 explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
242 TCustomViewCreator(iViewName, iDisplayName)
243 {
245#if EDITOR_MODE
247#endif
249 }
250 };
251};
252
283template<CViewSubclass TView, GUIStateSubclass TGUIState>
284class StateAwareView : public TView, public StateAware<TGUIState>
285{
286public:
287 // Constructor
288 template<typename... Args>
289 explicit StateAwareView(const CRect &iSize, Args&& ...args) : TView(iSize, std::forward<Args>(args)...) {}
290
291protected:
295 void initState(GUIState *iGUIState) override
296 {
297 TView::initState(iGUIState);
299 }
300};
301
306template<GUIStateSubclass TGUIState>
308
328template<CViewSubclass TView>
329class CustomViewAdapter : public TView, public ParamAware, public ICustomViewLifecycle
330{
331public:
332 // Constructor
333 template<typename... Args>
334 explicit CustomViewAdapter(const CRect &iSize, Args&& ...args) : TView(iSize, std::forward<Args>(args)...), fTag{UNDEFINED_PARAM_ID} {}
335
337 void markDirty() { TView::setDirty(true); }
338
340 void setCustomViewTag (TagID iTag) { fTag = iTag; }
341
343 TagID getCustomViewTag () const { return fTag; }
344
346 void setEditorMode(bool iEditorMode)
347 {
348#if EDITOR_MODE
349 if(fEditorMode != iEditorMode)
350 {
351 fEditorMode = iEditorMode;
352 onEditorModeChanged();
353 }
354#endif
355
356 // when not in editor mode, this does nothing...
357 }
358
360 bool getEditorMode() const
361 {
362#if EDITOR_MODE
363 return fEditorMode;
364#else
365 return false;
366#endif
367 }
368
369#if EDITOR_MODE
371 virtual void onEditorModeChanged() {}
372#endif
373
375 void onParameterChange(ParamID iParamID) override { markDirty(); }
376
378 void afterApplyAttributes() override
379 {
382 markDirty();
383 }
384
385protected:
387#if EDITOR_MODE
388 bool fEditorMode{false};
389#endif
390
391public:
393
398 {
399 public:
400 explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
401 creator_super_type(iViewName, iDisplayName)
402 {
404#if EDITOR_MODE
406#endif
407 }
408 };
409};
410
418template<CViewSubclass TView, GUIStateSubclass TGUIState>
419class StateAwareCustomViewAdapter : public CustomViewAdapter<TView>, public StateAware<TGUIState>
420{
421public:
422 // Constructor
423 template<typename... Args>
424 explicit StateAwareCustomViewAdapter(const CRect &iSize, Args&& ...args) :
425 CustomViewAdapter<TView>(iSize, std::forward<Args>(args)...) {}
426
427protected:
428 // initState - overridden to extract fParams
429 void initState(GUIState *iGUIState) override
430 {
431 ParamAware::initState(iGUIState);
433 }
434};
435
436}
Definition GUIState.h:45
This class is inherited by classes who want to be aware of parameters and be notified when they chang...
Definition ParamAware.h:64
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:485
void unregisterAll()
Unregisters all parameters.
Definition ParamAware.cpp:82
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition CustomView.h:241
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition CustomView.h:400
TagID getCustomViewTag() const
The tag associated with this custom view.
Definition CustomView.h:343
void onParameterChange(ParamID iParamID) override
Callback when a parameter changes.
Definition CustomView.h:375
void markDirty()
Marks this view dirty which will (at the appropriate time in the rendering lifecycle) trigger a call ...
Definition CustomView.h:337
void afterApplyAttributes() override
Handles the lifecycle behavior getting triggered once all the attributes have been set (which usually...
Definition CustomView.h:378
bool getEditorMode() const
A flag whose purpose is to render/log information during development when the flag is set to true.
Definition CustomView.h:360
void setCustomViewTag(TagID iTag)
Definition CustomView.h:340
CustomViewAdapter(const CRect &iSize, Args &&...args)
Definition CustomView.h:334
TCustomViewCreator< CustomViewAdapter > creator_super_type
Definition CustomView.h:392
TagID fTag
Definition CustomView.h:386
void setEditorMode(bool iEditorMode)
Definition CustomView.h:346
Class you should inherit from if you want to write a custom view.
Definition CustomView.h:61
virtual void drawBackColor(CDrawContext *iContext)
Draws the back color (if not set to transparent).
Definition CustomView.cpp:51
TagID getCustomViewTag() const
The tag associated with this custom view.
Definition CustomView.h:107
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:177
void sizeToFit(CCoord iWidth, CCoord iHeight)
Convenient call to size to fit this view according to the and height provided.
Definition CustomView.h:202
void afterApplyAttributes() override
Handles the lifecycle behavior getting triggered once all the attributes have been set (which usually...
Definition CustomView.h:186
bool sizeToFit(BitmapPtr iBitmap, int iFrameCount=1)
Convenient call to size to fit this view to match the bitmap.
Definition CustomView.h:214
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:225
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:229
CustomView(const CustomView &c)=delete
void setCustomViewTag(TagID iTag)
Definition CustomView.h:81
CColor const & getBackColor() const
The back color (background) for the view.
Definition CustomView.h:73
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
StateAwareCustomViewAdapter(const CRect &iSize, Args &&...args)
Definition CustomView.h:424
void initState(GUIState *iGUIState) override
Called during initialization.
Definition CustomView.h:429
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:43
Override from this class if you need to implement a (custom) view specific to a given plugin.
Definition CustomView.h:285
void initState(GUIState *iGUIState) override
Overriden to call both ParamAware::initState() and StateAware::initState().
Definition CustomView.h:295
StateAwareView(const CRect &iSize, Args &&...args)
Definition CustomView.h:289
Generic custom view creator base class.
Definition CustomViewCreator.h:163
void registerBooleanAttribute(std::string const &iName, BooleanAttribute::Getter iGetter, BooleanAttribute::Setter iSetter)
Definition CustomViewCreator.h:1114
void registerColorAttribute(std::string const &iName, ColorAttribute::Getter iGetter, ColorAttribute::Setter iSetter)
Definition CustomViewCreator.h:966
friend class TCustomViewCreator
Definition CustomViewCreator.h:1214
void registerTagAttribute(std::string const &iName, TagAttribute::Getter iGetter, TagAttribute::Setter iSetter)
Definition CustomViewCreator.h:1063
Definition CustomController.h:26
StateAwareView< CustomView, TGUIState > StateAwareCustomView
Shortcut alias when implementing a StateAwareView where the view is a CustomView.
Definition CustomView.h:307
CBitmap * BitmapPtr
Definition Types.h:48
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