Jamba  3.0.2
CustomView.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 
100 class CustomView : public CView, public GUIParamCxAware
101 {
102 public:
103  // Constructor
104  explicit CustomView(const CRect &iSize);
105 
106  // Deleting for now... not sure there is an actual use for it
107  CustomView(const CustomView &c) = delete;
108 
109  // setBackColor / getBackColor
110  void setBackColor(CColor const &iColor);
111  CColor const &getBackColor() const { return fBackColor; }
112 
113  // setCustomViewTag / getCustomViewTag
114  void setCustomViewTag (int32_t iTag) { fTag = iTag; }
115  int32_t getCustomViewTag () const { return fTag; }
116 
117  // setEditorMode / getEditorMode
118  void setEditorMode(bool iEditorMode);
119  bool getEditorMode() const;
120 
124 #if EDITOR_MODE
125  virtual void onEditorModeChanged() {}
126 #endif
127 
132  void draw(CDrawContext *iContext) override;
133 
137  virtual void drawBackColor(CDrawContext *iContext);
138 
142  void drawStyleChanged();
143 
147  void onParameterChange(ParamID iParamID) override;
148 
149  // markDirty
150  inline void markDirty() { setDirty(true); }
151 
152 public:
153  CLASS_METHODS_NOCOPY(CustomView, CControl)
154 
155 protected:
156  using CView::sizeToFit; // fixes overload hiding warning
157 
161  void sizeToFit(CCoord iWidth, CCoord iHeight)
162  {
163  CRect vs(getViewSize());
164  vs.setWidth(iWidth);
165  vs.setHeight(iHeight);
166  setViewSize(vs, true);
167  setMouseableArea(vs);
168  }
169 
173  bool sizeToFit(BitmapPtr iBitmap, int iFrameCount = 1)
174  {
175  if(iBitmap)
176  {
177  sizeToFit(iBitmap->getWidth(), iBitmap->getHeight() / iFrameCount);
178  return true;
179  }
180  return false;
181  }
182 
183 protected:
184  int32_t fTag;
186  CColor fBackColor;
187 
188 public:
189  class Creator : public TCustomViewCreator<CustomView>
190  {
191  public:
192  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
193  TCustomViewCreator<CustomView>(iViewName, iDisplayName)
194  {
195  registerTagAttribute("custom-view-tag", &CustomView::getCustomViewTag, &CustomView::setCustomViewTag);
196 #if EDITOR_MODE
197  registerBooleanAttribute("editor-mode", &CustomView::getEditorMode, &CustomView::setEditorMode);
198 #endif
199  registerColorAttribute(UIViewCreator::kAttrBackColor, &CustomView::getBackColor, &CustomView::setBackColor);
200  }
201  };
202 };
203 
211 template<typename TView, typename TGUIPluginState>
212 class PluginView : public TView, public PluginAccessor<TGUIPluginState>
213 {
214 public:
215  // Constructor
216  explicit PluginView(const CRect &iSize) : TView(iSize) {}
217 
218 protected:
219  // initState - overridden to extract fParams
220  void initState(GUIState *iGUIState) override
221  {
222  TView::initState(iGUIState);
224  }
225 };
226 
233 template<typename TGUIPluginState>
235 
242 template<typename TView>
243 class CustomViewAdapter : public TView, public GUIParamCxAware
244 {
245 public:
246  // Constructor
247  template<typename... Args>
248  explicit CustomViewAdapter(const CRect &iSize, Args... args) : TView(iSize, args...), fTag{-1} {}
249 
250  // markDirty
251  inline void markDirty() { TView::setDirty(true); }
252 
253  // setCustomViewTag / getCustomViewTag
254  void setCustomViewTag (int32_t iTag) { fTag = iTag; }
255  int32_t getCustomViewTag () const { return fTag; }
256 
260  void onParameterChange(ParamID iParamID) override { markDirty(); };
261 
262 protected:
263  int32_t fTag;
264 
265 public:
266  class Creator : public TCustomViewCreator<CustomViewAdapter>
267  {
268  public:
269  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
270  TCustomViewCreator<CustomViewAdapter>(iViewName, iDisplayName)
271  {
273  }
274  };
275 };
276 
284 template<typename TView, typename TGUIPluginState>
285 class PluginCustomViewAdapter : public CustomViewAdapter<TView>, public PluginAccessor<TGUIPluginState>
286 {
287 public:
288  // Constructor
289  template<typename... Args>
290  explicit PluginCustomViewAdapter(const CRect &iSize, Args... args) : CustomViewAdapter<TView>(iSize, args...) {}
291 
292 protected:
293  // initState - overridden to extract fParams
294  void initState(GUIState *iGUIState) override
295  {
296  GUIParamCxAware::initState(iGUIState);
298  }
299 };
300 
301 
302 }
303 }
304 }
305 }
CColor const & getBackColor() const
Definition: CustomView.h:111
bool sizeToFit(BitmapPtr iBitmap, int iFrameCount=1)
Definition: CustomView.h:173
Definition: GUIState.h:39
void initState(GUIState *iGUIState) override
Definition: CustomView.h:220
int32_t getCustomViewTag() const
Definition: CustomView.h:115
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:251
CBitmap * BitmapPtr
Definition: Types.h:35
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: CustomView.h:192
Definition: CustomView.h:100
void initState(GUIState *iGUIState) override
Definition: CustomView.h:294
void markDirty()
Definition: CustomView.h:150
bool getEditorMode() const
Definition: CustomView.cpp:113
int32_t getCustomViewTag() const
Definition: CustomView.h:255
PluginView(const CRect &iSize)
Definition: CustomView.h:216
virtual void initState(GUIState *iGUIState)
Definition: PluginAccessor.h:37
void setCustomViewTag(int32_t iTag)
Definition: CustomView.h:114
int32_t fTag
Definition: CustomView.h:184
Definition: PluginAccessor.h:33
Definition: CustomView.h:212
Definition: GUIParamCxAware.h:38
void sizeToFit(CCoord iWidth, CCoord iHeight)
Definition: CustomView.h:161
virtual void initState(GUIState *iGUIState)
Definition: GUIParamCxAware.cpp:37
void setEditorMode(bool iEditorMode)
Definition: CustomView.cpp:97
void setBackColor(CColor const &iColor)
Definition: CustomView.cpp:68
CustomViewAdapter(const CRect &iSize, Args... args)
Definition: CustomView.h:248
CColor fBackColor
Definition: CustomView.h:186
Definition: CustomViewCreator.h:121
bool fEditorMode
Definition: CustomView.h:185
PluginCustomViewAdapter(const CRect &iSize, Args... args)
Definition: CustomView.h:290
void onParameterChange(ParamID iParamID) override
Definition: CustomView.h:260
void setCustomViewTag(int32_t iTag)
Definition: CustomView.h:254
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: CustomView.h:269