Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert some block generators to goog.module #5769

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4f54e62
refactor: convert generators/lua/colour.js to goog.module
rachel-fenichel Dec 1, 2021
a73a8bf
refactor: convert generators/lua/colour.js to named requires
rachel-fenichel Dec 1, 2021
2c1edc1
chore: run clang-format
rachel-fenichel Dec 1, 2021
35427e1
refactor: convert generators/lua/lists.js to goog.module
rachel-fenichel Dec 1, 2021
e0a5d08
refactor: convert generators/lua/lists.js to named requires
rachel-fenichel Dec 1, 2021
5f0a05a
chore: run clang-format
rachel-fenichel Dec 1, 2021
30b2556
fix: use getListIndex helper function in lua list generators
rachel-fenichel Dec 1, 2021
470402e
refactor: convert generators/lua/logic.js to goog.module
rachel-fenichel Dec 1, 2021
7062810
refactor: convert generators/lua/logic.js to named requires
rachel-fenichel Dec 1, 2021
d0c676b
chore: run clang-format
rachel-fenichel Dec 1, 2021
546cba9
refactor: convert generators/lua/loops.js to goog.module
rachel-fenichel Dec 1, 2021
6fe3712
refactor: convert generators/lua/loops.js to named requires
rachel-fenichel Dec 1, 2021
0a2a9e8
chore: run clang-format
rachel-fenichel Dec 1, 2021
f924397
refactor: convert generators/lua/math.js to goog.module
rachel-fenichel Dec 1, 2021
8813787
refactor: convert generators/lua/math.js to named requires
rachel-fenichel Dec 1, 2021
68e4636
chore: run clang-format
rachel-fenichel Dec 1, 2021
ac6b994
refcator: convert generators/lua/procedures.js to goog.module
rachel-fenichel Dec 1, 2021
2a21419
refactor: convert generators/lua/procedures.js to named requires
rachel-fenichel Dec 1, 2021
0b85e9e
chore: run clang-format
rachel-fenichel Dec 1, 2021
8392804
chore: rebuild deps.js
rachel-fenichel Dec 1, 2021
cf16d63
refactor: convert generators/lua/text.js to goog.module
rachel-fenichel Dec 1, 2021
c077bab
refactor: convert generators/lua/text.js to named requires
rachel-fenichel Dec 1, 2021
b9383e9
refactor: convert generators/lua/variables_dynamic.js to goog.module
rachel-fenichel Dec 1, 2021
ee797a1
refactor: convert generators/lua/variables_dynamic.js to named requires
rachel-fenichel Dec 1, 2021
13968ef
chore: run clang-format on text.js
rachel-fenichel Dec 1, 2021
bd29c02
refactor: convert generators/lua/variables.js to goog.module
rachel-fenichel Dec 1, 2021
586ac2e
refactor: convert generators/lua/variables.js to named requires
rachel-fenichel Dec 1, 2021
ca6e376
chore: run clang-format
rachel-fenichel Dec 1, 2021
14c86de
chore: make a lua generator function internal
rachel-fenichel Dec 1, 2021
ee4bea3
chore: rebuild deps.js
rachel-fenichel Dec 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 42 additions & 48 deletions generators/lua/colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,61 @@
*/
'use strict';

goog.provide('Blockly.Lua.colour');
goog.module('Blockly.Lua.colour');

goog.require('Blockly.Lua');
const Lua = goog.require('Blockly.Lua');


