Skip to content

Commit

Permalink
Merge pull request #69 from bmcage/transTypes
Browse files Browse the repository at this point in the history
Translate types
  • Loading branch information
carlosperate committed Apr 12, 2016
2 parents 1ad1ecb + 23a8060 commit a7985ad
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 42 deletions.
25 changes: 16 additions & 9 deletions blockly/core/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,27 @@ goog.require('goog.asserts');

/**
* Blockly Type class constructor.
* @param {Object} args Object/dictionary with typeName, and compatibleTypes.
* @param {Object} args Object/dictionary with typeId, and compatibleTypes.
* @constructor
*/
Blockly.Type = function(args) {
if ((args.typeName === undefined) || (args.compatibleTypes === undefined)) {
if ((args.typeId === undefined) || (args.typeName === undefined) ||
(args.compatibleTypes === undefined)) {
throw new Error('Creating a Type requires the following format:\n{\n' +
' typeName: string,\n' +
' typeId: string,\n' +
' typeName: function returning a string,\n' +
' compatibleTypes: [Blockly.Type,]\n}');
}
if (!goog.isArray(args.compatibleTypes)) {
throw new Error('The compatible types for a Blockly Types needs to be an ' +
'array of Blockly.Type items.');
}
/** @type {string} */
this.typeId = args.typeId;
/** @type {function}
This is the translatable name. As translation is only present past build,
pass a function to return translated string.
*/
this.typeName = args.typeName;
/**
* @type {Array<Blockly.Type>}
Expand All @@ -48,7 +55,7 @@ Blockly.Type = function(args) {
/** Getter for the output property, used for block output types. */
Object.defineProperty(Blockly.Type.prototype, 'output', {
get: function() {
return this.typeName;
return this.typeId;
},
set: function(value) {
console.warn('"Blockly.Type" property "output" is not allowed to be set.');
Expand All @@ -75,12 +82,12 @@ Blockly.Type.prototype.generateCheckList_ = function(compatibleType) {
for (var i = 0; i < this.compatibleTypes_.length; i++) {
var unique = true;
for (var j = 0; j < this.generatedCheckList_.length; j++) {
if (this.generatedCheckList_[j] === this.compatibleTypes_[i].typeName) {
if (this.generatedCheckList_[j] === this.compatibleTypes_[i].typeId) {
unique = false;
}
}
if (unique) {
this.generatedCheckList_.push(this.compatibleTypes_[i].typeName);
this.generatedCheckList_.push(this.compatibleTypes_[i].typeId);
}
}
};
Expand All @@ -92,7 +99,7 @@ Blockly.Type.prototype.generateCheckList_ = function(compatibleType) {
Blockly.Type.prototype.addCompatibleType = function(compatibleType) {
if (!compatibleType || !compatibleType.constructor ||
!(compatibleType instanceof Blockly.Type)) {
throw new Error('To add a compatible type to ' + this.typeName +
throw new Error('To add a compatible type to ' + this.typeId +
' provide a Blockly.Type object.');
}
this.compatibleTypes_.push(compatibleType);
Expand All @@ -107,12 +114,12 @@ Blockly.Type.prototype.addCompatibleType = function(compatibleType) {
Blockly.Type.prototype.addCompatibleTypes = function(compatibleTypeArray) {
if (!goog.isArray(compatibleTypeArray)) {
throw new Error('To add compatible types to the Blockly Type ' +
this.typeName +' provide an array of Blockly.Type items.');
this.typeId +' provide an array of Blockly.Type items.');
}
for (var i = 0; i < compatibleTypeArray.length; i++) {
if (!compatibleTypeArray[i] || !compatibleTypeArray[i].constructor ||
!(compatibleTypeArray[i] instanceof Blockly.Type)) {
throw new Error('To add a compatible type to ' + this.typeName + ' you ' +
throw new Error('To add a compatible type to ' + this.typeId + ' you ' +
'must point to a Blockly.Type object.');
}
this.compatibleTypes_.push(compatibleTypeArray[i]);
Expand Down
57 changes: 35 additions & 22 deletions blockly/core/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,50 @@ goog.require('Blockly.Type');

/** Single character. */
Blockly.Types.CHARACTER = new Blockly.Type({
typeName: 'Character',
typeId: 'Character',
typeName: function() {return Blockly.Msg.ARD_TYPE_CHAR;},
compatibleTypes: []
});

/** Text string. */
Blockly.Types.TEXT = new Blockly.Type({
typeName: 'Text',
typeId: 'Text',
typeName: function() {return Blockly.Msg.ARD_TYPE_TEXT;},
compatibleTypes: [Blockly.Types.CHARACTER]
});

/** Boolean. */
Blockly.Types.BOOLEAN = new Blockly.Type({
typeName: 'Boolean',
typeId: 'Boolean',
typeName: function() {return Blockly.Msg.ARD_TYPE_BOOL;},
compatibleTypes: []
});

/** Short integer number. */
Blockly.Types.SHORT_NUMBER = new Blockly.Type({
typeName: 'Short Positive Number',
typeId: 'Short Positive Number',
typeName: function() {return Blockly.Msg.ARD_TYPE_SHORTPOS;},
compatibleTypes: [] // Circular dependencies, add after all declarations
});

/** Integer number. */
Blockly.Types.NUMBER = new Blockly.Type({
typeName: 'Number',
typeId: 'Number',
typeName: function() {return Blockly.Msg.ARD_TYPE_NUMBER;},
compatibleTypes: [] // Circular dependencies, add after all declarations
});

/** Large integer number. */
Blockly.Types.LARGE_NUMBER = new Blockly.Type({
typeName: 'Large Number',
typeId: 'Large Number',
typeName: function() {return Blockly.Msg.ARD_TYPE_LONG;},
compatibleTypes: [] // Circular dependencies, add after all declarations
});

/** Decimal/floating point number. */
Blockly.Types.DECIMAL = new Blockly.Type({
typeName: 'Decimal',
typeId: 'Decimal',
typeName: function() {return Blockly.Msg.ARD_TYPE_DECIMAL;},
compatibleTypes: [Blockly.Types.BOOLEAN,
Blockly.Types.SHORT_NUMBER,
Blockly.Types.NUMBER,
Expand All @@ -60,26 +67,30 @@ Blockly.Types.DECIMAL = new Blockly.Type({

/** Array/List of items. */
Blockly.Types.ARRAY = new Blockly.Type({
typeName: 'Array',
compatibleTypes: [],
typeId: 'Array',
typeName: function() {return Blockly.Msg.ARD_TYPE_ARRAY},
compatibleTypes: []
});

/** Null indicate there is no type. */
Blockly.Types.NULL = new Blockly.Type({
typeName: 'Null',
compatibleTypes: [],
typeId: 'Null',
typeName: function() {return Blockly.Msg.ARD_TYPE_NULL;},
compatibleTypes: []
});

/** Type not defined, or not yet defined. */
Blockly.Types.UNDEF = new Blockly.Type({
typeName: 'Undefined',
compatibleTypes: [],
typeId: 'Undefined',
typeName: function() {return Blockly.Msg.ARD_TYPE_UNDEF;},
compatibleTypes: []
});

/** Set when no child block (meant to define the variable type) is connected. */
Blockly.Types.CHILD_BLOCK_MISSING = new Blockly.Type({
typeName: 'ChildBlockMissing',
compatibleTypes: [],
typeId: 'ChildBlockMissing',
typeName: function() {return Blockly.Msg.ARD_TYPE_CHILDBLOCKMISSING;},
compatibleTypes: []
});

/**
Expand Down Expand Up @@ -107,19 +118,21 @@ Blockly.Types.LARGE_NUMBER.addCompatibleTypes([

/**
* Adds another type to the Blockly.Types collection.
* @param {string} typeName_ Identifiable name of the type.
* @param {string} typeId_ Identifiable name of the type.
* @param {string} typeName_ Descriptive name of the type for use in the UI.
* @param {Array<Blockly.Type>} compatibleTypes_ List of types this Type is
* compatible with.
*/
Blockly.Types.addType = function(typeName_, compatibleTypes_) {
// The name is used as the key from the value pair in the BlocklyTypes object
var key = typeName.toUpperCase().replace(/ /g, '_');
Blockly.Types.addType = function(typeId_, typeName_, compatibleTypes_) {
// The Id is used as the key from the value pair in the BlocklyTypes object
var key = typeId_.toUpperCase().replace(/ /g, '_');
if (Blockly.Types[key] !== undefined) {
throw 'The Blockly type ' + key + ' already exists.';
}
Blockly.Types[key] = new Blockly.Type({
typeName: typeName_,
compatibleTypes: compatibleTypes_,
typeId: typeId_,
typeName: function() {return typeName_;},
compatibleTypes: compatibleTypes_
});
};

Expand All @@ -135,7 +148,7 @@ Blockly.Types.getValidTypeArray = function() {
(typeKey !== 'NULL') && (typeKey !== 'ARRAY') &&
(typeof Blockly.Types[typeKey] !== 'function') &&
!(Blockly.Types[typeKey] instanceof RegExp)) {
typesArray.push([Blockly.Types[typeKey].typeName, typeKey]);
typesArray.push([Blockly.Types[typeKey].typeName(), typeKey]);
}
}
return typesArray;
Expand Down
22 changes: 11 additions & 11 deletions blockly/generators/arduino.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,26 +368,26 @@ Blockly.Arduino.scrub_ = function(block, code) {
* @private
*/
Blockly.Arduino.getArduinoType_ = function(typeBlockly) {
switch (typeBlockly.typeName) {
case Blockly.Types.SHORT_NUMBER.typeName:
switch (typeBlockly.typeId) {
case Blockly.Types.SHORT_NUMBER.typeId:
return 'char';
case Blockly.Types.NUMBER.typeName:
case Blockly.Types.NUMBER.typeId:
return 'int';
case Blockly.Types.LARGE_NUMBER.typeName:
case Blockly.Types.LARGE_NUMBER.typeId:
return 'long';
case Blockly.Types.DECIMAL.typeName:
case Blockly.Types.DECIMAL.typeId:
return 'float';
case Blockly.Types.TEXT.typeName:
case Blockly.Types.TEXT.typeId:
return 'String';
case Blockly.Types.CHARACTER.typeName:
case Blockly.Types.CHARACTER.typeId:
return 'char';
case Blockly.Types.BOOLEAN.typeName:
case Blockly.Types.BOOLEAN.typeId:
return 'boolean';
case Blockly.Types.NULL.typeName:
case Blockly.Types.NULL.typeId:
return 'void';
case Blockly.Types.UNDEF.typeName:
case Blockly.Types.UNDEF.typeId:
return 'undefined';
case Blockly.Types.CHILD_BLOCK_MISSING.typeName:
case Blockly.Types.CHILD_BLOCK_MISSING.typeId:
// If no block connected default to int, change for easier debugging
//return 'ChildBlockMissing';
return 'int';
Expand Down
11 changes: 11 additions & 0 deletions blockly/msg/json/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,17 @@
"PROCEDURES_IFRETURN_WARNING": "Waarschuwing: dit blok mag alleen gebruikt worden binnen de definitie van een functie.",
"REPLACE_EXISTING_BLOCKS": "Vervang aanwezige blokken? \"Annuleren\" zal blokken samenvoegen",
"WHAT_NAME_FOR_FILE": "Welke naam wil je geven aan je bestand?",
"ARD_TYPE_CHAR": "Letter",
"ARD_TYPE_TEXT": "Tekst",
"ARD_TYPE_BOOL": "Bool (0 of 1)",
"ARD_TYPE_SHORTPOS": "Kort Positief Nummer",
"ARD_TYPE_NUMBER": "Nummer",
"ARD_TYPE_LONG": "Groot Nummer",
"ARD_TYPE_DECIMAL": "Decimaal Getal",
"ARD_TYPE_ARRAY": "Lijst",
"ARD_TYPE_NULL": "Geen Type",
"ARD_TYPE_UNDEF": "Ongedefineerd",
"ARD_TYPE_CHILDBLOCKMISSING": "ChildBlockMissing",
"ARD_HIGH": "HOOG",
"ARD_LOW": "LAAG",
"ARD_ANALOGREAD": "lees analoge pin#",
Expand Down
11 changes: 11 additions & 0 deletions blockly/msg/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,17 @@ Blockly.Msg.PROCEDURES_IFRETURN_HELPURL = 'http://c2.com/cgi/wiki?GuardClause';
/// warning - This appears if the user tries to use this block outside of a function definition.
Blockly.Msg.PROCEDURES_IFRETURN_WARNING = 'Warning: This block may be used only within a function definition.';
//Arduino blocks define as a specific type
Blockly.Msg.ARD_TYPE_CHAR = 'Character';
Blockly.Msg.ARD_TYPE_TEXT = 'Text';
Blockly.Msg.ARD_TYPE_BOOL = 'Boolean';
Blockly.Msg.ARD_TYPE_SHORTPOS = 'Short Positive Number';
Blockly.Msg.ARD_TYPE_NUMBER = 'Number';
Blockly.Msg.ARD_TYPE_LONG = 'Large Number';
Blockly.Msg.ARD_TYPE_DECIMAL = 'Decimal';
Blockly.Msg.ARD_TYPE_ARRAY = 'Array';
Blockly.Msg.ARD_TYPE_NULL = 'Null';
Blockly.Msg.ARD_TYPE_UNDEF = 'Undefined';
Blockly.Msg.ARD_TYPE_CHILDBLOCKMISSING = 'ChildBlockMissing';
Blockly.Msg.ARD_HIGH = 'HIGH';
Blockly.Msg.ARD_LOW = 'LOW';
Blockly.Msg.ARD_ANALOGREAD = 'read analog pin#';
Expand Down

0 comments on commit a7985ad

Please sign in to comment.