-
Notifications
You must be signed in to change notification settings - Fork 13
/
LabeledKnob.h
172 lines (159 loc) · 5.3 KB
/
LabeledKnob.h
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
//
// LabeledKnob.h
// GuitarAmp
//
// Created by Andreas Pohl on 20/10/16.
//
//
#ifndef LabeledKnob_h
#define LabeledKnob_h
#include <sstream>
#include <string>
#include "IControl.h"
class LabeledKnob : public IKnobMultiControl {
public:
enum class LabelPosition { RIGHT, LEFT, BOTTOM, TOP_BOTTOM };
LabeledKnob(IPlugBase* pPlug, IRECT rect, int paramIdx, IBitmap* pImg, LabelPosition lpos, IText& textTitle,
IText& textValue, char* name, char* unit, bool showValue)
: IKnobMultiControl(pPlug, rect.L, rect.T, paramIdx, pImg),
mLabelPos(lpos),
mTextTitle(textTitle),
mTextValue(textValue),
mName(name),
mUnit(unit),
mShowValue(showValue) {
int x = rect.L, y = rect.T, nameX = 0, nameY = 0, valX = 0, valY = -1, width = 0;
// TODO: this stuff should by more dynamic...
switch (mLabelPos) {
case LabelPosition::RIGHT:
nameX = valX = x + pImg->W + 5;
nameY = y + (pImg->H / pImg->N / 2 - (mTextTitle.mSize + 1));
width = 45;
mTextTitle.mAlign = mTextValue.mAlign = IText::kAlignNear;
break;
case LabelPosition::LEFT:
nameX = valX = x;
x += 50;
nameY = y + (pImg->H / pImg->N / 2 - (mTextTitle.mSize + 1));
width = 45;
mTextTitle.mAlign = mTextValue.mAlign = IText::kAlignFar;
break;
case LabelPosition::BOTTOM:
nameX = valX = x;
nameY = y + pImg->H / pImg->N;
width = pImg->W;
mTextTitle.mAlign = mTextValue.mAlign = IText::kAlignCenter;
break;
case LabelPosition::TOP_BOTTOM:
nameX = valX = rect.L;
nameY = rect.T;
x = rect.L + rect.W() / 2 - pImg->W / 2;
y += 15;
valY = y + pImg->H / pImg->N;
width = rect.W();
mTextTitle.mAlign = mTextValue.mAlign = IText::kAlignCenter;
break;
}
if (valY == -1) {
if (*name == 0) {
valY = nameY;
} else {
valY = nameY + mTextTitle.mSize + 6;
}
}
mRECT = IRECT(x, y, pImg);
mTargetRECT = mRECT;
mNameR = IRECT(nameX, nameY, nameX + width, nameY + mTextTitle.mSize + 1);
mValR = IRECT(valX, valY, valX + width, valY + mTextTitle.mSize + 6);
}
virtual void SetValueFromPlug(double value) {
IControl::SetValueFromPlug(value);
UpdateValueString();
}
virtual void SetValueFromUserInput(double value) {
IControl::SetValueFromUserInput(value);
UpdateValueString();
}
virtual void OnMouseDrag(int x, int y, int dX, int dY, IMouseMod* pMod) {
IKnobControl::OnMouseDrag(x, y, dX, dY, pMod);
UpdateValueString();
}
virtual void OnMouseWheel(int x, int y, IMouseMod* pMod, int d) {
IKnobControl::OnMouseWheel(x, y, pMod, d);
UpdateValueString();
}
virtual bool Draw(IGraphics* pGraphics) {
if (*mName != 0) {
pGraphics->DrawIText(&mTextTitle, mName, &mNameR);
}
if (mShowValue) {
pGraphics->DrawIText(&mTextValue, const_cast<char*>(mValStr.c_str()), &mValR);
}
return IKnobMultiControl::Draw(pGraphics);
}
private:
LabelPosition mLabelPos;
IText mTextTitle, mTextValue;
char* mName;
char* mUnit;
IRECT mNameR, mValR;
std::string mValStr;
bool mShowValue;
void UpdateValueString() {
if (mShowValue) {
int precision = 3;
auto val = GetParam()->Value();
std::string unit = mUnit;
if (unit == "Hz" && val >= 1000.) {
val /= 1000;
unit = "KHz";
precision = 2;
} else if (unit == "Ohm") {
precision = 0;
if (val >= 1000.) {
val /= 1000;
unit = "KOhm";
}
if (val >= 1000.) {
val /= 1000;
unit = "MOhm";
}
} else if (unit == "F") {
precision = 0;
if (val <= 1) {
val *= 1000;
unit = "mF";
}
if (val <= 1) {
val *= 1000;
unit = "µF";
}
if (val <= 1) {
val *= 1000;
unit = "nF";
}
if (val <= 1) {
val *= 1000;
unit = "pF";
}
} else if (unit == "V") {
precision = 1;
if (val < 1) {
val *= 1000;
unit = "mV";
}
} else {
precision = 1;
double x = 0.1;
while (x > 0.00000001 && val < x) {
precision++;
x /= 10;
}
}
std::ostringstream s;
s << std::setprecision(precision) << std::fixed << val << unit;
mValStr = s.str();
}
}
};
#endif /* LabeledKnob_h */