Blockly.Lua['colour_picker'] = function(block) {
Lua['colour_picker'] = function(block) {
// Colour picker.
const code = Blockly.Lua.quote_(block.getFieldValue('COLOUR'));
return [code, Blockly.Lua.ORDER_ATOMIC];
const code = Lua.quote_(block.getFieldValue('COLOUR'));
return [code, Lua.ORDER_ATOMIC];
};

Blockly.Lua['colour_random'] = function(block) {
Lua['colour_random'] = function(block) {
// Generate a random colour.
const code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
return [code, Blockly.Lua.ORDER_HIGH];
return [code, Lua.ORDER_HIGH];
};

Blockly.Lua['colour_rgb'] = function(block) {
Lua['colour_rgb'] = function(block) {
// Compose a colour from RGB components expressed as percentages.
const functionName = Blockly.Lua.provideFunction_(
'colour_rgb',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b)',
' r = math.floor(math.min(100, math.max(0, r)) * 2.55 + .5)',
' g = math.floor(math.min(100, math.max(0, g)) * 2.55 + .5)',
' b = math.floor(math.min(100, math.max(0, b)) * 2.55 + .5)',
' return string.format("#%02x%02x%02x", r, g, b)',
'end']);
const r = Blockly.Lua.valueToCode(block, 'RED',
Blockly.Lua.ORDER_NONE) || 0;
const g = Blockly.Lua.valueToCode(block, 'GREEN',
Blockly.Lua.ORDER_NONE) || 0;
const b = Blockly.Lua.valueToCode(block, 'BLUE',
Blockly.Lua.ORDER_NONE) || 0;
const functionName = Lua.provideFunction_('colour_rgb', [
'function ' + Lua.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b)',
' r = math.floor(math.min(100, math.max(0, r)) * 2.55 + .5)',
' g = math.floor(math.min(100, math.max(0, g)) * 2.55 + .5)',
' b = math.floor(math.min(100, math.max(0, b)) * 2.55 + .5)',
' return string.format("#%02x%02x%02x", r, g, b)', 'end'
]);
const r = Lua.valueToCode(block, 'RED', Lua.ORDER_NONE) || 0;
const g = Lua.valueToCode(block, 'GREEN', Lua.ORDER_NONE) || 0;
const b = Lua.valueToCode(block, 'BLUE', Lua.ORDER_NONE) || 0;
const code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
return [code, Blockly.Lua.ORDER_HIGH];
return [code, Lua.ORDER_HIGH];
};

Blockly.Lua['colour_blend'] = function(block) {
Lua['colour_blend'] = function(block) {
// Blend two colours together.
const functionName = Blockly.Lua.provideFunction_(
'colour_blend',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
'(colour1, colour2, ratio)',
' local r1 = tonumber(string.sub(colour1, 2, 3), 16)',
' local r2 = tonumber(string.sub(colour2, 2, 3), 16)',
' local g1 = tonumber(string.sub(colour1, 4, 5), 16)',
' local g2 = tonumber(string.sub(colour2, 4, 5), 16)',
' local b1 = tonumber(string.sub(colour1, 6, 7), 16)',
' local b2 = tonumber(string.sub(colour2, 6, 7), 16)',
' local ratio = math.min(1, math.max(0, ratio))',
' local r = math.floor(r1 * (1 - ratio) + r2 * ratio + .5)',
' local g = math.floor(g1 * (1 - ratio) + g2 * ratio + .5)',
' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
' return string.format("#%02x%02x%02x", r, g, b)',
'end']);
const colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
const colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
const ratio = Blockly.Lua.valueToCode(block, 'RATIO',
Blockly.Lua.ORDER_NONE) || 0;
const code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
return [code, Blockly.Lua.ORDER_HIGH];
const functionName = Lua.provideFunction_('colour_blend', [
'function ' + Lua.FUNCTION_NAME_PLACEHOLDER_ + '(colour1, colour2, ratio)',
' local r1 = tonumber(string.sub(colour1, 2, 3), 16)',
' local r2 = tonumber(string.sub(colour2, 2, 3), 16)',
' local g1 = tonumber(string.sub(colour1, 4, 5), 16)',
' local g2 = tonumber(string.sub(colour2, 4, 5), 16)',
' local b1 = tonumber(string.sub(colour1, 6, 7), 16)',
' local b2 = tonumber(string.sub(colour2, 6, 7), 16)',
' local ratio = math.min(1, math.max(0, ratio))',
' local r = math.floor(r1 * (1 - ratio) + r2 * ratio + .5)',
' local g = math.floor(g1 * (1 - ratio) + g2 * ratio + .5)',
' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
' return string.format("#%02x%02x%02x", r, g, b)', 'end'
]);
const colour1 =
Lua.valueToCode(block, 'COLOUR1', Lua.ORDER_NONE) || '\'#000000\'';
const colour2 =
Lua.valueToCode(block, 'COLOUR2', Lua.ORDER_NONE) || '\'#000000\'';
const ratio = Lua.valueToCode(block, 'RATIO', Lua.ORDER_NONE) || 0;
const code =
functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
return [code, Lua.ORDER_HIGH];
};
Loading