-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmath.cpp
More file actions
151 lines (140 loc) · 4.61 KB
/
Copy pathmath.cpp
File metadata and controls
151 lines (140 loc) · 4.61 KB
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
#include "plugin.hpp"
#include "widgets/PanelBackground.hpp"
#include "widgets/InverterWidget.hpp"
struct Math : Module
{
enum ParamId
{
PARAMS_LEN
};
enum InputId
{
A_INPUT,
B_INPUT,
INPUTS_LEN
};
enum OutputId
{
ADD_OUTPUT,
SUB_OUTPUT,
MULT_OUTPUT,
DIV_OUTPUT,
MOD_OUTPUT,
AVG_OUTPUT,
OUTPUTS_LEN
};
enum LightId
{
LIGHTS_LEN
};
Math()
{
config(PARAMS_LEN, INPUTS_LEN, OUTPUTS_LEN, LIGHTS_LEN);
configInput(A_INPUT, "");
configInput(B_INPUT, "");
configOutput(ADD_OUTPUT, "");
configOutput(SUB_OUTPUT, "");
configOutput(MULT_OUTPUT, "");
configOutput(DIV_OUTPUT, "");
configOutput(MOD_OUTPUT, "");
configOutput(AVG_OUTPUT, "");
if (use_global_contrast[MATH])
{
module_contrast[MATH] = global_contrast;
}
}
void process(const ProcessArgs &args) override
{
int a_channels = inputs[A_INPUT].getChannels();
int b_channels = inputs[B_INPUT].getChannels();
int channels = std::max(a_channels, b_channels);
for (int o = 0; o < OUTPUTS_LEN; o++)
{
outputs[o].setChannels(channels);
}
for (int c = 0; c < channels; c++)
{
float a = inputs[A_INPUT].getVoltage(c);
float b = inputs[B_INPUT].getVoltage(c);
outputs[ADD_OUTPUT].setVoltage(clamp(a + b, -10.0f, 10.0f), c);
outputs[SUB_OUTPUT].setVoltage(clamp(a - b, -10.0f, 10.0f), c);
outputs[MULT_OUTPUT].setVoltage(clamp(a * b, -10.0f, 10.0f), c);
outputs[DIV_OUTPUT].setVoltage(clamp(a / b, -10.0f, 10.0f), c);
outputs[MOD_OUTPUT].setVoltage(clamp(fmod(a, b), -10.0f, 10.0f), c);
outputs[AVG_OUTPUT].setVoltage(clamp((a + b) / 2.0f, -10.0f, 10.0f), c);
}
}
};
struct MathWidget : ModuleWidget
{
PanelBackground *panelBackground = new PanelBackground();
SvgPanel *svgPanel;
Inverter *inverter = new Inverter();
MathWidget(Math *module)
{
setModule(module);
svgPanel = createPanel(asset::plugin(pluginInstance, "res/math.svg"));
setPanel(svgPanel);
panelBackground->box.size = svgPanel->box.size;
svgPanel->fb->addChildBottom(panelBackground);
inverter->box.pos = Vec(0.f, 0.f);
inverter->box.size = Vec(box.size.x, box.size.y);
addChild(inverter);
addInput(createInputCentered<BitPort>(mm2px(Vec(10.599, 24.981)), module, Math::A_INPUT));
addInput(createInputCentered<BitPort>(mm2px(Vec(10.599, 36.724)), module, Math::B_INPUT));
addOutput(createOutputCentered<BitPort>(mm2px(Vec(8.285, 51.547)), module, Math::ADD_OUTPUT));
addOutput(createOutputCentered<BitPort>(mm2px(Vec(8.285, 62.079)), module, Math::SUB_OUTPUT));
addOutput(createOutputCentered<BitPort>(mm2px(Vec(8.285, 73.563)), module, Math::MULT_OUTPUT));
addOutput(createOutputCentered<BitPort>(mm2px(Vec(8.285, 84.639)), module, Math::DIV_OUTPUT));
addOutput(createOutputCentered<BitPort>(mm2px(Vec(8.285, 96.023)), module, Math::MOD_OUTPUT));
addOutput(createOutputCentered<BitPort>(mm2px(Vec(8.285, 106.963)), module, Math::AVG_OUTPUT));
}
void step() override
{
Math *mathModule = dynamic_cast<Math *>(this->module);
if (!mathModule)
return;
if (use_global_contrast[MATH])
{
module_contrast[MATH] = global_contrast;
}
if (module_contrast[MATH] != panelBackground->contrast)
{
panelBackground->contrast = module_contrast[MATH];
if (panelBackground->contrast < 0.4f)
{
panelBackground->invert(true);
inverter->invert = true;
}
else
{
panelBackground->invert(false);
inverter->invert = false;
}
svgPanel->fb->dirty = true;
}
ModuleWidget::step();
}
void appendContextMenu(Menu *menu) override
{
Math *module = dynamic_cast<Math *>(this->module);
assert(module);
menu->addChild(new MenuSeparator());
menu->addChild(createSubmenuItem("contrast", "", [=](Menu *menu)
{
Menu* contrastMenu = new Menu();
ContrastSlider *contrastSlider = new ContrastSlider(&(module_contrast[MATH]));
contrastSlider->box.size.x = 200.f;
GlobalOption *globalOption = new GlobalOption(&(use_global_contrast[MATH]));
contrastMenu->addChild(globalOption);
contrastMenu->addChild(new MenuSeparator());
contrastMenu->addChild(contrastSlider);
contrastMenu->addChild(createMenuItem("set global contrast", "",
[]() {
global_contrast = module_contrast[MATH];
use_global_contrast[MATH] = true;
}));
menu->addChild(contrastMenu); }));
}
};
Model *modelMath = createModel<Math, MathWidget>("math");