Advertisement
Guest User

blockly

a guest
Oct 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. block.js
  2. Blockly.Blocks['print'] = {
  3. init: function() {
  4. this.appendValueInput("input")
  5. .setCheck("String")
  6. .appendField("print");
  7. this.setInputsInline(true);
  8. this.setPreviousStatement(true, null);
  9. this.setNextStatement(true, null);
  10. this.setColour(230);
  11. this.setTooltip("This block displays an alert box with the specified message");
  12. this.setHelpUrl("");
  13. }
  14. };
  15.  
  16. Blockly.Blocks['text_input'] = {
  17. init: function() {
  18. this.appendDummyInput()
  19. .appendField("\"")
  20. .appendField(new Blockly.FieldTextInput("text"), "NAME")
  21. .appendField("\"");
  22. this.setInputsInline(true);
  23. this.setOutput(true, "String");
  24. this.setColour(180);
  25. this.setTooltip("This block displays the text typed by the user");
  26. this.setHelpUrl("");
  27. }
  28. };
  29.  
  30. generator
  31. Blockly.JavaScript['print'] = function(block) {
  32. var value_input = Blockly.JavaScript.valueToCode(block, 'input', Blockly.JavaScript.ORDER_ATOMIC);
  33. var code = 'alert('+ value_input+');\n';
  34. return code;
  35. };
  36.  
  37. Blockly.JavaScript['text_input'] = function(block) {
  38. var text_name = block.getFieldValue('NAME');
  39. var code = '"'+text_name+'"';
  40. return [code, Blockly.JavaScript.ORDER_ATOMIC];
  41. };
  42.  
  43. index.html
  44.  
  45. <!DOCTYPE html>
  46. <html>
  47. <head>
  48. <meta charset="utf-8">
  49. <title>Blockly Demo</title>
  50. <script src="https://aframe.io/releases/0.5.0/aframe.min.js"></script>
  51. <script src="blockly_compressed.js"></script>
  52. <script src="blocks_compressed.js"></script>
  53. <script src="javascript_compressed.js"></script>
  54. <script src="en.js"></script>
  55. <script src="blocks.js"></script>
  56. <script src="generator_stub.js"></script>
  57. <style>
  58. body {
  59. background-color: #fff;
  60. font-family: sans-serif;
  61. }
  62. h1 {
  63. font-weight: normal;
  64. font-size: 140%;
  65. }
  66. a-scene {
  67. height: 700px;
  68. width: auto;
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <p>
  74. <button onclick="showCode()">Show Code</button>
  75. <button onclick="runCode()">Run</button>
  76. </p>
  77. <table style="table-layout: auto; width: 100%">
  78. <tr>
  79. <td id="blocklyArea" width=50%>
  80. <div id="blocklyDiv" style="height: 700px; width:auto;"></div>
  81. </td>
  82. <td id ="GUI" width=50%>
  83. <div style="height: 700px; width:auto;">
  84. <a-scene embedded>
  85. <a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
  86. <a-sky color="#ECECEC"></a-sky>
  87. </a-scene>
  88. </div>
  89. </td>
  90. </tr>
  91. </table>
  92.  
  93. <xml id="toolbox" style="display: none">
  94. <category name="Basic blocks" colour="180">
  95. <block type="print"></block>
  96. <block type="text_input"></block>
  97. </category>
  98. </xml>
  99.  
  100. <script>
  101. var demoWorkspace = Blockly.inject('blocklyDiv',
  102. {toolbox: document.getElementById('toolbox')});
  103.  
  104. function updateRealTimeCodeGeneration(event) {
  105. var code = Blockly.JavaScript.workspaceToCode(demoWorkspace);
  106. }
  107.  
  108. demoWorkspace.addChangeListener(updateRealTimeCodeGeneration);
  109.  
  110. function showCode() {
  111. var code = Blockly.JavaScript.workspaceToCode(demoWorkspace);
  112. alert(code);
  113. }
  114.  
  115. function runCode() {
  116. // Generate JavaScript code and run it.
  117. window.LoopTrap = 1000;
  118. Blockly.JavaScript.INFINITE_LOOP_TRAP = 'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
  119. var code = Blockly.JavaScript.workspaceToCode(demoWorkspace);
  120. Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
  121. try {
  122. eval(code);
  123. } catch (e) {
  124. alert(e);
  125. }
  126. }
  127. </script>
  128. </body>
  129. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement