Skip to content

Commit

Permalink
Individual target pausing support
Browse files Browse the repository at this point in the history
  • Loading branch information
AshimeeAlt committed Nov 6, 2024
1 parent 72f43c9 commit 510240e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,16 @@ class Runtime extends EventEmitter {
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

_sortThreadsToTarget() {

Check failure on line 2256 in src/engine/runtime.js

View workflow job for this annotation

GitHub Actions / build

Missing space before function parentheses
const targets = Object.create(null);

Check failure on line 2257 in src/engine/runtime.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 8 spaces but found 6
for (let i = this.threads.length - 1; i > -1; i--) {

Check failure on line 2258 in src/engine/runtime.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 8 spaces but found 6
const thread = this.threads[i];

Check failure on line 2259 in src/engine/runtime.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 12 spaces but found 10
targets[thread.target.id] ??= { target: thread.target, threads: [] };

Check failure on line 2260 in src/engine/runtime.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 12 spaces but found 10

Check failure on line 2260 in src/engine/runtime.js

View workflow job for this annotation

GitHub Actions / build

There should be no space after '{'
targets[thread.target.id].threads.push(thread);
}
return targets;
}

/**
* Set the "paused" status of the current project.
* @param {boolean} status The pause status of the project.
Expand All @@ -2261,20 +2271,29 @@ class Runtime extends EventEmitter {
status = status || false;
const didChange = this.paused !== status;
this.paused = status;
const targets = Object.values(this._sortThreadsToTarget());
// Pause all the targets with RUNNING threads
for (let i = targets.length - 1; i > -1; i--) {
const target = targets[i];
if (target.target.paused === status) continue;
target.target.setPause(status, target.threads);
}
// The for loop above will miss any target without any threads running
// So we iterate over all the targets and pause them
for (let i = this.targets.length - 1; i > -1; i--) {
// If its paused then we can assume its threads are paused
if (this.targets[i].paused === status) continue;
// Unlike the above for loop we don't have a list of threads to pause
// so we pass an empty array, it is safe to assume there are no threads unpaused
// after the above for loop
this.targets[i].setPause(status, []);
}
if (status) {
if (!this.ioDevices.clock._paused) {
this.ioDevices.clock.pause();
}
this.audioEngine.audioContext.suspend();
for (let i = this.threads.length - 1; i > -1; i--) {
if (this.threads[i].status === 5 /* STATUS_PAUSED */) continue;
this._pauseThread(this.threads[i]);
}
} else if (didChange) {
for (let i = this.threads.length - 1; i > -1; i--) {
if (this.threads[i].status !== 5 /* STATUS_PAUSED */) continue;
this._pauseThread(this.threads[i]);
}
this.audioEngine.audioContext.resume();
this.ioDevices.clock.resume();
}
Expand Down Expand Up @@ -2782,8 +2801,8 @@ class Runtime extends EventEmitter {
* Start all threads that start with the green flag.
*/
greenFlag () {
// pausing is done in stopAll
this.stopAll();
this.setPause(false);
this.emit(Runtime.PROJECT_START);
this.updateCurrentMSecs();
this.ioDevices.clock.resetProjectTimer();
Expand Down Expand Up @@ -2821,6 +2840,7 @@ class Runtime extends EventEmitter {
// Remove all remaining threads from executing in the next tick.
this.threads = [];
this.threadMap.clear();
this.setPause(false);

this.resetRunId();
}
Expand Down Expand Up @@ -3272,7 +3292,7 @@ class Runtime extends EventEmitter {
storeProjectOptions () {
const options = this.generateDifferingProjectOptions();
// TODO: translate
const text = `Configuration for https://turbowarp.org/\nYou can move, resize, and minimize this comment, but don't edit it by hand. This comment can be deleted to remove the stored settings.\n${ExtendedJSON.stringify(options)}${COMMENT_CONFIG_MAGIC}`;
const text = `Configuration for https://alpha.unsandboxed.org/\nYou can move, resize, and minimize this comment, but don't edit it by hand. This comment can be deleted to remove the stored settings.\n${ExtendedJSON.stringify(options)}${COMMENT_CONFIG_MAGIC}`;
const existingComment = this.findProjectOptionsComment();
if (existingComment) {
existingComment.text = text;
Expand Down Expand Up @@ -3729,7 +3749,7 @@ class Runtime extends EventEmitter {
label: extensionBlockInfo.text
};
}

// TODO: we may want to format the label in a locale-specific way.
return {
category: 'extension', // This assumes that all extensions have the same monitor color.
Expand Down
31 changes: 31 additions & 0 deletions src/engine/target.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@ class Target extends EventEmitter {
* @type {Object.<string, object>}
*/
this.extensionStorage = {};

/**
* Pause status of this target
*/
this.paused = this.runtime.paused;
}

/**
* Sets the "paused" status of this target
* @param {boolean} status The pause status of this sprite.
* @param {Array<import('./thread.js')>} threads An array of threads to pause,
(Note these must be owned by the sprite)
*/
setPause(status, threads) {
status = status || false;
const statusChanged = status !== this.paused;
this.paused = status;
threads = threads || this.runtime.threads;
if (status) {
for (let i = threads.length - 1; i > -1; i--) {
if (threads[i].status === 5 /* STATUS_PAUSED */) continue;
if (threads[i].target.id !== this.id) continue;
this.runtime._pauseThread(threads[i]);
}
} else if (statusChanged) {
for (let i = threads.length - 1; i > -1; i--) {
if (threads[i].status !== 5 /* STATUS_PAUSED */) continue;
if (threads[i].target.id !== this.id) continue;
this.runtime._pauseThread(threads[i]);
}
}
}

/**
Expand Down

0 comments on commit 510240e

Please sign in to comment.