File: IO.js

package info (click to toggle)
kuttypy 2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,896 kB
  • sloc: python: 58,651; javascript: 14,686; xml: 5,767; ansic: 2,716; makefile: 453; asm: 254; sh: 48
file content (275 lines) | stat: -rw-r--r-- 9,828 bytes parent folder | download
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
var gauges = [];

/*---print statememt----*/
Blockly.Blocks['cs_print'] = {
  init: function() {
    this.appendValueInput("TEXT")
        .setCheck(null)
        .appendField("Print");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(230);
 this.setTooltip("Print to screen");
 this.setHelpUrl("");
  }
};



Blockly.JavaScript['cs_print'] = function(block) {
  var txt = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE);
  var code = 'sleep(0.0001);print('+txt+');\n';
  return code;
};


Blockly.Python['cs_print'] = function(block) {
  var txt = Blockly.Python.valueToCode(block, 'TEXT', Blockly.Python.ORDER_NONE);
  var code = 'print('+txt+')\n';
  return code;
};


/*------ Fixed label in Registers area -----*/

/*---print statememt----*/
Blockly.Blocks['cs_sticker'] = {
  init: function() {
    this.appendValueInput("LABEL")
        .setCheck(null)
        .appendField("Label:");
    this.appendValueInput("TEXT")
        .setCheck(null)
        .appendField("Print:");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(230);
 this.setTooltip("Print to screen");
 this.setHelpUrl("");
  }
};

Blockly.JavaScript['cs_sticker'] = function(block) {
  var label = Blockly.JavaScript.valueToCode(block, 'LABEL', Blockly.JavaScript.ORDER_NONE);
  var txt = Blockly.JavaScript.valueToCode(block, 'TEXT', Blockly.JavaScript.ORDER_NONE);
  var code = 'sleep(0.0001);sticker('+label+','+txt+');\n';
  return code;
};


Blockly.Python['cs_sticker'] = function(block) {
  var label = Blockly.Python.valueToCode(block, 'LABEL', Blockly.Python.ORDER_NONE);
  var txt = Blockly.Python.valueToCode(block, 'TEXT', Blockly.Python.ORDER_NONE);
  var code = 'print('+label+','+txt+')\n';
  return code;
};


// -------------- Search string

/*---print statememt----*/
Blockly.Blocks['cs_search'] = {
  init: function() {
    this.appendValueInput("CHILD")
        .setAlign(Blockly.ALIGN_RIGHT)
        .setCheck(null)
        .appendField("Find position of");
    this.appendValueInput("PARENT")
        .setAlign(Blockly.ALIGN_RIGHT)
        .setCheck(null)
        .appendField("in a");
    this.appendDummyInput()
        .appendField("-1 if not found.")
    this.setOutput(true, null);
    this.setInputsInline(false);
    this.setColour(155);
 this.setTooltip("substring");
 this.setHelpUrl("");
  }
};

Blockly.JavaScript['cs_search'] = function(block) {
  var parent = Blockly.JavaScript.valueToCode(block, 'PARENT', Blockly.JavaScript.ORDER_NONE);
  var child = Blockly.JavaScript.valueToCode(block, 'CHILD', Blockly.JavaScript.ORDER_NONE);
  var code = parent+'.indexOf('+child+')';
  return [code, Blockly.JavaScript.ORDER_NONE];
};


Blockly.Python['cs_search'] = function(block) {
  var parent = Blockly.Python.valueToCode(block, 'PARENT', Blockly.Python.ORDER_NONE);
  var child = Blockly.Python.valueToCode(block, 'CHILD', Blockly.Python.ORDER_NONE);
  var code = parent+'.indexOf('+child+')';
  return [code, Blockly.Python.ORDER_NONE];
};


/*------ Fixed Gauge in Registers area -----

MIT License
Copyright (c) 2020 rotvalli
*/

Blockly.Blocks['cs_gauge'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Gauge Name:")
        .appendField(new Blockly.FieldTextInput("mygauge"), "ID");
    this.appendValueInput("VALUE")
        .setCheck(null)
        .appendField("Min:")
        .appendField(new Blockly.FieldNumber(0, -50000, 50000, 1), "MIN")
        .appendField("Max:")
        .appendField(new Blockly.FieldNumber(100, -50000, 50000, 1), "MAX")
        .appendField("Value:");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(230);
 this.setTooltip("Print to screen");
 this.setHelpUrl("");
  }
};

Blockly.JavaScript['cs_gauge'] = function(block) {
  var label = block.getFieldValue('ID');
  var mn = block.getFieldValue('MIN');
  var mx = block.getFieldValue('MAX');
  var val = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
  var code = 'sleep(0.0001);setGauge(\''+label+'\','+val+','+mn+','+mx+');\n';
  return code;
};


