forked from flipcomputing/flock
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtutorial.js
More file actions
465 lines (434 loc) · 9.13 KB
/
tutorial.js
File metadata and controls
465 lines (434 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import * as Blockly from "blockly";
import { CrossTabCopyPaste } from "@blockly/plugin-cross-tab-copy-paste";
import * as BlockDynamicConnection from "@blockly/block-dynamic-connection";
import { defineBlocks, CustomZelosRenderer } from "./blocks.js";
import { defineBaseBlocks } from "./blocks/base";
import { defineShapeBlocks } from "./blocks/shapes";
import { registerFieldColour } from "@blockly/field-colour";
defineBaseBlocks();
defineBlocks();
defineShapeBlocks();
console.log(Blockly.Blocks['wait']);
// Register the custom renderer
Blockly.registry.register(
Blockly.registry.Type.RENDERER,
"custom_zelos_renderer",
CustomZelosRenderer,
);
// Initialize Blockly workspace
const workspace = Blockly.inject("blocklyDiv", {
theme: Blockly.Themes.Modern,
renderer: "custom_zelos_renderer",
readOnly: true,
toolbox: null,
zoom: {
controls: false,
wheel: false,
startScale: 0.75, //
},
plugins: {
connectionPreviewer: BlockDynamicConnection.decoratePreviewer(),
},
});
const originalShowContextMenu = workspace.showContextMenu_;
workspace.showContextMenu_ = function(e) {
console.log('Blockly context menu triggered');
originalShowContextMenu.call(this, e);
};
workspace.configureContextMenu = function(menuOptions, e) {
// Add custom menu options or modify existing ones
// For example, to add a "Copy" option:
menuOptions.push({
text: 'Copy',
enabled: true,
callback: function() {
// Implement the copy functionality here
},
});
};
// Initialize the CrossTabCopyPaste plugin
const crossTabCopyPaste = new CrossTabCopyPaste();
crossTabCopyPaste.init({ contextMenu: true, shortcut: true });
document.getElementById('copyButton').addEventListener('click', () => {
try {
const topBlock = workspace.getTopBlocks(false)[0]; // Get the first top-level block
if (topBlock) {
// Use the plugin's storage mechanism to copy the block
localStorage.setItem('blocklyStash', JSON.stringify(topBlock.toCopyData()));
alert('Blocks copied to local storage!');
} else {
alert('No blocks available to copy.');
}
} catch (err) {
console.error('Error copying blocks:', err);
alert('Failed to copy blocks.');
}
});
let blocklyJson = {
"type": "wait",
"id": "5CTD]keF.g82U/~Oci~b",
"inputs": {
"DURATION": {
"shadow": {
"type": "math_number",
"id": "rm9RN(41w%N[=3?4[#~N",
"fields": {
"NUM": 1000
}
}
}
}
};
blocklyJson = {
"blocks": {
"languageVersion": 0,
"blocks": [
{
"type": "controls_repeat_ext",
"id": "test-block",
"inputs": {
"TIMES": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 10
}
}
}
}
}
]
}
};
blocklyJson = {
"blocks": {
"languageVersion": 0,
"blocks": [
{
"type": "wait",
"id": "test-block",
"inputs": {
"DURATION": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1000
}
}
}
}
}
]
}
};
/*blocklyJson = {
kind: "block",
type: "start",
inputs: {
DO: {
block: {
type: "load_character",
fields: {
MODELS: "Character1.glb",
ID_VAR: {
name: "player",
type: "",
},
},
inputs: {
SCALE: {
shadow: {
type: "math_number",
fields: {
NUM: 1,
},
},
},
X: {
shadow: {
type: "math_number",
fields: {
NUM: 0,
},
},
},
Y: {
shadow: {
type: "math_number",
fields: {
NUM: 0,
},
},
},
Z: {
shadow: {
type: "math_number",
fields: {
NUM: 0,
},
},
},
HAIR_COLOR: {
shadow: {
type: "colour",
fields: {
COLOR: "#000000",
},
},
},
SKIN_COLOR: {
shadow: {
type: "skin_colour",
fields: {
COLOR: "#a15c33",
},
},
},
EYES_COLOR: {
shadow: {
type: "colour",
fields: {
COLOR: "#000000",
},
},
},
SLEEVES_COLOR: {
shadow: {
type: "colour",
fields: {
COLOR: "#008b8b",
},
},
},
SHORTS_COLOR: {
shadow: {
type: "colour",
fields: {
COLOR: "#00008b",
},
},
},
TSHIRT_COLOR: {
shadow: {
type: "colour",
fields: {
COLOR: "#ff8f60",
},
},
},
},
next: {
block: {
type: "add_physics",
fields: {
MODEL_VAR: {
name: "player",
type: "",
},
PHYSICS_TYPE: "DYNAMIC",
},
next: {
block: {
type: "camera_follow",
fields: {
MESH_VAR: {
name: "player",
type: "",
},
},
inputs: {
RADIUS: {
block: {
type: "math_number",
fields: {
NUM: 7,
},
},
},
},
},
},
},
},
},
},
},
};*/
blocklyJson = {
"blocks": {
"languageVersion": 0,
"blocks": [
{
"type": "wait",
"id": "test-block",
"inputs": {
"DURATION": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1000
}
},
"block": {
"type": "math_number",
"fields": {
"NUM": 2000
}
}
}
}
}
]
}
};
blocklyJson ={
"blocks": {
"languageVersion": 0,
"blocks": [
{
"type": "start",
"id": "hP1(8=@A.+kG[L4(yPaZ",
"inputs": {
"DO": {
"block": {
"type": "set_sky_color",
"id": "LvX7C$m$;68`G_Qvq=pL",
"inputs": {
"COLOR": {
"shadow": {
"type": "colour",
"id": "Mlh8zzfY)p3!M2.q:a(8",
"fields": {
"COLOR": "#6495ed"
}
}
}
},
"next": {
"block": {
"type": "create_ground",
"id": "S%:d.e-l98E_Phu`CHx?",
"inputs": {
"COLOR": {
"shadow": {
"type": "colour",
"id": "Pe/-b7d5B1Qr*:EvoPsA",
"fields": {
"COLOR": "#71bc78"
}
}
}
},
"next": {
"block": {
"type": "print_text",
"id": "dVTxJPUi_sO!AuNY#oor",
"inputs": {
"TEXT": {
"shadow": {
"type": "text",
"id": "=L-/-1HT`]edbp63Mo8Q",
"fields": {
"TEXT": "Hold left mouse button down to look around"
}
}
},
"DURATION": {
"shadow": {
"type": "math_number",
"id": "PAPV5~p4xMSO]ZA,_=pN",
"fields": {
"NUM": 30
}
}
},
"COLOR": {
"shadow": {
"type": "colour",
"id": "w+iegX|E(VYe9KaiAi;Q",
"fields": {
"COLOR": "#000080"
}
}
}
},
"next": {
"block": {
"type": "print_text",
"id": "C2d:3-H=Y=ZcI:RlzSA+",
"inputs": {
"TEXT": {
"shadow": {
"type": "text",
"id": "rDKLD?S[.4^HZ(g*vX^y",
"fields": {
"TEXT": "W - forward; S backward; Space - Jump"
}
}
},
"DURATION": {
"shadow": {
"type": "math_number",
"id": "V.~W]u#$|n]}Z.DA(vN3",
"fields": {
"NUM": 30
}
}
},
"COLOR": {
"shadow": {
"type": "colour",
"id": "T=:hhU7UFGsFu|G!a8J!",
"fields": {
"COLOR": "#000080"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
]
}
};
Blockly.serialization.workspaces.load(blocklyJson, workspace);
function fitWorkspaceToBlocks(workspace) {
const blocksBoundingBox = workspace.getBlocksBoundingBox();
if (blocksBoundingBox) {
console.log("Blocks bounding box:", blocksBoundingBox);
// Account for workspace scale (zoom level)
const scale = workspace.scale; // Typically 0.75, 1, etc.
console.log("Workspace scale (zoom):", scale);
const width = (blocksBoundingBox.right - blocksBoundingBox.left) * scale;
const height = (blocksBoundingBox.bottom - blocksBoundingBox.top) * scale;
console.log("Calculated width (scaled):", width, "Calculated height (scaled):", height);
// Optional padding for better visual spacing
const padding = 20;
const totalWidth = Math.ceil(width + padding);
const totalHeight = Math.ceil(height + padding);
console.log("Total width with padding:", totalWidth, "Total height with padding:", totalHeight);
// Resize the parent container (blocklyDiv)
const blocklyDiv = document.getElementById('blocklyDiv');
if (blocklyDiv) {
console.log("Resizing blocklyDiv...");
blocklyDiv.style.width = `${totalWidth}px`;
blocklyDiv.style.height = `${totalHeight}px`;
} else {
console.warn("blocklyDiv not found. Ensure the container element exists.");
}
// Trigger Blockly to resize
console.log("Triggering Blockly SVG resize...");
Blockly.svgResize(workspace);
} else {
console.warn("No blocks bounding box found. Ensure the workspace contains blocks.");
}
}
// Call this function after workspace changes
fitWorkspaceToBlocks(workspace);