Jamba C++ API  4.3.0
MomentaryButtonView.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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>
22 #include "CustomControlView.h"
23 
25 
26 using namespace VSTGUI;
27 
56 {
57 public:
58  explicit MomentaryButtonView(const CRect &iSize) : CustomDiscreteControlView(iSize)
59  {
60  // off color is grey
61  fBackColor = CColor{200,200,200};
62  }
63 
65  void draw(CDrawContext *iContext) override;
66 
68  virtual void drawOn(CDrawContext *iContext);
69 
71  virtual void drawOff(CDrawContext *iContext);
72 
73  // input events (mouse/keyboard)
74  CMouseEventResult onMouseDown(CPoint &where, const CButtonState &buttons) override;
75  CMouseEventResult onMouseUp(CPoint &where, const CButtonState &buttons) override;
76  CMouseEventResult onMouseCancel() override;
77  int32_t onKeyDown(VstKeyCode &keyCode) override;
78  int32_t onKeyUp(VstKeyCode &keyCode) override;
79 
80  // sizeToFit
81  bool sizeToFit() override;
82 
87  int32 getOffStep() const { return fOffStep; }
88 
90  void setOffStep(int32 iStep) { fOffStep = iStep; markDirty(); }
91 
93  int32 getComputedOffStep() const { return std::max(Utils::ZERO_INT32, getOffStep()); }
94 
99  int32 getOnStep() const { return fOnStep; }
100 
102  void setOnStep(int32 iStep) { fOnStep = iStep; markDirty(); }
103 
105  int32 getComputedOnStep() const;
106 
107  // is on or off
108  bool isOff() const { return getControlValue() == getComputedOffStep(); }
109  bool isOn() const { return !isOff(); }
110 
115  CColor const &getOnColor() const { return fOnColor; }
116 
118  void setOnColor(CColor const &iColor) { fOnColor = iColor; }
119 
122  CColor const &getDisabledColor() const { return fDisabledColor; }
123  void setDisabledColor(CColor const &iColor) { fDisabledColor = iColor; }
124 
147  BitmapPtr getImage() const { return fImage; }
148 
150  void setImage(BitmapPtr iImage) { fImage = iImage; }
151 
156  bool getImageHasDisabledState() const { return fImageHasDisabledState; }
157  void setImageHasDisabledState(bool iValue) { fImageHasDisabledState = iValue; }
158 
160  bool getInverse() const { return fInverse; }
161  void setInverse(bool iInverse) { fInverse = iInverse; }
162 
163  // true if held
164  bool isHeld() const { return fHeld; }
165 
166 protected:
167  int32 fOffStep{-1};
168  int32 fOnStep{-1};
169 
172  bool fHeld{false};
173 
174  CColor fOnColor{kRedCColor};
175  CColor fDisabledColor{kBlackCColor};
176  BitmapSPtr fImage{nullptr};
177  bool fInverse{false};
178 
179  // whether the image has disabled state (3 frames) or not (2 frames)
180  bool fImageHasDisabledState{false};
181 
182 public:
183  class Creator : public CustomViewCreator<MomentaryButtonView, CustomDiscreteControlView>
184  {
185  public:
186  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
187  CustomViewCreator(iViewName, iDisplayName)
188  {
189  registerIntegerAttribute<int32>("on-step", &MomentaryButtonView::getOnStep, &MomentaryButtonView::setOnStep);
190  registerIntegerAttribute<int32>("off-step", &MomentaryButtonView::getOffStep, &MomentaryButtonView::setOffStep);
191  registerColorAttribute("on-color", &MomentaryButtonView::getOnColor, &MomentaryButtonView::setOnColor);
192  registerColorAttribute("disabled-color", &MomentaryButtonView::getDisabledColor, &MomentaryButtonView::setDisabledColor);
193  registerBitmapAttribute("button-image", &MomentaryButtonView::getImage, &MomentaryButtonView::setImage);
194  registerBooleanAttribute("button-image-has-disabled-state", &MomentaryButtonView::getImageHasDisabledState, &MomentaryButtonView::setImageHasDisabledState);
195  registerBooleanAttribute("inverse", &MomentaryButtonView::getInverse, &MomentaryButtonView::setInverse);
196  }
197  };
198 };
199 
200 }
BitmapPtr getImage() const
The image to use to draw the button.
Definition: MomentaryButtonView.h:147
Inherit from this class to provide the factory for a custom view.
Definition: CustomViewCreator.h:1312
void setOnColor(CColor const &iColor)
Attribute on-color (the "off" color is the back color...)
Definition: MomentaryButtonView.h:118
MomentaryButtonView(const CRect &iSize)
Definition: MomentaryButtonView.h:58
int32 getOnStep() const
Maps to the "on" value unless it is set to its default (-1) value in which case it maps to getStepCou...
Definition: MomentaryButtonView.h:99
int32 getComputedOffStep() const
Computes "off" step based on value of off-step attribute.
Definition: MomentaryButtonView.h:93
constexpr auto ZERO_INT32
Definition: Constants.h:24
CColor const & getOnColor() const
When no image is provided, back-color is used for the "off" state and on-color for the "on" state (dr...
Definition: MomentaryButtonView.h:115
CColor const & getDisabledColor() const
When no image is provided and the control is disabled, this color is used instead.
Definition: MomentaryButtonView.h:122
void setDisabledColor(CColor const &iColor)
Definition: MomentaryButtonView.h:123
int32 getOffStep() const
Maps to the "off" value unless it is set to its default (-1) value in which case it maps to 0 (should...
Definition: MomentaryButtonView.h:87
void setInverse(bool iInverse)
Definition: MomentaryButtonView.h:161
bool getImageHasDisabledState() const
Flag to determine whether the image contains a disabled state (3 frames) or not (2 frames)
Definition: MomentaryButtonView.h:156
Definition: Types.h:29
SharedPointer< CBitmap > BitmapSPtr
Definition: Types.h:50
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: MomentaryButtonView.h:186
bool isOn() const
Definition: MomentaryButtonView.h:109
void setOnStep(int32 iStep)
Attribute on-step
Definition: MomentaryButtonView.h:102
void setImage(BitmapPtr iImage)
Attribute button-image.
Definition: MomentaryButtonView.h:150
Definition: CustomController.h:24
bool isOff() const
Definition: MomentaryButtonView.h:108
bool isHeld() const
Definition: MomentaryButtonView.h:164
Specialization of TCustomControlView for discrete values.
Definition: CustomControlView.h:152
bool getInverse() const
Inverses the meaning of "on" and "off" in regards to drawing the view/image.
Definition: MomentaryButtonView.h:160
CBitmap * BitmapPtr
Definition: Types.h:49
A momentary button is a button that lets you set the value of a parameter to its "on" value when pres...
Definition: MomentaryButtonView.h:55
void setOffStep(int32 iStep)
Attribute off-step
Definition: MomentaryButtonView.h:90
void setImageHasDisabledState(bool iValue)
Definition: MomentaryButtonView.h:157