@d-healey Sorry my example is not very clear.
What I meant is that the
if (!Content.getComponent("testPanel"))
Works without disruptions.
It does throw an error to say that component does not exist, but because the "if" returns a "true", the code then proceeds without any problems.
The whole point of the if/else here is just an out-of-context part of factory methods that I use to create components, where the original creation will happen on the first compile and can run any code that doesn't need to be set the 2nd time around (such as properties).
That all takes place in an inline function like the following example:
// **** FACTORY RANGE SLIDER **** //
// "orientation" accepts "horizontal" or "vertical"
inline function createFactoryRangeSlider(stringName, xywArray, rangeMinMaxArray, highlightColour, orientation)
{
local x1 = xywArray[0];
local y1 = xywArray[1];
local size = xywArray[2];
local isVertical = orientation == "vertical";
local sliderTrackWidth = isVertical ? size * 0.2 : size;
local valueBubbleSpace = isVertical ? size * 0.4 : 0;
local w = isVertical ? size * 0.5 : size;
local h = isVertical ? size : size * 0.3;
local panel;
if (!Content.getComponent(stringName + "RangeSlider"))
{
panel = Content.addPanel(stringName + "RangeSlider", 0, 0);
panel.set("allowCallbacks", "All Callbacks");
panel.setPosition(x1, y1, w, h);
panel.set("saveInPreset", true);
local t = Engine.createTimerObject();
t.setTimerCallback(function[panel, t, rangeMinMaxArray]()
{
panel.setValue(
{
"minValue": rangeMinMaxArray[0],
"maxValue": rangeMinMaxArray[1]
});
t.stopTimer();
});
t.startTimer(50);
}
else
{
panel = Content.getComponent(stringName + "RangeSlider");
}
panel.data.parentPopup = Content.getComponent(stringName + "Popup");
// Safe to add data here: ensures it's done on every compile
if (!panel.data.rangeSliderTimer)
{
panel.data.rangeSliderTimer = Engine.createTimerObject();
panel.data.rangeSliderTimer.setTimerCallback(function[panel, stringName]()
{
if (panel.data.needsUpdate)
{
panel.data.needsUpdate = false;
var parentPopup = panel.data.parentPopup;
parentPopup.changed();
}
});
panel.data.rangeSliderTimer.startTimer(100);
}
panel.data.rangeMin = rangeMinMaxArray[0];
panel.data.rangeMax = rangeMinMaxArray[1];
panel.setPaintRoutine(function[highlightColour, isVertical, sliderTrackWidth, valueBubbleSpace](g)
{
var width = this.getWidth();
var height = this.getHeight();
var margin = (isVertical ? height : width) * 0.075;
var trackThickness = (isVertical ? sliderTrackWidth : height) * 0.15;
var rangeStart = margin;
var rangeEnd = (isVertical ? height : width) - margin;
var rangeMin = this.data.rangeMin;
var rangeMax = this.data.rangeMax;
var rangeSize = rangeMax - rangeMin;
function getRatioFromValue(value)
{
return (value - rangeMin) / rangeSize;
}
var minRatio = getRatioFromValue(this.getValue().minValue);
var maxRatio = getRatioFromValue(this.getValue().maxValue);
var minPos = isVertical
? rangeStart + (1.0 - minRatio) * (rangeEnd - rangeStart)
: rangeStart + minRatio * (rangeEnd - rangeStart);
var maxPos = isVertical
? rangeStart + (1.0 - maxRatio) * (rangeEnd - rangeStart)
: rangeStart + maxRatio * (rangeEnd - rangeStart);
var centerX = isVertical ? width / 2 : 0;
// Track
g.setColour(Colours.grey);
if (isVertical)
g.fillRoundedRectangle([centerX - trackThickness / 2, rangeStart, trackThickness, rangeEnd - rangeStart], width * 0.05);
else
g.fillRoundedRectangle([rangeStart, height / 2 - trackThickness / 2, rangeEnd - rangeStart, trackThickness], height * 0.075);
// Highlight range
g.setColour(highlightColour);
if (isVertical)
{
var y = minPos;
var h1 = maxPos - minPos;
g.fillRect([centerX - trackThickness / 2, y, trackThickness, h1]);
}
else
{
var x = minPos;
var w1 = maxPos - minPos;
g.fillRect([x, height / 2 - trackThickness / 2, w1, trackThickness]);
}
// Knobs
var knobRadius = (isVertical ? sliderTrackWidth : height) / 6;
g.setColour(Colours.white);
if (isVertical)
{
g.fillEllipse([centerX - knobRadius, minPos - knobRadius, knobRadius * 2, knobRadius * 2]);
g.fillEllipse([centerX - knobRadius, maxPos - knobRadius, knobRadius * 2, knobRadius * 2]);
}
else
{
g.fillEllipse([minPos - knobRadius, height / 2 - knobRadius, knobRadius * 2, knobRadius * 2]);
g.fillEllipse([maxPos - knobRadius, height / 2 - knobRadius, knobRadius * 2, knobRadius * 2]);
}
// Bubbles
var bubbleWidth = knobRadius * 5;
var bubbleHeight = knobRadius * 2.5;
var bubbleRadius = knobRadius;
function drawValueBubble(x, y, value)
{
g.setColour(Colours.white);
g.fillRoundedRectangle([x, y, bubbleWidth, bubbleHeight], bubbleRadius);
g.setColour(Colours.black);
g.setFont("Oxygen", bubbleHeight * 0.75);
g.drawAlignedText("" + Math.round(value), [x, y, bubbleWidth, bubbleHeight], "centred");
}
if (isVertical)
{
drawValueBubble(centerX - knobRadius - bubbleWidth - 4, minPos - bubbleHeight / 2, this.getValue().minValue);
drawValueBubble(centerX + knobRadius + 4, maxPos - bubbleHeight / 2, this.getValue().maxValue);
}
else
{
drawValueBubble(minPos - bubbleWidth / 2, height / 2 - knobRadius - bubbleHeight - 4, this.getValue().minValue);
drawValueBubble(maxPos - bubbleWidth / 2, height / 2 - knobRadius - bubbleHeight - 4, this.getValue().maxValue);
}
});
panel.setMouseCallback(function[isVertical, stringName](event)
{
var size1 = isVertical ? this.getHeight() : this.getWidth();
var margin = size1 * 0.075;
var rangeStart = margin;
var rangeEnd = size1 - margin;
var rangeMin = this.data.rangeMin;
var rangeMax = this.data.rangeMax;
var rangeSize = rangeMax - rangeMin;
var parentPopup = Content.getComponent(stringName + "Popup");
var dataTableContainer = Content.getComponent(parentPopup.get("parentComponent"));
var columnID = parentPopup.data.columnID;
var filterData = dataTableContainer.data.filterData[columnID];
if (event.clicked)
{
var pos = isVertical ? event.y : event.x;
function getRatioFromValue(value) { return (value - rangeMin) / rangeSize; }
var minRatio = getRatioFromValue(this.getValue().minValue);
var maxRatio = getRatioFromValue(this.getValue().maxValue);
var minPos = isVertical
? rangeStart + (1.0 - minRatio) * (rangeEnd - rangeStart)
: rangeStart + minRatio * (rangeEnd - rangeStart);
var maxPos = isVertical
? rangeStart + (1.0 - maxRatio) * (rangeEnd - rangeStart)
: rangeStart + maxRatio * (rangeEnd - rangeStart);
var distToMin = Math.abs(pos - minPos);
var distToMax = Math.abs(pos - maxPos);
this.data.dragTarget = distToMin < distToMax ? "min" : "max";
this.data.needsUpdate = true; // defer popup.changed
}
if (event.drag)
{
var pos = isVertical ? event.y : event.x;
var relPos = (pos - rangeStart) / (rangeEnd - rangeStart);
var valRatio = isVertical ? (1.0 - relPos) : relPos;
var val = rangeMin + valRatio * rangeSize;
var clampedVal = Math.max(rangeMin, Math.min(rangeMax, val));
if (this.data.dragTarget == "min")
{
var minValue = Math.min(clampedVal, this.getValue().maxValue);
filterData.minValue = minValue;
this.getValue().minValue = minValue;
}
else if (this.data.dragTarget == "max")
{
var maxValue = Math.max(clampedVal, this.getValue().minValue);
filterData.maxValue = maxValue;
this.getValue().maxValue = maxValue;
}
this.data.needsUpdate = true;
this.repaint();
}
if (event.mouseUp)
this.data.dragTarget = "";
});
return panel;
}