Blockly.Python['cs_gauge'] = function(block) {
  var label = block.getFieldValue('ID');
  var val = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_NONE);
  var code = 'print('+label+','+val+')\n';
  return code;
};

function addGauge(id){
        gaugearea.append(`
        <div class="item">
                <div id="${id}" class="gauge" style="
                --gauge-bg: #088478;
                --gauge-value:0;
                --gauge-display-value:0;">

                <div class="ticks">
                    <div class="tithe" style="--gauge-tithe-tick:1;"></div>
                    <div class="tithe" style="--gauge-tithe-tick:2;"></div>
                    <div class="tithe" style="--gauge-tithe-tick:3;"></div>
                    <div class="tithe" style="--gauge-tithe-tick:4;"></div>
                    <div class="tithe" style="--gauge-tithe-tick:6;"></div>
                    <div class="tithe" style="--gauge-tithe-tick:7;"></div>
                    <div class="tithe" style="--gauge-tithe-tick:8;"></div>
                    <div class="tithe" style="--gauge-tithe-tick:9;"></div>
                    <div class="min"></div>
                    <div class="mid"></div>
                    <div class="max"></div>
                </div>
                <div class="tick-circle"></div>

                <div class="needle">
                    <div class="needle-head"></div>
                </div>
                <div class="labels">
                    <div class="value-label"></div>
                </div>
            </div>
        </div>`);
        return true;
}
// Gauge
function updateGauge(id, value, min, max) {
    const newGaugeValue = Math.floor(((value - min) / (max - min)) * 100);
    document.getElementById(id).style.setProperty('--gauge-display-value', Math.floor(value));
    document.getElementById(id).style.setProperty('--gauge-value', newGaugeValue);
}


function savePNG(){
    var scaleFactor = 1;
    //Any modifications are executed on a deep copy of the element
    var cp = Blockly.mainWorkspace.svgBlockCanvas_.cloneNode(true);
    cp.removeAttribute("width");
    cp.removeAttribute("height");
    cp.removeAttribute("transform");

    var styleElem = document.createElementNS("http://www.w3.org/2000/svg", "style");
    //I've manually pasted codethemicrobit.com's CSS for blocks in here, but that can be removed as necessary
    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;}";
    cp.insertBefore(styleElem, cp.firstChild);

    //Creates a complete SVG document with the correct bounds (it is necessary to get the viewbox right, in the case of negative offsets)
    var bbox = Blockly.mainWorkspace.svgBlockCanvas_.getBBox();
    var xml = new XMLSerializer().serializeToString(cp);
    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>';
    //If you just want the SVG then do console.log(xml)
    //Otherwise we render as an image and export to PNG
    var svgBase64 = "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(xml)));
    var img = document.createElement('img');
    img.src = svgBase64;

    var canvas = document.createElement("canvas");
    canvas.width = Math.ceil(bbox.width) * scaleFactor;
    canvas.height = Math.ceil(bbox.height) * scaleFactor;
    var ctx = canvas.getContext('2d');
    ctx.scale(scaleFactor, scaleFactor);

    ctx.drawImage(img, 0, 0);
    //Opens the PNG image in a new tab for copying/saving
    window.open(canvas.toDataURL(), '_blank');
}



function initIO(interpreter, scope) {
        gauges = [];
        gaugearea.empty();

		  // Add an API function for the (alert) print() block.
		  var wrapper = function(text) {
			return results.html(results.html()+text+"<br>");
			//return document.getElementById("resulttext").innerHTML+=text+"<br>";
		  };
		  interpreter.setProperty(scope, 'alert',
			  interpreter.createNativeFunction(wrapper));
		  interpreter.setProperty(scope, 'print',
			  interpreter.createNativeFunction(wrapper));

		  // Add an API function for the sticker() block.
		  var wrapper = function(label, text) {
			return showReg(label, text);
		  };
		  interpreter.setProperty(scope, 'sticker',
			  interpreter.createNativeFunction(wrapper));


		  // Add an API function for the gauge() block.
		  var wrapper = function(label, val, min, max) {
		    if(!gauges.includes(label) ){
		        addGauge(label, val, min, max);
		        gauges.push(label);
                updateGauge(label, val, min, max);
		    }else{
                updateGauge(label, val, min, max);
			}
		  };
		  interpreter.setProperty(scope, 'setGauge',
			  interpreter.createNativeFunction(wrapper));

		  // Add an API function for the prompt() block.
		  var wrapper = function(text) {
			text = text ? text.toString() : '';
			return interpreter.createPrimitive(prompt(text));
		  };

		  interpreter.setProperty(scope, 'prompt',
			  interpreter.createNativeFunction(wrapper));


	}