Jamba C++ API  6.3.0
ToggleButtonView.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 
21 #include "CustomControlView.h"
22 
24 
25 using namespace VSTGUI;
26 using namespace Params;
27 
54 {
55 public:
56  explicit ToggleButtonView(const CRect &iSize) : CustomDiscreteControlView(iSize)
57  {
58  // off color is grey
59  fBackColor = CColor{200, 200, 200};
60  }
61 
62  // draw => does the actual drawing job
63  void draw(CDrawContext *iContext) override;
64 
65  // input events (mouse/keyboard)
66  CMouseEventResult onMouseDown(CPoint &where, const CButtonState &buttons) override;
67 
68  CMouseEventResult onMouseUp(CPoint &where, const CButtonState &buttons) override;
69 
70  CMouseEventResult onMouseCancel() override;
71 
72  int32_t onKeyDown(VstKeyCode &keyCode) override;
73 
74  int32_t onKeyUp(VstKeyCode &keyCode) override;
75 
76  // sizeToFit
77  bool sizeToFit() override;
78 
83  int32 getOffStep() const { return fOffStep; }
84  void setOffStep(int32 iStep) { fOffStep = iStep; markDirty(); }
85 
90  int32 getComputedOffStep() const { return std::max(Utils::ZERO_INT32, getOffStep()); }
91 
96  int32 getOnStep() const { return fOnStep; }
97  void setOnStep(int32 iStep) { fOnStep = iStep; markDirty(); }
98 
103  int32 getComputedOnStep() const;
104 
106  bool isOff() const { return getControlValue() == getComputedOffStep(); }
107 
116  bool isOn() const { return !isOff(); }
117 
119  inline void setOff() { setControlValue(getComputedOffStep()); }
120 
122  inline void setOn() { setControlValue(getComputedOnStep()); }
123 
125  inline void setOnOrOff(bool iOnOrOff) { iOnOrOff ? setOn() : setOff(); }
126 
132  int32 toggleControlValue();
133 
139  int getFrames() const { return fFrames; }
140 
141  void setFrames(int iFrames);
142 
147  CColor const &getOnColor() const { return fOnColor; }
148 
149  void setOnColor(CColor const &iColor) { fOnColor = iColor; }
150 
152  bool getInverse() const { return fInverse; }
153 
154  void setInverse(bool iInverse) { fInverse = iInverse; }
155 
179  BitmapPtr getImage() const { return fImage; }
180 
182  void setImage(BitmapPtr iImage) { fImage = iImage; }
183 
184 protected:
185  int32 fOffStep{-1};
186  int32 fOnStep{-1};
187 
188  int fFrames{4};
189  CColor fOnColor{kRedCColor};
190  BitmapSPtr fImage{nullptr};
191  bool fInverse{false};
192 
193  bool fPressed{false};
194 
195 public:
196  class Creator : public CustomViewCreator<ToggleButtonView, CustomDiscreteControlView>
197  {
198  public:
199  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
200  CustomViewCreator(iViewName, iDisplayName)
201  {
202  registerIntegerAttribute<int32>("on-step", &ToggleButtonView::getOnStep, &ToggleButtonView::setOnStep);
203  registerIntegerAttribute<int32>("off-step", &ToggleButtonView::getOffStep, &ToggleButtonView::setOffStep);
204  registerIntAttribute("frames", &ToggleButtonView::getFrames, &ToggleButtonView::setFrames);
205  registerColorAttribute("on-color", &ToggleButtonView::getOnColor, &ToggleButtonView::setOnColor);
206  registerBitmapAttribute("button-image", &ToggleButtonView::getImage, &ToggleButtonView::setImage);
207  registerBooleanAttribute("inverse", &ToggleButtonView::getInverse, &ToggleButtonView::setInverse);
208  }
209  };
210 };
211 
212 }
Inherit from this class to provide the factory for a custom view.
Definition: CustomViewCreator.h:1314
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: ToggleButtonView.h:96
bool isOn() const
Returns true if the toggle is not in the "off" state.
Definition: ToggleButtonView.h:116
constexpr auto ZERO_INT32
Definition: Constants.h:24
void setOnColor(CColor const &iColor)
Definition: ToggleButtonView.h:149
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: ToggleButtonView.h:83
bool isOff() const
Returns true if the toggle is in the "off" state (meaning the control value is equal to the off step)
Definition: ToggleButtonView.h:106
int32 getComputedOffStep() const
Actual value that the code should use for the underlying parameter.
Definition: ToggleButtonView.h:90
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: ToggleButtonView.h:199
A toggle button is a button that lets you toggle the value of a parameter between an "on" value and a...
Definition: ToggleButtonView.h:53
void setImage(BitmapPtr iImage)
Attribute button-image.
Definition: ToggleButtonView.h:182
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: ToggleButtonView.h:147
bool getInverse() const
Inverses the meaning of "on" and "off" in regards to drawing the view/image.
Definition: ToggleButtonView.h:152
void setInverse(bool iInverse)
Definition: ToggleButtonView.h:154
void setFrames(int iFrames)
Definition: ToggleButtonView.cpp:27
BitmapPtr getImage() const
The image to use to draw the button.
Definition: ToggleButtonView.h:179
Definition: Types.h:29
SharedPointer< CBitmap > BitmapSPtr
Definition: Types.h:50
void setOn()
Shortcut to set the toggle to its "on" state.
Definition: ToggleButtonView.h:122
Definition: CustomController.h:24
void setOffStep(int32 iStep)
Definition: ToggleButtonView.h:84
void setOnOrOff(bool iOnOrOff)
Shortcut to set the toggle to the boolean state provided (true means "on", false means off)
Definition: ToggleButtonView.h:125
Specialization of TCustomControlView for discrete values.
Definition: CustomControlView.h:152
void setOnStep(int32 iStep)
Definition: ToggleButtonView.h:97
CBitmap * BitmapPtr
Definition: Types.h:49
void setOff()
Shortcut to set the toggle to its "off" state.
Definition: ToggleButtonView.h:119
ToggleButtonView(const CRect &iSize)
Definition: ToggleButtonView.h:56
int getFrames() const
The number of frames the image contains.
Definition: ToggleButtonView.h:139