Skip to content

Commit 25bb09d

Browse files
committed
update
1 parent 512a9fc commit 25bb09d

File tree

1 file changed

+78
-14
lines changed

1 file changed

+78
-14
lines changed

src/addons/addons/blocks2text/userscript.js

+78-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default async function ({ addon, console, msg }) {
44
const debug = console.log;
55
const OnlyEditingTarget = addon.settings.get("target");
66

7-
let inputsOPs = ['math_number', 'math_positive_number', "math_whole_number", 'motion_goto_menu', "text"]
7+
let inputsOPs = ['math_number', 'math_positive_number', "math_whole_number", 'motion_goto_menu', "text", "argument_reporter_string_number"]
88
let ignoreBlocks = ["procedures_prototype"]
99
let Cblocks = ["control_forever", "control_if", "control_repeat", "control_if_else", "control_repeat_until", "control_while"]
1010
let preventUpdate = false;
@@ -72,25 +72,25 @@ export default async function ({ addon, console, msg }) {
7272
var inputBlock = blocks[input]//block that has input info
7373
var nextInput = null
7474
function getInputValue(block) {
75-
if (block==undefined){
75+
if (block == undefined) {
7676
return
7777
}
7878
let inputOpcode = block.opcode
7979
let v
80-
if (inputsOPs.includes(inputOpcode)){
80+
if (inputsOPs.includes(inputOpcode)) {
8181
//virtual block, has value
8282
let fieldValue = block.fields
8383
for (let fl in fieldValue) {
8484
v = convertInput(inputOpcode, fieldValue[fl].value)
8585
}
8686
nextInput = null
87-
87+
8888
return v
89-
}else{
89+
} else {
9090
//inner block
9191
let inputs = block.inputs
92-
for (let IN in inputs){
93-
nextInput = inputs[IN].block
92+
for (let IN in inputs) {
93+
nextInput = inputs[IN].block
9494
}
9595
v = "(" + inputOpcode
9696

@@ -100,19 +100,19 @@ export default async function ({ addon, console, msg }) {
100100
value = getInputValue(inputBlock)
101101
let closeN = 0
102102
let InCouter = 10 //max inputs
103-
while (1){
104-
if (nextInput != null){
103+
while (1) {
104+
if (nextInput != null) {
105105
//return value
106-
closeN ++
106+
closeN++
107107
value += getInputValue(blocks[nextInput])
108-
}else{
108+
} else {
109109
value += ")".repeat(closeN)
110110
return value
111111
}
112-
if (!InCouter){
112+
if (!InCouter) {
113113
return value
114114
}
115-
InCouter --
115+
InCouter--
116116
}
117117
}
118118
function handleBlockDefinition(block, sprite) {
@@ -121,15 +121,78 @@ export default async function ({ addon, console, msg }) {
121121
let mutation = blocks[prototype].mutation
122122
return "function " + mutation.proccode + "(" + "){"
123123
}
124+
function handleSubstack(_block, _sprite) {
125+
let blocks = _sprite.blocks._blocks;//blocks in sprite
126+
let text = ""
127+
let block = _block
128+
let a = 4
129+
let substacks = new Array()
130+
let substack = -1
131+
while (1) {
132+
debug(block)
133+
let opcode = block.opcode
134+
var id = block.id
135+
let next = block.next
136+
let afterC
137+
if (Cblocks.includes(opcode)) {//C block
138+
if ("SUBSTACK" in block.inputs) {
139+
substack++//increase counter
140+
text += "\t".repeat(substack) + opcode + "{\r\n"
141+
substacks[substack] = new Object //create new object for each substack
142+
substacks[substack].inside = block.inputs["SUBSTACK"].block//get some values in object
143+
substacks[substack].next = block.next
144+
afterC = block.next
145+
next = block.inputs["SUBSTACK"].block//get next block.id
146+
147+
}
148+
} else {//normal block
149+
150+
text += "\t".repeat(substack) + opcode + "\r\n"
151+
debug(opcode)
152+
debug(substacks)
153+
debug(substack)
154+
/*if (substacks[substack].next == id) {
155+
text += "\r\n"+"\t".repeat(substack)+"}"
156+
next = substacks[substack].next
157+
substack --
158+
}else{*/
159+
if (next != undefined) {//next block
160+
next = block.next
161+
//debug("next: "+next)
162+
} else {//return after C
163+
next = substacks[substack].next
164+
//debug("next: "+block.next)
165+
text += "\r\n" + "\t".repeat(substack) + "}"
166+
substack--
167+
if (substack === 1) {
168+
break
169+
//}
170+
}
171+
}
172+
173+
}
174+
if (!a) {
175+
break
176+
}
177+
if (next != undefined) {
178+
block = blocks[next] //get block[id]
179+
}
180+
a--
181+
}
182+
183+
debug(text)
184+
return text
185+
}
124186
function printText() {
125187
const editingTarget = vm.runtime.getEditingTarget();
188+
//debug(vm)
126189
//let sprite = new Set(vm.runtime.targets.map((i) => i.sprite))
127190
let sprite = new Set(vm.runtime.targets)
128191
//debug(vm.runtime.targets)
129192
//const editingTarget = vm.runtime.getEditingTarget();
130193
//debug(editingTarget);
131194
//debug(sprite)
132-
if (OnlyEditingTarget){
195+
if (OnlyEditingTarget) {
133196
sprite = [editingTarget]
134197
}
135198
sprite.forEach((_sprite, i) => {//sprites
@@ -169,6 +232,7 @@ export default async function ({ addon, console, msg }) {
169232
}
170233
text += blockOpcode
171234
if (Cblocks.includes(blockOpcode)) {
235+
handleSubstack(block, _sprite)
172236
text += "{"
173237
if (!"SUBSTACK" in block.inputs) {
174238
text += "\r\n}"

0 commit comments

Comments
 (0)