Skip to content

Commit 32b054f

Browse files
authored
Merge pull request #832 from rgantzos/select-self
Select self
2 parents 23b500b + 100e4a4 commit 32b054f

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

features/features.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
[
2+
{
3+
"version": 2,
4+
"id": "select-self",
5+
"versionAdded": "v4.0.0"
6+
},
27
{
38
"version": 2,
49
"id": "sidebar",

features/select-self/data.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"title": "Select Self",
3+
"description": "Allows you to select the current sprite when opening sprite dropdowns, instead of only other sprites.",
4+
"scripts": [
5+
{
6+
"file": "script.js",
7+
"runOn": "/projects/*"
8+
}
9+
],
10+
"credits": [
11+
{
12+
"url": "https://scratch.mit.edu/users/rgantzos/",
13+
"username": "rgantzos"
14+
}
15+
],
16+
"type": [
17+
"Editor"
18+
],
19+
"dynamic": true
20+
}

features/select-self/script.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
export default async function ({ feature, console }) {
2+
3+
let MENU_TYPES = ["motion_glideto_menu", "motion_goto_menu", "motion_pointtowards_menu", "sensing_touchingobjectmenu", "sensing_of_object_menu"]
4+
5+
let blocks = []
6+
7+
ScratchTools.waitForElements(
8+
"g.blocklyDraggable > g[data-shapes='argument round']",
9+
function (block) {
10+
if (!Blockly) return;
11+
12+
block = Blockly.getMainWorkspace().getBlockById(block.dataset.id);
13+
if (!block) return;
14+
15+
if (MENU_TYPES.includes(block.type)) {
16+
let menu = block.inputList[0].fieldRow[0].menuGenerator_;
17+
18+
if (!blocks.includes(block.id)) {
19+
blocks.push(block.id)
20+
}
21+
22+
updateMenu(block.id)
23+
}
24+
}
25+
);
26+
27+
feature.traps.vm.on("targetsUpdate", function (el) {
28+
for (var i in blocks) {
29+
updateMenu(blocks[i])
30+
}
31+
})
32+
33+
feature.addEventListener("disabled", function () {
34+
for (var i in blocks) {
35+
updateMenu(blocks[i])
36+
}
37+
})
38+
39+
feature.addEventListener("reenabled", function () {
40+
for (var i in blocks) {
41+
updateMenu(blocks[i])
42+
}
43+
})
44+
45+
function updateMenu(blockId) {
46+
let SPRITES = []
47+
48+
let targets = feature.traps.vm.runtime.targets.filter((target) => !target.isStage)
49+
50+
for (var i in targets) {
51+
SPRITES.push(targets[i].sprite.name)
52+
}
53+
54+
let block = Blockly.getMainWorkspace().getBlockById(blockId);
55+
56+
if (!block) return;
57+
58+
block.inputList[0].fieldRow[0].menuGenerator_ = function () {
59+
let data = [
60+
[
61+
"random position",
62+
"_random_"
63+
],
64+
[
65+
"mouse-pointer",
66+
"_mouse_"
67+
]
68+
]
69+
70+
for (var i in SPRITES) {
71+
if (feature.self.enabled || feature.traps.vm.runtime._editingTarget?.sprite?.name !== SPRITES[i]) {
72+
data.push([SPRITES[i], SPRITES[i]])
73+
}
74+
}
75+
76+
return data
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)