scratch-html5/js/Reporter.js

122 lines
4.3 KiB
JavaScript
Raw Normal View History

2013-10-28 16:00:20 -04:00
// Copyright (C) 2013 Massachusetts Institute of Technology
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
'use strict';
var Reporter = function(data) {
this.cmd = data.cmd;
this.color = data.color;
this.isDiscrete = data.isDiscrete;
this.label = data.label;
this.mode = data.mode;
this.param = data.param;
this.sliderMin = data.sliderMin;
this.sliderMax = data.sliderMax;
this.target = data.target;
this.visible = data.visible;
this.x = data.x;
this.y = data.y;
this.z = io.getCount();
this.el = null; // jQuery Element for the outer box
this.valueEl = null; // jQ element containing the reporter value
this.slider = null; // slider jQ element
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
Reporter.prototype.attach = function(scene) {
switch (this.mode) {
case 1: // Normal
case 3: // Slider
this.el = $('<div class="reporter-normal">' + this.label + '</div>');
this.valueEl = $('<div class="reporter-inset">null</div>');
this.el.append(this.valueEl);
if (this.mode == 3) {
// Slider-specific
// Temporarily, set the value to sliderMin until an update
this.slider = $('<input type="range" min="' + this.sliderMin +
'" max="' + this.sliderMax + '" step="1" value="' +
2013-11-01 22:44:51 -04:00
this.sliderMin + '" data-target="' + this.target +
2013-10-28 16:00:20 -04:00
'" data-var="' + this.param + '">');
this.slider.change(this.changeSlider);
this.el.append('<br>');
this.el.append(this.slider);
}
break;
case 2: // Large
this.el = $('<div class="reporter-large">null</div>');
this.valueEl = this.el;
break;
}
this.el.css('left', this.x);
this.el.css('top', this.y);
this.el.css('z-index', this.z);
var cR = (this.color >> 16);
var cG = (this.color >> 8 & 255);
var cB = (this.color & 255);
this.valueEl.css('background-color', 'rgb(' + cR + ',' + cG + ',' + cB + ')');
this.el.css('display', this.visible ? 'inline-block' : 'none');
scene.append(this.el);
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
Reporter.prototype.update = function() {
this.el.css('display', this.visible ? 'inline-block' : 'none');
if (!this.visible) return;
2013-11-01 22:44:51 -04:00
var newValue = '';
2013-10-28 16:00:20 -04:00
var target = runtime.spriteNamed(this.target);
switch (this.cmd) {
case 'getVar:':
newValue = target.variables[this.param];
if (typeof(newValue) == 'number' && this.mode != 3) newValue = newValue.toFixed(3);
break;
case 'xpos':
newValue = target.scratchX.toFixed(3);
break;
case 'ypos':
newValue = target.scratchY.toFixed(3);
break;
case 'heading':
newValue = target.direction;
break;
case 'scale':
newValue = target.getSize();
break;
case 'sceneName':
newValue = runtime.stage.costumes[runtime.stage.currentCostumeIndex].costumeName;
break;
case 'costumeIndex':
newValue = target.currentCostumeIndex + 1;
break;
2013-10-30 09:50:29 -04:00
case 'timer':
newValue = interp.primitiveTable.timer().toFixed(3);
break;
2013-10-28 16:00:20 -04:00
}
this.valueEl.html(newValue);
2013-11-01 22:44:51 -04:00
if (this.mode == 3) {
2013-10-28 16:00:20 -04:00
this.slider.val(parseInt(newValue));
2013-11-01 22:44:51 -04:00
}
};
2013-10-28 16:00:20 -04:00
Reporter.prototype.updateLayer = function() {
this.el.css('z-index', this.z);
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
Reporter.prototype.changeSlider = function() {
var newValue = parseInt($(this).val());
var target = runtime.spriteNamed($(this).attr('data-target'));
var variable = $(this).attr('data-var');
target.variables[variable] = newValue;
2013-11-01 22:44:51 -04:00
};