Merge pull request #15 from Scimonster/master

Date/Time blocks (not working on all browsers)
This commit is contained in:
Shane M. Clements 2013-10-30 13:08:22 -07:00
commit 73fdd4b7eb
6 changed files with 38 additions and 3 deletions

View file

@ -1 +1,3 @@
The inital work on the HTML5 Scratch player was done by Tim Mickel. John Maloney provided feedback and guidance. Shane Clements took over the development and management late summer 2013.
Scimonster fixed some bugs and added some features: the time/date blocks, list reporter block, fixed timer blocks, added timer reporter.

View file

@ -248,13 +248,13 @@ Interpreter.prototype.initPrims = function() {
// added by John:
this.primitiveTable['showBubble'] = function(b) { console.log(interp.arg(b, 1)) }
this.primitiveTable['timerReset'] = function(b) { interp.timerBase = new Date().getTime() }
this.primitiveTable['timer'] = function(b) { return (new Date().getTime() - interp.timerBase) / 1000 }
this.primitiveTable['timerReset'] = function(b) {interp.timerBase = (new Date()).getTime() }
this.primitiveTable['timer'] = function(b) {return ((new Date()).getTime() - interp.timerBase) / 1000 }
new Primitives().addPrimsTo(this.primitiveTable);
}
var timerBase = 0;
Interpreter.prototype.timerBase = (new Date()).getTime();
Interpreter.prototype.lookupPrim = function(op) {
var fcn = interp.primitiveTable[op];
if (fcn == null) fcn = function(b) { console.log('not implemented: ' + b.op) }

View file

@ -99,6 +99,9 @@ Reporter.prototype.update = function() {
case 'costumeIndex':
newValue = target.currentCostumeIndex + 1;
break;
case 'timer':
newValue = interp.primitiveTable.timer().toFixed(3);
break;
}
this.valueEl.html(newValue);
if (this.mode == 3)

View file

@ -75,6 +75,7 @@ Runtime.prototype.greenFlag = function() {
if (this.projectLoaded) {
interp.activeThread = new Thread(null);
interp.threads = [];
interp.primitiveTable.timerReset();
this.startGreenFlags();
}
}

View file

@ -31,6 +31,7 @@ SensingPrims.prototype.addPrimsTo = function(primTable) {
primTable['getAttribute:of:'] = this.primGetAttribute;
primTable['timeAndDate'] = function(b){ return runtime.getTimeString(interp.arg(b, 0)); };
primTable['timestamp'] = this.primTimestamp;
}
SensingPrims.prototype.primTouching = function(b) {
@ -208,6 +209,25 @@ SensingPrims.prototype.primGetAttribute = function(b) {
return 0;
}
SensingPrims.prototype.primTimeDate = function(b) {
var dt = interp.arg(b, 0);
var now = new Date();
if (dt == 'year') return now.getFullYear();
if (dt == 'month') return now.getMonth()+1;
if (dt == 'date') return now.getDate();
if (dt == 'day of week') return now.getDay()+1;
if (dt == 'hour') return now.getHours();
if (dt == 'minute') return now.getMinutes();
if (dt == 'second') return now.getSeconds();
return 0;
}
SensingPrims.prototype.primTimestamp = function(b) {
var now = new Date(), epoch = new Date(2000,0,1), dst = now.getTimezoneOffset() - epoch.getTimezoneOffset(), msSince = now.getTime() - epoch.getTime();
msSince += (now.getTimezoneOffset() - dst) * 60000;
return msSince / 86400000;
}
// Helpers
SensingPrims.prototype.mouseOrSpritePosition = function(arg) {
if (arg == "_mouse_") {

View file

@ -26,6 +26,7 @@ VarListPrims.prototype.addPrimsTo = function(primTable) {
primTable['showVariable:'] = this.primShowVar;
// List primitives
primTable['contentsOfList:'] = this.primReadList;
primTable['append:toList:'] = this.primListAppend;
primTable['deleteLine:ofList:'] = this.primListDeleteLine;
primTable['insert:at:ofList:'] = this.primListInsertAt;
@ -103,6 +104,14 @@ var findList = function(targetSprite, listName) {
return null;
}
VarListPrims.prototype.primReadList = function(b) {
var list = findList(interp.targetSprite(), interp.arg(b, 0));
if (list) {
var allOne = list.map(function(val){return val.length}).reduce(function(old,val){return old+val},0)===list.length;
return list.join(allOne?'':' ');
}
}
VarListPrims.prototype.primListAppend = function(b) {
var list = findList(interp.targetSprite(), interp.arg(b, 1));
if (list) list.push(interp.arg(b, 0));