File size: 12,943 Bytes
fbe84a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <title>Custom Procedure Playground</title>
    <script src="../blockly_uncompressed_vertical.js"></script>
    <script src="../msg/messages.js"></script>
    <script src="../blocks_vertical/vertical_extensions.js"></script>
    <script src="../blocks_common/math.js"></script>
    <script src="../blocks_common/text.js"></script>
    <script src="../blocks_common/colour.js"></script>
    <script src="../blocks_vertical/control.js"></script>
    <script src="../blocks_vertical/event.js"></script>
    <script src="../blocks_vertical/motion.js"></script>
    <script src="../blocks_vertical/looks.js"></script>
    <script src="../blocks_vertical/procedures.js"></script>
    <script src="../blocks_vertical/operators.js"></script>
    <script src="../blocks_vertical/sound.js"></script>
    <script src="../blocks_vertical/sensing.js"></script>
    <script src="../blocks_vertical/data.js"></script>
  <style>

    body {

      background-color: #fff;

      font-family: sans-serif;

    }

    h1 {

      font-weight: normal;

      font-size: 140%;

    }

  </style>
</head>
<body>
  <p>
    <input type="button" value="Export to XML" onclick="toXml()">
    &nbsp;
    <input type="button" value="Import from XML" onclick="fromXml()" id="import">
    <br>
    <textarea id="importExport" style="width: 26%; height: 12em"

      onchange="taChange();" onkeyup="taChange()"></textarea>
  </p>

  <table width="100%">
    <tr>
      <td>
        <div id="secondaryDiv" style="height: 480px; width: 600px;"></div>
      </td>
      <td>
        <div id="primaryDiv" style="height: 480px; width: 600px;"></div>
      </td>
    </tr>
    <tr>
      <td></td>
      <td id="editor-actions">
        <button id="text_number" onclick="addTextNumber()">Add text/number input</button>
        <button id="boolean" onclick="addBoolean()">Add boolean input</button>
        <button id="label" onclick="addLabel()">Add label</button>
        <button id="cancelButton" onclick="cancel()">cancel</button>
        <button id="okButton" onclick="applyMutation()">ok</button>
        <button id="rndButton" onclick="removeRandomInput()">remove random input</button>
        <button id="addRndButton" onclick="addRandomInput()">add random input</button>
        <button id="setOutputString" onclick="setOutput('string')">set output to string</button>
        <button id="setOutputNumber" onclick="setOutput('number')">set output to number</button>
        <button id="setOutputBoolean" onclick="setOutput('boolean')">set output to boolean</button>
        <button id="setStatement" onclick="setStatement('statement')">set statement to normal</button>
        <button id="setStatementEnd" onclick="setStatement('end')">set statement to end</button>
        <input type="file" id="customIcon" onchange="changeIcon()" accept=".svg, .png, .bmp, .jpg, .jpeg, .jfif, .webp, .gif"/>
        <button id="removeIcon" onclick="removeIcon()">remove icon</button>
        <input type="color" id="blockColorPrimary" onchange="setColor()">
        <input type="color" id="blockColorSecondary" onchange="setColor()">
        <input type="color" id="blockColorTertiary" onchange="setColor()">
        <button id="removeIcon" onclick="removeColor()">reset color</button>
      </td>
    </tr>
  </table>

  <xml id="mutator_blocks" style="display:none">
    <block type="procedures_declaration" x="25" y="25" deletable="false">
    <mutation proccode="say %s %n times if %b" argumentnames="[&quot;something&quot;,&quot;this many&quot;,&quot;this is true&quot;]" argumentdefaults="[&quot;&quot;,1,false]" warp="false" argumentids="[&quot;a42&quot;, &quot;b23&quot;, &quot;c99&quot;]"/>
    </block>
  </xml>

  <xml id="main_ws_blocks" style="display:none">
    <block id="]){\{Y!7N9ezN+j@Vr`8p" type="procedures_definition" x="25" y="25">
      <statement name="custom_block">
          <shadow type="procedures_prototype" id="caller_internal">
              <mutation proccode="say %s %n times if %b" argumentnames="[&quot;something&quot;,&quot;this many&quot;,&quot;this is true&quot;]" argumentdefaults="[&quot;&quot;,1,false]" warp="false" argumentids="[&quot;a42&quot;, &quot;b23&quot;, &quot;c99&quot;]"/>
          </shadow>
      </statement>
      <next>
          <block id="caller_external" type="procedures_call">
              <mutation proccode="say %s %n times if %b" argumentnames="[&quot;something&quot;,&quot;this many&quot;,&quot;this is true&quot;]" argumentdefaults="[&quot;&quot;,1,false]" warp="false" argumentids="[&quot;a42&quot;, &quot;b23&quot;, &quot;c99&quot;]"/>
              <value name="input0">
                  <shadow id="V0~M:TxRk0Ua%osjGzh," type="text">
                      <field name="TEXT"/></field>
                  </shadow>
              </value>
              <value name="input1">
                  <shadow id="uPx3si(KkbL1)-XpbXoQ" type="math_number">
                      <field name="NUM">1</field>
                  </shadow>
              </value>
          </block>
      </next>
  </block>
  </xml>

  <xml id="main_ws_blocks_simpler" style="display:none">
    <block id="]){\{Y!7N9ezN+j@Vr`8p" type="procedures_definition" x="25" y="25">
      <statement name="custom_block">
          <shadow type="procedures_prototype" id="caller_internal">
              <mutation proccode="say %s %n times if %b" argumentnames="[&quot;something&quot;,&quot;this many&quot;,&quot;this is true&quot;]" argumentdefaults="[&quot;&quot;,1,false]" warp="false" argumentids="[&quot;a42&quot;, &quot;b23&quot;, &quot;c99&quot;]"/>
          </shadow>
      </statement>
      <next>
          <block id="caller_external" type="procedures_call">
          </block>
      </next>
  </block>
  </xml>

  <xml id="main_ws_blocks_simplest" style="display:none">
    <block id="]){\{Y!7N9ezN+j@Vr`8p" type="procedures_definition" x="25" y="25">
      <statement name="custom_block">
          <shadow type="procedures_prototype" id="caller_internal">
              <mutation proccode="my first function" argumentnames="[]" argumentdefaults="[]" warp="false" argumentids="[]"/>
          </shadow>
      </statement>
      <next>
          <block id="caller_external" type="procedures_call">
            <mutation proccode="my first function" argumentnames="[]" argumentdefaults="[]" warp="false" argumentids="[]"/>
          </block>
      </next>
  </block>
  </xml>

  <xml id="mutator_blocks_simplest" style="display:none">
    <block type="procedures_declaration" x="25" y="25" deletable="false">
    <mutation proccode="my first function" argumentnames="[]" argumentdefaults="[]" warp="false" argumentids="[]"/>
    </block>
  </xml>

  <xml id="toolbox" style="display:none">
    <category name="More" colour="#FF6680" secondaryColour="#FF4D6A" custom="PROCEDURE">
    </category>
    <category name="Values">
      <block type="operator_round">
        <value name="NUM">
          <shadow type="math_number">
            <field name="NUM"></field>
          </shadow>
        </value>
      </block>
    </category>
    <category name="Statements">
      <block type="control_forever"></block>
      <block type="control_if"></block>
      <block type="sound_stopallsounds"></block>
      <block type="control_start_as_clone"></block>
      <block type="control_stop">
        <mutation hasnext="false"></mutation>
        <field name="STOP_OPTION">all</field>
      </block>
    </category>
  </xml>

  <script>

    var editorActions = document.getElementById('editor-actions');

    editorActions.style.visibility = 'hidden';



    var callback = null;

    var mutationRoot = null;



    var declarationWorkspace = Blockly.inject('primaryDiv',

        {media: '../media/'});



    declarationWorkspace.addChangeListener(function() {

      if (mutationRoot) {

        mutationRoot.onChangeFn();

      }

    });



    var definitionWorkspace = Blockly.inject('secondaryDiv', {

      media: '../media/',

      toolbox: document.getElementById('toolbox'),

      zoom: {

        startScale: 0.75

      }

    });



    Blockly.Xml.clearWorkspaceAndLoadFromXml(document.getElementById('main_ws_blocks_simplest'),

        definitionWorkspace);



    Blockly.Procedures.externalProcedureDefCallback = function (mutation, cb) {

      var primary = document.getElementById('blockColorPrimary')

      var secondary = document.getElementById('blockColorSecondary')

      var tertiary = document.getElementById('blockColorTertiary')

      editorActions.style.visibility = 'visible';

      callback = cb;

      declarationWorkspace.clear();

      mutationRoot = declarationWorkspace.newBlock('procedures_declaration');

      mutationRoot.domToMutation(mutation);

      mutationRoot.initSvg();

      mutationRoot.render(false);

      var color = mutationRoot.color

      primary.value = color[0]

      secondary.value = color[1]

      tertiary.value = color[2]

    }



    function applyMutation() {

      var mutation = mutationRoot.mutationToDom(/* opt_generateShadows */ true)

      console.log(mutation);

      callback(mutation);

      callback = null;

      mutationRoot = null;

      declarationWorkspace.clear();

      definitionWorkspace.refreshToolboxSelection_()

      editorActions.style.visibility = 'hidden';

    }



    function addLabel() {

      mutationRoot.addLabelExternal();

    }



    function addBoolean() {

      mutationRoot.addBooleanExternal();

    }



    function addTextNumber() {

      mutationRoot.addStringNumberExternal();

    }



    function setOutput(type) {

      mutationRoot.setReturns(true);

      mutationRoot.setType(type);

    }



    function setStatement(type) {

      mutationRoot.setReturns(false);

      mutationRoot.setType(type);

    }



    function addTextNumber() {

      mutationRoot.addStringNumberExternal();

    }



    function changeIcon() {

      var fileReader = new FileReader();

      fileReader.onload = e => {

        mutationRoot.setImage(e.target.result);

      };

      var fileElement = document.getElementById('customIcon')

      var file = fileElement.files[0]

      fileReader.readAsDataURL(file)

      fileElement.value = null

    }



    function removeIcon() {

      mutationRoot.unsetImage()

    }



    function setColor() {

      var primary = document.getElementById('blockColorPrimary').value

      var secondary = document.getElementById('blockColorSecondary').value

      var tertiary = document.getElementById('blockColorTertiary').value

      mutationRoot.setColor(primary, secondary, tertiary)

    }



    function removeColor() {

      mutationRoot.removeColor()

    }



    function removeRandomInput() {

      var rnd = Math.floor(Math.random() * mutationRoot.inputList.length);

      mutationRoot.removeInput(mutationRoot.inputList[rnd].name);

      mutationRoot.onChangeFn();

      mutationRoot.updateDisplay_();

    }



    function addRandomInput() {

      var rnd = Math.floor(Math.random() * 3);

      switch (rnd) {

        case 0:

          addTextNumber();

          break;

        case 1:

          addLabel();

          break;

        case 2:

          addBoolean();

          break;

      }

    }



    function cancel() {

      callback = null;

      mutationRoot = null;

      declarationWorkspace.clear();

      definitionWorkspace.refreshToolboxSelection_()

      editorActions.style.visibility = 'hidden';

    }



    if (sessionStorage) {

      // Restore previously displayed text.

      var text = sessionStorage.getItem('textarea');

      if (text) {

        document.getElementById('importExport').value = text;

      }

      taChange();

    }



    function toXml() {

      var output = document.getElementById('importExport');

      var xml = Blockly.Xml.workspaceToDom(definitionWorkspace);

      output.value = Blockly.Xml.domToPrettyText(xml);

      output.focus();

      output.select();

      taChange();

    }



    function fromXml() {

      var input = document.getElementById('importExport');

      var xml = Blockly.Xml.textToDom(input.value);

      Blockly.Xml.domToWorkspace(xml, definitionWorkspace);

      taChange();

    }



    function taChange() {

      var textarea = document.getElementById('importExport');

      if (sessionStorage) {

        sessionStorage.setItem('textarea', textarea.value)

      }

      var valid = true;

      try {

        Blockly.Xml.textToDom(textarea.value);

      } catch (e) {

        valid = false;

      }

      document.getElementById('import').disabled = !valid;

    }

  </script>

</body>
</html>