faking a checkbox to do a boolean property control

This commit is contained in:
Sam Gateau 2015-04-16 17:55:55 -07:00
parent b5b146b81b
commit 1c5c7cc239
2 changed files with 129 additions and 1 deletions

View file

@ -72,13 +72,19 @@ function runStageTime() {
}
Script.setInterval(runStageTime, tickTackPeriod);
panel.newCheckbox("Enable Earth Sun Model",
function(value) { Scene.setStageEarthSunModelEnable((value != 0)); },
function() { return Scene.isStageEarthSunModelEnabled(); },
function(value) { return (value); }
);
panel.newSlider("Light Intensity", 0.0, 5,
function(value) { Scene.setSunIntensity(value); },
function() { return Scene.getSunIntensity(); },
function(value) { return (value).toFixed(2); }
);
panel.newSlider("Light Ambient Intensity", 0.0, 1.0,
panel.newSlider("Ambient Light Intensity", 0.0, 1.0,
function(value) { Scene.setSunAmbientIntensity(value); },
function() { return Scene.getSunAmbientIntensity(); },
function(value) { return (value).toFixed(2); }

View file

@ -118,6 +118,113 @@ Slider = function(x,y,width,thumbSize) {
};
}
// The Checkbox class
Checkbox = function(x,y,width,thumbSize) {
this.thumb = Overlays.addOverlay("text", {
backgroundColor: { red: 255, green: 255, blue: 255 },
x: x,
y: y,
width: thumbSize,
height: thumbSize,
alpha: 1.0,
backgroundAlpha: 1.0,
visible: true
});
this.background = Overlays.addOverlay("text", {
backgroundColor: { red: 125, green: 125, blue: 255 },
x: x,
y: y,
width: width,
height: thumbSize,
alpha: 1.0,
backgroundAlpha: 0.5,
visible: true
});
this.thumbSize = thumbSize;
this.thumbHalfSize = 0.5 * thumbSize;
this.minThumbX = x + this.thumbHalfSize;
this.maxThumbX = x + width - this.thumbHalfSize;
this.thumbX = this.minThumbX;
this.minValue = 0.0;
this.maxValue = 1.0;
this.clickOffsetX = 0;
this.isMoving = false;
this.updateThumb = function() {
thumbTruePos = this.thumbX - 0.5 * this.thumbSize;
Overlays.editOverlay(this.thumb, { x: thumbTruePos } );
};
this.onMouseMoveEvent = function(event) {
if (this.isMoving) {
newThumbX = event.x - this.clickOffsetX;
if (newThumbX < this.minThumbX) {
newThumbX = this.minThumbX;
}
if (newThumbX > this.maxThumbX) {
newThumbX = this.maxThumbX;
}
this.thumbX = newThumbX;
this.updateThumb();
this.onValueChanged(this.getValue());
}
};
this.onMousePressEvent = function(event) {
this.isMoving = true;
var clickOffset = event.x - this.thumbX;
if ((clickOffset > -this.thumbHalfSize) && (clickOffset < this.thumbHalfSize)) {
this.clickOffsetX = clickOffset;
} else {
this.clickOffsetX = 0;
this.thumbX = event.x;
this.updateThumb();
this.onValueChanged(this.getValue());
}
};
this.onMouseReleaseEvent = function(event) {
this.isMoving = false;
};
// Public members:
this.setNormalizedValue = function(value) {
if (value < 0.0) {
this.thumbX = this.minThumbX;
} else if (value > 1.0) {
this.thumbX = this.maxThumbX;
} else {
this.thumbX = value * (this.maxThumbX - this.minThumbX) + this.minThumbX;
}
this.updateThumb();
};
this.getNormalizedValue = function() {
return (this.thumbX - this.minThumbX) / (this.maxThumbX - this.minThumbX);
};
this.setValue = function(value) {
var normValue = (value - this.minValue) / (this.maxValue - this.minValue);
this.setNormalizedValue(normValue);
};
this.getValue = function() {
return this.getNormalizedValue() * (this.maxValue - this.minValue) + this.minValue;
};
this.onValueChanged = function(value) {};
this.destroy = function() {
Overlays.deleteOverlay(this.background);
Overlays.deleteOverlay(this.thumb);
};
}
var textFontSize = 16;
@ -252,6 +359,21 @@ Panel = function(x, y) {
// print("created Item... slider=" + name);
};
this.newCheckbox = function(name, setValue, getValue, displayValue) {
var checkboxItem = new PanelItem(name, setValue, getValue, displayValue, this.x, this.nextY, textWidth, valueWidth, rawHeight);
var checkbox = new Checkbox(this.widgetX, this.nextY, widgetWidth, rawHeight);
checkbox.onValueChanged = function(value) { checkboxItem.setterFromWidget(value); };
checkboxItem.widget = checkbox;
checkboxItem.setter(getValue());
this.items[name] = checkboxItem;
this.nextY += rawYDelta;
// print("created Item... slider=" + name);
};
this.destroy = function() {
for (var i in this.items) {
this.items[i].destroy();