iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
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
20BEGIN_IPLUG_NAMESPACE
21BEGIN_IGRAPHICS_NAMESPACE
22
27 , public IVectorBase
28{
29public:
30 IVNumberBoxControl(const IRECT& bounds, int paramIdx = kNoParameter, IActionFunction actionFunc = nullptr, const char* label = "", const IVStyle& style = DEFAULT_STYLE, bool buttons = false, double defaultValue = 50.f, double minValue = 1.f, double maxValue = 100.f, const char* fmtStr = "%0.0f", bool drawTriangle = true)
31 : IContainerBase(bounds, paramIdx, actionFunc)
32 , IVectorBase(style.WithDrawShadows(false)
33 .WithDrawFrame(true)
34 .WithValueText(style.valueText.WithVAlign(EVAlign::Middle))
35 .WithLabelText(style.labelText.WithVAlign(EVAlign::Middle)))
36 , mFmtStr(fmtStr)
37 , mButtons(buttons)
38 , mMinValue(minValue)
39 , mMaxValue(maxValue)
40 , mRealValue(defaultValue)
41 , mDrawTriangle(drawTriangle)
42 {
43 assert(defaultValue >= minValue && defaultValue <= maxValue);
44
45 AttachIControl(this, label);
46 mAccessibleRole = EAccessibleRole::Slider;
47 }
48
50 {
51 if (mAccessibleRole == EAccessibleRole::None)
52 return;
53
54 nodes.emplace_back();
55 IAccessibleNode& node = nodes.back();
56 InitAccessibleNode(node, mAccessibleRole);
57 node.hasNumericValue = true;
58 node.min = mMinValue;
59 node.max = mMaxValue;
60 node.value = mRealValue;
61 node.step = mLargeIncrement;
62
63 if (mTextReadout)
64 node.valueText.Set(mTextReadout->GetStr());
65 }
66
67 bool OnAccessibleAction(EAccessibleAction action, int nodeIdx = 0, double value = 1.) override
68 {
69 if (IsDisabled())
70 return false;
71
72 const int nSteps = std::max(1, static_cast<int>(std::round(value)));
73
74 switch (action)
75 {
76 case EAccessibleAction::Increment: mRealValue += nSteps * mLargeIncrement; break;
77 case EAccessibleAction::Decrement: mRealValue -= nSteps * mLargeIncrement; break;
78 case EAccessibleAction::SetValue: mRealValue = value; break;
79 default: return false;
80 }
81
82 if (GetParam())
84
85 OnValueChanged();
86
87 if (GetParam())
89
90 return true;
91 }
92
93 void OnInit() override
94 {
95 if (GetParam())
96 {
97 mMinValue = GetParam()->GetMin();
98 mMaxValue = GetParam()->GetMax();
99 mRealValue = GetParam()->GetDefault();
100 }
101 }
102
103 void Draw(IGraphics& g) override
104 {
105 DrawLabel(g);
106
107 if (mMouseIsOver)
108 g.FillRect(GetColor(kHL), mTextReadout->GetRECT());
109
110 if (mMouseIsDown)
111 g.FillRect(GetColor(kFG), mTextReadout->GetRECT());
112
113 if (mDrawTriangle)
114 {
115 auto triangleRect = mTextReadout->GetRECT().GetPadded(-2.0f);
116
117 g.FillTriangle(GetColor(mMouseIsOver ? kX1 : kSH), triangleRect.L, triangleRect.T, triangleRect.L + triangleRect.H(), triangleRect.MH(), triangleRect.L, triangleRect.B);
118 }
119 }
120
121 void OnResize() override
122 {
123 MakeRects(mRECT, false);
124
125 IRECT sections = mWidgetBounds;
126 sections.Pad(-1.f);
127
128 if (mTextReadout)
129 {
130 mTextReadout->SetTargetAndDrawRECTs(sections.ReduceFromLeft(sections.W() * (mButtons ? 0.75f : 1.f)));
131
132 if (mButtons)
133 {
134 mIncButton->SetTargetAndDrawRECTs(sections.FracRectVertical(0.5f, true).GetPadded(-2.f, 0.f, 0.f, -1.f));
135 mDecButton->SetTargetAndDrawRECTs(sections.FracRectVertical(0.5f, false).GetPadded(-2.f, -1.f, 0.f, 0.f));
136 }
137
138 SetTargetRECT(mTextReadout->GetRECT());
139 }
140 }
141
142 void OnAttached() override
143 {
144 IRECT sections = mWidgetBounds;
145 sections.Pad(-1.f);
146
147 AddChildControl(mTextReadout = new IVLabelControl(sections.ReduceFromLeft(sections.W() * (mButtons ? 0.75f : 1.f)), "0", mStyle.WithDrawFrame(true)));
148
149 mTextReadout->SetStrFmt(32, mFmtStr.Get(), mRealValue);
150 mTextReadout->SetAccessibleRole(EAccessibleRole::None); // the number box itself is exposed as a slider
151
152 if (mButtons)
153 {
154 AddChildControl(mIncButton = new IVButtonControl(sections.FracRectVertical(0.5f, true).GetPadded(-2.f, 0.f, 0.f, -1.f), SplashClickActionFunc, "+", mStyle))->SetAnimationEndActionFunction(mIncrementFunc);
155 AddChildControl(mDecButton = new IVButtonControl(sections.FracRectVertical(0.5f, false).GetPadded(-2.f, -1.f, 0.f, 0.f), SplashClickActionFunc, "-", mStyle))->SetAnimationEndActionFunction(mDecrementFunc);
156 mIncButton->SetAccessibleRole(EAccessibleRole::None);
157 mDecButton->SetAccessibleRole(EAccessibleRole::None);
158 }
159 }
160
161 void OnMouseDown(float x, float y, const IMouseMod &mod) override
162 {
163 if (mHideCursorOnDrag)
164 GetUI()->HideMouseCursor(true, true);
165
166 mMouseIsDown = true;
167 }
168
169 void OnMouseUp(float x, float y, const IMouseMod &mod) override
170 {
171 if (mHideCursorOnDrag)
172 GetUI()->HideMouseCursor(false);
173
174 mMouseIsDown = false;
175
176 SetDirty(true);
177 }
178
179 void OnMouseDrag(float x, float y, float dX, float dY, const IMouseMod &mod) override
180 {
181 double gearing = IsFineControl(mod, true) ? mSmallIncrement : mLargeIncrement;
182 mRealValue -= (double(dY) * gearing);
183 OnValueChanged();
184 }
185
186 void OnMouseDblClick(float x, float y, const IMouseMod &mod) override
187 {
188 if (!IsDisabled() && mTextReadout->GetRECT().Contains(x, y))
189 GetUI()->CreateTextEntry(*this, mText, mTextReadout->GetRECT(), mTextReadout->GetStr());
190 }
191
192 void OnTextEntryCompletion(const char* str, int valIdx) override
193 {
194 mRealValue = atof(str);
195 OnValueChanged();
196 }
197
198 void OnMouseWheel(float x, float y, const IMouseMod& mod, float d) override
199 {
200 double gearing = IsFineControl(mod, true) ? mSmallIncrement : mLargeIncrement;
201 double inc = (d > 0.f ? 1. : -1.) * gearing;
202 mRealValue += inc;
203 OnValueChanged();
204 }
205
206 void SetValueFromDelegate(double value, int valIdx = 0) override
207 {
208 if (GetParam())
209 {
210 mRealValue = GetParam()->FromNormalized(value);
211 OnValueChanged(true);
212 }
213
214 IControl::SetValueFromDelegate(value, valIdx);
215 }
216
217 void SetValueFromUserInput(double value, int valIdx = 0) override
218 {
219 if (GetParam())
220 {
221 mRealValue = GetParam()->FromNormalized(value);
222 OnValueChanged(true);
223 }
224
225 IControl::SetValueFromUserInput(value, valIdx);
226 }
227
228 void SetStyle(const IVStyle& style) override
229 {
231 mTextReadout->SetStyle(style);
232
233 if (mButtons)
234 {
235 mIncButton->SetStyle(style);
236 mDecButton->SetStyle(style);
237 }
238 }
239
240 bool IsFineControl(const IMouseMod& mod, bool wheel) const
241 {
242 #ifdef PROTOOLS
243 #ifdef OS_WIN
244 return mod.C;
245 #else
246 return wheel ? mod.C : mod.R;
247 #endif
248 #else
249 return (mod.C || mod.S);
250 #endif
251 }
252
253 void OnValueChanged(bool preventAction = false)
254 {
255 mRealValue = Clip(mRealValue, mMinValue, mMaxValue);
256
257 mTextReadout->SetStrFmt(32, mFmtStr.Get(), mRealValue);
258
259 if (!preventAction && GetParam())
260 SetValue(GetParam()->ToNormalized(mRealValue));
261
262 SetDirty(!preventAction);
263 }
264
265 double GetRealValue() const { return mRealValue; }
266
267 void SetDrawTriangle(bool draw) { mDrawTriangle = draw; SetDirty(false); }
268
269protected:
270
271 IActionFunction mIncrementFunc = [this](IControl* pCaller) { mRealValue += mLargeIncrement; OnValueChanged(); };
272 IActionFunction mDecrementFunc = [this](IControl* pCaller) { mRealValue -= mLargeIncrement; OnValueChanged(); };
273 IVLabelControl* mTextReadout = nullptr;
274 IVButtonControl* mIncButton = nullptr;
275 IVButtonControl* mDecButton = nullptr;
276 WDL_String mFmtStr;
277 double mLargeIncrement = 1.f;
278 double mSmallIncrement = 0.1f;
279 double mMinValue;
280 double mMaxValue;
281 double mRealValue = 0.f;
282 bool mHideCursorOnDrag = true;
283 bool mButtons = false;
284 bool mDrawTriangle = true;
285 bool mMouseIsDown = false;
286};
287
288END_IGRAPHICS_NAMESPACE
289END_IPLUG_NAMESPACE
This file contains the base IControl implementation, along with some base classes for specific types ...
IContainerBase allows a control to nest sub controls and it clips the drawing of those subcontrols In...
Definition: IControl.h:662
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
IGraphics * GetUI()
Definition: IControl.h:472
bool mMouseIsOver
if mGraphics::mHandleMouseOver = true, this will be true when the mouse is over control.
Definition: IControl.h:597
virtual void SetValueFromDelegate(double value, int valIdx=0)
Set the control's value from the delegate This method is called from the class implementing the IEdit...
Definition: IControl.cpp:159
bool IsDisabled() const
Definition: IControl.h:367
virtual void SetValueFromUserInput(double value, int valIdx=0)
Set the control's value after user input.
Definition: IControl.cpp:174
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:109
const IRECT & GetRECT() const
Get the rectangular draw area for this control, within the graphics context.
Definition: IControl.h:316
void InitAccessibleNode(IAccessibleNode &node, EAccessibleRole role, int valIdx=0)
Helper for GetAccessibleNodes().
Definition: IControl.cpp:225
void SetTargetRECT(const IRECT &bounds)
Set the rectangular mouse tracking target area, within the graphics context for this control.
Definition: IControl.h:328
IGEditorDelegate * GetDelegate()
Gets a pointer to the class implementing the IEditorDelegate interface that handles parameter changes...
Definition: IControl.h:454
IControl * SetAnimationEndActionFunction(IActionFunction actionFunc)
Set an Action Function to be called at the end of an animation.
Definition: IControl.h:216
IControl * SetAccessibleRole(EAccessibleRole role)
Set the role that the control reports to assistive technology.
Definition: IControl.h:539
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:122
virtual void SetValue(double value, int valIdx=0)
Set one of the control's values.
Definition: IControl.cpp:147
void SetTargetAndDrawRECTs(const IRECT &bounds)
Set BOTH the draw rect and the target area, within the graphics context for this control.
Definition: IControl.h:332
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:198
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...
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...
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:87
void CreateTextEntry(IControl &control, const IText &text, const IRECT &bounds, const char *str="", int valIdx=0)
Create a text entry box.
Definition: IGraphics.cpp:2279
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:2929
virtual void FillTriangle(const IColor &color, float x1, float y1, float x2, float y2, float x3, float y3, const IBlend *pBlend=0)
Fill a triangle with a color.
Definition: IGraphics.cpp:2922
virtual void HideMouseCursor(bool hide=true, bool lock=true)=0
Call to hide/show the mouse cursor.
double GetDefault(bool normalized=false) const
Returns the parameter's default value.
double GetMin() const
Returns the parameter's minimum value.
double FromNormalized(double normalizedValue) const
Convert a normalized value to real value for this parameter.
double GetMax() const
Returns the parameter's maximum value.
virtual void SetStrFmt(int maxlen, const char *fmt,...)
Set the text to display, using a printf-like format string.
Definition: IControl.cpp:657
const char * GetStr() const
Definition: IControl.h:2267
A vector button/momentary switch control.
Definition: IControls.h:54
A vector label control that can display text with a shadow.
Definition: IControls.h:45
A "meta control" for a number box with an Inc/Dec button It adds several child buttons if buttons = t...
void OnMouseUp(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse up event on this control.
void SetValueFromDelegate(double value, int valIdx=0) override
Set the control's value from the delegate This method is called from the class implementing the IEdit...
void OnResize() override
Called when IControl is constructed or resized using SetRect().
void OnInit() override
Called just prior to when the control is attached, after its delegate and graphics member variable se...
void SetValueFromUserInput(double value, int valIdx=0) override
Set the control's value after user input.
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.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
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.
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
bool OnAccessibleAction(EAccessibleAction action, int nodeIdx=0, double value=1.) override
Perform an action requested by assistive technology or keyboard navigation.
void SetStyle(const IVStyle &style) override
Set the Style of this IVControl.
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set.
void GetAccessibleNodes(IAccessibleNodeList &nodes) override
Describe the control for assistive technology (e.g.
void OnTextEntryCompletion(const char *str, int valIdx) override
Implement this method to handle text input after IGraphics::CreateTextEntry/IControlPromptUserInput.
void OnMouseDblClick(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse double click event on this control.
A base interface to be combined with IControl for vectorial controls "IVControls",...
Definition: IControl.h:813
IRECT MakeRects(const IRECT &parent, bool hasHandle=false)
Calculate the rectangles for the various areas, depending on the style.
Definition: IControl.h:1215
virtual void SetStyle(const IVStyle &style)
Set the Style of this IVControl.
Definition: IControl.h:882
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:831
virtual void DrawLabel(IGraphics &g)
Draw the IVControl label text.
Definition: IControl.h:946
const IColor & GetColor(EVColor color) const
Get value of a specific EVColor in the IVControl.
Definition: IControl.h:857
std::vector< IAccessibleNode > IAccessibleNodeList
A list of IAccessibleNode.
void SplashClickActionFunc(IControl *pCaller)
The splash click action function is used by IVControls to start SplashAnimationFunc.
Definition: IControl.cpp:47
EAccessibleAction
An action that can be performed on a control via assistive technology or keyboard navigation.
BEGIN_IPLUG_NAMESPACE T Clip(T x, T lo, T hi)
Clips the value x between lo and hi.
Used to describe a control (or a part of a control) to assistive technology.
Used to manage mouse modifiers i.e.
Used to manage a rectangular area, independent of draw class/platform.
IRECT ReduceFromLeft(float amount)
Reduce in width from the left edge by 'amount' and return the removed region.
void Pad(float padding)
Pad this IRECT N.B.
IRECT FracRectVertical(float frac, bool fromTop=false) const
Returns a new IRECT with a height that is multiplied by frac.
float W() const
bool Contains(const IRECT &rhs) const
Returns true if this IRECT completely contains rhs.
IRECT GetPadded(float padding) const
Get a copy of this IRECT with each value padded by padding N.B.
A struct encapsulating a set of properties used to configure IVControls.