Skip to content

Commit

Permalink
add color blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SebCanet committed May 16, 2020
1 parent fd53b7d commit a9b5252
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
21 changes: 12 additions & 9 deletions www/blocklyduino/generators/arduino/blockly/colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ goog.require('Blockly.Arduino');

Blockly.Arduino['colour_picker'] = function (block) {
// Colour picker.
var code = '\'' + block.getFieldValue('COLOUR') + '\'';
var code = '\"' + block.getFieldValue('COLOUR') + '\"';
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino['colour_random'] = function (block) {
// Generate a random colour.
var functionName = Blockly.Arduino.provideFunction_(
'colourRandom',
var functionName = Blockly.Arduino.provideFunction_('colourRandom',
['function ' + Blockly.Arduino.FUNCTION_NAME_PLACEHOLDER_ + '() {',
' var num = Math.floor(Math.random() * Math.pow(2, 24));',
' return \'#\' + (\'00000\' + num.toString(16)).substr(-6);',
' String colorRand = "";',
' for (int i = 0; i < 6; i++) {',
' int randNum = random(0, 16);',
' colorRand += String(randNum, HEX);',
' }',
' return (\'#\' + colorRand);',
'}']);
var code = functionName + '()';
return [code, Blockly.Arduino.ORDER_FUNCTION_CALL];
Expand All @@ -56,10 +59,10 @@ Blockly.Arduino['colour_rgb'] = function (block) {
'colourRgb',
['String ' + Blockly.Arduino.FUNCTION_NAME_PLACEHOLDER_ +
'(int r, int g, int b) {',
' r = max(min(r, 100), 0) * 2.55;',
' g = max(min(g, 100), 0) * 2.55;',
' b = max(min(b, 100), 0) * 2.55;',
' return (\'#\' + String(r) + String(g) + String(b));',
' r = max(min(r * 2.55, 255), 0);',
' g = max(min(g * 2.55, 255), 0);',
' b = max(min(b * 2.55, 255), 0);',
' return (\'#\' + String(r, HEX) + String(g, HEX) + String(b, HEX));',
'}']);
var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
return [code, Blockly.Arduino.ORDER_FUNCTION_CALL];
Expand Down
7 changes: 0 additions & 7 deletions www/blocklyduino/js/addon/clipboard.min.js

This file was deleted.

1 change: 0 additions & 1 deletion www/blocklyduino/js/blockly/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ Code.init = function () {
Code.setBoard();
Code.initLanguage();
setOnOffLine();
var clipboard = new Clipboard(document.getElementById('copyCodeButton'));
var rtl = Code.isRtl();
//define resizable workspace
var container = document.getElementById('content_area');
Expand Down
19 changes: 19 additions & 0 deletions www/blocklyduino/js/buttons_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ if (document.fullscreenElement || document.webkitIsFullScreen || document.mozFul
fullScreen_ = false;
}
};


/**
* Copy code from div code_peek in clipboard system
*/
Code.copyToClipboard = function () {
if (document.selection) { // IE
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById("code_peek"));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById("code_peek"));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
document.execCommand("copy");
};

/**
* Undo/redo functions
*/
Expand Down
3 changes: 1 addition & 2 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<div id="separator" ></div>
<div id="code_peek">
<pre id="code_peek_content" class="code_peek_content prettyprint linenums"> </pre>
<button id="copyCodeButton" class="iconButtons" data-clipboard-action="copy" data-clipboard-target="#code_peek">
<button id="copyCodeButton" class="iconButtons" onclick="Code.copyToClipboard()">
<span id="copyCodeButton_span">
<i class="far fa-copy"></i>
</span>
Expand Down Expand Up @@ -301,7 +301,6 @@
<script src="./blocklyduino/js/blockly/keyboard_nav.js"></script>
<script src="./blocklyduino/js/blockly/playground.js"></script>
<!-- BlocklyDuino libraries of external functions -->
<script src="./blocklyduino/js/addon/clipboard.min.js"></script>
<script src="./blocklyduino/js/addon/diff.js"></script>
<script src="./blocklyduino/js/addon/prettify.js"></script>
<script src="./blocklyduino/js/buttons_functions.js"></script>
Expand Down

0 comments on commit a9b5252

Please sign in to comment.