Skip to content

Commit

Permalink
Update docs examples (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin authored Aug 16, 2023
1 parent a6fddd8 commit 615d78d
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 8 deletions.
1 change: 0 additions & 1 deletion extensions/docs-examples/unsandboxed/broadcast-5.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
opcode: 'broadcast',
blockType: Scratch.BlockType.REPORTER,
text: 'broadcast [EVENT]',
disableMonitor: true,
arguments: {
EVENT: {
type: Scratch.ArgumentType.STRING,
Expand Down
8 changes: 1 addition & 7 deletions extensions/docs-examples/unsandboxed/every-second.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
opcode: 'everySecond',
blockType: Scratch.BlockType.HAT,
text: 'every second',
isEdgeActivated: false,
arguments: {
EVENT_OPTION: {
type: Scratch.ArgumentType.STRING,
menu: 'EVENT_FIELD'
}
}
isEdgeActivated: false
}
]
};
Expand Down
55 changes: 55 additions & 0 deletions extensions/docs-examples/unsandboxed/when-key-pressed-restart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(function(Scratch) {
'use strict';

if (!Scratch.extensions.unsandboxed) {
throw new Error('This example must run unsandboxed');
}

class WhenKeyPressed {
getInfo() {
return {
id: 'restartexampleunsandboxed',
name: 'Restart Threads Example',
blocks: [
{
blockType: Scratch.BlockType.EVENT,
opcode: 'whenPressed',
text: 'when [KEY] key pressed',
isEdgeActivated: false,
// highlight-next-line
shouldRestartExistingThreads: true,
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
menu: 'key'
}
}
}
],
menus: {
key: {
acceptReporters: false,
items: [
{
text: 'space',
value: ' '
},
'a',
'b',
'c',
// ...
]
}
}
};
}
}

document.addEventListener('keydown', (e) => {
Scratch.vm.runtime.startHats('restartexampleunsandboxed_whenPressed', {
KEY: e.key
});
});

Scratch.extensions.register(new WhenKeyPressed());
})(Scratch);
54 changes: 54 additions & 0 deletions extensions/docs-examples/unsandboxed/when-key-pressed-stage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(function(Scratch) {
'use strict';

if (!Scratch.extensions.unsandboxed) {
throw new Error('This example must run unsandboxed');
}

class WhenKeyPressedInStage {
getInfo() {
return {
id: 'eventexample3unsandboxed',
name: 'Event Block Example 3',
blocks: [
{
blockType: Scratch.BlockType.EVENT,
opcode: 'whenPressed',
text: 'when [KEY] key pressed',
isEdgeActivated: false,
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
menu: 'key'
}
}
}
],
menus: {
key: {
acceptReporters: false,
items: [
{
text: 'space',
value: ' '
},
'a',
'b',
'c',
// ...
]
}
}
};
}
}

document.addEventListener('keydown', (e) => {
Scratch.vm.runtime.startHats('eventexample3unsandboxed_whenPressed', {
KEY: e.key
// highlight-next-line
}, Scratch.vm.runtime.getTargetForStage());
});

Scratch.extensions.register(new WhenKeyPressedInStage());
})(Scratch);
58 changes: 58 additions & 0 deletions extensions/docs-examples/unsandboxed/when-key-pressed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(function(Scratch) {
'use strict';

if (!Scratch.extensions.unsandboxed) {
throw new Error('This example must run unsandboxed');
}

class WhenKeyPressed {
getInfo() {
return {
id: 'eventexample2unsandboxed',
name: 'Event Block Example 2',
blocks: [
{
blockType: Scratch.BlockType.EVENT,
opcode: 'whenPressed',
text: 'when [KEY] key pressed',
isEdgeActivated: false, // required boilerplate
// highlight-start
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
menu: 'key'
}
}
// highlight-end
}
],
menus: {
key: {
acceptReporters: false,
items: [
{
// startHats filters by *value*, not by text
text: 'space',
value: ' '
},
'a',
'b',
'c',
// ...
]
}
}
};
}
}

document.addEventListener('keydown', (e) => {
// highlight-start
Scratch.vm.runtime.startHats('eventexample2unsandboxed_whenPressed', {
KEY: e.key
});
// highlight-end
});

Scratch.extensions.register(new WhenKeyPressed());
})(Scratch);
37 changes: 37 additions & 0 deletions extensions/docs-examples/unsandboxed/when-space-key-pressed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(function(Scratch) {
'use strict';

if (!Scratch.extensions.unsandboxed) {
throw new Error('This example must run unsandboxed');
}

class WhenSpaceKeyPressed {
getInfo() {
return {
id: 'eventexampleunsandboxed',
name: 'Event Block Example',
blocks: [
// highlight-start
{
blockType: Scratch.BlockType.EVENT,
opcode: 'whenSpacePressed',
text: 'when space key pressed',
isEdgeActivated: false // required boilerplate
}
// highlight-end
]
};
}
// Notice: whenSpacePressed does not have a function defined!
}

// highlight-start
document.addEventListener('keydown', (e) => {
if (e.key === ' ') {
Scratch.vm.runtime.startHats('eventexampleunsandboxed_whenSpacePressed');
}
});
// highlight-end

Scratch.extensions.register(new WhenSpaceKeyPressed());
})(Scratch);
45 changes: 45 additions & 0 deletions extensions/docs-examples/unsandboxed/when.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(function(Scratch) {
'use strict';

if (!Scratch.extensions.unsandboxed) {
throw new Error('This example must run unsandboxed');
}

class When {
getInfo() {
return {
id: 'whenunsandboxed',
name: 'When',
blocks: [
{
// highlight-start
blockType: Scratch.BlockType.HAT,
opcode: 'when',
text: 'when [CONDITION]',
isEdgeActivated: false, // required boilerplate
arguments: {
CONDITION: {
type: Scratch.BlockType.BOOLEAN
}
}
// highlight-end
}
]
};
}
// highlight-start
when(args) {
return Scratch.Cast.toBoolean(args.CONDITION);
}
// highlight-end
}

// highlight-start
Scratch.vm.runtime.on('BEFORE_EXECUTE', () => {
// startHats is the same as before!
Scratch.vm.runtime.startHats('whenunsandboxed_when');
});
// highlight-end

Scratch.extensions.register(new When());
})(Scratch);

0 comments on commit 615d78d

Please sign in to comment.