/
Switches.cpp
140 lines (120 loc) · 3.61 KB
/
Switches.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
#include "../Ahornberg.hpp"
void toggleParamValue(Param& param) {
param.getValue() ? param.setValue(0) : param.setValue(1);
}
float rescaleInput(Input& port, int channel) {
return rescale(port.getVoltage(channel), 0.1, 2, 0, 1);
}
const std::vector<std::string> BasicSwitch::ON_OFF_NAMES = {
"Off",
"On"
};
BasicSwitch::BasicSwitch() {
momentary = false;
shadow->opacity = 0;
hasContextMenu = false;
}
void BasicSwitch::addFrame(const std::string& filename) {
SvgSwitch::addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, filename)));
}
void BasicSwitch::onButton(const ButtonEvent& e) {
if (hasContextMenu || (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_LEFT && (e.mods & RACK_MOD_MASK) == 0)) {
SvgSwitch::onButton(e);
}
}
BasicRoundSwitch::BasicRoundSwitch() {
hasContextMenu = true;
}
void BasicRoundSwitch::onHover(const event::Hover& e) {
math::Vec c = box.size.div(2);
float dist = e.pos.minus(c).norm();
if (dist <= c.x) {
SvgSwitch::onHover(e);
}
}
void BasicRoundSwitch::onButton(const event::Button& e) {
math::Vec c = box.size.div(2);
float dist = e.pos.minus(c).norm();
if (dist <= c.x) {
BasicSwitch::onButton(e);
}
}
RoundSwitch::RoundSwitch() {
addFrame("res/switches/Round_off.svg");
addFrame("res/switches/Round_on.svg");
}
RoundSwitchMedium::RoundSwitchMedium() {
addFrame("res/switches/RoundMedium_off.svg");
addFrame("res/switches/RoundMedium_on.svg");
}
RoundSwitchRed::RoundSwitchRed() {
addFrame("res/switches/Round_off.svg");
addFrame("res/switches/Round_red.svg");
}
RoundSwitchMediumRed::RoundSwitchMediumRed() {
addFrame("res/switches/RoundMedium_off.svg");
addFrame("res/switches/RoundMedium_red.svg");
}
RoundSwitchLinearJump::RoundSwitchLinearJump() {
addFrame("res/switches/Round_linear.svg");
addFrame("res/switches/Round_linear_jump.svg");
}
RoundSwitchLarge::RoundSwitchLarge() {
hasContextMenu = true;
addFrame("res/switches/RoundLarge_off.svg");
addFrame("res/switches/RoundLarge_on.svg");
}
PauseSwitch::PauseSwitch() {
addFrame("res/switches/Pause_off.svg");
addFrame("res/switches/Pause_on.svg");
}
MidiButtonSmall::MidiButtonSmall() {
addFrame(APP->window->loadSvg(asset::plugin(pluginInstance,"res/switches/MidiButtonSmall.svg")));
shadow->opacity = 0.0;
}
struct MidiDriverValueItem : ui::MenuItem {
midi::Port* port;
int driverId;
void onAction(const ActionEvent& e) override {
port->setDriverId(driverId);
}
};
struct MidiDeviceValueItem : ui::MenuItem {
midi::Port* port;
int deviceId;
void onAction(const ActionEvent& e) override {
port->setDeviceId(deviceId);
}
};
void MidiButtonSmall::onAction(const ActionEvent& e) {
ui::Menu* menu = createMenu();
menu->addChild(createMenuLabel("MIDI driver"));
if (port) {
for (int driverId : midi::getDriverIds()) {
MidiDriverValueItem* item = new MidiDriverValueItem;
item->port = port;
item->driverId = driverId;
item->text = midi::getDriver(driverId)->getName();
item->rightText = CHECKMARK(item->driverId == port->getDriverId());
menu->addChild(item);
}
}
menu->addChild(new ui::MenuSeparator);
menu->addChild(createMenuLabel("MIDI device"));
if (port) {
MidiDeviceValueItem* item = new MidiDeviceValueItem;
item->port = port;
item->deviceId = -1;
item->text = "(No device)";
item->rightText = CHECKMARK(item->deviceId == port->getDeviceId());
menu->addChild(item);
for (int deviceId : port->getDeviceIds()) {
MidiDeviceValueItem* item = new MidiDeviceValueItem;
item->port = port;
item->deviceId = deviceId;
item->text = port->getDeviceName(deviceId);
item->rightText = CHECKMARK(item->deviceId == port->getDeviceId());
menu->addChild(item);
}
}
}