cables-gl / cables_docs

cables documentation docs.cables.gl
https://cables.gl/docs/docs
45 stars 16 forks source link

TriggerCounter and TriggerCounterLoop missing port #891

Open 60-hz opened 2 weeks ago

60-hz commented 2 weeks ago

TriggerCounter and TriggerCounterLoop Op are missing an input port to set the current count value, which is very convenient in many situations.

Also TriggerCounter Op could have different count mode (up/down/Pingpong)

franckaubry commented 2 weeks ago

I am unsure, what you mean... Do you mean that you want to be able to start counting from "100" f.ex. and be able to set that value from the op UI? Wouldn't just using a math op set to "+100" solve that?

60-hz commented 2 weeks ago

Do you mean that you want to be able to start counting from "100"

Not really, still start counting from zero, but be able to change the current count value of counter's output, be able to jump to a value or go back to a previous one for example. Also keeping a coherent "min", "max", and reset feature.

Here is a simple working modification of TriggerCounterLoop:

const exe = op.inTriggerButton("trigger in"),
    reset = op.inTriggerButton("reset"),
    trigger = op.outTrigger("trigger out"),
    num = op.outNumber("current count"),

    inMinLoopValue = op.inValueInt("Loop min", 0.0),
    inMaxLoopValue = op.inValueInt("Loop max", 4.0),
    inSetCount = op.inValueInt("Set count", 0); // New input port to modify current count

let n = Math.floor(inMinLoopValue.get());

// increments with each trigger and loops
exe.onTriggered = function () {
    let inMin = Math.floor(inMinLoopValue.get());
    let inMax = Math.floor(inMaxLoopValue.get());

    if (inMin < inMax) {
        if (n < inMin) {
            n = inMinLoopValue.get();
        } else if (n >= inMax) {
            n = inMinLoopValue.get();
        } else {
            n++;
        }
    } else if (inMin > inMax) {
        if (n < inMax) {
            n = inMin;
        } else if (n > inMin) {
            n = inMin;
        } else if (n <= inMax) {
            n = inMin;
        } else {
            n--;
        }
    }

    num.set(n);
    op.setUiAttrib({ "extendTitle": n });
    trigger.trigger();
};

// Update `n` whenever the new input changes
inSetCount.onChange = function () {
    n = inSetCount.get();
    num.set(n);
    op.setUiAttrib({ "extendTitle": n });
};

reset.onTriggered = function () {
    let inMin = Math.floor(inMinLoopValue.get());
    let inMax = Math.floor(inMaxLoopValue.get());

    if (inMin < inMax) {
        n = inMin;
    } else if (inMax < inMin) {
        n = inMin;
    } else {
        n = 0;
    }
    op.setUiAttrib({ "extendTitle": n });
    num.set(n);
};
60-hz commented 1 week ago

Edit: added also count mode to up / down / pingpong:

const exe = op.inTriggerButton("trigger in"),
    reset = op.inTriggerButton("reset"),
    trigger = op.outTrigger("trigger out"),
    num = op.outNumber("current count"),

    inMinLoopValue = op.inValueInt("Loop min", 0.0),
    inMaxLoopValue = op.inValueInt("Loop max", 4.0),
    inSetCount = op.inValueInt("Set count", 0),
    inMode = op.inSwitch("Mode", ["up", "down", "pingpong"], "up");

let n = Math.floor(inMinLoopValue.get());
let direction = 1;  // used for ping-pong mode to change direction

exe.onTriggered = function () {
    let inMin = Math.floor(inMinLoopValue.get());
    let inMax = Math.floor(inMaxLoopValue.get());

    if (inMode.get() === "up") {
        n++;
        if (n > inMax) n = inMin;
    } else if (inMode.get() === "down") {
        n--;
        if (n < inMin) n = inMax;
    } else if (inMode.get() === "pingpong") {
        n += direction;
        if (n >= inMax) direction = -1;
        else if (n <= inMin) direction = 1;
    }

    num.set(n);
    op.setUiAttrib({ "extendTitle": n });
    trigger.trigger();
};

inSetCount.onChange = function () {
    n = inSetCount.get();
    num.set(n);
    op.setUiAttrib({ "extendTitle": n });
};

reset.onTriggered = function () {
    n = Math.floor(inMinLoopValue.get());
    direction = 1;  // reset direction for ping-pong mode
    num.set(n);
    op.setUiAttrib({ "extendTitle": n });
};