Jamba  3.1.0
ToggleButtonView.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 
21 #include "CustomControlView.h"
22 
23 #include <iostream>
24 
25 namespace pongasoft {
26 namespace VST {
27 namespace GUI {
28 namespace Views {
29 
30 using namespace VSTGUI;
31 using namespace Params;
32 
34 {
35 public:
36  explicit ToggleButtonView(const CRect &iSize) : TCustomControlView(iSize)
37  {
38  // off color is grey
39  fBackColor = CColor{200, 200, 200};
40  }
41 
42  // draw => does the actual drawing job
43  void draw(CDrawContext *iContext) override;
44 
45  // input events (mouse/keyboard)
46  CMouseEventResult onMouseDown(CPoint &where, const CButtonState &buttons) override;
47 
48  CMouseEventResult onMouseUp(CPoint &where, const CButtonState &buttons) override;
49 
50  CMouseEventResult onMouseCancel() override;
51 
52  int32_t onKeyDown(VstKeyCode &keyCode) override;
53 
54  int32_t onKeyUp(VstKeyCode &keyCode) override;
55 
56  // sizeToFit
57  bool sizeToFit() override;
58 
59  // is on or off
60  bool isOn() const { return getControlValue(); }
61 
62  bool isOff() const { return !isOn(); }
63 
64  // get/set frames (should be either 2 or 4) 4 includes the pressed state
65  int getFrames() const { return fFrames; }
66 
67  void setFrames(int iFrames);
68 
69  // get/setOnColor (the off color is the back color...)
70  CColor const &getOnColor() const { return fOnColor; }
71 
72  void setOnColor(CColor const &iColor) { fOnColor = iColor; }
73 
74  // get/setInverse (toggles which image is on and which is off)
75  bool getInverse() const { return fInverse; }
76 
77  void setInverse(bool iInverse) { fInverse = iInverse; }
78 
91  BitmapPtr getImage() const { return fImage; }
92  void setImage(BitmapPtr iImage) { fImage = iImage; }
93 
94 public:
95  CLASS_METHODS_NOCOPY(ToggleButtonView, TCustomControlView<bool>)
96 
97 protected:
98  int fFrames{4};
99  CColor fOnColor{kRedCColor};
100  BitmapSPtr fImage{nullptr};
101  bool fInverse{false};
102 
103  bool fPressed{false};
104 
105 public:
106  class Creator : public CustomViewCreator<ToggleButtonView, TCustomControlView<bool>>
107  {
108  public:
109  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
110  CustomViewCreator(iViewName, iDisplayName)
111  {
112  registerIntAttribute("frames", &ToggleButtonView::getFrames, &ToggleButtonView::setFrames);
113  registerColorAttribute("on-color", &ToggleButtonView::getOnColor, &ToggleButtonView::setOnColor);
114  registerBitmapAttribute("button-image", &ToggleButtonView::getImage, &ToggleButtonView::setImage);
115  registerBooleanAttribute("inverse", &ToggleButtonView::getInverse, &ToggleButtonView::setInverse);
116  }
117  };
118 };
119 
120 }
121 }
122 }
123 }
Definition: CustomViewCreator.h:981
void setInverse(bool iInverse)
Definition: ToggleButtonView.h:77
SharedPointer< CBitmap > BitmapSPtr
Definition: Types.h:50
Definition: Clock.h:22
CBitmap * BitmapPtr
Definition: Types.h:49
CColor const & getOnColor() const
Definition: ToggleButtonView.h:70
Definition: CustomControlView.h:63
Definition: ToggleButtonView.h:33
int getFrames() const
Definition: ToggleButtonView.h:65
Definition: Types.h:29
void setImage(BitmapPtr iImage)
Definition: ToggleButtonView.h:92
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: ToggleButtonView.h:109
bool isOn() const
Definition: ToggleButtonView.h:60
bool getInverse() const
Definition: ToggleButtonView.h:75
void setFrames(int iFrames)
Definition: ToggleButtonView.cpp:30
bool isOff() const
Definition: ToggleButtonView.h:62
BitmapPtr getImage() const
Definition: ToggleButtonView.h:91
void setOnColor(CColor const &iColor)
Definition: ToggleButtonView.h:72
ToggleButtonView(const CRect &iSize)
Definition: ToggleButtonView.h:36