Skip to content

Commit

Permalink
simplify and fix some broadcasts
Browse files Browse the repository at this point in the history
 * fixes https://scratch.mit.edu/projects/304369304
   and other projects where broadcast IDs are inconsistent
 * remove lookupBroadcast, addBroadcast, broadcastReferences
 * sb3: broadcast names used instead of IDs
  • Loading branch information
GarboMuffin committed Apr 24, 2019
1 parent 0388152 commit c72bf86
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 65 deletions.
35 changes: 9 additions & 26 deletions phosphorus.dist.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions phosphorus/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,12 +960,6 @@ namespace P.core {
this.drawChildren(renderer, skip);
}

/**
* Determines the internal ID for a broadcast.
* @param name The name of the broadcast. It's what you see in the Scratch editor.
*/
abstract lookupBroadcast(name: string): string;

// Implement rotatedBounds() to return something.
rotatedBounds() {
return {
Expand Down
11 changes: 6 additions & 5 deletions phosphorus/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ namespace P.runtime {
}

var broadcast = function(name) {
return runtime.trigger('whenIReceive', self.lookupBroadcast(name));
return runtime.trigger('whenIReceive', name);
};

var running = function(bases) {
Expand Down Expand Up @@ -546,7 +546,7 @@ namespace P.runtime {
this.onError = this.onError.bind(this);
}

startThread(sprite: core.Base, base) {
startThread(sprite: core.Base, base: Fn) {
const thread = new Thread(sprite, base, base, [{
args: [],
stack: [{}],
Expand All @@ -567,8 +567,8 @@ namespace P.runtime {
/**
* Triggers an event for a single sprite.
*/
triggerFor(sprite: P.core.Base, event: string, arg?: any): Thread[] {
let threads;
triggerFor(sprite: P.core.Base, event: string, arg?: any): Fn[] {
let threads: Fn[];
switch (event) {
case 'whenClicked': threads = sprite.listeners.whenClicked; break;
case 'whenCloned': threads = sprite.listeners.whenCloned; break;
Expand All @@ -578,6 +578,7 @@ namespace P.runtime {
case 'whenBackdropChanges': threads = sprite.listeners.whenBackdropChanges['' + arg]; break;
case 'whenIReceive':
arg = '' + arg;
// TODO: remove toLowerCase() check?
threads = sprite.listeners.whenIReceive[arg] || sprite.listeners.whenIReceive[arg.toLowerCase()];
break;
default: throw new Error('Unknown trigger event: ' + event);
Expand All @@ -594,7 +595,7 @@ namespace P.runtime {
* Triggers an event on all sprites.
*/
trigger(event: string, arg?: any) {
let threads: Thread[] = [];
let threads: Fn[] = [];
for (let i = this.stage.children.length; i--;) {
threads = threads.concat(this.triggerFor(this.stage.children[i], event, arg));
}
Expand Down
5 changes: 0 additions & 5 deletions phosphorus/sb2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,6 @@ namespace P.sb2 {
private defaultWatcherX = 10;
private defaultWatcherY = 10;

lookupBroadcast(name: string) {
// Scratch 2 uses names as IDs.
return name;
}

lookupVariable(name: string) {
return this.vars[name];
}
Expand Down
31 changes: 8 additions & 23 deletions phosphorus/sb3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,9 @@ namespace P.sb3 {
// Implements a Scratch 3 Stage.
// Adds Scratch 3 specific things such as broadcastReferences
export class Scratch3Stage extends P.core.Stage {
private broadcastReferences: ObjectMap<string> = {};
public variableNames: ObjectMap<string> = {};
public sb3data: SB3Target;

addBroadcast(name: string, id: string) {
this.broadcastReferences[name] = id;
}

lookupBroadcast(name: string) {
// Use the mapped ID or fall back to the name.
// Usually the name is the unique ID, but occasionally it is not.
return this.broadcastReferences[name] || name;
}

lookupVariable(name: string) {
return this.vars[this.variableNames[name]];
}
Expand Down Expand Up @@ -700,11 +689,7 @@ namespace P.sb3 {
target.sb3data = data;

if (target.isStage) {
const stage = target as Scratch3Stage;
for (const id of Object.keys(data.broadcasts)) {
const name = data.broadcasts[id];
stage.addBroadcast(name, id);
}

} else {
const sprite = target as Scratch3Sprite;
sprite.scratchX = data.x;
Expand Down Expand Up @@ -1024,11 +1009,11 @@ namespace P.sb3.compiler {
currentTarget.listeners.whenBackdropChanges[backdrop].push(f);
},
event_whenbroadcastreceived(block, f) {
const optionId = block.fields.BROADCAST_OPTION[1];
if (!currentTarget.listeners.whenIReceive[optionId]) {
currentTarget.listeners.whenIReceive[optionId] = [];
const name = block.fields.BROADCAST_OPTION[0];
if (!currentTarget.listeners.whenIReceive[name]) {
currentTarget.listeners.whenIReceive[name] = [];
}
currentTarget.listeners.whenIReceive[optionId].push(f);
currentTarget.listeners.whenIReceive[name].push(f);
},

// Control
Expand Down Expand Up @@ -1810,7 +1795,7 @@ namespace P.sb3.compiler {
const condition = block.inputs.CONDITION;
const substack = block.inputs.SUBSTACK;
const id = label();
source += 'if (!' + compileExpression(condition) + ') {\n';
source += 'if (!' + compileExpression(condition, 'boolean') + ') {\n';
compileSubstack(substack);
queue(id);
source += '}\n';
Expand Down Expand Up @@ -2404,8 +2389,8 @@ namespace P.sb3.compiler {
return listReference(constant[2]);

case PrimitiveTypes.BROADCAST:
// Similar to variable references.
return compileExpression(constant[2]);
// [type, name, id]
return compileExpression(constant[1]);

case PrimitiveTypes.COLOR_PICKER:
// Colors are stored as strings like "#123ABC", so we must do some conversions to use them as numbers.
Expand Down

0 comments on commit c72bf86

Please sign in to comment.