ParamQuantity initialization and Widget centering helpers

Just wanted to share a few helpers I’ve started using:

// set displayPrecision = 4
inline ParamQuantity* dp4(ParamQuantity* p) {
    p->displayPrecision = 4;
    return p;
}

// set displayPrecision = 2
inline ParamQuantity* dp2(ParamQuantity* p) {
    p->displayPrecision = 2;
    return p;
}

// disable randomization
inline ParamQuantity* no_randomize(ParamQuantity* p) {
    p->randomizeEnabled = false;
    return p;
}

// enable snap (integer rounding)
inline ParamQuantity* snap(ParamQuantity* p) {
    p->snapEnabled = true;
    return p;
}

Too bad there aren’t setters that return references to allow “fluent-style/builder pattern” chaining, so you just have to nest the calls:

    dp4(no_randomize(configParam(P_PRE_LEVEL, 0.f, 10.f, 5.f, "Pre-level")));

Similarly, I have a lot of custom widgets, and I found the pattern of writing createFooCentered variants tedious, so I use this:

// Center any widget
// We use this instead of Rack's style of defining a lot of create<widget>Centered templates
template <class TWidget>
TWidget* Center(TWidget * widget) {
	widget->box.pos = widget->box.pos.minus(widget->box.size.div(2));
	return widget;
}

So you just do:

addChild(Center(createWhatever(...)));

It does stack parens and begin to look like LISP, but editors are smart enough to balance parens pretty easily.

1 Like