Jamba C++ API  4.0.0
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 (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 
20 #include <vstgui4/vstgui/lib/cview.h>
21 #include <map>
25 #include "CustomViewCreator.h"
26 #include "StateAware.h"
27 #include "CustomViewLifecycle.h"
28 
30 
31 using namespace VSTGUI;
32 using namespace Params;
33 
60 class CustomView : public CView, public ParamAware, public ICustomViewLifecycle
61 {
62 public:
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
144 
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  inline void markDirty() { setDirty(true); }
178 
186  void afterApplyAttributes() override
187  {
188  unregisterAll();
189  registerParameters();
190  markDirty();
191  }
192 
193 public:
194  CLASS_METHODS_NOCOPY(CustomView, CControl)
195 
196 protected:
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 
224 protected:
225  ParamID fTag;
226 #if EDITOR_MODE
227  bool fEditorMode{false};
228 #endif
229  CColor fBackColor;
230 
231 public:
238  class Creator : public TCustomViewCreator<CustomView>
239  {
240  public:
241  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
242  TCustomViewCreator<CustomView>(iViewName, iDisplayName)
243  {
244  registerTagAttribute("custom-view-tag", &CustomView::getCustomViewTag, &CustomView::setCustomViewTag);
245 #if EDITOR_MODE
246  registerBooleanAttribute("editor-mode", &CustomView::getEditorMode, &CustomView::setEditorMode);
247 #endif
248  registerColorAttribute(UIViewCreator::kAttrBackColor, &CustomView::getBackColor, &CustomView::setBackColor);
249  }
250  };
251 };
252 
283 template<typename TView, typename TGUIState>
284 class StateAwareView : public TView, public StateAware<TGUIState>
285 {
286  // ensures that TView is a subclass of CView
287  static_assert(std::is_convertible<TView *, CView*>::value, "TView must be a subclass of CView");
288 
289  // ensures that TGUIState is a subclass of GUIState
290  static_assert(std::is_convertible<TGUIState *, GUIState*>::value, "TGUIState must be a subclass of GUIState");
291 
292 public:
293  // Constructor
294  template<typename... Args>
295  explicit StateAwareView(const CRect &iSize, Args&& ...args) : TView(iSize, std::forward<Args>(args)...) {}
296 
297 protected:
301  void initState(GUIState *iGUIState) override
302  {
303  TView::initState(iGUIState);
305  }
306 };
307 
312 template<typename TGUIState>
314 
334 template<typename TView>
335 class CustomViewAdapter : public TView, public ParamAware, public ICustomViewLifecycle
336 {
337  // ensures that TView is a subclass of CView
338  static_assert(std::is_convertible<TView *, CView*>::value, "TView must be a subclass of CView");
339 
340 public:
341  // Constructor
342  template<typename... Args>
343  explicit CustomViewAdapter(const CRect &iSize, Args&& ...args) : TView(iSize, std::forward<Args>(args)...), fTag{UNDEFINED_PARAM_ID} {}
344 
346  inline void markDirty() { TView::setDirty(true); }
347 
349  void setCustomViewTag (TagID iTag) { fTag = iTag; }
350 
352  TagID getCustomViewTag () const { return fTag; }
353 
355  void setEditorMode(bool iEditorMode)
356  {
357 #if EDITOR_MODE
358  if(fEditorMode != iEditorMode)
359  {
360  fEditorMode = iEditorMode;
361  onEditorModeChanged();
362  }
363 #endif
364 
365  // when not in editor mode, this does nothing...
366  }
367 
369  bool getEditorMode() const
370  {
371 #if EDITOR_MODE
372  return fEditorMode;
373 #else
374  return false;
375 #endif
376  }
377 
378 #if EDITOR_MODE
379  virtual void onEditorModeChanged() {}
381 #endif
382 
384  void onParameterChange(ParamID iParamID) override { markDirty(); };
385 
387  void afterApplyAttributes() override
388  {
389  unregisterAll();
390  registerParameters();
391  markDirty();
392  }
393 
394 protected:
396 #if EDITOR_MODE
397  bool fEditorMode{false};
398 #endif
399 
400 public:
402 
407  {
408  public:
409  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
410  creator_super_type(iViewName, iDisplayName)
411  {
412  creator_super_type::registerTagAttribute("custom-view-tag", &CustomViewAdapter::getCustomViewTag, &CustomViewAdapter::setCustomViewTag);
413 #if EDITOR_MODE
414  creator_super_type::registerBooleanAttribute("editor-mode", &CustomViewAdapter::getEditorMode, &CustomViewAdapter::setEditorMode);
415 #endif
416  }
417  };
418 };
419 
427 template<typename TView, typename TGUIState>
428 class StateAwareCustomViewAdapter : public CustomViewAdapter<TView>, public StateAware<TGUIState>
429 {
430  // ensures that TView is a subclass of CView
431  static_assert(std::is_convertible<TView *, CView*>::value, "TView must be a subclass of CView");
432 
433  // ensures that TGUIState is a subclass of GUIState
434  static_assert(std::is_convertible<TGUIState *, GUIState*>::value, "TGUIState must be a subclass of GUIState");
435 
436 public:
437  // Constructor
438  template<typename... Args>
439  explicit StateAwareCustomViewAdapter(const CRect &iSize, Args&& ...args) :
440  CustomViewAdapter<TView>(iSize, std::forward<Args>(args)...) {}
441 
442 protected:
443  // initState - overridden to extract fParams
444  void initState(GUIState *iGUIState) override
445  {
446  ParamAware::initState(iGUIState);
448  }
449 };
450 
454 template<typename TView, typename TGUIState>
455 class [[deprecated("Since 4.0.0 - Use StateAwareView instead")]] PluginView : public StateAwareView<TView, TGUIState>
456 {
457 public:
458  explicit PluginView(const CRect &iSize) : StateAwareView<TView, TGUIState>(iSize) {}
459 };
460 
464 template<typename TGUIState>
465 class [[deprecated("Since 4.0.0 - Use StateAwareCustomView instead")]] PluginCustomView : public StateAwareCustomView<TGUIState>
466 {
467 public:
468  explicit PluginCustomView(const CRect &iSize) : StateAwareCustomView<TGUIState>(iSize) {}
469 };
470 
474 template<typename TView, typename TGUIState>
475 class [[deprecated("Since 4.0.0 - Use StateAwareCustomViewAdapter instead")]] PluginCustomViewAdapter : public StateAwareCustomViewAdapter<TView, TGUIState>
476 {
477 public:
478  template<typename... Args>
479  explicit PluginCustomViewAdapter(const CRect &iSize, Args &&... args) :
480  StateAwareCustomViewAdapter<TView, TGUIState>(iSize, std::forward<Args>(args)...)
481  {}
482 };
483 
484 }
TagID getCustomViewTag() const
The tag associated to this custom view.
Definition: CustomView.h:352
void setBackColor(CColor const &iColor)
Definition: CustomView.cpp:62
StateAwareView(const CRect &iSize, Args &&...args)
Definition: CustomView.h:295
This class is inherited by classes who want to be aware of parameters and be notified when they chang...
Definition: ParamAware.h:63
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:387
This class is used to get access to the GUI state and parameters of the plugin with their actual type...
Definition: StateAware.h:30
Definition: GUIState.h:40
TagID fTag
Definition: CustomView.h:395
PluginView(const CRect &iSize)
Definition: CustomView.h:458
Class you should inherit from if you want to write a custom view.
Definition: CustomView.h:60
This interface defines some methods that are important in the lifecycle of a custom view.
Definition: CustomViewLifecycle.h:27
void initState(GUIState *iGUIState) override
This method is called by Jamba automatically to initialize the state.
Definition: CustomView.h:444
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:107
virtual void initState(GUIState *iGUIState)
Called during initialization.
Definition: ParamAware.cpp:36
void setCustomViewTag(TagID iTag)
Definition: CustomView.h:81
ParamID TagID
Defining a type for tags.
Definition: Types.h:57
void afterApplyAttributes() override
Handles the lifecycle behavior getting triggered once all the attributes have been set (which usually...
Definition: CustomView.h:186
Defines and registers the attributes exposed in the VSTGUI Editor and XML file (.uidesc) for CustomVi...
Definition: CustomView.h:406
This class can be used to extend VST SDK classes directly while still benefiting from the extensions ...
Definition: CustomView.h:428
void markDirty()
Marks this view dirty which will (at the appropriate time in the rendering lifecycle) trigger a call ...
Definition: CustomView.h:346
void onParameterChange(ParamID iParamID) override
Callback when a parameter changes.
Definition: CustomView.h:384
void setEditorMode(bool iEditorMode)
Definition: CustomView.cpp:91
Definition: CustomView.h:455
This class can be used to extend VSTGUI classes directly while still benefiting from the extensions a...
Definition: CustomView.h:335
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: CustomView.h:241
Definition: Types.h:29
CColor fBackColor
Definition: CustomView.h:229
bool getEditorMode() const
A flag whose purpose is to render/log information during development when the flag is set to true.
Definition: CustomView.h:369
Defines and registers the attributes exposed in the VSTGUI Editor and XML file (.uidesc) for CustomVi...
Definition: CustomView.h:238
void markDirty()
Marks this view dirty which will (at the appropriate time in the rendering lifecycle) trigger a call ...
Definition: CustomView.h:177
virtual void initState(GUIState *iGUIState)
This method is called by Jamba automatically to initialize the state.
Definition: StateAware.h:41
StateAwareCustomViewAdapter(const CRect &iSize, Args &&...args)
Definition: CustomView.h:439
Definition: CustomController.h:24
void setEditorMode(bool iEditorMode)
Definition: CustomView.h:355
void initState(GUIState *iGUIState) override
Overriden to call both ParamAware::initState() and StateAware::initState()
Definition: CustomView.h:301
Override from this class if you need to implement a (custom) view specific to a given plugin.
Definition: CustomView.h:284
bool sizeToFit(BitmapPtr iBitmap, int iFrameCount=1)
Convenient call to size to fit this view to match the bitmap.
Definition: CustomView.h:214
void setCustomViewTag(TagID iTag)
Definition: CustomView.h:349
PluginCustomView(const CRect &iSize)
Definition: CustomView.h:468
Generic custom view creator base class.
Definition: CustomViewCreator.h:155
CustomViewAdapter(const CRect &iSize, Args &&...args)
Definition: CustomView.h:343
TagID getCustomViewTag() const
The tag associated to this custom view.
Definition: CustomView.h:107
CBitmap * BitmapPtr
Definition: Types.h:49
PluginCustomViewAdapter(const CRect &iSize, Args &&... args)
Definition: CustomView.h:479
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:47
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: CustomView.h:409
CColor const & getBackColor() const
The back color (background) for the view.
Definition: CustomView.h:73