Jamba C++ API  4.1.0
TextButtonView.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 
20 #include <vstgui4/vstgui/lib/controls/cbuttons.h>
22 
24 
25 using namespace VSTGUI;
26 using namespace Params;
27 
47 class TextButtonView : public CustomViewAdapter<CTextButton>
48 {
49 public:
50  using OnClickListener = std::function<void()>;
51 
52  // Constructor
53  explicit TextButtonView(const CRect &iSize) : CustomViewAdapter(iSize) {}
54 
56  virtual void setTitleTag(ParamID iValue);
57 
61  ParamID getTitleTag() const { return fTitleTag; };
62 
64  CColor const &getDisabledTextColor() const { return fDisabledTextColor; }
65 
67  virtual void setDisabledTextColor(CColor const &iColor) { fDisabledTextColor = iColor; };
68 
70  GradientPtr getDisabledGradient() const { return fDisabledGradient; }
71 
73  virtual void setDisabledGradient(GradientPtr iGradient) { fDisabledGradient = iGradient; };
74 
75  void setMouseEnabled(bool bEnable) override;
76 
77  // draw => overridden to handle disabled state
78  void draw(CDrawContext *context) override;
79 
80  // registerParameters
81  void registerParameters() override;
82 
83  // handles value change
84  void valueChanged() override;
85 
86  // call to programmatically click the button
87  virtual void click();
88 
89  // call to programmatically unClick the button
90  virtual void unClick();
91 
92  // callback when the button is clicked
93  virtual void onClick();
94 
95  // setOnClickListener
96  void setOnClickListener(OnClickListener iListener) { fOnClickListener = std::move(iListener); }
97 
120  BitmapPtr getImage() const { return fImage; }
121  void setImage(BitmapPtr iImage) { fImage = iImage; }
122 
127  bool getImageHasDisabledState() const { return fImageHasDisabledState; }
128  void setImageHasDisabledState(bool iValue) { fImageHasDisabledState = iValue; }
129 
138  int32 getPrecisionOverride() const { return fPrecisionOverride; }
139  void setPrecisionOverride(int32 iPrecisionOverride) { fPrecisionOverride = iPrecisionOverride; markDirty(); }
140 
141 protected:
145  virtual void drawButtonText(CDrawContext *context);
146 
152  virtual void drawButtonImage(CDrawContext *context);
153 
154 protected:
155  // (optionally) tie the title of this text button to a (Jmb) parameter
156  ParamID fTitleTag{UNDEFINED_PARAM_ID};
157 
158  // Which color and gradient to use when disabled
161 
162  // the underlying parameter => use its string representation to display the text of the button
163  int32 fPrecisionOverride{-1};
164 
165  // the image to use (in place of text)
166  BitmapSPtr fImage{nullptr};
167 
168  // whether the image has disabled state (3 frames) or not (2 frames)
169  bool fImageHasDisabledState{false};
170 
171  // the onclick listener
172  OnClickListener fOnClickListener{};
173 
174 public:
175  class Creator : public CustomViewCreator<TextButtonView, CustomViewAdapter<CTextButton>>
176  {
177  public:
178  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
179  CustomViewCreator(iViewName, iDisplayName, VSTGUI::UIViewCreator::kCTextButton)
180  {
181  registerTagAttribute("title-tag", &TextButtonView::getTitleTag, &TextButtonView::setTitleTag);
182  registerColorAttribute("disabled-text-color", &TextButtonView::getDisabledTextColor, &TextButtonView::setDisabledTextColor);
183  registerGradientAttribute("disabled-gradient", &TextButtonView::getDisabledGradient, &TextButtonView::setDisabledGradient);
184  registerBitmapAttribute("button-image", &TextButtonView::getImage, &TextButtonView::setImage);
185  registerBooleanAttribute("button-image-has-disabled-state", &TextButtonView::getImageHasDisabledState, &TextButtonView::setImageHasDisabledState);
186  registerIntegerAttribute<int32>("precision-override", &TextButtonView::getPrecisionOverride, &TextButtonView::setPrecisionOverride);
187  }
188  };
189 };
190 
191 }
192 
Inherit from this class to provide the factory for a custom view.
Definition: CustomViewCreator.h:1311
int32 getPrecisionOverride() const
Allow to override the precision of the parameter.
Definition: TextButtonView.h:138
GradientPtr getDisabledGradient() const
Gradient used to draw the button when no image is provided and the button is disabled.
Definition: TextButtonView.h:70
void setOnClickListener(OnClickListener iListener)
Definition: TextButtonView.h:96
CColor fDisabledTextColor
Definition: TextButtonView.h:159
SharedPointer< CGradient > GradientSPtr
Definition: Types.h:58
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: TextButtonView.h:178
Extension of the CTextButton view to add more functionality.
Definition: TextButtonView.h:47
void setImage(BitmapPtr iImage)
Definition: TextButtonView.h:121
virtual void setTitleTag(ParamID iValue)
Attribute title-tag.
Definition: TextButtonView.cpp:40
GradientSPtr fDisabledGradient
Definition: TextButtonView.h:160
BitmapPtr getImage() const
The image for the button.
Definition: TextButtonView.h:120
This class can be used to extend VSTGUI classes directly while still benefiting from the extensions a...
Definition: CustomView.h:335
virtual void setDisabledTextColor(CColor const &iColor)
Attribute disabled-text-color.
Definition: TextButtonView.h:67
Definition: Types.h:29
SharedPointer< CBitmap > BitmapSPtr
Definition: Types.h:50
std::function< void()> OnClickListener
Definition: TextButtonView.h:50
Definition: CustomController.h:24
ParamID getTitleTag() const
Optional id of the parameter whose string representation (IGUIParameter::toUTF8String()) will be used...
Definition: TextButtonView.h:61
void setImageHasDisabledState(bool iValue)
Definition: TextButtonView.h:128
virtual void setDisabledGradient(GradientPtr iGradient)
Attribute disabled-gradient.
Definition: TextButtonView.h:73
void setPrecisionOverride(int32 iPrecisionOverride)
Definition: TextButtonView.h:139
CColor const & getDisabledTextColor() const
Color used to draw the title when no image is provided and the button is disabled.
Definition: TextButtonView.h:64
CBitmap * BitmapPtr
Definition: Types.h:49
bool getImageHasDisabledState() const
Used when drawing the image.
Definition: TextButtonView.h:127
constexpr ParamID UNDEFINED_PARAM_ID
Constant used throughout the code to test whether the ParamID represents a valid id or an undefined o...
Definition: Types.h:47
TextButtonView(const CRect &iSize)
Definition: TextButtonView.h:53
CGradient * GradientPtr
Definition: Types.h:57