more refactorings for objects.js

pull/95/head
jmoenig 2020-04-28 09:19:32 +02:00
rodzic e54910cb9d
commit bbda980682
1 zmienionych plików z 42 dodań i 61 usunięć

Wyświetl plik

@ -7488,8 +7488,7 @@ StageMorph.prototype.setScale = function (number) {
var delta = number / this.scale, var delta = number / this.scale,
pos = this.position(), pos = this.position(),
relativePos, relativePos,
bubble, bubble;
myself = this;
if (delta === 1) {return; } if (delta === 1) {return; }
this.cachedPenTrailsMorph = null; this.cachedPenTrailsMorph = null;
@ -7497,7 +7496,7 @@ StageMorph.prototype.setScale = function (number) {
this.setExtent(this.dimensions.multiplyBy(number)); this.setExtent(this.dimensions.multiplyBy(number));
// now move and resize all children - sprites, bubbles, watchers etc.. // now move and resize all children - sprites, bubbles, watchers etc..
this.children.forEach(function (morph) { this.children.forEach(morph => {
relativePos = morph.position().subtract(pos); relativePos = morph.position().subtract(pos);
morph.fixLayout(); morph.fixLayout();
morph.setPosition( morph.setPosition(
@ -7512,13 +7511,13 @@ StageMorph.prototype.setScale = function (number) {
morph.positionTalkBubble(); morph.positionTalkBubble();
} }
} else if (morph instanceof StagePrompterMorph) { } else if (morph instanceof StagePrompterMorph) {
if (myself.scale < 1) { if (this.scale < 1) {
morph.setWidth(myself.width() - 10); morph.setWidth(this.width() - 10);
} else { } else {
morph.setWidth(myself.dimensions.x - 20); morph.setWidth(this.dimensions.x - 20);
} }
morph.setCenter(myself.center()); morph.setCenter(this.center());
morph.setBottom(myself.bottom()); morph.setBottom(this.bottom());
} }
}); });
}; };
@ -7950,13 +7949,12 @@ StageMorph.prototype.updateProjection = function () {
StageMorph.prototype.stepGenericConditions = function (stopAll) { StageMorph.prototype.stepGenericConditions = function (stopAll) {
var hatCount = 0, var hatCount = 0,
myself = this,
ide; ide;
this.children.concat(this).forEach(function (morph) { this.children.concat(this).forEach(morph => {
if (isSnapObject(morph)) { if (isSnapObject(morph)) {
morph.allGenericHatBlocks().forEach(function (block) { morph.allGenericHatBlocks().forEach(block => {
hatCount += 1; hatCount += 1;
myself.threads.doWhen(block, morph, stopAll); this.threads.doWhen(block, morph, stopAll);
}); });
} }
}); });
@ -7970,13 +7968,10 @@ StageMorph.prototype.stepGenericConditions = function (stopAll) {
}; };
StageMorph.prototype.developersMenu = function () { StageMorph.prototype.developersMenu = function () {
var myself = this, var menu = StageMorph.uber.developersMenu.call(this);
menu = StageMorph.uber.developersMenu.call(this);
menu.addItem( menu.addItem(
"stop", "stop",
function () { () => this.threads.stopAll(),
myself.threads.stopAll();
},
'terminate all running threads' 'terminate all running threads'
); );
return menu; return menu;
@ -8043,8 +8038,7 @@ StageMorph.prototype.processKeyEvent = function (event, action) {
StageMorph.prototype.fireKeyEvent = function (key) { StageMorph.prototype.fireKeyEvent = function (key) {
var evt = key.toLowerCase(), var evt = key.toLowerCase(),
procs = [], procs = [],
ide = this.parentThatIsA(IDE_Morph), ide = this.parentThatIsA(IDE_Morph);
myself = this;
this.keysPressed[evt] = true; this.keysPressed[evt] = true;
if (evt === 'ctrl enter' && !ide.isAppMode) { if (evt === 'ctrl enter' && !ide.isAppMode) {
@ -8084,10 +8078,10 @@ StageMorph.prototype.fireKeyEvent = function (key) {
if (evt === 'esc' && !ide.isAppMode) { if (evt === 'esc' && !ide.isAppMode) {
return this.fireStopAllEvent(); return this.fireStopAllEvent();
} }
this.children.concat(this).forEach(function (morph) { this.children.concat(this).forEach(morph => {
if (isSnapObject(morph)) { if (isSnapObject(morph)) {
morph.allHatBlocksForKey(evt).forEach(function (block) { morph.allHatBlocksForKey(evt).forEach(block => {
procs.push(myself.threads.startProcess( procs.push(this.threads.startProcess(
block, block,
morph, morph,
true // ignore running scripts, was: myself.isThreadSafe true // ignore running scripts, was: myself.isThreadSafe
@ -8111,17 +8105,16 @@ StageMorph.prototype.inspectKeyEvent
StageMorph.prototype.fireGreenFlagEvent = function () { StageMorph.prototype.fireGreenFlagEvent = function () {
var procs = [], var procs = [],
ide = this.parentThatIsA(IDE_Morph), ide = this.parentThatIsA(IDE_Morph);
myself = this;
this.removeAllClones(); this.removeAllClones();
this.children.concat(this).forEach(function (morph) { this.children.concat(this).forEach(morph => {
if (isSnapObject(morph)) { if (isSnapObject(morph)) {
morph.allHatBlocksFor('__shout__go__').forEach(function (block) { morph.allHatBlocksFor('__shout__go__').forEach(block => {
procs.push(myself.threads.startProcess( procs.push(this.threads.startProcess(
block, block,
morph, morph,
myself.isThreadSafe this.isThreadSafe
)); ));
}); });
} }
@ -8133,8 +8126,7 @@ StageMorph.prototype.fireGreenFlagEvent = function () {
}; };
StageMorph.prototype.fireStopAllEvent = function () { StageMorph.prototype.fireStopAllEvent = function () {
var ide = this.parentThatIsA(IDE_Morph), var ide = this.parentThatIsA(IDE_Morph);
myself = this;
this.threads.resumeAll(this.stage); this.threads.resumeAll(this.stage);
@ -8144,7 +8136,7 @@ StageMorph.prototype.fireStopAllEvent = function () {
this.keysPressed = {}; this.keysPressed = {};
this.threads.stopAll(); this.threads.stopAll();
this.stopAllActiveSounds(); this.stopAllActiveSounds();
this.children.forEach(function (morph) { this.children.forEach(morph => {
if (morph.stopTalking) { if (morph.stopTalking) {
morph.stopTalking(); morph.stopTalking();
} }
@ -8153,9 +8145,9 @@ StageMorph.prototype.fireStopAllEvent = function () {
if (ide) { if (ide) {
ide.nextSteps([ ide.nextSteps([
nop, nop,
function () {myself.stopAllActiveSounds(); }, // catch forever loops () => this.stopAllActiveSounds(), // catch forever loops
function () {myself.stopProjection(); }, () => this.stopProjection(),
function () {ide.controlBar.pauseButton.refresh(); } () => ide.controlBar.pauseButton.refresh()
]); ]);
} }
}; };
@ -8164,7 +8156,7 @@ StageMorph.prototype.runStopScripts = function () {
// experimental: Allow each sprite to run one last step before termination // experimental: Allow each sprite to run one last step before termination
// usage example: Stop a robot or device associated with the sprite // usage example: Stop a robot or device associated with the sprite
this.receiveUserInteraction('stopped', true, true); this.receiveUserInteraction('stopped', true, true);
this.children.forEach(function (morph) { this.children.forEach(morph => {
if (morph instanceof SpriteMorph) { if (morph instanceof SpriteMorph) {
morph.receiveUserInteraction('stopped', true, true); morph.receiveUserInteraction('stopped', true, true);
} }
@ -8172,14 +8164,11 @@ StageMorph.prototype.runStopScripts = function () {
}; };
StageMorph.prototype.removeAllClones = function () { StageMorph.prototype.removeAllClones = function () {
var myself = this, var clones = this.children.filter(
clones = this.children.filter( morph => morph instanceof SpriteMorph && morph.isTemporary
function (morph) {
return morph instanceof SpriteMorph && morph.isTemporary;
}
); );
clones.forEach(function (clone) { clones.forEach(clone => {
myself.threads.stopAllForReceiver(clone); this.threads.stopAllForReceiver(clone);
clone.detachFromAnchor(); clone.detachFromAnchor();
clone.corpsify(); clone.corpsify();
clone.destroy(); clone.destroy();
@ -8605,7 +8594,7 @@ StageMorph.prototype.blockTemplates = function (category) {
null, null,
myself myself
); );
myself.variables.allNames().forEach(function (name) { myself.variables.allNames().forEach(name => {
menu.addItem( menu.addItem(
name, name,
name, name,
@ -8717,8 +8706,7 @@ StageMorph.prototype.clear = function () {
StageMorph.prototype.userMenu = function () { StageMorph.prototype.userMenu = function () {
var ide = this.parentThatIsA(IDE_Morph), var ide = this.parentThatIsA(IDE_Morph),
menu = new MenuMorph(this), menu = new MenuMorph(this);
myself = this;
if (ide && ide.isAppMode) { if (ide && ide.isAppMode) {
// menu.addItem('help', 'nop'); // menu.addItem('help', 'nop');
@ -8728,18 +8716,13 @@ StageMorph.prototype.userMenu = function () {
menu.addItem("show all", 'showAll'); menu.addItem("show all", 'showAll');
menu.addItem( menu.addItem(
"pic...", "pic...",
function () { () => ide.saveCanvasAs(this.fullImage(), this.name),
ide.saveCanvasAs(
myself.fullImage(),
myself.name
);
},
'open a new window\nwith a picture of the stage' 'open a new window\nwith a picture of the stage'
); );
menu.addLine(); menu.addLine();
menu.addItem( menu.addItem(
'pen trails', 'pen trails',
function () { () => {
var costume = ide.currentSprite.reportPenTrailsAsCostume().copy(); var costume = ide.currentSprite.reportPenTrailsAsCostume().copy();
ide.currentSprite.addCostume(costume); ide.currentSprite.addCostume(costume);
ide.currentSprite.wearCostume(costume); ide.currentSprite.wearCostume(costume);
@ -8763,16 +8746,15 @@ StageMorph.prototype.userMenu = function () {
}; };
StageMorph.prototype.showAll = function () { StageMorph.prototype.showAll = function () {
var myself = this; this.children.forEach(m => {
this.children.forEach(function (m) {
if (m instanceof SpriteMorph) { if (m instanceof SpriteMorph) {
if (!m.anchor) { if (!m.anchor) {
m.show(); m.show();
m.keepWithin(myself); m.keepWithin(this);
} }
} else { } else {
m.show(); m.show();
m.keepWithin(myself); m.keepWithin(this);
if (m.fixLayout) {m.fixLayout(); } if (m.fixLayout) {m.fixLayout(); }
} }
}); });
@ -8795,8 +8777,7 @@ StageMorph.prototype.fancyThumbnail = function (
nonRetina, nonRetina,
recycleMe recycleMe
) { ) {
var myself = this, var src = this.getImage(),
src = this.getImage(),
scale = Math.min( scale = Math.min(
(extentPoint.x / src.width), (extentPoint.x / src.width),
(extentPoint.y / src.height) (extentPoint.y / src.height)
@ -8832,15 +8813,15 @@ StageMorph.prototype.fancyThumbnail = function (
); );
ctx.restore(); ctx.restore();
} }
this.children.forEach(function (morph) { this.children.forEach(morph => {
if (morph.isVisible && (morph !== excludedSprite)) { if (morph.isVisible && (morph !== excludedSprite)) {
fb = morph.fullBounds(); fb = morph.fullBounds();
fimg = morph.fullImage(); fimg = morph.fullImage();
if (fimg.width && fimg.height) { if (fimg.width && fimg.height) {
ctx.drawImage( ctx.drawImage(
morph.fullImage(), morph.fullImage(),
fb.origin.x - myself.bounds.origin.x, fb.origin.x - this.bounds.origin.x,
fb.origin.y - myself.bounds.origin.y fb.origin.y - this.bounds.origin.y
); );
} }
} }