turtlestitch/stitchcode/threads.js

134 wiersze
4.1 KiB
JavaScript
Czysty Zwykły widok Historia

2018-11-24 12:06:07 +00:00
2022-12-06 09:42:19 +00:00
Process.prototype.proxy = 'https://cors-proxy.turtlestitch.org';
2021-07-12 15:48:40 +00:00
Process.prototype.enableJS = true;
2018-11-24 12:06:07 +00:00
2017-02-28 21:26:29 +00:00
Process.prototype.reportMouseX = function () {
var stage, world;
if (this.homeContext.receiver) {
stage = this.homeContext.receiver.parentThatIsA(StageMorph);
if (stage) {
world = stage.world();
if (world) {
return (
(world.hand.position().x - stage.center().x)
/ stage.camera.zoomFactor
/ stage.scale
* 2
+ stage.controls.center.x
);
2017-02-28 21:26:29 +00:00
}
}
}
return 0;
};
Process.prototype.reportMouseY = function () {
var stage, world;
if (this.homeContext.receiver) {
stage = this.homeContext.receiver.parentThatIsA(StageMorph);
if (stage) {
world = stage.world();
if (world) {
return (
(stage.center().y - world.hand.position().y)
/ stage.camera.zoomFactor
/ stage.scale
* 2
+ stage.controls.center.y
);
2017-02-28 21:26:29 +00:00
}
}
}
return 0;
};
Process.prototype.zoomToFit = function() {
stage = this.homeContext.receiver.parentThatIsA(StageMorph);
if (stage) {
stage.camera.fitScene();
}
}
2018-10-24 08:31:05 +00:00
Process.prototype.reportPi = function (min, max) {
return Math.PI;
};
2022-10-05 15:12:41 +00:00
// Process URI retrieval (interpolated)
Process.prototype.reportURL = function (url) {
var response;
if (!this.httpRequest) {
// use the location protocol unless the user specifies otherwise
if (url.indexOf('//') < 0 || url.indexOf('//') > 8) {
if (location.protocol === 'file:') {
// allow requests from locally loaded sources
url = 'https://' + url;
} else {
url = location.protocol + '//' + url;
}
}
this.httpRequest = new XMLHttpRequest();
this.httpRequest.open("GET", url, true);
// cache-control, commented out for now
// added for Snap4Arduino but has issues with local robot servers
// this.httpRequest.setRequestHeader('Cache-Control', 'max-age=0');
this.httpRequest.send(null);
if (this.context.isCustomCommand) {
// special case when ignoring the result, e.g. when
// communicating with a robot to control its motors
this.httpRequest = null;
return null;
}
} else if (this.httpRequest.readyState === 4) {
response = this.httpRequest.responseText;
this.httpRequest = null;
return response;
}
this.pushContext('doYield');
this.pushContext();
};
2018-11-24 12:06:07 +00:00
Process.prototype.reportProxiedURL = function (url) {
return this.reportURL(this.proxy + '/' + url);
};
Process.prototype.origReportDistanceTo = Process.prototype.reportDistanceTo;
Process.prototype.reportDistanceTo = function (name) {
var thisObj = this.blockReceiver();
if (thisObj && this.inputOption(name) === 'mouse-pointer') {
2022-10-05 15:12:41 +00:00
return new Point(thisObj.xPosition(), thisObj.yPosition()).distanceTo(new Point(this.reportMouseX(), this.reportMouseY()));
} else {
return this.origReportDistanceTo(name);
2021-08-28 11:04:28 +00:00
}
}
2021-08-28 10:40:43 +00:00
Process.prototype.origDoGotoObject = Process.prototype.doGotoObject;
Process.prototype.doGotoObject = function (name) {
var thisObj = this.blockReceiver(),
stage;
2022-10-05 15:12:41 +00:00
if (thisObj && this.inputOption(name) === 'random position') {
2022-10-05 15:12:41 +00:00
stage = thisObj.parentThatIsA(StageMorph);
if (stage) {
2021-08-28 11:04:28 +00:00
thisObj.gotoXY(
this.reportBasicRandom(stage.reportX(stage.left()), stage.reportX(stage.right())),
this.reportBasicRandom(stage.reportY(stage.top()), stage.reportY(stage.bottom()))
);
}
} else {
this.origDoGotoObject(name);
}
};
Process.prototype.reportRandomPosition = function () {
var thisObj = this.blockReceiver(),
stage;
2022-10-05 15:12:41 +00:00
if (thisObj) {
stage = thisObj.parentThatIsA(StageMorph);
return new List([this.reportBasicRandom(stage.reportX(stage.left()), stage.reportX(stage.right())),
2022-10-05 15:12:41 +00:00
this.reportBasicRandom(stage.reportY(stage.top()), stage.reportY(stage.bottom()))]);
}
2022-10-05 15:12:41 +00:00
};