Debian Codegraph @

online/static/blocks/IO.js

raw symbols SCIP
1 var gauges = [];
2
3 /*---print statememt----*/
4 Blockly.Blocks['cs_print'] = {
5 init: function() {
6 this.appendValueInput("TEXT")
7 .setCheck(null)
8 .appendField("Print");
9 this.setPreviousStatement(true, null);
10 this.setNextStatement(true, null);
11 this.setColour(230);
12 this.setTooltip("Print to screen");
13 this.setHelpUrl("");
14 }
15 };
16
17
18
19 Blockly.JavaScript['cs_print'] = function(block) {
20 var txt = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE);
21 var code = 'sleep(0.0001);print('+txt+');\n';
22 return code;
23 };
24
25
26 Blockly.Python['cs_print'] = function(block) {
27 var txt = Blockly.Python.valueToCode(block, 'TEXT', Blockly.Python.ORDER_NONE);
28 var code = 'print('+txt+')\n';
29 return code;
30 };
31
32
33 /*------ Fixed label in Registers area -----*/
34
35 /*---print statememt----*/
36 Blockly.Blocks['cs_sticker'] = {
37 init: function() {
38 this.appendValueInput("LABEL")
39 .setCheck(null)
40 .appendField("Label:");
41 this.appendValueInput("TEXT")
42 .setCheck(null)
43 .appendField("Print:");
44 this.setPreviousStatement(true, null);
45 this.setNextStatement(true, null);
46 this.setColour(230);
47 this.setTooltip("Print to screen");
48 this.setHelpUrl("");
49 }
50 };
51
52 Blockly.JavaScript['cs_sticker'] = function(block) {
53 var label = Blockly.JavaScript.valueToCode(block, 'LABEL', Blockly.JavaScript.ORDER_NONE);
54 var txt = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE);
55 var code = 'sleep(0.0001);sticker('+label+','+txt+');\n';
56 return code;
57 };
58
59
60 Blockly.Python['cs_sticker'] = function(block) {
61 var label = Blockly.Python.valueToCode(block, 'LABEL', Blockly.Python.ORDER_NONE);
62 var txt = Blockly.Python.valueToCode(block, 'TEXT', Blockly.Python.ORDER_NONE);
63 var code = 'print('+label+','+txt+')\n';
64 return code;
65 };
66
67
68 // -------------- Search string
69
70 /*---print statememt----*/
71 Blockly.Blocks['cs_search'] = {
72 init: function() {
73 this.appendValueInput("CHILD")
74 .setAlign(Blockly.ALIGN_RIGHT)
75 .setCheck(null)
76 .appendField("Find position of");
77 this.appendValueInput("PARENT")
78 .setAlign(Blockly.ALIGN_RIGHT)
79 .setCheck(null)
80 .appendField("in a");
81 this.appendDummyInput()
82 .appendField("-1 if not found.")
83 this.setOutput(true, null);
84 this.setInputsInline(false);
85 this.setColour(155);
86 this.setTooltip("substring");
87 this.setHelpUrl("");
88 }
89 };
90
91 Blockly.JavaScript['cs_search'] = function(block) {
92 var parent = Blockly.JavaScript.valueToCode(block, 'PARENT', Blockly.JavaScript.ORDER_NONE);
93 var child = Blockly.JavaScript.valueToCode(block, 'CHILD', Blockly.JavaScript.ORDER_NONE);
94 var code = parent+'.indexOf('+child+')';
95 return [code, Blockly.JavaScript.ORDER_NONE];
96 };
97
98
99 Blockly.Python['cs_search'] = function(block) {
100 var parent = Blockly.Python.valueToCode(block, 'PARENT', Blockly.Python.ORDER_NONE);
101 var child = Blockly.Python.valueToCode(block, 'CHILD', Blockly.Python.ORDER_NONE);
102 var code = parent+'.indexOf('+child+')';
103 return [code, Blockly.Python.ORDER_NONE];
104 };
105
106
107 /*------ Fixed Gauge in Registers area -----
108
109 MIT License
110 Copyright (c) 2020 rotvalli
111 */
112
113 Blockly.Blocks['cs_gauge'] = {
114 init: function() {
115 this.appendDummyInput()
116 .appendField("Gauge Name:")
117 .appendField(new Blockly.FieldTextInput("mygauge"), "ID");
118 this.appendValueInput("VALUE")
119 .setCheck(null)
120 .appendField("Min:")
121 .appendField(new Blockly.FieldNumber(0, -50000, 50000, 1), "MIN")
122 .appendField("Max:")
123 .appendField(new Blockly.FieldNumber(100, -50000, 50000, 1), "MAX")
124 .appendField("Value:");
125 this.setPreviousStatement(true, null);
126 this.setNextStatement(true, null);
127 this.setColour(230);
128 this.setTooltip("Print to screen");
129 this.setHelpUrl("");
130 }
131 };
132
133 Blockly.JavaScript['cs_gauge'] = function(block) {
134 var label = block.getFieldValue('ID');
135 var mn = block.getFieldValue('MIN');
136 var mx = block.getFieldValue('MAX');
137 var val = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
138 var code = 'sleep(0.0001);setGauge(\''+label+'\','+val+','+mn+','+mx+');\n';
139 return code;
140 };
141
142
143 Blockly.Python['cs_gauge'] = function(block) {
144 var label = block.getFieldValue('ID');
145 var val = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_NONE);
146 var code = 'print('+label+','+val+')\n';
147 return code;
148 };
149
150 function addGauge(id){
151 gaugearea.append(`
152 <div class="item">
153 <div id="${id}" class="gauge" style="
154 --gauge-bg: #088478;
155 --gauge-value:0;
156 --gauge-display-value:0;">
157
158 <div class="ticks">
159 <div class="tithe" style="--gauge-tithe-tick:1;"></div>
160 <div class="tithe" style="--gauge-tithe-tick:2;"></div>
161 <div class="tithe" style="--gauge-tithe-tick:3;"></div>
162 <div class="tithe" style="--gauge-tithe-tick:4;"></div>
163 <div class="tithe" style="--gauge-tithe-tick:6;"></div>
164 <div class="tithe" style="--gauge-tithe-tick:7;"></div>
165 <div class="tithe" style="--gauge-tithe-tick:8;"></div>
166 <div class="tithe" style="--gauge-tithe-tick:9;"></div>
167 <div class="min"></div>
168 <div class="mid"></div>
169 <div class="max"></div>
170 </div>
171 <div class="tick-circle"></div>
172
173 <div class="needle">
174 <div class="needle-head"></div>
175 </div>
176 <div class="labels">
177 <div class="value-label"></div>
178 </div>
179 </div>
180 </div>`);
181 return true;
182 }
183 // Gauge
184 function updateGauge(id, value, min, max) {
185 const newGaugeValue = Math.floor(((value - min) / (max - min)) * 100);
186 document.getElementById(id).style.setProperty('--gauge-display-value', Math.floor(value));
187 document.getElementById(id).style.setProperty('--gauge-value', newGaugeValue);
188 }
189
190
191 function savePNG(){
192 var scaleFactor = 1;
193 //Any modifications are executed on a deep copy of the element
194 var cp = Blockly.mainWorkspace.svgBlockCanvas_.cloneNode(true);
195 cp.removeAttribute("width");
196 cp.removeAttribute("height");
197 cp.removeAttribute("transform");
198
199 var styleElem = document.createElementNS("http://www.w3.org/2000/svg", "style");
200 //I've manually pasted codethemicrobit.com's CSS for blocks in here, but that can be removed as necessary
201 styleElem.textContent = ".blocklyToolboxDiv {background: rgba(0, 0, 0, 0.05);}.blocklyMainBackground {stroke:none !important;}.blocklyTreeLabel, .blocklyText, .blocklyHtmlInput {font-family:'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace !important;}.blocklyText { font-size:1rem !important;}.rtl .blocklyText {text-align:right;} .blocklyTreeLabel { font-size:1.25rem !important;} .blocklyCheckbox {fill: #ff3030 !important;text-shadow: 0px 0px 6px #f00;font-size: 17pt !important;}";
202 cp.insertBefore(styleElem, cp.firstChild);
203
204 //Creates a complete SVG document with the correct bounds (it is necessary to get the viewbox right, in the case of negative offsets)
205 var bbox = Blockly.mainWorkspace.svgBlockCanvas_.getBBox();
206 var xml = new XMLSerializer().serializeToString(cp);
207 xml = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'+bbox.width+'" height="'+bbox.height+'" viewBox="' + bbox.x + ' ' + bbox.y + ' ' + bbox.width + ' ' + bbox.height + '"><rect width="100%" height="100%" fill="white"></rect>'+xml+'</svg>';
208 //If you just want the SVG then do console.log(xml)
209 //Otherwise we render as an image and export to PNG
210 var svgBase64 = "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(xml)));
211 var img = document.createElement('img');
212 img.src = svgBase64;
213
214 var canvas = document.createElement("canvas");
215 canvas.width = Math.ceil(bbox.width) * scaleFactor;
216 canvas.height = Math.ceil(bbox.height) * scaleFactor;
217 var ctx = canvas.getContext('2d');
218 ctx.scale(scaleFactor, scaleFactor);
219
220 ctx.drawImage(img, 0, 0);
221 //Opens the PNG image in a new tab for copying/saving
222 window.open(canvas.toDataURL(), '_blank');
223 }
224
225
226
227 function initIO(interpreter, scope) {
228 gauges = [];
229 gaugearea.empty();
230
231 // Add an API function for the (alert) print() block.
232 var wrapper = function(text) {
233 return results.html(results.html()+text+"<br>");
234 //return document.getElementById("resulttext").innerHTML+=text+"<br>";
235 };
236 interpreter.setProperty(scope, 'alert',
237 interpreter.createNativeFunction(wrapper));
238 interpreter.setProperty(scope, 'print',
239 interpreter.createNativeFunction(wrapper));
240
241 // Add an API function for the sticker() block.
242 var wrapper = function(label, text) {
243 return showReg(label, text);
244 };
245 interpreter.setProperty(scope, 'sticker',
246 interpreter.createNativeFunction(wrapper));
247
248
249 // Add an API function for the gauge() block.
250 var wrapper = function(label, val, min, max) {
251 if(!gauges.includes(label) ){
252 addGauge(label, val, min, max);
253 gauges.push(label);
254 updateGauge(label, val, min, max);
255 }else{
256 updateGauge(label, val, min, max);
257 }
258 };
259 interpreter.setProperty(scope, 'setGauge',
260 interpreter.createNativeFunction(wrapper));
261
262 // Add an API function for the prompt() block.
263 var wrapper = function(text) {
264 text = text ? text.toString() : '';
265 return interpreter.createPrimitive(prompt(text));
266 };
267
268 interpreter.setProperty(scope, 'prompt',
269 interpreter.createNativeFunction(wrapper));
270
271
272 }
273
274
275