Not quite finished and not really efficient but just applying the concept to a single class helped me cut it done a lot
just really mouse->hitTest->find->(add,update,delete)
this is for multi segment envelopes
the model is saved and recalled with glaze json
struct mseg is not real time safe you need to properly pass another structure to the processor.
struct mseg
{
struct node {
double x;
double y;
double z;
};
std::vector<node> nodes;
void add_node(node new_node) {
auto it = std::lower_bound(nodes.begin(), nodes.end(), new_node,
[](const node& a, const node& b) {
return a.x < b.x;
}
);
nodes.insert(it, new_node);
}
struct glaze
{
using T = mseg;
static constexpr auto value = glz::object(
"nodes", &T::nodes
);
};
};
class MainComponent : public juce::Component,
public juce::Timer
{
public:
MainComponent(PluginProcessor &p):proc(p)
{
startTimerHz(60);
}
void timerCallback() override {
repaint();
}
~MainComponent() override {
stopTimer();
}
void mouseDoubleClick(const juce::MouseEvent &event) override {
auto b = getLocalBounds();
double w = b.getWidth();
double h = b.getHeight();
auto & nodes = proc.mseg_.nodes;
auto it = std::find_if(nodes.begin(), nodes.end(), [&](const auto& n)
{
return std::abs(n.x*w - event.position.x) < 10.0f
&& std::abs(n.y*h - event.position.y) < 10.0f;
});
if(it == nodes.end()) {
proc.mseg_.add_node(mseg::node(event.position.x/w,event.position.y/h, 0.5));
}else{
proc.mseg_.nodes.erase(it);
}
}
void mouseDown(const juce::MouseEvent &event) override {
auto bounds = getLocalBounds();
double w = bounds.getWidth();
double h = bounds.getHeight();
auto & nodes = proc.mseg_.nodes;
currentxy = nullptr;
currentz = nullptr;
auto it = std::find_if(nodes.begin(), nodes.end(), [&](const auto& n)
{
return std::abs(n.x*w - event.position.x) < 10.0f
&& std::abs(n.y*h - event.position.y) < 10.0f;
});
if(it != nodes.end()) {
currentxy = &(*it);
}
auto cx = event.position.x;
auto cy = event.position.y;
auto itz = std::find_if(nodes.begin(), nodes.end()-1, [cx,cy,w,h](const auto& a)
{
double M = 32;
const auto& b = *(&a + 1);
for(double m = 1; m < M; m+=1.0)
{
auto zin = a.y-b.y > 0 ? -a.z : a.z;
auto fin = abs(zin) > 1e-3 ? ((exp(16*zin*(m / M))-1)/(exp(16*zin)-1)) : m/M;
auto px = a.x * w + (b.x - a.x) * w * m / M;
auto py = a.y * h + (b.y - a.y) * h * fin;
bool hit = std::abs(px - cx) <= 5.0 && std::abs(py - cy) <= 5.0;
if(hit){
return true;
}
}
return false;
});
if(itz != nodes.end()) {
currentz = &(*itz);
currentz_start = currentz->z;
}
}
mseg::node * currentxy;
mseg::node * currentz;
double currentz_start;
void mouseDrag(const juce::MouseEvent &event) override {
auto bounds = getLocalBounds();
double w = bounds.getWidth();
double h = bounds.getHeight();
if(currentxy)
{
currentxy->x = std::clamp(event.position.x/w,0.0,1.0);
currentxy->y = std::clamp(event.position.y/h,0.0,1.0);
}
else if(currentz)
{
currentz->z = std::clamp(currentz_start+(event.mouseDownPosition.y-event.position.y)/h,-1.0,1.0);
}
}
void paint(juce::Graphics &g) override {
g.setColour(juce::Colours::black);
g.fillAll();
g.setColour(juce::Colours::white);
auto bounds = getLocalBounds();
double w = bounds.getWidth();
double h = bounds.getHeight();
auto & nodes = proc.mseg_.nodes;
for(int n = 0; n < (int)nodes.size()-1; n++)
{
auto a = nodes[n];
auto b = nodes[n+1];
g.drawEllipse((a.x*w)-10,a.y*h-10,20.0f,20.0f,1.0);
double M = 32;
juce::Path p;
p.startNewSubPath(a.x*w, a.y*h);
for(double m = 1; m < M; m+=1.0)
{
auto zin = a.y-b.y > 0 ? -a.z : a.z;
auto fin = abs(zin) > 1e-3 ? ((exp(16*zin*(m / M))-1)/(exp(16*zin)-1)) : m/M;
p.lineTo(a.x * w + (b.x - a.x) * w * m / M,
a.y * h + (b.y - a.y) * h * fin);
}
p.lineTo(b.x*w,b.y*h);
g.strokePath(p, juce::PathStrokeType(1.0f));
}
}
private:
PluginProcessor& proc;
};