Would immediate mode work for juce?

Is it bad idea have one timer just calling a bunch of functions that draw and handle mouse events or drag and drop zones within juce itself rather everything in classes . Free animation as well described in this talk

Of course you would have to rewrite a lot of this logic without look and feel and parameter attachment perks but only has to be done once .

Knob(“param1”,graphicVar,Mouse, bounds);

Knob(“param2”, graphicVar,Mouse,bounds) ;

ComboBox(“param3”,bounds);

1 Like

JUCE is already immediate mode - i.e. when you write graphics code the changes are applied immediately rather than going into a rendering queue and applied asynchronously.

The main difference between JUCE’s API and ImGui is that JUCE is object-oriented whereas ImGui is procedural. You could absoluately write a procedural wrapper around JUCE’s API.

JUCE is already immediate mode

Your comment relates to the Graphics API and OP is talking about something else. As mentioned in the talk at 07:40 the term derives from the same meaning you referred to so that certainly contributes to the confusion.

1 Like

I was just pointing out that from OP’s example code it looks like they’re asking about whether a procedural GUI API would work for JUCE, not whether immediate mode would work since that’s already how JUCE works.

procedural would have been a better word to use. It looks super clean to me for smaller projects.

1 Like

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;
    
};

I think immediate mode could work quite well for certain JUCE use cases, especially custom editors and interfaces where the UI changes frequently. The main thing to watch out for is performance and how component state is managed. A hybrid approach might be the sweet spot, keeping JUCE’s existing component system while allowing immediate-mode rendering where it makes sense.

Immediate mode is an interface. It can be used to manage retained mode classes.

IMGUI and similar libraries just hide retained state using all kinds of tricks (for example thread local static variables you don’t see).

You can definitely write a JUCE backend to IMGUI and I wrote one for my graphics library, EACP: GitHub - eyalamirmusic/imgui-eacp: Examples to combine IMGUI with an eacp backend · GitHub

There are many questions when it comes to implementing it and some performance/interface tradeoffs, but if you like IMGUI style interface and are willing to tackle the rough edges (caching paths that don’t change, etc) it should work in theory.

1 Like