Jamba C++ API  6.3.0
StepButtonView.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 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/controls/ccontrol.h>
21 #include <vstgui4/vstgui/lib/cdrawcontext.h>
23 #include "CustomControlView.h"
24 
26 
27 using namespace VSTGUI;
28 
49 {
50 public:
53  enum class EArrowDirection
54  {
55  kAuto,
56  kUp,
57  kRight,
58  kDown,
59  kLeft
60  };
61 public:
62  explicit StepButtonView(const CRect &iSize) : CustomDiscreteControlView(iSize)
63  {
64  }
65 
66  // draw => does the actual drawing job
67  void draw(CDrawContext *iContext) override;
68 
69  // called to display the "held" button state
70  virtual void drawHeldButton(CDrawContext *iContext);
71 
72  // called to display the "released" button state
73  virtual void drawReleasedButton(CDrawContext *iContext);
74 
75  // input events (mouse/keyboard)
76  CMouseEventResult onMouseDown(CPoint &where, const CButtonState &buttons) override;
77  CMouseEventResult onMouseUp(CPoint &where, const CButtonState &buttons) override;
78  CMouseEventResult onMouseCancel() override;
79  int32_t onKeyDown(VstKeyCode &keyCode) override;
80  int32_t onKeyUp(VstKeyCode &keyCode) override;
81 
82  // true if held
83  bool isHeld() const { return fHeld; }
84 
86  inline CColor const &getHeldColor() const { return fHeldColor; }
87  void setHeldColor(CColor const &iColor) { fHeldColor = iColor; markDirty(); }
88 
90  inline CColor const &getReleasedColor() const { return fReleasedColor; }
91  void setReleasedColor(CColor const &iColor) { fReleasedColor = iColor; markDirty(); }
92 
106  BitmapPtr getImage() const { return fImage; }
107 
109  void setImage(BitmapPtr iImage) { fImage = iImage; markDirty(); }
110 
114  int32 getStepIncrement() const { return fStepIncrement; }
115 
119  void setStepIncrement(int32 iStepIncrement) { fStepIncrement = iStepIncrement; fButtonPolygon = nullptr; }
120 
125  int32 getShiftStepIncrement() const { return fShiftStepIncrement; }
126 
130  void setShiftStepIncrement(int32 iStepIncrement) { fShiftStepIncrement = iStepIncrement; }
131 
135  inline bool getWrap() const { return fWrap; }
136  void setWrap(bool iFlag) { fWrap = iFlag; markDirty(); }
137 
142  EArrowDirection getArrowDirection() const { return fArrowDirection; }
143 
145  void setArrowDirection(EArrowDirection iArrowDirection) { fArrowDirection = iArrowDirection; fButtonPolygon = nullptr; markDirty(); }
146 
147 protected:
148  // registerParameters
149  void registerParameters() override;
150 
156  virtual int32 computeNextValue(int32 iIncrement) const;
157 
161  virtual CDrawContext::PointList *getButtonPolygon();
162 
163  // setViewSize
164  void setViewSize(const CRect &rect, bool invalid) override;
165 
166 protected:
170  bool fHeld{false};
171 
172  int32 fStepIncrement{1};
173  int32 fShiftStepIncrement{1};
174  bool fWrap{false};
175  CColor fHeldColor{kRedCColor};
176  CColor fReleasedColor{kGreyCColor};
177  BitmapSPtr fImage{nullptr};
178 
179  EArrowDirection fArrowDirection{EArrowDirection::kAuto};
180  std::unique_ptr<CDrawContext::PointList> fButtonPolygon{};
181 
182 public:
183  class Creator : public CustomViewCreator<StepButtonView, CustomDiscreteControlView>
184  {
185  public:
186  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
187  CustomViewCreator(iViewName, iDisplayName)
188  {
189  registerIntegerAttribute<int32>("step-increment", &StepButtonView::getStepIncrement, &StepButtonView::setStepIncrement);
190  registerIntegerAttribute<int32>("shift-step-increment", &StepButtonView::getShiftStepIncrement, &StepButtonView::setShiftStepIncrement);
191  registerBooleanAttribute("wrap", &StepButtonView::getWrap, &StepButtonView::setWrap);
192  registerColorAttribute("held-color", &StepButtonView::getHeldColor, &StepButtonView::setHeldColor);
193  registerColorAttribute("released-color", &StepButtonView::getReleasedColor, &StepButtonView::setReleasedColor);
194  registerBitmapAttribute("button-image", &StepButtonView::getImage, &StepButtonView::setImage);
195  registerListAttribute<EArrowDirection>("arrow-direction",
197  {
198  {"auto", EArrowDirection::kAuto},
199  {"up", EArrowDirection::kUp},
200  {"right", EArrowDirection::kRight},
201  {"down", EArrowDirection::kDown},
202  {"left", EArrowDirection::kLeft}
203  }
204  );
205  }
206  };
207 };
208 
209 }
void setHeldColor(CColor const &iColor)
Definition: StepButtonView.h:87
EArrowDirection getArrowDirection() const
Used only when no bitmap provided in order to draw an arrow pointing in the direction defined by this...
Definition: StepButtonView.h:142
Inherit from this class to provide the factory for a custom view.
Definition: CustomViewCreator.h:1314
bool isHeld() const
Definition: StepButtonView.h:83
bool getWrap() const
Defines what happens when the value reaches its end of range after being incremented (resp.
Definition: StepButtonView.h:135
CColor const & getReleasedColor() const
Color to use when the button is not held and no image is provided.
Definition: StepButtonView.h:90
void setStepIncrement(int32 iStepIncrement)
Definition: StepButtonView.h:119
EArrowDirection
Represents the direction of the arrow (if not using an image)
Definition: StepButtonView.h:53
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: StepButtonView.h:186
StepButtonView(const CRect &iSize)
Definition: StepButtonView.h:62
void setReleasedColor(CColor const &iColor)
Definition: StepButtonView.h:91
CColor const & getHeldColor() const
Color to use when the button is held and no image is provided.
Definition: StepButtonView.h:86
Definition: Types.h:29
SharedPointer< CBitmap > BitmapSPtr
Definition: Types.h:50
int32 getShiftStepIncrement() const
Value by which this button will increment (positive) or decrement (negative) the parameter when the s...
Definition: StepButtonView.h:125
void setShiftStepIncrement(int32 iStepIncrement)
Definition: StepButtonView.h:130
BitmapPtr getImage() const
The image for the button.
Definition: StepButtonView.h:106
Definition: CustomController.h:24
void setImage(BitmapPtr iImage)
Attribute button-image.
Definition: StepButtonView.h:109
void setArrowDirection(EArrowDirection iArrowDirection)
Attribute arrow-direction.
Definition: StepButtonView.h:145
Specialization of TCustomControlView for discrete values.
Definition: CustomControlView.h:152
void setWrap(bool iFlag)
Definition: StepButtonView.h:136
int32 getStepIncrement() const
Value by which this button will increment (positive) or decrement (negative) the parameter.
Definition: StepButtonView.h:114
CBitmap * BitmapPtr
Definition: Types.h:49
A step button lets you step through the values of a parameter by repeatedly clicking on the button.
Definition: StepButtonView.h:48