Jamba  3.1.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 "PluginAccessor.h"
27 
28 namespace pongasoft {
29 namespace VST {
30 namespace GUI {
31 namespace Views {
32 
33 using namespace VSTGUI;
34 using namespace Params;
35 
44 class CustomView : public CView, public GUIParamCxAware
45 {
46 public:
47  // Constructor
48  explicit CustomView(const CRect &iSize);
49 
50  // Deleting for now... not sure there is an actual use for it
51  CustomView(const CustomView &c) = delete;
52 
53  // setBackColor / getBackColor
54  void setBackColor(CColor const &iColor);
55  CColor const &getBackColor() const { return fBackColor; }
56 
57  // setCustomViewTag / getCustomViewTag
58  void setCustomViewTag (int32_t iTag) { fTag = iTag; }
59  int32_t getCustomViewTag () const { return fTag; }
60 
61  // setEditorMode / getEditorMode
62  void setEditorMode(bool iEditorMode);
63  bool getEditorMode() const;
64 
68 #if EDITOR_MODE
69  virtual void onEditorModeChanged() {}
70 #endif
71 
76  void draw(CDrawContext *iContext) override;
77 
81  virtual void drawBackColor(CDrawContext *iContext);
82 
86  void drawStyleChanged();
87 
91  void onParameterChange(ParamID iParamID) override;
92 
93  // markDirty
94  inline void markDirty() { setDirty(true); }
95 
96 public:
97  CLASS_METHODS_NOCOPY(CustomView, CControl)
98 
99 protected:
100  using CView::sizeToFit; // fixes overload hiding warning
101 
105  void sizeToFit(CCoord iWidth, CCoord iHeight)
106  {
107  CRect vs(getViewSize());
108  vs.setWidth(iWidth);
109  vs.setHeight(iHeight);
110  setViewSize(vs, true);
111  setMouseableArea(vs);
112  }
113 
117  bool sizeToFit(BitmapPtr iBitmap, int iFrameCount = 1)
118  {
119  if(iBitmap)
120  {
121  sizeToFit(iBitmap->getWidth(), iBitmap->getHeight() / iFrameCount);
122  return true;
123  }
124  return false;
125  }
126 
127 protected:
128  int32_t fTag;
130  CColor fBackColor;
131 
132 public:
133  class Creator : public TCustomViewCreator<CustomView>
134  {
135  public:
136  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
137  TCustomViewCreator<CustomView>(iViewName, iDisplayName)
138  {
139  registerTagAttribute("custom-view-tag", &CustomView::getCustomViewTag, &CustomView::setCustomViewTag);
140 #if EDITOR_MODE
141  registerBooleanAttribute("editor-mode", &CustomView::getEditorMode, &CustomView::setEditorMode);
142 #endif
143  registerColorAttribute(UIViewCreator::kAttrBackColor, &CustomView::getBackColor, &CustomView::setBackColor);
144  }
145  };
146 };
147 
155 template<typename TView, typename TGUIPluginState>
156 class PluginView : public TView, public PluginAccessor<TGUIPluginState>
157 {
158 public:
159  // Constructor
160  explicit PluginView(const CRect &iSize) : TView(iSize) {}
161 
162 protected:
163  // initState - overridden to extract fParams
164  void initState(GUIState *iGUIState) override
165  {
166  TView::initState(iGUIState);
168  }
169 };
170 
177 template<typename TGUIPluginState>
179 
186 template<typename TView>
187 class CustomViewAdapter : public TView, public GUIParamCxAware
188 {
189 public:
190  // Constructor
191  template<typename... Args>
192  explicit CustomViewAdapter(const CRect &iSize, Args... args) : TView(iSize, args...), fTag{-1} {}
193 
194  // markDirty
195  inline void markDirty() { TView::setDirty(true); }
196 
197  // setCustomViewTag / getCustomViewTag
198  void setCustomViewTag (int32_t iTag) { fTag = iTag; }
199  int32_t getCustomViewTag () const { return fTag; }
200 
204  void onParameterChange(ParamID iParamID) override { markDirty(); };
205 
206 protected:
207  int32_t fTag;
208 
209 public:
210  class Creator : public TCustomViewCreator<CustomViewAdapter>
211  {
212  public:
213  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
214  TCustomViewCreator<CustomViewAdapter>(iViewName, iDisplayName)
215  {
217  }
218  };
219 };
220 
228 template<typename TView, typename TGUIPluginState>
229 class PluginCustomViewAdapter : public CustomViewAdapter<TView>, public PluginAccessor<TGUIPluginState>
230 {
231 public:
232  // Constructor
233  template<typename... Args>
234  explicit PluginCustomViewAdapter(const CRect &iSize, Args... args) : CustomViewAdapter<TView>(iSize, args...) {}
235 
236 protected:
237  // initState - overridden to extract fParams
238  void initState(GUIState *iGUIState) override
239  {
240  GUIParamCxAware::initState(iGUIState);
242  }
243 };
244 
245 
246 }
247 }
248 }
249 }
CColor const & getBackColor() const
Definition: CustomView.h:55
bool sizeToFit(BitmapPtr iBitmap, int iFrameCount=1)
Definition: CustomView.h:117
Definition: GUIState.h:39
void initState(GUIState *iGUIState) override
Definition: CustomView.h:164
int32_t getCustomViewTag() const
Definition: CustomView.h:59
void registerTagAttribute(std::string const &iName, typename TagAttribute::Getter iGetter, typename TagAttribute::Setter iSetter)
Definition: CustomViewCreator.h:807
Definition: Clock.h:22
void markDirty()
Definition: CustomView.h:195
CBitmap * BitmapPtr
Definition: Types.h:49
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: CustomView.h:136
Definition: CustomView.h:44
void initState(GUIState *iGUIState) override
Definition: CustomView.h:238
void markDirty()
Definition: CustomView.h:94
bool getEditorMode() const
Definition: CustomView.cpp:112
int32_t getCustomViewTag() const
Definition: CustomView.h:199
PluginView(const CRect &iSize)
Definition: CustomView.h:160
virtual void initState(GUIState *iGUIState)
Definition: PluginAccessor.h:37
void setCustomViewTag(int32_t iTag)
Definition: CustomView.h:58
int32_t fTag
Definition: CustomView.h:128
Definition: PluginAccessor.h:33
Definition: CustomView.h:156
Definition: Types.h:29
Definition: GUIParamCxAware.h:38
void sizeToFit(CCoord iWidth, CCoord iHeight)
Definition: CustomView.h:105
virtual void initState(GUIState *iGUIState)
Definition: GUIParamCxAware.cpp:37
void setEditorMode(bool iEditorMode)
Definition: CustomView.cpp:96
void setBackColor(CColor const &iColor)
Definition: CustomView.cpp:67
CustomViewAdapter(const CRect &iSize, Args... args)
Definition: CustomView.h:192
CColor fBackColor
Definition: CustomView.h:130
Definition: CustomViewCreator.h:121
bool fEditorMode
Definition: CustomView.h:129
PluginCustomViewAdapter(const CRect &iSize, Args... args)
Definition: CustomView.h:234
void onParameterChange(ParamID iParamID) override
Definition: CustomView.h:204
void setCustomViewTag(int32_t iTag)
Definition: CustomView.h:198
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: CustomView.h:213