scratch-html5/js/primitives/LooksPrims.js

191 lines
6 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';
2013-11-01 22:44:51 -04:00
var LooksPrims = function() {};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.addPrimsTo = function(primTable) {
primTable['show'] = this.primShow;
primTable['hide'] = this.primHide;
2013-10-28 16:00:20 -04:00
primTable['nextCostume'] = this.primNextCostume;
primTable['lookLike:'] = this.primShowCostume;
primTable['costumeIndex'] = this.primCostumeNum;
2013-10-28 16:00:20 -04:00
primTable['nextScene'] = this.primNextCostume;
primTable['showBackground:'] = this.primShowCostume;
primTable['backgroundIndex'] = this.primCostumeNum;
2013-10-28 16:00:20 -04:00
primTable['startScene'] = this.primStartScene;
primTable['backgroundIndex'] = this.primCostumeNum;
2013-10-28 16:00:20 -04:00
primTable['changeSizeBy:'] = this.primChangeSize;
primTable['setSizeTo:'] = this.primSetSize;
primTable['scale'] = this.primSize;
2013-10-28 16:00:20 -04:00
primTable['comeToFront'] = this.primGoFront;
primTable['goBackByLayers:'] = this.primGoBack;
2013-10-28 16:00:20 -04:00
primTable['changeGraphicEffect:by:'] = this.primChangeEffect;
primTable['setGraphicEffect:to:'] = this.primSetEffect;
primTable['filterReset'] = this.primClearEffects;
2013-11-01 22:44:51 -04:00
primTable['say:'] = function(b) { showBubble(b, 'say'); };
primTable['say:duration:elapsed:from:'] = function(b) { showBubbleAndWait(b, 'say'); };
primTable['think:'] = function(b) { showBubble(b, 'think'); };
primTable['think:duration:elapsed:from:'] = function(b) { showBubbleAndWait(b, 'think'); };
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primShow = function(b) {
interp.targetSprite().setVisible(true);
interp.redraw();
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primHide = function(b) {
2013-11-01 22:44:51 -04:00
interp.targetSprite().setVisible(false);
2013-10-28 16:00:20 -04:00
interp.redraw();
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primNextCostume = function(b) {
interp.targetSprite().showCostume(interp.targetSprite().currentCostumeIndex + 1);
interp.redraw();
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primShowCostume = function(b) {
var s = interp.targetSprite();
if (s == null) return;
var arg = interp.arg(b, 0);
if (typeof(arg) == 'number') {
s.showCostume(arg - 1);
} else {
if ((arg == 'CAMERA') || (arg == 'CAMERA - MIRROR')) {
2013-10-28 16:00:20 -04:00
s.showCostumeNamed(arg);
return;
}
var i = s.indexOfCostumeNamed(arg);
if (i >= 0) {
s.showCostume(i);
} else {
var n = parseInt(arg, 10);
if (n === n) { // if n is not NaN
2013-10-28 16:00:20 -04:00
s.showCostume(n - 1);
} else {
return; // arg did not match a costume name nor is a valid number
}
}
}
if (s.visible) interp.redraw();
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primStartScene = function(b) {
var s = runtime.stage;
var arg = interp.arg(b, 0);
if (typeof(arg) == 'number') {
s.showCostume(arg - 1);
} else {
if ((arg == 'CAMERA') || (arg == 'CAMERA - MIRROR')) {
2013-10-28 16:00:20 -04:00
s.showCostumeNamed(arg);
return;
}
var i = s.indexOfCostumeNamed(arg);
if (i >= 0) {
s.showCostume(i);
} else {
var n = parseInt(arg, 10);
if (n === n) { // fast !isNaN check
2013-10-28 16:00:20 -04:00
s.showCostume(n - 1);
} else {
return; // arg did not match a costume name nor is a valid number
}
}
}
if (s.visible) interp.redraw();
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primCostumeNum = function(b) {
var s = interp.targetSprite();
2013-11-01 22:44:51 -04:00
return s == null ? 1 : s.currentCostumeIndex + 1;
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primChangeSize = function(b) {
var s = interp.targetSprite();
if (s == null) return;
s.setSize(s.getSize() + interp.numarg(b, 0));
2013-10-28 16:00:20 -04:00
if (s.visible) interp.redraw();
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primSetSize = function(b) {
var s = interp.targetSprite();
if (s == null) return;
s.setSize(interp.numarg(b, 0));
2013-10-28 16:00:20 -04:00
if (s.visible) interp.redraw();
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primSize = function(b) {
var s = interp.targetSprite();
if (s == null) return 100;
return s.getSize();
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primGoFront = function(b) {
var s = interp.targetSprite();
runtime.reassignZ(s, null);
2013-11-01 22:44:51 -04:00
if (s.visible) interp.redraw();
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primGoBack = function(b) {
var s = interp.targetSprite();
runtime.reassignZ(s, interp.numarg(b, 0));
2013-10-28 16:00:20 -04:00
if(s.visible) interp.redraw();
2013-11-01 22:44:51 -04:00
};
LooksPrims.prototype.primChangeEffect = function(b) {
var s = interp.targetSprite();
s.filters[interp.arg(b, 0)] += interp.numarg(b, 1);
s.updateFilters();
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primSetEffect = function(b) {
var s = interp.targetSprite();
s.filters[interp.arg(b, 0)] = interp.numarg(b, 1);
s.updateFilters();
};
2013-10-28 16:00:20 -04:00
LooksPrims.prototype.primClearEffects = function(b) {
var s = interp.targetSprite();
s.resetFilters();
s.updateFilters();
};
2013-10-28 16:00:20 -04:00
var showBubble = function(b, type) {
var s = interp.targetSprite();
2014-03-09 13:59:38 -04:00
if (s !== null) s.showBubble(interp.arg(b, 0), type);
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
var showBubbleAndWait = function(b, type) {
var s = interp.targetSprite();
2014-03-09 13:59:38 -04:00
if (s === null) return;
2013-10-28 16:00:20 -04:00
if (interp.activeThread.firstTime) {
var text = interp.arg(b, 0);
var secs = interp.numarg(b, 1);
2013-10-28 16:00:20 -04:00
s.showBubble(text, type);
if (s.visible) interp.redraw();
interp.startTimer(secs);
} else {
if (interp.checkTimer()) s.hideBubble();
}
2013-11-01 22:44:51 -04:00
};