@d-healey said in Trying to make a factory method but getting an error:
@VirtualVirgin Got a snippet?
// Factory bipolar knob
inline function createBipolarKnob(name, x, y, min, max)
{
// check if component already exists
local c = Content.getComponent(name + "BipolarKnob");
if (!isDefined(c))
{
// Create a panel, knob, text label and value label
local panel = Content.addPanel(name + "Panel", x, y);
local knob = Content.addKnob(name + "BipolarKnob", x, y);
local text = Content.addLabel(name + "TextLabel", x, y);
// set panel as parent
knob.set("parentComponent", panel.getId());
text.set("parentComponent", panel.getId());
//value.set("parentComponent", panel.getId());
panel.setPosition(x, y, 100, 100);
// set knob value to panel text
panel.set("text", knob.getValue());
// draw value text on panel
panel.setPaintRoutine(function(g)
{
var pa = this.getLocalBounds(0);
g.setFont("oxygen", pa[2] / 10);
g.setColour(Colours.white);
g.drawAlignedText(Math.round(this.get("text")), [pa[0], pa[1] - pa[2] / 5, pa[2], pa[3]], "centredBottom");
});
// create broadcasters for knob
// Broadcaster for sending the knob value to the text property
local bc = Engine.createBroadcaster({
"id": name + "bcValueSender",
"args": ["component", "value"],
"tags": []
});
// attach to event Type
bc.attachToComponentValue(knob, "");
// attach panel to trigger text value update
bc.addComponentPropertyListener(panel, ["text"], "value to text", function(index, component, value){
return value;
});
// attach panel to trigger repaint on value change
bc.addComponentRefreshListener(panel, "repaint", "\"update the panel\"");
// knob settings
knob.set("itemColour", 0xFFFFFFFF);
knob.set("min", min);
knob.set("max", max);
knob.set("stepSize", 1.0);
knob.set("middlePosition", 0.0);
// set text
local s = name;
local splitName = s.splitCamelCase();
text.set("text", splitName[0] + " " + splitName[1]);
text.set("fontName", "Avenir");
// LAF for bipolar knob
knob.setLocalLookAndFeel(LAF.bipolarKnob);
// get area of panel
local a = panel.getLocalBounds(0);
// positioning and font
knob.setPosition(
a[0] + a[2] * 3/10,
a[1] + a[3] * 3/10,
a[2] * 4/10,
a[3] * 4/10);
text.setPosition(
a[0],
a[1] + a[3] * 0.5/10,
a[2],
a[3] * 3/10);
text.set("fontSize", a[3] * 1.9/10 );
// return the local object. This will transfer the "ownership" to the left side of the equation
return panel;
return knob;
return text;
}
else
{
// reference panel, knob, text label and value label
local panel = Content.getComponent(name + "Panel");
local knob = Content.getComponent(name + "BipolarKnob");
local text = Content.getComponent(name + "TextLabel");
//local value = Content.getComponent(name + "ValueLabel");
// LAF for bipolar knob
knob.setLocalLookAndFeel(LAF.bipolarKnob);
// get area of panel
local a = panel.getLocalBounds(0);
// set knob value to panel text
panel.set("text", knob.getValue());
// draw value text on panel
panel.setPaintRoutine(function(g)
{
var pa = this.getLocalBounds(0);
g.setFont("oxygen", pa[2] / 10);
g.setColour(Colours.white);
g.drawAlignedText(Math.round(this.get("text")), [pa[0], pa[1] - pa[2] / 5, pa[2], pa[3]], "centredBottom");
});
// Broadcaster for sending the knob value to the text property
local bc = Engine.createBroadcaster({
"id": name + "bcValueSender",
"args": ["component", "value"],
"tags": []
});
// attach to event Type
bc.attachToComponentValue(knob, "");
// attach panel to trigger text value update
bc.addComponentPropertyListener(panel, ["text"], "value to text", function(index, component, value){
return value;
});
// attach panel to trigger repaint on value change
bc.addComponentRefreshListener(panel, "repaint", "\"update the panel\"");
// positioning and font
knob.setPosition(
a[0] + a[2] * 3/10,
a[1] + a[3] * 3/10,
a[2] * 4/10,
a[3] * 4/10);
text.setPosition(
a[0],
a[1] + a[3] * 0.5/10,
a[2],
a[3] * 3/10);
text.set("fontSize", a[3] * 1.9/10 );
// return the local object. This will transfer the "ownership" to the left side of the equation
return panel;
return knob;
return text;
}
};
createBipolarKnob("TestKnob0", 0, 100, -127, 127);
createBipolarKnob("TestKnob1", 0, 200, -127, 127);