iPlug2 - C++ Audio Plug-in Framework
IVNumberBoxControl.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #pragma once
12 
18 #include "IControl.h"
19 
20 BEGIN_IPLUG_NAMESPACE
21 BEGIN_IGRAPHICS_NAMESPACE
22 
27  , public IVectorBase
28 {
29 public:
30  IVNumberBoxControl(const IRECT& bounds, int paramIdx = kNoParameter, IActionFunction actionFunc = nullptr, const char* label = "", const IVStyle& style = DEFAULT_STYLE, double defaultValue = 50.f, double minValue = 1.f, double maxValue = 100.f, const char* fmtStr = "%0.0f")
31  : IControl(bounds, paramIdx, actionFunc)
32  , IVectorBase(style.WithDrawShadows(false)
33  .WithValueText(style.valueText.WithVAlign(EVAlign::Middle)))
34  , mFmtStr(fmtStr)
35  , mMinValue(minValue)
36  , mMaxValue(maxValue)
37  , mRealValue(defaultValue)
38  {
39  assert(defaultValue >= minValue && defaultValue <= maxValue);
40 
41  AttachIControl(this, label);
42  }
43 
44  void OnInit() override
45  {
46  if(GetParam())
47  {
48  mMinValue = GetParam()->GetMin();
49  mMaxValue = GetParam()->GetMax();
50  mRealValue = GetParam()->GetDefault();
51  }
52  }
53 
54  void Draw(IGraphics& g) override
55  {
56  DrawLabel(g);
57 
58  if(mMouseIsOver)
59  g.FillRect(GetColor(kHL), mTextReadout->GetRECT());
60  }
61 
62  void OnResize() override
63  {
64  MakeRects(mRECT, false);
65 
66  if(mIncButton && mDecButton)
67  {
68  IRECT sections = mWidgetBounds;
69  mTextReadout->SetTargetAndDrawRECTs(sections.ReduceFromLeft(sections.W() * 0.75f));
70  sections.Pad(-1.f, 1.f, 0.f, 1.f);
71  mIncButton->SetTargetAndDrawRECTs(sections.FracRectVertical(0.5f, true));
72  mDecButton->SetTargetAndDrawRECTs(sections.FracRectVertical(0.5f, false));
73  SetTargetRECT(mTextReadout->GetRECT());
74  }
75  }
76 
77  void OnAttached() override
78  {
79  IRECT sections = mWidgetBounds;
80  GetUI()->AttachControl(mTextReadout = new IVLabelControl(sections.ReduceFromLeft(sections.W() * 0.75f), "0", mStyle.WithDrawFrame(true)));
81 
82  mTextReadout->SetStrFmt(32, mFmtStr.Get(), mRealValue);
83 
84  sections.Pad(-1.f, 1.f, 0.f, 1.f);
85  GetUI()->AttachControl(mIncButton = new IVButtonControl(sections.FracRectVertical(0.5f, true), SplashClickActionFunc, "+", mStyle))->SetAnimationEndActionFunction(mIncrementFunc);
86  GetUI()->AttachControl(mDecButton = new IVButtonControl(sections.FracRectVertical(0.5f, false), SplashClickActionFunc, "-", mStyle))->SetAnimationEndActionFunction(mDecrementFunc);
87  }
88 
89  void OnMouseDown(float x, float y, const IMouseMod &mod) override
90  {
91  if (mHideCursorOnDrag)
92  GetUI()->HideMouseCursor(true, true);
93 
94  if(GetParam())
96  }
97 
98  void OnMouseUp(float x, float y, const IMouseMod &mod) override
99  {
100  if (mHideCursorOnDrag)
101  GetUI()->HideMouseCursor(false);
102 
103  if(GetParam())
105  }
106 
107  void OnMouseDrag(float x, float y, float dX, float dY, const IMouseMod &mod) override
108  {
109  double gearing = IsFineControl(mod, true) ? mSmallIncrement : mLargeIncrement;
110  mRealValue -= (double(dY) * gearing);
111  OnValueChanged();
112  }
113 
114  void OnMouseDblClick(float x, float y, const IMouseMod &mod) override
115  {
116  if(mTextReadout->GetRECT().Contains(x, y))
117  GetUI()->CreateTextEntry(*this, mText, mTextReadout->GetRECT(), mTextReadout->GetStr());
118  }
119 
120  void OnTextEntryCompletion(const char* str, int valIdx) override
121  {
122  mRealValue = atof(str);
123  OnValueChanged();
124  }
125 
126  void OnMouseWheel(float x, float y, const IMouseMod& mod, float d) override
127  {
128  double gearing = IsFineControl(mod, true) ? mSmallIncrement : mLargeIncrement;
129  double inc = (d > 0.f ? 1. : -1.) * gearing;
130  mRealValue += inc;
131  OnValueChanged();
132  }
133 
134  void SetValueFromDelegate(double value, int valIdx = 0) override
135  {
136  if(GetParam())
137  {
138  mRealValue = GetParam()->FromNormalized(value);
139  OnValueChanged(true);
140  }
141 
142  IControl::SetValueFromDelegate(value, valIdx);
143  }
144 
145  void SetValueFromUserInput(double value, int valIdx = 0) override
146  {
147  if(GetParam())
148  {
149  mRealValue = GetParam()->FromNormalized(value);
150  OnValueChanged(true);
151  }
152 
153  IControl::SetValueFromUserInput(value, valIdx);
154  }
155 
156  void SetStyle(const IVStyle& style) override
157  {
158  IVectorBase::SetStyle(style);
159  mTextReadout->SetStyle(style);
160  mIncButton->SetStyle(style);
161  mDecButton->SetStyle(style);
162  }
163 
164  bool IsFineControl(const IMouseMod& mod, bool wheel) const
165  {
166  #ifdef PROTOOLS
167  #ifdef OS_WIN
168  return mod.C;
169  #else
170  return wheel ? mod.C : mod.R;
171  #endif
172  #else
173  return (mod.C || mod.S);
174  #endif
175  }
176 
177  void OnValueChanged(bool preventAction = false)
178  {
179  mRealValue = Clip(mRealValue, mMinValue, mMaxValue);
180 
181  mTextReadout->SetStrFmt(32, mFmtStr.Get(), mRealValue);
182 
183  if(!preventAction && GetParam())
184  SetValue(GetParam()->ToNormalized(mRealValue));
185 
186  SetDirty(!preventAction);
187  }
188 
189  double GetRealValue() const { return mRealValue; }
190 
191 protected:
192 
193  IActionFunction mIncrementFunc = [this](IControl* pCaller) { mRealValue += mLargeIncrement; OnValueChanged(); };
194  IActionFunction mDecrementFunc = [this](IControl* pCaller) { mRealValue -= mLargeIncrement; OnValueChanged(); };
195  IVLabelControl* mTextReadout = nullptr;
196  IVButtonControl* mIncButton = nullptr;
197  IVButtonControl* mDecButton = nullptr;
198  WDL_String mFmtStr;
199  double mLargeIncrement = 1.f;
200  double mSmallIncrement = 0.1f;
201  double mMinValue;
202  double mMaxValue;
203  double mRealValue = 0.f;
204  bool mHideCursorOnDrag = true;
205 };
206 
207 END_IGRAPHICS_NAMESPACE
208 END_IPLUG_NAMESPACE
bool Contains(const IRECT &rhs) const
Returns true if this IRECT completely contains rhs.
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
virtual void SetValueFromUserInput(double value, int valIdx=0)
Set the control&#39;s value after user input.
Definition: IControl.cpp:172
IGEditorDelegate * GetDelegate()
Gets a pointer to the class implementing the IEditorDelegate interface that handles parameter changes...
Definition: IControl.h:439
void CreateTextEntry(IControl &control, const IText &text, const IRECT &bounds, const char *str="", int valIdx=0)
Create a text entry box.
Definition: IGraphics.cpp:1900
A vector button/momentary switch control.
Definition: IControls.h:50
Used to manage mouse modifiers i.e.
bool mMouseIsOver
if mGraphics::mHandleMouseOver = true, this will be true when the mouse is over control.
Definition: IControl.h:545
const IParam * GetParam(int valIdx=0) const
Get a const pointer to the IParam object (owned by the editor delegate class), associated with this c...
Definition: IControl.cpp:120
virtual void SetValueFromDelegate(double value, int valIdx=0)
Set the control&#39;s value from the delegate This method is called from the class implementing the IEdit...
Definition: IControl.cpp:157
virtual void BeginInformHostOfParamChangeFromUI(int paramIdx)=0
Called by the UI at the beginning of a parameter change gesture, in order to notify the host (via a c...
const char * GetStr() const
Definition: IControl.h:1965
void OnMouseUp(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse up event on this control.
IRECT FracRectVertical(float frac, bool fromTop=false) const
Returns a new IRECT with a height that is multiplied by frac.
void SetValueFromUserInput(double value, int valIdx=0) override
Set the control&#39;s value after user input.
IControl * SetAnimationEndActionFunction(IActionFunction actionFunc)
Set an Action Function to be called at the end of an animation.
Definition: IControl.h:206
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:641
int GetParamIdx(int valIdx=0) const
Get the index of a parameter that the control is linked to Normaly controls are either linked to a si...
Definition: IControl.cpp:107
This file contains the base IControl implementation, along with some base classes for specific types ...
A "meta control" for a number box with an Inc/Dec button It adds several child buttons.
IRECT MakeRects(const IRECT &parent, bool hasHandle=false)
Calculate the rectangles for the various areas, depending on the style.
Definition: IControl.h:1014
double GetMin() const
Returns the parameter&#39;s minimum value.
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set...
double FromNormalized(double normalizedValue) const
Convert a normalized value to real value for this parameter.
const IRECT & GetRECT() const
Get the rectangular draw area for this control, within the graphics context.
Definition: IControl.h:305
double GetMax() const
Returns the parameter&#39;s maximum value.
float W() const
void SetValueFromDelegate(double value, int valIdx=0) override
Set the control&#39;s value from the delegate This method is called from the class implementing the IEdit...
A vector label control that can display text with a shadow.
Definition: IControls.h:41
void Draw(IGraphics &g) override
Draw the control to the graphics context.
const IColor & GetColor(EVColor color) const
Get value of a specific EVColor in the IVControl.
Definition: IControl.h:664
void OnMouseWheel(float x, float y, const IMouseMod &mod, float d) override
Implement this method to respond to a mouse wheel event on this control.
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
void OnMouseDrag(float x, float y, float dX, float dY, const IMouseMod &mod) override
Implement this method to respond to a mouse drag event on this control.
double GetDefault(bool normalized=false) const
Returns the parameter&#39;s default value.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
IVectorBase(const IVStyle &style, bool labelInWidget=false, bool valueInWidget=false)
IVectorBase Constructor.
Definition: IControl.h:631
virtual void DrawLabel(IGraphics &g)
Draw the IVControl label text.
Definition: IControl.h:746
IRECT ReduceFromLeft(float amount)
Reduce in width from the left edge by &#39;amount&#39; and return the removed region.
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2547
virtual void EndInformHostOfParamChangeFromUI(int paramIdx)=0
Called by the user interface at the end of a parameter change gesture, in order to notify the host (v...
BEGIN_IPLUG_NAMESPACE T Clip(T x, T lo, T hi)
Clips the value x between lo and hi.
void SetTargetAndDrawRECTs(const IRECT &bounds)
Set BOTH the draw rect and the target area, within the graphics context for this control.
Definition: IControl.h:321
virtual void HideMouseCursor(bool hide=true, bool lock=true)=0
Call to hide/show the mouse cursor.
void OnTextEntryCompletion(const char *str, int valIdx) override
Implement this method to handle text input after IGraphics::CreateTextEntry/IControl::PromptUserInput...
void SplashClickActionFunc(IControl *pCaller)
The splash click action function is used by IVControls to start SplashAnimationFunc.
Definition: IControl.cpp:47
void Pad(float padding)
Pad this IRECT N.B.
double ToNormalized(double nonNormalizedValue) const
Convert a real value to normalized value for this parameter.
void SetTargetRECT(const IRECT &bounds)
Set the rectangular mouse tracking target area, within the graphics context for this control...
Definition: IControl.h:317
IGraphics * GetUI()
Definition: IControl.h:452
void OnMouseDblClick(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse double click event on this control.
virtual void SetStyle(const IVStyle &style)
Set the Style of this IVControl.
Definition: IControl.h:687
IControl * AttachControl(IControl *pControl, int ctrlTag=kNoTag, const char *group="")
Attach an IControl to the graphics context and add it to the top of the control stack.
Definition: IGraphics.cpp:298
virtual void SetStrFmt(int maxlen, const char *fmt,...)
Set the text to display, using a printf-like format string.
Definition: IControl.cpp:467
void OnResize() override
Called when IControl is constructed or resized using SetRect().
virtual void SetValue(double value, int valIdx=0)
Set one of the control&#39;s values.
Definition: IControl.cpp:145
A base interface to be combined with IControl for vectorial controls "IVControls", in order for them to share a common style If you need more flexibility, you&#39;re on your own!
Definition: IControl.h:624
void SetStyle(const IVStyle &style) override
Set the Style of this IVControl.
void OnInit() override
Called just prior to when the control is attached, after its delegate and graphics member variable se...
A struct encapsulating a set of properties used to configure IVControls.
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:196