-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnobLineCoronaControl.cpp
194 lines (169 loc) · 4.67 KB
/
KnobLineCoronaControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "KnobLineCoronaControl.h"
#include "MidiMapper.h"
static const double kRadToDeg = 180.0 / PI;
#pragma region KnobLineCoronaControl
KnobLineCoronaControl::KnobLineCoronaControl(IRECT pR, int paramIdx,
const IColor& color, const IColor& coronaColor,
float lineThickness,
double innerRadius, double outerRadius,
double minAngle, double maxAngle,
EDirection direction, double gearing)
: IKnobControlBase(pR, paramIdx, direction, gearing)
, mCX(mRECT.MW())
, mCY(mRECT.MH())
, mInnerRadius(innerRadius)
, mOuterRadius(outerRadius)
, mMinAngle(minAngle)
, mMaxAngle(maxAngle)
, mColor(color)
, mCoronaColor(coronaColor)
, mLineThickness(lineThickness)
, mLabelControl(nullptr)
, mSharedLabel(false)
, mHasMouse(false)
{
if (mOuterRadius == 0.0)
{
mOuterRadius = 0.5 * pR.W();
}
}
void KnobLineCoronaControl::Draw(IGraphics& pGraphics)
{
float v = mMinAngle + (float)GetValue() * (mMaxAngle - mMinAngle);
pGraphics.DrawArc(mCoronaColor, mCX, mCY, mOuterRadius, mMinAngle*kRadToDeg, v*kRadToDeg, 0, mLineThickness);
IColor color(mCoronaColor.A, mCoronaColor.R / 2, mCoronaColor.G / 2, mCoronaColor.B / 2);
pGraphics.DrawArc(color, mCX, mCY, mOuterRadius, v*kRadToDeg, mMaxAngle*kRadToDeg, 0, mLineThickness);
float sinV = (float)sin(v);
float cosV = (float)cos(v);
float x1 = mCX + mInnerRadius * sinV, y1 = mCY - mInnerRadius * cosV;
float x2 = mCX + mOuterRadius * sinV, y2 = mCY - mOuterRadius * cosV;
return pGraphics.DrawLine(mColor, x1, y1, x2, y2, 0, mLineThickness);
}
void KnobLineCoronaControl::OnMouseDown(float x, float y, const IMouseMod& pMod)
{
mHasMouse = true;
}
void KnobLineCoronaControl::OnMouseDrag(float x, float y, float dX, float dY, const IMouseMod& pMod)
{
double gearing = mGearing;
#ifdef PROTOOLS
#ifdef OS_WIN
if (pMod->C) gearing *= 10.0;
#else
if (pMod->R) gearing *= 10.0;
#endif
#else
if (pMod.C || pMod.S) gearing *= 10.0;
#endif
float value = GetValue();
value += (double)dY / (double)(mRECT.T - mRECT.B) / gearing;
value += (double)dX / (double)(mRECT.R - mRECT.L) / gearing;
SetValue(value);
SetDirty();
}
void KnobLineCoronaControl::OnMouseUp(float x, float y, const IMouseMod& pMod)
{
if (!mRECT.Contains(x, y))
{
HideLabel();
}
mHasMouse = false;
}
void KnobLineCoronaControl::OnMouseOver(float x, float y, const IMouseMod& pMod)
{
ShowLabel();
}
void KnobLineCoronaControl::OnMouseOut()
{
if (!mHasMouse)
{
HideLabel();
}
}
void KnobLineCoronaControl::ShowLabel()
{
if (mLabelControl != nullptr)
{
// if our label was hidden when we attached it,
// that means we should reposition it below the knob before displaying it.
if (mSharedLabel)
{
IRECT targetRect = mRECT;
targetRect.T = targetRect.B - 16;
IRECT labelRect = mLabelControl->GetRECT();
labelRect = targetRect;
mLabelControl->SetTargetRECT(targetRect);
}
UpdateLabel();
}
}
void KnobLineCoronaControl::HideLabel()
{
if (mLabelControl != nullptr)
{
mLabelControl->SetStr(mLabelString.Get());
SetDirty(false);
}
}
void KnobLineCoronaControl::UpdateLabel()
{
if (mLabelControl != nullptr)
{
const IParam* pParam = GetParam();
if (pParam)
{
WDL_String str;
pParam->GetDisplayForHost(str);
str.Append(" ");
str.Append(pParam->GetLabelForHost());
mLabelControl->SetStr(str.Get());
}
}
}
void KnobLineCoronaControl::SetLabelControl(ITextControl* control, const char * label, bool bShared)
{
mLabelControl = control;
mSharedLabel = bShared;
if (mLabelControl != nullptr)
{
mLabelString.Set(label);
}
else
{
mLabelString.Set("");
mSharedLabel = false;
}
// if our label control is shared, extend our rect to include where we will place the label when showing it.
// this ensures that the area the label draws in will be cleared when the labels is moved elsewhere.
if (mSharedLabel)
{
mRECT.L -= 15;
mRECT.R += 15;
mRECT.B += 15;
}
}
void KnobLineCoronaControl::CreateContextMenu(IPopupMenu& contextMenu)
{
MidiMapper* control = dynamic_cast<MidiMapper*>(GetUI()->GetControlWithTag(kMidiMapper));
if (control != nullptr)
{
control->CreateContextMenu(contextMenu, GetParamIdx());
}
}
void KnobLineCoronaControl::OnContextSelection(int itemSelected)
{
MidiMapper* control = dynamic_cast<MidiMapper*>(GetUI()->GetControlWithTag(kMidiMapper));
if (control != nullptr)
{
control->OnContextSelection(itemSelected);
}
}
void KnobLineCoronaControl::SetDirty(bool triggerAction /*= true*/, int valIdx)
{
IKnobControlBase::SetDirty(triggerAction, valIdx);
if (triggerAction && mHasMouse)
{
UpdateLabel();
}
}
#pragma endregion KnobLineCoronaControl