Skip to content

Commit a648ce6

Browse files
authored
Merge pull request #130 from Nitro-Bolt/around-the-world
Global Procedures (scratch-gui)
2 parents f84b22e + fc4c8b8 commit a648ce6

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

src/components/custom-procedures/custom-procedures.jsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,19 @@ const CustomProcedures = props => {
228228
/>
229229
</label>
230230
</div>
231+
<div className={styles.checkboxRow}>
232+
<label>
233+
<FancyCheckbox
234+
checked={props.global}
235+
onChange={props.onToggleGlobal}
236+
/>
237+
<FormattedMessage
238+
defaultMessage="For all sprites"
239+
description="Label for checkbox to toggle availability for all sprites"
240+
id="gui.customProcedures.forAllSprites"
241+
/>
242+
</label>
243+
</div>
231244
<Box className={styles.buttonRow}>
232245
<button
233246
className={styles.cancelButton}
@@ -265,7 +278,9 @@ CustomProcedures.propTypes = {
265278
onCancel: PropTypes.func.isRequired,
266279
onOk: PropTypes.func.isRequired,
267280
onToggleWarp: PropTypes.func.isRequired,
268-
warp: PropTypes.bool.isRequired
281+
warp: PropTypes.bool.isRequired,
282+
onToggleGlobal: PropTypes.func.isRequired,
283+
global: PropTypes.bool.isRequired
269284
};
270285

271286
export default injectIntl(CustomProcedures);

src/containers/blocks.jsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,36 @@ class Blocks extends React.Component {
168168
Blocks.defaultOptions
169169
);
170170
this.workspace = this.ScratchBlocks.inject(this.blocks, workspaceConfig);
171+
172+
if (this.ScratchBlocks.Procedures &&
173+
this.ScratchBlocks.Procedures.setProcedureBlocksAcrossTargetsCallback) {
174+
this.ScratchBlocks.Procedures.setProcedureBlocksAcrossTargetsCallback(() => {
175+
const runtime = this.props.vm && this.props.vm.runtime;
176+
if (!runtime || !runtime.targets) {
177+
return [];
178+
}
179+
180+
const procedureBlocks = [];
181+
for (let i = 0; i < runtime.targets.length; i++) {
182+
const target = runtime.targets[i];
183+
const blocks = target && target.blocks && target.blocks._blocks;
184+
if (!blocks) continue;
185+
186+
for (const blockId in blocks) {
187+
if (!Object.prototype.hasOwnProperty.call(blocks, blockId)) continue;
188+
const block = blocks[blockId];
189+
if (!block) continue;
190+
if (
191+
block.opcode === 'procedures_prototype' ||
192+
block.opcode === this.ScratchBlocks.PROCEDURES_CALL_BLOCK_TYPE
193+
) {
194+
procedureBlocks.push(block);
195+
}
196+
}
197+
}
198+
return procedureBlocks;
199+
});
200+
}
171201
AddonHooks.blocklyWorkspace = this.workspace;
172202

173203
// Register buttons under new callback keys for creating variables,
@@ -298,6 +328,12 @@ class Blocks extends React.Component {
298328
componentWillUnmount () {
299329
this.detachVM();
300330
this.unmounted = true;
331+
332+
if (this.ScratchBlocks && this.ScratchBlocks.Procedures &&
333+
this.ScratchBlocks.Procedures.setProcedureBlocksAcrossTargetsCallback) {
334+
this.ScratchBlocks.Procedures.setProcedureBlocksAcrossTargetsCallback(() => []);
335+
}
336+
301337
this.workspace.dispose();
302338
clearTimeout(this.toolboxUpdateTimeout);
303339

@@ -515,6 +551,29 @@ class Blocks extends React.Component {
515551
}
516552
log.error(error);
517553
}
554+
555+
const allBlocks = this.workspace.getAllBlocks(false);
556+
for (let i = 0; i < allBlocks.length; i++) {
557+
const block = allBlocks[i];
558+
if (block.type === this.ScratchBlocks.PROCEDURES_CALL_BLOCK_TYPE && block.getProcCode) {
559+
const procCode = block.getProcCode();
560+
if (procCode) {
561+
// Get the latest global procedure mutation from the provider
562+
const globalMutations = this.ScratchBlocks.Procedures.allGlobalProcedureMutations(this.workspace);
563+
for (let j = 0; j < globalMutations.length; j++) {
564+
const mutation = globalMutations[j];
565+
if (this.ScratchBlocks.Names.equals(mutation.getAttribute('proccode'), procCode)) {
566+
// Found the matching procedure, update the call block with the latest mutation
567+
if (block.domToMutation) {
568+
block.domToMutation(mutation);
569+
}
570+
break;
571+
}
572+
}
573+
}
574+
}
575+
}
576+
518577
this.workspace.addChangeListener(this.props.vm.blockListener);
519578

520579
if (this.props.vm.editingTarget && this.props.workspaceMetrics.targets[this.props.vm.editingTarget.id]) {

src/containers/custom-procedures.jsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class CustomProcedures extends React.Component {
1414
'handleAddInput',
1515
'handleAddColor',
1616
'handleToggleWarp',
17+
'handleToggleGlobal',
1718
'handlePropagation',
1819
'handleInputMenuChange',
1920
'handleCancel',
@@ -23,8 +24,9 @@ class CustomProcedures extends React.Component {
2324
this.state = {
2425
rtlOffset: 0,
2526
warp: false,
26-
colour: '#000000',
27-
menuInput: 'stringornumber'
27+
global: false,
28+
colour: "#000000",
29+
menuInput: "stringornumber"
2830
};
2931
}
3032
componentWillUnmount () {
@@ -108,7 +110,11 @@ class CustomProcedures extends React.Component {
108110
this.mutationRoot.domToMutation(this.props.mutator);
109111
this.mutationRoot.initSvg();
110112
this.mutationRoot.render();
111-
this.setState({warp: this.mutationRoot.getWarp(), colour: this.mutationRoot.colour_});
113+
this.setState({
114+
warp: this.mutationRoot.getWarp(),
115+
global: this.mutationRoot.getGlobal(),
116+
colour: this.mutationRoot.colour_
117+
});
112118
// Allow the initial events to run to position this block, then focus.
113119
setTimeout(() => {
114120
this.mutationRoot.focusLastEditor_();
@@ -158,6 +164,13 @@ class CustomProcedures extends React.Component {
158164
this.setState({warp: newWarp});
159165
}
160166
}
167+
handleToggleGlobal () {
168+
if (this.mutationRoot) {
169+
const newGlobal = !this.mutationRoot.getGlobal();
170+
this.mutationRoot.setGlobal(newGlobal);
171+
this.setState({global: newGlobal});
172+
}
173+
}
161174
handlePropagation (e) {
162175
e.stopPropagation();
163176
}
@@ -168,6 +181,7 @@ class CustomProcedures extends React.Component {
168181
return (
169182
<CustomProceduresComponent
170183
componentRef={this.setBlocks}
184+
global={this.state.global}
171185
warp={this.state.warp}
172186
colour={this.state.colour}
173187
onAddInput={this.handleAddInput}
@@ -179,6 +193,7 @@ class CustomProcedures extends React.Component {
179193
onCancel={this.handleCancel}
180194
onOk={this.handleOk}
181195
onToggleWarp={this.handleToggleWarp}
196+
onToggleGlobal={this.handleToggleGlobal}
182197
/>
183198
);
184199
}

0 commit comments

Comments
 (0)